mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-30 12:31:41 +09:00
Added Comments to all gui files
This commit is contained in:
@@ -1,10 +1,23 @@
|
||||
--- Adds a button handler
|
||||
--- Gui class define for buttons and sprite buttons
|
||||
--[[
|
||||
>>>> Functions
|
||||
Button.new_button(name) --- Creates a new button element define
|
||||
|
||||
Button._prototype:on_click(player,element) --- Registers a handler for when the button is clicked
|
||||
Button._prototype:on_left_click(player,element) --- Registers a handler for when the button is clicked with the left mouse button
|
||||
Button._prototype:on_right_click(player,element) --- Registers a handler for when the button is clicked with the right mouse button
|
||||
|
||||
Button._prototype:set_sprites(sprite,hovered_sprite,clicked_sprite) --- Adds sprites to a button making it a spirte button
|
||||
Button._prototype:set_click_filter(filter,...) --- Adds a click / mouse button filter to the button
|
||||
Button._prototype:set_key_filter(filter,...) --- Adds a control key filter to the button
|
||||
|
||||
Other functions present from expcore.gui.core
|
||||
]]
|
||||
local mod_gui = require 'mod-gui'
|
||||
local Gui = require './core'
|
||||
|
||||
local Button = {
|
||||
config={},
|
||||
clean_names={},
|
||||
_prototype=Gui._prototype_factory{
|
||||
on_click = Gui._event_factory('on_click'),
|
||||
on_left_click = Gui._event_factory('on_left_click'),
|
||||
@@ -12,6 +25,9 @@ local Button = {
|
||||
}
|
||||
}
|
||||
|
||||
--- Creates a new button element define
|
||||
-- @tparam[opt] name string the optional debug name that can be added
|
||||
-- @treturn table the new button element define
|
||||
function Button.new_button(name)
|
||||
|
||||
local self = Gui._define_factory(Button._prototype)
|
||||
@@ -28,13 +44,13 @@ function Button.new_button(name)
|
||||
event.keys = keys
|
||||
|
||||
if self.post_authenticator then
|
||||
if not self.post_authenticator(event.player,self.clean_name or self.name) then return end
|
||||
if not self.post_authenticator(event.player,self.name) then return end
|
||||
end
|
||||
|
||||
if mouse_button == defines.mouse_button_type.left and self.events.on_left_click then
|
||||
self.events.on_left_click(event.player,event.element,event)
|
||||
self.events.on_left_click(event.player,event.element)
|
||||
elseif mouse_button == defines.mouse_button_type.right and self.events.on_right_click then
|
||||
self.events.on_right_click(event.player,event.element,event)
|
||||
self.events.on_right_click(event.player,event.element)
|
||||
end
|
||||
|
||||
if self.mouse_button_filter and not self.mouse_button_filter[mouse_button] then return end
|
||||
@@ -52,6 +68,11 @@ function Button.new_button(name)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Adds sprites to a button making it a spirte button
|
||||
-- @tparam sprite SpritePath the sprite path for the default sprite for the button
|
||||
-- @tparam[opt] hovered_sprite SpritePath the sprite path for the sprite when the player hovers over the button
|
||||
-- @tparam[opt] clicked_sprite SpritePath the sprite path for the sprite when the player clicks the button
|
||||
-- @treturn self returns the button define to allow chaining
|
||||
function Button._prototype:set_sprites(sprite,hovered_sprite,clicked_sprite)
|
||||
self.draw_data.type = 'sprite-button'
|
||||
self.draw_data.sprite = sprite
|
||||
@@ -60,6 +81,10 @@ function Button._prototype:set_sprites(sprite,hovered_sprite,clicked_sprite)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Adds a click / mouse button filter to the button
|
||||
-- @tparam filter ?string|table either a table of mouse buttons or the first mouse button to filter, with a table true means allowed
|
||||
-- @tparam[opt] ... when filter is not a table you can add the mouse buttons one after each other
|
||||
-- @treturn self returns the button define to allow chaining
|
||||
function Button._prototype:set_click_filter(filter,...)
|
||||
if type(filter) == 'string' then
|
||||
filter = {[filter]=true}
|
||||
@@ -78,6 +103,10 @@ function Button._prototype:set_click_filter(filter,...)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Adds a control key filter to the button
|
||||
-- @tparam filter ?string|table either a table of control keys or the first control keys to filter, with a table true means allowed
|
||||
-- @tparam[opt] ... when filter is not a table you can add the control keyss one after each other
|
||||
-- @treturn self returns the button define to allow chaining
|
||||
function Button._prototype:set_key_filter(filter,...)
|
||||
if type(filter) == 'string' then
|
||||
filter = {[filter]=true}
|
||||
|
||||
@@ -1,34 +1,94 @@
|
||||
--- Gui class define for checkboxs and radiobuttons
|
||||
--[[
|
||||
>>>> Using an option set
|
||||
An option set is a set of radio buttons where only one of them can be active at a time, this means that when one
|
||||
is clicked all the other ones are set to false, an option set must be defined before hand and will always store
|
||||
its state but is not limited by how it can categorize the store.
|
||||
|
||||
First you must register the store with a name and a update callback, and an optional function for categorize:
|
||||
|
||||
local example_option_set =
|
||||
Gui.new_option_set('example-option-set',function(value,category)
|
||||
game.print('Example options set '..category..' is now: '..tostring(value))
|
||||
end,Gui.player_store)
|
||||
|
||||
Then you must register some radiobutton defines and include them in the option set:
|
||||
|
||||
local example_option_one =
|
||||
Gui.new_radiobutton()
|
||||
:set_caption('Option One')
|
||||
:add_as_option(example_option_set,'One')
|
||||
|
||||
local example_option_two =
|
||||
Gui.new_radiobutton()
|
||||
:set_caption('Option Two')
|
||||
:add_as_option(example_option_set,'Two')
|
||||
|
||||
Note that these radiobuttons can still have on_element_update events but this may result in a double trigger of events as
|
||||
the option set update is always triggered; also add_store cant be used as the option set acts as the store however get
|
||||
and set store will still work but will effect the option set rather than the indivual radiobuttons.
|
||||
|
||||
>>>> Functions
|
||||
Checkbox.new_checkbox(name) --- Creates a new checkbox element define
|
||||
Checkbox._prototype_checkbox:on_element_update(callback) --- Registers a handler for when an element instance updates
|
||||
Checkbox._prototype_checkbox:on_store_update(callback) --- Registers a handler for when the stored value updates
|
||||
|
||||
Checkbox.new_radiobutton(name) --- Creates a new radiobutton element define
|
||||
Checkbox._prototype_radiobutton:on_element_update(callback) --- Registers a handler for when an element instance updates
|
||||
Checkbox._prototype_radiobutton:on_store_update(callback) --- Registers a handler for when the stored value updates
|
||||
Checkbox._prototype_radiobutton:add_as_option(option_set,option_name) --- Adds this radiobutton to be an option in the given option set (only one can be true at a time)
|
||||
|
||||
Checkbox.new_option_set(name,callback,categorize) --- Registers a new option set that can be linked to radiobutotns (only one can be true at a time)
|
||||
Checkbox.draw_option_set(name,element) --- Draws all radiobuttons that are part of an option set at once (Gui.draw will not work)
|
||||
|
||||
Checkbox.reset_radiobutton(element,exclude,recursive) --- Sets all radiobutotn in a element to false (unless excluded) and can act recursivly
|
||||
|
||||
Other functions present from expcore.gui.core
|
||||
]]
|
||||
local Gui = require './core'
|
||||
local Store = require 'expcore.store'
|
||||
local Game = require 'utils.game'
|
||||
|
||||
local function event_call(self,element,value)
|
||||
if self.events.on_change then
|
||||
--- Event call for on_checked_state_changed and store update
|
||||
-- @tparam define table the define that this is acting on
|
||||
-- @tparam element LuaGuiElement the element that triggered the event
|
||||
-- @tparam value boolean the new state of the checkbox
|
||||
local function event_call(define,element,value)
|
||||
if define.events.on_element_update then
|
||||
local player = Game.get_player_by_index(element.player_index)
|
||||
self.events.on_change(player,element,value)
|
||||
define.events.on_element_update(player,element,value)
|
||||
end
|
||||
end
|
||||
|
||||
local function store_call(self,element,value)
|
||||
--- Store call for store update
|
||||
-- @tparam define table the define that this is acting on
|
||||
-- @tparam element LuaGuiElement the element that triggered the event
|
||||
-- @tparam value boolean the new state of the checkbox
|
||||
local function store_call(define,element,value)
|
||||
element.state = value
|
||||
event_call(self,element,value)
|
||||
event_call(define,element,value)
|
||||
end
|
||||
|
||||
local Checkbox = {
|
||||
option_sets={},
|
||||
option_categorize={},
|
||||
_prototype_checkbox=Gui._prototype_factory{
|
||||
on_change = Gui._event_factory('on_change'),
|
||||
on_element_update = Gui._event_factory('on_element_update'),
|
||||
on_store_update = Gui._event_factory('on_store_update'),
|
||||
add_store = Gui._store_factory(store_call),
|
||||
add_sync_store = Gui._sync_store_factory(store_call)
|
||||
},
|
||||
_prototype_radiobutton=Gui._prototype_factory{
|
||||
on_change = Gui._event_factory('on_change'),
|
||||
on_element_update = Gui._event_factory('on_element_update'),
|
||||
on_store_update = Gui._event_factory('on_store_update'),
|
||||
add_store = Gui._store_factory(store_call),
|
||||
add_sync_store = Gui._sync_store_factory(store_call)
|
||||
}
|
||||
}
|
||||
|
||||
--- Creates a new checkbox element define
|
||||
-- @tparam[opt] name string the optional debug name that can be added
|
||||
-- @treturn table the new checkbox element define
|
||||
function Checkbox.new_checkbox(name)
|
||||
|
||||
local self = Gui._define_factory(Checkbox._prototype_checkbox)
|
||||
@@ -70,33 +130,9 @@ function Checkbox.new_checkbox(name)
|
||||
return self
|
||||
end
|
||||
|
||||
function Checkbox.reset_radiobutton(element,exclude,recursive)
|
||||
if not element or not element.valid then return end
|
||||
exclude = type(exclude) == 'table' and exclude or exclude ~= nil and {[exclude]=true} or {}
|
||||
|
||||
for _,child in pairs(element.children) do
|
||||
if child and child.valid and child.type == 'radiobutton' then
|
||||
local state = exclude[child.name] or false
|
||||
local define = Gui.defines[child.name]
|
||||
|
||||
if define then
|
||||
local category = define.categorize and define.categorize(child) or state
|
||||
define:set_store(category,state)
|
||||
|
||||
else
|
||||
child.state = state
|
||||
|
||||
end
|
||||
|
||||
elseif child.children and (type(recursive) == 'number' and recursive > 0 or recursive == true) then
|
||||
Checkbox.reset_radiobutton(child,exclude,recursive)
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
--- Creates a new radiobutton element define, has all functions checkbox has
|
||||
-- @tparam[opt] name string the optional debug name that can be added
|
||||
-- @treturn table the new button element define
|
||||
function Checkbox.new_radiobutton(name)
|
||||
local self = Checkbox.new_checkbox(name)
|
||||
self.draw_data.type = 'radiobutton'
|
||||
@@ -107,9 +143,13 @@ function Checkbox.new_radiobutton(name)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Adds this radiobutton to be an option in the given option set (only one can be true at a time)
|
||||
-- @tparam option_set string the name of the option set to add this element to
|
||||
-- @tparam option_name string the name of this option that will be used to idenitife it
|
||||
-- @tparam self the define to allow chaining
|
||||
function Checkbox._prototype_radiobutton:add_as_option(option_set,option_name)
|
||||
self.option_set = option_set
|
||||
self.option_name = option_name or self.clean_name or self.name
|
||||
self.option_name = option_name or self.name
|
||||
|
||||
Checkbox.option_sets[option_set][self.option_name] = self.name
|
||||
Checkbox.option_sets[option_set][self.name] = self.option_name
|
||||
@@ -119,6 +159,9 @@ function Checkbox._prototype_radiobutton:add_as_option(option_set,option_name)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Gets the stored value of the radiobutton or the option set if present
|
||||
-- @tparam category[opt] string the category to get such as player name or force name
|
||||
-- @treturn any the value that is stored for this define
|
||||
function Checkbox._prototype_radiobutton:get_store(category,internal)
|
||||
if not self.store then return end
|
||||
local location = not internal and self.option_set or self.store
|
||||
@@ -130,6 +173,10 @@ function Checkbox._prototype_radiobutton:get_store(category,internal)
|
||||
end
|
||||
end
|
||||
|
||||
--- Sets the stored value of the radiobutton or the option set if present
|
||||
-- @tparam category[opt] string the category to get such as player name or force name
|
||||
-- @tparam value any the value to set for this define, must be valid for its type ie boolean for checkbox etc
|
||||
-- @treturn boolean true if the value was set
|
||||
function Checkbox._prototype_radiobutton:set_store(category,value,internal)
|
||||
if not self.store then return end
|
||||
local location = not internal and self.option_set or self.store
|
||||
@@ -141,6 +188,13 @@ function Checkbox._prototype_radiobutton:set_store(category,value,internal)
|
||||
end
|
||||
end
|
||||
|
||||
--- Registers a new option set that can be linked to radiobutotns (only one can be true at a time)
|
||||
-- @tparam name string the name of the option set, must be unique
|
||||
-- @tparam callback function the update callback when the value of the option set chagnes
|
||||
-- callback param - value string - the new selected option for this option set
|
||||
-- callback param - category string - the category that updated if categorize was used
|
||||
-- @tpram categorize function the function used to convert an element into a string
|
||||
-- @treturn string the name of this option set to be passed to add_as_option
|
||||
function Checkbox.new_option_set(name,callback,categorize)
|
||||
|
||||
Store.register(name,function(value,category)
|
||||
@@ -161,6 +215,9 @@ function Checkbox.new_option_set(name,callback,categorize)
|
||||
return name
|
||||
end
|
||||
|
||||
--- Draws all radiobuttons that are part of an option set at once (Gui.draw will not work)
|
||||
-- @tparam name string the name of the option set to draw the radiobuttons of
|
||||
-- @tparam element LuaGuiElement the parent element that the radiobuttons will be drawn to
|
||||
function Checkbox.draw_option_set(name,element)
|
||||
if not Checkbox.option_sets[name] then return end
|
||||
local options = Checkbox.option_sets[name]
|
||||
@@ -173,4 +230,37 @@ function Checkbox.draw_option_set(name,element)
|
||||
|
||||
end
|
||||
|
||||
--- Sets all radiobutotn in a element to false (unless excluded) and can act recursivly
|
||||
-- @tparam element LuaGuiElement the root gui element to start setting radio buttons from
|
||||
-- @tparam[opt] exclude ?string|table the name of the radiobutton to exclude or a table of radiobuttons where true will set the state true
|
||||
-- @tparam[opt=false] recursive boolean if true will recur as much as possible, if a number will recur that number of times
|
||||
-- @treturn boolean true if successful
|
||||
function Checkbox.reset_radiobuttons(element,exclude,recursive)
|
||||
if not element or not element.valid then return end
|
||||
exclude = type(exclude) == 'table' and exclude or exclude ~= nil and {[exclude]=true} or {}
|
||||
recursive = type(recursive) == 'number' and recursive-1 or recursive
|
||||
|
||||
for _,child in pairs(element.children) do
|
||||
if child and child.valid and child.type == 'radiobutton' then
|
||||
local state = exclude[child.name] or false
|
||||
local define = Gui.defines[child.name]
|
||||
|
||||
if define then
|
||||
local category = define.categorize and define.categorize(child) or state
|
||||
define:set_store(category,state)
|
||||
|
||||
else
|
||||
child.state = state
|
||||
|
||||
end
|
||||
|
||||
elseif child.children and (type(recursive) == 'number' and recursive >= 0 or recursive == true) then
|
||||
Checkbox.reset_radiobutton(child,exclude,recursive)
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
return Checkbox
|
||||
@@ -1,17 +1,172 @@
|
||||
--- Core gui file for making element defines and element classes (use require 'expcore.gui')
|
||||
-- see utils.gui for event handlering
|
||||
-- see expcore.gui.test for examples for element defines
|
||||
--[[
|
||||
>>>> Basic useage with no defines
|
||||
This module can be igroned if you are only wanting only event handlers as utils.gui adds the following:
|
||||
|
||||
Gui.uid_name() --- Generates a unqiue name to register events to
|
||||
Gui.on_checked_state_changed(callback) --- Register a handler for the on_gui_checked_state_changed event
|
||||
Gui.on_click(callback) --- Register a handler for the on_gui_click event
|
||||
Gui.on_elem_changed(callback) --- Register a handler for the on_gui_elem_changed
|
||||
Gui.on_selection_state_changed(callback) --- Register a handler for the on_gui_selection_state_changed event
|
||||
Gui.on_text_changed(callback) --- Register a handler for the on_gui_text_changed event
|
||||
Gui.on_value_changed(callback) --- Register a handler for the on_gui_value_changed event
|
||||
|
||||
Note that all event handlers will include event.player as a valid player and that if the player or the
|
||||
element is not valid then the callback will not be run.
|
||||
|
||||
>>>> Interal factory functions
|
||||
There are a few factory function that are used by the class definations the use of these function are important to
|
||||
know about but should only be used when making a new class deination rather than an element defination. See one of
|
||||
the existing class definations for an example of when to use these.
|
||||
|
||||
>>>> Basic prototype functions
|
||||
Using a class defination you can create a new element dinfation in our examples we will be using the checkbox.
|
||||
|
||||
local checkbox_example = Gui.new_checkbox()
|
||||
|
||||
Although all class definations are stored in Gui.classes the main function used to make new element defination are
|
||||
made aviable in the top level gui module. All functions which return a new element defination will accept a name argument
|
||||
which is a name which is used while debuging and is not required to be used (has not been used in examples)
|
||||
|
||||
Every element define will accept a caption and tooltip (although some may not show) and to do this you would use the two
|
||||
set function provided for the element defines:
|
||||
|
||||
checkbox_example:set_caption('Example Checkbox')
|
||||
checkbox_example:set_tooltip('Example checkbox')
|
||||
|
||||
Each element define can have event handlers set, for our example checkbox we only have access to on_change which will trigger
|
||||
when the state of the checkbox changes; if we want to assign handlers using the utils.gui methods then we can get the uid by calling
|
||||
the uid function on the element define; however, each element can only have one handler (of each event) so it is not possible to use
|
||||
Gui.on_checked_state_changed and on_change at the same time in our example.
|
||||
|
||||
checkbox_example:on_change(function(player,element,value)
|
||||
player.print('Example checkbox is now: '..tostring(value))
|
||||
end)
|
||||
|
||||
local checkbox_example_uid = checkbox_example:uid()
|
||||
Gui.on_click(checkbox_example_uid,function(event)
|
||||
event.player.print('You clicked the example checkbox!')
|
||||
end)
|
||||
|
||||
Finally you will want to draw your element defines for which you can call deirectly on the deinfe or use Gui.draw to do; when Gui.draw is
|
||||
used it can be given either the element define, the define's uid or the debug name of the define (if set):
|
||||
|
||||
checkbox_example:draw_to(parent_element)
|
||||
Gui.draw(checkbox_example_uid,parent_element)
|
||||
|
||||
>>>> Using authenticators with draw
|
||||
When an element is drawn to its parent it can always be used but if you want to limit who can use it then you can use an authenticator. There
|
||||
are two types which can be used: post and pre; using a pre authenticator will mean that the draw function is stoped before the element is added
|
||||
to the parent element while using a post authenticator will draw the element to the parent but will disable the element from interaction. Both may
|
||||
be used if you have use for such.
|
||||
|
||||
-- unless global.checkbox_example_allow_pre_auth is true then the checkbox will not be drawn
|
||||
checkbox_example:set_pre_authenticator(function(player,define_name)
|
||||
player.print('Example checkbox pre auth callback ran')
|
||||
return global.checkbox_example_allow_pre_auth
|
||||
end)
|
||||
|
||||
-- unless global.checkbox_example_allow_post_auth is true then the checkbox will be drawn but deactiveated (provided pre auth returns true)
|
||||
checkbox_example:set_post_authenticator(function(player,define_name)
|
||||
player.print('Example checkbox pre auth callback ran')
|
||||
return global.checkbox_example_allow_post_auth
|
||||
end)
|
||||
|
||||
>>>> Using store
|
||||
A powerful assept of this gui system is allowing an automatic store for the state of a gui element, this means that when a gui is closed and re-opened
|
||||
the elements which have a store will retain they value even if the element was previously destroied. The store is not limited to only per player and can
|
||||
be catergorised by any method you want such as one that is shared between all players or by all players on a force. Using a method that is not limited to
|
||||
one player means that when one player changes the state of the element it will be automaticlly updated for all other player (even if the element is already drawn)
|
||||
and so this is a powerful and easy way to sync gui elements.
|
||||
|
||||
-- note the example below is the same as checkbox_example:add_store(Gui.player_store)
|
||||
checkbox_example:add_store(function(element)
|
||||
local player = Game.get_player_by_index(element.player_index)
|
||||
return player.force.name
|
||||
end)
|
||||
|
||||
Of course this tool is not limited to only player interactions; the current satate of a define can be gotten using a number of methods and the value can
|
||||
even be updated by the script and have all instances of the element define be updated. When you use a category then we must give a category to the get
|
||||
and set functions; in our case we used Gui.player_store which uses the player's name as the category which is why 'Cooldude2606' is given as a argument,
|
||||
if we did not set a function for add_store then all instances for all players have the same value and so a category is not required.
|
||||
|
||||
checkbox_example:get_store('Cooldude2606')
|
||||
Gui.get_store(name,'Cooldude2606')
|
||||
|
||||
checkbox_example:set_store('Cooldude2606',true)
|
||||
Gui.set_store(name,'Cooldude2606',true)
|
||||
|
||||
These methods use the Store module which means that if you have the need to access these sotre location (for example if you want to add a watch function) then
|
||||
you can get the store location of any define using checkbox_example.store
|
||||
|
||||
Important note about event handlers: when the store is updated it will also trigger the event handlers (such as on_element_update) for that define but only
|
||||
for the valid instances of the define which means if a player does not have the element drawn on a gui then it will not trigger the events; if you want a
|
||||
trigger for all updates then you can use on_store_update however you will be required to parse the category which may or may not be a
|
||||
player name (depends what store categorize function you use)
|
||||
|
||||
>>>> Example formating
|
||||
|
||||
local checkbox_example =
|
||||
Gui.new_checkbox()
|
||||
:set_caption('Example Checkbox')
|
||||
:set_tooltip('Example checkbox')
|
||||
:add_store(Gui.player_store)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Example checkbox is now: '..tostring(value))
|
||||
end)
|
||||
|
||||
>>>> Functions
|
||||
Gui._prototype_factory(tbl) --- Used internally to create new prototypes for element defines
|
||||
Gui._event_factory(name) --- Used internally to create event handler adders for element defines
|
||||
Gui._store_factory(callback) --- Used internally to create store adders for element defines
|
||||
Gui._sync_store_factory(callback) --- Used internally to create synced store adders for element defines
|
||||
Gui._define_factory(prototype) --- Used internally to create new element defines from a class prototype
|
||||
|
||||
Gui._prototype:uid() --- Gets the uid for the element define
|
||||
Gui._prototype:debug_name(name) --- Sets a debug alias for the define
|
||||
Gui._prototype:set_caption(caption) --- Sets the caption for the element define
|
||||
Gui._prototype:set_tooltip(tooltip) --- Sets the tooltip for the element define
|
||||
Gui._prototype:on_element_update(callback) --- Add a hander to run on the general value update event, different classes will handle this event differently
|
||||
|
||||
Gui._prototype:set_pre_authenticator(callback) --- Sets an authenticator that blocks the draw function if check fails
|
||||
Gui._prototype:set_post_authenticator(callback) --- Sets an authenticator that disables the element if check fails
|
||||
Gui._prototype:draw_to(element) --- Draws the element using what is in the draw_data table, allows use of authenticator if present, registers new instances if store present
|
||||
Gui.draw(name,element) --- Draws a copy of the element define to the parent element, see draw_to
|
||||
|
||||
Gui._prototype:add_store(categorize) --- Adds a store location for the define that will save the state of the element, categorize is a function that returns a string
|
||||
Gui._prototype:add_sync_store(location,categorize) --- Adds a store location for the define that will sync between games, categorize is a function that returns a string
|
||||
Gui._prototype:on_store_update(callback) --- Adds a event callback for when the store changes are other events are not gauenteted to be raised
|
||||
Gui.player_store(element) --- A categorize function to be used with add_store, each player has their own value
|
||||
Gui.force_store(element) --- A categorize function to be used with add_store, each force has its own value
|
||||
Gui.surface_store(element) --- A categorize function to be used with add_store, each surface has its own value
|
||||
|
||||
Gui._prototype:get_store(category) --- Gets the value in this elements store, category needed if categorize function used
|
||||
Gui._prototype:set_store(category,value) --- Sets the value in this elements store, category needed if categorize function used
|
||||
Gui.get_store(name,category) --- Gets the value that is stored for a given element define, category needed if categorize function used
|
||||
Gui.set_store(name,category,value) --- Sets the value stored for a given element define, category needed if categorize function used
|
||||
|
||||
Gui.toggle_enable(element) --- Will toggle the enabled state of an element
|
||||
Gui.toggle_visible(element) --- Will toggle the visiblity of an element
|
||||
]]
|
||||
local Gui = require 'utils.gui'
|
||||
local Game = require 'utils.game'
|
||||
local Global = require 'utils.global'
|
||||
local Store = require 'expcore.store'
|
||||
|
||||
Gui._prototype = {} -- Stores the base prototype of all gui defines
|
||||
Gui.classes = {} -- Stores the class types of gui defines
|
||||
Gui.defines = {} -- Stores the indivdual gui element definations
|
||||
Gui._prototype = {} -- Stores the base prototype of all element defines
|
||||
Gui.classes = {} -- Stores the class definations used to create element defines
|
||||
Gui.defines = {} -- Stores the indivdual element definations
|
||||
Gui.names = {} -- Stores debug names to link to gui uids
|
||||
Gui.instances = {} -- Stores runtime data of all active instances of each define
|
||||
Gui.instances = {} -- Stores runtime data of all active instances of an element define
|
||||
Global.register(Gui.instances,function(tbl)
|
||||
Gui.instances = tbl
|
||||
end)
|
||||
|
||||
--- Used internally to create new prototypes for element defines
|
||||
-- @tparam tbl table a table that will have functions added to it
|
||||
-- @treturn table the new table with the keys added to it
|
||||
function Gui._prototype_factory(tbl)
|
||||
for k,v in pairs(Gui._prototype) do
|
||||
if not tbl[k] then tbl[k] = v end
|
||||
@@ -19,7 +174,18 @@ function Gui._prototype_factory(tbl)
|
||||
return tbl
|
||||
end
|
||||
|
||||
--- Used internally to create event handler adders for element defines
|
||||
-- @tparam name string the key that the event will be stored under, should be the same as the event name
|
||||
-- @treturn function the function that can be used to add an event handler
|
||||
function Gui._event_factory(name)
|
||||
--- Gui._prototype:on_event(callback)
|
||||
--- Add a hander to run on this event, replace event with the event, different classes have different events
|
||||
-- @tparam callback function the function that will be called on the event
|
||||
-- callback param - player LuaPlayer - the player who owns the gui element
|
||||
-- callback param - element LuaGuiElement - the element that caused the event
|
||||
-- callback param - value any - (not always present) the updated value for the element
|
||||
-- callback param - ... any - other class defines may add more params
|
||||
-- @treturn self the element define to allow chaining
|
||||
return function(self,callback)
|
||||
if type(callback) ~= 'function' then
|
||||
return error('Event callback must be a function',2)
|
||||
@@ -30,7 +196,16 @@ function Gui._event_factory(name)
|
||||
end
|
||||
end
|
||||
|
||||
--- Used internally to create store adders for element defines
|
||||
-- @tparam callback a callback is called when there is an update to the stored value and stould set the state of the element
|
||||
-- @treturn function the function that can be used to add a store the the define
|
||||
function Gui._store_factory(callback)
|
||||
--- Gui._prototype:add_store(categorize)
|
||||
--- Adds a store location for the define that will save the state of the element, categorize is a function that returns a string
|
||||
-- @tparam[opt] categorize function if present will be called to convert an element into a category string
|
||||
-- categorize param - element LuaGuiElement - the element that needs to be converted
|
||||
-- categorize return - string - a determistic string that referses to a category such as player name or force name
|
||||
-- @treturn self the element define to allow chaining
|
||||
return function(self,categorize)
|
||||
if self.store then return end
|
||||
|
||||
@@ -39,6 +214,10 @@ function Gui._store_factory(callback)
|
||||
Gui.instances[self.name]={}
|
||||
|
||||
Store.register(self.store,function(value,category)
|
||||
if self.events.on_store_update then
|
||||
self.events.on_store_update(value,category)
|
||||
end
|
||||
|
||||
local instances = Gui.get_instances(self,category)
|
||||
if instances then
|
||||
|
||||
@@ -57,7 +236,17 @@ function Gui._store_factory(callback)
|
||||
end
|
||||
end
|
||||
|
||||
--- Used internally to create synced store adders for element defines
|
||||
-- @tparam callback a callback is called when there is an update to the stored value and stould set the state of the element
|
||||
-- @treturn function the function that can be used to add a sync store the the define
|
||||
function Gui._sync_store_factory(callback)
|
||||
--- Gui._prototype:add_sync_store(location,categorize)
|
||||
--- Adds a store location for the define that will sync between games, categorize is a function that returns a string
|
||||
-- @tparam location string a unique string location, unlike add_store a uid location should not be used to avoid migration problems
|
||||
-- @tparam[opt] categorize function if present will be called to convert an element into a category string
|
||||
-- categorize param - element LuaGuiElement - the element that needs to be converted
|
||||
-- categorize return - string - a determistic string that referses to a category such as player name or force name
|
||||
-- @treturn self the element define to allow chaining
|
||||
return function(self,location,categorize)
|
||||
if self.store then return end
|
||||
|
||||
@@ -70,6 +259,10 @@ function Gui._sync_store_factory(callback)
|
||||
Gui.instances[self.name]={}
|
||||
|
||||
Store.register_synced(self.store,function(value,category)
|
||||
if self.events.on_store_update then
|
||||
self.events.on_store_update(value,category)
|
||||
end
|
||||
|
||||
local instances = Gui.get_instances(self,category)
|
||||
if instances then
|
||||
|
||||
@@ -88,6 +281,9 @@ function Gui._sync_store_factory(callback)
|
||||
end
|
||||
end
|
||||
|
||||
--- Used internally to create new element defines from a class prototype
|
||||
-- @tparam prototype table the class prototype that will be used for the element define
|
||||
-- @treturn table the new element define with all functions accessed via __index metamethod
|
||||
function Gui._define_factory(prototype)
|
||||
local uid = Gui.uid_name()
|
||||
local define = setmetatable({
|
||||
@@ -106,36 +302,42 @@ function Gui._define_factory(prototype)
|
||||
return define
|
||||
end
|
||||
|
||||
--- Gets the uid for the config
|
||||
--- Gets the uid for the element define
|
||||
-- @treturn string the uid of this element define
|
||||
function Gui._prototype:uid()
|
||||
return self.name
|
||||
end
|
||||
|
||||
--- Sets an alias to the uid
|
||||
--- Sets a debug alias for the define
|
||||
-- @tparam name string the debug name for the element define that can be used to get this element define
|
||||
-- @treturn self the element define to allow chaining
|
||||
function Gui._prototype:debug_name(name)
|
||||
if name then
|
||||
self.debug_name = name
|
||||
Gui.names[name] = self.name
|
||||
Gui.names[self.name] = name
|
||||
else
|
||||
return self.debug_name
|
||||
end
|
||||
self.debug_name = name
|
||||
return self
|
||||
end
|
||||
|
||||
--- Sets the caption for the element config
|
||||
--- Sets the caption for the element define
|
||||
-- @tparam caption string the caption that will be drawn with the element
|
||||
-- @treturn self the element define to allow chaining
|
||||
function Gui._prototype:set_caption(caption)
|
||||
self.draw_data.caption = caption
|
||||
return self
|
||||
end
|
||||
|
||||
--- Sets the tooltip for the element config
|
||||
--- Sets the tooltip for the element define
|
||||
-- @tparam tooltip string the tooltip that will be displayed for this element when drawn
|
||||
-- @treturn self the element define to allow chaining
|
||||
function Gui._prototype:set_tooltip(tooltip)
|
||||
self.draw_data.tooltip = tooltip
|
||||
return self
|
||||
end
|
||||
|
||||
--- Sets an authenticator that blocks the draw function if check fails
|
||||
-- @tparam callback function the function that will be ran to test if the element should be drawn or not
|
||||
-- callback param - player LuaPlayer - the player that the element is being drawn to
|
||||
-- callback param - define_name string - the name of the define that is being drawn
|
||||
-- callback return - boolean - false will stop the element from being drawn
|
||||
-- @treturn self the element define to allow chaining
|
||||
function Gui._prototype:set_pre_authenticator(callback)
|
||||
if type(callback) ~= 'function' then
|
||||
return error('Pre authenticator callback must be a function')
|
||||
@@ -146,6 +348,11 @@ function Gui._prototype:set_pre_authenticator(callback)
|
||||
end
|
||||
|
||||
--- Sets an authenticator that disables the element if check fails
|
||||
-- @tparam callback function the function that will be ran to test if the element should be enabled or not
|
||||
-- callback param - player LuaPlayer - the player that the element is being drawn to
|
||||
-- callback param - define_name string - the name of the define that is being drawn
|
||||
-- callback return - boolean - false will disable the element
|
||||
-- @treturn self the element define to allow chaining
|
||||
function Gui._prototype:set_post_authenticator(callback)
|
||||
if type(callback) ~= 'function' then
|
||||
return error('Authenicater callback must be a function')
|
||||
@@ -155,19 +362,22 @@ function Gui._prototype:set_post_authenticator(callback)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Draws the element using what is in the draw_data table, allows use of authenticator if present
|
||||
--- Draws the element using what is in the draw_data table, allows use of authenticator if present, registers new instances if store present
|
||||
-- the data with in the draw_data is set up through the use of all the other functions
|
||||
-- @tparam element LuaGuiElement the element that the define will draw a copy of its self onto
|
||||
-- @treturn LuaGuiElement the new element that was drawn so styles can be applied
|
||||
function Gui._prototype:draw_to(element)
|
||||
if element[self.name] then return end
|
||||
local player = Game.get_player_by_index(element.player_index)
|
||||
|
||||
if self.pre_authenticator then
|
||||
if not self.pre_authenticator(player,self.clean_name or self.name) then return end
|
||||
if not self.pre_authenticator(player,self.name) then return end
|
||||
end
|
||||
|
||||
local new_element = element.add(self.draw_data)
|
||||
|
||||
if self.post_authenticator then
|
||||
new_element.enabled = self.post_authenticator(player,self.clean_name or self.name)
|
||||
new_element.enabled = self.post_authenticator(player,self.name)
|
||||
end
|
||||
|
||||
if self.store then
|
||||
@@ -183,7 +393,9 @@ function Gui._prototype:draw_to(element)
|
||||
return new_element
|
||||
end
|
||||
|
||||
--- Gets the value in this elements store
|
||||
--- Gets the value in this elements store, category needed if categorize function used
|
||||
-- @tparam category[opt] string the category to get such as player name or force name
|
||||
-- @treturn any the value that is stored for this define
|
||||
function Gui._prototype:get_store(category)
|
||||
if not self.store then return end
|
||||
if self.categorize then
|
||||
@@ -193,7 +405,10 @@ function Gui._prototype:get_store(category)
|
||||
end
|
||||
end
|
||||
|
||||
--- Sets the value in this elements store
|
||||
--- Sets the value in this elements store, category needed if categorize function used
|
||||
-- @tparam category[opt] string the category to get such as player name or force name
|
||||
-- @tparam value any the value to set for this define, must be valid for its type ie boolean for checkbox etc
|
||||
-- @treturn boolean true if the value was set
|
||||
function Gui._prototype:set_store(category,value)
|
||||
if not self.store then return end
|
||||
if self.categorize then
|
||||
@@ -203,7 +418,17 @@ function Gui._prototype:set_store(category,value)
|
||||
end
|
||||
end
|
||||
|
||||
--- Gets an element define give the uid, debug name or a copy of the element define
|
||||
-- @tparam name ?string|table the uid, debug name or define for the element define to get
|
||||
-- @tparam[opt] internal boolean when true the error trace is one level higher (used internally)
|
||||
-- @treturn table the element define that was found or an error
|
||||
function Gui.get_define(name,internal)
|
||||
if type(name) == 'table' then
|
||||
if name.name and Gui.defines[name.name] then
|
||||
return Gui.defines[name.name]
|
||||
end
|
||||
end
|
||||
|
||||
local define = Gui.defines[name]
|
||||
|
||||
if not define and Gui.names[name] then
|
||||
@@ -217,11 +442,16 @@ function Gui.get_define(name,internal)
|
||||
return define
|
||||
end
|
||||
|
||||
function Gui.get_instances(self,category)
|
||||
if not Gui.instances[self.name] then return end
|
||||
--- Gets all instances of the element define, mostly internal use and note invalid elements may be present in the return
|
||||
-- @tparam name string the uid or debug name for the define to get the instances for
|
||||
-- @tparam[opt] category string the category to get the instances for
|
||||
-- @treturn table a table of LuaGuiElements that might be invalid which belong to this define
|
||||
function Gui.get_instances(name,category)
|
||||
local define = Gui.get_define(name,true)
|
||||
if not Gui.instances[define.name] then return end
|
||||
|
||||
local instances = Gui.instances[self.name]
|
||||
if self.categorize then
|
||||
local instances = Gui.instances[define.name]
|
||||
if define.categorize then
|
||||
if not instances[category] then instances[category] = {} end
|
||||
return instances[category]
|
||||
|
||||
@@ -230,21 +460,60 @@ function Gui.get_instances(self,category)
|
||||
return instances
|
||||
end
|
||||
|
||||
--- Gets the value that is stored for a given element define, category needed if categorize function used
|
||||
-- @tparam name ?string|table the uid, debug name or define for the element define to get
|
||||
-- @tparam[opt] category string the category to get the value for
|
||||
-- @treturn any the value that is stored for this define
|
||||
function Gui.get_store(name,category)
|
||||
local define = Gui.get_define(name,true)
|
||||
return define:get_store(category)
|
||||
end
|
||||
|
||||
--- Sets the value stored for a given element define, category needed if categorize function used
|
||||
-- @tparam name ?string|table the uid, debug name or define for the element define to set
|
||||
-- @tparam[opt] category string the category to set the value for
|
||||
-- @tparam value any the value to set for the define, must be valid for its type ie boolean for a checkbox
|
||||
-- @treturn boolean true if the value was set
|
||||
function Gui.set_store(name,category,value)
|
||||
local define = Gui.get_define(name,true)
|
||||
return define:get_store(category,value)
|
||||
end
|
||||
|
||||
--- A categorize function to be used with add_store, each player has their own value
|
||||
-- @tparam element LuaGuiElement the element that will be converted to a string
|
||||
-- @treturn string the player's name who owns this element
|
||||
function Gui.player_store(element)
|
||||
local player = Game.get_player_by_index(element.player_index)
|
||||
return player.name
|
||||
end
|
||||
|
||||
--- A categorize function to be used with add_store, each force has its own value
|
||||
-- @tparam element LuaGuiElement the element that will be converted to a string
|
||||
-- @treturn string the player's force name who owns this element
|
||||
function Gui.force_store(element)
|
||||
local player = Game.get_player_by_index(element.player_index)
|
||||
return player.force.name
|
||||
end
|
||||
|
||||
--- A categorize function to be used with add_store, each surface has its own value
|
||||
-- @tparam element LuaGuiElement the element that will be converted to a string
|
||||
-- @treturn string the player's surface name who owns this element
|
||||
function Gui.surface_store(element)
|
||||
local player = Game.get_player_by_index(element.player_index)
|
||||
return player.surface.name
|
||||
end
|
||||
|
||||
--- Draws a copy of the element define to the parent element, see draw_to
|
||||
-- @tparam name ?string|table the uid, debug name or define for the element define to draw
|
||||
-- @tparam element LuaGuiEelement the parent element that it the define will be drawn to
|
||||
-- @treturn LuaGuiElement the new element that was created
|
||||
function Gui.draw(name,element)
|
||||
local define = Gui.get_define(name,true)
|
||||
return define:draw_to(element)
|
||||
end
|
||||
|
||||
--- Will toggle the enabled state of an element
|
||||
-- @tparam element LuaGuiElement the gui element to toggle
|
||||
function Gui.toggle_enable(element)
|
||||
if not element or not element.valid then return end
|
||||
if not element.enabled then
|
||||
@@ -254,6 +523,8 @@ function Gui.toggle_enable(element)
|
||||
end
|
||||
end
|
||||
|
||||
--- Will toggle the visiblity of an element
|
||||
-- @tparam element LuaGuiElement the gui element to toggle
|
||||
function Gui.toggle_visible(element)
|
||||
if not element or not element.valid then return end
|
||||
if not element.visible then
|
||||
|
||||
@@ -1,11 +1,33 @@
|
||||
--- Gui class define for dropdowns and list boxs
|
||||
--[[
|
||||
>>>> Functions
|
||||
Dropdown.new_dropdown(name) --- Creates a new dropdown element define
|
||||
Dropdown.new_list_box(name) --- Creates a new list box element define
|
||||
|
||||
Dropdown._prototype:on_element_update(callback) --- Registers a handler for when an element instance updates
|
||||
Dropdown._prototype:on_store_update(callback) --- Registers a handler for when the stored value updates
|
||||
|
||||
Dropdown._prototype:new_static_options(options,...) --- Adds new static options to the dropdown which will trigger the general callback
|
||||
Dropdown._prototype:new_dynamic_options(callback) --- Adds a callback which should return a table of values to be added as options for the dropdown (appended after static options)
|
||||
Dropdown._prototype:add_option_callback(option,callback) --- Adds a case specific callback which will only run when that option is selected (general case still triggered)
|
||||
|
||||
Dropdown.select_value(element,value) --- Selects the option from a dropdown or list box given the value rather than key
|
||||
Dropdown.get_selected_value(element) --- Returns the currently selected value rather than index
|
||||
|
||||
Other functions present from expcore.gui.core
|
||||
]]
|
||||
local Gui = require './core'
|
||||
local Game = require 'utils.game'
|
||||
|
||||
--- Event call for on_selection_state_changed and store update
|
||||
-- @tparam define table the define that this is acting on
|
||||
-- @tparam element LuaGuiElement the element that triggered the event
|
||||
-- @tparam value string the new option for the dropdown
|
||||
local function event_call(define,element,value)
|
||||
local player = Game.get_player_by_index(element.player_index)
|
||||
|
||||
if define.events.on_change then
|
||||
define.events.on_change(player,element,value)
|
||||
if define.events.on_element_update then
|
||||
define.events.on_element_update(player,element,value)
|
||||
end
|
||||
|
||||
if define.option_callbacks and define.option_callbacks[value] then
|
||||
@@ -13,20 +35,28 @@ local function event_call(define,element,value)
|
||||
end
|
||||
end
|
||||
|
||||
--- Store call for store update
|
||||
-- @tparam define table the define that this is acting on
|
||||
-- @tparam element LuaGuiElement the element that triggered the event
|
||||
-- @tparam value string the new option for the dropdown
|
||||
local _select_value
|
||||
local function store_call(self,element,value)
|
||||
local function store_call(define,element,value)
|
||||
_select_value(element,value)
|
||||
event_call(self,element,value)
|
||||
event_call(define,element,value)
|
||||
end
|
||||
|
||||
local Dropdown = {
|
||||
_prototype=Gui._prototype_factory{
|
||||
on_change = Gui._event_factory('on_change'),
|
||||
on_element_update = Gui._event_factory('on_element_update'),
|
||||
on_store_update = Gui._event_factory('on_store_update'),
|
||||
add_store = Gui._store_factory(store_call),
|
||||
add_sync_store = Gui._sync_store_factory(store_call)
|
||||
}
|
||||
}
|
||||
|
||||
--- Creates a new dropdown element define
|
||||
-- @tparam[opt] name string the optional debug name that can be added
|
||||
-- @treturn table the new dropdown element define
|
||||
function Dropdown.new_dropdown(name)
|
||||
|
||||
local self = Gui._define_factory(Dropdown._prototype)
|
||||
@@ -72,6 +102,20 @@ function Dropdown.new_dropdown(name)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Creates a new list box element define
|
||||
-- @tparam[opt] name string the optional debug name that can be added
|
||||
-- @treturn table the new list box element define
|
||||
function Dropdown.new_list_box(name)
|
||||
local self = Dropdown.new_dropdown(name)
|
||||
self.draw_data.type = 'list-box'
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Adds new static options to the dropdown which will trigger the general callback
|
||||
-- @tparam options ?string|table either a table of option strings or the first option string, with a table values are the options
|
||||
-- @tparam[opt] ... when options is not a table you can add the options one after each other
|
||||
-- @tparam self the define to allow chaining
|
||||
function Dropdown._prototype:new_static_options(options,...)
|
||||
if type(options) == 'string' then
|
||||
options = {options}
|
||||
@@ -86,6 +130,12 @@ function Dropdown._prototype:new_static_options(options,...)
|
||||
end
|
||||
Dropdown._prototype.add_options = Dropdown._prototype.new_static_options
|
||||
|
||||
--- Adds a callback which should return a table of values to be added as options for the dropdown (appended after static options)
|
||||
-- @tparam callback function the function that will run to get the options for the dropdown
|
||||
-- callback param - player LuaPlayer - the player that the element is being drawn to
|
||||
-- callback param - element LuaGuiElement - the element that is being drawn
|
||||
-- callback return - table - the values of this table will be appended to the static options of the dropdown
|
||||
-- @tparam self the define to allow chaining
|
||||
function Dropdown._prototype:new_dynamic_options(callback)
|
||||
if type(callback) ~= 'function' then
|
||||
return error('Dynamic options callback must be a function',2)
|
||||
@@ -95,6 +145,13 @@ function Dropdown._prototype:new_dynamic_options(callback)
|
||||
end
|
||||
Dropdown._prototype.add_dynamic = Dropdown._prototype.new_dynamic_options
|
||||
|
||||
--- Adds a case specific callback which will only run when that option is selected (general case still triggered)
|
||||
-- @tparam option string the name of the option to trigger the callback on; if not already added then will be added as an option
|
||||
-- @tparam callback function the function that will be called when that option is selected
|
||||
-- callback param - player LuaPlayer - the player who owns the gui element
|
||||
-- callback param - element LuaGuiElement - the element which is being effected
|
||||
-- callback param - value string - the new option that has been selected
|
||||
-- @tparam self the define to allow chaining
|
||||
function Dropdown._prototype:add_option_callback(option,callback)
|
||||
if not self.option_callbacks then self.option_callbacks = {} end
|
||||
if not self.options then self.options = {} end
|
||||
@@ -107,6 +164,10 @@ function Dropdown._prototype:add_option_callback(option,callback)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Selects the option from a dropdown or list box given the value rather than key
|
||||
-- @tparam element LuaGuiElement the element that contains the option
|
||||
-- @tparam value string the option to select from the dropdown
|
||||
-- @treturn number the key where the value was
|
||||
function Dropdown.select_value(element,value)
|
||||
for k,item in pairs(element.items) do
|
||||
if item == value then
|
||||
@@ -117,16 +178,12 @@ function Dropdown.select_value(element,value)
|
||||
end
|
||||
_select_value = Dropdown.select_value
|
||||
|
||||
--- Returns the currently selected value rather than index
|
||||
-- @tparam element LuaGuiElement the gui element that you want to get the value of
|
||||
-- @treturn string the value that is currently selected
|
||||
function Dropdown.get_selected_value(element)
|
||||
local index = element.selected_index
|
||||
return element.items[index]
|
||||
end
|
||||
|
||||
function Dropdown.new_list_box(name)
|
||||
local self = Dropdown.new_dropdown(name)
|
||||
self.draw_data.type = 'list-box'
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
return Dropdown
|
||||
@@ -1,15 +1,36 @@
|
||||
--- Gui class defines for elem buttons
|
||||
--[[
|
||||
>>>> Functions
|
||||
ElemButton.new_elem_button(name) --- Creates a new elem button element define
|
||||
|
||||
ElemButton._prototype:on_element_update(callback) --- Registers a handler for when an element instance updates
|
||||
ElemButton._prototype:on_store_update(callback) --- Registers a handler for when the stored value updates
|
||||
|
||||
ElemButton._prototype:set_type(type) --- Sets the type of the elem button, the type is required so this must be called at least once
|
||||
ElemButton._prototype:set_default(value) --- Sets the default value for the elem button, this may be a function or a string
|
||||
|
||||
Other functions present from expcore.gui.core
|
||||
]]
|
||||
local Gui = require './core'
|
||||
local Game = require 'utils.game'
|
||||
|
||||
--- Event call for on_elem_changed and store update
|
||||
-- @tparam define table the define that this is acting on
|
||||
-- @tparam element LuaGuiElement the element that triggered the event
|
||||
-- @tparam value string the new value for the elem button
|
||||
local function event_call(define,element,value)
|
||||
local player = Game.get_player_by_index(element.player_index)
|
||||
|
||||
if define.events.on_change then
|
||||
define.events.on_change(player,element,value)
|
||||
if define.events.on_element_update then
|
||||
define.events.on_element_update(player,element,value)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
--- Store call for store update
|
||||
-- @tparam define table the define that this is acting on
|
||||
-- @tparam element LuaGuiElement the element that triggered the event
|
||||
-- @tparam value string the new value for the elem button
|
||||
local function store_call(self,element,value)
|
||||
element.elem_value = value
|
||||
event_call(self,element,value)
|
||||
@@ -17,12 +38,16 @@ end
|
||||
|
||||
local ElemButton = {
|
||||
_prototype=Gui._prototype_factory{
|
||||
on_change = Gui._event_factory('on_change'),
|
||||
on_element_update = Gui._event_factory('on_element_update'),
|
||||
on_store_update = Gui._event_factory('on_store_update'),
|
||||
add_store = Gui._store_factory(store_call),
|
||||
add_sync_store = Gui._sync_store_factory(store_call)
|
||||
}
|
||||
}
|
||||
|
||||
--- Creates a new elem button element define
|
||||
-- @tparam[opt] name string the optional debug name that can be added
|
||||
-- @treturn table the new elem button element define
|
||||
function ElemButton.new_elem_button(name)
|
||||
|
||||
local self = Gui._define_factory(ElemButton._prototype)
|
||||
@@ -64,11 +89,17 @@ function ElemButton.new_elem_button(name)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Sets the type of the elem button, the type is required so this must be called at least once
|
||||
-- @tparam type string the type that this elem button is see factorio api
|
||||
-- @treturn the element define to allow for chaining
|
||||
function ElemButton._prototype:set_type(type)
|
||||
self.draw_data.elem_type = type
|
||||
return self
|
||||
end
|
||||
|
||||
--- Sets the default value for the elem button, this may be a function or a string
|
||||
-- @tparam value ?string|function a string will be a static default and a function will be called when drawn to get the default
|
||||
-- @treturn the element define to allow for chaining
|
||||
function ElemButton._prototype:set_default(value)
|
||||
self.default = value
|
||||
if type(value) ~= 'function' then
|
||||
|
||||
@@ -1,6 +1,24 @@
|
||||
--- Gui class define for silders
|
||||
--[[
|
||||
>>>> Functions
|
||||
Slider.new_slider(name) --- Creates a new slider element define
|
||||
|
||||
Slider._prototype:on_element_update(callback) --- Registers a handler for when an element instance updates
|
||||
Slider._prototype:on_store_update(callback) --- Registers a handler for when the stored value updates
|
||||
|
||||
Slider._prototype:set_range(min,max) --- Sets the range of a slider, if not used will use default values for a slider
|
||||
Slider._prototype:draw_label(element) --- Draws a new label and links its value to the value of this slider, if no store then it will only show one value per player
|
||||
Slider._prototype:enable_auto_draw_label(state) --- Enables auto draw of the label, the label will share the same parent element as the slider
|
||||
|
||||
Other functions present from expcore.gui.core
|
||||
]]
|
||||
local Gui = require './core'
|
||||
local Game = require 'utils.game'
|
||||
|
||||
--- Gets the active lables for a define
|
||||
-- @tparam define table the define to get the labels for
|
||||
-- @tparam element LuaGuiElement the element that will be used to get the category
|
||||
-- @treturn table the table of active instances for the slider lables
|
||||
local function get_labels(define,element)
|
||||
local function cat(e)
|
||||
return e.player_index
|
||||
@@ -19,6 +37,9 @@ local function get_labels(define,element)
|
||||
return instances
|
||||
end
|
||||
|
||||
--- Gets and updates the label values
|
||||
-- @tparam define table the define to get the labels for
|
||||
-- @tparam element LuaGuiElement the element that will be used to get the category
|
||||
local function update_lables(define,element)
|
||||
local instances = get_labels(define,element)
|
||||
local value = element.slider_value
|
||||
@@ -33,6 +54,10 @@ local function update_lables(define,element)
|
||||
end
|
||||
end
|
||||
|
||||
--- Event call for on_value_changed and store update
|
||||
-- @tparam define table the define that this is acting on
|
||||
-- @tparam element LuaGuiElement the element that triggered the event
|
||||
-- @tparam value number the new value for the slider
|
||||
local function event_call(define,element,value)
|
||||
local player = Game.get_player_by_index(element.player_index)
|
||||
|
||||
@@ -40,26 +65,34 @@ local function event_call(define,element,value)
|
||||
local delta = max-min
|
||||
local percent = delta == 0 and 0 or (value-min)/delta
|
||||
|
||||
if define.events.on_change then
|
||||
define.events.on_change(player,element,value,percent)
|
||||
if define.events.on_element_update then
|
||||
define.events.on_element_update(player,element,value,percent)
|
||||
end
|
||||
|
||||
update_lables(define,element)
|
||||
end
|
||||
|
||||
local function store_call(self,element,value)
|
||||
--- Store call for store update
|
||||
-- @tparam define table the define that this is acting on
|
||||
-- @tparam element LuaGuiElement the element that triggered the event
|
||||
-- @tparam value number the new value for the slider
|
||||
local function store_call(define,element,value)
|
||||
element.slider_value = value
|
||||
event_call(self,element,value)
|
||||
event_call(define,element,value)
|
||||
end
|
||||
|
||||
local Slider = {
|
||||
_prototype=Gui._prototype_factory{
|
||||
on_change = Gui._event_factory('on_change'),
|
||||
on_element_update = Gui._event_factory('on_element_update'),
|
||||
on_store_update = Gui._event_factory('on_store_update'),
|
||||
add_store = Gui._store_factory(store_call),
|
||||
add_sync_store = Gui._sync_store_factory(store_call)
|
||||
}
|
||||
}
|
||||
|
||||
--- Creates a new slider element define
|
||||
-- @tparam[opt] name string the optional debug name that can be added
|
||||
-- @treturn table the new slider element define
|
||||
function Slider.new_slider(name)
|
||||
|
||||
local self = Gui._define_factory(Slider._prototype)
|
||||
@@ -112,6 +145,10 @@ function Slider.new_slider(name)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Sets the range of a slider, if not used will use default values for a slider
|
||||
-- @tparam[opt] min number the minimum value that the slider can take
|
||||
-- @tparam[opt] max number the maximum value that the slider can take
|
||||
-- @treturn self the define to allow chaining
|
||||
function Slider._prototype:set_range(min,max)
|
||||
self.min = min
|
||||
self.max = max
|
||||
@@ -127,6 +164,9 @@ function Slider._prototype:set_range(min,max)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Draws a new label and links its value to the value of this slider, if no store then it will only show one value per player
|
||||
-- @tparam element LuaGuiElement the parent element that the lable will be drawn to
|
||||
-- @treturn LuaGuiElement the new label element so that styles can be applied
|
||||
function Slider._prototype:draw_label(element)
|
||||
local name = self.name..'-label'
|
||||
if element[name] then return end
|
||||
@@ -151,6 +191,9 @@ function Slider._prototype:draw_label(element)
|
||||
return new_element
|
||||
end
|
||||
|
||||
--- Enables auto draw of the label, the label will share the same parent element as the slider
|
||||
-- @tparam[opt=true] state boolean when false will disable the auto draw of the label
|
||||
-- @treturn self the define to allow chaining
|
||||
function Slider._prototype:enable_auto_draw_label(state)
|
||||
if state == false then
|
||||
self.auto_label = false
|
||||
|
||||
@@ -8,11 +8,6 @@ local Game = require 'utils.game'
|
||||
|
||||
local tests = {}
|
||||
|
||||
local function categozie_by_player(element)
|
||||
local player = Game.get_player_by_index(element.player_index)
|
||||
return player.name
|
||||
end
|
||||
|
||||
--[[
|
||||
Toolbar Tests
|
||||
> No display - Toolbar button with no display
|
||||
@@ -25,7 +20,7 @@ Gui.new_toolbar_button('click-1')
|
||||
:set_post_authenticator(function(player,button_name)
|
||||
return global.click_one
|
||||
end)
|
||||
:on_click(function(player,element,event)
|
||||
:on_click(function(player,element)
|
||||
player.print('CLICK 1')
|
||||
end)
|
||||
|
||||
@@ -34,7 +29,7 @@ Gui.new_toolbar_button('click-2')
|
||||
:set_post_authenticator(function(player,button_name)
|
||||
return global.click_two
|
||||
end)
|
||||
:on_click(function(player,element,event)
|
||||
:on_click(function(player,element)
|
||||
player.print('CLICK 2')
|
||||
end)
|
||||
|
||||
@@ -43,7 +38,7 @@ Gui.new_toolbar_button('click-3')
|
||||
:set_post_authenticator(function(player,button_name)
|
||||
return global.click_three
|
||||
end)
|
||||
:on_click(function(player,element,event)
|
||||
:on_click(function(player,element)
|
||||
player.print('CLICK 3')
|
||||
end)
|
||||
|
||||
@@ -52,7 +47,7 @@ Gui.new_toolbar_button('gui-test-open')
|
||||
:set_post_authenticator(function(player,button_name)
|
||||
return global.show_test_gui
|
||||
end)
|
||||
:on_click(function(player,_element,event)
|
||||
:on_click(function(player,_element)
|
||||
if player.gui.center.TestGui then player.gui.center.TestGui.destroy() return end
|
||||
|
||||
local frame = player.gui.center.add{
|
||||
@@ -108,7 +103,7 @@ end)
|
||||
local button_no_display =
|
||||
Gui.new_button('test-button-no-display')
|
||||
:set_tooltip('Button no display')
|
||||
:on_click(function(player,element,event)
|
||||
:on_click(function(player,element)
|
||||
player.print('Button no display')
|
||||
global.test_auth_button = not global.test_auth_button
|
||||
player.print('Auth Button auth state: '..tostring(global.test_auth_button))
|
||||
@@ -118,7 +113,7 @@ local button_with_caption =
|
||||
Gui.new_button('test-button-with-caption')
|
||||
:set_tooltip('Button with caption')
|
||||
:set_caption('Button Caption')
|
||||
:on_click(function(player,element,event)
|
||||
:on_click(function(player,element)
|
||||
player.print('Button with caption')
|
||||
end)
|
||||
|
||||
@@ -126,7 +121,7 @@ local button_with_icon =
|
||||
Gui.new_button('test-button-with-icon')
|
||||
:set_tooltip('Button with icons')
|
||||
:set_sprites('utility/warning_icon','utility/warning','utility/warning_white')
|
||||
:on_click(function(player,element,event)
|
||||
:on_click(function(player,element)
|
||||
player.print('Button with icons')
|
||||
end)
|
||||
|
||||
@@ -136,7 +131,7 @@ Gui.new_button('test-button-with-auth')
|
||||
:set_post_authenticator(function(player,button_name)
|
||||
return global.test_auth_button
|
||||
end)
|
||||
:on_click(function(player,element,event)
|
||||
:on_click(function(player,element)
|
||||
player.print('Button with auth')
|
||||
end)
|
||||
|
||||
@@ -159,7 +154,7 @@ local checkbox_local =
|
||||
Gui.new_checkbox('test-checkbox-local')
|
||||
:set_tooltip('Checkbox local')
|
||||
:set_caption('Checkbox Local')
|
||||
:on_change(function(player,element,state)
|
||||
:on_element_update(function(player,element,state)
|
||||
player.print('Checkbox local: '..tostring(state))
|
||||
end)
|
||||
|
||||
@@ -168,7 +163,7 @@ Gui.new_checkbox('test-checkbox-store-game')
|
||||
:set_tooltip('Checkbox store game')
|
||||
:set_caption('Checkbox Store Game')
|
||||
:add_store()
|
||||
:on_change(function(player,element,state)
|
||||
:on_element_update(function(player,element,state)
|
||||
player.print('Checkbox store game: '..tostring(state))
|
||||
end)
|
||||
|
||||
@@ -176,11 +171,8 @@ local checkbox_force =
|
||||
Gui.new_checkbox('test-checkbox-store-force')
|
||||
:set_tooltip('Checkboc store force')
|
||||
:set_caption('Checkbox Store Force')
|
||||
:add_store(function(element)
|
||||
local player = Game.get_player_by_index(element.player_index)
|
||||
return player.force.name
|
||||
end)
|
||||
:on_change(function(player,element,state)
|
||||
:add_store(Gui.force_store)
|
||||
:on_element_update(function(player,element,state)
|
||||
player.print('Checkbox store force: '..tostring(state))
|
||||
end)
|
||||
|
||||
@@ -188,8 +180,8 @@ local checkbox_player =
|
||||
Gui.new_checkbox('test-checkbox-store-player')
|
||||
:set_tooltip('Checkbox store player')
|
||||
:set_caption('Checkbox Store Player')
|
||||
:add_store(categozie_by_player)
|
||||
:on_change(function(player,element,state)
|
||||
:add_store(Gui.player_store)
|
||||
:on_element_update(function(player,element,state)
|
||||
player.print('Checkbox store player: '..tostring(state))
|
||||
end)
|
||||
|
||||
@@ -211,7 +203,7 @@ local radiobutton_local =
|
||||
Gui.new_radiobutton('test-radiobutton-local')
|
||||
:set_tooltip('Radiobutton local')
|
||||
:set_caption('Radiobutton Local')
|
||||
:on_change(function(player,element,state)
|
||||
:on_element_update(function(player,element,state)
|
||||
player.print('Radiobutton local: '..tostring(state))
|
||||
end)
|
||||
|
||||
@@ -219,22 +211,22 @@ local radiobutton_player =
|
||||
Gui.new_radiobutton('test-radiobutton-store')
|
||||
:set_tooltip('Radiobutton store')
|
||||
:set_caption('Radiobutton Store')
|
||||
:add_store(categozie_by_player)
|
||||
:on_change(function(player,element,state)
|
||||
:add_store(Gui.player_store)
|
||||
:on_element_update(function(player,element,state)
|
||||
player.print('Radiobutton store: '..tostring(state))
|
||||
end)
|
||||
|
||||
local radiobutton_option_set =
|
||||
Gui.new_radiobutton_option_set('gui.test.share',function(value,category)
|
||||
game.print('Radiobutton option set for: '..category..' is now: '..tostring(value))
|
||||
end,categozie_by_player)
|
||||
end,Gui.player_store)
|
||||
|
||||
local radiobutton_option_one =
|
||||
Gui.new_radiobutton('test-radiobutton-option-one')
|
||||
:set_tooltip('Radiobutton option set')
|
||||
:set_caption('Radiobutton Option One')
|
||||
:add_as_option(radiobutton_option_set,'One')
|
||||
:on_change(function(player,element,state)
|
||||
:on_element_update(function(player,element,state)
|
||||
player.print('Radiobutton option one: '..tostring(state))
|
||||
end)
|
||||
|
||||
@@ -243,7 +235,7 @@ Gui.new_radiobutton('test-radiobutton-option-two')
|
||||
:set_tooltip('Radiobutton option set')
|
||||
:set_caption('Radiobutton Option Two')
|
||||
:add_as_option(radiobutton_option_set,'Two')
|
||||
:on_change(function(player,element,state)
|
||||
:on_element_update(function(player,element,state)
|
||||
player.print('Radiobutton option two: '..tostring(state))
|
||||
end)
|
||||
|
||||
@@ -252,7 +244,7 @@ Gui.new_radiobutton('test-radiobutton-option-three')
|
||||
:set_tooltip('Radiobutton option set')
|
||||
:set_caption('Radiobutton Option Three')
|
||||
:add_as_option(radiobutton_option_set,'Three')
|
||||
:on_change(function(player,element,state)
|
||||
:on_element_update(function(player,element,state)
|
||||
player.print('Radiobutton option three: '..tostring(state))
|
||||
end)
|
||||
|
||||
@@ -278,7 +270,7 @@ local dropdown_local_static_general =
|
||||
Gui.new_dropdown('test-dropdown-local-static-general')
|
||||
:set_tooltip('Dropdown local static general')
|
||||
:add_options('One','Two','Three','Four')
|
||||
:on_change(function(player,element,value)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Dropdown local static general: '..tostring(value))
|
||||
end)
|
||||
|
||||
@@ -286,8 +278,8 @@ local dropdown_player_static_general =
|
||||
Gui.new_dropdown('test-dropdown-store-static-general')
|
||||
:set_tooltip('Dropdown store static general')
|
||||
:add_options('One','Two','Three','Four')
|
||||
:add_store(categozie_by_player)
|
||||
:on_change(function(player,element,value)
|
||||
:add_store(Gui.player_store)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Dropdown store static general: '..tostring(value))
|
||||
end)
|
||||
|
||||
@@ -303,7 +295,7 @@ Gui.new_dropdown('test-dropdown-local-static-case')
|
||||
:add_option_callback('Two',print_option_selected_1)
|
||||
:add_option_callback('Three',print_option_selected_1)
|
||||
:add_option_callback('Four',print_option_selected_1)
|
||||
:on_change(function(player,element,value)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Dropdown local static case (general): '..tostring(value))
|
||||
end)
|
||||
|
||||
@@ -314,13 +306,13 @@ end
|
||||
local dropdown_player_static_case =
|
||||
Gui.new_dropdown('test-dropdown-store-static-case')
|
||||
:set_tooltip('Dropdown store static case')
|
||||
:add_store(categozie_by_player)
|
||||
:add_store(Gui.player_store)
|
||||
:add_options('One','Two')
|
||||
:add_option_callback('One',print_option_selected_2)
|
||||
:add_option_callback('Two',print_option_selected_2)
|
||||
:add_option_callback('Three',print_option_selected_2)
|
||||
:add_option_callback('Four',print_option_selected_2)
|
||||
:on_change(function(player,element,value)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Dropdown store static case (general): '..tostring(value))
|
||||
end)
|
||||
|
||||
@@ -331,7 +323,7 @@ Gui.new_dropdown('test-dropdown-local-dynamic')
|
||||
:add_dynamic(function(player,element)
|
||||
return table_keys(Colors)
|
||||
end)
|
||||
:on_change(function(player,element,value)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Dropdown local dynamic: '..tostring(value))
|
||||
end)
|
||||
|
||||
@@ -342,8 +334,8 @@ Gui.new_dropdown('test-dropdown-store-dynamic')
|
||||
:add_dynamic(function(player,element)
|
||||
return table_keys(Colors)
|
||||
end)
|
||||
:add_store(categozie_by_player)
|
||||
:on_change(function(player,element,value)
|
||||
:add_store(Gui.player_store)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Dropdown store dynamic: '..tostring(value))
|
||||
end)
|
||||
|
||||
@@ -366,7 +358,7 @@ local list_box_local =
|
||||
Gui.new_list_box('test-list-box-local')
|
||||
:set_tooltip('List box local')
|
||||
:add_options('One','Two','Three','Four')
|
||||
:on_change(function(player,element,value)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Dropdown local: '..tostring(value))
|
||||
end)
|
||||
|
||||
@@ -374,8 +366,8 @@ local list_box_player =
|
||||
Gui.new_list_box('test-list-box-store')
|
||||
:set_tooltip('List box store')
|
||||
:add_options('One','Two','Three','Four')
|
||||
:add_store(categozie_by_player)
|
||||
:on_change(function(player,element,value)
|
||||
:add_store(Gui.player_store)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Dropdown store: '..tostring(value))
|
||||
end)
|
||||
|
||||
@@ -397,15 +389,15 @@ tests["List Boxs"] = {
|
||||
local slider_local_default =
|
||||
Gui.new_slider('test-slider-local-default')
|
||||
:set_tooltip('Silder local default')
|
||||
:on_change(function(player,element,value,percent)
|
||||
:on_element_update(function(player,element,value,percent)
|
||||
player.print('Slider local default: '..tostring(math.round(value))..' '..tostring(math.round(percent,1)))
|
||||
end)
|
||||
|
||||
local slider_player_default =
|
||||
Gui.new_slider('test-slider-store-default')
|
||||
:set_tooltip('Silder store default')
|
||||
:add_store(categozie_by_player)
|
||||
:on_change(function(player,element,value,percent)
|
||||
:add_store(Gui.player_store)
|
||||
:on_element_update(function(player,element,value,percent)
|
||||
player.print('Slider store default: '..tostring(math.round(value))..' '..tostring(math.round(percent,1)))
|
||||
end)
|
||||
|
||||
@@ -413,7 +405,7 @@ local slider_static =
|
||||
Gui.new_slider('test-slider-static-range')
|
||||
:set_tooltip('Silder static range')
|
||||
:set_range(5,50)
|
||||
:on_change(function(player,element,value,percent)
|
||||
:on_element_update(function(player,element,value,percent)
|
||||
player.print('Slider static range: '..tostring(math.round(value))..' '..tostring(math.round(percent,1)))
|
||||
end)
|
||||
|
||||
@@ -425,7 +417,7 @@ Gui.new_slider('test-slider-dynamic-range')
|
||||
end,function(player,element)
|
||||
return player.index + 4
|
||||
end)
|
||||
:on_change(function(player,element,value,percent)
|
||||
:on_element_update(function(player,element,value,percent)
|
||||
player.print('Slider dynamic range: '..tostring(math.round(value))..' '..tostring(math.round(percent,1)))
|
||||
end)
|
||||
|
||||
@@ -433,7 +425,7 @@ local label_slider_local =
|
||||
Gui.new_slider('test-slider-local-label')
|
||||
:set_tooltip('Silder local label')
|
||||
:enable_auto_draw_label()
|
||||
:on_change(function(player,element,value,percent)
|
||||
:on_element_update(function(player,element,value,percent)
|
||||
player.print('Slider local label: '..tostring(math.round(value))..' '..tostring(math.round(percent,1)))
|
||||
end)
|
||||
|
||||
@@ -441,8 +433,8 @@ local label_slider_player =
|
||||
Gui.new_slider('test-slider-store-label')
|
||||
:set_tooltip('Silder store label')
|
||||
:enable_auto_draw_label()
|
||||
:add_store(categozie_by_player)
|
||||
:on_change(function(player,element,value,percent)
|
||||
:add_store(Gui.player_store)
|
||||
:on_element_update(function(player,element,value,percent)
|
||||
player.print('Slider store label: '..tostring(math.round(value))..' '..tostring(math.round(percent,1)))
|
||||
end)
|
||||
|
||||
@@ -472,22 +464,22 @@ tests.Sliders = {
|
||||
local text_filed_local =
|
||||
Gui.new_text_filed('test-text-field-local')
|
||||
:set_tooltip('Text field local')
|
||||
:on_change(function(player,element,value)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Text field local: '..value)
|
||||
end)
|
||||
|
||||
local text_filed_store =
|
||||
Gui.new_text_filed('test-text-field-store')
|
||||
:set_tooltip('Text field store')
|
||||
:add_store(categozie_by_player)
|
||||
:on_change(function(player,element,value)
|
||||
:add_store(Gui.player_store)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Text field store: '..value)
|
||||
end)
|
||||
|
||||
local text_box_local =
|
||||
Gui.new_text_box('test-text-box-local')
|
||||
:set_tooltip('Text box local')
|
||||
:on_change(function(player,element,value)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Text box local: '..value)
|
||||
end)
|
||||
|
||||
@@ -496,7 +488,7 @@ Gui.new_text_box('test-text-box-wrap')
|
||||
:set_tooltip('Text box wrap')
|
||||
:set_selectable(false)
|
||||
:set_word_wrap()
|
||||
:on_change(function(player,element,value)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Text box wrap: '..value)
|
||||
end)
|
||||
|
||||
@@ -519,7 +511,7 @@ local elem_local =
|
||||
Gui.new_elem_button('test-elem-local')
|
||||
:set_tooltip('Elem')
|
||||
:set_type('item')
|
||||
:on_change(function(player,element,value)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Elem: '..value)
|
||||
end)
|
||||
|
||||
@@ -528,7 +520,7 @@ Gui.new_elem_button('test-elem-default')
|
||||
:set_tooltip('Elem default')
|
||||
:set_type('item')
|
||||
:set_default('iron-plate')
|
||||
:on_change(function(player,element,value)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Elem default: '..value)
|
||||
end)
|
||||
|
||||
@@ -539,7 +531,7 @@ Gui.new_elem_button('test-elem-function')
|
||||
:set_default(function(player,element)
|
||||
return 'iron-plate'
|
||||
end)
|
||||
:on_change(function(player,element,value)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Elem function: '..value)
|
||||
end)
|
||||
|
||||
@@ -547,8 +539,8 @@ local elem_store =
|
||||
Gui.new_elem_button('test-elem-store')
|
||||
:set_tooltip('Elem store')
|
||||
:set_type('item')
|
||||
:add_store(categozie_by_player)
|
||||
:on_change(function(player,element,value)
|
||||
:add_store(Gui.player_store)
|
||||
:on_element_update(function(player,element,value)
|
||||
player.print('Elem store: '..value)
|
||||
end)
|
||||
|
||||
|
||||
@@ -1,15 +1,39 @@
|
||||
--- Gui class define for text fields and text boxs
|
||||
--[[
|
||||
>>>> Functions
|
||||
Text.new_text_field(name) --- Creates a new text field element define
|
||||
Text._prototype_field:on_element_update(callback) --- Registers a handler for when an element instance updates
|
||||
Text._prototype_field:on_store_update(callback) --- Registers a handler for when the stored value updates
|
||||
|
||||
Text.new_text_box(name) --- Creates a new text box element define
|
||||
Text._prototype_field:on_element_update(callback) --- Registers a handler for when an element instance updates
|
||||
Text._prototype_field:on_store_update(callback) --- Registers a handler for when the stored value updates
|
||||
Text._prototype_box:set_selectable(state) --- Sets the text box to be selectable
|
||||
Text._prototype_box:set_word_wrap(state) --- Sets the text box to have word wrap
|
||||
Text._prototype_box:set_read_only(state) --- Sets the text box to be read only
|
||||
|
||||
Other functions present from expcore.gui.core
|
||||
]]
|
||||
local Gui = require './core'
|
||||
local Game = require 'utils.game'
|
||||
|
||||
--- Event call for on_text_changed and store update
|
||||
-- @tparam define table the define that this is acting on
|
||||
-- @tparam element LuaGuiElement the element that triggered the event
|
||||
-- @tparam value string the new text for the text field
|
||||
local function event_call(define,element,value)
|
||||
local player = Game.get_player_by_index(element.player_index)
|
||||
|
||||
if define.events.on_change then
|
||||
define.events.on_change(player,element,value)
|
||||
if define.events.on_element_update then
|
||||
define.events.on_element_update(player,element,value)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
--- Store call for store update
|
||||
-- @tparam define table the define that this is acting on
|
||||
-- @tparam element LuaGuiElement the element that triggered the event
|
||||
-- @tparam value string the new text for the text field
|
||||
local function store_call(self,element,value)
|
||||
element.text = value
|
||||
event_call(self,element,value)
|
||||
@@ -17,17 +41,22 @@ end
|
||||
|
||||
local Text = {
|
||||
_prototype_field=Gui._prototype_factory{
|
||||
on_change = Gui._event_factory('on_change'),
|
||||
on_element_update = Gui._event_factory('on_element_update'),
|
||||
on_store_update = Gui._event_factory('on_store_update'),
|
||||
add_store = Gui._store_factory(store_call),
|
||||
add_sync_store = Gui._sync_store_factory(store_call)
|
||||
},
|
||||
_prototype_box=Gui._prototype_factory{
|
||||
on_change = Gui._event_factory('on_change'),
|
||||
on_element_update = Gui._event_factory('on_element_update'),
|
||||
on_store_update = Gui._event_factory('on_store_update'),
|
||||
add_store = Gui._store_factory(store_call),
|
||||
add_sync_store = Gui._sync_store_factory(store_call)
|
||||
}
|
||||
}
|
||||
|
||||
--- Creates a new text field element define
|
||||
-- @tparam[opt] name string the optional debug name that can be added
|
||||
-- @treturn table the new text field element define
|
||||
function Text.new_text_field(name)
|
||||
|
||||
local self = Gui._define_factory(Text._prototype_field)
|
||||
@@ -75,6 +104,9 @@ function Text.new_text_field(name)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Creates a new text box element define
|
||||
-- @tparam[opt] name string the optional debug name that can be added
|
||||
-- @treturn table the new text box element define
|
||||
function Text.new_text_box(name)
|
||||
local self = Text.new_text_field(name)
|
||||
self.draw_data.type = 'text-box'
|
||||
@@ -85,6 +117,9 @@ function Text.new_text_box(name)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Sets the text box to be selectable
|
||||
-- @tparam[opt=true] state boolean when false will set the state to false
|
||||
-- @treturn self table the define to allow for chaining
|
||||
function Text._prototype_box:set_selectable(state)
|
||||
if state == false then
|
||||
self.selectable = false
|
||||
@@ -94,6 +129,9 @@ function Text._prototype_box:set_selectable(state)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Sets the text box to have word wrap
|
||||
-- @tparam[opt=true] state boolean when false will set the state to false
|
||||
-- @treturn self table the define to allow for chaining
|
||||
function Text._prototype_box:set_word_wrap(state)
|
||||
if state == false then
|
||||
self.word_wrap = false
|
||||
@@ -103,6 +141,9 @@ function Text._prototype_box:set_word_wrap(state)
|
||||
return self
|
||||
end
|
||||
|
||||
--- Sets the text box to be read only
|
||||
-- @tparam[opt=true] state boolean when false will set the state to false
|
||||
-- @treturn self table the define to allow for chaining
|
||||
function Text._prototype_box:set_read_only(state)
|
||||
if state == false then
|
||||
self.read_only = false
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
--- Gui structure for the toolbar (top left)
|
||||
--[[
|
||||
>>>> Functions
|
||||
Toolbar.new_button(name) --- Adds a new button to the toolbar
|
||||
Toolbar.add_button(button) --- Adds an existing buttton to the toolbar
|
||||
Toolbar.update(player) --- Updates the player's toolbar with an new buttons or expected change in auth return
|
||||
]]
|
||||
local Buttons = require './buttons'
|
||||
local Gui = require './core'
|
||||
local Roles = require 'expcore.roles'
|
||||
@@ -8,6 +15,9 @@ local Toolbar = {
|
||||
buttons = {}
|
||||
}
|
||||
|
||||
--- Adds a new button to the toolbar
|
||||
-- @tparam[opt] name string the name of the button to be added
|
||||
-- @treturn table the button define
|
||||
function Toolbar.new_button(name)
|
||||
name = name or #Toolbar.buttons+1
|
||||
local button = Buttons.new_button('toolbar/'..name)
|
||||
@@ -16,11 +26,13 @@ function Toolbar.new_button(name)
|
||||
return button
|
||||
end
|
||||
|
||||
--- Adds an existing buttton to the toolbar
|
||||
-- @tparam button table the button define for the button to be added
|
||||
function Toolbar.add_button(button)
|
||||
table.insert(Toolbar.buttons,button)
|
||||
Gui.allow_player_to_toggle_top_element_visibility(button.name)
|
||||
Gui.on_player_show_top(button.name,function(event)
|
||||
if not button.post_authenticator(event.player,button.clean_name or button.name) then
|
||||
if not button.post_authenticator(event.player,button.name) then
|
||||
event.element.visible = false
|
||||
end
|
||||
end)
|
||||
@@ -29,6 +41,8 @@ function Toolbar.add_button(button)
|
||||
end
|
||||
end
|
||||
|
||||
--- Updates the player's toolbar with an new buttons or expected change in auth return
|
||||
-- @tparam player LuaPlayer the player to update the toolbar for
|
||||
function Toolbar.update(player)
|
||||
local top = Gui.get_top_element_flow(player)
|
||||
if not top then return end
|
||||
@@ -37,7 +51,7 @@ function Toolbar.update(player)
|
||||
local element
|
||||
if top[button.name] then element = top[button.name]
|
||||
else element = button:draw_to(top) end
|
||||
if button.post_authenticator(player,button.clean_name or button.name) then
|
||||
if button.post_authenticator(player,button.name) then
|
||||
element.visible = visible
|
||||
element.enabled = true
|
||||
else
|
||||
@@ -47,16 +61,19 @@ function Toolbar.update(player)
|
||||
end
|
||||
end
|
||||
|
||||
--- When there is a new player they will have the toolbar update
|
||||
Event.add(defines.events.on_player_created,function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
Toolbar.update(player)
|
||||
end)
|
||||
|
||||
--- When a player gets a new role they will have the toolbar updated
|
||||
Event.add(Roles.player_role_assigned,function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
Toolbar.update(player)
|
||||
end)
|
||||
|
||||
--- When a player loses a role they will have the toolbar updated
|
||||
Event.add(Roles.player_role_unassigned,function(event)
|
||||
local player = Game.get_player_by_index(event.player_index)
|
||||
Toolbar.update(player)
|
||||
|
||||
113
expcore/gui.lua
113
expcore/gui.lua
@@ -1,15 +1,66 @@
|
||||
-- This file is used to require all the different elements of the gui module
|
||||
--- This file is used to require all the different elements of the gui module
|
||||
-- each module has an outline here but for more details see their seperate files in ./gui
|
||||
|
||||
local Gui = require('./gui/core')
|
||||
--[[
|
||||
Gui._prototype_factory(tbl) --- Used internally to create new prototypes for element defines
|
||||
Gui._event_factory(name) --- Used internally to create event handler adders for element defines
|
||||
Gui._store_factory(callback) --- Used internally to create store adders for element defines
|
||||
Gui._sync_store_factory(callback) --- Used internally to create synced store adders for element defines
|
||||
Gui._define_factory(prototype) --- Used internally to create new element defines from a class prototype
|
||||
|
||||
Gui._prototype:uid() --- Gets the uid for the element define
|
||||
Gui._prototype:debug_name(name) --- Sets a debug alias for the define
|
||||
Gui._prototype:set_caption(caption) --- Sets the caption for the element define
|
||||
Gui._prototype:set_tooltip(tooltip) --- Sets the tooltip for the element define
|
||||
Gui._prototype:on_element_update(callback) --- Add a hander to run on the general value update event, different classes will handle this event differently
|
||||
|
||||
Gui._prototype:set_pre_authenticator(callback) --- Sets an authenticator that blocks the draw function if check fails
|
||||
Gui._prototype:set_post_authenticator(callback) --- Sets an authenticator that disables the element if check fails
|
||||
Gui._prototype:draw_to(element) --- Draws the element using what is in the draw_data table, allows use of authenticator if present, registers new instances if store present
|
||||
Gui.draw(name,element) --- Draws a copy of the element define to the parent element, see draw_to
|
||||
|
||||
Gui._prototype:add_store(categorize) --- Adds a store location for the define that will save the state of the element, categorize is a function that returns a string
|
||||
Gui._prototype:add_sync_store(location,categorize) --- Adds a store location for the define that will sync between games, categorize is a function that returns a string
|
||||
Gui._prototype:on_store_update(callback) --- Adds a event callback for when the store changes are other events are not gauenteted to be raised
|
||||
Gui.player_store(element) --- A categorize function to be used with add_store, each player has their own value
|
||||
Gui.force_store(element) --- A categorize function to be used with add_store, each force has its own value
|
||||
Gui.surface_store(element) --- A categorize function to be used with add_store, each surface has its own value
|
||||
|
||||
Gui._prototype:get_store(category) --- Gets the value in this elements store, category needed if categorize function used
|
||||
Gui._prototype:set_store(category,value) --- Sets the value in this elements store, category needed if categorize function used
|
||||
Gui.get_store(name,category) --- Gets the value that is stored for a given element define, category needed if categorize function used
|
||||
Gui.set_store(name,category,value) --- Sets the value stored for a given element define, category needed if categorize function used
|
||||
|
||||
Gui.toggle_enable(element) --- Will toggle the enabled state of an element
|
||||
Gui.toggle_visible(element) --- Will toggle the visiblity of an element
|
||||
]]
|
||||
|
||||
local Button = require('./gui/buttons')
|
||||
Gui.new_button = Button.new_button
|
||||
Gui.classes.button = Button
|
||||
--[[
|
||||
Button.new_button(name) --- Creates a new button element define
|
||||
|
||||
Button._prototype:on_click(player,element) --- Registers a handler for when the button is clicked
|
||||
Button._prototype:on_left_click(player,element) --- Registers a handler for when the button is clicked with the left mouse button
|
||||
Button._prototype:on_right_click(player,element) --- Registers a handler for when the button is clicked with the right mouse button
|
||||
|
||||
Button._prototype:set_sprites(sprite,hovered_sprite,clicked_sprite) --- Adds sprites to a button making it a spirte button
|
||||
Button._prototype:set_click_filter(filter,...) --- Adds a click / mouse button filter to the button
|
||||
Button._prototype:set_key_filter(filter,...) --- Adds a control key filter to the button
|
||||
]]
|
||||
|
||||
local Toolbar = require('./gui/toolbar')
|
||||
Gui.new_toolbar_button = Toolbar.new_button
|
||||
Gui.add_button_to_toolbar = Toolbar.add_button
|
||||
Gui.update_toolbar = Toolbar.update
|
||||
Gui.classes.toolbar = Toolbar
|
||||
--[[
|
||||
Toolbar.new_button(name) --- Adds a new button to the toolbar
|
||||
Toolbar.add_button(button) --- Adds an existing buttton to the toolbar
|
||||
Toolbar.update(player) --- Updates the player's toolbar with an new buttons or expected change in auth return
|
||||
]]
|
||||
|
||||
local Checkbox = require('./gui/checkboxs')
|
||||
Gui.new_checkbox = Checkbox.new_checkbox
|
||||
@@ -17,23 +68,83 @@ Gui.new_radiobutton = Checkbox.new_radiobutton
|
||||
Gui.new_radiobutton_option_set = Checkbox.new_option_set
|
||||
Gui.draw_option_set = Checkbox.draw_option_set
|
||||
Gui.classes.checkbox = Checkbox
|
||||
--[[
|
||||
Checkbox.new_checkbox(name) --- Creates a new checkbox element define
|
||||
Checkbox._prototype_checkbox:on_element_update(callback) --- Registers a handler for when an element instance updates
|
||||
Checkbox._prototype_checkbox:on_store_update(callback) --- Registers a handler for when the stored value updates
|
||||
|
||||
Checkbox.new_radiobutton(name) --- Creates a new radiobutton element define
|
||||
Checkbox._prototype_radiobutton:on_element_update(callback) --- Registers a handler for when an element instance updates
|
||||
Checkbox._prototype_radiobutton:on_store_update(callback) --- Registers a handler for when the stored value updates
|
||||
Checkbox._prototype_radiobutton:add_as_option(option_set,option_name) --- Adds this radiobutton to be an option in the given option set (only one can be true at a time)
|
||||
|
||||
Checkbox.new_option_set(name,callback,categorize) --- Registers a new option set that can be linked to radiobutotns (only one can be true at a time)
|
||||
Checkbox.draw_option_set(name,element) --- Draws all radiobuttons that are part of an option set at once (Gui.draw will not work)
|
||||
|
||||
Checkbox.reset_radiobutton(element,exclude,recursive) --- Sets all radiobutotn in a element to false (unless excluded) and can act recursivly
|
||||
]]
|
||||
|
||||
local Dropdown = require('./gui/dropdown')
|
||||
Gui.new_dropdown = Dropdown.new_dropdown
|
||||
Gui.new_list_box = Dropdown.new_list_box
|
||||
Gui.classes.dropdown = Dropdown
|
||||
--[[
|
||||
Dropdown.new_dropdown(name) --- Creates a new dropdown element define
|
||||
Dropdown.new_list_box(name) --- Creates a new list box element define
|
||||
|
||||
Dropdown._prototype:on_element_update(callback) --- Registers a handler for when an element instance updates
|
||||
Dropdown._prototype:on_store_update(callback) --- Registers a handler for when the stored value updates
|
||||
|
||||
Dropdown._prototype:new_static_options(options,...) --- Adds new static options to the dropdown which will trigger the general callback
|
||||
Dropdown._prototype:new_dynamic_options(callback) --- Adds a callback which should return a table of values to be added as options for the dropdown (appended after static options)
|
||||
Dropdown._prototype:add_option_callback(option,callback) --- Adds a case specific callback which will only run when that option is selected (general case still triggered)
|
||||
|
||||
Dropdown.select_value(element,value) --- Selects the option from a dropdown or list box given the value rather than key
|
||||
Dropdown.get_selected_value(element) --- Returns the currently selected value rather than index
|
||||
]]
|
||||
|
||||
local Slider = require('./gui/slider')
|
||||
Gui.new_slider = Slider.new_slider
|
||||
Gui.classes.slider = Slider
|
||||
--[[
|
||||
Slider.new_slider(name) --- Creates a new slider element define
|
||||
|
||||
Slider._prototype:on_element_update(callback) --- Registers a handler for when an element instance updates
|
||||
Slider._prototype:on_store_update(callback) --- Registers a handler for when the stored value updates
|
||||
|
||||
Slider._prototype:set_range(min,max) --- Sets the range of a slider, if not used will use default values for a slider
|
||||
Slider._prototype:draw_label(element) --- Draws a new label and links its value to the value of this slider, if no store then it will only show one value per player
|
||||
Slider._prototype:enable_auto_draw_label(state) --- Enables auto draw of the label, the label will share the same parent element as the slider
|
||||
]]
|
||||
|
||||
local Text = require('./gui/text')
|
||||
Gui.new_text_filed = Text.new_text_field
|
||||
Gui.new_text_box = Text.new_text_box
|
||||
Gui.classes.text = Text
|
||||
--[[
|
||||
Text.new_text_field(name) --- Creates a new text field element define
|
||||
Text._prototype_field:on_element_update(callback) --- Registers a handler for when an element instance updates
|
||||
Text._prototype_field:on_store_update(callback) --- Registers a handler for when the stored value updates
|
||||
|
||||
Text.new_text_box(name) --- Creates a new text box element define
|
||||
Text._prototype_field:on_element_update(callback) --- Registers a handler for when an element instance updates
|
||||
Text._prototype_field:on_store_update(callback) --- Registers a handler for when the stored value updates
|
||||
Text._prototype_box:set_selectable(state) --- Sets the text box to be selectable
|
||||
Text._prototype_box:set_word_wrap(state) --- Sets the text box to have word wrap
|
||||
Text._prototype_box:set_read_only(state) --- Sets the text box to be read only
|
||||
]]
|
||||
|
||||
local ElemButton = require('./gui/elem-button')
|
||||
Gui.new_elem_button = ElemButton.new_elem_button
|
||||
Gui.classes.elem_button = ElemButton
|
||||
--[[
|
||||
ElemButton.new_elem_button(name) --- Creates a new elem button element define
|
||||
|
||||
ElemButton._prototype:on_element_update(callback) --- Registers a handler for when an element instance updates
|
||||
ElemButton._prototype:on_store_update(callback) --- Registers a handler for when the stored value updates
|
||||
|
||||
ElemButton._prototype:set_type(type) --- Sets the type of the elem button, the type is required so this must be called at least once
|
||||
ElemButton._prototype:set_default(value) --- Sets the default value for the elem button, this may be a function or a string
|
||||
]]
|
||||
|
||||
return Gui
|
||||
Reference in New Issue
Block a user