mirror of
https://github.com/Insality/druid.git
synced 2025-09-27 18:12:19 +02:00
Add key_trigger to the button
This commit is contained in:
@@ -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,17 +172,23 @@ 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)
|
||||
if self.click_zone then
|
||||
is_pick = is_pick and gui.pick_node(self.click_zone, 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
|
||||
@@ -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
|
||||
|
||||
|
||||
|
@@ -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
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user