diff --git a/README.md b/README.md index 2efbc43..ff9622a 100644 --- a/README.md +++ b/README.md @@ -100,9 +100,9 @@ end Basic custom component template looks like this: ```lua local const = require("druid.const") +local component = require("druid.system.component") -local M = {} -M.interest = { const.ON_INPUT } +local M = component.new("amazing_component", { const.ON_INPUT }) function M.init(self, ...) -- Component constructor @@ -137,8 +137,9 @@ On each component recomended describe component schema in next way: ```lua -- Component module local helper = require("druid.helper") +local component = require("druid.system.component") -local M = {} +local M = component.new("new_component") local SCHEME = { ROOT = "/root", @@ -146,20 +147,18 @@ local SCHEME = { TITLE = "/title" } --- TODO: Rework self.template/self.nodes --- Make self._inner_data? { component_name, template, nodes } function M.init(self, template_name, node_table) -- If component use template, setup it: - self.template = template_name + self:set_template(template_name) -- If component was cloned with gui.clone_tree, pass his nodes - self.nodes = node_table + self:set_nodes(node_table) -- helper can get node from gui/template/table local root = helper.node(self, SCHEME.ROOT) -- This component can spawn another druid components: - local druid = helper.get_druid(self) + local druid = self:get_druid(self) -- Button self on callback is self of _this_ component local button = druid:new_button(...) diff --git a/druid/druid.lua b/druid/druid.lua index becfb26..c1f582d 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -14,7 +14,7 @@ local log = settings.log --- Basic components -M.comps = { +M.components = { button = require("druid.base.button"), blocker = require("druid.base.blocker"), back_handler = require("druid.base.back_handler"), @@ -34,7 +34,7 @@ M.comps = { local function register_basic_components() - for k, v in pairs(M.comps) do + for k, v in pairs(M.components) do if not druid_instance["new_" .. k] then M.register(k, v) else @@ -60,25 +60,25 @@ end --- Create Druid instance for creating components -- @return instance with all ui components -function M.new(component_script, style) +function M.new(context, style) if register_basic_components then register_basic_components() register_basic_components = false end - local self = setmetatable({}, { __index = druid_instance }) + local druid = setmetatable({}, { __index = druid_instance }) -- Druid context here (who created druid) -- Usually gui_script, but can be component from self:get_druid() - self._context = component_script - self._style = style or settings.default_style + druid._context = context + druid._style = style or settings.default_style -- TODO: Find the better way to handle components -- All component list - self.all_components = {} + druid.all_components = {} -- Map: interest: {components} - self.components = {} + druid.components = {} - return self + return druid end diff --git a/druid/helper.lua b/druid/helper.lua index 66c724f..953c7ac 100644 --- a/druid/helper.lua +++ b/druid/helper.lua @@ -1,7 +1,6 @@ --- Druid helper module -- @module helper - local const = require("druid.const") local M = {} @@ -71,22 +70,11 @@ function M.centrate_icon_with_text(icon_node, text_node, margin) end --- call callback after count calls -function M.after(count, callback) - local closure = function() - count = count - 1 - if count <= 0 then - callback() - end - end - return closure -end - - function M.get_node(node_or_name) if type(node_or_name) == const.STRING then return gui.get_node(node_or_name) end + return node_or_name end @@ -97,9 +85,10 @@ end -- template или таблица нод после gui.clone_tree) function M.node(component, name) local template_name = component._meta.template or const.EMPTY_STRING + local nodes = component._meta.nodes - if component._meta.nodes then - return component._meta.nodes[template_name .. name] + if nodes then + return nodes[template_name .. name] else return gui.get_node(template_name .. name) end @@ -139,6 +128,7 @@ function M.sign(val) if val == 0 then return 0 end + return (val < 0) and -1 or 1 end @@ -156,6 +146,7 @@ function M.is_enabled(node) is_enabled = is_enabled and gui.is_enabled(parent) parent = gui.get_parent(parent) end + return is_enabled end diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index 86cb821..35abac6 100644 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -80,24 +80,13 @@ function M.remove(self, instance) end ---- Called on_message -function M.on_message(self, message_id, message, sender) - local specific_ui_message = const.SPECIFIC_UI_MESSAGES[message_id] - if specific_ui_message then - local array = self.components[message_id] - if array then - local item - for i = 1, #array do - item = array[i] - item[specific_ui_message](item, message, sender) - end - end - else - local array = self.components[const.ON_MESSAGE] - if array then - for i = 1, #array do - array[i]:on_message(message_id, message, sender) - end +--- Druid instance update function +-- @function druid:update(dt) +function M.update(self, dt) + local array = self.components[const.ON_UPDATE] + if array then + for i = 1, #array do + array[i]:update(dt) end end end @@ -129,8 +118,11 @@ local function match_event(action_id, events) end ---- Called ON_INPUT +--- Druid instance on_input function +-- @function druid:on_input(action_id, action) function M.on_input(self, action_id, action) + -- TODO: расписать отличия ON_SWIPE и ON_INPUT + -- Почему-то некоторые используют ON_SWIPE, а логичнее ON_INPUT? (blocker, slider) local array = self.components[const.ON_SWIPE] if array then local v, result @@ -161,12 +153,25 @@ function M.on_input(self, action_id, action) end ---- Called on_update -function M.update(self, dt) - local array = self.components[const.ON_UPDATE] - if array then - for i = 1, #array do - array[i]:update(dt) +--- Druid instance on_message function +-- @function druid:on_message(message_id, message, sender) +function M.on_message(self, message_id, message, sender) + local specific_ui_message = const.SPECIFIC_UI_MESSAGES[message_id] + if specific_ui_message then + local array = self.components[message_id] + if array then + local item + for i = 1, #array do + item = array[i] + item[specific_ui_message](item, message, sender) + end + end + else + local array = self.components[const.ON_MESSAGE] + if array then + for i = 1, #array do + array[i]:on_message(message_id, message, sender) + end end end end