Fixed stores in gui

This commit is contained in:
Cooldude2606
2019-10-15 22:37:48 +01:00
parent 258ca8dbde
commit 3b5c69cfd1
103 changed files with 213 additions and 243 deletions

View File

@@ -15,10 +15,10 @@
the caption of all of them at once; this is where this module comes it.
First you must register the way that the instances are stored and under what name, using Instances.register you will give the
name of the collective group of instances followed by an optional categorise function which allows variants to be stored under one
name of the collective group of instances followed by an optional serializer function which allows variants to be stored under one
name (like one for each force or player)
-- categorise works in the same way as store categorise
-- serializer works in the same way as store serializer
-- so the function will world here but no value is stored only gui elements
Instances.register('score',Gui.categorize_by_force)
@@ -34,7 +34,7 @@
element.caption = 0
end)
Note that if you don't give a categorise function then you don't need to give a category when getting the elements.
Note that if you don't give a serializer function then you don't need to give a category when getting the elements.
>>>> Using unregistered instance groups
When using a registered group and the functions that go with them it is much simpler to use and more importantly includes error checking
@@ -72,9 +72,9 @@
end) -- gets all instances and sets the element caption to 0
>>>> Functions
Instances.has_categories(name) --- Returns if a instance group has a categorise function; must be registered
Instances.has_categories(name) --- Returns if a instance group has a serializer function; must be registered
Instances.is_registered(name) --- Returns if the given name is a registered instance group
Instances.register(name,categorise) --- Registers the name of an instance group to allow for storing element instances
Instances.register(name,serializer) --- Registers the name of an instance group to allow for storing element instances
Instances.add_element(name,element) --- Adds an element to the instance group under the correct category; must be registered
Instances.get_elements_raw(name,category) --- Gets all element instances without first removing any invalid ones; used internally and must be registered
@@ -86,46 +86,46 @@
local Global = require 'utils.global' --- @dep utils.global
local Instances = {
categorise={},
serializer={},
data={}
}
Global.register(Instances.data,function(tbl)
Instances.data = tbl
end)
--- Returns if a instance group has a categorise function; must be registered
--- Returns if a instance group has a serializer function; must be registered
-- @tparam string name the name of the instance group
-- @treturn boolean true if there is a categorise function
-- @treturn boolean true if there is a serializer function
function Instances.has_categories(name)
return type(Instances.categorise[name]) == 'function'
return type(Instances.serializer[name]) == 'function'
end
--- Returns if the given name is a registered instance group
-- @tparam string name the name of the instance group you are testing
-- @treturn boolean true if the name is registered
function Instances.is_registered(name)
return Instances.categorise[name] ~= nil
return Instances.serializer[name] ~= nil
end
--- Registers the name of an instance group to allow for storing element instances
-- @tparam string name the name of the instance group; must to unique
-- @tparam[opt] function categorise function used to turn the element into a string
-- categorise param - element LuaGuiElement - the gui element to be turned into a string
-- categorise return - string - the category that the element will be added to like the player's name or force's name
-- @tparam[opt] function serializer function used to turn the element into a string
-- serializer param - element LuaGuiElement - the gui element to be turned into a string
-- serializer return - string - the category that the element will be added to like the player's name or force's name
-- @treturn string the name that was added so it can be used as a variable
function Instances.register(name,categorise)
function Instances.register(name,serializer)
if _LIFECYCLE ~= _STAGE.control then
return error('Can only be called during the control stage', 2)
end
if Instances.categorise[name] then
if Instances.serializer[name] then
return error('Instances for '..name..' already exist.',2)
end
categorise = type(categorise) == 'function' and categorise or true
serializer = type(serializer) == 'function' and serializer or true
Instances.data[name] = {}
Instances.categorise[name] = categorise
Instances.serializer[name] = serializer
return name
end
@@ -134,12 +134,12 @@ end
-- @tparam string name the name of the instance group to add the element to
-- @tparam LuaGuiElement element the element to add the the instance group
function Instances.add_element(name,element)
if not Instances.categorise[name] then
if not Instances.serializer[name] then
return error('Invalid name for instance group: '..name,2)
end
if Instances.has_categories(name) then
local category = Instances.categorise[name](element)
local category = Instances.serializer[name](element)
if not Instances.data[name][category] then Instances.data[name][category] = {} end
table.insert(Instances.data[name][category],element)
else
@@ -149,10 +149,10 @@ end
--- Gets all element instances without first removing any invalid ones; used internally and must be registered
-- @tparam string name the name of the instance group to get the instances of
-- @tparam[opt] string category the category to get the instance from, not needed when no categorise function
-- @tparam[opt] string category the category to get the instance from, not needed when no serializer function
-- @treturn table the table of element instances of which some may be invalid
function Instances.get_elements_raw(name,category)
if not Instances.categorise[name] then
if not Instances.serializer[name] then
return error('Invalid name for instance group: '..name,2)
end
@@ -165,24 +165,24 @@ end
--- Gets all valid element instances and has the option of running a callback on those that are valid
-- @tparam string name the name of the instance group to get the instances of
-- @tparam[opt] string category the category to get the instances of, not needed when no categorise function
-- @tparam[opt] string category the category to get the instances of, not needed when no serializer function
-- @tparam[opt] function callback when given the callback will be ran on all valid elements
-- callback param - element LuaGuiElement - the current valid element
-- @treturn table the table of element instances with all invalid ones removed
function Instances.get_valid_elements(name,category,callback)
if not Instances.categorise[name] then
if not Instances.serializer[name] then
return error('Invalid name for instance group: '..name,2)
end
category = category or callback
local elements = Instances.get_elements_raw(name,category)
local categorise = Instances.has_categories(name)
local serializer = Instances.has_categories(name)
for key,element in pairs(elements) do
if not element or not element.valid then
elements[key] = nil
else
if categorise and callback then callback(element)
if serializer and callback then callback(element)
elseif category then category(element) end
end
end