#160 Remove component interests, now setup it with function declaring

This commit is contained in:
Insality 2022-01-27 01:48:05 +02:00
parent 7a5faa2b85
commit 54f80aeccd
20 changed files with 72 additions and 85 deletions

View File

@ -17,7 +17,7 @@ local Event = require("druid.event")
local const = require("druid.const") local const = require("druid.const")
local component = require("druid.component") local component = require("druid.component")
local BackHandler = component.create("back_handler", { component.ON_INPUT }) local BackHandler = component.create("back_handler")
--- Component init function --- Component init function

View File

@ -13,7 +13,7 @@
local const = require("druid.const") local const = require("druid.const")
local component = require("druid.component") local component = require("druid.component")
local Blocker = component.create("blocker", { component.ON_INPUT }) local Blocker = component.create("blocker")
--- Component init function --- Component init function

View File

@ -57,11 +57,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 Button = component.create("button", { local Button = component.create("button")
component.ON_INPUT,
component.ON_MESSAGE_INPUT,
component.ON_LATE_INIT
})
local function is_input_match(self, action_id) local function is_input_match(self, action_id)

View File

@ -51,7 +51,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, component.ON_LATE_INIT }, const.PRIORITY_INPUT_HIGH) local Drag = component.create("drag", const.PRIORITY_INPUT_HIGH)
local function start_touch(self, touch) local function start_touch(self, touch)

View File

@ -18,7 +18,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 Hover = component.create("hover", { component.ON_INPUT, component.ON_LATE_INIT }) local Hover = component.create("hover")
--- Component init function --- Component init function

View File

@ -61,12 +61,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 Scroll = component.create("scroll", { local Scroll = component.create("scroll")
component.ON_INPUT,
component.ON_UPDATE,
component.ON_LAYOUT_CHANGE,
component.ON_LATE_INIT
})
local function inverse_lerp(min, max, current) local function inverse_lerp(min, max, current)

View File

@ -49,7 +49,7 @@ local Event = require("druid.event")
local helper = require("druid.helper") local helper = require("druid.helper")
local component = require("druid.component") 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) local function _extend_border(border, pos, size, pivot)

View File

@ -23,7 +23,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 Swipe = component.create("swipe", { component.ON_INPUT, component.ON_LATE_INIT }) local Swipe = component.create("swipe")
local function start_swipe(self, action) local function start_swipe(self, action)

View File

@ -50,7 +50,7 @@ local const = require("druid.const")
local utf8 = require("druid.system.utf8") local utf8 = require("druid.system.utf8")
local component = require("druid.component") 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) local function update_text_size(self)

View File

