diff --git a/druid/component.lua b/druid/component.lua index 9b79b88..ba0d834 100644 --- a/druid/component.lua +++ b/druid/component.lua @@ -162,9 +162,11 @@ end -- by `Component.static.create` -- @function component:initialize -- @tparam string name Component name --- @tparam table interest List of component's interest +-- @tparam[opt={}] table interest List of component's interest -- @local function Component.initialize(self, name, interest) + interest = interest or {} + self._component = { name = name, interest = interest @@ -176,7 +178,7 @@ end -- druid component. -- @function Component.create -- @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) -- Yea, inheritance here local new_class = class(name, Component) diff --git a/druid/const.lua b/druid/const.lua index 1db17e2..83ed071 100644 --- a/druid/const.lua +++ b/druid/const.lua @@ -37,6 +37,19 @@ M.ON_LAYOUT_CHANGE = hash("on_layout_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 = { [gui.PIVOT_CENTER] = vmath.vector3(0), [gui.PIVOT_N] = vmath.vector3(0, 0.5, 0), diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index bd02e44..93782d7 100644 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -63,21 +63,15 @@ local function create(self, instance_class) local instance = instance_class() instance:setup_component(self._context, self._style) - self.components[const.ALL] = self.components[const.ALL] or {} table.insert(self.components[const.ALL], instance) local register_to = instance:get_interests() - if register_to then - for i = 1, #register_to do - local interest = register_to[i] - if not self.components[interest] then - self.components[interest] = {} - end - table.insert(self.components[interest], instance) + for i = 1, #register_to do + local interest = register_to[i] + table.insert(self.components[interest], instance) - if const.UI_INPUT[interest] then - input_init(self) - end + if const.UI_INPUT[interest] then + input_init(self) end end @@ -86,7 +80,7 @@ end local function process_input(action_id, action, components, is_input_consumed) - if not components then + if #components == 0 then return is_input_consumed end @@ -130,7 +124,11 @@ function Druid.initialize(self, context, style) self._style = style or settings.default_style self._deleted = false self.url = msg.url() + self.components = {} + for i = 1, #const.ALL_INTERESTS do + self.components[const.ALL_INTERESTS[i]] = {} + end end @@ -182,14 +180,12 @@ function Druid.remove(self, component) end local interests = component:get_interests() - if interests then - for i = 1, #interests do - local interest = interests[i] - local components = self.components[interest] - for j = #components, 1, -1 do - if components[j] == component then - table.remove(components, j) - end + for i = 1, #interests do + local interest = interests[i] + local components = self.components[interest] + for j = #components, 1, -1 do + if components[j] == component then + table.remove(components, j) end end end @@ -201,10 +197,8 @@ end -- @tparam number dt Delta time function Druid.update(self, dt) local components = self.components[const.ON_UPDATE] - if components then - for i = 1, #components do - components[i]:update(dt) - end + for i = 1, #components do + components[i]:update(dt) end end @@ -233,6 +227,7 @@ end -- @tparam hash sender Sender from on_message function Druid.on_message(self, message_id, message, sender) local specific_ui_message = const.SPECIFIC_UI_MESSAGES[message_id] + if specific_ui_message then local components = self.components[message_id] if components then @@ -243,10 +238,8 @@ function Druid.on_message(self, message_id, message, sender) end else local components = self.components[const.ON_MESSAGE] - if components then - for i = 1, #components do - components[i]:on_message(message_id, message, sender) - end + for i = 1, #components do + components[i]:on_message(message_id, message, sender) end end end @@ -257,10 +250,8 @@ end -- @function druid:on_focus_lost function Druid.on_focus_lost(self) local components = self.components[const.ON_FOCUS_LOST] - if components then - for i = 1, #components do - components[i]:on_focus_lost() - end + for i = 1, #components do + components[i]:on_focus_lost() end end @@ -270,10 +261,8 @@ end -- @function druid:on_focus_gained function Druid.on_focus_gained(self) local components = self.components[const.ON_FOCUS_GAINED] - if components then - for i = 1, #components do - components[i]:on_focus_gained() - end + for i = 1, #components do + components[i]:on_focus_gained() end end @@ -283,10 +272,8 @@ end -- @function druid:on_layout_change function Druid.on_layout_change(self) local components = self.components[const.ON_LAYOUT_CHANGE] - if components then - for i = 1, #components do - components[i]:on_layout_change() - end + for i = 1, #components do + components[i]:on_layout_change() end end @@ -297,10 +284,8 @@ end -- @function druid.on_language_change function Druid.on_language_change(self) local components = self.components[const.ON_LANGUAGE_CHANGE] - if components then - for i = 1, #components do - components[i]:on_language_change() - end + for i = 1, #components do + components[i]:on_language_change() end end