mirror of
https://github.com/Insality/druid.git
synced 2025-06-27 18:37:44 +02:00
druid (factory) metatable refactoring
This commit is contained in:
commit
b317d390f6
@ -1,11 +0,0 @@
|
|||||||
local M = {}
|
|
||||||
|
|
||||||
--- input handler
|
|
||||||
-- @param action_id - input action id
|
|
||||||
-- @param action - input action
|
|
||||||
function M.on_input(instance, action_id, action)
|
|
||||||
instance.callback(instance.parent.parent)
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
|
146
druid/druid.lua
146
druid/druid.lua
@ -1,12 +1,16 @@
|
|||||||
local data = require("druid.data")
|
local data = require("druid.data")
|
||||||
local druid_input = require("druid.helper.druid_input")
|
local druid_input = require("druid.helper.druid_input")
|
||||||
|
local settings = require("druid.settings")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
local log = settings.log
|
||||||
|
local _factory = {}
|
||||||
|
|
||||||
local STRING = "string"
|
local STRING = "string"
|
||||||
|
|
||||||
--- New druid era, registering components
|
--- New druid era, registering components
|
||||||
local components = {
|
M.comps = {
|
||||||
-- basic
|
-- basic
|
||||||
button = require("druid.base.button"),
|
button = require("druid.base.button"),
|
||||||
android_back = require("druid.base.android_back"),
|
android_back = require("druid.base.android_back"),
|
||||||
@ -15,8 +19,8 @@ local components = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
local function register_basic_components()
|
local register_basic_components = function ()
|
||||||
for k, v in pairs(components) do
|
for k, v in pairs(M.comps) do
|
||||||
M.register(k, v)
|
M.register(k, v)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -24,79 +28,25 @@ end
|
|||||||
|
|
||||||
function M.register(name, module)
|
function M.register(name, module)
|
||||||
-- TODO: Find better solution to creating elements?
|
-- TODO: Find better solution to creating elements?
|
||||||
M["new_" .. name] = function(factory, node_name, ...)
|
_factory["new_" .. name] = function(factory, node_or_name, ...)
|
||||||
M.create(factory, module, node_name, ...)
|
_factory.new(factory, module, node_or_name, ...)
|
||||||
end
|
end
|
||||||
print("[Druid]: register component", name)
|
log("Register component", name)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Called on_message
|
|
||||||
function M.on_message(factory, message_id, message, sender)
|
|
||||||
if message_id == data.LAYOUT_CHANGED then
|
|
||||||
if factory[data.LAYOUT_CHANGED] then
|
|
||||||
M.translate(factory)
|
|
||||||
for i, v in ipairs(factory[data.LAYOUT_CHANGED]) do
|
|
||||||
v:on_layout_updated(message)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
elseif message_id == data.TRANSLATABLE then
|
|
||||||
M.translate(factory)
|
|
||||||
else
|
|
||||||
if factory[data.ON_MESSAGE] then
|
|
||||||
for i, v in ipairs(factory[data.ON_MESSAGE]) do
|
|
||||||
v:on_message(message_id, message, sender)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
--- Called ON_INPUT
|
|
||||||
function M.on_input(factory, action_id, action)
|
|
||||||
if factory[data.ON_SWIPE] then
|
|
||||||
local v, result
|
|
||||||
local len = #factory[data.ON_SWIPE]
|
|
||||||
for i = 1, len do
|
|
||||||
v = factory[data.ON_SWIPE][i]
|
|
||||||
result = result or v:on_input(action_id, action)
|
|
||||||
end
|
|
||||||
if result then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if factory[data.ON_INPUT] then
|
|
||||||
local v
|
|
||||||
local len = #factory[data.ON_INPUT]
|
|
||||||
for i = 1, len do
|
|
||||||
v = factory[data.ON_INPUT][i]
|
|
||||||
if action_id == v.event and v:on_input(action_id, action) then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
--- Called on_update
|
|
||||||
function M.update(factory, dt)
|
|
||||||
if factory[data.ON_UPDATE] then
|
|
||||||
for i, v in ipairs(factory[data.ON_UPDATE]) do
|
|
||||||
v:update(dt)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
--- Create UI instance for ui elements
|
--- Create UI instance for ui elements
|
||||||
-- @return instance with all ui components
|
-- @return instance with all ui components
|
||||||
function M.new(self)
|
function M.new(self)
|
||||||
local factory = setmetatable({}, {__index = M})
|
if register_basic_components then
|
||||||
|
register_basic_components()
|
||||||
|
register_basic_components = false
|
||||||
|
end
|
||||||
|
local factory = setmetatable({}, {__index = _factory})
|
||||||
factory.parent = self
|
factory.parent = self
|
||||||
return factory
|
return factory
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
local function input_init(factory)
|
local function input_init(factory)
|
||||||
if not factory.input_inited then
|
if not factory.input_inited then
|
||||||
@ -105,7 +55,6 @@ local function input_init(factory)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
local function create(module, factory, name, ...)
|
local function create(module, factory, name, ...)
|
||||||
local instance = setmetatable({}, {__index = module})
|
local instance = setmetatable({}, {__index = module})
|
||||||
@ -137,8 +86,8 @@ local function create(module, factory, name, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function M.create(factory, module, name, ...)
|
function _factory.new(factory, module, node_or_name, ...)
|
||||||
local instance = create(module, factory, name)
|
local instance = create(module, factory, node_or_name)
|
||||||
|
|
||||||
if instance.init then
|
if instance.init then
|
||||||
instance:init(...)
|
instance:init(...)
|
||||||
@ -146,5 +95,62 @@ function M.create(factory, module, name, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
register_basic_components()
|
--- Called on_message
|
||||||
|
function _factory.on_message(factory, message_id, message, sender)
|
||||||
|
if message_id == data.LAYOUT_CHANGED then
|
||||||
|
if factory[data.LAYOUT_CHANGED] then
|
||||||
|
M.translate(factory)
|
||||||
|
for i, v in ipairs(factory[data.LAYOUT_CHANGED]) do
|
||||||
|
v:on_layout_updated(message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
elseif message_id == data.TRANSLATABLE then
|
||||||
|
M.translate(factory)
|
||||||
|
else
|
||||||
|
if factory[data.ON_MESSAGE] then
|
||||||
|
for i, v in ipairs(factory[data.ON_MESSAGE]) do
|
||||||
|
v:on_message(message_id, message, sender)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
--- Called ON_INPUT
|
||||||
|
function _factory.on_input(factory, action_id, action)
|
||||||
|
if factory[data.ON_SWIPE] then
|
||||||
|
local v, result
|
||||||
|
local len = #factory[data.ON_SWIPE]
|
||||||
|
for i = 1, len do
|
||||||
|
v = factory[data.ON_SWIPE][i]
|
||||||
|
result = result or v:on_input(action_id, action)
|
||||||
|
end
|
||||||
|
if result then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if factory[data.ON_INPUT] then
|
||||||
|
local v
|
||||||
|
local len = #factory[data.ON_INPUT]
|
||||||
|
for i = 1, len do
|
||||||
|
v = factory[data.ON_INPUT][i]
|
||||||
|
if action_id == v.event and v:on_input(action_id, action) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
--- Called on_update
|
||||||
|
function _factory.update(factory, dt)
|
||||||
|
if factory[data.ON_UPDATE] then
|
||||||
|
for i, v in ipairs(factory[data.ON_UPDATE]) do
|
||||||
|
v:update(dt)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
@ -1,5 +1,6 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
M.is_debug = false
|
||||||
|
|
||||||
M.button = {
|
M.button = {
|
||||||
IS_HOVER = true,
|
IS_HOVER = true,
|
||||||
@ -23,5 +24,12 @@ function M.play_sound(name)
|
|||||||
-- override to play sound with name
|
-- override to play sound with name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.log(...)
|
||||||
|
if M.is_debug then
|
||||||
|
print("[Druid]: ", ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return M
|
return M
|
@ -6,6 +6,13 @@ local lang = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
local function setup_druid(self)
|
local function setup_druid(self)
|
||||||
|
|
||||||
|
-- two different way of exernal component regesstration
|
||||||
|
druid.comps["my_mega_test_comp"] = require "druid.base.text"
|
||||||
|
druid.register("my_custom_component", {})
|
||||||
|
|
||||||
|
druid_settings.is_debug = true
|
||||||
|
|
||||||
druid_settings.play_sound = function(name)
|
druid_settings.play_sound = function(name)
|
||||||
sound.play("sounds#" .. name)
|
sound.play("sounds#" .. name)
|
||||||
end
|
end
|
||||||
@ -19,18 +26,19 @@ function init(self)
|
|||||||
setup_druid(self)
|
setup_druid(self)
|
||||||
self.druid = druid.new(self)
|
self.druid = druid.new(self)
|
||||||
|
|
||||||
|
|
||||||
self.druid:new_button("button_1", function()
|
self.druid:new_button("button_1", function()
|
||||||
print("On button 1")
|
print("On button 1")
|
||||||
end)
|
end)
|
||||||
self.druid:new_button("button_2", function()
|
|
||||||
|
--alternative way of component creation
|
||||||
|
self.druid:new(druid.comps.button, "button_2", function()
|
||||||
print("On button 2")
|
print("On button 2")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
self.druid:new_button("button_3", function()
|
self.druid:new_button("button_3", function()
|
||||||
print("On button 3")
|
print("On button 3")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
self.druid:new_android_back(function(self, params)
|
self.druid:new_android_back(function(self, params)
|
||||||
print("On android back", params)
|
print("On android back", params)
|
||||||
end, 2)
|
end, 2)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user