Optimised Code Layout

This commit is contained in:
Cooldude2606
2019-05-13 19:41:23 +01:00
parent 6769642e48
commit c36550816c
4 changed files with 219 additions and 198 deletions

View File

@@ -1,10 +1,38 @@
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.names = {} -- Stores debug names to link to gui uids
Gui.instances = {} -- Stores runtime data of all active instances of each define
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
Gui._prototype = {}
Gui.inputs = {}
Gui.structure = {}
Gui.outputs = {}
function Gui._extend_prototype(tbl)
for k,v in pairs(Gui._prototype) do
@@ -23,11 +51,63 @@ function Gui._new_event_adder(name)
end
end
function Gui._new_store_adder(callback)
return function(self,categorize)
if self.store then return end
self.store = Store.uid_location()
self.categorize = categorize
Gui.instances[self.name]={}
Store.register(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({
name=uid,
events={},
draw_data={
name=uid
}
},{
__index=prototype,
__call=function(self,element)
return self:draw_to(element)
end
})
Gui.defines[define.name] = define
return define
end
--- Gets the uid for the config
function Gui._prototype:uid()
return self.name
end
--- Sets an alias to the uid
function Gui._prototype:debug_name(name)
self.debug_name = name
Gui.names[name] = self.name
return self
end
--- Sets the caption for the element config
function Gui._prototype:set_caption(caption)
self.draw_data.caption = caption
@@ -62,15 +142,63 @@ end
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
end
local _element = element.add(self.draw_data)
local new_element = element.add(self.draw_data)
if self.post_authenticator then
_element.enabled = not not self.post_authenticator(player,self.clean_name or self.name)
new_element.enabled = self.post_authenticator(player,self.clean_name or self.name)
end
if self._post_draw then self._post_draw(_element) end
return _element
if self.store then
local category = self.categorize and self.categorize(element) or nil
local instances = Gui.get_instances(self,category)
if instances then
table.insert(instances,new_element)
end
end
if self.post_draw then self.post_draw(new_element) end
return new_element
end
--- Gets the value in this elements store
function Gui._prototype:get_store(category)
if not self.store then return end
if self.categorize then
return Store.get_child(self.store,category)
else
return Store.get(self.store)
end
end
--- Sets the value in this elements store
function Gui._prototype:set_store(category,value)
if not self.store then return end
if self.categorize then
return Store.set_child(self.store,category,value)
else
return Store.set(self.store,category)
end
end
function Gui.get_store(name,category)
local define = Gui.get_define(name,true)
return define:get_store(category)
end
function Gui.set_store(name,category,value)
local define = Gui.get_define(name,true)
return define:get_store(category,value)
end
function Gui.draw(name,element)
local define = Gui.get_define(name,true)
return define:draw_to(element)
end
function Gui.toggle_enable(element)