More Code Cleaning

This commit is contained in:
Cooldude2606
2019-05-14 20:29:13 +01:00
parent 4b3459f20f
commit b7dd534f59
5 changed files with 150 additions and 70 deletions

View File

@@ -67,11 +67,13 @@ function Button._prototype:set_click_filter(filter,...)
filter[v] = true
end
end
for k,v in pairs(filter) do
if type(v) == 'string' then
filter[k] = defines.mouse_button_type[v]
end
end
self.mouse_button_filter = filter
return self
end
@@ -83,6 +85,7 @@ function Button._prototype:set_key_filter(filter,...)
filter[v] = true
end
end
self.key_button_filter = filter
return self
end

View File

@@ -2,27 +2,32 @@ local Gui = require './core'
local Store = require 'expcore.store'
local Game = require 'utils.game'
local function store_state(self,element,value)
element.state = value
local function event_call(self,element,value)
if self.events.on_state_change then
local player = Game.get_player_by_index(element.player_index)
self.events.on_state_change(player,element,value)
end
end
local function store_call(self,element,value)
element.state = value
event_call(self,element,value)
end
local Checkbox = {
option_sets={},
option_categorize={},
_prototype_checkbox=Gui._extend_prototype{
on_state_change = Gui._new_event_adder('on_state_change'),
add_store = Gui._new_store_adder(store_state)
add_store = Gui._new_store_adder(store_call),
add_sync_store = Gui._new_sync_store_adder(store_call)
},
_prototype_radiobutton=Gui._extend_prototype{
on_state_change = Gui._new_event_adder('on_state_change'),
add_store = Gui._new_store_adder(store_state)
add_store = Gui._new_store_adder(store_call),
add_sync_store = Gui._new_sync_store_adder(store_call)
}
}
setmetatable(Checkbox._prototype_radiobutton,{__index=Checkbox._prototype_checkbox})
function Checkbox.new_checkbox(name)
@@ -55,8 +60,9 @@ function Checkbox.new_checkbox(name)
local category = self.categorize and self.categorize(element) or value
self:set_store(category,value)
elseif self.events.on_state_change then
self.events.on_state_change(event.player,element,element.state)
else
local value = element.state
event_call(self,element,value)
end
end)
@@ -72,14 +78,19 @@ function Checkbox.reset_radiobutton(element,exclude,recursive)
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

View File

