diff --git a/druid/base/button.lua b/druid/base/button.lua index df0ea53..38a6fa2 100644 --- a/druid/base/button.lua +++ b/druid/base/button.lua @@ -1,6 +1,7 @@ local data = require("druid.data") -local ui_animate = require "druid.helper.druid_animate" +local ui_animate = require("druid.helper.druid_animate") local settings = require("druid.settings") +local helper = require("druid.helper.helper") local b_settings = settings.button local M = {} @@ -14,7 +15,8 @@ M.DEFAULT_ACTIVATE_SCALE = vmath.vector3(1, 1, 1) M.DEFAUL_ACTIVATION_TIME = 0.2 -function M.init(instance, callback, params, anim_node) +function M.init(instance, node, callback, params, anim_node, event) + instance.node = helper.get_node(node) instance.event = data.A_TOUCH instance.anim_node = anim_node and gui.get_node(anim_node) or instance.node instance.scale_from = gui.get_scale(instance.anim_node) diff --git a/druid/base/text.lua b/druid/base/text.lua index 466daf1..13ee774 100644 --- a/druid/base/text.lua +++ b/druid/base/text.lua @@ -1,5 +1,6 @@ local data = require("druid.data") local settings = require("druid.settings") +local helper = require("druid.helper.helper") local M = {} M.interest = { @@ -8,7 +9,8 @@ M.interest = { } -function M.init(instance, value, is_locale) +function M.init(instance, node, value, is_locale) + instance.node = helper.get_node(node) instance.last_color = gui.get_color(instance.node) if is_locale then instance.text_id = value diff --git a/druid/base/timer.lua b/druid/base/timer.lua index 8439778..38deb1c 100644 --- a/druid/base/timer.lua +++ b/druid/base/timer.lua @@ -1,6 +1,6 @@ local data = require("druid.data") local formats = require("druid.helper.formats") -local helper = require("druid.helper.ui_helper") +local helper = require("druid.helper.helper") local M = {} M.interest = { @@ -10,7 +10,8 @@ M.interest = { local empty = function() end -function M.init(instance, seconds_from, seconds_to, callback) +function M.init(instance, node, seconds_from, seconds_to, callback) + instance.node = helper.get_node(node) seconds_from = math.max(seconds_from, 0) seconds_to = math.max(seconds_to or 0, 0) callback = callback or empty diff --git a/druid/druid.lua b/druid/druid.lua index 7d6e8cc..1dbd7a8 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -7,8 +7,6 @@ local M = {} local log = settings.log local _factory = {} -local STRING = "string" - M.comps = { button = require("druid.base.button"), android_back = require("druid.base.android_back"), @@ -26,8 +24,8 @@ end function M.register(name, module) -- TODO: Find better solution to creating elements? - _factory["new_" .. name] = function(factory, node_or_name, ...) - return _factory.new(factory, module, node_or_name, ...) + _factory["new_" .. name] = function(factory, ...) + return _factory.new(factory, module, ...) end log("Register component", name) end @@ -54,19 +52,9 @@ local function input_init(factory) end -local function create(module, factory, name) +local function create(module, factory) 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 {} @@ -84,8 +72,8 @@ local function create(module, factory, name) end -function _factory.new(factory, module, node_or_name, ...) - local instance = create(module, factory, node_or_name) +function _factory.new(factory, module, ...) + local instance = create(module, factory) if instance.init then instance:init(...) diff --git a/druid/helper/ui_helper.lua b/druid/helper/helper.lua similarity index 82% rename from druid/helper/ui_helper.lua rename to druid/helper/helper.lua index 976a63a..fe150da 100644 --- a/druid/helper/ui_helper.lua +++ b/druid/helper/helper.lua @@ -18,6 +18,15 @@ function M.centrate_text_with_icon(text_node, icon_node, offset_x) end +local STRING = "string" +function M.get_node(node_or_name) + if type(node_or_name) == STRING then + return gui.get_node(node_or_name) + end + return node_or_name +end + + function M.step(current, target, step) if current < target then return math.min(current + step, target) diff --git a/example/example.gui.gui_script b/example/example.gui.gui_script index fbf0574..b92ea02 100644 --- a/example/example.gui.gui_script +++ b/example/example.gui.gui_script @@ -46,7 +46,7 @@ function init(self) self.druid:new_text("text_2", "Simple text") self.druid:new_text("text_3", "locale_text", true) - self.druid:new_timer("text_1", 0.5, 0, function() + self.druid:new_timer("text_1", 5, 0, function() print("On timer end") end)