register base components in factory, interest moved to druid.data

This commit is contained in:
Insality 2019-03-27 00:46:58 +03:00
parent c894c16ed9
commit e986b2c3b8
3 changed files with 66 additions and 33 deletions

18
druid/data.lua Normal file
View File

@ -0,0 +1,18 @@
local M = {}
M.A_TOUCH = hash("touch")
M.A_TEXT = hash("text")
M.A_BACKSPACE = hash("backspace")
M.A_ENTER = hash("enter")
M.A_ANDR_BACK = hash("back")
M.LAYOUT_CHANGED = hash("layout_changed")
M.ON_MESSAGE = hash("on_message")
M.ON_INPUT = hash("on_input")
M.ON_SWIPE = hash("on_swipe")
M.ON_UPDATE = hash("on_update")
M.RELEASED = "released"
M.PRESSED = "pressed"
return M

View File

@ -1,37 +1,53 @@
local M = {}
local druid_input = require "druid.help_modules.druid_input"
local druid_input = require("druid.help_modules.druid_input")
local data = require("druid.data")
M.input = druid_input
local LAYOUT_CHANGED = hash("layout_changed")
local ON_MESSAGE = hash("on_message")
local ON_INPUT = hash("on_input")
local ON_SWIPE = hash("on_swipe")
local ON_UPDATE = hash("on_update")
M.TRANSLATABLE = hash("TRANSLATABLE")
local STRING = "string"
--- New druid era, registering components
-- temporary make components outside
local components = {
-- base
button = require("druid.base.button"),
-- text = require("druid.base.text"),
-- android_back = require("druid.base.android_back"),
-- timer = require("druid.base.timer"),
}
local function register_components()
for k, v in pairs(components) do
-- TODO: Find better solution to creating elements?
M["new_" .. k] = function(factory, name, ...)
M.create(factory, v, name, ...)
end
print("[Druid]: register component", k)
end
end
end
register_components()
--- Called on_message
function M.on_message(factory, message_id, message, sender)
if message_id == LAYOUT_CHANGED then
if factory[LAYOUT_CHANGED] then
if message_id == data.LAYOUT_CHANGED then
if factory[data.LAYOUT_CHANGED] then
M.translate(factory)
for i, v in ipairs(factory[LAYOUT_CHANGED]) do
for i, v in ipairs(factory[data.LAYOUT_CHANGED]) do
v:on_layout_updated(message)
end
end
elseif message_id == M.TRANSLATABLE then
M.translate(factory)
else
if factory[ON_MESSAGE] then
for i, v in ipairs(factory[ON_MESSAGE]) do
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
@ -40,22 +56,22 @@ end
--- Called ON_INPUT
function M.on_input(factory, action_id, action)
if factory[ON_SWIPE] then
if factory[data.ON_SWIPE] then
local v, result
local len = #factory[ON_SWIPE]
local len = #factory[data.ON_SWIPE]
for i = 1, len do
v = factory[ON_SWIPE][i]
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[ON_INPUT] then
if factory[data.ON_INPUT] then
local v
local len = #factory[ON_INPUT]
local len = #factory[data.ON_INPUT]
for i = 1, len do
v = factory[ON_INPUT][i]
v = factory[data.ON_INPUT][i]
if action_id == v.event and action[v.action] and v:on_input(action_id, action) then
return true
end
@ -66,10 +82,10 @@ function M.on_input(factory, action_id, action)
end
--- Called on_update
function M.on_update(factory, dt)
if factory[ON_UPDATE] then
for i, v in ipairs(factory[ON_UPDATE]) do
v:on_updated(dt)
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
@ -105,19 +121,27 @@ local function create(meta, factory, name, ...)
end
end
factory[#factory + 1] = instance
local register_to = {...}
local register_to = meta.interest or {}
for i, v in ipairs(register_to) do
if not factory[v] then
factory[v] = {}
end
factory[v][#factory[v] + 1] = instance
if v == data.ON_INPUT then
input_init(factory)
end
end
return instance
end
function M.create(factory, meta, name, ...)
local instance = create(meta, factory, name)
instance.factory = factory
if instance.init then
instance:init(...)
end
end

View File

@ -4,15 +4,6 @@ local ADD_FOCUS = hash("acquire_input_focus")
local REMOVE_FOCUS = hash("release_input_focus")
local PATH_OBJ = "."
M.A_CLICK = hash("click")
M.A_TEXT = hash("text")
M.A_BACKSPACE = hash("backspace")
M.A_ENTER = hash("enter")
M.A_ANDR_BACK = hash("back")
M.RELEASED = "released"
M.PRESSED = "pressed"
function M.focus()
msg.post(PATH_OBJ, ADD_FOCUS)
end