Add separate input priority component value

This commit is contained in:
Insality 2021-02-07 14:57:39 +05:00
parent 4e65895966
commit 731a47e01d
6 changed files with 54 additions and 14 deletions

View File

@ -48,7 +48,7 @@ local const = require("druid.const")
local helper = require("druid.helper") local helper = require("druid.helper")
local component = require("druid.component") 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) local function start_touch(self, touch)

View File

@ -15,7 +15,6 @@ BaseComponent.ALL = const.ALL
BaseComponent.ON_INPUT = const.ON_INPUT BaseComponent.ON_INPUT = const.ON_INPUT
BaseComponent.ON_UPDATE = const.ON_UPDATE BaseComponent.ON_UPDATE = const.ON_UPDATE
BaseComponent.ON_MESSAGE = const.ON_MESSAGE 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_LOST = const.ON_FOCUS_LOST
BaseComponent.ON_FOCUS_GAINED = const.ON_FOCUS_GAINED BaseComponent.ON_FOCUS_GAINED = const.ON_FOCUS_GAINED
BaseComponent.ON_LAYOUT_CHANGE = const.ON_LAYOUT_CHANGE BaseComponent.ON_LAYOUT_CHANGE = const.ON_LAYOUT_CHANGE
@ -28,7 +27,6 @@ BaseComponent.ALL_INTERESTS = {
BaseComponent.ON_UPDATE, BaseComponent.ON_UPDATE,
BaseComponent.ON_MESSAGE, BaseComponent.ON_MESSAGE,
BaseComponent.ON_FOCUS_LOST, BaseComponent.ON_FOCUS_LOST,
BaseComponent.ON_INPUT_HIGH,
BaseComponent.ON_FOCUS_GAINED, BaseComponent.ON_FOCUS_GAINED,
BaseComponent.ON_LAYOUT_CHANGE, BaseComponent.ON_LAYOUT_CHANGE,
BaseComponent.ON_LANGUAGE_CHANGE, BaseComponent.ON_LANGUAGE_CHANGE,
@ -45,11 +43,17 @@ BaseComponent.SPECIFIC_UI_MESSAGES = {
BaseComponent.UI_INPUT = { BaseComponent.UI_INPUT = {
[BaseComponent.ON_INPUT_HIGH] = true,
[BaseComponent.ON_INPUT] = true [BaseComponent.ON_INPUT] = true
} }
local uid = 0
function BaseComponent.static.get_uid()
uid = uid + 1
return uid
end
--- Set current component style table. --- Set current component style table.
-- Invoke `on_style_change` on component, if exist. BaseComponent should handle -- Invoke `on_style_change` on component, if exist. BaseComponent should handle
-- their style changing and store all style params -- their style changing and store all style params
@ -151,6 +155,22 @@ function BaseComponent.get_name(self)
end 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 --- Set component input state. By default it enabled
-- You can disable any input of component by this function -- You can disable any input of component by this function
-- @tparam BaseComponent self -- @tparam BaseComponent self
@ -216,13 +236,16 @@ end
-- @tparam BaseComponent self -- @tparam BaseComponent self
-- @tparam string name BaseComponent name -- @tparam string name BaseComponent name
-- @tparam[opt={}] table interest List of component's interest -- @tparam[opt={}] table interest List of component's interest
-- @tparam[opt=DEFAULT] number input_priority The input priority. The bigger number processed first
-- @local -- @local
function BaseComponent.initialize(self, name, interest) function BaseComponent.initialize(self, name, interest, input_priority)
interest = interest or {} interest = interest or {}
self._component = { self._component = {
name = name, name = name,
interest = interest interest = interest,
input_priority = input_priority or const.PRIORITY_INPUT,
uid = BaseComponent.get_uid()
} }
end end
@ -294,13 +317,14 @@ end
-- druid component. -- druid component.
-- @tparam string name BaseComponent name -- @tparam string name BaseComponent name
-- @tparam[opt={}] table interest List of component's interest -- @tparam[opt={}] table interest List of component's interest
-- @tparam[opt=DEFAULT] number input_priority The input priority. The bigger number processed first
-- @local -- @local
function BaseComponent.static.create(name, interest) function BaseComponent.static.create(name, interest, input_priority)
-- Yea, inheritance here -- Yea, inheritance here
local new_class = class(name, BaseComponent) local new_class = class(name, BaseComponent)
new_class.initialize = function(self) new_class.initialize = function(self)
BaseComponent.initialize(self, name, interest) BaseComponent.initialize(self, name, interest, input_priority)
end end
return new_class return new_class

View File

@ -31,13 +31,16 @@ M.ALL = "all"
M.ON_INPUT = hash("on_input") M.ON_INPUT = hash("on_input")
M.ON_UPDATE = hash("on_update") M.ON_UPDATE = hash("on_update")
M.ON_MESSAGE = hash("on_message") 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_LOST = hash("on_focus_lost")
M.ON_FOCUS_GAINED = hash("on_focus_gained") M.ON_FOCUS_GAINED = hash("on_focus_gained")
M.ON_LAYOUT_CHANGE = hash("layout_changed") M.ON_LAYOUT_CHANGE = hash("layout_changed")
M.ON_LANGUAGE_CHANGE = hash("on_language_change") M.ON_LANGUAGE_CHANGE = hash("on_language_change")
M.PRIORITY_INPUT = 10
M.PRIORITY_INPUT_HIGH = 20
M.PIVOTS = { M.PIVOTS = {
[gui.PIVOT_CENTER] = vmath.vector3(0), [gui.PIVOT_CENTER] = vmath.vector3(0),
[gui.PIVOT_N] = vmath.vector3(0, 0.5, 0), [gui.PIVOT_N] = vmath.vector3(0, 0.5, 0),

View File

@ -16,7 +16,7 @@ function Component:update(dt)
end 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) function Component:on_input(action_id, action)
return false return false
end end

View File

@ -37,7 +37,7 @@ local helper = require("druid.helper")
local const = require("druid.const") local const = require("druid.const")
local component = require("druid.component") 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) local function on_change_value(self)

View File

@ -82,6 +82,21 @@ local function input_release(self)
end 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 -- Create the component itself
local function create(self, instance_class) local function create(self, instance_class)
local instance = instance_class() local instance = instance_class()
@ -96,6 +111,7 @@ local function create(self, instance_class)
if base_component.UI_INPUT[interest] then if base_component.UI_INPUT[interest] then
input_init(self) input_init(self)
sort_input_stack(self)
end end
end end
@ -258,9 +274,6 @@ function DruidInstance.on_input(self, action_id, action)
local is_input_consumed = false 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, is_input_consumed = process_input(action_id, action,
self.components[base_component.ON_INPUT], is_input_consumed) self.components[base_component.ON_INPUT], is_input_consumed)