mirror of
https://github.com/Insality/druid.git
synced 2025-06-27 18:37:44 +02:00
Feature/component register (#1)
* remove components from main factory * register base components in factory, interest moved to druid.data * add simple usage of button * translatable should be in data * ability to extend components, stubs for sounds and locale
This commit is contained in:
parent
e80ba7106b
commit
1123d09e5d
11
druid/base/android_back.lua
Normal file
11
druid/base/android_back.lua
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
local M = {}
|
||||||
|
|
||||||
|
--- input handler
|
||||||
|
-- @param action_id - input action id
|
||||||
|
-- @param action - input action
|
||||||
|
function M.on_input(instance, action_id, action)
|
||||||
|
instance.callback(instance.parent.parent)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
139
druid/base/button.lua
Normal file
139
druid/base/button.lua
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
local data = require("druid.data")
|
||||||
|
local ui_animate = require "druid.help_modules.druid_animate"
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
M.interest = {
|
||||||
|
data.ON_INPUT
|
||||||
|
}
|
||||||
|
|
||||||
|
M.DEFAULT_SCALE_CHANGE = vmath.vector3(-0.05, - 0.1, 1)
|
||||||
|
M.DEFAULT_POS_CHANGE = vmath.vector3(0, - 10, 0)
|
||||||
|
M.DEFAULT_MOVE_SPEED = 5
|
||||||
|
M.DEFAULT_ALPHA_DOWN = 0.8
|
||||||
|
M.DEFAULT_TIME_ANIM = 0.1
|
||||||
|
M.DEFAULT_DEACTIVATE_COLOR = vmath.vector4(0, 0, 0, 0)
|
||||||
|
M.DEFAULT_DEACTIVATE_SCALE = vmath.vector3(0.8, 0.9, 1)
|
||||||
|
M.DEFAULT_ACTIVATE_SCALE = vmath.vector3(1, 1, 1)
|
||||||
|
M.DEFAUL_ACTIVATION_TIME = 0.2
|
||||||
|
|
||||||
|
|
||||||
|
function M.init(instance, callback, event, action, animate_node_name, sound)
|
||||||
|
instance.event = event or data.A_TOUCH
|
||||||
|
instance.action = action or data.RELEASED
|
||||||
|
instance.anim_node = animate_node_name and gui.get_node(animate_node_name) or instance.node
|
||||||
|
instance.scale_from = gui.get_scale(instance.anim_node)
|
||||||
|
instance.scale_to = instance.scale_from + M.DEFAULT_SCALE_CHANGE
|
||||||
|
instance.pos = gui.get_position(instance.anim_node)
|
||||||
|
instance.callback = callback
|
||||||
|
-- instance.params = params
|
||||||
|
instance.tap_anim = M.tap_scale_animation
|
||||||
|
instance.back_anim = M.back_scale_animation
|
||||||
|
-- instance.sound = sound or M.BTN_SOUND_FUNC
|
||||||
|
-- instance.sound_disable = sound_disable or M.BTN_SOUND_DISABLE_FUNC
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Set text to text field
|
||||||
|
-- @param action_id - input action id
|
||||||
|
-- @param action - input action
|
||||||
|
function M.on_input(instance, action_id, action)
|
||||||
|
if gui.is_enabled(instance.node) and gui.pick_node(instance.node, action.x, action.y) then
|
||||||
|
if not instance.disabled then
|
||||||
|
instance.tap_anim(instance)
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
-- instance.sound_disable()
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.tap_scale_animation(instance)
|
||||||
|
ui_animate.scale_to(instance, instance.anim_node, instance.scale_to,
|
||||||
|
function()
|
||||||
|
if instance.back_anim then
|
||||||
|
instance.back_anim(instance)
|
||||||
|
end
|
||||||
|
-- instance.sound()
|
||||||
|
instance.callback(instance.parent.parent, instance.params, instance)
|
||||||
|
end
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.back_scale_animation(instance)
|
||||||
|
ui_animate.scale_to(instance, instance.anim_node, instance.scale_from)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.tap_tab_animation(instance, force)
|
||||||
|
ui_animate.alpha(instance, instance.anim_node, M.DEFAULT_ALPHA_DOWN, nil, M.DEFAULT_TIME_ANIM)
|
||||||
|
ui_animate.fly_to(instance, instance.anim_node, instance.pos + M.DEFAULT_POS_CHANGE, M.DEFAULT_MOVE_SPEED)
|
||||||
|
ui_animate.scale_to(instance, instance.anim_node, instance.scale_to,
|
||||||
|
function()
|
||||||
|
if instance.back_anim then
|
||||||
|
instance.back_anim(instance)
|
||||||
|
end
|
||||||
|
instance.callback(instance.parent.parent, instance.params, force)
|
||||||
|
end
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.back_tab_animation(instance)
|
||||||
|
ui_animate.alpha(instance, instance.anim_node, 1, nil, M.DEFAULT_TIME_ANIM)
|
||||||
|
ui_animate.fly_to(instance, instance.anim_node, instance.pos, M.DEFAULT_MOVE_SPEED)
|
||||||
|
ui_animate.scale_to(instance, instance.anim_node, instance.scale_from)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.deactivate(instance, is_animate, callback)
|
||||||
|
instance.disabled = true
|
||||||
|
if is_animate then
|
||||||
|
local counter = 0
|
||||||
|
local clbk = function()
|
||||||
|
counter = counter + 1
|
||||||
|
if counter == 3 and callback then
|
||||||
|
callback(instance.parent.parent)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
ui_animate.color(instance, instance.node, M.DEFAULT_DEACTIVATE_COLOR, clbk, M.DEFAUL_ACTIVATION_TIME, 0,
|
||||||
|
gui.EASING_OUTBOUNCE)
|
||||||
|
ui_animate.scale_y_from_to(instance, instance.node, M.DEFAULT_ACTIVATE_SCALE.x, M.DEFAULT_DEACTIVATE_SCALE.x, clbk,
|
||||||
|
M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE)
|
||||||
|
ui_animate.scale_x_from_to(instance, instance.node, M.DEFAULT_ACTIVATE_SCALE.y, M.DEFAULT_DEACTIVATE_SCALE.y, clbk,
|
||||||
|
M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE)
|
||||||
|
else
|
||||||
|
gui.set_color(instance.node, M.DEFAULT_DEACTIVATE_COLOR)
|
||||||
|
gui.set_scale(instance.node, M.DEFAULT_DEACTIVATE_SCALE)
|
||||||
|
if callback then
|
||||||
|
callback(instance.parent.parent)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.activate(instance, is_animate, callback)
|
||||||
|
if is_animate then
|
||||||
|
local counter = 0
|
||||||
|
local clbk = function()
|
||||||
|
counter = counter + 1
|
||||||
|
if counter == 3 then
|
||||||
|
instance.disabled = false
|
||||||
|
if callback then
|
||||||
|
callback(instance.parent.parent)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
ui_animate.color(instance, instance.node, ui_animate.TINT_SHOW, clbk, M.DEFAUL_ACTIVATION_TIME, 0,
|
||||||
|
gui.EASING_OUTBOUNCE)
|
||||||
|
ui_animate.scale_y_from_to(instance, instance.node, M.DEFAULT_DEACTIVATE_SCALE.x, M.DEFAULT_ACTIVATE_SCALE.x, clbk,
|
||||||
|
M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE)
|
||||||
|
ui_animate.scale_x_from_to(instance, instance.node, M.DEFAULT_DEACTIVATE_SCALE.y, M.DEFAULT_ACTIVATE_SCALE.y, clbk,
|
||||||
|
M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE)
|
||||||
|
else
|
||||||
|
gui.set_color(instance.node, ui_animate.TINT_SHOW)
|
||||||
|
gui.set_scale(instance.node, M.DEFAULT_ACTIVATE_SCALE)
|
||||||
|
instance.disabled = false
|
||||||
|
if callback then
|
||||||
|
callback(instance.parent.parent)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
42
druid/base/text.lua
Normal file
42
druid/base/text.lua
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
local M = {}
|
||||||
|
|
||||||
|
local ui_animate = require "druid.help_modules.druid_animate"
|
||||||
|
|
||||||
|
--- Bounce text field
|
||||||
|
function M.bounce(instance, callback)
|
||||||
|
gui.set_scale(instance.node, instance.scale_from)
|
||||||
|
ui_animate.bounce(nil, instance.node, instance.scale_to, callback)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Set text to text field
|
||||||
|
-- @param set_to - set value to text field
|
||||||
|
function M.set_to(instance, set_to)
|
||||||
|
instance.last_value = set_to
|
||||||
|
gui.set_text(instance.node, set_to)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Set color
|
||||||
|
-- @param color
|
||||||
|
function M.set_color(instance, color)
|
||||||
|
instance.last_color = color
|
||||||
|
gui.set_color(instance.node, color)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Set scale
|
||||||
|
-- @param scale
|
||||||
|
function M.set_scale(instance, scale)
|
||||||
|
instance.last_scale = scale
|
||||||
|
gui.set_scale(instance.node, scale)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Called when layout updated (rotate for example)
|
||||||
|
function M.on_layout_updated(instance)
|
||||||
|
if instance.last_color then
|
||||||
|
M.set_color(instance, instance.last_color)
|
||||||
|
end
|
||||||
|
if instance.last_scale then
|
||||||
|
M.set_scale(instance, instance.last_scale)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
53
druid/base/timer.lua
Normal file
53
druid/base/timer.lua
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
local M = {}
|
||||||
|
|
||||||
|
local formats = require "druid.help_modules.formats"
|
||||||
|
|
||||||
|
--- Set text to text field
|
||||||
|
-- @param set_to - set value in seconds
|
||||||
|
function M.set_to(instance, set_to)
|
||||||
|
instance.last_value = set_to
|
||||||
|
gui.set_text(instance.node, formats.second_string_min(set_to))
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Called when layout updated (rotate for example)
|
||||||
|
function M.on_layout_updated(instance)
|
||||||
|
M.set_to(instance, instance.last_value)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Called when update
|
||||||
|
-- @param is_on - boolean is timer on
|
||||||
|
function M.set_work_mode(instance, is_on)
|
||||||
|
instance.is_on = is_on
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Set time interval
|
||||||
|
-- @param from - "from" time in seconds
|
||||||
|
-- @param to - "to" time in seconds
|
||||||
|
function M.set_interval(instance, from, to)
|
||||||
|
instance.second_from = from
|
||||||
|
instance.seconds_counter = from
|
||||||
|
instance.seconds_temp = 0
|
||||||
|
instance.seconds_to = to
|
||||||
|
instance.second_step = from < to and 1 or - 1
|
||||||
|
M.set_work_mode(instance, true)
|
||||||
|
M.set_to(instance, from)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Called when update
|
||||||
|
-- @param dt - delta time
|
||||||
|
function M.on_updated(instance, dt)
|
||||||
|
if instance.is_on then
|
||||||
|
instance.seconds_temp = instance.seconds_temp + dt
|
||||||
|
if instance.seconds_temp > 1 then
|
||||||
|
instance.seconds_temp = instance.seconds_temp - 1
|
||||||
|
instance.seconds_counter = instance.seconds_counter + instance.second_step
|
||||||
|
M.set_to(instance, instance.seconds_counter)
|
||||||
|
if instance.seconds_counter == instance.seconds_to then
|
||||||
|
instance.is_on = false
|
||||||
|
instance.callback(instance)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
20
druid/data.lua
Normal file
20
druid/data.lua
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
local M = {}
|
||||||
|
|
||||||
|
M.A_TOUCH = hash("touch")
|
||||||
|
M.A_TEXT = hash("text")
|
||||||
|
M.A_BACKSPACE = hash("backspace")
|
||||||
|
M.A_ENTER = hash("enter")
|
||||||
|
M.A_ANDR_BACK = hash("back")
|
||||||
|
|
||||||
|
-- interest
|
||||||
|
M.LAYOUT_CHANGED = hash("layout_changed")
|
||||||
|
M.ON_MESSAGE = hash("on_message")
|
||||||
|
M.ON_INPUT = hash("on_input")
|
||||||
|
M.ON_SWIPE = hash("on_swipe")
|
||||||
|
M.ON_UPDATE = hash("on_update")
|
||||||
|
M.TRANSLATABLE = hash("TRANSLATABLE")
|
||||||
|
|
||||||
|
M.RELEASED = "released"
|
||||||
|
M.PRESSED = "pressed"
|
||||||
|
|
||||||
|
return M
|
396
druid/druid.lua
396
druid/druid.lua
@ -1,92 +1,75 @@
|
|||||||
|
local data = require("druid.data")
|
||||||
|
local druid_input = require("druid.help_modules.druid_input")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local druid_input = require "druid.help_modules.druid_input"
|
|
||||||
M.input = druid_input
|
|
||||||
|
|
||||||
local pie_progress_bar = require "druid.components.pie_progress_bar"
|
|
||||||
local progress_bar = require "druid.components.progress_bar"
|
|
||||||
local flying_particles = require "druid.components.flying_particles"
|
|
||||||
local text_field = require "druid.components.text_field"
|
|
||||||
local counter = require "druid.components.counter"
|
|
||||||
local image = require "druid.components.image"
|
|
||||||
local button = require "druid.components.button"
|
|
||||||
local timer = require "druid.components.timer"
|
|
||||||
local tab_page = require "druid.components.tab_page"
|
|
||||||
local tabs_container = require "druid.components.tabs_container"
|
|
||||||
local spine_anim = require "druid.components.spine_anim"
|
|
||||||
local scrolling_box = require "druid.components.scrolling_box"
|
|
||||||
|
|
||||||
local andr_back_btn = require "druid.components.andr_back_btn"
|
|
||||||
|
|
||||||
local LAYOUT_CHANGED = hash("layout_changed")
|
|
||||||
local ON_MESSAGE = hash("on_message")
|
|
||||||
local ON_INPUT = hash("on_input")
|
|
||||||
local ON_SWIPE = hash("on_swipe")
|
|
||||||
local ON_UPDATE = hash("on_update")
|
|
||||||
M.TRANSLATABLE = hash("TRANSLATABLE")
|
|
||||||
|
|
||||||
local STRING = "string"
|
local STRING = "string"
|
||||||
|
|
||||||
--- Call this method when you need to update translations.
|
--- New druid era, registering components
|
||||||
function M.translate(factory)
|
local components = {
|
||||||
if factory[M.TRANSLATABLE] then
|
-- basic
|
||||||
local key, result
|
button = require("druid.base.button"),
|
||||||
for i, v in ipairs(factory[M.TRANSLATABLE]) do
|
-- text = require("druid.base.text"),
|
||||||
key = v.lang_key or v.name
|
-- android_back = require("druid.base.android_back"),
|
||||||
if key then
|
-- timer = require("druid.base.timer"),
|
||||||
if v.lang_params then
|
}
|
||||||
result = lang.txp(key, v.lang_params)
|
|
||||||
else
|
|
||||||
result = lang.txt(key)
|
local function register_basic_components()
|
||||||
end
|
for k, v in pairs(components) do
|
||||||
if result then
|
M.register(k, v)
|
||||||
lang.set_node_properties(v.node, key)
|
|
||||||
end
|
|
||||||
result = result or v.last_value
|
|
||||||
v:set_to(result)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
register_basic_components()
|
||||||
|
|
||||||
|
|
||||||
|
function M.register(name, module)
|
||||||
|
-- TODO: Find better solution to creating elements?
|
||||||
|
M["new_" .. name] = function(factory, node_name, ...)
|
||||||
|
M.create(factory, module, node_name, ...)
|
||||||
|
end
|
||||||
|
print("[Druid]: register component", name)
|
||||||
|
end
|
||||||
|
|
||||||
--- Called on_message
|
--- Called on_message
|
||||||
function M.on_message(factory, message_id, message, sender)
|
function M.on_message(factory, message_id, message, sender)
|
||||||
if message_id == LAYOUT_CHANGED then
|
if message_id == data.LAYOUT_CHANGED then
|
||||||
if factory[LAYOUT_CHANGED] then
|
if factory[data.LAYOUT_CHANGED] then
|
||||||
M.translate(factory)
|
M.translate(factory)
|
||||||
for i, v in ipairs(factory[LAYOUT_CHANGED]) do
|
for i, v in ipairs(factory[data.LAYOUT_CHANGED]) do
|
||||||
v:on_layout_updated(message)
|
v:on_layout_updated(message)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif message_id == M.TRANSLATABLE then
|
elseif message_id == data.TRANSLATABLE then
|
||||||
M.translate(factory)
|
M.translate(factory)
|
||||||
else
|
else
|
||||||
if factory[ON_MESSAGE] then
|
if factory[data.ON_MESSAGE] then
|
||||||
for i, v in ipairs(factory[ON_MESSAGE]) do
|
for i, v in ipairs(factory[data.ON_MESSAGE]) do
|
||||||
v:on_message(message_id, message, sender)
|
v:on_message(message_id, message, sender)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Called ON_INPUT
|
--- Called ON_INPUT
|
||||||
function M.on_input(factory, action_id, action)
|
function M.on_input(factory, action_id, action)
|
||||||
if factory[ON_SWIPE] then
|
if factory[data.ON_SWIPE] then
|
||||||
local v, result
|
local v, result
|
||||||
local len = #factory[ON_SWIPE]
|
local len = #factory[data.ON_SWIPE]
|
||||||
for i = 1, len do
|
for i = 1, len do
|
||||||
v = factory[ON_SWIPE][i]
|
v = factory[data.ON_SWIPE][i]
|
||||||
result = result or v:on_input(action_id, action)
|
result = result or v:on_input(action_id, action)
|
||||||
end
|
end
|
||||||
if result then
|
if result then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if factory[ON_INPUT] then
|
if factory[data.ON_INPUT] then
|
||||||
local v
|
local v
|
||||||
local len = #factory[ON_INPUT]
|
local len = #factory[data.ON_INPUT]
|
||||||
for i = 1, len do
|
for i = 1, len do
|
||||||
v = factory[ON_INPUT][i]
|
v = factory[data.ON_INPUT][i]
|
||||||
if action_id == v.event and action[v.action] and v:on_input(action_id, action) then
|
if action_id == v.event and action[v.action] and v:on_input(action_id, action) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@ -96,15 +79,17 @@ function M.on_input(factory, action_id, action)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Called on_update
|
--- Called on_update
|
||||||
function M.on_update(factory, dt)
|
function M.update(factory, dt)
|
||||||
if factory[ON_UPDATE] then
|
if factory[data.ON_UPDATE] then
|
||||||
for i, v in ipairs(factory[ON_UPDATE]) do
|
for i, v in ipairs(factory[data.ON_UPDATE]) do
|
||||||
v:on_updated(dt)
|
v:update(dt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Create UI instance for ui elements
|
--- Create UI instance for ui elements
|
||||||
-- @return instance with all ui components
|
-- @return instance with all ui components
|
||||||
function M.new(self)
|
function M.new(self)
|
||||||
@ -113,6 +98,7 @@ function M.new(self)
|
|||||||
return factory
|
return factory
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function input_init(factory)
|
local function input_init(factory)
|
||||||
if not factory.input_inited then
|
if not factory.input_inited then
|
||||||
factory.input_inited = true
|
factory.input_inited = true
|
||||||
@ -122,8 +108,8 @@ end
|
|||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
local function create(meta, factory, name, ...)
|
local function create(module, factory, name, ...)
|
||||||
local instance = setmetatable({}, {__index = meta})
|
local instance = setmetatable({}, {__index = module})
|
||||||
instance.parent = factory
|
instance.parent = factory
|
||||||
if name then
|
if name then
|
||||||
if type(name) == STRING then
|
if type(name) == STRING then
|
||||||
@ -136,285 +122,39 @@ local function create(meta, factory, name, ...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
factory[#factory + 1] = instance
|
factory[#factory + 1] = instance
|
||||||
local register_to = {...}
|
|
||||||
|
local register_to = module.interest or {}
|
||||||
for i, v in ipairs(register_to) do
|
for i, v in ipairs(register_to) do
|
||||||
if not factory[v] then
|
if not factory[v] then
|
||||||
factory[v] = {}
|
factory[v] = {}
|
||||||
end
|
end
|
||||||
factory[v][#factory[v] + 1] = instance
|
factory[v][#factory[v] + 1] = instance
|
||||||
end
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Create new instance of a text_field
|
if v == data.ON_INPUT then
|
||||||
-- @param factory - parent factory
|
input_init(factory)
|
||||||
-- @param name - name of text node
|
|
||||||
-- @param init_value - init ui object with this value
|
|
||||||
-- @return instance of a text_field
|
|
||||||
function M.new_text_field(factory, name, init_value, bounce_in)
|
|
||||||
local instance = create(text_field, factory, name, M.TRANSLATABLE, LAYOUT_CHANGED)
|
|
||||||
instance.scale_from = gui.get_scale(instance.node)
|
|
||||||
instance.scale_to = bounce_in and vmath.mul_per_elem(instance.scale_from, bounce_in) or instance.scale_from
|
|
||||||
instance:set_to(init_value or 0)
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Create new instance of a counter
|
|
||||||
-- @param factory - parent factory
|
|
||||||
-- @param name - name of text node
|
|
||||||
-- @param init_value - init ui object with this value
|
|
||||||
-- @return instance of a text_field
|
|
||||||
function M.new_counter(factory, name, init_value)
|
|
||||||
local instance = create(counter, factory, name, LAYOUT_CHANGED, ON_UPDATE)
|
|
||||||
instance.scale_from = gui.get_scale(instance.node)
|
|
||||||
instance.scale_to = instance.scale_from * 1.2
|
|
||||||
instance:set_to(init_value or 0)
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Create new instance of an image
|
|
||||||
-- @param factory - parent factory
|
|
||||||
-- @param name - name of image node
|
|
||||||
-- @param anim_table - table with animations or frames
|
|
||||||
-- @param init_frame - init with this frame
|
|
||||||
-- @return instance of an image
|
|
||||||
function M.new_image(factory, name, anim_table, init_frame, bounce_in)
|
|
||||||
local instance = create(image, factory, name, LAYOUT_CHANGED)
|
|
||||||
instance.scale_from = gui.get_scale(instance.node)
|
|
||||||
instance.scale_to = bounce_in and vmath.mul_per_elem(instance.scale_from, bounce_in) or instance.scale_from
|
|
||||||
instance.anim_table = anim_table
|
|
||||||
if init_frame then
|
|
||||||
instance:set_to(init_frame)
|
|
||||||
elseif anim_table then
|
|
||||||
instance:set_to(1)
|
|
||||||
end
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Create new instance of a timer
|
|
||||||
-- @param factory - parent factory
|
|
||||||
-- @param name - name of image node
|
|
||||||
-- @param second_from - start time
|
|
||||||
-- @param seconds_to - end time
|
|
||||||
-- @param callback - call when timer finished
|
|
||||||
-- @return instance of a timer
|
|
||||||
function M.new_timer(factory, name, second_from, seconds_to, callback)
|
|
||||||
local instance = create(timer, factory, name, LAYOUT_CHANGED, ON_UPDATE)
|
|
||||||
instance:set_to(second_from)
|
|
||||||
instance:set_interval(second_from, seconds_to)
|
|
||||||
instance.is_on = true
|
|
||||||
instance.callback = callback
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Add new pie progress component for handling
|
|
||||||
-- @param factory - parent factory
|
|
||||||
-- @param name - a node name for a pie progress instance
|
|
||||||
-- @param init_value - init ui object with this value
|
|
||||||
-- @return instance with pie_progress
|
|
||||||
function M.new_pie_progress(factory, name, init_value)
|
|
||||||
local instance = create(pie_progress_bar, factory, name, LAYOUT_CHANGED)
|
|
||||||
instance:set_to(init_value or 1)
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Add new progress bar component for handling
|
|
||||||
-- @param factory - parent factory
|
|
||||||
-- @param name - name of the fill node
|
|
||||||
-- @param key - x or y - key for scale
|
|
||||||
-- @param init_value - init ui object with this value
|
|
||||||
-- @return instance with pie_progress
|
|
||||||
function M.new_progress_bar(factory, name, key, init_value)
|
|
||||||
local instance = create(progress_bar, factory, name, LAYOUT_CHANGED)
|
|
||||||
instance.prop = hash("scale."..key)
|
|
||||||
instance.key = key
|
|
||||||
instance.node = gui.get_node(name)
|
|
||||||
instance.scale = gui.get_scale(instance.node)
|
|
||||||
instance:set_to(init_value or 1)
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Create new instance of a flying particles
|
|
||||||
-- @param factory - parent factory
|
|
||||||
-- @param name - name of prototype
|
|
||||||
-- @param count - how many particles need to cache
|
|
||||||
-- @param get_pos_func - function that returns target pos for flying
|
|
||||||
-- @return instance of a flying particles
|
|
||||||
function M.new_flying_particles(factory, name, count, get_pos_func)
|
|
||||||
local instance = create(flying_particles, factory, name, LAYOUT_CHANGED)
|
|
||||||
instance.get_pos_func = get_pos_func
|
|
||||||
local node = instance.node
|
|
||||||
instance.node = node
|
|
||||||
instance.fly_particles = {}
|
|
||||||
instance.fly_particles[1] = node
|
|
||||||
for i = 2, count do
|
|
||||||
instance.fly_particles[i] = gui.clone(node)
|
|
||||||
end
|
|
||||||
instance.scale = gui.get_scale(node)
|
|
||||||
instance.last_particle = 0
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
M.BTN_SOUND_FUNC = function() end
|
|
||||||
M.BTN_SOUND_DISABLE_FUNC = function()end
|
|
||||||
|
|
||||||
--- Add new button component for handling
|
|
||||||
-- @param factory - parent factory
|
|
||||||
-- @param name - a node name for a button instance
|
|
||||||
-- @param callback - click button callback
|
|
||||||
-- @param params - callback parameters, will be returned with self callback(self, params)
|
|
||||||
-- @param animate_node_name - node for animation, if it's not a main node
|
|
||||||
-- @return instance of button
|
|
||||||
function M.new_button(factory, name, callback, params, animate_node_name, event, action, sound, sound_disable)
|
|
||||||
input_init(factory)
|
|
||||||
local instance = create(button, factory, name, ON_INPUT)
|
|
||||||
instance.event = event or druid_input.A_CLICK
|
|
||||||
instance.action = action or druid_input.RELEASED
|
|
||||||
instance.anim_node = animate_node_name and gui.get_node(animate_node_name) or instance.node
|
|
||||||
instance.scale_from = gui.get_scale(instance.anim_node)
|
|
||||||
instance.scale_to = instance.scale_from + button.DEFAULT_SCALE_CHANGE
|
|
||||||
instance.pos = gui.get_position(instance.anim_node)
|
|
||||||
instance.callback = callback
|
|
||||||
instance.params = params
|
|
||||||
instance.tap_anim = button.tap_scale_animation
|
|
||||||
instance.back_anim = button.back_scale_animation
|
|
||||||
instance.sound = sound or M.BTN_SOUND_FUNC
|
|
||||||
instance.sound_disable = sound_disable or M.BTN_SOUND_DISABLE_FUNC
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Add reaction for back btn (on Android for example)
|
|
||||||
-- @param factory - parent factory
|
|
||||||
-- @param callback - tap button callback
|
|
||||||
function M.new_back_handler(factory, callback)
|
|
||||||
input_init(factory)
|
|
||||||
local instance = create(andr_back_btn, factory, nil, ON_INPUT)
|
|
||||||
instance.event = druid_input.A_ANDR_BACK
|
|
||||||
instance.action = druid_input.RELEASED
|
|
||||||
instance.callback = callback
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Create new tab page instance
|
|
||||||
-- @param factory - parent factory
|
|
||||||
-- @param name - name of parental node that represents tab page content
|
|
||||||
-- @param easing - easing for tab page
|
|
||||||
-- @param duration - duration of animation for tab page
|
|
||||||
-- @param callback - call when change page
|
|
||||||
-- @return instance that represents the tab page
|
|
||||||
function M.new_tab_page(factory, name, easing, duration, callback)
|
|
||||||
local instance = create(tab_page, factory, name, M.EVENTS.ON_MESSAGE)
|
|
||||||
instance.in_pos = gui.get_position(instance.node)
|
|
||||||
instance.out_pos = gui.get_position(instance.node)
|
|
||||||
instance.easing = easing or tab_page.DEFAULT_EASING
|
|
||||||
instance.duration = duration or tab_page.DEFAULT_DURATION
|
|
||||||
instance.callback = callback
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Create new tab btns container instance
|
|
||||||
-- @param factory - parent factory
|
|
||||||
-- @param name - name of parental node that represents tab btns container
|
|
||||||
-- @return instance that represents the tab btns container
|
|
||||||
function M.new_tabs_container(factory, name, callback)
|
|
||||||
local instance = create(tabs_container, factory, name, LAYOUT_CHANGED)
|
|
||||||
instance:update_sizes()
|
|
||||||
instance.url = msg.url()
|
|
||||||
--- Create new tab btn instance
|
|
||||||
-- @param name - name of parental node that represents tab btn
|
|
||||||
-- @return instance that represents the tab btn
|
|
||||||
function instance.new_tab_btn(_instance, _name, url, index)
|
|
||||||
local params = {url = url, index = index, name = _name}
|
|
||||||
local btn = M.new_button(factory, _name, nil, params)
|
|
||||||
btn.back_anim = nil
|
|
||||||
btn.manual_back = button.back_tab_animation
|
|
||||||
btn.tap_anim = button.tap_tab_animation
|
|
||||||
btn.callback = function(_, _, force)
|
|
||||||
instance.switch_tab(instance, params, force)
|
|
||||||
if callback then
|
|
||||||
callback(factory.parent, index, force)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
instance[_name] = params
|
|
||||||
if not instance.btns then
|
|
||||||
instance.btns = {}
|
|
||||||
end
|
|
||||||
instance.btns[index] = btn
|
|
||||||
return btn
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Add new spine animation
|
|
||||||
-- @param factory - parent factory
|
function M.create(factory, module, name, ...)
|
||||||
-- @param name - a node name for a spine anim
|
local instance = create(module, factory, name)
|
||||||
-- @param idle_table - table with idle animations
|
|
||||||
-- @param active_table - table with active animations
|
if instance.init then
|
||||||
-- @param init_idle - init idle animation name or index in idle table
|
instance:init(...)
|
||||||
-- @return instance with spine anim
|
end
|
||||||
function M.new_spine_anim(factory, name, idle_table, active_table, init_idle)
|
|
||||||
local instance = create(spine_anim, factory, name, LAYOUT_CHANGED)
|
|
||||||
instance.idle_table = idle_table
|
|
||||||
instance.active_table = active_table
|
|
||||||
instance:play_idle(init_idle)
|
|
||||||
return instance
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Add new scrolling box
|
|
||||||
-- @param factory - parent factory
|
function M.get_text(name)
|
||||||
-- @param name - a node name for a spine anim
|
-- override to get text for localized text
|
||||||
-- @param zone_name - node name of zone for tap
|
|
||||||
-- @param speed_coef - vector3 coef. of speed for scrolling
|
|
||||||
-- @param maximum - vector3 maximum position for scrolling
|
|
||||||
-- @param points_of_interest - table with vector3 point of interes
|
|
||||||
-- @param callback - scrolling events callback
|
|
||||||
-- @return instance with scrolling box
|
|
||||||
function M.new_scrolling_box(factory, name, zone_name, speed_coef, maximum, points_of_interest, callback)
|
|
||||||
local instance = create(scrolling_box, factory, name, ON_UPDATE, ON_SWIPE)
|
|
||||||
instance.pos = gui.get_position(instance.node)
|
|
||||||
instance.start_pos = vmath.vector3(instance.pos)
|
|
||||||
instance.maximum = maximum
|
|
||||||
instance.points_of_interest = points_of_interest
|
|
||||||
instance.callback = callback
|
|
||||||
if instance.start_pos.x > instance.maximum.x then
|
|
||||||
instance.start_pos.x, instance.maximum.x = instance.maximum.x, instance.start_pos.x
|
|
||||||
end
|
|
||||||
if instance.start_pos.y > instance.maximum.y then
|
|
||||||
instance.start_pos.y, instance.maximum.y = instance.maximum.y, instance.start_pos.y
|
|
||||||
end
|
|
||||||
if type(name) == STRING then
|
|
||||||
instance.scrolling_zone = gui.get_node(zone_name)
|
|
||||||
else
|
|
||||||
instance.scrolling_zone = zone_name
|
|
||||||
end
|
|
||||||
instance.swipe = {
|
|
||||||
minSwipeDistance = 40,
|
|
||||||
speed_down_coef = 1.1,
|
|
||||||
speed_up_coef = speed_coef or vmath.vector3(1.1, 1.1, 0),
|
|
||||||
speed = vmath.vector3(0, 0, 0),
|
|
||||||
maximum = vmath.vector3(0, 0, 0),
|
|
||||||
min_speed = 2,
|
|
||||||
beginX = 0,
|
|
||||||
beginY = 0,
|
|
||||||
endX = 0,
|
|
||||||
endY = 0,
|
|
||||||
xDistance = nil,
|
|
||||||
yDistance = nil,
|
|
||||||
totalSwipeDistanceLeft = nil,
|
|
||||||
totalSwipeDistanceRight = nil,
|
|
||||||
totalSwipeDistanceUp = nil,
|
|
||||||
totalSwipeDistanceDown = nil,
|
|
||||||
is_swipe = nil,
|
|
||||||
end_move_coef_x = 1,
|
|
||||||
end_move_coef_y = 1,
|
|
||||||
back_slow_coef = 0.4,
|
|
||||||
end_position_x = nil,
|
|
||||||
end_position_y = nil,
|
|
||||||
is_x = instance.start_pos.x ~= instance.maximum.x,
|
|
||||||
is_y = instance.start_pos.y ~= instance.maximum.y
|
|
||||||
}
|
|
||||||
return instance
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function M.play_sound(name)
|
||||||
|
-- override to play sound with name
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
return M
|
return M
|
@ -4,15 +4,6 @@ local ADD_FOCUS = hash("acquire_input_focus")
|
|||||||
local REMOVE_FOCUS = hash("release_input_focus")
|
local REMOVE_FOCUS = hash("release_input_focus")
|
||||||
local PATH_OBJ = "."
|
local PATH_OBJ = "."
|
||||||
|
|
||||||
M.A_CLICK = hash("click")
|
|
||||||
M.A_TEXT = hash("text")
|
|
||||||
M.A_BACKSPACE = hash("backspace")
|
|
||||||
M.A_ENTER = hash("enter")
|
|
||||||
M.A_ANDR_BACK = hash("back")
|
|
||||||
|
|
||||||
M.RELEASED = "released"
|
|
||||||
M.PRESSED = "pressed"
|
|
||||||
|
|
||||||
function M.focus()
|
function M.focus()
|
||||||
msg.post(PATH_OBJ, ADD_FOCUS)
|
msg.post(PATH_OBJ, ADD_FOCUS)
|
||||||
end
|
end
|
||||||
|
@ -1,10 +1,131 @@
|
|||||||
script: "/example/example.gui.gui_script"
|
script: "/example/example.gui.gui_script"
|
||||||
|
fonts {
|
||||||
|
name: "system_font"
|
||||||
|
font: "/builtins/fonts/system_font.font"
|
||||||
|
}
|
||||||
background_color {
|
background_color {
|
||||||
x: 0.0
|
x: 0.0
|
||||||
y: 0.0
|
y: 0.0
|
||||||
z: 0.0
|
z: 0.0
|
||||||
w: 0.0
|
w: 0.0
|
||||||
}
|
}
|
||||||
|
nodes {
|
||||||
|
position {
|
||||||
|
x: 200.0
|
||||||
|
y: 200.0
|
||||||
|
z: 0.0
|
||||||
|
w: 1.0
|
||||||
|
}
|
||||||
|
rotation {
|
||||||
|
x: 0.0
|
||||||
|
y: 0.0
|
||||||
|
z: 0.0
|
||||||
|
w: 1.0
|
||||||
|
}
|
||||||
|
scale {
|
||||||
|
x: 1.0
|
||||||
|
y: 1.0
|
||||||
|
z: 1.0
|
||||||
|
w: 1.0
|
||||||
|
}
|
||||||
|
size {
|
||||||
|
x: 200.0
|
||||||
|
y: 100.0
|
||||||
|
z: 0.0
|
||||||
|
w: 1.0
|
||||||
|
}
|
||||||
|
color {
|
||||||
|
x: 1.0
|
||||||
|
y: 1.0
|
||||||
|
z: 1.0
|
||||||
|
w: 1.0
|
||||||
|
}
|
||||||
|
type: TYPE_BOX
|
||||||
|
blend_mode: BLEND_MODE_ALPHA
|
||||||
|
texture: ""
|
||||||
|
id: "button"
|
||||||
|
xanchor: XANCHOR_NONE
|
||||||
|
yanchor: YANCHOR_NONE
|
||||||
|
pivot: PIVOT_CENTER
|
||||||
|
adjust_mode: ADJUST_MODE_FIT
|
||||||
|
layer: ""
|
||||||
|
inherit_alpha: true
|
||||||
|
slice9 {
|
||||||
|
x: 0.0
|
||||||
|
y: 0.0
|
||||||
|
z: 0.0
|
||||||
|
w: 0.0
|
||||||
|
}
|
||||||
|
clipping_mode: CLIPPING_MODE_NONE
|
||||||
|
clipping_visible: true
|
||||||
|
clipping_inverted: false
|
||||||
|
alpha: 1.0
|
||||||
|
template_node_child: false
|
||||||
|
size_mode: SIZE_MODE_AUTO
|
||||||
|
}
|
||||||
|
nodes {
|
||||||
|
position {
|
||||||
|
x: 0.0
|
||||||
|
y: 0.0
|
||||||
|
z: 0.0
|
||||||
|
w: 1.0
|
||||||
|
}
|
||||||
|
rotation {
|
||||||
|
x: 0.0
|
||||||
|
y: 0.0
|
||||||
|
z: 0.0
|
||||||
|
w: 1.0
|
||||||
|
}
|
||||||
|
scale {
|
||||||
|
x: 2.0
|
||||||
|
y: 2.0
|
||||||
|
z: 1.0
|
||||||
|
w: 1.0
|
||||||
|
}
|
||||||
|
size {
|
||||||
|
x: 200.0
|
||||||
|
y: 100.0
|
||||||
|
z: 0.0
|
||||||
|
w: 1.0
|
||||||
|
}
|
||||||
|
color {
|
||||||
|
x: 0.0
|
||||||
|
y: 0.0
|
||||||
|
z: 0.0
|
||||||
|
w: 1.0
|
||||||
|
}
|
||||||
|
type: TYPE_TEXT
|
||||||
|
blend_mode: BLEND_MODE_ALPHA
|
||||||
|
text: "button text"
|
||||||
|
font: "system_font"
|
||||||
|
id: "text"
|
||||||
|
xanchor: XANCHOR_NONE
|
||||||
|
yanchor: YANCHOR_NONE
|
||||||
|
pivot: PIVOT_CENTER
|
||||||
|
outline {
|
||||||
|
x: 1.0
|
||||||
|
y: 1.0
|
||||||
|
z: 1.0
|
||||||
|
w: 1.0
|
||||||
|
}
|
||||||
|
shadow {
|
||||||
|
x: 1.0
|
||||||
|
y: 1.0
|
||||||
|
z: 1.0
|
||||||
|
w: 1.0
|
||||||
|
}
|
||||||
|
adjust_mode: ADJUST_MODE_FIT
|
||||||
|
line_break: false
|
||||||
|
parent: "button"
|
||||||
|
layer: ""
|
||||||
|
inherit_alpha: true
|
||||||
|
alpha: 1.0
|
||||||
|
outline_alpha: 1.0
|
||||||
|
shadow_alpha: 1.0
|
||||||
|
template_node_child: false
|
||||||
|
text_leading: 1.0
|
||||||
|
text_tracking: 0.0
|
||||||
|
}
|
||||||
material: "/builtins/materials/gui.material"
|
material: "/builtins/materials/gui.material"
|
||||||
adjust_reference: ADJUST_REFERENCE_PARENT
|
adjust_reference: ADJUST_REFERENCE_PARENT
|
||||||
max_nodes: 512
|
max_nodes: 512
|
||||||
|
@ -1,29 +1,21 @@
|
|||||||
local druid = require "druid.druid"
|
local druid = require "druid.druid"
|
||||||
|
|
||||||
function init(self)
|
function init(self)
|
||||||
end
|
self.druid = druid.new(self)
|
||||||
|
|
||||||
function final(self)
|
self.button = self.druid:new_button("button", function()
|
||||||
-- Add finalization code here
|
print("New click")
|
||||||
-- Remove this function if not needed
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function update(self, dt)
|
function update(self, dt)
|
||||||
-- Add update code here
|
self.druid:update(dt)
|
||||||
-- Remove this function if not needed
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function on_message(self, message_id, message, sender)
|
function on_message(self, message_id, message, sender)
|
||||||
-- Add message-handling code here
|
self.druid:on_message(message_id, message, sender)
|
||||||
-- Remove this function if not needed
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function on_input(self, action_id, action)
|
function on_input(self, action_id, action)
|
||||||
-- Add input-handling code here
|
self.druid:on_input(action_id, action)
|
||||||
-- Remove this function if not needed
|
|
||||||
end
|
|
||||||
|
|
||||||
function on_reload(self)
|
|
||||||
-- Add input-handling code here
|
|
||||||
-- Remove this function if not needed
|
|
||||||
end
|
end
|
@ -5,8 +5,8 @@ main_collection = /example/example.collectionc
|
|||||||
shared_state = 1
|
shared_state = 1
|
||||||
|
|
||||||
[display]
|
[display]
|
||||||
width = 960
|
width = 400
|
||||||
height = 640
|
height = 400
|
||||||
|
|
||||||
[project]
|
[project]
|
||||||
title = druid
|
title = druid
|
||||||
|
Loading…
x
Reference in New Issue
Block a user