Button docs

This commit is contained in:
Cooldude2606
2019-08-27 21:08:48 +01:00
parent 32f41ff722
commit cb59cc0537
99 changed files with 982 additions and 138 deletions

View File

@@ -21,7 +21,7 @@ end)
-- Note that element might be nil if this is the first draw function
-- in this case button is a new concept so we know this is the first function and element is nil
if properties.type == 'button' then
element = parent.draw{
element = parent.add{
type = properties.type,
name = properties.name,
caption = properties.caption,
@@ -29,7 +29,7 @@ end)
}
else
element = parent.draw{
element = parent.add{
type = properties.type,
name = properties.name,
sprite = properties.sprite,
@@ -67,4 +67,7 @@ custom_button:draw(game.player.gui.left)
local Gui = require 'expcore.gui.core'
Gui.require_concept('frame')
Gui.require_concept('button')
return Gui

View File

@@ -0,0 +1,58 @@
--[[-- Core Module - Gui
@module Gui
@alias Gui
]]
local Gui = require 'expcore.gui.core'
--[[-- The basic button element
@element button
@param on_click fired when the player clicks the button
@param on_left_click fired when the player clicks with the left mouse button
@param on_left_click fired when the player clicks with the right mouse button
@tparam ?string|Concepts.LocalisedString caption the message that is shown on the button
@tparam ?string|Concepts.LocalisedString tooltip the tooltip that shows when a player hovers over the button
@tparam SpritePath sprite upto three sprites in the order: default, hovered, clicked
]]
Gui.new_concept('button')
:new_event('on_click',defines.events.on_gui_click)
:new_event('on_left_click',defines.events.on_gui_click,function(event)
return event.mouse_button == defines.mouse_button_type.left
end)
:new_event('on_right_click',defines.events.on_gui_click,function(event)
return event.mouse_button == defines.mouse_button_type.right
end)
:new_property('tooltip')
:new_property('caption',nil,function(properties,value)
properties.caption = value
properties.type = 'button'
end)
:new_property('sprite',nil,function(properties,value,hovered_sprite,clicked_sprite)
properties.sprite = value
properties.hovered_sprite = hovered_sprite
properties.clicked_sprite = clicked_sprite
properties.type = 'sprite-button'
end)
:define_draw(function(properties,parent,element)
if properties.type == 'button' then
element = parent.add{
name = properties.name,
type = properties.type,
caption = properties.caption,
tooltip = properties.tooltip
}
else
element = parent.add{
name = properties.name,
type = properties.type,
sprite = properties.sprite,
hovered_sprite = properties.hovered_sprite,
clicked_sprite = properties.clicked_sprite,
tooltip = properties.tooltip
}
end
return element
end)

View File

@@ -0,0 +1,24 @@
--[[-- Core Module - Gui
@module Gui
@alias Gui
]]
local Gui = require 'expcore.gui.core'
--[[-- The basic frame element
@element frame
@tparam ?string|Concepts.LocalisedString title the title that will show in the frame
]]
Gui.new_concept('frame')
:new_property('title',function(properties,value)
properties.title = value
end)
:define_draw(function(properties,parent,element)
element = parent.add{
name = properties.name,
type = 'frame',
caption = properties.title
}
return element
end)

View File

@@ -14,6 +14,15 @@ local Gui = {
concepts = {}
}
--[[-- Loads a concept from the concepts file, used internally
@tparam string concept the name of the concept to require
@usage-- Load a base concept
Gui.require_concept('frame')
]]
function Gui.require_concept(concept)
require('expcore.gui.concepts.'..concept)
end
--[[-- Gets the gui concept with this name
@tparam string name the name of the concept that you want to get
@usage-- Getting a gui concept
@@ -64,8 +73,8 @@ local custom_button = Gui.clone_concept('Button','CustomButton')
function Gui.clone_concept(name,new_name)
local concept = Gui.concepts[name] or error('Gui concept "'..name..'" is not defind',2)
if Gui.concepts[name] then
error('Gui concept "'..name..'" is already defind',2)
if Gui.concepts[new_name] then
error('Gui concept "'..new_name..'" is already defind',2)
end
return concept:clone(new_name)

View File

@@ -76,22 +76,24 @@ local Factorio_Events = {}
local Prototype = {
draw_callbacks = {},
properties = {},
factorio_events = {},
events = {}
}
--- Acts as a gernal handler for any factorio event
local function factorio_event_handler(event)
local element = event.element
local event_handlers = Factorio_Events[event.name]
if element then
if not element.valid then return end
local concept_name = element.name
local concept_event = Factorio_Events[event.name][concept_name]
concept_event[1]:raise_event(concept_event[2],event,true)
local concept_event_raise = event_handlers[element.name]
if concept_event_raise then
concept_event_raise(event)
end
else
local events_handlers = Factorio_Events[event.name]
for _,concept_event in pairs(events_handlers) do
concept_event[1]:raise_event(concept_event[2],event,true)
for _,concept_event_raise in pairs(event_handlers) do
concept_event_raise(event)
end
end
@@ -110,6 +112,7 @@ function Prototype:clone(concept_name)
-- Replace name of the concept
concept.name = concept_name
concept.properties.name = concept_name
concept:change_name()
-- Remove all event handlers that were copied
concept.events = {}
@@ -117,6 +120,14 @@ function Prototype:clone(concept_name)
concept.events[event_name] = {}
end
-- Remakes even handlers for factorio
concept.factorio_events = {}
for event_name,factorio_event in pairs(self.factorio_events) do
Factorio_Events[factorio_event][concept.name] = function(event)
concept:raise_event(event_name,event,true)
end
end
-- Remove all refrences to an instance store
if concept.instance_store then
concept.instance_store = nil
@@ -142,9 +153,6 @@ function Prototype:clone(concept_name)
concept.set_store_from_instance = nil
end
-- Sets the concept name
concept:change_name()
return concept
end
@@ -201,19 +209,24 @@ end)
end
local handlers = concept.events[event_name]
handlers[#handlers] = handler
handlers[#handlers+1] = handler
return concept
end
-- Adds the factorio event handler if this event is linked to one
if factorio_event then
self.factorio_events[event_name] = factorio_event
self.events[event_name].factorio_handler = event_condition
if not Factorio_Events[factorio_event] then
Factorio_Events[factorio_event] = {}
Event.add(factorio_event,factorio_event_handler)
end
Factorio_Events[factorio_event][self.name] = {self,event_name}
Factorio_Events[factorio_event][self.name] = function(event)
self:raise_event(event_name,event,true)
end
end
return self
@@ -259,7 +272,7 @@ function Prototype:raise_event(event_name,event,from_factorio)
for _,handler in ipairs(handlers) do
local success, err = pcall(handler,event)
if not success then
print('Gui event handler error with '..self.name..'/'..event_name..': '..err)
error('Gui event handler error with '..self.name..'/'..event_name..': '..err)
end
end
end
@@ -314,7 +327,7 @@ Gui.get_concept('CustomButton')
-- Call the setter method to update values if present
local success, err = pcall(setter_callback,concept.properties,value,...)
if not success then
print('Gui property handler error with '..concept.name..'/'..property_name..': '..err)
error('Gui property handler error with '..concept.name..'/'..property_name..': '..err)
end
else
-- Otherwise just update the key
@@ -392,7 +405,7 @@ function Prototype:draw(parent_element,...)
if success and rtn then
element = rtn
elseif not success then
print('Gui draw handler error with '..self.name..': '..rtn)
error('Gui draw handler error with '..self.name..': '..rtn)
end
end

View File

@@ -3,15 +3,40 @@
@alias Gui
]]
local Gui = require 'expcore.gui'
--- Tests.
-- functions used to test
-- @section tests
local Gui = require 'expcore.gui'
local Game = require 'utils.game' -- @dep utils.game
local test_prefix = '__GUI_TEST_'
local tests = {}
local function TEST(str) return test_prefix..str end
--[[
The main test frame
]]
local test_frame =
Gui.clone_concept('frame',TEST 'test_frame')
:set_title('Gui Tests')
:define_draw(function(properties,parent,element)
for category, _ in pairs(tests) do
element.add{
type = 'flow',
name = category,
direction = 'vertical'
}
end
end)
--[[-- Runs a set of gui tests to ensure that the system is working
@tparam LuaPlayer player the player that the guis are made for and who recives the results
@tparam[opt] string category when given only tests in this category are ran
@usage-- Run all gui tests
Gui.run_tests(Gui.test_string_return(game.print))
Gui.run_tests(game.player)
]]
function Gui.run_tests(player,category)
local results = {
@@ -42,18 +67,20 @@ function Gui.run_tests(player,category)
return results
end
local frame = player.gui.center[test_frame.name] or test_frame:draw(player.gui.center)
local cat_tests = tests[category]
results.total = #cat_tests
local output = player.print
for test_name, callback in pairs(cat_tests) do
local success, err = pcall(callback,player)
for test_name, concept in pairs(cat_tests) do
local success, err = pcall(concept.draw,concept,frame[category])
if success then
results.passed = results.passed + 1
else
results.erorrs[test_name] = err
results.errors[test_name] = err
results.failed = results.failed + 1
output(string.format('Test "%s / %s" failed:\n%s',category,test_name,err))
end
@@ -65,29 +92,51 @@ function Gui.run_tests(player,category)
end
--[[
Basic frame creation
Buttons
]]
local test_frame =
Gui.new_concept('test_frame')
:define_draw(function(properties,parent,element)
element =
parent.add{
name = properties.name,
type = 'frame',
caption = 'Gui Tests'
}
element.add{
type = 'label',
caption = 'Hello, World!'
}
return element
local basic_button =
Gui.clone_concept('button',TEST 'basic_button')
:set_caption('Basic Button')
:set_tooltip('Basic button')
:on_click(function(event)
event.player.print('You pressed basic button!')
end)
tests.Frame = {
['Draw Frame'] = function(player)
test_frame:draw(player.gui.center)
local sprite_button =
Gui.clone_concept('button',TEST 'sprite_button')
:set_sprite('utility/warning_icon')
:set_tooltip('Sprite button')
:on_click(function(event)
event.player.print('You pressed sprite button!')
end)
local multi_sprite_button =
Gui.clone_concept('button',TEST 'multi_sprite_button')
:set_sprite('utility/warning_icon','utility/warning','utility/warning_white')
:set_tooltip('Multi-sprite button')
:on_click(function(event)
event.player.print('You pressed multi sprite button!')
end)
local admin_button =
Gui.clone_concept('button',TEST 'admin_button')
:set_caption('Admin Button')
:set_tooltip('Admin button')
:define_draw(function(properties,parent,element)
local player = Game.get_player_by_index(element.player_index)
if not player.admin then
element.enabled = false
element.tooltip = 'You must be admin to press this button'
end
end)
:on_click(function(event)
event.player.print('You pressed admin button!')
end)
tests.Buttons = {
['Basic Button'] = basic_button,
['Sprite Button'] = sprite_button,
['Multi Sprite Button'] = multi_sprite_button,
['Admin Button'] = admin_button,
}