Merge branch 'feature/button' into develop

This commit is contained in:
Insality 2019-03-27 19:38:10 +03:00
commit f660476046
24 changed files with 505 additions and 149 deletions

View File

@ -1,139 +1,156 @@
local data = require("druid.data")
local ui_animate = require "druid.help_modules.druid_animate"
local ui_animate = require "druid.helper.druid_animate"
local settings = require("druid.settings")
local b_settings = settings.button
local M = {}
M.interest = {
data.ON_INPUT
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
function M.init(instance, callback, params, animate_node_name, event)
instance.event = data.A_TOUCH
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 + b_settings.SCALE_CHANGE
instance.scale_hover_to = instance.scale_from + b_settings.HOVER_SCALE
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.hover_anim = b_settings.IS_HOVER
instance.sound = b_settings.BTN_SOUND
instance.sound_disable = b_settings.BTN_SOUND_DISABLED
end
local function set_hover(instance, state)
if instance.hover_anim and instance._is_hovered ~= state then
local target_scale = state and instance.scale_hover_to or instance.scale_from
ui_animate.scale(instance, instance.node, target_scale, b_settings.HOVER_TIME)
instance._is_hovered = state
end
end
local function on_button_release(instance)
if not instance.disabled then
if not instance.stub and instance.can_action then
instance.can_action = false
instance.tap_anim(instance)
settings.play_sound(instance.sound)
instance.callback(instance.parent.parent, instance.params, instance)
else
set_hover(instance, false)
end
return true
else
instance.sound_disable()
return false
end
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
if gui.pick_node(instance.node, action.x, action.y) then
if action.pressed then
-- Can interact if start touch on the button
instance.can_action = true
return true
end
if action.released then
return on_button_release(instance)
else
set_hover(instance, true)
end
return not instance.disabled
else
-- Can't interact, if touch outside of button
instance.can_action = false
set_hover(instance, false)
return false
end
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
)
ui_animate.scale_to(instance, instance.anim_node, instance.scale_to,
function()
if instance.back_anim then
instance.back_anim(instance)
end
end
)
end
function M.back_scale_animation(instance)
ui_animate.scale_to(instance, instance.anim_node, instance.scale_from)
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
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
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

View File

@ -1,6 +1,6 @@
local M = {}
local ui_animate = require "druid.help_modules.druid_animate"
local ui_animate = require "druid.helper.druid_animate"
--- Bounce text field
function M.bounce(instance, callback)

View File

@ -1,6 +1,6 @@
local M = {}
local formats = require "druid.help_modules.formats"
local formats = require "druid.helper.formats"
--- Set text to text field
-- @param set_to - set value in seconds

View File

@ -1,6 +1,6 @@
local M = {}
local ui_animate = require "druid.help_modules.druid_animate"
local ui_animate = require "druid.helper.druid_animate"
M.DEFAULT_SCALE_CHANGE = vmath.vector3(-0.05, - 0.1, 1)
M.DEFAULT_POS_CHANGE = vmath.vector3(0, - 10, 0)

View File

@ -1,6 +1,6 @@
local M = {}
local ui_animate = require "druid.help_modules.druid_animate"
local ui_animate = require "druid.helper.druid_animate"
local function fly_to(instance, pos_from, speed, callback)
local pos_to = instance.get_pos_func()

View File

@ -1,6 +1,6 @@
local M = {}
local ui_animate = require "druid.help_modules.druid_animate"
local ui_animate = require "druid.helper.druid_animate"
--- Bounce image
function M.bounce(instance)

View File

@ -1,7 +1,7 @@
local M = {}
local druid_input = require "druid.help_modules.druid_input"
local ui_animate = require "druid.help_modules.druid_animate"
local druid_input = require "druid.helper.druid_input"
local ui_animate = require "druid.helper.druid_animate"
M.START = hash("START")
M.FINISH = hash("FINISH")

View File

@ -1,6 +1,6 @@
local M = {}
local ui_animate = require "druid.help_modules.druid_animate"
local ui_animate = require "druid.helper.druid_animate"
--- Bounce text field
function M.bounce(instance, callback)

View File

@ -1,6 +1,6 @@
local M = {}
local formats = require "druid.help_modules.formats"
local formats = require "druid.helper.formats"
--- Set text to text field
-- @param set_to - set value in seconds

View File

@ -1,5 +1,5 @@
local data = require("druid.data")
local druid_input = require("druid.help_modules.druid_input")
local druid_input = require("druid.helper.druid_input")
local M = {}
@ -20,7 +20,6 @@ local function register_basic_components()
M.register(k, v)
end
end
register_basic_components()
function M.register(name, module)
@ -70,7 +69,7 @@ function M.on_input(factory, action_id, action)
local len = #factory[data.ON_INPUT]
for i = 1, len do
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 v:on_input(action_id, action) then
return true
end
end
@ -147,14 +146,5 @@ function M.create(factory, module, name, ...)
end
function M.get_text(name)
-- override to get text for localized text
end
function M.play_sound(name)
-- override to play sound with name
end
register_basic_components()
return M