@@ -12,27 +12,6 @@ Global.register(Gui.instances,function(tbl)
Gui.instances = tbl
end)
function Gui.get_define(name,internal)
local define = Gui.defines[name]
if not define and Gui.names[name] then
return Gui.defines[Gui.names[name]]
elseif not define then
return error('Invalid name for checkbox, name not found.',internal and 3 or 2) or nil
end
return define
end
function Gui.get_instances(self,category)
if not Gui.instances[self.name] then return end
local instances = Gui.instances[self.name]
if self.categorize then
if not instances[category] then instances[category] = {} end
return instances[category]
end
return instances
end
function Gui._extend_prototype(tbl)
for k,v in pairs(Gui._prototype) do
if not tbl[k] then tbl[k] = v end
@@ -45,6 +24,7 @@ function Gui._new_event_adder(name)
if type(callback) ~= 'function' then
return error('Event callback must be a function',2)
end
self.events[name] = callback
return self
end
@@ -77,6 +57,37 @@ function Gui._new_store_adder(callback)
end
end
function Gui._new_sync_store_adder(callback)
return function(self,location,categorize)
if self.store then return end
if Store.is_registered(location) then
return error('Location for store is already registered: '..location,2)
end
self.store = location
self.categorize = categorize
Gui.instances[self.name]={}
Store.register_synced(self.store,function(value,category)
local instances = Gui.get_instances(self,category)
if instances then
for k,element in pairs(instances) do
if element and element.valid then
callback(self,element,value)
else
instances[k] = nil
end
end
end
end)
return self
end
end
function Gui._new_define(prototype)
local uid = Gui.uid_name()
local define = setmetatable({
@@ -125,6 +136,7 @@ function Gui._prototype:set_pre_authenticator(callback)
if type(callback) ~= 'function' then
return error('Pre authenticator callback must be a function')
end
self.pre_authenticator = callback
return self
end
@@ -134,6 +146,7 @@ function Gui._prototype:set_post_authenticator(callback)
if type(callback) ~= 'function' then
return error('Authenicater callback must be a function')
end
self.post_authenticator = callback
return self
end
@@ -186,6 +199,33 @@ function Gui._prototype:set_store(category,value)
end
end
function Gui.get_define(name,internal)
local define = Gui.defines[name]
if not define and Gui.names[name] then
return Gui.defines[Gui.names[name]]
elseif not define then
return error('Invalid name for checkbox, name not found.',internal and 3 or 2) or nil
end
return define
end
function Gui.get_instances(self,category)
if not Gui.instances[self.name] then return end
local instances = Gui.instances[self.name]
if self.categorize then
if not instances[category] then instances[category] = {} end
return instances[category]
end
return instances
end
function Gui.get_store(name,category)
local define = Gui.get_define(name,true)
return define:get_store(category)
@@ -204,7 +244,6 @@ end
function Gui.toggle_enable(element)
if not element or not element.valid then return end
if not element.enabled then
-- this way round so if its nil it will become false
element.enabled = true
else
element.enabled = false
@@ -214,7 +253,6 @@ end
function Gui.toggle_visible(element)
if not element or not element.valid then return end
if not element.visible then
-- this way round so if its nil it will become false
element.visible = true
else
element.visible = false

View File

@@ -1,20 +1,29 @@
local Gui = require './core'
local Game = require 'utils.game'
local function event_call(define,element,value)
local player = Game.get_player_by_index(element.player_index)
if define.events.on_selection then
define.events.on_selection(player,element,value)
end
if define.option_callbacks and define.option_callbacks[value] then
define.option_callbacks[value](player,element,value)
end
end
local _select_value
local function store_call(self,element,value)
_select_value(element,value)
event_call(self,element,value)
end
local Dropdown = {
_prototype=Gui._extend_prototype{
on_selection = Gui._new_event_adder('on_selection'),
add_store = Gui._new_store_adder(function(self,element,value)
_select_value(element,value)
local player = Game.get_player_by_index(element.player_index)
if self.events.on_selection then
self.events.on_selection(player,element,value)
end
if self.option_callbacks and self.option_callbacks[value] then
self.option_callbacks[value](player,element,value)
end
end)
add_store = Gui._new_store_adder(store_call),
add_sync_store = Gui._new_sync_store_adder(store_call)
}
}
@@ -35,6 +44,7 @@ function Dropdown.new_dropdown(name)
element.add_item(v)
end
end
if self.store then
local category = self.categorize and self.categorize(element) or nil
local value = self:get_store(category)
@@ -49,15 +59,10 @@ function Dropdown.new_dropdown(name)
if self.store then
local category = self.categorize and self.categorize(element) or value
self:set_store(category,value)
return
end
if self.events.on_selection then
self.events.on_selection(event.player,element,value)
end
else
event_call(self,element,value)
if self.option_callbacks and self.option_callbacks[value] then
self.option_callbacks[value](event.player,element,value)
end
end)
@@ -72,6 +77,7 @@ function Dropdown._prototype:new_static_options(options,...)
table.insert(options,v)
end
end
self.options = options
self.draw_data.items = options
return self
@@ -90,10 +96,12 @@ Dropdown._prototype.add_dynamic = Dropdown._prototype.new_dynamic_options
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
self.option_callbacks[option] = callback
if not table.contains(self.options,option) then
table.insert(self.options,option)
end
return self
end

View File

@@ -1,7 +1,7 @@
local Gui = require './core'
local Game = require 'utils.game'
local function get_instances(define,element)
local function get_labels(define,element)
local function cat(e)
return e.player_index
end
@@ -11,7 +11,7 @@ local function get_instances(define,element)
local categorize = define.categorize or not define.store and cat
local category = categorize and categorize(element) or nil
local instances = Gui.get_instances({
local instances = Gui.get_labels({
name=name,
categorize=categorize
},category)
@@ -19,8 +19,8 @@ local function get_instances(define,element)
return instances
end
local function update_instances(define,element)
local instances = get_instances(define,element)
local function update_lables(define,element)
local instances = get_labels(define,element)
local value = element.slider_value
if instances then
for k,instance in pairs(instances) do
@@ -33,20 +33,30 @@ local function update_instances(define,element)
end
end
local function event_call(define,element,value)
local player = Game.get_player_by_index(element.player_index)
local min,max = element.get_slider_minimum(),element.get_slider_maximum()
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)
end
update_lables(define,element)
end
local function store_call(self,element,value)
element.slider_value = value
event_call(self,element,value)
end
local Slider = {
_prototype=Gui._extend_prototype{
on_change = Gui._new_event_adder('on_change'),
add_store = Gui._new_store_adder(function(self,element,value)
element.slider_value = value
local min,max = element.get_slider_minimum(),element.get_slider_maximum()
local delta = max-min
local percent = delta == 0 and 0 or (value-min)/delta
local player = Game.get_player_by_index(element.player_index)
if self.events.on_change then
self.events.on_change(player,element,value,percent)
end
update_instances(self,element)
end)
add_store = Gui._new_store_adder(store_call),
add_sync_store = Gui._new_sync_store_adder(store_call)
}
}
@@ -62,18 +72,23 @@ function Slider.new_slider(name)
self.post_draw = function(element)
local player = Game.get_player_by_index(element.player_index)
local min,max = element.get_slider_minimum(),element.get_slider_maximum()
if type(self.min) == 'function' then
min = self.min(player,element)
end
if type(self.max) == 'function' then
max = self.max(player,element)
end
element.set_slider_minimum_maximum(min,max)
if self.store then
local category = self.categorize and self.categorize(element) or nil
local value = self:get_store(category)
if value then element.slider_value = value end
end
if self.auto_label then
self:draw_label(element.parent)
end
@@ -82,17 +97,14 @@ function Slider.new_slider(name)
Gui.on_value_changed(self.name,function(event)
local element = event.element
local value = element.slider_value
local min,max = element.get_slider_minimum(),element.get_slider_maximum()
local delta = max-min
local percent = delta == 0 and 0 or (value-min)/delta
local category = self.categorize and self.categorize(element) or value
if self.store then
local category = self.categorize and self.categorize(element) or value
self:set_store(category,value)
elseif self.events.on_change then
self.events.on_change(event.player,element,value,percent)
update_instances(self,element)
else
event_call(self,element,value)
end
end)
@@ -103,31 +115,39 @@ end
function Slider._prototype:set_range(min,max)
self.min = min
self.max = max
if type(min) == 'number' then
self.draw_data.minimum_value = min
end
if type(max) == 'number' then
self.draw_data.maximum_value = max
end
return self
end
function Slider._prototype:draw_label(element)
local name = self.name..'-label'
if element[name] then return end
local value = 0
if self.store then
local category = self.categorize and self.categorize(element) or value
value = self:get_store(category) or 0
end
local new_element = element.add{
name=name,
type='label',
caption=tostring(math.round(value,2))
}
if not Gui.instances[name] then Gui.instances[name] = {} end
local instances = get_instances(self,element)
local instances = get_labels(self,element)
table.insert(instances,new_element)
return new_element
end