diff --git a/README.md b/README.md index 7873bb8..2efbc43 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ function M.init(self, template_name, node_table) local button = druid:new_button(...) -- helper can return you the component style - local my_style = helper.get_style(self, "component_name") + local my_style = self:get_style() end ``` diff --git a/druid/base/back_handler.lua b/druid/base/back_handler.lua index 45d0a2e..bd4078e 100644 --- a/druid/base/back_handler.lua +++ b/druid/base/back_handler.lua @@ -2,11 +2,10 @@ -- @module base.back_handler local const = require("druid.const") +local component = require("druid.system.component") + +local M = component.new("back_handler", { const.ON_INPUT }) -local M = {} -M.interest = { - const.ON_INPUT -} --- Component init function -- @function back_handler:init @@ -26,7 +25,7 @@ end -- @tparam table action on_input action function M.on_input(self, action_id, action) if action[const.RELEASED] then - self.callback(self.context, self.params) + self.callback(self:get_context(), self.params) end return true diff --git a/druid/base/blocker.lua b/druid/base/blocker.lua index 14f3fc2..3011f1a 100644 --- a/druid/base/blocker.lua +++ b/druid/base/blocker.lua @@ -3,12 +3,9 @@ local const = require("druid.const") local helper = require("druid.helper") +local component = require("druid.system.component") - -local M = {} -M.interest = { - const.ON_SWIPE -} +local M = component.new("blocker", { const.ON_SWIPE }) function M.init(self, node) diff --git a/druid/base/button.lua b/druid/base/button.lua index 5d6ca1a..67c485e 100644 --- a/druid/base/button.lua +++ b/druid/base/button.lua @@ -7,12 +7,9 @@ local const = require("druid.const") local helper = require("druid.helper") +local component = require("druid.system.component") -local M = {} -M.interest = { - const.ON_INPUT -} - +local M = component.new("button", { const.ON_INPUT }) --- Component init function -- @function button:init @@ -24,7 +21,7 @@ M.interest = { -- @tparam[opt] string event Button react event, const.ACTION_TOUCH by default function M.init(self, node, callback, params, anim_node, event) assert(callback, "Button should have callback. To block input on zone use blocker component") - self.style = helper.get_style(self, "BUTTON") + self.style = self:get_style() self.node = helper.get_node(node) self.event = const.ACTION_TOUCH @@ -55,7 +52,7 @@ local function on_button_release(self) if self.style.on_click then self.style.on_click(self, self.anim_node) end - self.callback(self.context, self.params, self) + self.callback(self:get_context(), self.params, self) else set_hover(self, false) end diff --git a/druid/base/checkbox.lua b/druid/base/checkbox.lua index ffc74df..1a52ad0 100644 --- a/druid/base/checkbox.lua +++ b/druid/base/checkbox.lua @@ -2,8 +2,9 @@ -- @module base.checkbox local helper = require("druid.helper") +local component = require("druid.system.component") -local M = {} +local M = component.new("checkbox") function M.set_state(self, state, is_silence) @@ -17,7 +18,7 @@ function M.set_state(self, state, is_silence) end if not is_silence and self.callback then - self.callback(self.context, state) + self.callback(self:get_context(), state) end end @@ -33,8 +34,8 @@ end function M.init(self, node, callback, click_node) - self.style = helper.get_style(self, "CHECKBOX") - self.druid = helper.get_druid(self) + self.style = self:get_style() + self.druid = self:get_druid() self.node = helper.get_node(node) self.click_node = helper.get_node(click_node) self.callback = callback diff --git a/druid/base/checkbox_group.lua b/druid/base/checkbox_group.lua index 6e92987..03faf8e 100644 --- a/druid/base/checkbox_group.lua +++ b/druid/base/checkbox_group.lua @@ -1,14 +1,14 @@ --- Checkboux group module -- @module base.checkbox_group -local helper = require("druid.helper") +local component = require("druid.system.component") -local M = {} +local M = component.new("checkbox_group") local function on_checkbox_click(self, index) if self.callback then - self.callback(self.context, index) + self.callback(self:get_context(), index) end end @@ -34,7 +34,7 @@ end function M.init(self, nodes, callback, click_nodes) - self.druid = helper.get_druid(self) + self.druid = self:get_druid() self.checkboxes = {} self.callback = callback diff --git a/druid/base/grid.lua b/druid/base/grid.lua index acdfcbd..74c4d66 100644 --- a/druid/base/grid.lua +++ b/druid/base/grid.lua @@ -3,8 +3,9 @@ -- @module base.grid local helper = require("druid.helper") +local component = require("druid.system.component") -local M = {} +local M = component.new("grid") function M.init(self, parent, element, in_row) diff --git a/druid/base/locale.lua b/druid/base/locale.lua index f2abda7..ec73bf9 100644 --- a/druid/base/locale.lua +++ b/druid/base/locale.lua @@ -3,17 +3,14 @@ -- @module base.text local const = require("druid.const") -local settings = require("druid.settings") -local helper = require("druid.helper") +local settings = require("druid.system.settings") +local component = require("druid.system.component") -local M = {} -M.interest = { - const.ON_CHANGE_LANGUAGE, -} +local M = component.new("locale", { const.ON_CHANGE_LANGUAGE }) function M.init(self, node, lang_id, no_adjust) - self.druid = helper.get_druid(self) + self.druid = self:get_druid() self.text = self.druid:new_text(node, lang_id, no_adjust) self:translate(lang_id) diff --git a/druid/base/progress.lua b/druid/base/progress.lua index 3a9019e..564cebb 100644 --- a/druid/base/progress.lua +++ b/druid/base/progress.lua @@ -3,12 +3,9 @@ local const = require("druid.const") local helper = require("druid.helper") +local component = require("druid.system.component") -local M = {} - -M.interest = { - const.ON_UPDATE, -} +local M = component.new("progress", { const.ON_UPDATE }) --- Component init function @@ -23,7 +20,7 @@ function M.init(self, node, key, init_value) self.prop = hash("scale."..key) self.key = key - self.style = helper.get_style(self, "PROGRESS") + self.style = self:get_style() self.node = helper.get_node(node) self.scale = gui.get_scale(self.node) self.size = gui.get_size(self.node) @@ -52,10 +49,10 @@ local function check_steps(self, from, to, exactly) end if v1 < step and step < v2 then - self.step_callback(self.context, step) + self.step_callback(self:get_context(), step) end if exactly and exactly == step then - self.step_callback(self.context, step) + self.step_callback(self:get_context(), step) end end end @@ -139,7 +136,7 @@ function M.to(self, to, callback) self.target_callback = callback else if callback then - callback(self.context, to) + callback(self:get_context(), to) end end end @@ -156,7 +153,7 @@ function M.update(self, dt) check_steps(self, prev_value, self.target, self.target) if self.target_callback then - self.target_callback(self.context, self.target) + self.target_callback(self:get_context(), self.target) end self.target = nil diff --git a/druid/base/radio_group.lua b/druid/base/radio_group.lua index ee22e6a..4802ca8 100644 --- a/druid/base/radio_group.lua +++ b/druid/base/radio_group.lua @@ -1,9 +1,9 @@ --- Radio group module -- @module base.checkbox_group -local helper = require("druid.helper") +local component = require("druid.system.component") -local M = {} +local M = component.new("radio_group") local function on_checkbox_click(self, index) @@ -12,7 +12,7 @@ local function on_checkbox_click(self, index) end if self.callback then - self.callback(self.context, index) + self.callback(self:get_context(), index) end end @@ -37,7 +37,7 @@ end function M.init(self, nodes, callback, click_nodes) - self.druid = helper.get_druid(self) + self.druid = self:get_druid() self.checkboxes = {} self.callback = callback diff --git a/druid/base/scroll.lua b/druid/base/scroll.lua index 15da58c..7716e3e 100644 --- a/druid/base/scroll.lua +++ b/druid/base/scroll.lua @@ -3,13 +3,10 @@ local helper = require("druid.helper") local const = require("druid.const") +local component = require("druid.system.component") -local M = {} +local M = component.new("scroll", { const.ON_UPDATE, const.ON_SWIPE }) -M.interest = { - const.ON_UPDATE, - const.ON_SWIPE, -} -- Global on all scrolls -- TODO: remove it @@ -17,7 +14,7 @@ M.current_scroll = nil function M.init(self, scroll_parent, input_zone, border) - self.style = helper.get_style(self, "SCROLL") + self.style = self:get_style() self.node = helper.get_node(scroll_parent) self.input_zone = helper.get_node(input_zone) @@ -355,7 +352,7 @@ function M.scroll_to_index(self, index, skip_cb) self.selected = index if not skip_cb and self.on_point_callback then - self.on_point_callback(self.context, index, self.points[index]) + self.on_point_callback(self:get_context(), index, self.points[index]) end end diff --git a/druid/base/slider.lua b/druid/base/slider.lua index 2009274..8503501 100644 --- a/druid/base/slider.lua +++ b/druid/base/slider.lua @@ -3,16 +3,14 @@ local helper = require("druid.helper") local const = require("druid.const") +local component = require("druid.system.component") -local M = {} -M.interest = { - const.ON_SWIPE -} +local M = component.new("slider", { const.ON_SWIPE }) local function on_change_value(self) if self.callback then - self.callback(self.context, self.value) + self.callback(self:get_context(), self.value) end end diff --git a/druid/base/text.lua b/druid/base/text.lua index cbeaaff..f7db024 100644 --- a/druid/base/text.lua +++ b/druid/base/text.lua @@ -4,8 +4,9 @@ local const = require("druid.const") local helper = require("druid.helper") +local component = require("druid.system.component") -local M = {} +local M = component.new("text") function M.init(self, node, value, no_adjust) diff --git a/druid/base/timer.lua b/druid/base/timer.lua index 08afe4c..5c26410 100644 --- a/druid/base/timer.lua +++ b/druid/base/timer.lua @@ -4,20 +4,16 @@ local const = require("druid.const") local formats = require("druid.helper.formats") local helper = require("druid.helper") +local component = require("druid.system.component") -local M = {} -M.interest = { - const.ON_UPDATE -} - -local empty = function() end +local M = component.new("timer", { const.ON_UPDATE }) function M.init(self, node, seconds_from, seconds_to, callback) self.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 + callback = callback or const.EMPTY_FUNCTION self:set_to(seconds_from) self:set_interval(seconds_from, seconds_to) @@ -25,7 +21,7 @@ function M.init(self, node, seconds_from, seconds_to, callback) if seconds_to - seconds_from == 0 then self:set_state(false) - self.callback(self.context, self) + self.callback(self:get_context(), self) end return self end @@ -76,7 +72,7 @@ function M.update(self, dt) M.set_to(self, self.value) if self.value == self.target then self:set_state(false) - self.callback(self.context, self) + self.callback(self:get_context(), self) end end end diff --git a/druid/const.lua b/druid/const.lua index a20f5d3..19feaea 100644 --- a/druid/const.lua +++ b/druid/const.lua @@ -56,5 +56,6 @@ M.SPECIFIC_UI_MESSAGES = { M.EMPTY_FUNCTION = function() end M.EMPTY_STRING = "" +M.EMPTY_TABLE = {} return M \ No newline at end of file diff --git a/druid/druid.lua b/druid/druid.lua index 2263588..d8b00a4 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -71,7 +71,7 @@ function M.new(component_script, style) end local self = setmetatable({}, { __index = druid_instance }) -- Druid context here (who created druid) - -- Usually gui_script, but can be component from helper.get_druid(component) + -- Usually gui_script, but can be component from self:get_druid() self._context = component_script self._style = style or default_style return self diff --git a/druid/helper.lua b/druid/helper.lua index e8ef801..66c724f 100644 --- a/druid/helper.lua +++ b/druid/helper.lua @@ -96,10 +96,10 @@ end -- node - может брать ноду у компонента по схеме (если есть -- template или таблица нод после gui.clone_tree) function M.node(component, name) - local template_name = component.template or const.EMPTY_STRING + local template_name = component._meta.template or const.EMPTY_STRING - if component.nodes then - return component.nodes[template_name .. name] + if component._meta.nodes then + return component._meta.nodes[template_name .. name] else return gui.get_node(template_name .. name) end @@ -165,19 +165,4 @@ function M.get_pivot_offset(pivot) end -function M.get_druid(self) - local context = { _context = self } - return setmetatable(context, { __index = self.context.druid }) -end - - -function M.get_style(self, section) - if not self.druid_style then - return {} - end - - return self.druid_style[section] or {} -end - - return M diff --git a/druid/rich/progress_rich.lua b/druid/rich/progress_rich.lua index 277e435..4a6d3e1 100644 --- a/druid/rich/progress_rich.lua +++ b/druid/rich/progress_rich.lua @@ -1,14 +1,14 @@ --- Component for rich progress component -- @module rich.progress_rich -local helper = require("druid.helper") +local component = require("druid.system.component") -local M = {} +local M = component.new("progress_rich") function M.init(self, name, red, green, key) - self.druid = helper.get_druid(self) - self.style = helper.get_style(self, "PROGRESS_RICH") + self.druid = self:get_druid() + self.style = self:get_style() self.red = self.druid:new_progress(red, key) self.green = self.druid:new_progress(green, key) self.fill = self.druid:new_progress(name, key) diff --git a/druid/styles/bounce/style.lua b/druid/styles/bounce/style.lua index 1a0bfa2..e9d93bf 100644 --- a/druid/styles/bounce/style.lua +++ b/druid/styles/bounce/style.lua @@ -4,7 +4,7 @@ local anims = require("druid.styles.bounce.anims") local M = {} -M.BUTTON = { +M["button"] = { HOVER_SCALE = vmath.vector3(-0.025, -0.025, 1), HOVER_TIME = 0.05, SCALE_CHANGE = vmath.vector3(-0.05, - 0.05, 1), @@ -16,34 +16,34 @@ M.BUTTON = { on_hover = function(self, node, state) if self.hover_anim then - local scale_to = self.scale_from + M.BUTTON.HOVER_SCALE + local scale_to = self.scale_from + M.button.HOVER_SCALE local target_scale = state and scale_to or self.scale_from - anims.hover_scale(self, target_scale, M.BUTTON.HOVER_TIME) + anims.hover_scale(self, target_scale, M.button.HOVER_TIME) end end, on_click = function(self, node) - local scale_to = self.scale_from + M.BUTTON.SCALE_CHANGE + local scale_to = self.scale_from + M.button.SCALE_CHANGE anims.tap_scale_animation(self, node, scale_to) - settings.play_sound(M.BUTTON.BTN_SOUND) + settings.play_sound(M.button.BTN_SOUND) end, on_click_disabled = function(self, node) - settings.play_sound(M.BUTTON.BTN_SOUND_DISABLED) + settings.play_sound(M.button.BTN_SOUND_DISABLED) end, on_set_enabled = function(self, node, state) if state then - gui.set_color(node, M.BUTTON.ENABLED_COLOR) + gui.set_color(node, M.button.ENABLED_COLOR) else - gui.set_color(node, M.BUTTON.DISABLED_COLOR) + gui.set_color(node, M.button.DISABLED_COLOR) end end } -M.SCROLL = { +M["scroll"] = { FRICT_HOLD = 0.8, -- mult. for inert, while touching FRICT = 0.93, -- mult for free inert INERT_THRESHOLD = 2, -- speed to stop inertion @@ -55,18 +55,18 @@ M.SCROLL = { } -M.PROGRESS = { +M["progress"] = { SPEED = 5, -- progress bar fill rate, more faster MIN_DELTA = 0.005 } -M.PROGRESS_RICH = { +M["progress_rich"] = { DELAY = 1, -- delay in seconds before main fill } -M.CHECKBOX = { +M["checkbox"] = { on_change_state = function(self, node, state) local target = state and 1 or 0 gui.animate(node, "color.w", target, gui.EASING_OUTSINE, 0.1) diff --git a/druid/styles/empty/style.lua b/druid/styles/empty/style.lua index 10f627d..2ef9698 100644 --- a/druid/styles/empty/style.lua +++ b/druid/styles/empty/style.lua @@ -1,7 +1,7 @@ local M = {} -M.BUTTON = { +M["button"] = { BTN_SOUND = "click", BTN_SOUND_DISABLED = "click", DISABLED_COLOR = vmath.vector4(0, 0, 0, 1), @@ -10,7 +10,7 @@ M.BUTTON = { } -M.SCROLL = { +M["scroll"] = { FRICT_HOLD = 0, -- mult. for inert, while touching FRICT = 0, -- mult for free inert INERT_THRESHOLD = 2, -- speed to stop inertion @@ -22,18 +22,18 @@ M.SCROLL = { } -M.PROGRESS = { +M["progress"] = { SPEED = 5, -- progress bar fill rate, more faster MIN_DELTA = 1 } -M.PROGRESS_RICH = { +M["progress_rich"] = { DELAY = 0, -- delay in seconds before main fill } -M.CHECKBOX = { +M["checkbox"] = { on_change_state = function(self, node, state) gui.set_enabled(node, state) end diff --git a/druid/system/component.lua b/druid/system/component.lua new file mode 100644 index 0000000..57fced6 --- /dev/null +++ b/druid/system/component.lua @@ -0,0 +1,75 @@ +local const = require("druid.const") + +local M = {} +local instance = {} + + +function instance.get_style(self) + if not self._meta.style then + return const.EMPTY_TABLE + end + + return self._meta.style[self._component.name] or const.EMPTY_TABLE +end + + +function instance.set_style(self, component_style) + self._meta.style = component_style +end + + +function instance.set_template(self, template) + self._meta.template = template +end + + +function instance.set_nodes(self, nodes) + self._meta.nodes = nodes +end + + +function instance.get_context(self, context) + return self._meta.context +end + + +function instance.set_context(self, context) + self._meta.context = context +end + + +function instance.get_druid(self) + local context = { _context = self } + return setmetatable(context, { __index = self:get_context().druid }) +end + + +function instance.setup_component(self, context, style) + self._meta = { + template = nil, + context = nil, + nodes = nil, + style = nil, + } + + self:set_context(context) + self:set_style(style) + + return self +end + + +function M.new(name, interest) + local mt = { + _component = { + name = name, + interest = interest + } + } + local component = setmetatable(mt, { __index = instance }) + + return component +end + + +return M \ No newline at end of file diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index 9b3403c..3d1c1d3 100644 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -16,11 +16,11 @@ end local function create(self, module) local instance = setmetatable({}, { __index = module }) -- Component context, self from component creation - instance.context = self._context - instance.druid_style = self._style + instance:setup_component(self._context, self._style) + table.insert(self, instance) - local register_to = module.interest + local register_to = module._component.interest if register_to then local v for i = 1, #register_to do @@ -58,7 +58,7 @@ function M.remove(self, instance) end end - local interest = instance.interest + local interest = instance._component.interest if interest then local v for i = 1, #interest do