From ba12a9868a58368ccf7e112bf33227d021a2d972 Mon Sep 17 00:00:00 2001 From: Insality Date: Wed, 23 Sep 2020 01:21:44 +0300 Subject: [PATCH 1/6] Add __tostring to component class --- druid/component.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/druid/component.lua b/druid/component.lua index 3d8fb60..5c263c9 100644 --- a/druid/component.lua +++ b/druid/component.lua @@ -189,6 +189,11 @@ function Component.initialize(self, name, interest) end +function Component:__tostring() + return self._component.name +end + + --- Create new component. It will inheritance from basic -- druid component. -- @function Component.create From 54a1eda738f473b90a476505f7760bb8b6c10794 Mon Sep 17 00:00:00 2001 From: Insality Date: Wed, 23 Sep 2020 01:22:27 +0300 Subject: [PATCH 2/6] Refactor Component to class notation --- druid/component.lua | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/druid/component.lua b/druid/component.lua index 5c263c9..ab59b63 100644 --- a/druid/component.lua +++ b/druid/component.lua @@ -14,7 +14,7 @@ local Component = class("druid.component") -- their style changing and store all style params -- @function component:set_style -- @tparam table style Druid style module -function Component.set_style(self, druid_style) +function Component:set_style(druid_style) self._meta.style = druid_style or const.EMPTY_TABLE local component_style = self._meta.style[self._component.name] or const.EMPTY_TABLE @@ -27,7 +27,7 @@ end --- Get current component template name -- @function component:get_template -- @treturn string Component template name -function Component.get_template(self) +function Component:get_template() return self._meta.template end @@ -35,7 +35,7 @@ end --- Set current component template name -- @function component:set_template -- @tparam string template Component template name -function Component.set_template(self, template) +function Component:set_template(template) self._meta.template = template end @@ -43,7 +43,7 @@ end --- Get current component nodes -- @function component:get_nodes -- @treturn table Component nodes table -function Component.get_nodes(self) +function Component:get_nodes() return self._meta.nodes end @@ -51,7 +51,7 @@ end --- Set current component nodes -- @function component:set_nodes -- @tparam table nodes Component nodes table -function Component.set_nodes(self, nodes) +function Component:set_nodes(nodes) self._meta.nodes = nodes end @@ -59,7 +59,7 @@ end --- Get current component context -- @function component:get_context -- @treturn table Component context -function Component.get_context(self, context) +function Component:get_context(context) return self._meta.context end @@ -67,7 +67,7 @@ end --- Set current component context -- @function component:set_context -- @tparam table context Druid context. Usually it is self of script -function Component.set_context(self, context) +function Component:set_context(context) self._meta.context = context end @@ -75,21 +75,21 @@ end --- Get current component interests -- @function component:get_interests -- @treturn table List of component interests -function Component.get_interests(self) +function Component:get_interests() return self._component.interest end --- Increase input priority in current input stack -- @function component:increase_input_priority -function Component.increase_input_priority(self) +function Component:increase_input_priority() self._meta.increased_input_priority = true end --- Reset input priority in current input stack -- @function component:reset_input_priority -function Component.reset_input_priority(self) +function Component:reset_input_priority() self._meta.increased_input_priority = false end @@ -101,7 +101,7 @@ end -- @function component:get_node -- @tparam string|node node_or_name Node name or node itself -- @treturn node Gui node -function Component.get_node(self, node_or_name) +function Component:get_node(node_or_name) local template_name = self:get_template() or const.EMPTY_STRING local nodes = self:get_nodes() @@ -128,7 +128,7 @@ end -- Use it to create component inside of other components. -- @function component:get_druid -- @treturn Druid Druid instance with component context -function Component.get_druid(self) +function Component:get_druid() local context = { _context = self } return setmetatable(context, { __index = self._meta.druid }) end @@ -137,7 +137,7 @@ end --- Return true, if current component is child of another component -- @function component:is_child_of -- @treturn bool True, if current component is child of another -function Component.is_child_of(self, component) +function Component:is_child_of(component) return self:get_context() == component end @@ -145,7 +145,7 @@ end --- Return component name -- @function component:get_name -- @treturn string The component name -function Component.get_name(self) +function Component:get_name() return self._component.name end @@ -179,7 +179,7 @@ end -- @tparam string name Component name -- @tparam[opt={}] table interest List of component's interest -- @local -function Component.initialize(self, name, interest) +function Component:initialize(name, interest) interest = interest or {} self._component = { From 71b393b2c22b2f55d73e0c41e1f3054346c1bb49 Mon Sep 17 00:00:00 2001 From: Insality Date: Wed, 23 Sep 2020 01:28:54 +0300 Subject: [PATCH 3/6] Add children list to the component --- druid/component.lua | 36 +++++++++++++++++++++++++++++++-- druid/system/druid_instance.lua | 14 +++++++------ 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/druid/component.lua b/druid/component.lua index ab59b63..1dc359c 100644 --- a/druid/component.lua +++ b/druid/component.lua @@ -150,25 +150,57 @@ function Component:get_name() end +function Component:get_parent_component() + local context = self:get_context() + + if context.isInstanceOf and context:isInstanceOf(Component) then + return context + end + + return nil +end + + +function Component:add_children(children) + table.insert(self._meta.children, children) +end + + +function Component:remove_children(children) + for i = #self._meta.children, 1, -1 do + if self._meta.children[i] == children then + table.remove(self._meta.children, i) + end + end +end + + --- Setup component context and his style table -- @function component:setup_component -- @tparam druid_instance table The parent druid instance -- @tparam context table Druid context. Usually it is self of script -- @tparam style table Druid style module -- @treturn Component Component itself -function Component.setup_component(self, druid_instance, context, style) +function Component:setup_component(druid_instance, context, style) self._meta = { template = nil, context = nil, nodes = nil, style = nil, druid = druid_instance, - increased_input_priority = false + increased_input_priority = false, + input_enabled = true, + children = {} } self:set_context(context) self:set_style(style) + local parent = self:get_parent_component() + if parent then + parent:add_children(self) + end + return self end diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index 259f49b..102cff9 100644 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -190,16 +190,18 @@ function Druid.remove(self, component) return end - local all_components = self.components[const.ALL] - -- Recursive remove all children of component - for i = #all_components, 1, -1 do - local inst = all_components[i] - if inst:is_child_of(component) then - self:remove(inst) + local children = component._meta.children + for i = 1, #children do + self:remove(children[i]) + local parent = children[i]:get_parent_component() + if parent then + parent:remove_children(children[i]) end end + component._meta.children = {} + local all_components = self.components[const.ALL] for i = #all_components, 1, -1 do if all_components[i] == component then if component.on_remove then From 3c32a5f75eb404c73f50032d23d6450d76bdfb0b Mon Sep 17 00:00:00 2001 From: Insality Date: Wed, 23 Sep 2020 01:29:16 +0300 Subject: [PATCH 4/6] Add set_input_state for component to enable/disable input --- druid/component.lua | 16 ++++++++++++++++ druid/system/druid_instance.lua | 7 +++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/druid/component.lua b/druid/component.lua index 1dc359c..99fd222 100644 --- a/druid/component.lua +++ b/druid/component.lua @@ -150,6 +150,22 @@ function Component:get_name() end +--- Set component input state. By default it enabled +-- You can disable any input of component by this function +-- @function component:set_input_enabled +-- @tparam bool state The component input state +-- @treturn Component Component itself +function Component:set_input_enabled(state) + self._meta.input_enabled = state + + for index = 1, #self._meta.children do + self._meta.children[index]:set_input_enabled(state) + end + + return self +end + + function Component:get_parent_component() local context = self:get_context() diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index 102cff9..d3b8452 100644 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -100,7 +100,8 @@ local function process_input(action_id, action, components, is_input_consumed) for i = #components, 1, -1 do local component = components[i] -- Process increased input priority first - if component._meta.increased_input_priority then + local meta = component._meta + if meta.input_enabled and meta.increased_input_priority then if not is_input_consumed then is_input_consumed = component:on_input(action_id, action) else @@ -113,7 +114,9 @@ local function process_input(action_id, action, components, is_input_consumed) for i = #components, 1, -1 do local component = components[i] - if not component._meta.increased_input_priority then + -- Process usual input priority next + local meta = component._meta + if meta.input_enabled and not meta.increased_input_priority then if not is_input_consumed then is_input_consumed = component:on_input(action_id, action) else From c8bf57ecd3a751d9d0ae9b147b411538f044135a Mon Sep 17 00:00:00 2001 From: Insality Date: Wed, 23 Sep 2020 01:51:10 +0300 Subject: [PATCH 5/6] Move several component functions to private scope --- druid/component.lua | 119 ++++++++++++++++---------------- druid/system/druid_instance.lua | 6 +- 2 files changed, 63 insertions(+), 62 deletions(-) diff --git a/druid/component.lua b/druid/component.lua index 99fd222..c58d0c4 100644 --- a/druid/component.lua +++ b/druid/component.lua @@ -24,14 +24,6 @@ function Component:set_style(druid_style) end ---- Get current component template name --- @function component:get_template --- @treturn string Component template name -function Component:get_template() - return self._meta.template -end - - --- Set current component template name -- @function component:set_template -- @tparam string template Component template name @@ -40,14 +32,6 @@ function Component:set_template(template) end ---- Get current component nodes --- @function component:get_nodes --- @treturn table Component nodes table -function Component:get_nodes() - return self._meta.nodes -end - - --- Set current component nodes -- @function component:set_nodes -- @tparam table nodes Component nodes table @@ -64,22 +48,6 @@ function Component:get_context(context) end ---- Set current component context --- @function component:set_context --- @tparam table context Druid context. Usually it is self of script -function Component:set_context(context) - self._meta.context = context -end - - ---- Get current component interests --- @function component:get_interests --- @treturn table List of component interests -function Component:get_interests() - return self._component.interest -end - - --- Increase input priority in current input stack -- @function component:increase_input_priority function Component:increase_input_priority() @@ -102,8 +70,8 @@ end -- @tparam string|node node_or_name Node name or node itself -- @treturn node Gui node function Component:get_node(node_or_name) - local template_name = self:get_template() or const.EMPTY_STRING - local nodes = self:get_nodes() + local template_name = self:__get_template() or const.EMPTY_STRING + local nodes = self:__get_nodes() if template_name ~= const.EMPTY_STRING then template_name = template_name .. "/" @@ -134,14 +102,6 @@ function Component:get_druid() end ---- Return true, if current component is child of another component --- @function component:is_child_of --- @treturn bool True, if current component is child of another -function Component:is_child_of(component) - return self:get_context() == component -end - - --- Return component name -- @function component:get_name -- @treturn string The component name @@ -166,6 +126,9 @@ function Component:set_input_enabled(state) end +--- Return the parent for current component +-- @function component:get_parent_component +-- @treturn Component|nil The druid component instance or nil function Component:get_parent_component() local context = self:get_context() @@ -177,26 +140,12 @@ function Component:get_parent_component() end -function Component:add_children(children) - table.insert(self._meta.children, children) -end - - -function Component:remove_children(children) - for i = #self._meta.children, 1, -1 do - if self._meta.children[i] == children then - table.remove(self._meta.children, i) - end - end -end - - --- Setup component context and his style table -- @function component:setup_component -- @tparam druid_instance table The parent druid instance -- @tparam context table Druid context. Usually it is self of script -- @tparam style table Druid style module --- @treturn Component Component itself +-- @treturn component Component itself function Component:setup_component(druid_instance, context, style) self._meta = { template = nil, @@ -209,12 +158,12 @@ function Component:setup_component(druid_instance, context, style) children = {} } - self:set_context(context) + self:__set_context(context) self:set_style(style) local parent = self:get_parent_component() if parent then - parent:add_children(self) + parent:__add_children(self) end return self @@ -242,6 +191,58 @@ function Component:__tostring() end +--- Set current component context +-- @function component:__set_context +-- @tparam table context Druid context. Usually it is self of script +function Component:__set_context(context) + self._meta.context = context +end + + +--- Get current component interests +-- @function component:__get_interests +-- @treturn table List of component interests +function Component:__get_interests() + return self._component.interest +end + + +--- Get current component template name +-- @function component:__get_template +-- @treturn string Component template name +function Component:__get_template() + return self._meta.template +end + + +--- Get current component nodes +-- @function component:__get_nodes +-- @treturn table Component nodes table +function Component:__get_nodes() + return self._meta.nodes +end + + +--- Add child to component children list +-- @function component:__add_children +-- @tparam component children The druid component instance +function Component:__add_children(children) + table.insert(self._meta.children, children) +end + + +--- Remove child from component children list +-- @function component:__remove_children +-- @tparam component children The druid component instance +function Component:__remove_children(children) + for i = #self._meta.children, 1, -1 do + if self._meta.children[i] == children then + table.remove(self._meta.children, i) + end + end +end + + --- Create new component. It will inheritance from basic -- druid component. -- @function Component.create diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index d3b8452..01e7a7d 100644 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -78,7 +78,7 @@ local function create(self, instance_class) table.insert(self.components[const.ALL], instance) - local register_to = instance:get_interests() + local register_to = instance:__get_interests() for i = 1, #register_to do local interest = register_to[i] table.insert(self.components[interest], instance) @@ -199,7 +199,7 @@ function Druid.remove(self, component) self:remove(children[i]) local parent = children[i]:get_parent_component() if parent then - parent:remove_children(children[i]) + parent:__remove_children(children[i]) end end component._meta.children = {} @@ -214,7 +214,7 @@ function Druid.remove(self, component) end end - local interests = component:get_interests() + local interests = component:__get_interests() for i = 1, #interests do local interest = interests[i] local components = self.components[interest] From b7eb7a678228eb0bd1a98d3fdfbe23cd327bcbf4 Mon Sep 17 00:00:00 2001 From: Insality Date: Wed, 23 Sep 2020 01:52:58 +0300 Subject: [PATCH 6/6] Update changelog --- docs_md/changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs_md/changelog.md b/docs_md/changelog.md index 7f397dc..58ccbc1 100644 --- a/docs_md/changelog.md +++ b/docs_md/changelog.md @@ -102,5 +102,6 @@ Druid 0.5.0: - **Fix #76:** Add params for lang text localization component - **Fix #79:** Fix druid:remove inside on_input callback - **Fix #80:** Fix hover set_enable typo function call +- **FIX #88:** Add _component:set_input_enabled_ function to enable/disable input for druid component - Add `component.tempalte.lua` as template for Druid custom component