mirror of
https://github.com/Insality/druid
synced 2025-09-27 18:12:21 +02:00
Add component instance with general methods. New component constructor
This commit is contained in:
@@ -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
|
||||
|
@@ -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)
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
||||
|
@@ -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)
|
||||
|
@@ -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)
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
||||
|
@@ -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
|
||||
|
||||
|
@@ -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
|
||||
|
||||
|
@@ -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)
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user