From e986b2c3b8c007501b74492b7af0d1cd6ada1936 Mon Sep 17 00:00:00 2001 From: Insality Date: Wed, 27 Mar 2019 00:46:58 +0300 Subject: [PATCH] register base components in factory, interest moved to druid.data --- druid/data.lua | 18 ++++++++ druid/druid.lua | 72 ++++++++++++++++++++---------- druid/help_modules/druid_input.lua | 9 ---- 3 files changed, 66 insertions(+), 33 deletions(-) create mode 100644 druid/data.lua diff --git a/druid/data.lua b/druid/data.lua new file mode 100644 index 0000000..e0d4650 --- /dev/null +++ b/druid/data.lua @@ -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 \ No newline at end of file diff --git a/druid/druid.lua b/druid/druid.lua index ab31684..1419f78 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -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 +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 diff --git a/druid/help_modules/druid_input.lua b/druid/help_modules/druid_input.lua index 21ac43a..bc2665a 100644 --- a/druid/help_modules/druid_input.lua +++ b/druid/help_modules/druid_input.lua @@ -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