diff --git a/docs_md/changelog.md b/docs_md/changelog.md index 97ab4a1..536d82b 100644 --- a/docs_md/changelog.md +++ b/docs_md/changelog.md @@ -107,6 +107,8 @@ Druid 0.5.0: - This grid can not have spaces between elements. You will get the error, if spawn element far away from other elements - The grid can spawn elements only in row or in collumn - **#37** Add _on_layout_change_ support. Druid will keep and restore GUI component data between changing game layout. Override function _on_layout_change_ in your custom components to do stuff you need. +- **#85** Move several components from `base` folder to `extended`. In future, to use them, you have to register them manually. This is need for decrease build size by excluding unused components +- Add _scroll:set_vertical_scroll_ and _scroll:set_horizontal_scroll_ for disable scroll sides - **Fix #61:** Button component: fix button animation node creation - **Fix #64:** Hover component: wrong mouse_hover default state - **Fix #71:** Blocker: blocker now correct block mouse hover event @@ -115,8 +117,7 @@ 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 -- _druid:create_ deprecated. Use _druid:new_ instead (for custom components) -- Add _scroll:set_vertical_scroll_ and _scroll:set_horizontal_scroll_ for disable scroll sides -- **#85** Move several components from `base` folder to `extended`. In future, to use them, you have to register them manually. This is need for decrease build size by excluding unused components -- Add `component.template.lua` as template for Druid custom component +- **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 +>>>>>>> develop diff --git a/druid/component.lua b/druid/component.lua index ba277dd..c58d0c4 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 @@ -24,34 +24,18 @@ function Component.set_style(self, druid_style) end ---- Get current component template name --- @function component:get_template --- @treturn string Component template name -function Component.get_template(self) - return self._meta.template -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 ---- Get current component nodes --- @function component:get_component_nodes --- @treturn table Component nodes table -function Component.get_component_nodes(self) - return self._meta.nodes -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,37 +43,21 @@ 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 ---- 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) - self._meta.context = context -end - - ---- Get current component interests --- @function component:get_interests --- @treturn table List of component interests -function Component.get_interests(self) - 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,9 +69,9 @@ 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) - local template_name = self:get_template() or const.EMPTY_STRING - local nodes = self:get_component_nodes() +function Component:get_node(node_or_name) + 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 .. "/" @@ -128,47 +96,76 @@ 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 ---- 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) - return self:get_context() == component -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 +--- 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 + + +--- 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() + + if context.isInstanceOf and context:isInstanceOf(Component) then + return context + end + + return nil +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) +-- @treturn component Component itself +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_context(context) self:set_style(style) + local parent = self:get_parent_component() + if parent then + parent:__add_children(self) + end + return self end @@ -179,7 +176,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 = { @@ -189,6 +186,63 @@ function Component.initialize(self, name, interest) end +function Component:__tostring() + return self._component.name +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 9d81c07..ce00c7e 100644 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -80,7 +80,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) @@ -102,7 +102,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 @@ -115,7 +116,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 @@ -192,16 +195,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 @@ -211,7 +216,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]