rename help_modules to helper

This commit is contained in:
Insality
2019-03-27 09:09:05 +03:00
parent a0a171df55
commit e4c8b65cc1
14 changed files with 15 additions and 11 deletions

View File

@@ -0,0 +1,173 @@
local M = {}
local PROP_SCALE = gui.PROP_SCALE
local PROP_POSITION = gui.PROP_POSITION
M.PROP_POS_X = hash("position.x")
M.PROP_POS_Y = hash("position.y")
M.PROP_ALPHA = hash("color.w")
local PROP_COLOR = hash("color")
local PROP_SCALE_X = "scale.x"
local PROP_SCALE_Y = "scale.y"
M.TINT_HIDE = vmath.vector4(1, 1, 1, 0)
M.TINT_SHOW = vmath.vector4(1, 1, 1, 1)
M.V3_ONE = vmath.vector3(1, 1, 1)
M.V3_ZERO = vmath.vector3(0, 0, 1)
M.SCALE_ANIMATION_TIME = 0.1
M.BOUNCE_ANIMATION_TIME = 0.25
M.ALPHA_ANIMATION_TIME = 0.25
function M.alpha(self, node, alpha, callback, time, delay, easing, playback)
time = time or M.ALPHA_ANIMATION_TIME
delay = delay or 0
easing = easing or gui.EASING_LINEAR
playback = playback or gui.PLAYBACK_ONCE_FORWARD
gui.animate(node, M.PROP_ALPHA, alpha, easing, time, delay,
function()
if callback then
callback(self, node)
end
end,
playback)
end
function M.color(self, node, color, callback, time, delay, easing, playback)
time = time or M.ALPHA_ANIMATION_TIME
delay = delay or 0
easing = easing or gui.EASING_LINEAR
playback = playback or gui.PLAYBACK_ONCE_FORWARD
gui.animate(node, PROP_COLOR, color, easing, time, delay,
function()
if callback then
callback(self, node)
end
end,
playback)
end
function M.shake(self, node, callback, str, time)
str = str or - 30
time = time or 0.25
local pos = gui.get_position(node)
pos.x = pos.x + str
gui.animate(node, PROP_POSITION, pos, gui.EASING_INELASTIC, time,
0,
function()
if callback then
callback(self)
end
end,
gui.PLAYBACK_ONCE_BACKWARD
)
end
function M.bounce(self, node, change_to, callback, time, easing, playback, delaly)
time = time or M.BOUNCE_ANIMATION_TIME
delaly = delaly or 0
easing = easing or gui.EASING_OUTSINE
playback = playback or gui.PLAYBACK_ONCE_PINGPONG
gui.animate(node, PROP_SCALE, change_to, easing, time, delaly,
function()
if callback then
callback(self)
end
end,
playback)
end
function M.fly_to(self, node, to_pos, speed, callback, delay, easing)
easing = easing or gui.EASING_OUTSINE
delay = delay or 0
local time = vmath.length(to_pos - gui.get_position(node)) / 100 / speed
gui.animate(node, gui.PROP_POSITION, to_pos, easing, time, delay,
function()
if callback then
callback(self, node)
end
end)
end
function M.fly_by_x(self, node, to_x, time, callback, delay, easing, playback)
playback = playback or gui.PLAYBACK_ONCE_FORWARD
easing = easing or gui.EASING_OUTSINE
delay = delay or 0
gui.animate(node, M.PROP_POS_X, to_x, easing, time, delay,
function()
if callback then
callback(self, node)
end
end,
playback)
end
function M.fly_by_y(self, node, to_y, time, callback, delay, easing, playback)
playback = playback or gui.PLAYBACK_ONCE_FORWARD
easing = easing or gui.EASING_OUTSINE
delay = delay or 0
time = time or 0.25
gui.animate(node, M.PROP_POS_Y, to_y, easing, time, delay,
function()
if callback then
callback(self, node)
end
end,
playback)
end
function M.scale_to(self, node, to, callback, time, delay, easing)
easing = easing or gui.EASING_INSINE
time = time or M.SCALE_ANIMATION_TIME
delay = delay or 0
time = time or 0.25
gui.animate(node, PROP_SCALE, to, easing, time, delay,
function()
if callback then
callback(self, node)
end
end
)
end
function M.scale(self, node, to, time)
gui.animate(node, "scale", to, gui.EASING_OUTSINE, time)
end
function M.scale_x_from_to(self, node, from, to, callback, time, easing, delay, playback)
easing = easing or gui.EASING_INSINE
time = time or M.SCALE_ANIMATION_TIME
delay = delay or 0
playback = playback or gui.PLAYBACK_ONCE_FORWARD
local scale = gui.get_scale(node)
scale.x = from
gui.set_scale(node, scale)
gui.animate(node, PROP_SCALE_X, to, easing, time, delay,
function()
if callback then
callback(self)
end
end,
playback
)
end
function M.scale_y_from_to(self, node, from, to, callback, time, easing, delay, playback)
easing = easing or gui.EASING_INSINE
time = time or M.SCALE_ANIMATION_TIME
delay = delay or 0
playback = playback or gui.PLAYBACK_ONCE_FORWARD
local scale = gui.get_scale(node)
scale.y = from
gui.set_scale(node, scale)
gui.animate(node, PROP_SCALE_Y, to, easing, time, delay,
function()
if callback then
callback(self)
end
end,
playback
)
end
return M

View File

@@ -0,0 +1,15 @@
local M = {}
local ADD_FOCUS = hash("acquire_input_focus")
local REMOVE_FOCUS = hash("release_input_focus")
local PATH_OBJ = "."
function M.focus()
msg.post(PATH_OBJ, ADD_FOCUS)
end
function M.remove()
msg.post(PATH_OBJ, REMOVE_FOCUS)
end
return M

34
druid/helper/formats.lua Normal file
View File

@@ -0,0 +1,34 @@
local M = {}
local ZERO = "0"
-- Return number with zero number prefix
-- @param num - number for conversion
-- @param count - count of numerals
-- @return string with need count of zero (1,3) -> 001
function M.add_prefix_zeros(num, count)
local result = tostring(num)
for i = string.len(result), count - 1 do
result = ZERO..result
end
return result
end
-- Convert seconds to string minutes:seconds
-- @param num - number of seconds
-- @return string minutes:seconds
function M.second_string_min(sec)
local mins = math.floor(sec / 60)
local seconds = math.floor(sec - mins * 60)
return string.format("%.2d:%.2d", mins, seconds)
end
-- Interpolate string with named Parameters in Table
-- @param s - string for interpolate
-- @param tab - table with parameters
-- @return string with replaced parameters
function M.interpolate_strinng(s, tab)
return (s:gsub('($%b{})', function(w) return tab[w:sub(3, -2)] or w end))
end
return M

View File

@@ -0,0 +1,16 @@
local M = {}
function M.centrate_text_with_icon(text_node, icon_node)
local metr = gui.get_text_metrics_from_node(text_node)
local scl = gui.get_scale(text_node).x
local scl_i = gui.get_scale(icon_node).x
local pos_i = gui.get_position(icon_node)
local pos = gui.get_position(text_node)
local w = metr.width * scl * scl_i
local icon_w = gui.get_size(icon_node).x * scl_i
local width = w + icon_w + (math.abs(pos.x) - icon_w / 2) * scl_i
pos_i.x = width / 2 - (icon_w / 2)
gui.set_position(icon_node, pos_i)
end
return M