#53 Add init all interests arrays, fix final on empty druid instance

This commit is contained in:
Insality 2020-05-07 23:16:37 +03:00
parent 0cc848b53f
commit 94e882577b
3 changed files with 46 additions and 46 deletions

View File

@ -162,9 +162,11 @@ end
-- by `Component.static.create` -- by `Component.static.create`
-- @function component:initialize -- @function component:initialize
-- @tparam string name Component name -- @tparam string name Component name
-- @tparam table interest List of component's interest -- @tparam[opt={}] table interest List of component's interest
-- @local -- @local
function Component.initialize(self, name, interest) function Component.initialize(self, name, interest)
interest = interest or {}
self._component = { self._component = {
name = name, name = name,
interest = interest interest = interest
@ -176,7 +178,7 @@ end
-- druid component. -- druid component.
-- @function Component.create -- @function Component.create
-- @tparam string name Component name -- @tparam string name Component name
-- @tparam table interest List of component's interest -- @tparam[opt={}] table interest List of component's interest
function Component.static.create(name, interest) function Component.static.create(name, interest)
-- Yea, inheritance here -- Yea, inheritance here
local new_class = class(name, Component) local new_class = class(name, Component)

View File

@ -37,6 +37,19 @@ M.ON_LAYOUT_CHANGE = hash("on_layout_change")
M.ON_LANGUAGE_CHANGE = hash("on_language_change") 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 = { 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

@ -63,30 +63,24 @@ local function create(self, instance_class)
local instance = instance_class() local instance = instance_class()
instance:setup_component(self._context, self._style) instance:setup_component(self._context, self._style)
self.components[const.ALL] = self.components[const.ALL] or {}
table.insert(self.components[const.ALL], instance) table.insert(self.components[const.ALL], instance)
local register_to = instance:get_interests() local register_to = instance:get_interests()
if register_to then
for i = 1, #register_to do for i = 1, #register_to do
local interest = register_to[i] local interest = register_to[i]
if not self.components[interest] then
self.components[interest] = {}
end
table.insert(self.components[interest], instance) table.insert(self.components[interest], instance)
if const.UI_INPUT[interest] then if const.UI_INPUT[interest] then
input_init(self) input_init(self)
end end
end end
end
return instance return instance
end end
local function process_input(action_id, action, components, is_input_consumed) local function process_input(action_id, action, components, is_input_consumed)
if not components then if #components == 0 then
return is_input_consumed return is_input_consumed
end end
@ -130,7 +124,11 @@ function Druid.initialize(self, context, style)
self._style = style or settings.default_style self._style = style or settings.default_style
self._deleted = false self._deleted = false
self.url = msg.url() self.url = msg.url()
self.components = {} self.components = {}
for i = 1, #const.ALL_INTERESTS do
self.components[const.ALL_INTERESTS[i]] = {}
end
end end
@ -182,7 +180,6 @@ function Druid.remove(self, component)
end end
local interests = component:get_interests() local interests = component:get_interests()
if interests then
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]
@ -192,7 +189,6 @@ function Druid.remove(self, component)
end end
end end
end end
end
end end
@ -201,11 +197,9 @@ end
-- @tparam number dt Delta time -- @tparam number dt Delta time
function Druid.update(self, dt) function Druid.update(self, dt)
local components = self.components[const.ON_UPDATE] local components = self.components[const.ON_UPDATE]
if components then
for i = 1, #components do for i = 1, #components do
components[i]:update(dt) components[i]:update(dt)
end end
end
end end
@ -233,6 +227,7 @@ end
-- @tparam hash sender Sender from on_message -- @tparam hash sender Sender from on_message
function Druid.on_message(self, message_id, message, sender) function Druid.on_message(self, message_id, message, sender)
local specific_ui_message = const.SPECIFIC_UI_MESSAGES[message_id] local specific_ui_message = const.SPECIFIC_UI_MESSAGES[message_id]
if specific_ui_message then if specific_ui_message then
local components = self.components[message_id] local components = self.components[message_id]
if components then if components then
@ -243,12 +238,10 @@ function Druid.on_message(self, message_id, message, sender)
end end
else else
local components = self.components[const.ON_MESSAGE] local components = self.components[const.ON_MESSAGE]
if components then
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
end end
end
end end
@ -257,11 +250,9 @@ end
-- @function druid:on_focus_lost -- @function druid:on_focus_lost
function Druid.on_focus_lost(self) function Druid.on_focus_lost(self)
local components = self.components[const.ON_FOCUS_LOST] local components = self.components[const.ON_FOCUS_LOST]
if components then
for i = 1, #components do for i = 1, #components do
components[i]:on_focus_lost() components[i]:on_focus_lost()
end end
end
end end
@ -270,11 +261,9 @@ end
-- @function druid:on_focus_gained -- @function druid:on_focus_gained
function Druid.on_focus_gained(self) function Druid.on_focus_gained(self)
local components = self.components[const.ON_FOCUS_GAINED] local components = self.components[const.ON_FOCUS_GAINED]
if components then
for i = 1, #components do for i = 1, #components do
components[i]:on_focus_gained() components[i]:on_focus_gained()
end end
end
end end
@ -283,11 +272,9 @@ end
-- @function druid:on_layout_change -- @function druid:on_layout_change
function Druid.on_layout_change(self) function Druid.on_layout_change(self)
local components = self.components[const.ON_LAYOUT_CHANGE] local components = self.components[const.ON_LAYOUT_CHANGE]
if components then
for i = 1, #components do for i = 1, #components do
components[i]:on_layout_change() components[i]:on_layout_change()
end end
end
end end
@ -297,11 +284,9 @@ end
-- @function druid.on_language_change -- @function druid.on_language_change
function Druid.on_language_change(self) function Druid.on_language_change(self)
local components = self.components[const.ON_LANGUAGE_CHANGE] local components = self.components[const.ON_LANGUAGE_CHANGE]
if components then
for i = 1, #components do for i = 1, #components do
components[i]:on_language_change() components[i]:on_language_change()
end end
end
end end