mirror of
https://github.com/Insality/druid
synced 2025-06-27 18:37:45 +02:00
Move components array from self to self.components and self.components_all
This commit is contained in:
parent
ae47bcee8f
commit
add6b8e301
@ -13,6 +13,7 @@ M.ACTION_BACK = hash("back")
|
|||||||
M.RELEASED = "released"
|
M.RELEASED = "released"
|
||||||
M.PRESSED = "pressed"
|
M.PRESSED = "pressed"
|
||||||
M.STRING = "string"
|
M.STRING = "string"
|
||||||
|
M.TABLE = "table"
|
||||||
M.ZERO = "0"
|
M.ZERO = "0"
|
||||||
|
|
||||||
--- Interests
|
--- Interests
|
||||||
|
@ -70,10 +70,18 @@ function M.new(component_script, style)
|
|||||||
register_basic_components = false
|
register_basic_components = false
|
||||||
end
|
end
|
||||||
local self = setmetatable({}, { __index = druid_instance })
|
local self = setmetatable({}, { __index = druid_instance })
|
||||||
|
|
||||||
-- Druid context here (who created druid)
|
-- Druid context here (who created druid)
|
||||||
-- Usually gui_script, but can be component from self:get_druid()
|
-- Usually gui_script, but can be component from self:get_druid()
|
||||||
self._context = component_script
|
self._context = component_script
|
||||||
self._style = style or default_style
|
self._style = style or default_style
|
||||||
|
|
||||||
|
-- TODO: Find the better way to handle components
|
||||||
|
-- All component list
|
||||||
|
self.all_components = {}
|
||||||
|
-- Map: interest: {components}
|
||||||
|
self.components = {}
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
--- General component class
|
||||||
|
--@class component
|
||||||
|
|
||||||
local const = require("druid.const")
|
local const = require("druid.const")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
@ -14,23 +14,23 @@ end
|
|||||||
|
|
||||||
-- Create the component
|
-- Create the component
|
||||||
local function create(self, module)
|
local function create(self, module)
|
||||||
|
---@class component
|
||||||
local instance = setmetatable({}, { __index = module })
|
local instance = setmetatable({}, { __index = module })
|
||||||
-- 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, instance)
|
table.insert(self.all_components, instance)
|
||||||
|
|
||||||
local register_to = module._component.interest
|
local register_to = module._component.interest
|
||||||
if register_to then
|
if register_to then
|
||||||
local v
|
|
||||||
for i = 1, #register_to do
|
for i = 1, #register_to do
|
||||||
v = register_to[i]
|
local interest = register_to[i]
|
||||||
if not self[v] then
|
if not self.components[interest] then
|
||||||
self[v] = {}
|
self.components[interest] = {}
|
||||||
end
|
end
|
||||||
table.insert(self[v], instance)
|
table.insert(self.components[interest], instance)
|
||||||
|
|
||||||
if const.UI_INPUT[v] then
|
if const.UI_INPUT[interest] then
|
||||||
input_init(self)
|
input_init(self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -52,18 +52,17 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function M.remove(self, instance)
|
function M.remove(self, instance)
|
||||||
for i = #self, 1, -1 do
|
for i = #self.all_components, 1, -1 do
|
||||||
if self[i] == instance then
|
if self.all_components[i] == instance then
|
||||||
table.remove(self, i)
|
table.remove(self, i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local interest = instance._component.interest
|
local interests = instance._component.interest
|
||||||
if interest then
|
if interests then
|
||||||
local v
|
for i = 1, #interests do
|
||||||
for i = 1, #interest do
|
local interest = interests[i]
|
||||||
v = interest[i]
|
local array = self.components[interest]
|
||||||
local array = self[v]
|
|
||||||
for j = #array, 1, -1 do
|
for j = #array, 1, -1 do
|
||||||
if array[j] == instance then
|
if array[j] == instance then
|
||||||
table.remove(array, j)
|
table.remove(array, j)
|
||||||
@ -78,7 +77,7 @@ end
|
|||||||
function M.on_message(self, message_id, message, sender)
|
function M.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[message_id]
|
local array = self.components[message_id]
|
||||||
if array then
|
if array then
|
||||||
local item
|
local item
|
||||||
for i = 1, #array do
|
for i = 1, #array do
|
||||||
@ -87,7 +86,7 @@ function M.on_message(self, message_id, message, sender)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
local array = self[const.ON_MESSAGE]
|
local array = self.components[const.ON_MESSAGE]
|
||||||
if array then
|
if array then
|
||||||
for i = 1, #array do
|
for i = 1, #array do
|
||||||
array[i]:on_message(message_id, message, sender)
|
array[i]:on_message(message_id, message, sender)
|
||||||
@ -98,10 +97,10 @@ end
|
|||||||
|
|
||||||
|
|
||||||
local function notify_input_on_swipe(self)
|
local function notify_input_on_swipe(self)
|
||||||
if self[const.ON_INPUT] then
|
if self.components[const.ON_INPUT] then
|
||||||
local len = #self[const.ON_INPUT]
|
local len = #self.components[const.ON_INPUT]
|
||||||
for i = len, 1, -1 do
|
for i = len, 1, -1 do
|
||||||
local comp = self[const.ON_INPUT][i]
|
local comp = self.components[const.ON_INPUT][i]
|
||||||
if comp.on_swipe then
|
if comp.on_swipe then
|
||||||
comp:on_swipe()
|
comp:on_swipe()
|
||||||
end
|
end
|
||||||
@ -111,7 +110,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
local function match_event(action_id, events)
|
local function match_event(action_id, events)
|
||||||
if type(events) == "table" then
|
if type(events) == const.TABLE then
|
||||||
for i = 1, #events do
|
for i = 1, #events do
|
||||||
if action_id == events[i] then
|
if action_id == events[i] then
|
||||||
return true
|
return true
|
||||||
@ -125,7 +124,7 @@ end
|
|||||||
|
|
||||||
--- Called ON_INPUT
|
--- Called ON_INPUT
|
||||||
function M.on_input(self, action_id, action)
|
function M.on_input(self, action_id, action)
|
||||||
local array = self[const.ON_SWIPE]
|
local array = self.components[const.ON_SWIPE]
|
||||||
if array then
|
if array then
|
||||||
local v, result
|
local v, result
|
||||||
local len = #array
|
local len = #array
|
||||||
@ -139,7 +138,7 @@ function M.on_input(self, action_id, action)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
array = self[const.ON_INPUT]
|
array = self.components[const.ON_INPUT]
|
||||||
if array then
|
if array then
|
||||||
local v
|
local v
|
||||||
local len = #array
|
local len = #array
|
||||||
@ -157,7 +156,7 @@ end
|
|||||||
|
|
||||||
--- Called on_update
|
--- Called on_update
|
||||||
function M.update(self, dt)
|
function M.update(self, dt)
|
||||||
local array = self[const.ON_UPDATE]
|
local array = self.components[const.ON_UPDATE]
|
||||||
if array then
|
if array then
|
||||||
for i = 1, #array do
|
for i = 1, #array do
|
||||||
array[i]:update(dt)
|
array[i]:update(dt)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user