From cb59cc05379b3b159b3d97e1e2e04b528bdf591a Mon Sep 17 00:00:00 2001
From: Cooldude2606
-- Makeing a alternative button based on the first
local custom_button =
@@ -275,6 +306,50 @@ button:clone('CustomButton')
+ Elements
+
+
+
+
+ button
+ The basic button element
+
+
+ frame
+ The basic frame element
+
+
+
+
+
+ Core
+
+
+
+
+ require_concept(concept)
+ Loads a concept from the concepts file, used internally
+
+
+ get_concept(name)
+ Gets the gui concept with this name
+
+
+ Prototype:change_name([new_name=self.name])
+ Used internally to save concept names to the core gui module
+
+
+ new_concept(name)
+ Returns a new gui concept with no properties or events
+
+
+ clone_concept(name, new_name)
+ Making anew concept based on the properties and drawing of another
+
+
+
+
+
Concept Base
@@ -284,6 +359,10 @@ button:clone('CustomButton')
Used to copy all the settings from one concept to another and removing links to the orginal
+ Prototype:change_name([new_name=self.name])
+ Used internally to save concept names to the core gui module
+
+
Prototype:new_event(event_name[, factorio_event][, event_condition])
Adds a new event trigger to the concept which can be linked to a factorio event
@@ -377,11 +456,490 @@ button:clone('CustomButton')
+
+
+ Tests
+
+
+
+
+ run_tests(player[, category])
+ Runs a set of gui tests to ensure that the system is working
+
+
+
+ Elements
+
+ -
+
+
+ #
+ button
+
+
+ -
+
+
+
The basic button element
+
+
+
+ Properties / Events:
+
+
+
+
+
+
+
+ -
+
+ on_click
+
+ :
+
+
+ fired when the player clicks the button
+
+
+
+
+
+
+
+ -
+
+ on_left_click
+
+ :
+
+
+ fired when the player clicks with the right mouse button
+
+
+
+
+
+
+
+ -
+
+ on_left_click
+
+ :
+
+
+ fired when the player clicks with the right mouse button
+
+
+
+
+
+
+
+ -
+
+ caption
+
+ :
+
+ (string or LocalisedString)
+
+ the message that is shown on the button
+
+
+
+
+
+
+
+ -
+
+ tooltip
+
+ :
+
+ (string or LocalisedString)
+
+ the tooltip that shows when a player hovers over the button
+
+
+
+
+
+
+
+ -
+
+ sprite
+
+ :
+
+ (SpritePath)
+
+ upto three sprites in the order: default, hovered, clicked
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ -
+
+
+ #
+ frame
+
+
+ -
+
+
+
The basic frame element
+
+
+
+ Properties / Events:
+
+
+
+
+
+
+
+ -
+
+ title
+
+ :
+
+ (string or LocalisedString)
+
+ the title that will show in the frame
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Core
+
+ -
+
+
+ #
+ require_concept(concept)
+
+
+ -
+
+
+
Loads a concept from the concepts file, used internally
+
+
+
+ Parameters:
+
+
+
+
+
+
+
+ -
+
+ concept
+
+ :
+
+ (string)
+
+ the name of the concept to require
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Usage:
+ -- Load a base concept
+Gui.require_concept('frame')
+
+
+
+ -
+
+
+ #
+ get_concept(name)
+
+
+ -
+
+
+
Gets the gui concept with this name
+
+
+
+ Parameters:
+
+
+
+
+
+
+
+ -
+
+ name
+
+ :
+
+ (string)
+
+ the name of the concept that you want to get
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Usage:
+ -- Getting a gui concept
+local button = Gui.get_concept('Button')
+
+
+
+ -
+
+
+ #
+ Prototype:change_name([new_name=self.name])
+
+
+ -
+
+
+
Used internally to save concept names to the core gui module
+
+
+
+ Parameters:
+
+
+
+
+
+
+
+ -
+
+ new_name
+
+ :
+
+ (string)
+
+ the new name of the concept
+
+ (default: self.name)
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Usage:
+ -- Internal Saving
+-- this is never needed to be done, internal use only!
+local button = Gui.get_concept('Button')
+button:change_name('Not Button')
+
+
+
+ -
+
+
+ #
+ new_concept(name)
+
+
+ -
+
+
+
Returns a new gui concept with no properties or events
+
+
+
+ Parameters:
+
+
+
+
+
+
+
+ -
+
+ name
+
+ :
+
+ (string)
+
+ the name that you want this concept to have
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Usage:
+ -- Making a new concept, see module usage
+local button = Gui.new_concept('Button')
+
+
+
+ -
+
+
+ #
+ clone_concept(name, new_name)
+
+
+ -
+
+
+
Making anew concept based on the properties and drawing of another
+
+
+
+ Parameters:
+
+
+
+
+
+
+
+ -
+
+ name
+
+ :
+
+ (string)
+
+ the name of the concept that you want as the base
+
+
+
+
+
+
+
+ -
+
+ new_name
+
+ :
+
+ (string)
+
+ the name that you want the new concept to have
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Usage:
+ -- Making a new concept from another, see module usage
+local custom_button = Gui.clone_concept('Button','CustomButton')
+
+
+
+
Concept Base
-
@@ -444,6 +1002,62 @@ button:clone('CustomButton')
Gui.get_concept('Button'):clone('CustomButton')
+
+
-
+
+
+ #
+ Prototype:change_name([new_name=self.name])
+
+
+ -
+
+
+
Used internally to save concept names to the core gui module
+
+
+
+ Parameters:
+
+
+
+
+
+
+
+ -
+
+ new_name
+
+ :
+
+ (string)
+
+ the new name of the concept
+
+ (default: self.name)
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Usage:
+ -- Internal Saving
+-- this is never needed to be done, internal use only!
+local button = Gui.get_concept('Button')
+button:change_name('Not Button')
+
+
-
@@ -934,6 +1548,7 @@ Gui.get_concept('Button')
:define_draw(function(properties,parent,element)
-- Note that element might be nil if this is the first draw function
-- for this example we assume button was cloned from Prototype and so has no other draw functions defined
+ -- this means that there is no element yet and what we return will be the first time the element is returned
-- although not shown here you also can recive any extra arguments here from the call to draw
if properties.type == 'button' then
element = parent.draw{
@@ -953,8 +1568,8 @@ Gui.get_concept('Button')
end
- -- We must return the element or what we want to be seen as the instance
- -- this is so other draw functions have access to it, say if our custom button defined a draw function to change the font color to red
+ -- We must return the element or what we want to be seen as the instance, this is so other draw functions have access to it
+ -- for example if our custom button defined a draw function to change the font color to red
return element
end)
@@ -1871,6 +2486,79 @@ Gui.get_concept('CustomButton')
custom_button.set_store_from_instance(element)
+
+
+
Tests
+
+ -
+
+
+ #
+ run_tests(player[, category])
+
+
+ -
+
+
+
Runs a set of gui tests to ensure that the system is working
+
+
+
+ Parameters:
+
+
+
+
+
+
+
+ -
+
+ player
+
+ :
+
+ (LuaPlayer)
+
+ the player that the guis are made for and who recives the results
+
+
+
+
+
+
+
+ -
+
+ category
+
+ :
+
+ (string)
+
+ when given only tests in this category are ran
+
+ (optional)
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Usage:
+ -- Run all gui tests
+Gui.run_tests(game.player)
+
+
@@ -1887,7 +2575,7 @@ Gui.get_concept('CustomButton')
generated by LDoc
diff --git a/docs/core/Permissions-Groups.html b/docs/core/Permissions-Groups.html
index 4befca44..e05ed687 100644
--- a/docs/core/Permissions-Groups.html
+++ b/docs/core/Permissions-Groups.html
@@ -1432,7 +1432,7 @@
generated by LDoc
diff --git a/docs/core/Roles.html b/docs/core/Roles.html
index a2bef34f..5b989791 100644
--- a/docs/core/Roles.html
+++ b/docs/core/Roles.html
@@ -3152,7 +3152,7 @@
generated by LDoc
diff --git a/docs/core/Store.html b/docs/core/Store.html
index 6026f246..937ec539 100644
--- a/docs/core/Store.html
+++ b/docs/core/Store.html
@@ -1128,7 +1128,7 @@ Store.register(team_scores,function(value,key)
generated by LDoc
diff --git a/docs/core/Sudo.html b/docs/core/Sudo.html
index b037fbf6..aabdfb54 100644
--- a/docs/core/Sudo.html
+++ b/docs/core/Sudo.html
@@ -544,7 +544,7 @@
generated by LDoc
diff --git a/docs/guis/Player-List.html b/docs/guis/Player-List.html
index 8aa3ef94..3a76fdcc 100644
--- a/docs/guis/Player-List.html
+++ b/docs/guis/Player-List.html
@@ -626,7 +626,7 @@
generated by LDoc
diff --git a/docs/guis/Rocket-Info.html b/docs/guis/Rocket-Info.html
index 49db82bf..d5144327 100644
--- a/docs/guis/Rocket-Info.html
+++ b/docs/guis/Rocket-Info.html
@@ -629,7 +629,7 @@
generated by LDoc
diff --git a/docs/guis/Science-Info.html b/docs/guis/Science-Info.html
index 1d910135..2e733887 100644
--- a/docs/guis/Science-Info.html
+++ b/docs/guis/Science-Info.html
@@ -449,7 +449,7 @@
generated by LDoc
diff --git a/docs/guis/Task-List.html b/docs/guis/Task-List.html
index 8b3221fb..a3fa28cb 100644
--- a/docs/guis/Task-List.html
+++ b/docs/guis/Task-List.html
@@ -632,7 +632,7 @@
generated by LDoc
diff --git a/docs/guis/Warps-List.html b/docs/guis/Warps-List.html
index 0a12a90f..4d84021e 100644
--- a/docs/guis/Warps-List.html
+++ b/docs/guis/Warps-List.html
@@ -837,7 +837,7 @@
generated by LDoc
diff --git a/docs/index.html b/docs/index.html
index bd8cb83a..d55abbff 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -510,7 +510,7 @@ see ./expcore/commands.lua for more details
generated by LDoc
diff --git a/docs/modules/control.html b/docs/modules/control.html
index c79d10b4..b1e1a273 100644
--- a/docs/modules/control.html
+++ b/docs/modules/control.html
@@ -351,7 +351,7 @@
generated by LDoc
diff --git a/docs/modules/utils.alien_evolution_progress.html b/docs/modules/utils.alien_evolution_progress.html
index a8dd8517..e70f53cf 100644
--- a/docs/modules/utils.alien_evolution_progress.html
+++ b/docs/modules/utils.alien_evolution_progress.html
@@ -419,7 +419,7 @@ fraction will decide a chance to spawn. 1 alien for 2 spawner's will have 50% on
generated by LDoc
diff --git a/docs/modules/utils.core.html b/docs/modules/utils.core.html
index d3329c77..2eafdc25 100644
--- a/docs/modules/utils.core.html
+++ b/docs/modules/utils.core.html
@@ -1164,7 +1164,7 @@
generated by LDoc
diff --git a/docs/modules/utils.debug.html b/docs/modules/utils.debug.html
index 5e50472a..e5bab76d 100644
--- a/docs/modules/utils.debug.html
+++ b/docs/modules/utils.debug.html
@@ -654,7 +654,7 @@
generated by LDoc
diff --git a/docs/modules/utils.dump_env.html b/docs/modules/utils.dump_env.html
index ef683374..f86ef92c 100644
--- a/docs/modules/utils.dump_env.html
+++ b/docs/modules/utils.dump_env.html
@@ -323,7 +323,7 @@
generated by LDoc
diff --git a/docs/modules/utils.event.html b/docs/modules/utils.event.html
index 9f27e495..edf052c2 100644
--- a/docs/modules/utils.event.html
+++ b/docs/modules/utils.event.html
@@ -1292,7 +1292,7 @@
generated by LDoc
diff --git a/docs/modules/utils.event_core.html b/docs/modules/utils.event_core.html
index d369a623..74fd5324 100644
--- a/docs/modules/utils.event_core.html
+++ b/docs/modules/utils.event_core.html
@@ -434,7 +434,7 @@
generated by LDoc
diff --git a/docs/modules/utils.math.html b/docs/modules/utils.math.html
index 78cd66e7..8898c3e4 100644
--- a/docs/modules/utils.math.html
+++ b/docs/modules/utils.math.html
@@ -338,7 +338,7 @@
generated by LDoc
diff --git a/docs/modules/utils.recipe_locker.html b/docs/modules/utils.recipe_locker.html
index dc87e650..b3a13c92 100644
--- a/docs/modules/utils.recipe_locker.html
+++ b/docs/modules/utils.recipe_locker.html
@@ -441,7 +441,7 @@
generated by LDoc
diff --git a/docs/modules/utils.state_machine.html b/docs/modules/utils.state_machine.html
index 394ccfb3..a780a4d1 100644
--- a/docs/modules/utils.state_machine.html
+++ b/docs/modules/utils.state_machine.html
@@ -752,7 +752,7 @@
generated by LDoc
diff --git a/docs/modules/utils.table.html b/docs/modules/utils.table.html
index 406abc06..6c086a80 100644
--- a/docs/modules/utils.table.html
+++ b/docs/modules/utils.table.html
@@ -1382,7 +1382,7 @@
generated by LDoc
diff --git a/docs/modules/utils.task.html b/docs/modules/utils.task.html
index e70b97ec..b6ca1a92 100644
--- a/docs/modules/utils.task.html
+++ b/docs/modules/utils.task.html
@@ -651,7 +651,7 @@
generated by LDoc
diff --git a/docs/modules/utils.timestamp.html b/docs/modules/utils.timestamp.html
index 54faeee6..46b83cfd 100644
--- a/docs/modules/utils.timestamp.html
+++ b/docs/modules/utils.timestamp.html
@@ -442,7 +442,7 @@
generated by LDoc
diff --git a/docs/topics/license.html b/docs/topics/license.html
index 79b030c8..beee1b37 100644
--- a/docs/topics/license.html
+++ b/docs/topics/license.html
@@ -789,7 +789,7 @@ Public License instead of this License. But first, please read
generated by LDoc
diff --git a/docs/topics/readme.md.html b/docs/topics/readme.md.html
index 419754b9..8c8c821c 100644
--- a/docs/topics/readme.md.html
+++ b/docs/topics/readme.md.html
@@ -332,7 +332,7 @@
generated by LDoc
diff --git a/expcore/gui.lua b/expcore/gui.lua
index 7ab0bdd3..a384e8bd 100644
--- a/expcore/gui.lua
+++ b/expcore/gui.lua
@@ -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
\ No newline at end of file
diff --git a/expcore/gui/concepts/button.lua b/expcore/gui/concepts/button.lua
new file mode 100644
index 00000000..cecc257c
--- /dev/null
+++ b/expcore/gui/concepts/button.lua
@@ -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)
\ No newline at end of file
diff --git a/expcore/gui/concepts/frame.lua b/expcore/gui/concepts/frame.lua
new file mode 100644
index 00000000..c484cbe8
--- /dev/null
+++ b/expcore/gui/concepts/frame.lua
@@ -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)
\ No newline at end of file
diff --git a/expcore/gui/core.lua b/expcore/gui/core.lua
index 07a69097..5ce2ec2a 100644
--- a/expcore/gui/core.lua
+++ b/expcore/gui/core.lua
@@ -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)
diff --git a/expcore/gui/prototype.lua b/expcore/gui/prototype.lua
index 79128ef8..575ae9b6 100644
--- a/expcore/gui/prototype.lua
+++ b/expcore/gui/prototype.lua
@@ -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
diff --git a/expcore/gui/test.lua b/expcore/gui/test.lua
index fd6d6796..a4b8f56a 100644
--- a/expcore/gui/test.lua
+++ b/expcore/gui/test.lua
@@ -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,
}
\ No newline at end of file