mirror of
https://github.com/Insality/druid.git
synced 2025-06-27 18:37:44 +02:00
Druid instance refactoring
This commit is contained in:
parent
be8869951a
commit
9ae07b4784
@ -16,6 +16,8 @@ M.STRING = "string"
|
|||||||
M.TABLE = "table"
|
M.TABLE = "table"
|
||||||
M.ZERO = "0"
|
M.ZERO = "0"
|
||||||
|
|
||||||
|
M.ALL = "all"
|
||||||
|
|
||||||
--- Interests
|
--- Interests
|
||||||
M.ON_MESSAGE = hash("on_message")
|
M.ON_MESSAGE = hash("on_message")
|
||||||
M.ON_UPDATE = hash("on_update")
|
M.ON_UPDATE = hash("on_update")
|
||||||
|
@ -86,7 +86,7 @@ end
|
|||||||
function M.set_text_function(callback)
|
function M.set_text_function(callback)
|
||||||
settings.get_text = callback or const.EMPTY_FUNCTION
|
settings.get_text = callback or const.EMPTY_FUNCTION
|
||||||
-- TODO: Update all localized text
|
-- TODO: Update all localized text
|
||||||
-- TOOD: Need to store all current druid instances?
|
-- Need to store all current druid instances to iterate over it?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,7 +26,9 @@ local function create(self, instance_class)
|
|||||||
|
|
||||||
-- Component context, self from component creation
|
-- Component context, self from component creation
|
||||||
instance:setup_component(self._context, self._style)
|
instance:setup_component(self._context, self._style)
|
||||||
table.insert(self.all_components, instance)
|
|
||||||
|
self.components[const.ALL] = self.components[const.ALL] or {}
|
||||||
|
table.insert(self.components[const.ALL], instance)
|
||||||
|
|
||||||
local register_to = instance:get_interests()
|
local register_to = instance:get_interests()
|
||||||
if register_to then
|
if register_to then
|
||||||
@ -77,7 +79,6 @@ end
|
|||||||
function Druid.initialize(self, context, style)
|
function Druid.initialize(self, context, style)
|
||||||
self._context = context
|
self._context = context
|
||||||
self._style = style or settings.default_style
|
self._style = style or settings.default_style
|
||||||
self.all_components = {}
|
|
||||||
self.components = {}
|
self.components = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -97,8 +98,9 @@ end
|
|||||||
--- Remove component from druid instance
|
--- Remove component from druid instance
|
||||||
-- It will call on_remove on component, if exist
|
-- It will call on_remove on component, if exist
|
||||||
function Druid.remove(self, component)
|
function Druid.remove(self, component)
|
||||||
for i = #self.all_components, 1, -1 do
|
local all_components = self.components[const.ALL]
|
||||||
if self.all_components[i] == component then
|
for i = #all_components, 1, -1 do
|
||||||
|
if all_components[i] == component then
|
||||||
if component.on_remove then
|
if component.on_remove then
|
||||||
component:on_remove()
|
component:on_remove()
|
||||||
end
|
end
|
||||||
@ -110,10 +112,10 @@ function Druid.remove(self, component)
|
|||||||
if interests then
|
if interests then
|
||||||
for i = 1, #interests do
|
for i = 1, #interests do
|
||||||
local interest = interests[i]
|
local interest = interests[i]
|
||||||
local array = self.components[interest]
|
local components = self.components[interest]
|
||||||
for j = #array, 1, -1 do
|
for j = #components, 1, -1 do
|
||||||
if array[j] == component then
|
if components[j] == component then
|
||||||
table.remove(array, j)
|
table.remove(components, j)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -124,10 +126,10 @@ end
|
|||||||
--- Druid instance update function
|
--- Druid instance update function
|
||||||
-- @function druid:update(dt)
|
-- @function druid:update(dt)
|
||||||
function Druid.update(self, dt)
|
function Druid.update(self, dt)
|
||||||
local array = self.components[const.ON_UPDATE]
|
local components = self.components[const.ON_UPDATE]
|
||||||
if array then
|
if components then
|
||||||
for i = 1, #array do
|
for i = 1, #components do
|
||||||
array[i]:update(dt)
|
components[i]:update(dt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -138,11 +140,11 @@ end
|
|||||||
function Druid.on_input(self, action_id, action)
|
function Druid.on_input(self, action_id, action)
|
||||||
-- TODO: расписать отличия ON_SWIPE и ON_INPUT
|
-- TODO: расписать отличия ON_SWIPE и ON_INPUT
|
||||||
-- Почему-то некоторые используют ON_SWIPE, а логичнее ON_INPUT? (blocker, slider)
|
-- Почему-то некоторые используют ON_SWIPE, а логичнее ON_INPUT? (blocker, slider)
|
||||||
local array = self.components[const.ON_SWIPE]
|
local components = self.components[const.ON_SWIPE]
|
||||||
if array then
|
if components then
|
||||||
local result
|
local result
|
||||||
for i = #array, 1, -1 do
|
for i = #components, 1, -1 do
|
||||||
local v = array[i]
|
local v = components[i]
|
||||||
result = result or v:on_input(action_id, action)
|
result = result or v:on_input(action_id, action)
|
||||||
end
|
end
|
||||||
if result then
|
if result then
|
||||||
@ -151,10 +153,10 @@ function Druid.on_input(self, action_id, action)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
array = self.components[const.ON_INPUT]
|
components = self.components[const.ON_INPUT]
|
||||||
if array then
|
if components then
|
||||||
for i = #array, 1, -1 do
|
for i = #components, 1, -1 do
|
||||||
local v = array[i]
|
local v = components[i]
|
||||||
if match_event(action_id, v.event) and v:on_input(action_id, action) then
|
if match_event(action_id, v.event) and v:on_input(action_id, action) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@ -171,17 +173,17 @@ end
|
|||||||
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 array = self.components[message_id]
|
local components = self.components[message_id]
|
||||||
if array then
|
if components then
|
||||||
for i = 1, #array do
|
for i = 1, #components do
|
||||||
local item = array[i]
|
local component = components[i]
|
||||||
item[specific_ui_message](item, message, sender)
|
component[specific_ui_message](component, message, sender)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
local array = self.components[const.ON_MESSAGE] or const.EMPTY_TABLE
|
local components = self.components[const.ON_MESSAGE] or const.EMPTY_TABLE
|
||||||
for i = 1, #array do
|
for i = 1, #components do
|
||||||
array[i]:on_message(message_id, message, sender)
|
components[i]:on_message(message_id, message, sender)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user