From 54f80aeccd8862c02da1fc1ab3253451779cb23c Mon Sep 17 00:00:00 2001 From: Insality Date: Thu, 27 Jan 2022 01:48:05 +0200 Subject: [PATCH] #160 Remove component interests, now setup it with function declaring --- druid/base/back_handler.lua | 2 +- druid/base/blocker.lua | 2 +- druid/base/button.lua | 6 +--- druid/base/drag.lua | 2 +- druid/base/hover.lua | 2 +- druid/base/scroll.lua | 7 +---- druid/base/static_grid.lua | 2 +- druid/base/swipe.lua | 2 +- druid/base/text.lua | 2 +- druid/component.lua | 40 ++++++++++++-------------- druid/const.lua | 19 ++++++------- druid/extended/checkbox.lua | 2 +- druid/extended/component.template.lua | 16 +++++------ druid/extended/dynamic_grid.lua | 2 +- druid/extended/input.lua | 2 +- druid/extended/lang_text.lua | 2 +- druid/extended/progress.lua | 2 +- druid/extended/slider.lua | 2 +- druid/extended/timer.lua | 2 +- druid/system/druid_instance.lua | 41 ++++++++++++++------------- 20 files changed, 72 insertions(+), 85 deletions(-) diff --git a/druid/base/back_handler.lua b/druid/base/back_handler.lua index 4d09d61..e0af15c 100644 --- a/druid/base/back_handler.lua +++ b/druid/base/back_handler.lua @@ -17,7 +17,7 @@ local Event = require("druid.event") local const = require("druid.const") local component = require("druid.component") -local BackHandler = component.create("back_handler", { component.ON_INPUT }) +local BackHandler = component.create("back_handler") --- Component init function diff --git a/druid/base/blocker.lua b/druid/base/blocker.lua index ed3a3cf..d6aee61 100644 --- a/druid/base/blocker.lua +++ b/druid/base/blocker.lua @@ -13,7 +13,7 @@ local const = require("druid.const") local component = require("druid.component") -local Blocker = component.create("blocker", { component.ON_INPUT }) +local Blocker = component.create("blocker") --- Component init function diff --git a/druid/base/button.lua b/druid/base/button.lua index 0686c5b..14f647b 100755 --- a/druid/base/button.lua +++ b/druid/base/button.lua @@ -57,11 +57,7 @@ local const = require("druid.const") local helper = require("druid.helper") local component = require("druid.component") -local Button = component.create("button", { - component.ON_INPUT, - component.ON_MESSAGE_INPUT, - component.ON_LATE_INIT -}) +local Button = component.create("button") local function is_input_match(self, action_id) diff --git a/druid/base/drag.lua b/druid/base/drag.lua index 71cd176..18cf4f9 100644 --- a/druid/base/drag.lua +++ b/druid/base/drag.lua @@ -51,7 +51,7 @@ local const = require("druid.const") local helper = require("druid.helper") local component = require("druid.component") -local Drag = component.create("drag", { component.ON_INPUT, component.ON_LATE_INIT }, const.PRIORITY_INPUT_HIGH) +local Drag = component.create("drag", const.PRIORITY_INPUT_HIGH) local function start_touch(self, touch) diff --git a/druid/base/hover.lua b/druid/base/hover.lua index e30009e..e1b56f0 100644 --- a/druid/base/hover.lua +++ b/druid/base/hover.lua @@ -18,7 +18,7 @@ local const = require("druid.const") local helper = require("druid.helper") local component = require("druid.component") -local Hover = component.create("hover", { component.ON_INPUT, component.ON_LATE_INIT }) +local Hover = component.create("hover") --- Component init function diff --git a/druid/base/scroll.lua b/druid/base/scroll.lua index 1ff1d37..8134c46 100755 --- a/druid/base/scroll.lua +++ b/druid/base/scroll.lua @@ -61,12 +61,7 @@ local const = require("druid.const") local helper = require("druid.helper") local component = require("druid.component") -local Scroll = component.create("scroll", { - component.ON_INPUT, - component.ON_UPDATE, - component.ON_LAYOUT_CHANGE, - component.ON_LATE_INIT -}) +local Scroll = component.create("scroll") local function inverse_lerp(min, max, current) diff --git a/druid/base/static_grid.lua b/druid/base/static_grid.lua index 6e83082..7716718 100644 --- a/druid/base/static_grid.lua +++ b/druid/base/static_grid.lua @@ -49,7 +49,7 @@ local Event = require("druid.event") local helper = require("druid.helper") local component = require("druid.component") -local StaticGrid = component.create("static_grid", { component.ON_LAYOUT_CHANGE }) +local StaticGrid = component.create("static_grid") local function _extend_border(border, pos, size, pivot) diff --git a/druid/base/swipe.lua b/druid/base/swipe.lua index bba7980..f4fbbcd 100644 --- a/druid/base/swipe.lua +++ b/druid/base/swipe.lua @@ -23,7 +23,7 @@ local const = require("druid.const") local helper = require("druid.helper") local component = require("druid.component") -local Swipe = component.create("swipe", { component.ON_INPUT, component.ON_LATE_INIT }) +local Swipe = component.create("swipe") local function start_swipe(self, action) diff --git a/druid/base/text.lua b/druid/base/text.lua index c640f89..c53df2d 100755 --- a/druid/base/text.lua +++ b/druid/base/text.lua @@ -50,7 +50,7 @@ local const = require("druid.const") local utf8 = require("druid.system.utf8") local component = require("druid.component") -local Text = component.create("text", { component.ON_LAYOUT_CHANGE, component.ON_MESSAGE_INPUT }) +local Text = component.create("text") local function update_text_size(self) diff --git a/druid/component.lua b/druid/component.lua index 87c9488..3c32e64 100644 --- a/druid/component.lua +++ b/druid/component.lua @@ -14,7 +14,6 @@ local BaseComponent = class("druid.component") --- Component Interests -BaseComponent.ALL = const.ALL BaseComponent.ON_INPUT = const.ON_INPUT BaseComponent.ON_UPDATE = const.ON_UPDATE BaseComponent.ON_MESSAGE = const.ON_MESSAGE @@ -27,7 +26,6 @@ BaseComponent.ON_LANGUAGE_CHANGE = const.ON_LANGUAGE_CHANGE BaseComponent.ALL_INTERESTS = { - BaseComponent.ALL, BaseComponent.ON_INPUT, BaseComponent.ON_UPDATE, BaseComponent.ON_MESSAGE, @@ -40,18 +38,13 @@ BaseComponent.ALL_INTERESTS = { } --- Value is method name of component +-- Mapping from on_message method to specific method name BaseComponent.SPECIFIC_UI_MESSAGES = { - [BaseComponent.ON_FOCUS_LOST] = "on_focus_lost", - [BaseComponent.ON_FOCUS_GAINED] = "on_focus_gained", - [BaseComponent.ON_LAYOUT_CHANGE] = "on_layout_change", - [BaseComponent.ON_MESSAGE_INPUT] = "on_message_input", - [BaseComponent.ON_LANGUAGE_CHANGE] = "on_language_change", -} - - -BaseComponent.UI_INPUT = { - [BaseComponent.ON_INPUT] = true + [hash(BaseComponent.ON_FOCUS_LOST)] = "on_focus_lost", + [hash(BaseComponent.ON_FOCUS_GAINED)] = "on_focus_gained", + [hash(BaseComponent.ON_LAYOUT_CHANGE)] = "on_layout_change", + [hash(BaseComponent.ON_MESSAGE_INPUT)] = "on_message_input", + [hash(BaseComponent.ON_LANGUAGE_CHANGE)] = "on_language_change", } @@ -267,15 +260,11 @@ end -- by `BaseComponent.static.create` -- @tparam BaseComponent self -- @tparam string name BaseComponent name --- @tparam[opt={}] table interest List of component's interest -- @tparam[opt=DEFAULT] number input_priority The input priority. The bigger number processed first -- @local -function BaseComponent.initialize(self, name, interest, input_priority) - interest = interest or {} - +function BaseComponent.initialize(self, name, input_priority) self._component = { name = name, - interest = interest, input_priority = input_priority or const.PRIORITY_INPUT, default_input_priority = input_priority or const.PRIORITY_INPUT, _is_input_priority_changed = true, -- Default true for sort once time after GUI init @@ -319,7 +308,15 @@ end -- @treturn table List of component interests -- @local function BaseComponent.__get_interests(self) - return self._component.interest + local interests = {} + for index = 1, #BaseComponent.ALL_INTERESTS do + local interest = BaseComponent.ALL_INTERESTS[index] + if self[interest] and type(self[interest]) == "function" then + table.insert(interests, interest) + end + end + + return interests end @@ -386,15 +383,14 @@ end --- Create new component. It will inheritance from basic -- druid component. -- @tparam string name BaseComponent name --- @tparam[opt={}] table interest List of component's interest -- @tparam[opt=DEFAULT] number input_priority The input priority. The bigger number processed first -- @local -function BaseComponent.static.create(name, interest, input_priority) +function BaseComponent.static.create(name, input_priority) -- Yea, inheritance here local new_class = class(name, BaseComponent) new_class.initialize = function(self) - BaseComponent.initialize(self, name, interest, input_priority) + BaseComponent.initialize(self, name, input_priority) end return new_class diff --git a/druid/const.lua b/druid/const.lua index af9a5bd..f05cb90 100755 --- a/druid/const.lua +++ b/druid/const.lua @@ -32,16 +32,15 @@ M.ZERO = "0" --- Component Interests -M.ALL = "all" -M.ON_INPUT = hash("on_input") -M.ON_UPDATE = hash("on_update") -M.ON_MESSAGE = hash("on_message") -M.ON_LATE_INIT = hash("on_late_init") -M.ON_FOCUS_LOST = hash("on_focus_lost") -M.ON_FOCUS_GAINED = hash("on_focus_gained") -M.ON_LAYOUT_CHANGE = hash("layout_changed") -M.ON_MESSAGE_INPUT = hash("on_message_input") -M.ON_LANGUAGE_CHANGE = hash("on_language_change") +M.ON_INPUT = "on_input" +M.ON_UPDATE = "update" +M.ON_MESSAGE = "on_message" +M.ON_LATE_INIT = "on_late_init" +M.ON_FOCUS_LOST = "on_focus_lost" +M.ON_FOCUS_GAINED = "on_focus_gained" +M.ON_LAYOUT_CHANGE = "on_layout_changed" +M.ON_MESSAGE_INPUT = "on_message_input" +M.ON_LANGUAGE_CHANGE = "on_language_change" M.PRIORITY_INPUT = 10 diff --git a/druid/extended/checkbox.lua b/druid/extended/checkbox.lua index 8195c44..6ce29a1 100755 --- a/druid/extended/checkbox.lua +++ b/druid/extended/checkbox.lua @@ -22,7 +22,7 @@ local Event = require("druid.event") local component = require("druid.component") -local Checkbox = component.create("checkbox", { component.ON_LAYOUT_CHANGE }) +local Checkbox = component.create("checkbox") local function on_click(self) diff --git a/druid/extended/component.template.lua b/druid/extended/component.template.lua index 3d2824c..65972f8 100644 --- a/druid/extended/component.template.lua +++ b/druid/extended/component.template.lua @@ -5,7 +5,7 @@ -- @local local component = require("druid.component") -local Component = component.create("my_component_name", { component.ON_UPDATE }) +local Component = component.create("my_component_name") -- Component constructor @@ -13,12 +13,12 @@ function Component:init(...) end --- Call only if exist interest: component.ON_UPDATE +-- Call every update step function Component:update(dt) end --- Call only if exist interest: component.ON_INPUT +-- Call default on_input from gui script function Component:on_input(action_id, action) return false end @@ -29,17 +29,17 @@ function Component:on_style_change(style) end --- Call only if exist interest: component.ON_MESSAGE +-- Call default on_message from gui script function Component:on_message(message_id, message, sender) end --- Call only if component with ON_LANGUAGE_CHANGE interest +-- Call if druid has triggered on_language_change function Component:on_language_change() end --- Call only if component with ON_LAYOUT_CHANGE interest +-- Call if game layout has changed and need to restore values in component function Component:on_layout_change() end @@ -50,12 +50,12 @@ function Component:on_input_interrupt() end --- Call, if game lost focus. Need ON_FOCUS_LOST intereset +-- Call, if game lost focus function Component:on_focus_lost() end --- Call, if game gained focus. Need ON_FOCUS_GAINED intereset +-- Call, if game gained focus function Component:on_focus_gained() end diff --git a/druid/extended/dynamic_grid.lua b/druid/extended/dynamic_grid.lua index cc83204..f22b564 100644 --- a/druid/extended/dynamic_grid.lua +++ b/druid/extended/dynamic_grid.lua @@ -45,7 +45,7 @@ local Event = require("druid.event") local helper = require("druid.helper") local component = require("druid.component") -local DynamicGrid = component.create("dynamic_grid", { component.ON_LAYOUT_CHANGE }) +local DynamicGrid = component.create("dynamic_grid") local SIDE_VECTORS = { diff --git a/druid/extended/input.lua b/druid/extended/input.lua index 356bacb..856b314 100755 --- a/druid/extended/input.lua +++ b/druid/extended/input.lua @@ -53,7 +53,7 @@ local const = require("druid.const") local component = require("druid.component") local utf8 = require("druid.system.utf8") -local Input = component.create("input", { component.ON_INPUT, component.ON_FOCUS_LOST }) +local Input = component.create("input") --- Mask text by replacing every character with a mask character diff --git a/druid/extended/lang_text.lua b/druid/extended/lang_text.lua index 61f473a..658d8a8 100755 --- a/druid/extended/lang_text.lua +++ b/druid/extended/lang_text.lua @@ -18,7 +18,7 @@ local Event = require("druid.event") local settings = require("druid.system.settings") local component = require("druid.component") -local LangText = component.create("lang_text", { component.ON_LANGUAGE_CHANGE }) +local LangText = component.create("lang_text") --- Component init function diff --git a/druid/extended/progress.lua b/druid/extended/progress.lua index e42b228..e1ddaf0 100644 --- a/druid/extended/progress.lua +++ b/druid/extended/progress.lua @@ -34,7 +34,7 @@ local const = require("druid.const") local helper = require("druid.helper") local component = require("druid.component") -local Progress = component.create("progress", { component.ON_UPDATE, component.ON_LAYOUT_CHANGE }) +local Progress = component.create("progress") local function check_steps(self, from, to, exactly) diff --git a/druid/extended/slider.lua b/druid/extended/slider.lua index d5185a3..a71a930 100644 --- a/druid/extended/slider.lua +++ b/druid/extended/slider.lua @@ -40,7 +40,7 @@ local helper = require("druid.helper") local const = require("druid.const") local component = require("druid.component") -local Slider = component.create("slider", { component.ON_INPUT, component.ON_LAYOUT_CHANGE }, const.PRIORITY_INPUT_HIGH) +local Slider = component.create("slider", const.PRIORITY_INPUT_HIGH) local function on_change_value(self) diff --git a/druid/extended/timer.lua b/druid/extended/timer.lua index 983a39c..8e82981 100644 --- a/druid/extended/timer.lua +++ b/druid/extended/timer.lua @@ -35,7 +35,7 @@ local formats = require("druid.helper.formats") local helper = require("druid.helper") local component = require("druid.component") -local Timer = component.create("timer", { component.ON_UPDATE }) +local Timer = component.create("timer") --- Component init function diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index 5f9b27b..e781f5e 100755 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -87,7 +87,7 @@ end local function sort_input_stack(self) - local input_components = self.components[base_component.ON_INPUT] + local input_components = self.components_interest[base_component.ON_INPUT] if not input_components then return end @@ -107,12 +107,12 @@ local function create(self, instance_class) local instance = instance_class() instance:setup_component(self, self._context, self._style) - table.insert(self.components[base_component.ALL], instance) + table.insert(self.components_all, instance) local register_to = instance:__get_interests() for i = 1, #register_to do local interest = register_to[i] - table.insert(self.components[interest], instance) + table.insert(self.components_interest[interest], instance) end return instance @@ -207,9 +207,10 @@ function DruidInstance.initialize(self, context, style) self._input_whitelist = nil self._no_auto_input = (sys.get_config("druid.no_auto_input") == "1") - self.components = {} + self.components_interest = {} + self.components_all = {} for i = 1, #base_component.ALL_INTERESTS do - self.components[base_component.ALL_INTERESTS[i]] = {} + self.components_interest[base_component.ALL_INTERESTS[i]] = {} end end @@ -245,7 +246,7 @@ end -- on all druid components -- @tparam DruidInstance self function DruidInstance.final(self) - local components = self.components[base_component.ALL] + local components = self.components_all for i = #components, 1, -1 do if components[i].on_remove then @@ -280,7 +281,7 @@ function DruidInstance.remove(self, component) end component._meta.children = {} - local all_components = self.components[base_component.ALL] + local all_components = self.components_all for i = #all_components, 1, -1 do if all_components[i] == component then if component.on_remove then @@ -293,7 +294,7 @@ function DruidInstance.remove(self, component) local interests = component:__get_interests() for i = 1, #interests do local interest = interests[i] - local components = self.components[interest] + local components = self.components_interest[interest] for j = #components, 1, -1 do if components[j] == component then table.remove(components, j) @@ -307,18 +308,18 @@ end -- @tparam DruidInstance self -- @tparam number dt Delta time function DruidInstance.update(self, dt) - local late_init_components = self.components[base_component.ON_LATE_INIT] + local late_init_components = self.components_interest[base_component.ON_LATE_INIT] while late_init_components[1] do late_init_components[1]:on_late_init() table.remove(late_init_components, 1) end - if not self.input_inited and #self.components[base_component.ON_INPUT] > 0 then + if not self.input_inited and #self.components_interest[base_component.ON_INPUT] > 0 then -- Input init on late init step, to be sure it goes after user go acquire input input_init(self) end - local components = self.components[base_component.ON_UPDATE] + local components = self.components_interest[base_component.ON_UPDATE] for i = 1, #components do components[i]:update(dt) end @@ -333,7 +334,7 @@ end function DruidInstance.on_input(self, action_id, action) self._is_input_processing = true - local components = self.components[base_component.ON_INPUT] + local components = self.components_interest[base_component.ON_INPUT] check_sort_input_stack(self, components) local is_input_consumed = process_input(self, action_id, action, components) @@ -358,10 +359,10 @@ end function DruidInstance.on_message(self, message_id, message, sender) -- TODO: refactor for more juicy code local specific_ui_message = base_component.SPECIFIC_UI_MESSAGES[message_id] - local on_message_input_message = base_component.SPECIFIC_UI_MESSAGES[base_component.ON_MESSAGE_INPUT] + local on_message_input_message = base_component.ON_MESSAGE_INPUT if specific_ui_message == on_message_input_message then - local components = self.components[message_id] + local components = self.components_interest[base_component.ON_MESSAGE_INPUT] if components then for i = 1, #components do local component = components[i] @@ -371,7 +372,7 @@ function DruidInstance.on_message(self, message_id, message, sender) end end elseif specific_ui_message then - local components = self.components[message_id] + local components = self.components_interest[specific_ui_message] if components then for i = 1, #components do local component = components[i] @@ -379,7 +380,7 @@ function DruidInstance.on_message(self, message_id, message, sender) end end else - local components = self.components[base_component.ON_MESSAGE] + local components = self.components_interest[base_component.ON_MESSAGE] for i = 1, #components do components[i]:on_message(message_id, message, sender) end @@ -391,7 +392,7 @@ end -- This one called by on_window_callback by global window listener -- @tparam DruidInstance self function DruidInstance.on_focus_lost(self) - local components = self.components[base_component.ON_FOCUS_LOST] + local components = self.components_interest[base_component.ON_FOCUS_LOST] for i = 1, #components do components[i]:on_focus_lost() end @@ -402,7 +403,7 @@ end -- This one called by on_window_callback by global window listener -- @tparam DruidInstance self function DruidInstance.on_focus_gained(self) - local components = self.components[base_component.ON_FOCUS_GAINED] + local components = self.components_interest[base_component.ON_FOCUS_GAINED] for i = 1, #components do components[i]:on_focus_gained() end @@ -413,7 +414,7 @@ end -- Called on update gui layout -- @tparam DruidInstance self function DruidInstance.on_layout_change(self) - local components = self.components[base_component.ON_LAYOUT_CHANGE] + local components = self.components_interest[base_component.ON_LAYOUT_CHANGE] for i = 1, #components do components[i]:on_layout_change() end @@ -426,7 +427,7 @@ end -- @tparam DruidInstance self -- @function druid.on_language_change function DruidInstance.on_language_change(self) - local components = self.components[base_component.ON_LANGUAGE_CHANGE] + local components = self.components_interest[base_component.ON_LANGUAGE_CHANGE] for i = 1, #components do components[i]:on_language_change() end