Made code cleaner

This commit is contained in:
Cooldude2606
2019-05-11 22:01:28 +01:00
parent 35a3d11f58
commit 6769642e48
7 changed files with 157 additions and 136 deletions

View File

@@ -5,7 +5,11 @@ local Gui = require './core'
local Button = { local Button = {
config={}, config={},
clean_names={}, clean_names={},
_prototype=Gui._extend_prototype{} _prototype=Gui._extend_prototype{
on_click = Gui._new_event_adder('on_click'),
on_left_click = Gui._new_event_adder('on_left_click'),
on_right_click = Gui._new_event_adder('on_right_click'),
}
} }
local function get_config(name) local function get_config(name)
@@ -24,7 +28,8 @@ function Button.new_button(name)
local self = setmetatable({ local self = setmetatable({
name=uid, name=uid,
clean_name=name, clean_name=name,
_draw={ events={},
draw_data={
name=uid, name=uid,
style=mod_gui.button_style, style=mod_gui.button_style,
type='button' type='button'
@@ -41,45 +46,45 @@ function Button.new_button(name)
end end
Gui.on_click(self.name,function(event) Gui.on_click(self.name,function(event)
local mosue_button = event.button local mouse_button = event.button
local keys = {alt=event.alt,control=event.control,shift=event.shift} local keys = {alt=event.alt,control=event.control,shift=event.shift}
event.keys = keys event.keys = keys
if self.authenticator then if self.post_authenticator then
if not self.authenticator(event.player,self.clean_name or self.name) then return end if not self.post_authenticator(event.player,self.clean_name or self.name) then return end
end end
if mosue_button == defines.mouse_button_type.left and self._on_left_click then if mouse_button == defines.mouse_button_type.left and self.events.on_left_click then
self.on_left_click(event.player,event.element,event) self.events.on_left_click(event.player,event.element,event)
elseif mosue_button == defines.mouse_button_type.right and self._on_right_click then elseif mouse_button == defines.mouse_button_type.right and self.events.on_right_click then
self.on_right_click(event.player,event.element,event) self.events.on_right_click(event.player,event.element,event)
end end
if self.mouse_button_filter and not self.mouse_button_filter[mosue_button] then return end if self.mouse_button_filter and not self.mouse_button_filter[mouse_button] then return end
if self.key_button_filter then if self.key_button_filter then
for key,state in pairs(self.key_button_filter) do for key,state in pairs(self.key_button_filter) do
if state and not keys[key] then return end if state and not keys[key] then return end
end end
end end
if self._on_click then if self.events.on_click then
self._on_click(event.player,event.element,event) self.events.on_click(event.player,event.element,event)
end end
end) end)
return Button.config[uid] return self
end end
function Button.draw_button(name,element) function Button.draw_button(name,element)
local button = get_config(name) local config = get_config(name)
return button:draw_to(element) return config:draw_to(element)
end end
function Button._prototype:set_sprites(sprite,hovered_sprite,clicked_sprite) function Button._prototype:set_sprites(sprite,hovered_sprite,clicked_sprite)
self._draw.type = 'sprite-button' self.draw_data.type = 'sprite-button'
self._draw.sprite = sprite self.draw_data.sprite = sprite
self._draw.hovered_sprite = hovered_sprite self.draw_data.hovered_sprite = hovered_sprite
self._draw.clicked_sprite = clicked_sprite self.draw_data.clicked_sprite = clicked_sprite
return self return self
end end
@@ -110,28 +115,4 @@ function Button._prototype:set_key_filter(filter,...)
return self return self
end end
function Button._prototype:on_click(callback)
if type(callback) ~= 'function' then
return error('Event callback must be a function')
end
self._on_click = callback
return self
end
function Button._prototype:on_left_click(callback)
if type(callback) ~= 'function' then
return error('Event callback must be a function')
end
self._on_left_click = callback
return self
end
function Button._prototype:on_right_click(callback)
if type(callback) ~= 'function' then
return error('Event callback must be a function')
end
self._on_right_click = callback
return self
end
return Button return Button

View File

@@ -7,10 +7,14 @@ local Checkbox = {
config={}, config={},
clean_names={}, clean_names={},
instances={}, instances={},
option_mapping={}, option_sets={},
option_categorize={}, option_categorize={},
_prototype_checkbox=Gui._extend_prototype{}, _prototype_checkbox=Gui._extend_prototype{
_prototype_radiobutton=Gui._extend_prototype{} on_state_change = Gui._new_event_adder('on_state_change')
},
_prototype_radiobutton=Gui._extend_prototype{
on_state_change = Gui._new_event_adder('on_state_change')
}
} }
setmetatable(Checkbox._prototype_radiobutton,{__index=Checkbox._prototype_checkbox}) setmetatable(Checkbox._prototype_radiobutton,{__index=Checkbox._prototype_checkbox})
Global.register(Checkbox.instances,function(tbl) Global.register(Checkbox.instances,function(tbl)
@@ -27,10 +31,6 @@ local function get_config(name)
return config return config
end end
local function get_store_location(checkbox)
return 'gui.inputs.checkbox.'..(checkbox.clean_name or checkbox.name)
end
local function get_instances(checkbox,category) local function get_instances(checkbox,category)
if not Checkbox.instances[checkbox.name] then return end if not Checkbox.instances[checkbox.name] then return end
local instances = Checkbox.instances[checkbox.name] local instances = Checkbox.instances[checkbox.name]
@@ -56,7 +56,8 @@ function Checkbox.new_checkbox(name)
local self = setmetatable({ local self = setmetatable({
name=uid, name=uid,
clean_name=name, clean_name=name,
_draw={ events={},
draw_data={
name=uid, name=uid,
type='checkbox', type='checkbox',
state=false state=false
@@ -85,12 +86,12 @@ function Checkbox.new_checkbox(name)
Gui.on_checked_state_changed(self.name,function(event) Gui.on_checked_state_changed(self.name,function(event)
local element = event.element local element = event.element
if self.share_store_location then if self.option_set then
set_store(self,self.share_store_location,element,Checkbox.option_mapping[self.share_store_location][element.name]) set_store(self,self.option_set,element,Checkbox.option_sets[self.option_set][element.name])
elseif self.store then elseif self.store then
set_store(self,self.store,element,element.state) set_store(self,self.store,element,element.state)
elseif self._on_state_change then elseif self.events.on_state_change then
self._on_state_change(event.player,element) self.events.on_state_change(event.player,element)
end end
end) end)
@@ -98,31 +99,36 @@ function Checkbox.new_checkbox(name)
end end
function Checkbox.draw_checkbox(name,element) function Checkbox.draw_checkbox(name,element)
local checkbox = get_config(name) local config = get_config(name)
return checkbox:draw_to(element) return config:draw_to(element)
end end
function Checkbox._prototype_checkbox:add_store(categorize) function Checkbox._prototype_checkbox:add_store(categorize)
if self.store then return end if self.store then return end
self.store = get_store_location(self)
self.store = Store.uid_location()
self.categorize = categorize self.categorize = categorize
Checkbox.instances[self.name]={} Checkbox.instances[self.name]={}
Store.register(self.store,function(value,category) Store.register(self.store,function(value,category)
local instances = get_instances(self,category) local instances = get_instances(self,category)
if instances then if instances then
for k,element in pairs(instances) do for k,element in pairs(instances) do
if element and element.valid then if element and element.valid then
element.state = value element.state = value
if self._on_state_change then if self.events.on_state_change then
local player = Game.get_player_by_index(element.player_index) local player = Game.get_player_by_index(element.player_index)
self._on_state_change(player,element) self.events.on_state_change(player,element)
end end
else else
instances[k] = nil instances[k] = nil
end end
end end
end end
end) end)
return self return self
end end
@@ -137,21 +143,13 @@ end
function Checkbox._prototype_checkbox:set_store_state(category,state) function Checkbox._prototype_checkbox:set_store_state(category,state)
if not self.store then return end if not self.store then return end
state = not not state set_store(self,self.store,category,not not state)
set_store(self,self.store,category,state)
end
function Checkbox._prototype_checkbox:on_state_change(callback)
if type(callback) ~= 'function' then
return error('Event callback must be a function')
end
self._on_state_change = callback
return self
end end
function Checkbox.reset_radiobutton(element,exclude,recursive) function Checkbox.reset_radiobutton(element,exclude,recursive)
if not element or not element.valid then return end if not element or not element.valid then return end
exclude = type(exclude) == 'table' and exclude or exclude ~= nil and {[exclude]=true} or {} exclude = type(exclude) == 'table' and exclude or exclude ~= nil and {[exclude]=true} or {}
for _,child in pairs(element.children) do for _,child in pairs(element.children) do
if child and child.valid and child.type == 'radiobutton' then if child and child.valid and child.type == 'radiobutton' then
child.state = exclude[child.name] or false child.state = exclude[child.name] or false
@@ -163,13 +161,14 @@ function Checkbox.reset_radiobutton(element,exclude,recursive)
Checkbox.reset_radiobutton(child,exclude,recursive) Checkbox.reset_radiobutton(child,exclude,recursive)
end end
end end
return true return true
end end
function Checkbox.new_radiobutton(name) function Checkbox.new_radiobutton(name)
local self = Checkbox.new_checkbox(name) local self = Checkbox.new_checkbox(name)
local uid = self.name local uid = self.name
self._draw.type = 'radiobutton' self.draw_data.type = 'radiobutton'
setmetatable(self,{ setmetatable(self,{
__index=Checkbox._prototype_radiobutton, __index=Checkbox._prototype_radiobutton,
@@ -179,53 +178,56 @@ function Checkbox.new_radiobutton(name)
return self return self
end end
function Checkbox.new_share_store(location,callback,categorize)
Store.register(location,function(value,category)
local options = Checkbox.option_mapping[location]
for opt_name,name in pairs(options) do
if Checkbox.config[name] then
get_config(name):set_store_state(category,opt_name == value)
end
end
callback(value,category)
end)
Checkbox.option_categorize[location] = categorize
Checkbox.option_mapping[location] = {}
return location
end
Checkbox.draw_radiobutton = Checkbox.draw_checkbox Checkbox.draw_radiobutton = Checkbox.draw_checkbox
function Checkbox._prototype_radiobutton:share_store(location,option_name) function Checkbox._prototype_radiobutton:add_as_option(option_set,option_name)
self.share_store_location = location self.option_set = option_set
self.option_name = option_name or self.clean_name or self.name self.option_name = option_name or self.clean_name or self.name
Checkbox.option_mapping[location][self.option_name] = self.name Checkbox.option_sets[option_set][self.option_name] = self.name
Checkbox.option_mapping[location][self.name] = self.option_name Checkbox.option_sets[option_set][self.name] = self.option_name
self:add_store(Checkbox.option_categorize[location]) self:add_store(Checkbox.option_categorize[option_set])
return self return self
end end
function Checkbox.new_option_set(name,callback,categorize)
Store.register(name,function(value,category)
local options = Checkbox.option_sets[name]
for opt_name,config_name in pairs(options) do
if Checkbox.config[config_name] then
get_config(config_name):set_store_state(category,opt_name == value)
end
end
callback(value,category)
end)
Checkbox.option_categorize[name] = categorize
Checkbox.option_sets[name] = {}
return name
end
function Checkbox.get_stored_state(name,category) function Checkbox.get_stored_state(name,category)
local checkbox = get_config(name) local config = get_config(name)
if checkbox.share_store_location then
if checkbox.categorize then if config.option_set then
return Store.get_child(checkbox.share_store_location,category) if config.categorize then
return Store.get_child(config.option_set,category)
else else
return Store.get(checkbox.share_store_location) return Store.get(config.option_set)
end end
end end
return checkbox:get_store_state(category)
return config:get_store_state(category)
end end
function Checkbox.set_stored_state(name,category,value) function Checkbox.set_stored_state(name,category,value)
local checkbox = get_config(name) local config = get_config(name)
if checkbox.share_store_location then local location = config.option_set or config.store
set_store(checkbox,checkbox.share_store_location,category,value) set_store(config,location,category,value)
end
return checkbox:set_store_state(category,value)
end end
return Checkbox return Checkbox

View File

@@ -13,15 +13,30 @@ function Gui._extend_prototype(tbl)
return tbl return tbl
end end
function Gui._new_event_adder(name)
return function(self,callback)
if type(callback) ~= 'function' then
return error('Event callback must be a function',2)
end
self.events[name] = callback
return self
end
end
--- Gets the uid for the config
function Gui._prototype:uid()
return self.name
end
--- Sets the caption for the element config --- Sets the caption for the element config
function Gui._prototype:set_caption(caption) function Gui._prototype:set_caption(caption)
self._draw.caption = caption self.draw_data.caption = caption
return self return self
end end
--- Sets the tooltip for the element config --- Sets the tooltip for the element config
function Gui._prototype:set_tooltip(tooltip) function Gui._prototype:set_tooltip(tooltip)
self._draw.tooltip = tooltip self.draw_data.tooltip = tooltip
return self return self
end end
@@ -35,24 +50,24 @@ function Gui._prototype:set_pre_authenticator(callback)
end end
--- Sets an authenticator that disables the element if check fails --- Sets an authenticator that disables the element if check fails
function Gui._prototype:set_authenticator(callback) function Gui._prototype:set_post_authenticator(callback)
if type(callback) ~= 'function' then if type(callback) ~= 'function' then
return error('Authenicater callback must be a function') return error('Authenicater callback must be a function')
end end
self.authenticator = callback self.post_authenticator = callback
return self return self
end end
--- Draws the element using what is in the _draw table, allows use of authenticator if present --- Draws the element using what is in the draw_data table, allows use of authenticator if present
function Gui._prototype:draw_to(element) function Gui._prototype:draw_to(element)
if element[self.name] then return end if element[self.name] then return end
local player = Game.get_player_by_index(element.player_index) local player = Game.get_player_by_index(element.player_index)
if self.pre_authenticator then 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.clean_name or self.name) then return end
end end
local _element = element.add(self._draw) local _element = element.add(self.draw_data)
if self.authenticator then if self.post_authenticator then
_element.enabled = not not self.authenticator(player,self.clean_name or self.name) _element.enabled = not not self.post_authenticator(player,self.clean_name or self.name)
end end
if self._post_draw then self._post_draw(_element) end if self._post_draw then self._post_draw(_element) end
return _element return _element

View File

@@ -3,6 +3,7 @@ local format_chat_colour = ext_require('expcore.common','format_chat_colour')
local Colors = require 'resources.color_presets' local Colors = require 'resources.color_presets'
local Game = require 'utils.game' local Game = require 'utils.game'
local clean_stack_trace = ext_require('modules.commands.interface','clean_stack_trace') local clean_stack_trace = ext_require('modules.commands.interface','clean_stack_trace')
local Store = require 'expcore.store'
local tests = {} local tests = {}
@@ -12,7 +13,7 @@ local function categozie_by_player(element)
end end
Gui.new_toolbar_button('click-1') Gui.new_toolbar_button('click-1')
:set_authenticator(function(player,button_name) :set_post_authenticator(function(player,button_name)
return global.click_one return global.click_one
end) end)
:on_click(function(player,element,event) :on_click(function(player,element,event)
@@ -21,7 +22,7 @@ end)
Gui.new_toolbar_button('click-2') Gui.new_toolbar_button('click-2')
:set_caption('Click Two') :set_caption('Click Two')
:set_authenticator(function(player,button_name) :set_post_authenticator(function(player,button_name)
return global.click_two return global.click_two
end) end)
:on_click(function(player,element,event) :on_click(function(player,element,event)
@@ -30,7 +31,7 @@ end)
Gui.new_toolbar_button('click-3') Gui.new_toolbar_button('click-3')
:set_sprites('utility/questionmark') :set_sprites('utility/questionmark')
:set_authenticator(function(player,button_name) :set_post_authenticator(function(player,button_name)
return global.click_three return global.click_three
end) end)
:on_click(function(player,element,event) :on_click(function(player,element,event)
@@ -39,7 +40,7 @@ end)
Gui.new_toolbar_button('gui-test-open') Gui.new_toolbar_button('gui-test-open')
:set_caption('Open Test Gui') :set_caption('Open Test Gui')
:set_authenticator(function(player,button_name) :set_post_authenticator(function(player,button_name)
return global.show_test_gui return global.show_test_gui
end) end)
:on_click(function(player,_element,event) :on_click(function(player,_element,event)
@@ -69,14 +70,14 @@ tests['Button caption'] = Gui.new_button('test button caption')
player.print('Button caption') player.print('Button caption')
end) end)
tests['Button icon'] = Gui.new_button('test Bbutton icon') tests['Button icon'] = Gui.new_button('test button icon')
:set_sprites('utility/warning_icon','utility/warning','utility/warning_white') :set_sprites('utility/warning_icon','utility/warning','utility/warning_white')
:on_click(function(player,element,event) :on_click(function(player,element,event)
player.print('Button icon') player.print('Button icon')
end) end)
tests['Button auth'] = Gui.new_button('test button auth') tests['Button auth'] = Gui.new_button('test button auth')
:set_authenticator(function(player,button_name) :set_post_authenticator(function(player,button_name)
return global.test_auth_button return global.test_auth_button
end) end)
:on_click(function(player,element,event) :on_click(function(player,element,event)
@@ -126,27 +127,27 @@ tests['Radiobutton store player'] = Gui.new_radiobutton('test radiobutton store
player.print('Radiobutton store player: '..tostring(element.state)) player.print('Radiobutton store player: '..tostring(element.state))
end) end)
local test_share = Gui.new_radiobutton_option_set('gui.test.share',function(value,category) local test_option_set = Gui.new_radiobutton_option_set('gui.test.share',function(value,category)
game.print('Radiobutton share: '..category..' is now: '..tostring(value)) game.print('Radiobutton option set for: '..category..' is now: '..tostring(value))
end,categozie_by_player) end,categozie_by_player)
tests['Radiobutton share one'] = Gui.new_radiobutton('test radiobutton share one') tests['Radiobutton option one'] = Gui.new_radiobutton('test radiobutton option one')
:set_caption('Radiobutton Share One') :set_caption('Radiobutton Option One')
:share_store(test_share,'One') :add_as_option(test_option_set,'One')
:on_state_change(function(player,element) :on_state_change(function(player,element)
player.print('Radiobutton share one: '..tostring(element.state)) player.print('Radiobutton option one: '..tostring(element.state))
end) end)
tests['Radiobutton share two'] = Gui.new_radiobutton('test radiobutton share two') tests['Radiobutton option two'] = Gui.new_radiobutton('test radiobutton option two')
:set_caption('Radiobutton Share Two') :set_caption('Radiobutton Option Two')
:share_store(test_share,'Two') :add_as_option(test_option_set,'Two')
:on_state_change(function(player,element) :on_state_change(function(player,element)
player.print('Radiobutton share two: '..tostring(element.state)) player.print('Radiobutton option two: '..tostring(element.state))
end) end)
tests['Radiobutton share three'] = Gui.new_radiobutton('test radiobutton share three') tests['Radiobutton option three'] = Gui.new_radiobutton('test radiobutton option three')
:set_caption('Radiobutton Share Three') :set_caption('Radiobutton Option Three')
:share_store(test_share,'Three') :add_as_option(test_option_set,'Three')
:on_state_change(function(player,element) :on_state_change(function(player,element)
player.print('Radiobutton share three: '..tostring(element.state)) player.print('Radiobutton option three: '..tostring(element.state))
end) end)

View File

@@ -11,7 +11,7 @@ local Toolbar = {
function Toolbar.new_button(name) function Toolbar.new_button(name)
name = name or #Toolbar.buttons+1 name = name or #Toolbar.buttons+1
local button = Buttons.new_button('toolbar/'..name) local button = Buttons.new_button('toolbar/'..name)
button:set_authenticator(Roles.player_allowed) button:set_post_authenticator(Roles.player_allowed)
Toolbar.add_button(button) Toolbar.add_button(button)
return button return button
end end
@@ -20,12 +20,12 @@ function Toolbar.add_button(button)
table.insert(Toolbar.buttons,button) table.insert(Toolbar.buttons,button)
Gui.allow_player_to_toggle_top_element_visibility(button.name) Gui.allow_player_to_toggle_top_element_visibility(button.name)
Gui.on_player_show_top(button.name,function(event) Gui.on_player_show_top(button.name,function(event)
if not button.authenticator(player,button.clean_name or button.name) then if not button.post_authenticator(event.player,button.clean_name or button.name) then
event.element.visible = false event.element.visible = false
end end
end) end)
if not button.authenticator then if not button.post_authenticator then
button:set_authenticator(function() return true end) button:set_post_authenticator(function() return true end)
end end
end end
@@ -36,7 +36,7 @@ function Toolbar.update(player)
local element local element
if top[button.name] then element = top[button.name] if top[button.name] then element = top[button.name]
else element = button:draw_to(top) end else element = button:draw_to(top) end
if button.authenticator(player,button.clean_name or button.name) then if button.post_authenticator(player,button.clean_name or button.name) then
element.visible = true element.visible = true
element.enabled = true element.enabled = true
else else

View File

@@ -13,7 +13,7 @@ Gui.structure.toolbar = Toolbar
local Checkboxs = require('./gui/checkboxs') local Checkboxs = require('./gui/checkboxs')
Gui.new_checkbox = Checkboxs.new_checkbox Gui.new_checkbox = Checkboxs.new_checkbox
Gui.new_radiobutton = Checkboxs.new_radiobutton Gui.new_radiobutton = Checkboxs.new_radiobutton
Gui.new_radiobutton_option_set = Checkboxs.new_share_store Gui.new_radiobutton_option_set = Checkboxs.new_option_set
Gui.inputs.checkboxs = Checkboxs Gui.inputs.checkboxs = Checkboxs
return Gui return Gui

View File

@@ -74,11 +74,27 @@
Store.add_watch('game.speed',function() Store.add_watch('game.speed',function()
return game.speed return game.speed
end) end)
>>>> Alternative method
Some people may prefer to use a varible rather than a string for formating reasons here is an example. Also for any times when
there will be little external input Store.uid_location() can be used to generate non conflicting locations.
local store_game_speed = Store.uid_location()
Store.register(store_game_speed,function(value)
game.print('The game speed has been set to: '..value)
end)
Store.add_watch(store_game_speed,function()
return game.speed
end)
]] ]]
local Global = require 'utils.global' local Global = require 'utils.global'
local Event = require 'utils.event' local Event = require 'utils.event'
local write_json = ext_require('expcore.common','write_json','table_keys') local write_json = ext_require('expcore.common','write_json','table_keys')
local Token = require 'utils.token'
local Store = { local Store = {
data={}, data={},
@@ -97,6 +113,12 @@ function Store.is_registered(location)
return not not Store.callbacks[location] return not not Store.callbacks[location]
end end
--- Returns a unqiue name that can be used for a store
-- @treturn string a unqiue name
function Store.uid_location()
return tostring(Token.uid())
end
--- Registers a new location with an update callback which is triggered when the value updates --- Registers a new location with an update callback which is triggered when the value updates
-- @tparam location string a unique string that points to the data, string used rather than token to allow migration -- @tparam location string a unique string that points to the data, string used rather than token to allow migration
-- @tparam callback function this callback will be called when the stored value is set to a new value -- @tparam callback function this callback will be called when the stored value is set to a new value