From cb82acd5da864161a5e5a92914f49363a97a60f6 Mon Sep 17 00:00:00 2001 From: Alexey Gulev Date: Wed, 27 Mar 2019 21:30:58 +0100 Subject: [PATCH 1/5] - new log function for debug - clear metatable for the factory --- druid/druid.lua | 228 +++++++++++++++++---------------- druid/settings.lua | 8 ++ example/example.gui.gui_script | 3 +- 3 files changed, 126 insertions(+), 113 deletions(-) diff --git a/druid/druid.lua b/druid/druid.lua index 4d734de..9ba84aa 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -1,150 +1,154 @@ local data = require("druid.data") local druid_input = require("druid.helper.druid_input") +local settings = require("druid.settings") local M = {} +local log = settings.log +local _factory = {} + local STRING = "string" --- New druid era, registering components local components = { - -- basic - button = require("druid.base.button"), - android_back = require("druid.base.android_back"), - text = require("druid.base.text"), - timer = require("druid.base.timer"), + -- basic + button = require("druid.base.button"), + android_back = require("druid.base.android_back"), + text = require("druid.base.text"), + timer = require("druid.base.timer"), } local function register_basic_components() - for k, v in pairs(components) do - M.register(k, v) - end + for k, v in pairs(components) do + M.register(k, v) + end end function M.register(name, module) - -- TODO: Find better solution to creating elements? - M["new_" .. name] = function(factory, node_name, ...) - M.create(factory, module, node_name, ...) - end - print("[Druid]: register component", name) + -- TODO: Find better solution to creating elements? + _factory["new_" .. name] = function(factory, node_name, ...) + M.create(factory, module, node_name, ...) + end + log("Register component", name) 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 -- @return instance with all ui components function M.new(self) - local factory = setmetatable({}, {__index = M}) - factory.parent = self - return factory -end - - -local function input_init(factory) - if not factory.input_inited then - factory.input_inited = true - druid_input.focus() - end + local factory = setmetatable({}, {__index = _factory}) + factory.parent = self + return factory end -------------------------------------------------------------------------------- +local function input_init(factory) + if not factory.input_inited then + factory.input_inited = true + druid_input.focus() + end +end + + local function create(module, factory, name, ...) - local instance = setmetatable({}, {__index = module}) - instance.parent = factory - if name then - if type(name) == STRING then - instance.name = name - instance.node = gui.get_node(name) - else - --name already is node - instance.name = nil - instance.node = name - end - end - factory[#factory + 1] = instance + local instance = setmetatable({}, {__index = module}) + instance.parent = factory + if name then + if type(name) == STRING then + instance.name = name + instance.node = gui.get_node(name) + else + --name already is node + instance.name = nil + instance.node = name + end + end + factory[#factory + 1] = instance - local register_to = module.interest or {} - for i, v in ipairs(register_to) do - if not factory[v] then - factory[v] = {} - end - factory[v][#factory[v] + 1] = instance + local register_to = module.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 + if v == data.ON_INPUT then + input_init(factory) + end + end + return instance end function M.create(factory, module, name, ...) - local instance = create(module, factory, name) + local instance = create(module, factory, name) - if instance.init then - instance:init(...) - end + if instance.init then + instance:init(...) + 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 -register_basic_components() +--- 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 \ No newline at end of file diff --git a/druid/settings.lua b/druid/settings.lua index 25a02bb..0b20797 100644 --- a/druid/settings.lua +++ b/druid/settings.lua @@ -1,5 +1,6 @@ local M = {} +M.is_debug = false M.button = { IS_HOVER = true, @@ -23,5 +24,12 @@ function M.play_sound(name) -- override to play sound with name end +function M.log(...) + if M.is_debug then + print("[Druid]: ", ...) + end +end + + return M \ No newline at end of file diff --git a/example/example.gui.gui_script b/example/example.gui.gui_script index 41cee78..d17b88c 100644 --- a/example/example.gui.gui_script +++ b/example/example.gui.gui_script @@ -6,6 +6,8 @@ local lang = { } local function setup_druid(self) + druid_settings.is_debug = true + druid_settings.play_sound = function(name) sound.play("sounds#" .. name) end @@ -19,7 +21,6 @@ function init(self) setup_druid(self) self.druid = druid.new(self) - self.druid:new_button("button_1", function() print("On button 1") end) From d5c3aae7458fadb9fadc5e336be63e8e06dd6026 Mon Sep 17 00:00:00 2001 From: Alexey Gulev Date: Wed, 27 Mar 2019 22:00:02 +0100 Subject: [PATCH 2/5] add create as _factory method --- druid/components/andr_back_btn.lua | 11 ----------- druid/druid.lua | 16 ++++++++-------- example/example.gui.gui_script | 8 ++++++-- 3 files changed, 14 insertions(+), 21 deletions(-) delete mode 100644 druid/components/andr_back_btn.lua diff --git a/druid/components/andr_back_btn.lua b/druid/components/andr_back_btn.lua deleted file mode 100644 index a237675..0000000 --- a/druid/components/andr_back_btn.lua +++ /dev/null @@ -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 diff --git a/druid/druid.lua b/druid/druid.lua index 9ba84aa..e5c5bf6 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -10,7 +10,7 @@ local _factory = {} local STRING = "string" --- New druid era, registering components -local components = { +M.comps = { -- basic button = require("druid.base.button"), android_back = require("druid.base.android_back"), @@ -20,7 +20,7 @@ local components = { local function register_basic_components() - for k, v in pairs(components) do + for k, v in pairs(M.comps) do M.register(k, v) end end @@ -28,8 +28,8 @@ end function M.register(name, module) -- TODO: Find better solution to creating elements? - _factory["new_" .. name] = function(factory, node_name, ...) - M.create(factory, module, node_name, ...) + _factory["new_" .. name] = function(factory, node_or_name, ...) + _factory.create(factory, module, node_or_name, ...) end log("Register component", name) end @@ -82,16 +82,14 @@ local function create(module, factory, name, ...) end -function M.create(factory, module, name, ...) - local instance = create(module, factory, name) +function _factory.create(factory, module, node_or_name, ...) + local instance = create(module, factory, node_or_name) if instance.init then instance:init(...) end end -register_basic_components() --------------------------------------------------------------------------------- --- Called on_message function _factory.on_message(factory, message_id, message, sender) @@ -151,4 +149,6 @@ function _factory.update(factory, dt) end end +register_basic_components() + return M \ No newline at end of file diff --git a/example/example.gui.gui_script b/example/example.gui.gui_script index d17b88c..4c645fe 100644 --- a/example/example.gui.gui_script +++ b/example/example.gui.gui_script @@ -24,14 +24,18 @@ function init(self) self.druid:new_button("button_1", function() print("On button 1") end) - self.druid:new_button("button_2", function() + + --alternative way of component registration + self.druid:create(druid.comps.button, "button_2", function() print("On button 2") end) + self.druid:new_button("button_3", function() print("On button 3") end) - + druid.register("my_custom_component", {}) + self.druid:new_android_back(function(self, params) print("On android back", params) end, 2) From 832ebe56744e0693bab8ade906d242259a3f1774 Mon Sep 17 00:00:00 2001 From: Alexey Gulev Date: Wed, 27 Mar 2019 22:07:28 +0100 Subject: [PATCH 3/5] New way of component registration --- druid/druid.lua | 8 +++++--- example/example.gui.gui_script | 7 +++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/druid/druid.lua b/druid/druid.lua index e5c5bf6..d91f781 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -19,7 +19,7 @@ M.comps = { } -local function register_basic_components() +local register_basic_components = function () for k, v in pairs(M.comps) do M.register(k, v) end @@ -37,6 +37,10 @@ end --- Create UI instance for ui elements -- @return instance with all ui components function M.new(self) + if register_basic_components then + register_basic_components() + register_basic_components = false + end local factory = setmetatable({}, {__index = _factory}) factory.parent = self return factory @@ -149,6 +153,4 @@ function _factory.update(factory, dt) end end -register_basic_components() - return M \ No newline at end of file diff --git a/example/example.gui.gui_script b/example/example.gui.gui_script index 4c645fe..dc5bd03 100644 --- a/example/example.gui.gui_script +++ b/example/example.gui.gui_script @@ -6,6 +6,11 @@ local lang = { } 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) @@ -33,8 +38,6 @@ function init(self) self.druid:new_button("button_3", function() print("On button 3") end) - - druid.register("my_custom_component", {}) self.druid:new_android_back(function(self, params) print("On android back", params) From 87e6f6ef1fa8754a3764ca5a85dd9c6ce0a7f608 Mon Sep 17 00:00:00 2001 From: Alexey Gulev Date: Wed, 27 Mar 2019 22:13:45 +0100 Subject: [PATCH 4/5] rename create() to new() --- druid/druid.lua | 4 ++-- example/example.gui.gui_script | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/druid/druid.lua b/druid/druid.lua index d91f781..78ce5ae 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -29,7 +29,7 @@ end function M.register(name, module) -- TODO: Find better solution to creating elements? _factory["new_" .. name] = function(factory, node_or_name, ...) - _factory.create(factory, module, node_or_name, ...) + _factory.new(factory, module, node_or_name, ...) end log("Register component", name) end @@ -86,7 +86,7 @@ local function create(module, factory, name, ...) end -function _factory.create(factory, module, node_or_name, ...) +function _factory.new(factory, module, node_or_name, ...) local instance = create(module, factory, node_or_name) if instance.init then diff --git a/example/example.gui.gui_script b/example/example.gui.gui_script index dc5bd03..84ba19e 100644 --- a/example/example.gui.gui_script +++ b/example/example.gui.gui_script @@ -30,8 +30,8 @@ function init(self) print("On button 1") end) - --alternative way of component registration - self.druid:create(druid.comps.button, "button_2", function() + --alternative way of component creation + self.druid:new(druid.comps.button, "button_2", function() print("On button 2") end) From c38354e532be554c6afc8e7cb7f9e67ce7963126 Mon Sep 17 00:00:00 2001 From: Alexey Gulev Date: Thu, 28 Mar 2019 07:55:18 +0100 Subject: [PATCH 5/5] Whitespace fixes --- druid/druid.lua | 2 +- example/example.gui.gui_script | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/druid/druid.lua b/druid/druid.lua index 78ce5ae..769e603 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -37,7 +37,7 @@ end --- Create UI instance for ui elements -- @return instance with all ui components function M.new(self) - if register_basic_components then + if register_basic_components then register_basic_components() register_basic_components = false end diff --git a/example/example.gui.gui_script b/example/example.gui.gui_script index 84ba19e..fbf0574 100644 --- a/example/example.gui.gui_script +++ b/example/example.gui.gui_script @@ -29,16 +29,16 @@ function init(self) self.druid:new_button("button_1", function() print("On button 1") end) - + --alternative way of component creation self.druid:new(druid.comps.button, "button_2", function() print("On button 2") end) - + self.druid:new_button("button_3", function() print("On button 3") end) - + self.druid:new_android_back(function(self, params) print("On android back", params) end, 2)