From 731a47e01dd3268194ded91c030a444fa52de080 Mon Sep 17 00:00:00 2001 From: Insality Date: Sun, 7 Feb 2021 14:57:39 +0500 Subject: [PATCH] Add separate input priority component value --- druid/base/drag.lua | 2 +- druid/component.lua | 38 ++++++++++++++++++++++----- druid/const.lua | 5 +++- druid/extended/component.template.lua | 2 +- druid/extended/slider.lua | 2 +- druid/system/druid_instance.lua | 19 +++++++++++--- 6 files changed, 54 insertions(+), 14 deletions(-) diff --git a/druid/base/drag.lua b/druid/base/drag.lua index 8632929..35d9de4 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", { component.ON_INPUT_HIGH }) +local Drag = component.create("drag", { component.ON_INPUT }, const.PRIORITY_INPUT_HIGH) local function start_touch(self, touch) diff --git a/druid/component.lua b/druid/component.lua index 07475b4..5e482fc 100644 --- a/druid/component.lua +++ b/druid/component.lua @@ -15,7 +15,6 @@ 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 @@ -28,7 +27,6 @@ BaseComponent.ALL_INTERESTS = { 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, @@ -45,11 +43,17 @@ BaseComponent.SPECIFIC_UI_MESSAGES = { BaseComponent.UI_INPUT = { - [BaseComponent.ON_INPUT_HIGH] = true, [BaseComponent.ON_INPUT] = true } +local uid = 0 +function BaseComponent.static.get_uid() + uid = uid + 1 + return uid +end + + --- Set current component style table. -- Invoke `on_style_change` on component, if exist. BaseComponent should handle -- their style changing and store all style params @@ -151,6 +155,22 @@ function BaseComponent.get_name(self) end +--- Return component input priority +-- @tparam BaseComponent self +-- @treturn number The component input priority +function BaseComponent.get_input_priority(self) + return self._component.input_priority +end + + +--- Return component uid. UID generated in component creation order +-- @tparam BaseComponent self +-- @treturn number The component uid +function BaseComponent.get_uid(self) + return self._component.uid +end + + --- Set component input state. By default it enabled -- You can disable any input of component by this function -- @tparam BaseComponent self @@ -216,13 +236,16 @@ end -- @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) +function BaseComponent.initialize(self, name, interest, input_priority) interest = interest or {} self._component = { name = name, - interest = interest + interest = interest, + input_priority = input_priority or const.PRIORITY_INPUT, + uid = BaseComponent.get_uid() } end @@ -294,13 +317,14 @@ end -- 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) +function BaseComponent.static.create(name, interest, input_priority) -- Yea, inheritance here local new_class = class(name, BaseComponent) new_class.initialize = function(self) - BaseComponent.initialize(self, name, interest) + BaseComponent.initialize(self, name, interest, input_priority) end return new_class diff --git a/druid/const.lua b/druid/const.lua index c50cf8e..77973d6 100644 --- a/druid/const.lua +++ b/druid/const.lua @@ -31,13 +31,16 @@ M.ALL = "all" M.ON_INPUT = hash("on_input") M.ON_UPDATE = hash("on_update") M.ON_MESSAGE = hash("on_message") -M.ON_INPUT_HIGH = hash("on_input_high") 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_LANGUAGE_CHANGE = hash("on_language_change") +M.PRIORITY_INPUT = 10 +M.PRIORITY_INPUT_HIGH = 20 + + M.PIVOTS = { [gui.PIVOT_CENTER] = vmath.vector3(0), [gui.PIVOT_N] = vmath.vector3(0, 0.5, 0), diff --git a/druid/extended/component.template.lua b/druid/extended/component.template.lua index a6956be..587d0e8 100644 --- a/druid/extended/component.template.lua +++ b/druid/extended/component.template.lua @@ -16,7 +16,7 @@ function Component:update(dt) end --- Call only if exist interest: component.ON_INPUT or component.ON_INPUT_HIGH +-- Call only if exist interest: component.ON_INPUT function Component:on_input(action_id, action) return false end diff --git a/druid/extended/slider.lua b/druid/extended/slider.lua index f07d5c8..33cfbdd 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", { component.ON_INPUT_HIGH, component.ON_LAYOUT_CHANGE }) +local Slider = component.create("slider", { component.ON_INPUT, component.ON_LAYOUT_CHANGE }, const.PRIORITY_INPUT_HIGH) local function on_change_value(self) diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index 88eed5b..e5d232f 100644 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -82,6 +82,21 @@ local function input_release(self) end +local function sort_input_stack(self) + local input_components = self.components[base_component.ON_INPUT] + if not input_components then + return + end + + table.sort(input_components, function(a, b) + if a:get_input_priority() ~= b:get_input_priority() then + return a:get_input_priority() < b:get_input_priority() + end + return a:get_uid() < b:get_uid() + end) +end + + -- Create the component itself local function create(self, instance_class) local instance = instance_class() @@ -96,6 +111,7 @@ local function create(self, instance_class) if base_component.UI_INPUT[interest] then input_init(self) + sort_input_stack(self) end end @@ -258,9 +274,6 @@ function DruidInstance.on_input(self, action_id, action) local is_input_consumed = false - is_input_consumed = process_input(action_id, action, - self.components[base_component.ON_INPUT_HIGH], is_input_consumed) - is_input_consumed = process_input(action_id, action, self.components[base_component.ON_INPUT], is_input_consumed)