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,51 +1,86 @@
local data = require("druid.data") 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 = {} local M = {}
M.interest = { 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_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.DEFAUL_ACTIVATION_TIME = 0.2
function M.init(instance, callback, event, action, animate_node_name, sound) function M.init(instance, callback, params, animate_node_name, event)
instance.event = event or data.A_TOUCH instance.event = 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.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_from = gui.get_scale(instance.anim_node)
instance.scale_to = instance.scale_from + M.DEFAULT_SCALE_CHANGE 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.pos = gui.get_position(instance.anim_node)
instance.callback = callback instance.callback = callback
-- instance.params = params instance.params = params
instance.tap_anim = M.tap_scale_animation instance.tap_anim = M.tap_scale_animation
instance.back_anim = M.back_scale_animation instance.back_anim = M.back_scale_animation
-- instance.sound = sound or M.BTN_SOUND_FUNC instance.hover_anim = b_settings.IS_HOVER
-- instance.sound_disable = sound_disable or M.BTN_SOUND_DISABLE_FUNC instance.sound = b_settings.BTN_SOUND
instance.sound_disable = b_settings.BTN_SOUND_DISABLED
end 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 --- Set text to text field
-- @param action_id - input action id -- @param action_id - input action id
-- @param action - input action -- @param action - input action
function M.on_input(instance, action_id, 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 gui.pick_node(instance.node, action.x, action.y) then
if not instance.disabled then if action.pressed then
instance.tap_anim(instance) -- Can interact if start touch on the button
instance.can_action = true
return true return true
end
if action.released then
return on_button_release(instance)
else else
-- instance.sound_disable() 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 return false
end end
end
return false
end end
function M.tap_scale_animation(instance) function M.tap_scale_animation(instance)
@ -54,34 +89,15 @@ function M.tap_scale_animation(instance)
if instance.back_anim then if instance.back_anim then
instance.back_anim(instance) instance.back_anim(instance)
end end
-- instance.sound()
instance.callback(instance.parent.parent, instance.params, instance)
end end
) )
end end
function M.back_scale_animation(instance) 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 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) function M.deactivate(instance, is_animate, callback)
instance.disabled = true instance.disabled = true
@ -108,6 +124,7 @@ function M.deactivate(instance, is_animate, callback)
end end
end end
function M.activate(instance, is_animate, callback) function M.activate(instance, is_animate, callback)
if is_animate then if is_animate then
local counter = 0 local counter = 0

View File

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

View File

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

View File

@ -1,6 +1,6 @@
local M = {} 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_SCALE_CHANGE = vmath.vector3(-0.05, - 0.1, 1)
M.DEFAULT_POS_CHANGE = vmath.vector3(0, - 10, 0) M.DEFAULT_POS_CHANGE = vmath.vector3(0, - 10, 0)

View File

@ -1,6 +1,6 @@
local M = {} 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 function fly_to(instance, pos_from, speed, callback)
local pos_to = instance.get_pos_func() local pos_to = instance.get_pos_func()

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

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