View File

@ -130,6 +130,10 @@ function M.scale_to(self, node, to, callback, time, delay, easing)
)
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

26
druid/settings.lua Normal file
View File

@ -0,0 +1,26 @@
local M = {}
M.button = {
IS_HOVER = true,
IS_HOLD = true,
BTN_SOUND = "click",
BTN_SOUND_DISABLED = "button_click_disabled",
HOVER_SCALE = vmath.vector3(-0.025, -0.025, 1),
HOVER_TIME = 0.05,
SCALE_CHANGE = vmath.vector3(-0.05, - 0.05, 1),
}
function M.get_text(name)
-- override to get text for localized text
end
function M.play_sound(name)
-- override to play sound with name
end
return M

View File

@ -35,3 +35,43 @@ embedded_instances {
z: 1.0
}
}
embedded_instances {
id: "sounds"
data: "embedded_components {\n"
" id: \"click\"\n"
" type: \"sound\"\n"
" data: \"sound: \\\"/example/res/click.ogg\\\"\\n"
"looping: 0\\n"
"group: \\\"master\\\"\\n"
"gain: 0.4\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}

View File

@ -1,7 +1,11 @@
script: "/example/example.gui.gui_script"
fonts {
name: "system_font"
font: "/builtins/fonts/system_font.font"
name: "game"
font: "/example/game.font"
}
textures {
name: "gui"
texture: "/example/gui.atlas"
}
background_color {
x: 0.0
@ -12,7 +16,7 @@ background_color {
nodes {
position {
x: 200.0
y: 200.0
y: 325.0
z: 0.0
w: 1.0
}
@ -23,14 +27,14 @@ nodes {
w: 1.0
}
scale {
x: 1.0
y: 1.0
x: 0.5
y: 0.5
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 100.0
x: 426.0
y: 190.0
z: 0.0
w: 1.0
}
@ -42,8 +46,8 @@ nodes {
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: ""
id: "button"
texture: "gui/green_long"
id: "button_1"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
@ -66,7 +70,7 @@ nodes {
nodes {
position {
x: 0.0
y: 0.0
y: 20.0
z: 0.0
w: 1.0
}
@ -77,8 +81,8 @@ nodes {
w: 1.0
}
scale {
x: 2.0
y: 2.0
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
@ -89,16 +93,16 @@ nodes {
w: 1.0
}
color {
x: 0.0
y: 0.0
z: 0.0
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "button text"
font: "system_font"
id: "text"
text: "Button 1"
font: "game"
id: "text_1"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
@ -109,14 +113,248 @@ nodes {
w: 1.0
}
shadow {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
parent: "button_1"
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
}
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: 0.5
y: 0.5
z: 1.0
w: 1.0
}
size {
x: 426.0
y: 190.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: "gui/green_long"
id: "button_2"
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: 20.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_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "Button 2"
font: "game"
id: "text_2"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
outline {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
shadow {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
parent: "button"
parent: "button_2"
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
}
nodes {
position {
x: 200.0
y: 75.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 0.5
y: 0.5
z: 1.0
w: 1.0
}
size {
x: 426.0
y: 190.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: "gui/green_long"
id: "button_3"
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: 20.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_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "Button 3"
font: "game"
id: "text_3"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
outline {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
shadow {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
parent: "button_3"
layer: ""
inherit_alpha: true
alpha: 1.0

View File

@ -1,10 +1,25 @@
local druid = require "druid.druid"
local druid = require("druid.druid")
local druid_settings = require("druid.settings")
local function setup_druid(self)
druid_settings.play_sound = function(name)
sound.play("sounds#" .. name)
end
end
function init(self)
setup_druid(self)
self.druid = druid.new(self)
self.button = self.druid:new_button("button", function()
print("New click")
self.druid:new_button("button_1", function()
print("On button 1")
end)
self.druid:new_button("button_2", function()
print("On button 2")
end)
self.druid:new_button("button_3", function()
print("On button 3")
end)
end

17
example/game.font Normal file
View File

@ -0,0 +1,17 @@
font: "/example/res/exo2.ttf"
material: "/builtins/fonts/font.material"
size: 64
antialias: 1
alpha: 1.0
outline_alpha: 0.0
outline_width: 0.0
shadow_alpha: 1.0
shadow_blur: 0
shadow_x: 3.0
shadow_y: -5.0
extra_characters: ""
output_format: TYPE_BITMAP
all_chars: false
cache_width: 0
cache_height: 0
render_mode: MODE_MULTI_LAYER

9
example/gui.atlas Normal file
View File

@ -0,0 +1,9 @@
images {
image: "/example/res/gray_long.png"
}
images {
image: "/example/res/green_long.png"
}
margin: 0
extrude_borders: 0
inner_padding: 0

BIN
example/res/click.ogg Executable file

Binary file not shown.

BIN
example/res/exo2.ttf Executable file

Binary file not shown.

BIN
example/res/gray_long.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
example/res/green_long.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB