Add component instance with general methods. New component constructor

This commit is contained in:
Insality 2020-01-30 01:46:02 +03:00
parent 0542ec4e69
commit ae47bcee8f
22 changed files with 156 additions and 114 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -56,5 +56,6 @@ M.SPECIFIC_UI_MESSAGES = {
M.EMPTY_FUNCTION = function() end
M.EMPTY_STRING = ""
M.EMPTY_TABLE = {}
return M

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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