From da085238cc3ca9c1443e1207c0dc4cafbf3796d8 Mon Sep 17 00:00:00 2001 From: Insality Date: Sat, 7 Nov 2020 17:28:07 +0300 Subject: [PATCH] #108 Move component interests const from const.lua to component.lua --- druid/base/back_handler.lua | 2 +- druid/base/blocker.lua | 2 +- druid/base/button.lua | 2 +- druid/base/drag.lua | 2 +- druid/base/hover.lua | 2 +- druid/base/scroll.lua | 3 +- druid/base/static_grid.lua | 2 +- druid/base/swipe.lua | 2 +- druid/base/text.lua | 2 +- druid/component.lua | 41 +++++++++++++++++++++++++++ druid/const.lua | 30 +------------------- druid/druid.lua | 11 +++---- druid/extended/checkbox.lua | 3 +- druid/extended/component.template.lua | 9 +++--- druid/extended/dynamic_grid.lua | 2 +- druid/extended/input.lua | 2 +- druid/extended/lang_text.lua | 3 +- druid/extended/progress.lua | 2 +- druid/extended/slider.lua | 2 +- druid/extended/timer.lua | 3 +- druid/system/druid_instance.lua | 36 +++++++++++------------ 21 files changed, 86 insertions(+), 77 deletions(-) diff --git a/druid/base/back_handler.lua b/druid/base/back_handler.lua index d6711df..1f2c5f0 100644 --- a/druid/base/back_handler.lua +++ b/druid/base/back_handler.lua @@ -14,7 +14,7 @@ local Event = require("druid.event") local const = require("druid.const") local component = require("druid.component") -local BackHandler = component.create("back_handler", { const.ON_INPUT }) +local BackHandler = component.create("back_handler", { component.ON_INPUT }) --- Component init function diff --git a/druid/base/blocker.lua b/druid/base/blocker.lua index 1017db9..50daf1b 100644 --- a/druid/base/blocker.lua +++ b/druid/base/blocker.lua @@ -10,7 +10,7 @@ local const = require("druid.const") local component = require("druid.component") -local Blocker = component.create("blocker", { const.ON_INPUT }) +local Blocker = component.create("blocker", { component.ON_INPUT }) --- Component init function diff --git a/druid/base/button.lua b/druid/base/button.lua index 3f25c4e..7d524f7 100644 --- a/druid/base/button.lua +++ b/druid/base/button.lua @@ -51,7 +51,7 @@ local const = require("druid.const") local helper = require("druid.helper") local component = require("druid.component") -local Button = component.create("button", { const.ON_INPUT }) +local Button = component.create("button", { component.ON_INPUT }) local function is_input_match(self, action_id) diff --git a/druid/base/drag.lua b/druid/base/drag.lua index 2eef901..8632929 100644 --- a/druid/base/drag.lua +++ b/druid/base/drag.lua @@ -48,7 +48,7 @@ local const = require("druid.const") local helper = require("druid.helper") local component = require("druid.component") -local Drag = component.create("drag", { const.ON_INPUT_HIGH }) +local Drag = component.create("drag", { component.ON_INPUT_HIGH }) local function start_touch(self, touch) diff --git a/druid/base/hover.lua b/druid/base/hover.lua index 4d61461..b426f58 100644 --- a/druid/base/hover.lua +++ b/druid/base/hover.lua @@ -15,7 +15,7 @@ local const = require("druid.const") local helper = require("druid.helper") local component = require("druid.component") -local Hover = component.create("hover", { const.ON_INPUT }) +local Hover = component.create("hover", { component.ON_INPUT }) --- Component init function diff --git a/druid/base/scroll.lua b/druid/base/scroll.lua index 7f76bb6..4c532ca 100644 --- a/druid/base/scroll.lua +++ b/druid/base/scroll.lua @@ -54,11 +54,10 @@ local Event = require("druid.event") -local const = require("druid.const") local helper = require("druid.helper") local component = require("druid.component") -local Scroll = component.create("scroll", { const.ON_UPDATE, const.ON_LAYOUT_CHANGE }) +local Scroll = component.create("scroll", { component.ON_UPDATE, component.ON_LAYOUT_CHANGE }) local function inverse_lerp(min, max, current) diff --git a/druid/base/static_grid.lua b/druid/base/static_grid.lua index 86f1c13..4bd6e9a 100644 --- a/druid/base/static_grid.lua +++ b/druid/base/static_grid.lua @@ -46,7 +46,7 @@ local Event = require("druid.event") local helper = require("druid.helper") local component = require("druid.component") -local StaticGrid = component.create("static_grid", { const.ON_LAYOUT_CHANGE }) +local StaticGrid = component.create("static_grid", { component.ON_LAYOUT_CHANGE }) --- Component init function diff --git a/druid/base/swipe.lua b/druid/base/swipe.lua index f248e64..84feff6 100644 --- a/druid/base/swipe.lua +++ b/druid/base/swipe.lua @@ -20,7 +20,7 @@ local const = require("druid.const") local helper = require("druid.helper") local component = require("druid.component") -local Swipe = component.create("swipe", { const.ON_INPUT }) +local Swipe = component.create("swipe", { component.ON_INPUT }) local function start_swipe(self, action) diff --git a/druid/base/text.lua b/druid/base/text.lua index ff29f19..2e3c2aa 100644 --- a/druid/base/text.lua +++ b/druid/base/text.lua @@ -43,7 +43,7 @@ local Event = require("druid.event") local const = require("druid.const") local component = require("druid.component") -local Text = component.create("text", { const.ON_LAYOUT_CHANGE }) +local Text = component.create("text", { component.ON_LAYOUT_CHANGE }) local function update_text_size(self) diff --git a/druid/component.lua b/druid/component.lua index f735edf..07475b4 100644 --- a/druid/component.lua +++ b/druid/component.lua @@ -6,9 +6,50 @@ local const = require("druid.const") local class = require("druid.system.middleclass") + 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 +BaseComponent.ON_INPUT_HIGH = const.ON_INPUT_HIGH +BaseComponent.ON_FOCUS_LOST = const.ON_FOCUS_LOST +BaseComponent.ON_FOCUS_GAINED = const.ON_FOCUS_GAINED +BaseComponent.ON_LAYOUT_CHANGE = const.ON_LAYOUT_CHANGE +BaseComponent.ON_LANGUAGE_CHANGE = const.ON_LANGUAGE_CHANGE + + +BaseComponent.ALL_INTERESTS = { + BaseComponent.ALL, + BaseComponent.ON_INPUT, + BaseComponent.ON_UPDATE, + BaseComponent.ON_MESSAGE, + BaseComponent.ON_FOCUS_LOST, + BaseComponent.ON_INPUT_HIGH, + BaseComponent.ON_FOCUS_GAINED, + BaseComponent.ON_LAYOUT_CHANGE, + BaseComponent.ON_LANGUAGE_CHANGE, +} + + +-- Value is method name of component +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_LANGUAGE_CHANGE] = "on_language_change", +} + + +BaseComponent.UI_INPUT = { + [BaseComponent.ON_INPUT_HIGH] = true, + [BaseComponent.ON_INPUT] = true +} + + --- Set current component style table. -- Invoke `on_style_change` on component, if exist. BaseComponent should handle -- their style changing and store all style params diff --git a/druid/const.lua b/druid/const.lua index a16499e..c50cf8e 100644 --- a/druid/const.lua +++ b/druid/const.lua @@ -24,10 +24,10 @@ M.PRESSED = "pressed" M.STRING = "string" M.TABLE = "table" M.ZERO = "0" -M.ALL = "all" --- Component Interests +M.ALL = "all" M.ON_INPUT = hash("on_input") M.ON_UPDATE = hash("on_update") M.ON_MESSAGE = hash("on_message") @@ -38,19 +38,6 @@ M.ON_LAYOUT_CHANGE = hash("layout_changed") M.ON_LANGUAGE_CHANGE = hash("on_language_change") -M.ALL_INTERESTS = { - M.ALL, - M.ON_INPUT, - M.ON_UPDATE, - M.ON_MESSAGE, - M.ON_FOCUS_LOST, - M.ON_INPUT_HIGH, - M.ON_FOCUS_GAINED, - M.ON_LAYOUT_CHANGE, - M.ON_LANGUAGE_CHANGE, -} - - M.PIVOTS = { [gui.PIVOT_CENTER] = vmath.vector3(0), [gui.PIVOT_N] = vmath.vector3(0, 0.5, 0), @@ -64,21 +51,6 @@ M.PIVOTS = { } --- Value is method name of component -M.SPECIFIC_UI_MESSAGES = { - [M.ON_FOCUS_LOST] = "on_focus_lost", - [M.ON_FOCUS_GAINED] = "on_focus_gained", - [M.ON_LAYOUT_CHANGE] = "on_layout_change", - [M.ON_LANGUAGE_CHANGE] = "on_language_change", -} - - -M.UI_INPUT = { - [M.ON_INPUT_HIGH] = true, - [M.ON_INPUT] = true -} - - M.OS = { ANDROID = "Android", IOS = "iPhone OS", diff --git a/druid/druid.lua b/druid/druid.lua index 707eb28..041bd04 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -15,8 +15,9 @@ -- @module druid local const = require("druid.const") -local druid_instance = require("druid.system.druid_instance") +local base_component = require("druid.component") local settings = require("druid.system.settings") +local druid_instance = require("druid.system.druid_instance") local default_style = require("druid.styles.default.style") @@ -105,13 +106,13 @@ function M.on_window_callback(event) if event == window.WINDOW_EVENT_FOCUS_LOST then for i = 1, #instances do - msg.post(instances[i].url, const.ON_FOCUS_LOST) + msg.post(instances[i].url, base_component.ON_FOCUS_LOST) end end if event == window.WINDOW_EVENT_FOCUS_GAINED then for i = 1, #instances do - msg.post(instances[i].url, const.ON_FOCUS_GAINED) + msg.post(instances[i].url, base_component.ON_FOCUS_GAINED) end end end @@ -123,7 +124,7 @@ function M.on_layout_change() local instances = get_druid_instances() for i = 1, #instances do - msg.post(instances[i].url, const.ON_LAYOUT_CHANGE) + msg.post(instances[i].url, base_component.ON_LAYOUT_CHANGE) end end @@ -135,7 +136,7 @@ function M.on_language_change() local instances = get_druid_instances() for i = 1, #instances do - msg.post(instances[i].url, const.ON_LANGUAGE_CHANGE) + msg.post(instances[i].url, base_component.ON_LANGUAGE_CHANGE) end end diff --git a/druid/extended/checkbox.lua b/druid/extended/checkbox.lua index 24b5e56..d9d7a8b 100644 --- a/druid/extended/checkbox.lua +++ b/druid/extended/checkbox.lua @@ -16,11 +16,10 @@ -- @tfield Button button -local const = require("druid.const") local Event = require("druid.event") local component = require("druid.component") -local Checkbox = component.create("checkbox", { const.ON_LAYOUT_CHANGE }) +local Checkbox = component.create("checkbox", { component.ON_LAYOUT_CHANGE }) local function on_click(self) diff --git a/druid/extended/component.template.lua b/druid/extended/component.template.lua index fbf33b8..a6956be 100644 --- a/druid/extended/component.template.lua +++ b/druid/extended/component.template.lua @@ -1,10 +1,9 @@ --- Druid component template -- @module druid.component -- @local -local const = require("druid.const") local component = require("druid.component") -local Component = component.create("my_component_name", { const.ON_UPDATE }) +local Component = component.create("my_component_name", { component.ON_UPDATE }) -- Component constructor @@ -12,12 +11,12 @@ function Component:init(...) end --- Call only if exist interest: const.ON_UPDATE +-- Call only if exist interest: component.ON_UPDATE function Component:update(dt) end --- Call only if exist interest: const.ON_INPUT or const.ON_INPUT_HIGH +-- Call only if exist interest: component.ON_INPUT or component.ON_INPUT_HIGH function Component:on_input(action_id, action) return false end @@ -28,7 +27,7 @@ function Component:on_style_change(style) end --- Call only if exist interest: const.ON_MESSAGE +-- Call only if exist interest: component.ON_MESSAGE function Component:on_message(message_id, message, sender) end diff --git a/druid/extended/dynamic_grid.lua b/druid/extended/dynamic_grid.lua index e2b618f..66caae5 100644 --- a/druid/extended/dynamic_grid.lua +++ b/druid/extended/dynamic_grid.lua @@ -42,7 +42,7 @@ local Event = require("druid.event") local helper = require("druid.helper") local component = require("druid.component") -local DynamicGrid = component.create("dynamic_grid", { const.ON_LAYOUT_CHANGE }) +local DynamicGrid = component.create("dynamic_grid", { component.ON_LAYOUT_CHANGE }) local SIDE_VECTORS = { diff --git a/druid/extended/input.lua b/druid/extended/input.lua index bbd49b9..c6b7adb 100644 --- a/druid/extended/input.lua +++ b/druid/extended/input.lua @@ -50,7 +50,7 @@ local const = require("druid.const") local component = require("druid.component") local utf8 = require("druid.system.utf8") -local Input = component.create("input", { const.ON_INPUT, const.ON_FOCUS_LOST }) +local Input = component.create("input", { component.ON_INPUT, component.ON_FOCUS_LOST }) --- 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 b51992c..b62aa6e 100644 --- a/druid/extended/lang_text.lua +++ b/druid/extended/lang_text.lua @@ -12,11 +12,10 @@ local Event = require("druid.event") -local const = require("druid.const") local settings = require("druid.system.settings") local component = require("druid.component") -local LangText = component.create("lang_text", { const.ON_LANGUAGE_CHANGE }) +local LangText = component.create("lang_text", { component.ON_LANGUAGE_CHANGE }) --- Component init function diff --git a/druid/extended/progress.lua b/druid/extended/progress.lua index fb257e1..2cfe229 100644 --- a/druid/extended/progress.lua +++ b/druid/extended/progress.lua @@ -31,7 +31,7 @@ local const = require("druid.const") local helper = require("druid.helper") local component = require("druid.component") -local Progress = component.create("progress", { const.ON_UPDATE, const.ON_LAYOUT_CHANGE }) +local Progress = component.create("progress", { component.ON_UPDATE, component.ON_LAYOUT_CHANGE }) local function check_steps(self, from, to, exactly) diff --git a/druid/extended/slider.lua b/druid/extended/slider.lua index 97b306b..f07d5c8 100644 --- a/druid/extended/slider.lua +++ b/druid/extended/slider.lua @@ -37,7 +37,7 @@ local helper = require("druid.helper") local const = require("druid.const") local component = require("druid.component") -local Slider = component.create("slider", { const.ON_INPUT_HIGH, const.ON_LAYOUT_CHANGE }) +local Slider = component.create("slider", { component.ON_INPUT_HIGH, component.ON_LAYOUT_CHANGE }) local function on_change_value(self) diff --git a/druid/extended/timer.lua b/druid/extended/timer.lua index 5ae1ba8..7076543 100644 --- a/druid/extended/timer.lua +++ b/druid/extended/timer.lua @@ -28,12 +28,11 @@ local Event = require("druid.event") -local const = require("druid.const") local formats = require("druid.helper.formats") local helper = require("druid.helper") local component = require("druid.component") -local Timer = component.create("timer", { const.ON_UPDATE }) +local Timer = component.create("timer", { component.ON_UPDATE }) --- Component init function diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index 3d19ea3..88eed5b 100644 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -28,11 +28,11 @@ -- @see Drag -- @see Hover -local const = require("druid.const") local helper = require("druid.helper") -local druid_input = require("druid.helper.druid_input") -local settings = require("druid.system.settings") local class = require("druid.system.middleclass") +local settings = require("druid.system.settings") +local base_component = require("druid.component") +local druid_input = require("druid.helper.druid_input") local back_handler = require("druid.base.back_handler") local blocker = require("druid.base.blocker") @@ -87,14 +87,14 @@ local function create(self, instance_class) local instance = instance_class() instance:setup_component(self, self._context, self._style) - table.insert(self.components[const.ALL], instance) + table.insert(self.components[base_component.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) - if const.UI_INPUT[interest] then + if base_component.UI_INPUT[interest] then input_init(self) end end @@ -155,8 +155,8 @@ function DruidInstance.initialize(self, context, style) self.url = msg.url() self.components = {} - for i = 1, #const.ALL_INTERESTS do - self.components[const.ALL_INTERESTS[i]] = {} + for i = 1, #base_component.ALL_INTERESTS do + self.components[base_component.ALL_INTERESTS[i]] = {} end end @@ -180,7 +180,7 @@ end -- on all druid components -- @tparam DruidInstance self function DruidInstance.final(self) - local components = self.components[const.ALL] + local components = self.components[base_component.ALL] for i = #components, 1, -1 do if components[i].on_remove then @@ -215,7 +215,7 @@ function DruidInstance.remove(self, component) end component._meta.children = {} - local all_components = self.components[const.ALL] + local all_components = self.components[base_component.ALL] for i = #all_components, 1, -1 do if all_components[i] == component then if component.on_remove then @@ -242,7 +242,7 @@ end -- @tparam DruidInstance self -- @tparam number dt Delta time function DruidInstance.update(self, dt) - local components = self.components[const.ON_UPDATE] + local components = self.components[base_component.ON_UPDATE] for i = 1, #components do components[i]:update(dt) end @@ -259,10 +259,10 @@ function DruidInstance.on_input(self, action_id, action) local is_input_consumed = false is_input_consumed = process_input(action_id, action, - self.components[const.ON_INPUT_HIGH], is_input_consumed) + self.components[base_component.ON_INPUT_HIGH], is_input_consumed) is_input_consumed = process_input(action_id, action, - self.components[const.ON_INPUT], is_input_consumed) + self.components[base_component.ON_INPUT], is_input_consumed) self._is_input_processing = false @@ -283,7 +283,7 @@ end -- @tparam table message Message from on_message -- @tparam hash sender Sender from on_message function DruidInstance.on_message(self, message_id, message, sender) - local specific_ui_message = const.SPECIFIC_UI_MESSAGES[message_id] + local specific_ui_message = base_component.SPECIFIC_UI_MESSAGES[message_id] if specific_ui_message then local components = self.components[message_id] @@ -294,7 +294,7 @@ function DruidInstance.on_message(self, message_id, message, sender) end end else - local components = self.components[const.ON_MESSAGE] + local components = self.components[base_component.ON_MESSAGE] for i = 1, #components do components[i]:on_message(message_id, message, sender) end @@ -306,7 +306,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[const.ON_FOCUS_LOST] + local components = self.components[base_component.ON_FOCUS_LOST] for i = 1, #components do components[i]:on_focus_lost() end @@ -317,7 +317,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[const.ON_FOCUS_GAINED] + local components = self.components[base_component.ON_FOCUS_GAINED] for i = 1, #components do components[i]:on_focus_gained() end @@ -328,7 +328,7 @@ end -- Called on update gui layout -- @tparam DruidInstance self function DruidInstance.on_layout_change(self) - local components = self.components[const.ON_LAYOUT_CHANGE] + local components = self.components[base_component.ON_LAYOUT_CHANGE] for i = 1, #components do components[i]:on_layout_change() end @@ -340,7 +340,7 @@ end -- call manualy to update all translations -- @function druid.on_language_change function DruidInstance.on_language_change(self) - local components = self.components[const.ON_LANGUAGE_CHANGE] + local components = self.components[base_component.ON_LANGUAGE_CHANGE] for i = 1, #components do components[i]:on_language_change() end