This commit is contained in:
Insality
2025-04-19 22:25:58 +03:00
parent 4cb16de782
commit c7b11562d5
8 changed files with 51 additions and 25 deletions

View File

@@ -107,9 +107,16 @@ end
---Set current component nodes, returned from `gui.clone_tree` function.
---@param nodes table<hash, node>
---@param nodes table<hash, node>|node|string|nil The nodes table from gui.clone_tree or prefab node to use for clone or node id to clone
---@return druid.component
function M:set_nodes(nodes)
if type(nodes) == "string" then
nodes = self:get_node(nodes)
end
if type(nodes) == "userdata" then
nodes = gui.clone_tree(nodes) --[[@as table<hash, node>]]
end
self._meta.nodes = nodes
return self
end
@@ -132,7 +139,7 @@ end
---Get Druid instance for inner component creation.
---@param template string|nil
---@param nodes table<hash, node>|nil
---@param nodes table<hash, node>|node|string|nil The nodes table from gui.clone_tree or prefab node to use for clone or node id to clone
---@return druid.instance
function M:get_druid(template, nodes)
local druid_instance = setmetatable({
@@ -396,7 +403,11 @@ function M.create_widget(self, widget_class, context)
default_input_priority = const.PRIORITY_INPUT,
_is_input_priority_changed = true, -- Default true for sort once time after GUI init
}
instance._meta = {
-- I'll hide a meta fields under metatable to hide this tables from pprint output
-- cause it's leads to recursive pprint's from (druid = self)
-- Wish this to be better, since it can reduce a memory usage
instance._meta = setmetatable({}, { __index = {
druid = self,
template = "",
nodes = nil,
@@ -406,7 +417,7 @@ function M.create_widget(self, widget_class, context)
children = {},
parent = type(context) ~= "userdata" and context or nil,
instance_class = widget_class
}
}})
-- Register
if instance._meta.parent then

View File

@@ -33,8 +33,16 @@ end
---@param name string Module name
---@param module table Lua table with component
function M.register(name, module)
druid_instance["new_" .. name] = function(self, ...)
return druid_instance.new(self, module, ...)
local is_custom_component = getmetatable(module) ~= nil
if is_custom_component then
druid_instance["new_" .. name] = function(self, ...)
return druid_instance.new(self, module, ...)
end
else
-- Just for some compatability. But better to use direct druid_instance:new_widget(module, ...) function
druid_instance["new_" .. name] = function(self, template, nodes, ...)
return druid_instance.new_widget(self, module, template, nodes, ...)
end
end
end

View File

@@ -494,13 +494,6 @@ end
function M:new_widget(widget, template, nodes, ...)
local instance = create_widget(self, widget)
if type(nodes) == "string" then
nodes = gui.get_node(nodes)
end
if type(nodes) == "userdata" then
nodes = gui.clone_tree(nodes) --[[@as table<hash, node>]]
end
instance.druid = instance:get_druid(template, nodes)
if instance.init then