Add key_trigger to the button

This commit is contained in:
Insality 2020-02-24 23:56:57 +03:00
parent 162bbd0ed9
commit 2d45573f02
6 changed files with 216 additions and 13 deletions

View File

@ -38,6 +38,19 @@ local component = require("druid.component")
local M = component.create("button", { const.ON_INPUT })
local function is_input_match(self, action_id)
if action_id == const.ACTION_TOUCH then
return true
end
if self.key_trigger and action_id == self.key_trigger then
return true
end
return false
end
local function on_button_hover(self, hover_state)
if not self.style.on_hover then
return
@ -57,6 +70,11 @@ end
local function on_button_repeated_click(self)
if not self.is_repeated_started then
self.click_in_row = 0
self.is_repeated_started = true
end
if self.style.on_click then
self.style.on_click(self, self.anim_node)
end
@ -143,6 +161,7 @@ function M.init(self, node, callback, params, anim_node, event)
self.last_pressed_time = 0
self.last_released_time = 0
self.click_in_row = 0
self.key_trigger = nil
-- Event stubs
self.on_click = Event(callback)
@ -153,18 +172,24 @@ end
function M.on_input(self, action_id, action)
if action_id ~= const.ACTION_TOUCH then
if not is_input_match(self, action_id) then
return false
end
local is_key_trigger = (action_id == self.key_trigger)
if not helper.is_enabled(self.node) then
return false
end
local is_pick = gui.pick_node(self.node, action.x, action.y)
local is_pick = true
if not is_key_trigger then
is_pick = gui.pick_node(self.node, action.x, action.y)
if self.click_zone then
is_pick = is_pick and gui.pick_node(self.click_zone, action.x, action.y)
end
end
if not is_pick then
-- Can't interact, if touch outside of button
@ -172,6 +197,10 @@ function M.on_input(self, action_id, action)
return false
end
if is_key_trigger then
self.hover:set_hover(not action.released)
end
if action.pressed then
-- Can interact if start touch on the button
self.can_action = true
@ -183,7 +212,6 @@ function M.on_input(self, action_id, action)
-- While hold button, repeat rate pick from input.repeat_interval
if action.repeated then
if not self.disabled and self.on_repeated_click:is_exist() and self.can_action then
self.is_repeated_started = true
on_button_repeated_click(self)
return true
end
@ -234,14 +262,18 @@ end
--- Set key-code to trigger this button
-- @function button:set_key_trigger
-- @tparam hash key The action_id of the key
function M.set_key_trigger(self, key)
self.key_trigger = hash(key)
end
--- Get key-code to trigger this button
-- @function button:get_key_trigger
-- @treturn hash The action_id of the key
function M.get_key_trigger(self)
return self.key_trigger
end

View File

@ -24,7 +24,7 @@ function M.init(self, node, on_hover_callback)
end
local function set_hover(self, state)
function M.set_hover(self, state)
if self._is_hovered ~= state then
self._is_hovered = state
self.on_hover:trigger(self:get_context(), state)
@ -44,20 +44,20 @@ function M.on_input(self, action_id, action)
local is_pick = gui.pick_node(self.node, action.x, action.y)
if not is_pick then
set_hover(self, false)
M.set_hover(self, false)
return false
end
if action.released then
set_hover(self, false)
M.set_hover(self, false)
else
set_hover(self, true)
M.set_hover(self, true)
end
end
function M.on_input_interrupt(self)
set_hover(self, false)
M.set_hover(self, false)
end

View File

@ -4835,6 +4835,166 @@ nodes {
text_leading: 1.0
text_tracking: 0.0
}
nodes {
position {
x: -200.0
y: -220.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_TEMPLATE
id: "button_key_trigger"
parent: "button_page"
layer: ""
inherit_alpha: true
alpha: 1.0
template: "/example/kenney/templates/button.gui"
template_node_child: false
}
nodes {
position {
x: 0.0
y: 0.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: 130.0
y: 60.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: "kenney/button_blue"
id: "button_key_trigger/button"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
parent: "button_key_trigger"
layer: ""
inherit_alpha: true
slice9 {
x: 15.0
y: 15.0
z: 15.0
w: 15.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: true
size_mode: SIZE_MODE_MANUAL
}
nodes {
position {
x: 0.0
y: 7.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.7
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: "Press Space"
font: "game"
id: "button_key_trigger/text"
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.101960786
y: 0.2
z: 0.6
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
parent: "button_key_trigger/button"
layer: ""
inherit_alpha: true
alpha: 1.0
outline_alpha: 0.0
shadow_alpha: 0.78
overridden_fields: 3
overridden_fields: 8
template_node_child: true
text_leading: 1.0
text_tracking: 0.0
}
nodes {
position {
x: 1800.0

View File

@ -29,13 +29,17 @@ local function setup_buttons(self)
custom_style.HOVER_IMAGE = "button_yellow"
custom_style.DEFAULT_IMAGE = "button_blue"
self.druid:new_button("button_long_tap/button", usual_callback)
.on_long_click:subscribe(long_tap_callback)
self.druid:new_button("button_repeated_tap/button", usual_callback)
.on_repeated_click:subscribe(repeated_callback)
self.druid:new_button("button_double_tap/button", usual_callback)
.on_double_click:subscribe(double_tap_callback)
local button_space = self.druid:new_button("button_key_trigger/button", usual_callback)
button_space:set_key_trigger("key_space")
button_space.on_long_click:subscribe(long_tap_callback)
button_space.on_double_click:subscribe(double_tap_callback)
end

View File

@ -17,3 +17,6 @@ include_dirs = druid
[graphics]
texture_profiles = /example/res/custom.texture_profiles
[input]
gamepads = /builtins/input/default.gamepadsc

View File

@ -6,6 +6,10 @@ key_trigger {
input: KEY_BACK
action: "back"
}
key_trigger {
input: KEY_SPACE
action: "key_space"
}
mouse_trigger {
input: MOUSE_BUTTON_1
action: "touch"