@ -14,7 +14,6 @@ local BaseComponent = class("druid.component")
--- Component Interests --- Component Interests
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
@ -27,7 +26,6 @@ BaseComponent.ON_LANGUAGE_CHANGE = const.ON_LANGUAGE_CHANGE
BaseComponent.ALL_INTERESTS = { BaseComponent.ALL_INTERESTS = {
BaseComponent.ALL,
BaseComponent.ON_INPUT, BaseComponent.ON_INPUT,
BaseComponent.ON_UPDATE, BaseComponent.ON_UPDATE,
BaseComponent.ON_MESSAGE, 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.SPECIFIC_UI_MESSAGES = {
[BaseComponent.ON_FOCUS_LOST] = "on_focus_lost", [hash(BaseComponent.ON_FOCUS_LOST)] = "on_focus_lost",
[BaseComponent.ON_FOCUS_GAINED] = "on_focus_gained", [hash(BaseComponent.ON_FOCUS_GAINED)] = "on_focus_gained",
[BaseComponent.ON_LAYOUT_CHANGE] = "on_layout_change", [hash(BaseComponent.ON_LAYOUT_CHANGE)] = "on_layout_change",
[BaseComponent.ON_MESSAGE_INPUT] = "on_message_input", [hash(BaseComponent.ON_MESSAGE_INPUT)] = "on_message_input",
[BaseComponent.ON_LANGUAGE_CHANGE] = "on_language_change", [hash(BaseComponent.ON_LANGUAGE_CHANGE)] = "on_language_change",
}
BaseComponent.UI_INPUT = {
[BaseComponent.ON_INPUT] = true
} }
@ -267,15 +260,11 @@ end
-- by `BaseComponent.static.create` -- by `BaseComponent.static.create`
-- @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=DEFAULT] number input_priority The input priority. The bigger number processed first -- @tparam[opt=DEFAULT] number input_priority The input priority. The bigger number processed first
-- @local -- @local
function BaseComponent.initialize(self, name, interest, input_priority) function BaseComponent.initialize(self, name, input_priority)
interest = interest or {}
self._component = { self._component = {
name = name, name = name,
interest = interest,
input_priority = input_priority or const.PRIORITY_INPUT, input_priority = input_priority or const.PRIORITY_INPUT,
default_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 _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 -- @treturn table List of component interests
-- @local -- @local
function BaseComponent.__get_interests(self) 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 end
@ -386,15 +383,14 @@ end
--- Create new component. It will inheritance from basic --- Create new component. It will inheritance from basic
-- 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=DEFAULT] number input_priority The input priority. The bigger number processed first -- @tparam[opt=DEFAULT] number input_priority The input priority. The bigger number processed first
-- @local -- @local
function BaseComponent.static.create(name, interest, input_priority) function BaseComponent.static.create(name, 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, input_priority) BaseComponent.initialize(self, name, input_priority)
end end
return new_class return new_class

View File

@ -32,16 +32,15 @@ M.ZERO = "0"
--- Component Interests --- Component Interests
M.ALL = "all" M.ON_INPUT = "on_input"
M.ON_INPUT = hash("on_input") M.ON_UPDATE = "update"
M.ON_UPDATE = hash("on_update") M.ON_MESSAGE = "on_message"
M.ON_MESSAGE = hash("on_message") M.ON_LATE_INIT = "on_late_init"
M.ON_LATE_INIT = hash("on_late_init") M.ON_FOCUS_LOST = "on_focus_lost"
M.ON_FOCUS_LOST = hash("on_focus_lost") M.ON_FOCUS_GAINED = "on_focus_gained"
M.ON_FOCUS_GAINED = hash("on_focus_gained") M.ON_LAYOUT_CHANGE = "on_layout_changed"
M.ON_LAYOUT_CHANGE = hash("layout_changed") M.ON_MESSAGE_INPUT = "on_message_input"
M.ON_MESSAGE_INPUT = hash("on_message_input") M.ON_LANGUAGE_CHANGE = "on_language_change"
M.ON_LANGUAGE_CHANGE = hash("on_language_change")
M.PRIORITY_INPUT = 10 M.PRIORITY_INPUT = 10

View File

@ -22,7 +22,7 @@
local Event = require("druid.event") local Event = require("druid.event")
local component = require("druid.component") local component = require("druid.component")
local Checkbox = component.create("checkbox", { component.ON_LAYOUT_CHANGE }) local Checkbox = component.create("checkbox")
local function on_click(self) local function on_click(self)

View File

@ -5,7 +5,7 @@
-- @local -- @local
local component = require("druid.component") local component = require("druid.component")
local Component = component.create("my_component_name", { component.ON_UPDATE }) local Component = component.create("my_component_name")
-- Component constructor -- Component constructor
@ -13,12 +13,12 @@ function Component:init(...)
end end
-- Call only if exist interest: component.ON_UPDATE -- Call every update step
function Component:update(dt) function Component:update(dt)
end end
-- Call only if exist interest: component.ON_INPUT -- Call default on_input from gui script
function Component:on_input(action_id, action) function Component:on_input(action_id, action)
return false return false
end end
@ -29,17 +29,17 @@ function Component:on_style_change(style)
end end
-- Call only if exist interest: component.ON_MESSAGE -- Call default on_message from gui script
function Component:on_message(message_id, message, sender) function Component:on_message(message_id, message, sender)
end end
-- Call only if component with ON_LANGUAGE_CHANGE interest -- Call if druid has triggered on_language_change
function Component:on_language_change() function Component:on_language_change()
end 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() function Component:on_layout_change()
end end
@ -50,12 +50,12 @@ function Component:on_input_interrupt()
end end
-- Call, if game lost focus. Need ON_FOCUS_LOST intereset -- Call, if game lost focus
function Component:on_focus_lost() function Component:on_focus_lost()
end end
-- Call, if game gained focus. Need ON_FOCUS_GAINED intereset -- Call, if game gained focus
function Component:on_focus_gained() function Component:on_focus_gained()
end end

View File

@ -45,7 +45,7 @@ local Event = require("druid.event")
local helper = require("druid.helper") local helper = require("druid.helper")
local component = require("druid.component") local component = require("druid.component")
local DynamicGrid = component.create("dynamic_grid", { component.ON_LAYOUT_CHANGE }) local DynamicGrid = component.create("dynamic_grid")
local SIDE_VECTORS = { local SIDE_VECTORS = {

View File

@ -53,7 +53,7 @@ local const = require("druid.const")
local component = require("druid.component") local component = require("druid.component")
local utf8 = require("druid.system.utf8") 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 --- Mask text by replacing every character with a mask character

View File

@ -18,7 +18,7 @@ local Event = require("druid.event")
local settings = require("druid.system.settings") local settings = require("druid.system.settings")
local component = require("druid.component") local component = require("druid.component")
local LangText = component.create("lang_text", { component.ON_LANGUAGE_CHANGE }) local LangText = component.create("lang_text")
--- Component init function --- Component init function

View File

@ -34,7 +34,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 Progress = component.create("progress", { component.ON_UPDATE, component.ON_LAYOUT_CHANGE }) local Progress = component.create("progress")
local function check_steps(self, from, to, exactly) local function check_steps(self, from, to, exactly)

View File

@ -40,7 +40,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, component.ON_LAYOUT_CHANGE }, const.PRIORITY_INPUT_HIGH) local Slider = component.create("slider", const.PRIORITY_INPUT_HIGH)
local function on_change_value(self) local function on_change_value(self)

View File

@ -35,7 +35,7 @@ local formats = require("druid.helper.formats")
local helper = require("druid.helper") local helper = require("druid.helper")
local component = require("druid.component") local component = require("druid.component")
local Timer = component.create("timer", { component.ON_UPDATE }) local Timer = component.create("timer")
--- Component init function --- Component init function

View File

@ -87,7 +87,7 @@ end
local function sort_input_stack(self) 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 if not input_components then
return return
end end
@ -107,12 +107,12 @@ local function create(self, instance_class)
local instance = instance_class() local instance = instance_class()
instance:setup_component(self, self._context, self._style) 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() local register_to = instance:__get_interests()
for i = 1, #register_to do for i = 1, #register_to do
local interest = register_to[i] local interest = register_to[i]
table.insert(self.components[interest], instance) table.insert(self.components_interest[interest], instance)
end end
return instance return instance
@ -207,9 +207,10 @@ function DruidInstance.initialize(self, context, style)
self._input_whitelist = nil self._input_whitelist = nil
self._no_auto_input = (sys.get_config("druid.no_auto_input") == "1") 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 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
end end
@ -245,7 +246,7 @@ end
-- on all druid components -- on all druid components
-- @tparam DruidInstance self -- @tparam DruidInstance self
function DruidInstance.final(self) function DruidInstance.final(self)
local components = self.components[base_component.ALL] local components = self.components_all
for i = #components, 1, -1 do for i = #components, 1, -1 do
if components[i].on_remove then if components[i].on_remove then
@ -280,7 +281,7 @@ function DruidInstance.remove(self, component)
end end
component._meta.children = {} component._meta.children = {}
local all_components = self.components[base_component.ALL] local all_components = self.components_all
for i = #all_components, 1, -1 do for i = #all_components, 1, -1 do
if all_components[i] == component then if all_components[i] == component then
if component.on_remove then if component.on_remove then
@ -293,7 +294,7 @@ function DruidInstance.remove(self, component)
local interests = component:__get_interests() local interests = component:__get_interests()
for i = 1, #interests do for i = 1, #interests do
local interest = interests[i] local interest = interests[i]
local components = self.components[interest] local components = self.components_interest[interest]
for j = #components, 1, -1 do for j = #components, 1, -1 do
if components[j] == component then if components[j] == component then
table.remove(components, j) table.remove(components, j)
@ -307,18 +308,18 @@ end
-- @tparam DruidInstance self -- @tparam DruidInstance self
-- @tparam number dt Delta time -- @tparam number dt Delta time
function DruidInstance.update(self, dt) 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 while late_init_components[1] do
late_init_components[1]:on_late_init() late_init_components[1]:on_late_init()
table.remove(late_init_components, 1) table.remove(late_init_components, 1)
end 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 on late init step, to be sure it goes after user go acquire input
input_init(self) input_init(self)
end end
local components = self.components[base_component.ON_UPDATE] local components = self.components_interest[base_component.ON_UPDATE]
for i = 1, #components do for i = 1, #components do
components[i]:update(dt) components[i]:update(dt)
end end
@ -333,7 +334,7 @@ end
function DruidInstance.on_input(self, action_id, action) function DruidInstance.on_input(self, action_id, action)
self._is_input_processing = true 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) check_sort_input_stack(self, components)
local is_input_consumed = process_input(self, action_id, action, 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) function DruidInstance.on_message(self, message_id, message, sender)
-- TODO: refactor for more juicy code -- TODO: refactor for more juicy code
local specific_ui_message = base_component.SPECIFIC_UI_MESSAGES[message_id] 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 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 if components then
for i = 1, #components do for i = 1, #components do
local component = components[i] local component = components[i]
@ -371,7 +372,7 @@ function DruidInstance.on_message(self, message_id, message, sender)
end end
end end
elseif specific_ui_message then elseif specific_ui_message then
local components = self.components[message_id] local components = self.components_interest[specific_ui_message]
if components then if components then
for i = 1, #components do for i = 1, #components do
local component = components[i] local component = components[i]
@ -379,7 +380,7 @@ function DruidInstance.on_message(self, message_id, message, sender)
end end
end end
else else
local components = self.components[base_component.ON_MESSAGE] local components = self.components_interest[base_component.ON_MESSAGE]
for i = 1, #components do for i = 1, #components do
components[i]:on_message(message_id, message, sender) components[i]:on_message(message_id, message, sender)
end end
@ -391,7 +392,7 @@ end
-- This one called by on_window_callback by global window listener -- This one called by on_window_callback by global window listener
-- @tparam DruidInstance self -- @tparam DruidInstance self
function DruidInstance.on_focus_lost(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 for i = 1, #components do
components[i]:on_focus_lost() components[i]:on_focus_lost()
end end
@ -402,7 +403,7 @@ end
-- This one called by on_window_callback by global window listener -- This one called by on_window_callback by global window listener
-- @tparam DruidInstance self -- @tparam DruidInstance self
function DruidInstance.on_focus_gained(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 for i = 1, #components do
components[i]:on_focus_gained() components[i]:on_focus_gained()
end end
@ -413,7 +414,7 @@ end
-- Called on update gui layout -- Called on update gui layout
-- @tparam DruidInstance self -- @tparam DruidInstance self
function DruidInstance.on_layout_change(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 for i = 1, #components do
components[i]:on_layout_change() components[i]:on_layout_change()
end end
@ -426,7 +427,7 @@ end
-- @tparam DruidInstance self -- @tparam DruidInstance self
-- @function druid.on_language_change -- @function druid.on_language_change
function DruidInstance.on_language_change(self) 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 for i = 1, #components do
components[i]:on_language_change() components[i]:on_language_change()
end end