Merge pull request #32 from AGulev/feature/druid-component-context

Feature/druid component context
This commit is contained in:
Maxim Tuprikov 2020-01-11 15:27:39 +05:00 committed by GitHub
commit 6cb3b78e3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 51 additions and 31 deletions

View File

@ -26,7 +26,7 @@ end
-- @tparam table action on_input action -- @tparam table action on_input action
function M.on_input(self, action_id, action) function M.on_input(self, action_id, action)
if action[const.RELEASED] then if action[const.RELEASED] then
self.callback(self.parent.parent, self.params) self.callback(self.context, self.params)
end end
return true return true

View File

@ -74,7 +74,7 @@ local function on_button_release(self)
if self.tap_anim then if self.tap_anim then
self.tap_anim(self) self.tap_anim(self)
end end
self.callback(self.parent.parent, self.params, self) self.callback(self.context, self.params, self)
settings.play_sound(self.sound) settings.play_sound(self.sound)
else else
set_hover(self, false) set_hover(self, false)
@ -151,7 +151,7 @@ function M.deactivate(self, is_animate, callback)
-- callback call three times in gui.animation -- callback call three times in gui.animation
local clbk = helper.after(3, function() local clbk = helper.after(3, function()
if callback then if callback then
callback(self.parent.parent) callback(self.context)
end end
end) end)
@ -167,7 +167,7 @@ function M.deactivate(self, is_animate, callback)
gui.set_color(self.node, M.DEFAULT_DEACTIVATE_COLOR) gui.set_color(self.node, M.DEFAULT_DEACTIVATE_COLOR)
gui.set_scale(self.node, M.DEFAULT_DEACTIVATE_SCALE) gui.set_scale(self.node, M.DEFAULT_DEACTIVATE_SCALE)
if callback then if callback then
callback(self.parent.parent) callback(self.context)
end end
end end
end end
@ -179,7 +179,7 @@ function M.activate(self, is_animate, callback)
local clbk = helper.after(3, function() local clbk = helper.after(3, function()
self.disabled = false self.disabled = false
if callback then if callback then
callback(self.parent.parent) callback(self.context)
end end
end) end)
@ -196,7 +196,7 @@ function M.activate(self, is_animate, callback)
gui.set_scale(self.node, M.DEFAULT_ACTIVATE_SCALE) gui.set_scale(self.node, M.DEFAULT_ACTIVATE_SCALE)
self.disabled = false self.disabled = false
if callback then if callback then
callback(self.parent.parent) callback(self.context)
end end
end end
end end

View File

@ -21,7 +21,7 @@ function M.set_state(self, state, is_silence)
state_animate(self.node, state) state_animate(self.node, state)
if not is_silence and self.callback then if not is_silence and self.callback then
self.callback(self.parent.parent, state) self.callback(self.context, state)
end end
end end
@ -31,18 +31,18 @@ function M.get_state(self)
end end
-- TODO: pass self as first parameter local function on_click(self)
local function on_click(context, self)
M.set_state(self, not self.state) M.set_state(self, not self.state)
end end
function M.init(self, node, callback, click_node) function M.init(self, node, callback, click_node)
self.druid = helper.get_druid(self)
self.node = helper.node(node) self.node = helper.node(node)
self.click_node = helper.node(click_node) self.click_node = helper.node(click_node)
self.callback = callback 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) M.set_state(self, false, true)
end end

View File

@ -1,12 +1,14 @@
--- Checkboux group module --- Checkboux group module
-- @module base.checkbox_group -- @module base.checkbox_group
local helper = require("druid.helper")
local M = {} local M = {}
local function on_checkbox_click(self, index) local function on_checkbox_click(self, index)
if self.callback then if self.callback then
self.callback(self.parent.parent, index) self.callback(self.context, index)
end end
end end
@ -32,12 +34,13 @@ end
function M.init(self, nodes, callback, click_nodes) function M.init(self, nodes, callback, click_nodes)
self.druid = helper.get_druid(self)
self.checkboxes = {} self.checkboxes = {}
self.callback = callback self.callback = callback
for i = 1, #nodes do for i = 1, #nodes do
local click_node = click_nodes and click_nodes[i] or nil 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) on_checkbox_click(self, i)
end, click_node) end, click_node)

View File

@ -56,10 +56,10 @@ local function check_steps(self, from, to, exactly)
end end
if v1 < step and step < v2 then if v1 < step and step < v2 then
self.step_callback(self.parent.parent, step) self.step_callback(self.context, step)
end end
if exactly and exactly == step then if exactly and exactly == step then
self.step_callback(self.parent.parent, step) self.step_callback(self.context, step)
end end
end end
end end
@ -143,7 +143,7 @@ function M.to(self, to, callback)
self.target_callback = callback self.target_callback = callback
else else
if callback then if callback then
callback(self.parent.parent, to) callback(self.context, to)
end end
end end
end end
@ -160,7 +160,7 @@ function M.update(self, dt)
check_steps(self, prev_value, self.target, self.target) check_steps(self, prev_value, self.target, self.target)
if self.target_callback then if self.target_callback then
self.target_callback(self.parent.parent, self.target) self.target_callback(self.context, self.target)
end end
self.target = nil self.target = nil

