From b8dec4826f2b5edadcaea73f6eb3793a90c0f338 Mon Sep 17 00:00:00 2001 From: Insality Date: Thu, 5 Dec 2019 22:39:35 +0300 Subject: [PATCH 1/2] Add druid with context, get component druid as helper.get_druid(context) --- druid/base/back_handler.lua | 2 +- druid/base/button.lua | 10 +++++----- druid/base/checkbox.lua | 7 ++++--- druid/base/checkbox_group.lua | 7 +++++-- druid/base/progress.lua | 8 ++++---- druid/base/radio_group.lua | 7 +++++-- druid/base/scroll.lua | 2 +- druid/base/slider.lua | 2 +- druid/base/timer.lua | 4 ++-- druid/druid.lua | 18 ++++++++++++------ druid/helper.lua | 6 ++++++ druid/rich/progress_rich.lua | 8 +++++--- 12 files changed, 51 insertions(+), 30 deletions(-) diff --git a/druid/base/back_handler.lua b/druid/base/back_handler.lua index dffe1ed..45d0a2e 100644 --- a/druid/base/back_handler.lua +++ b/druid/base/back_handler.lua @@ -26,7 +26,7 @@ end -- @tparam table action on_input action function M.on_input(self, action_id, action) if action[const.RELEASED] then - self.callback(self.parent.parent, self.params) + self.callback(self.context, self.params) end return true diff --git a/druid/base/button.lua b/druid/base/button.lua index dd80aec..8a11e86 100644 --- a/druid/base/button.lua +++ b/druid/base/button.lua @@ -74,7 +74,7 @@ local function on_button_release(self) if self.tap_anim then self.tap_anim(self) end - self.callback(self.parent.parent, self.params, self) + self.callback(self.context, self.params, self) settings.play_sound(self.sound) else set_hover(self, false) @@ -151,7 +151,7 @@ function M.deactivate(self, is_animate, callback) -- callback call three times in gui.animation local clbk = helper.after(3, function() if callback then - callback(self.parent.parent) + callback(self.context) end end) @@ -167,7 +167,7 @@ function M.deactivate(self, is_animate, callback) gui.set_color(self.node, M.DEFAULT_DEACTIVATE_COLOR) gui.set_scale(self.node, M.DEFAULT_DEACTIVATE_SCALE) if callback then - callback(self.parent.parent) + callback(self.context) end end end @@ -179,7 +179,7 @@ function M.activate(self, is_animate, callback) local clbk = helper.after(3, function() self.disabled = false if callback then - callback(self.parent.parent) + callback(self.context) end end) @@ -196,7 +196,7 @@ function M.activate(self, is_animate, callback) gui.set_scale(self.node, M.DEFAULT_ACTIVATE_SCALE) self.disabled = false if callback then - callback(self.parent.parent) + callback(self.context) end end end diff --git a/druid/base/checkbox.lua b/druid/base/checkbox.lua index d7290d2..6c3075d 100644 --- a/druid/base/checkbox.lua +++ b/druid/base/checkbox.lua @@ -21,7 +21,7 @@ function M.set_state(self, state, is_silence) state_animate(self.node, state) if not is_silence and self.callback then - self.callback(self.parent.parent, state) + self.callback(self.context, state) end end @@ -32,17 +32,18 @@ end -- TODO: pass self as first parameter -local function on_click(context, self) +local function on_click(self) M.set_state(self, not self.state) end function M.init(self, node, callback, click_node) + self.druid = helper.get_druid(self) self.node = helper.node(node) self.click_node = helper.node(click_node) self.callback = callback - self.button = self.parent:new_button(self.click_node or self.node, on_click, self) + self.button = self.druid:new_button(self.click_node or self.node, on_click) M.set_state(self, false, true) end diff --git a/druid/base/checkbox_group.lua b/druid/base/checkbox_group.lua index 3d8de6c..6e92987 100644 --- a/druid/base/checkbox_group.lua +++ b/druid/base/checkbox_group.lua @@ -1,12 +1,14 @@ --- Checkboux group module -- @module base.checkbox_group +local helper = require("druid.helper") + local M = {} local function on_checkbox_click(self, index) if self.callback then - self.callback(self.parent.parent, index) + self.callback(self.context, index) end end @@ -32,12 +34,13 @@ end function M.init(self, nodes, callback, click_nodes) + self.druid = helper.get_druid(self) self.checkboxes = {} self.callback = callback for i = 1, #nodes do local click_node = click_nodes and click_nodes[i] or nil - local checkbox = self.parent:new_checkbox(nodes[i], function() + local checkbox = self.druid:new_checkbox(nodes[i], function() on_checkbox_click(self, i) end, click_node) diff --git a/druid/base/progress.lua b/druid/base/progress.lua index 5f631b7..450f2a4 100644 --- a/druid/base/progress.lua +++ b/druid/base/progress.lua @@ -56,10 +56,10 @@ local function check_steps(self, from, to, exactly) end if v1 < step and step < v2 then - self.step_callback(self.parent.parent, step) + self.step_callback(self.context, step) end if exactly and exactly == step then - self.step_callback(self.parent.parent, step) + self.step_callback(self.context, step) end end end @@ -143,7 +143,7 @@ function M.to(self, to, callback) self.target_callback = callback else if callback then - callback(self.parent.parent, to) + callback(self.context, to) end end end @@ -160,7 +160,7 @@ function M.update(self, dt) check_steps(self, prev_value, self.target, self.target) if self.target_callback then - self.target_callback(self.parent.parent, self.target) + self.target_callback(self.context, self.target) end self.target = nil diff --git a/druid/base/radio_group.lua b/druid/base/radio_group.lua index 25e74b6..ee22e6a 100644 --- a/druid/base/radio_group.lua +++ b/druid/base/radio_group.lua @@ -1,6 +1,8 @@ --- Radio group module -- @module base.checkbox_group +local helper = require("druid.helper") + local M = {} @@ -10,7 +12,7 @@ local function on_checkbox_click(self, index) end if self.callback then - self.callback(self.parent.parent, index) + self.callback(self.context, index) end end @@ -35,12 +37,13 @@ end function M.init(self, nodes, callback, click_nodes) + self.druid = helper.get_druid(self) self.checkboxes = {} self.callback = callback for i = 1, #nodes do local click_node = click_nodes and click_nodes[i] or nil - local checkbox = self.parent:new_checkbox(nodes[i], function() + local checkbox = self.druid:new_checkbox(nodes[i], function() on_checkbox_click(self, i) end, click_node) diff --git a/druid/base/scroll.lua b/druid/base/scroll.lua index 2fbd8bc..e3e79ee 100644 --- a/druid/base/scroll.lua +++ b/druid/base/scroll.lua @@ -352,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.parent.parent, index, self.points[index]) + self.on_point_callback(self.context, index, self.points[index]) end end diff --git a/druid/base/slider.lua b/druid/base/slider.lua index 29f25c0..98b5846 100644 --- a/druid/base/slider.lua +++ b/druid/base/slider.lua @@ -12,7 +12,7 @@ M.interest = { local function on_change_value(self) if self.callback then - self.callback(self.parent.parent, self.value) + self.callback(self.context, self.value) end end diff --git a/druid/base/timer.lua b/druid/base/timer.lua index af22d1f..c531205 100644 --- a/druid/base/timer.lua +++ b/druid/base/timer.lua @@ -25,7 +25,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.parent.parent, self) + self.callback(self.context, self) end return self end @@ -76,7 +76,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.parent.parent, self) + self.callback(self.context, self) end end end diff --git a/druid/druid.lua b/druid/druid.lua index d0170f7..ce533c3 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -48,6 +48,8 @@ end -- @tparam table module lua table with module function M.register(name, module) -- TODO: Find better solution to creating elements? + -- Possibly: druid.new(druid.BUTTON, etc?) + -- Current way is very implicit _fct_metatable["new_" .. name] = function(self, ...) return _fct_metatable.new(self, module, ...) end @@ -62,8 +64,10 @@ function M.new(component_script) register_basic_components() register_basic_components = false end - local self = setmetatable({}, {__index = _fct_metatable}) - self.parent = component_script + local self = setmetatable({}, { __index = _fct_metatable }) + -- Druid context here (who created druid) + -- Usually gui_script, but can be component from helper.get_druid(component) + self._context = component_script return self end @@ -77,9 +81,10 @@ end local function create(self, module) - local instance = setmetatable({}, {__index = module}) - instance.parent = self - self[#self + 1] = instance + local instance = setmetatable({}, { __index = module }) + -- Component context, self from component creation + instance.context = self._context + table.insert(self, instance) local register_to = module.interest if register_to then @@ -89,13 +94,14 @@ local function create(self, module) if not self[v] then self[v] = {} end - self[v][#self[v] + 1] = instance + table.insert(self[v], instance) if const.UI_INPUT[v] then input_init(self) end end end + return instance end diff --git a/druid/helper.lua b/druid/helper.lua index c7c7a36..1a3a893 100644 --- a/druid/helper.lua +++ b/druid/helper.lua @@ -148,4 +148,10 @@ function M.get_pivot_offset(pivot) end +function M.get_druid(self) + local context = { _context = self } + return setmetatable(context, { __index = self.context.druid }) +end + + return M diff --git a/druid/rich/progress_rich.lua b/druid/rich/progress_rich.lua index 87c9b10..793c9d5 100644 --- a/druid/rich/progress_rich.lua +++ b/druid/rich/progress_rich.lua @@ -1,6 +1,7 @@ --- Component for rich progress component -- @module rich.progress_rich +local helper = require("druid.helper") local settings = require("druid.settings") local pr_settings = settings.progress_rich @@ -8,9 +9,10 @@ local M = {} function M.init(self, name, red, green, key) - self.red = self.parent:new_progress(red, key) - self.green = self.parent:new_progress(green, key) - self.fill = self.parent:new_progress(name, key) + self.druid = helper.get_druid(self) + self.red = self.druid:new_progress(red, key) + self.green = self.druid:new_progress(green, key) + self.fill = self.druid:new_progress(name, key) end From 89933dfaf5fccedf15164051c17d00ececadab13 Mon Sep 17 00:00:00 2001 From: Insality Date: Thu, 5 Dec 2019 22:39:46 +0300 Subject: [PATCH 2/2] Close TODO on self context --- druid/base/checkbox.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/druid/base/checkbox.lua b/druid/base/checkbox.lua index 6c3075d..7725082 100644 --- a/druid/base/checkbox.lua +++ b/druid/base/checkbox.lua @@ -31,7 +31,6 @@ function M.get_state(self) end --- TODO: pass self as first parameter local function on_click(self) M.set_state(self, not self.state) end