refactor button

This commit is contained in:
Insality 2019-09-25 20:53:32 +03:00
parent b72e7451e1
commit 711b28ede1
3 changed files with 82 additions and 42 deletions

View File

@ -10,6 +10,12 @@ Defold UI library
# API # API
# LDoc
Generate with `ldoc .` with `config.ld` file
Insctruction here: https://github.com/stevedonovan/LDoc
# Authors # Authors
# License # License

View File

@ -1,6 +1,10 @@
--- Component to handle basic GUI button --- Component to handle basic GUI button
-- @module base.button -- @module base.button
-- TODO: Add button mode:
-- Long tap
-- Repeated tap
local data = require("druid.data") local data = require("druid.data")
local ui_animate = require("druid.helper.druid_animate") local ui_animate = require("druid.helper.druid_animate")
local settings = require("druid.settings") local settings = require("druid.settings")
@ -15,7 +19,7 @@ M.interest = {
M.DEFAULT_DEACTIVATE_COLOR = vmath.vector4(0, 0, 0, 0) M.DEFAULT_DEACTIVATE_COLOR = vmath.vector4(0, 0, 0, 0)
M.DEFAULT_DEACTIVATE_SCALE = vmath.vector3(0.8, 0.9, 1) M.DEFAULT_DEACTIVATE_SCALE = vmath.vector3(0.8, 0.9, 1)
M.DEFAULT_ACTIVATE_SCALE = vmath.vector3(1, 1, 1) M.DEFAULT_ACTIVATE_SCALE = vmath.vector3(1, 1, 1)
M.DEFAUL_ACTIVATION_TIME = 0.2 M.DEFAULT_ACTIVATION_TIME = 0.2
function M.init(self, node, callback, params, anim_node, event) function M.init(self, node, callback, params, anim_node, event)
@ -33,14 +37,23 @@ function M.init(self, node, callback, params, anim_node, event)
self.hover_anim = b_settings.IS_HOVER self.hover_anim = b_settings.IS_HOVER
self.sound = b_settings.BTN_SOUND self.sound = b_settings.BTN_SOUND
self.sound_disable = b_settings.BTN_SOUND_DISABLED self.sound_disable = b_settings.BTN_SOUND_DISABLED
self.ext_zone = nil self.click_zone = nil
-- TODO: to separate component "block_input"?
-- If callback is nil, so the buttons is stub and without anim
-- Used for zones, what should dont pass the input to other UI elements
if not callback then
self.stub = true
self.hover_anim = false
self.callback = function() end
end
end end
local function set_hover(self, state) local function set_hover(self, state)
if self.hover_anim and self._is_hovered ~= state then if self.hover_anim and self._is_hovered ~= state then
local target_scale = state and self.scale_hover_to or self.scale_from local target_scale = state and self.scale_hover_to or self.scale_from
ui_animate.scale(self, self.node, target_scale, b_settings.HOVER_TIME) ui_animate.scale(self, self.anim_node, target_scale, b_settings.HOVER_TIME)
self._is_hovered = state self._is_hovered = state
end end
end end
@ -50,15 +63,17 @@ local function on_button_release(self)
if not self.disabled then if not self.disabled then
if not self.stub and self.can_action then if not self.stub and self.can_action then
self.can_action = false self.can_action = false
self.tap_anim(self) if self.tap_anim then
settings.play_sound(self.sound) self.tap_anim(self)
end
self.callback(self.parent.parent, self.params, self) self.callback(self.parent.parent, self.params, self)
settings.play_sound(self.sound)
else else
set_hover(self, false) set_hover(self, false)
end end
return true return true
else else
self.sound_disable() settings.play_sound(self.sound_disable)
return false return false
end end
end end
@ -73,30 +88,32 @@ function M.on_input(self, action_id, action)
end end
local is_pick = gui.pick_node(self.node, action.x, action.y) local is_pick = gui.pick_node(self.node, action.x, action.y)
if self.ext_zone then if self.click_zone then
is_pick = is_pick and gui.pick_node(self.ext_zone, action.x, action.y) is_pick = is_pick and gui.pick_node(self.click_zone, action.x, action.y)
end end
if is_pick then if not is_pick then
if action.pressed then
-- Can interact if start touch on the button
self.can_action = true
return true
end
if action.released then
set_hover(self, false)
return on_button_release(self)
else
set_hover(self, true)
end
return not self.disabled
else
-- Can't interact, if touch outside of button -- Can't interact, if touch outside of button
self.can_action = false self.can_action = false
set_hover(self, false) set_hover(self, false)
return false return false
end end
if action.pressed then
-- Can interact if start touch on the button
self.can_action = true
self.repeated_counter = 0
return true
end
if action.released then
set_hover(self, false)
return on_button_release(self)
else
set_hover(self, true)
end
return not self.disabled
end end
@ -126,21 +143,21 @@ end
function M.deactivate(self, is_animate, callback) function M.deactivate(self, is_animate, callback)
self.disabled = true self.disabled = true
if is_animate then if is_animate then
local counter = 0 -- callback call three times in gui.animation
local clbk = function() local clbk = helper.after(3, function()
counter = counter + 1 if callback then
if counter == 3 and callback then
callback(self.parent.parent) callback(self.parent.parent)
end end
end end)
ui_animate.color(self, self.node, M.DEFAULT_DEACTIVATE_COLOR, ui_animate.color(self, self.node, M.DEFAULT_DEACTIVATE_COLOR,
clbk, M.DEFAUL_ACTIVATION_TIME, 0, gui.EASING_OUTBOUNCE) clbk, M.DEFAULT_ACTIVATION_TIME, 0, gui.EASING_OUTBOUNCE)
ui_animate.scale_y_from_to(self, self.node, M.DEFAULT_ACTIVATE_SCALE.x, ui_animate.scale_y_from_to(self, self.node, M.DEFAULT_ACTIVATE_SCALE.x,
M.DEFAULT_DEACTIVATE_SCALE.x, clbk, M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE) M.DEFAULT_DEACTIVATE_SCALE.x, clbk, M.DEFAULT_ACTIVATION_TIME, gui.EASING_OUTBOUNCE)
ui_animate.scale_x_from_to(self, self.node, M.DEFAULT_ACTIVATE_SCALE.y, ui_animate.scale_x_from_to(self, self.node, M.DEFAULT_ACTIVATE_SCALE.y,
M.DEFAULT_DEACTIVATE_SCALE.y, clbk, M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE) M.DEFAULT_DEACTIVATE_SCALE.y, clbk, M.DEFAULT_ACTIVATION_TIME, gui.EASING_OUTBOUNCE)
else else
gui.set_color(self.node, M.DEFAULT_DEACTIVATE_COLOR) gui.set_color(self.node, M.DEFAULT_DEACTIVATE_COLOR)
gui.set_scale(self.node, M.DEFAULT_DEACTIVATE_SCALE) gui.set_scale(self.node, M.DEFAULT_DEACTIVATE_SCALE)
@ -153,24 +170,22 @@ end
function M.activate(self, is_animate, callback) function M.activate(self, is_animate, callback)
if is_animate then if is_animate then
local counter = 0 -- callback call three times in gui.animation
local clbk = function() local clbk = helper.after(3, function()
counter = counter + 1
if counter == 3 then
self.disabled = false self.disabled = false
if callback then if callback then
callback(self.parent.parent) callback(self.parent.parent)
end end
end end)
end
ui_animate.color(self, self.node, ui_animate.TINT_SHOW, ui_animate.color(self, self.node, ui_animate.TINT_SHOW,
clbk, M.DEFAUL_ACTIVATION_TIME, 0, gui.EASING_OUTBOUNCE) clbk, M.DEFAULT_ACTIVATION_TIME, 0, gui.EASING_OUTBOUNCE)
ui_animate.scale_y_from_to(self, self.node, M.DEFAULT_DEACTIVATE_SCALE.x, ui_animate.scale_y_from_to(self, self.node, M.DEFAULT_DEACTIVATE_SCALE.x,
M.DEFAULT_ACTIVATE_SCALE.x, clbk, M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE) M.DEFAULT_ACTIVATE_SCALE.x, clbk, M.DEFAULT_ACTIVATION_TIME, gui.EASING_OUTBOUNCE)
ui_animate.scale_x_from_to(self, self.node, M.DEFAULT_DEACTIVATE_SCALE.y, ui_animate.scale_x_from_to(self, self.node, M.DEFAULT_DEACTIVATE_SCALE.y,
M.DEFAULT_ACTIVATE_SCALE.y, clbk, M.DEFAUL_ACTIVATION_TIME, gui.EASING_OUTBOUNCE) M.DEFAULT_ACTIVATE_SCALE.y, clbk, M.DEFAULT_ACTIVATION_TIME, gui.EASING_OUTBOUNCE)
else else
gui.set_color(self.node, ui_animate.TINT_SHOW) gui.set_color(self.node, ui_animate.TINT_SHOW)
gui.set_scale(self.node, M.DEFAULT_ACTIVATE_SCALE) gui.set_scale(self.node, M.DEFAULT_ACTIVATE_SCALE)
@ -181,10 +196,18 @@ function M.activate(self, is_animate, callback)
end end
end end
function M.disable_animation(self)
self.hover_anim = false
self.tap_anim = nil
self.back_anim = nil
end
--- Set additional node, what need to be clicked on button click --- Set additional node, what need to be clicked on button click
-- Used, if need setup, what button can be clicked only in special zone -- Used, if need setup, what button can be clicked only in special zone
function M.set_ext_zone(self, zone) function M.set_click_zone(self, zone)
self.ext_zone = helper.get_node(zone) self.click_zone = helper.get_node(zone)
end end

View File

@ -67,6 +67,17 @@ function M.centrate_icon_with_text(icon_node, text_node, margin)
end end
-- call callback after count calls
function M.after(count, callback)
local closure = function()
count = count - 1
if count <= 0 then
callback()
end
end
return closure
end
local STRING = "string" local STRING = "string"
function M.get_node(node_or_name) function M.get_node(node_or_name)