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
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

View File

@ -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

View File

@ -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
@ -31,18 +31,18 @@ function M.get_state(self)
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

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
@ -63,7 +65,9 @@ function M.new(component_script)
register_basic_components = false
end
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
end
@ -78,8 +82,9 @@ end
local function create(self, module)
local instance = setmetatable({}, { __index = module })
instance.parent = self
self[#self + 1] = instance
-- 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

View File

@ -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

View File

@ -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