From 14dccea79964b26965438c1730584ec9abffd360 Mon Sep 17 00:00:00 2001 From: Insality Date: Thu, 1 Apr 2021 20:59:47 +0300 Subject: [PATCH] Update set/reset input priority functions. Remove increase_input_priority function --- druid/base/drag.lua | 2 +- druid/component.lua | 39 +++++++++++++++++++-------- druid/const.lua | 1 + druid/extended/input.lua | 4 +-- druid/system/druid_instance.lua | 47 ++++++++++++++++++--------------- example/kenney.collection | 1 + 6 files changed, 59 insertions(+), 35 deletions(-) diff --git a/druid/base/drag.lua b/druid/base/drag.lua index 35d9de4..0b9c6cd 100644 --- a/druid/base/drag.lua +++ b/druid/base/drag.lua @@ -91,7 +91,7 @@ local function process_touch(self, touch) if not self.is_drag and distance >= self.style.DRAG_DEADZONE then self.is_drag = true self.on_drag_start:trigger(self:get_context()) - self:increase_input_priority() + self:set_input_priority(const.PRIORITY_INPUT_MAX) end end diff --git a/druid/component.lua b/druid/component.lua index 34d4dd8..195a6d5 100644 --- a/druid/component.lua +++ b/druid/component.lua @@ -5,6 +5,7 @@ local const = require("druid.const") local class = require("druid.system.middleclass") +local helper = require("druid.helper") local BaseComponent = class("druid.component") @@ -95,17 +96,12 @@ end --- Increase input priority in current input stack -- @tparam BaseComponent self +-- @local function BaseComponent.increase_input_priority(self) - self._meta.increased_input_priority = true + helper.deprecated("The component:increase_input_priority is deprecated. Please use component:set_input_priority(druid_const.PRIORITY_INPUT_MAX) instead") end ---- Reset input priority in current input stack --- @tparam BaseComponent self -function BaseComponent.reset_input_priority(self) - self._meta.increased_input_priority = false -end - --- Get node for component by name. -- If component has nodes, node_or_name should be string @@ -169,7 +165,12 @@ end -- @treturn number The component input priority function BaseComponent.set_input_priority(self, value) assert(value) - self._component.input_priority = value + + if self._component.input_priority ~= value then + self._component.input_priority = value + self._component._is_input_priority_changed = true + end + return self end @@ -187,7 +188,7 @@ end -- @tparam BaseComponent self -- @treturn number The component uid function BaseComponent.get_uid(self) - return self._component.uid + return self._component._uid end @@ -234,7 +235,6 @@ function BaseComponent.setup_component(self, druid_instance, context, style) nodes = nil, style = nil, druid = druid_instance, - increased_input_priority = false, input_enabled = true, children = {} } @@ -266,11 +266,28 @@ function BaseComponent.initialize(self, name, interest, input_priority) interest = interest, input_priority = input_priority or const.PRIORITY_INPUT, default_input_priority = input_priority or const.PRIORITY_INPUT, - uid = BaseComponent.get_uid() + _is_input_priority_changed = true, -- Default true for sort once time after GUI init + _uid = BaseComponent.get_uid() } end +--- Return true, if input priority was changed +-- @tparam BaseComponent self +-- @local +function BaseComponent._is_input_priority_changed(self) + return self._component._is_input_priority_changed +end + + +--- Reset is_input_priority_changed field +-- @tparam BaseComponent self +-- @local +function BaseComponent._reset_input_priority_changed(self) + self._component._is_input_priority_changed = false +end + + function BaseComponent.__tostring(self) return self._component.name end diff --git a/druid/const.lua b/druid/const.lua index 77973d6..b98750d 100644 --- a/druid/const.lua +++ b/druid/const.lua @@ -39,6 +39,7 @@ M.ON_LANGUAGE_CHANGE = hash("on_language_change") M.PRIORITY_INPUT = 10 M.PRIORITY_INPUT_HIGH = 20 +M.PRIORITY_INPUT_MAX = 100 M.PIVOTS = { diff --git a/druid/extended/input.lua b/druid/extended/input.lua index c6b7adb..9159b56 100644 --- a/druid/extended/input.lua +++ b/druid/extended/input.lua @@ -271,8 +271,8 @@ function Input.select(self) gui.reset_keyboard() self.marked_value = "" if not self.is_selected then - self:increase_input_priority() - self.button:increase_input_priority() + self:set_input_priority(const.PRIORITY_INPUT_MAX) + self.button:set_input_priority(const.PRIORITY_INPUT_MAX) self.previous_value = self.value self.is_selected = true diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index e5d232f..6a4fac7 100644 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -92,6 +92,7 @@ local function sort_input_stack(self) 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 @@ -111,7 +112,6 @@ local function create(self, instance_class) if base_component.UI_INPUT[interest] then input_init(self) - sort_input_stack(self) end end @@ -119,6 +119,27 @@ local function create(self, instance_class) end +local function check_sort_input_stack(self, components) + if not components or #components == 0 then + return + end + + local is_need_sort_input_stack = false + + for i = #components, 1, -1 do + local component = components[i] + if component:_is_input_priority_changed() then + is_need_sort_input_stack = true + end + component:_reset_input_priority_changed() + end + + if is_need_sort_input_stack then + sort_input_stack(self) + end +end + + local function process_input(action_id, action, components, is_input_consumed) if #components == 0 then return is_input_consumed @@ -126,24 +147,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 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 - if component.on_input_interrupt then - component:on_input_interrupt() - end - end - end - end - - for i = #components, 1, -1 do - local component = components[i] - -- Process usual input priority next - local meta = component._meta - if meta.input_enabled and not meta.increased_input_priority then + if meta.input_enabled then if not is_input_consumed then is_input_consumed = component:on_input(action_id, action) else @@ -273,9 +278,9 @@ function DruidInstance.on_input(self, action_id, action) self._is_input_processing = true local is_input_consumed = false - - is_input_consumed = process_input(action_id, action, - self.components[base_component.ON_INPUT], is_input_consumed) + local components = self.components[base_component.ON_INPUT] + check_sort_input_stack(self, components) + is_input_consumed = process_input(action_id, action, components, is_input_consumed) self._is_input_processing = false diff --git a/example/kenney.collection b/example/kenney.collection index a8b3693..b983685 100644 --- a/example/kenney.collection +++ b/example/kenney.collection @@ -81,6 +81,7 @@ embedded_instances { "gain: 1.0\\n" "pan: 0.0\\n" "speed: 1.0\\n" + "loopcount: 0\\n" "\"\n" " position {\n" " x: 0.0\n"