check project code style. add .luacheckrc (#7)

This commit is contained in:
Maxim Tuprikov 2019-03-29 00:45:46 +03:00 committed by GitHub
parent 235476fee4
commit 12f5f22b69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 328 additions and 247 deletions

51
.luacheckrc Normal file
View File

@ -0,0 +1,51 @@
std = "max"
files['.luacheckrc'].global = false
unused_args = false
max_code_line_length = 90
max_comment_line_length = false
globals = {
"sys",
"go",
"gui",
"label",
"render",
"crash",
"sprite",
"sound",
"tilemap",
"spine",
"particlefx",
"physics",
"factory",
"collectionfactory",
"iac",
"msg",
"vmath",
"url",
"http",
"image",
"json",
"zlib",
"iap",
"push",
"facebook",
"hash",
"hash_to_hex",
"pprint",
"init",
"final",
"update",
"on_input",
"on_message",
"on_reload",
"socket",
"table",
"debug",
"timer",
"window",
"buffer",
"resource",
"defos",
}

View File

@ -1,14 +1,15 @@
# druid # Defold Druid
Defold UI library Defold UI library
--
This project was created from the "empty" project template. # Install
The settings in ["game.project"](defold://open?path=/game.project) are all the default. A bootstrap empty ["main.collection"](defold://open?path=/main/main.collection) is included. # Setup
Check out [the documentation pages](https://defold.com/learn) for examples, tutorials, manuals and API docs. # Usage
If you run into trouble, help is available in [our forum](https://forum.defold.com). # API
Happy Defolding! # Authors
--- # License

View File

@ -1,7 +1,6 @@
local data = require("druid.data") local data = require("druid.data")
local M = {} local M = {}
M.interest = { M.interest = {
data.ON_INPUT data.ON_INPUT
} }

View File

@ -4,7 +4,6 @@ local settings = require("druid.settings")
local b_settings = settings.button local b_settings = settings.button
local M = {} local M = {}
M.interest = { M.interest = {
data.ON_INPUT data.ON_INPUT
} }
@ -15,9 +14,9 @@ M.DEFAULT_ACTIVATE_SCALE = vmath.vector3(1, 1, 1)
M.DEFAUL_ACTIVATION_TIME = 0.2 M.DEFAUL_ACTIVATION_TIME = 0.2
function M.init(instance, callback, params, animate_node_name, event) function M.init(instance, callback, params, anim_node)
instance.event = data.A_TOUCH instance.event = data.A_TOUCH
instance.anim_node = animate_node_name and gui.get_node(animate_node_name) or instance.node instance.anim_node = anim_node and gui.get_node(anim_node) or instance.node
instance.scale_from = gui.get_scale(instance.anim_node) instance.scale_from = gui.get_scale(instance.anim_node)
instance.scale_to = instance.scale_from + b_settings.SCALE_CHANGE instance.scale_to = instance.scale_from + b_settings.SCALE_CHANGE
instance.scale_hover_to = instance.scale_from + b_settings.HOVER_SCALE instance.scale_hover_to = instance.scale_from + b_settings.HOVER_SCALE
@ -40,6 +39,7 @@ local function set_hover(instance, state)
end end
end end
local function on_button_release(instance) local function on_button_release(instance)
if not instance.disabled then if not instance.disabled then
if not instance.stub and instance.can_action then if not instance.stub and instance.can_action then
@ -83,6 +83,7 @@ function M.on_input(instance, action_id, action)
end end
end end
function M.tap_scale_animation(instance) function M.tap_scale_animation(instance)
ui_animate.scale_to(instance, instance.anim_node, instance.scale_to, ui_animate.scale_to(instance, instance.anim_node, instance.scale_to,
function() function()
@ -109,12 +110,14 @@ function M.deactivate(instance, is_animate, callback)
callback(instance.parent.parent) callback(instance.parent.parent)
end end
end end
ui_animate.color(instance, instance.node, M.DEFAULT_DEACTIVATE_COLOR, clbk, M.DEFAUL_ACTIVATION_TIME, 0, ui_animate.color(instance, instance.node, M.DEFAULT_DEACTIVATE_COLOR,
gui.EASING_OUTBOUNCE) 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_y_from_to(instance, instance.node, M.DEFAULT_ACTIVATE_SCALE.x,
ui_animate.scale_x_from_to(instance, instance.node, M.DEFAULT_ACTIVATE_SCALE.y, M.DEFAULT_DEACTIVATE_SCALE.y, clbk, M.DEFAULT_DEACTIVATE_SCALE.x, clbk, M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE)
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 else
gui.set_color(instance.node, M.DEFAULT_DEACTIVATE_COLOR) gui.set_color(instance.node, M.DEFAULT_DEACTIVATE_COLOR)
gui.set_scale(instance.node, M.DEFAULT_DEACTIVATE_SCALE) gui.set_scale(instance.node, M.DEFAULT_DEACTIVATE_SCALE)
@ -137,12 +140,14 @@ function M.activate(instance, is_animate, callback)
end end
end end
end end
ui_animate.color(instance, instance.node, ui_animate.TINT_SHOW, clbk, M.DEFAUL_ACTIVATION_TIME, 0, ui_animate.color(instance, instance.node, ui_animate.TINT_SHOW,
gui.EASING_OUTBOUNCE) 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_y_from_to(instance, instance.node, M.DEFAULT_DEACTIVATE_SCALE.x,
ui_animate.scale_x_from_to(instance, instance.node, M.DEFAULT_DEACTIVATE_SCALE.y, M.DEFAULT_ACTIVATE_SCALE.y, clbk, M.DEFAULT_ACTIVATE_SCALE.x, clbk, M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE)
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 else
gui.set_color(instance.node, ui_animate.TINT_SHOW) gui.set_color(instance.node, ui_animate.TINT_SHOW)
gui.set_scale(instance.node, M.DEFAULT_ACTIVATE_SCALE) gui.set_scale(instance.node, M.DEFAULT_ACTIVATE_SCALE)

View File

@ -3,70 +3,71 @@ local settings = require("druid.settings")
local M = {} local M = {}
M.interest = { M.interest = {
data.TRANSLATABLE, data.TRANSLATABLE,
data.LAYOUT_CHANGED data.LAYOUT_CHANGED
} }
function M.init(instance, value, is_locale) function M.init(instance, value, is_locale)
instance.last_color = gui.get_color(instance.node) instance.last_color = gui.get_color(instance.node)
if is_locale then if is_locale then
instance.text_id = value instance.text_id = value
instance:translate() instance:translate()
else else
instance:set_to(value or 0) instance:set_to(value or 0)
end end
return instance return instance
end end
function M.translate(instance) function M.translate(instance)
if instance.text_id then if instance.text_id then
instance:set_to(settings.get_text(instance.text_id)) instance:set_to(settings.get_text(instance.text_id))
end end
end end
--- Set text to text field --- Set text to text field
-- @param set_to - set value to text field -- @param set_to - set value to text field
function M.set_to(instance, set_to) function M.set_to(instance, set_to)
instance.last_value = set_to instance.last_value = set_to
gui.set_text(instance.node, set_to) gui.set_text(instance.node, set_to)
end end
--- Set color --- Set color
-- @param color -- @param color
function M.set_color(instance, color) function M.set_color(instance, color)
instance.last_color = color instance.last_color = color
gui.set_color(instance.node, color) gui.set_color(instance.node, color)
end end
--- Set alpha --- Set alpha
-- @param alpha, number [0-1] -- @param alpha, number [0-1]
function M.set_alpha(instance, alpha) function M.set_alpha(instance, alpha)
instance.last_color.w = alpha instance.last_color.w = alpha
gui.set_color(instance.node, instance.last_color) gui.set_color(instance.node, instance.last_color)
end end
--- Set scale --- Set scale
-- @param scale -- @param scale
function M.set_scale(instance, scale) function M.set_scale(instance, scale)
instance.last_scale = scale instance.last_scale = scale
gui.set_scale(instance.node, scale) gui.set_scale(instance.node, scale)
end end
--- Called when layout updated (rotate for example) --- Called when layout updated (rotate for example)
function M.on_layout_updated(instance) function M.on_layout_updated(instance)
if instance.last_color then if instance.last_color then
M.set_color(instance, instance.last_color) M.set_color(instance, instance.last_color)
end end
if instance.last_scale then if instance.last_scale then
M.set_scale(instance, instance.last_scale) M.set_scale(instance, instance.last_scale)
end end
end end
return M return M

View File

@ -3,49 +3,48 @@ local formats = require("druid.helper.formats")
local helper = require("druid.helper.ui_helper") local helper = require("druid.helper.ui_helper")
local M = {} local M = {}
M.interest = { M.interest = {
data.LAYOUT_CHANGED, data.LAYOUT_CHANGED,
data.ON_UPDATE data.ON_UPDATE
} }
local empty = function() end local empty = function() end
function M.init(instance, seconds_from, seconds_to, callback) function M.init(instance, seconds_from, seconds_to, callback)
seconds_from = math.max(seconds_from, 0) seconds_from = math.max(seconds_from, 0)
seconds_to = math.max(seconds_to or 0, 0) seconds_to = math.max(seconds_to or 0, 0)
callback = callback or empty callback = callback or empty
instance:set_to(seconds_from) instance:set_to(seconds_from)
instance:set_interval(seconds_from, seconds_to) instance:set_interval(seconds_from, seconds_to)
instance.callback = callback instance.callback = callback
if seconds_to - seconds_from == 0 then if seconds_to - seconds_from == 0 then
instance:set_state(false) instance:set_state(false)
instance.callback(instance.parent.parent, instance) instance.callback(instance.parent.parent, instance)
end end
return instance return instance
end end
--- Set text to text field --- Set text to text field
-- @param set_to - set value in seconds -- @param set_to - set value in seconds
function M.set_to(instance, set_to) function M.set_to(instance, set_to)
instance.last_value = set_to instance.last_value = set_to
gui.set_text(instance.node, formats.second_string_min(set_to)) gui.set_text(instance.node, formats.second_string_min(set_to))
end end
--- Called when layout updated (rotate for example) --- Called when layout updated (rotate for example)
function M.on_layout_updated(instance) function M.on_layout_updated(instance)
M.set_to(instance, instance.last_value) M.set_to(instance, instance.last_value)
end end
--- Called when update --- Called when update
-- @param is_on - boolean is timer on -- @param is_on - boolean is timer on
function M.set_state(instance, is_on) function M.set_state(instance, is_on)
instance.is_on = is_on instance.is_on = is_on
end end
@ -53,32 +52,33 @@ end
-- @param from - "from" time in seconds -- @param from - "from" time in seconds
-- @param to - "to" time in seconds -- @param to - "to" time in seconds
function M.set_interval(instance, from, to) function M.set_interval(instance, from, to)
instance.from = from instance.from = from
instance.value = from instance.value = from
instance.temp = 0 instance.temp = 0
instance.target = to instance.target = to
M.set_state(instance, true) M.set_state(instance, true)
M.set_to(instance, from) M.set_to(instance, from)
end end
--- Called when update --- Called when update
-- @param dt - delta time -- @param dt - delta time
function M.update(instance, dt) function M.update(instance, dt)
if instance.is_on then if instance.is_on then
instance.temp = instance.temp + dt instance.temp = instance.temp + dt
local dist = math.min(1, math.abs(instance.value - instance.target)) local dist = math.min(1, math.abs(instance.value - instance.target))
if instance.temp > dist then if instance.temp > dist then
instance.temp = instance.temp - dist instance.temp = instance.temp - dist
instance.value = helper.step(instance.value, instance.target, 1) instance.value = helper.step(instance.value, instance.target, 1)
M.set_to(instance, instance.value) M.set_to(instance, instance.value)
if instance.value == instance.target then if instance.value == instance.target then
instance:set_state(false) instance:set_state(false)
instance.callback(instance.parent.parent, instance) instance.callback(instance.parent.parent, instance)
end end
end end
end end
end end
return M return M

View File

@ -9,9 +9,7 @@ local _factory = {}
local STRING = "string" local STRING = "string"
--- New druid era, registering components
M.comps = { M.comps = {
-- basic
button = require("druid.base.button"), button = require("druid.base.button"),
android_back = require("druid.base.android_back"), android_back = require("druid.base.android_back"),
text = require("druid.base.text"), text = require("druid.base.text"),
@ -34,6 +32,7 @@ function M.register(name, module)
log("Register component", name) log("Register component", name)
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)
@ -46,7 +45,6 @@ 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
@ -56,7 +54,7 @@ local function input_init(factory)
end end
local function create(module, factory, name, ...) local function create(module, factory, name)
local instance = setmetatable({}, {__index = module}) local instance = setmetatable({}, {__index = module})
instance.parent = factory instance.parent = factory
if name then if name then
@ -155,4 +153,5 @@ function _factory.update(factory, dt)
end end
end end
return M return M

View File

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

View File

@ -4,12 +4,15 @@ 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 = "."
function M.focus() function M.focus()
msg.post(PATH_OBJ, ADD_FOCUS) msg.post(PATH_OBJ, ADD_FOCUS)
end end
function M.remove() function M.remove()
msg.post(PATH_OBJ, REMOVE_FOCUS) msg.post(PATH_OBJ, REMOVE_FOCUS)
end end
return M return M

View File

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

View File

@ -1,24 +1,30 @@
local M = {} local M = {}
function M.centrate_text_with_icon(text_node, icon_node) function M.centrate_text_with_icon(text_node, icon_node, offset_x)
local metr = gui.get_text_metrics_from_node(text_node) offset_x = offset_x or 0
local scl = gui.get_scale(text_node).x local metr = gui.get_text_metrics_from_node(text_node)
local scl_i = gui.get_scale(icon_node).x local scl = gui.get_scale(text_node).x
local pos_i = gui.get_position(icon_node) local pos = gui.get_position(text_node)
local pos = gui.get_position(text_node) local scl_i = gui.get_scale(icon_node).x
local w = metr.width * scl * scl_i local pos_i = gui.get_position(icon_node)
local icon_w = gui.get_size(icon_node).x * scl_i local w = metr.width * scl -- text width
local width = w + icon_w + (math.abs(pos.x) - icon_w / 2) * scl_i local icon_w = gui.get_size(icon_node).x * scl_i -- icon width
pos_i.x = width / 2 - (icon_w / 2) local width = w + icon_w
gui.set_position(icon_node, pos_i)
pos.x = -width/2 + w + offset_x
gui.set_position(text_node, pos)
pos_i.x = width/2 - icon_w + offset_x
gui.set_position(icon_node, pos_i)
end end
function M.step(current, target, step) function M.step(current, target, step)
if current < target then if current < target then
return math.min(current + step, target) return math.min(current + step, target)
else else
return math.max(target, current - step) return math.max(target, current - step)
end end
end end
return M return M

View File

@ -6,7 +6,7 @@ M.button = {
IS_HOVER = true, IS_HOVER = true,
IS_HOLD = true, IS_HOLD = true,
BTN_SOUND = "click", BTN_SOUND = "click",
BTN_SOUND_DISABLED = "button_click_disabled", BTN_SOUND_DISABLED = "click_disabled",
HOVER_SCALE = vmath.vector3(-0.025, -0.025, 1), HOVER_SCALE = vmath.vector3(-0.025, -0.025, 1),
HOVER_TIME = 0.05, HOVER_TIME = 0.05,
SCALE_CHANGE = vmath.vector3(-0.05, - 0.05, 1), SCALE_CHANGE = vmath.vector3(-0.05, - 0.05, 1),
@ -24,6 +24,7 @@ function M.play_sound(name)
-- override to play sound with name -- override to play sound with name
end end
function M.log(...) function M.log(...)
if M.is_debug then if M.is_debug then
print("[Druid]: ", ...) print("[Druid]: ", ...)
@ -31,5 +32,4 @@ function M.log(...)
end end
return M return M