View File

@ -1,6 +1,8 @@
--- Radio group module --- Radio group module
-- @module base.checkbox_group -- @module base.checkbox_group
local helper = require("druid.helper")
local M = {} local M = {}
@ -10,7 +12,7 @@ local function on_checkbox_click(self, index)
end end
if self.callback then if self.callback then
self.callback(self.parent.parent, index) self.callback(self.context, index)
end end
end end
@ -35,12 +37,13 @@ end
function M.init(self, nodes, callback, click_nodes) function M.init(self, nodes, callback, click_nodes)
self.druid = helper.get_druid(self)
self.checkboxes = {} self.checkboxes = {}
self.callback = callback self.callback = callback
for i = 1, #nodes do for i = 1, #nodes do
local click_node = click_nodes and click_nodes[i] or nil 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) on_checkbox_click(self, i)
end, click_node) end, click_node)

View File

@ -352,7 +352,7 @@ function M.scroll_to_index(self, index, skip_cb)
self.selected = index self.selected = index
if not skip_cb and self.on_point_callback then 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
end end

View File

@ -12,7 +12,7 @@ M.interest = {
local function on_change_value(self) local function on_change_value(self)
if self.callback then if self.callback then
self.callback(self.parent.parent, self.value) self.callback(self.context, self.value)
end end
end end

View File

@ -25,7 +25,7 @@ function M.init(self, node, seconds_from, seconds_to, callback)
if seconds_to - seconds_from == 0 then if seconds_to - seconds_from == 0 then
self:set_state(false) self:set_state(false)
self.callback(self.parent.parent, self) self.callback(self.context, self)
end end
return self return self
end end
@ -76,7 +76,7 @@ function M.update(self, dt)
M.set_to(self, self.value) M.set_to(self, self.value)
if self.value == self.target then if self.value == self.target then
self:set_state(false) self:set_state(false)
self.callback(self.parent.parent, self) self.callback(self.context, self)
end end
end end
end end

View File

@ -48,6 +48,8 @@ end
-- @tparam table module lua table with module -- @tparam table module lua table with module
function M.register(name, module) function M.register(name, module)
-- TODO: Find better solution to creating elements? -- TODO: Find better solution to creating elements?
-- Possibly: druid.new(druid.BUTTON, etc?)
-- Current way is very implicit
_fct_metatable["new_" .. name] = function(self, ...) _fct_metatable["new_" .. name] = function(self, ...)
return _fct_metatable.new(self, module, ...) return _fct_metatable.new(self, module, ...)
end end
@ -63,7 +65,9 @@ function M.new(component_script)
register_basic_components = false register_basic_components = false
end end
local self = setmetatable({}, { __index = _fct_metatable }) local self = setmetatable({}, { __index = _fct_metatable })
self.parent = component_script -- Druid context here (who created druid)
-- Usually gui_script, but can be component from helper.get_druid(component)
self._context = component_script
return self return self
end end
@ -78,8 +82,9 @@ end
local function create(self, module) local function create(self, module)
local instance = setmetatable({}, { __index = module }) local instance = setmetatable({}, { __index = module })
instance.parent = self -- Component context, self from component creation
self[#self + 1] = instance instance.context = self._context
table.insert(self, instance)
local register_to = module.interest local register_to = module.interest
if register_to then if register_to then
@ -89,13 +94,14 @@ local function create(self, module)
if not self[v] then if not self[v] then
self[v] = {} self[v] = {}
end end
self[v][#self[v] + 1] = instance table.insert(self[v], instance)
if const.UI_INPUT[v] then if const.UI_INPUT[v] then
input_init(self) input_init(self)
end end
end end
end end
return instance return instance
end end

View File

@ -148,4 +148,10 @@ function M.get_pivot_offset(pivot)
end end
function M.get_druid(self)
local context = { _context = self }
return setmetatable(context, { __index = self.context.druid })
end
return M return M

View File

@ -1,6 +1,7 @@
--- Component for rich progress component --- Component for rich progress component
-- @module rich.progress_rich -- @module rich.progress_rich
local helper = require("druid.helper")
local settings = require("druid.settings") local settings = require("druid.settings")
local pr_settings = settings.progress_rich local pr_settings = settings.progress_rich
@ -8,9 +9,10 @@ local M = {}
function M.init(self, name, red, green, key) function M.init(self, name, red, green, key)
self.red = self.parent:new_progress(red, key) self.druid = helper.get_druid(self)
self.green = self.parent:new_progress(green, key) self.red = self.druid:new_progress(red, key)
self.fill = self.parent:new_progress(name, key) self.green = self.druid:new_progress(green, key)
self.fill = self.druid:new_progress(name, key)
end end