Add class component and druid_instance

This commit is contained in:
Insality
2020-02-03 22:08:49 +03:00
parent 8c011dcc27
commit be8869951a
20 changed files with 404 additions and 154 deletions

View File

@@ -1,8 +1,9 @@
local const = require("druid.const")
local druid_input = require("druid.helper.druid_input")
local settings = require("druid.system.settings")
local class = require("druid.system.middleclass")
local M = {}
local Druid = class("druid.druid_instance")
local function input_init(self)
@@ -19,16 +20,15 @@ end
-- Create the component
local function create(self, module)
local function create(self, instance_class)
---@class component
local instance = setmetatable({}, { __index = module })
local instance = instance_class()
-- Component context, self from component creation
instance:setup_component(self._context, self._style)
table.insert(self.all_components, instance)
local register_to = module._component.interest
local register_to = instance:get_interests()
if register_to then
for i = 1, #register_to do
local interest = register_to[i]
@@ -47,51 +47,6 @@ local function create(self, module)
end
function M.new(self, module, ...)
local instance = create(self, module)
if instance.init then
instance:init(...)
end
return instance
end
function M.remove(self, instance)
for i = #self.all_components, 1, -1 do
if self.all_components[i] == instance then
table.remove(self, i)
end
end
local interests = instance._component.interest
if interests then
for i = 1, #interests do
local interest = interests[i]
local array = self.components[interest]
for j = #array, 1, -1 do
if array[j] == instance then
table.remove(array, j)
end
end
end
end
end
--- Druid instance update function
-- @function druid:update(dt)
function M.update(self, dt)
local array = self.components[const.ON_UPDATE]
if array then
for i = 1, #array do
array[i]:update(dt)
end
end
end
local function notify_input_on_swipe(self)
if self.components[const.ON_INPUT] then
local len = #self.components[const.ON_INPUT]
@@ -118,17 +73,76 @@ local function match_event(action_id, events)
end
--- Druid constructor
function Druid.initialize(self, context, style)
self._context = context
self._style = style or settings.default_style
self.all_components = {}
self.components = {}
end
--- Create new component inside druid instance
function Druid.create(self, instance_class, ...)
local instance = create(self, instance_class)
if instance.init then
instance:init(...)
end
return instance
end
--- Remove component from druid instance
-- It will call on_remove on component, if exist
function Druid.remove(self, component)
for i = #self.all_components, 1, -1 do
if self.all_components[i] == component then
if component.on_remove then
component:on_remove()
end
table.remove(self, i)
end
end
local interests = component:get_interests()
if interests then
for i = 1, #interests do
local interest = interests[i]
local array = self.components[interest]
for j = #array, 1, -1 do
if array[j] == component then
table.remove(array, j)
end
end
end
end
end
--- Druid instance update function
-- @function druid:update(dt)
function Druid.update(self, dt)
local array = self.components[const.ON_UPDATE]
if array then
for i = 1, #array do
array[i]:update(dt)
end
end
end
--- Druid instance on_input function
-- @function druid:on_input(action_id, action)
function M.on_input(self, action_id, action)
function Druid.on_input(self, action_id, action)
-- TODO: расписать отличия ON_SWIPE и ON_INPUT
-- Почему-то некоторые используют ON_SWIPE, а логичнее ON_INPUT? (blocker, slider)
local array = self.components[const.ON_SWIPE]
if array then
local v, result
local len = #array
for i = len, 1, -1 do
v = array[i]
local result
for i = #array, 1, -1 do
local v = array[i]
result = result or v:on_input(action_id, action)
end
if result then
@@ -139,42 +153,38 @@ function M.on_input(self, action_id, action)
array = self.components[const.ON_INPUT]
if array then
local v
local len = #array
for i = len, 1, -1 do
v = array[i]
for i = #array, 1, -1 do
local v = array[i]
if match_event(action_id, v.event) and v:on_input(action_id, action) then
return true
end
end
return false
end
return false
end
--- Druid instance on_message function
-- @function druid:on_message(message_id, message, sender)
function M.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]
if specific_ui_message then
local array = self.components[message_id]
if array then
local item
for i = 1, #array do
item = array[i]
local item = array[i]
item[specific_ui_message](item, message, sender)
end
end
else
local array = self.components[const.ON_MESSAGE]
if array then
for i = 1, #array do
array[i]:on_message(message_id, message, sender)
end
local array = self.components[const.ON_MESSAGE] or const.EMPTY_TABLE
for i = 1, #array do
array[i]:on_message(message_id, message, sender)
end
end
end
return M
return Druid