Remove match_event from druid, add hover component

This commit is contained in:
Insality
2020-02-22 15:09:29 +03:00
parent 0dd37a03cd
commit 3e30fd4417
5 changed files with 109 additions and 21 deletions

View File

@@ -13,7 +13,6 @@ local M = component.create("back_handler", { const.ON_INPUT })
-- @tparam callback callback On back button
-- @tparam[opt] params Callback argument
function M.init(self, callback, params)
self.event = const.ACTION_BACK
self.callback = callback
self.params = params
end
@@ -24,11 +23,12 @@ end
-- @tparam string action_id on_input action id
-- @tparam table action on_input action
function M.on_input(self, action_id, action)
if action[const.RELEASED] then
if action_id == const.ACTION_BACK and action[const.RELEASED] then
self.callback(self:get_context(), self.params)
return true
end
return true
return false
end

View File

@@ -10,11 +10,14 @@ local M = component.create("blocker", { const.ON_SWIPE })
function M.init(self, node)
self.node = self:get_node(node)
self.event = const.ACTION_TOUCH
end
function M.on_input(self, action_id, action)
if action_id ~= const.ACTION_TOUCH then
return false
end
if not helper.is_enabled(self.node) then
return false
end

View File

@@ -22,8 +22,6 @@ function M.init(self, node, callback, params, anim_node, event)
self.style = self:get_style()
self.node = self:get_node(node)
-- TODO: match event inside on_input?
self.event = const.ACTION_TOUCH
self.anim_node = anim_node and helper:get_node(anim_node) or self.node
-- TODO: rename to start_scale
self.scale_from = gui.get_scale(self.anim_node)
@@ -67,6 +65,10 @@ end
function M.on_input(self, action_id, action)
if action_id ~= const.ACTION_TOUCH then
return false
end
if not helper.is_enabled(self.node) then
return false
end

76
druid/base/hover.lua Normal file
View File

@@ -0,0 +1,76 @@
--- Component to handle hover node interaction
-- @module druid.input
local Event = require("druid.event")
local const = require("druid.const")
local helper = require("druid.helper")
local component = require("druid.component")
local M = component.create("hover", { const.ON_INPUT })
--- Component init function
-- @function hover:init
-- @tparam table self Component instance
-- @tparam node node Gui node
-- @tparam function on_hover_callback Hover callback
function M.init(self, node, on_hover_callback)
self.style = self:get_style()
self.node = self:get_node(node)
self._is_hovered = false
self.on_hover = Event()
if on_hover_callback then
self.on_hover:subscribe(on_hover_callback)
end
end
local function set_hover(self, state)
if self._is_hovered ~= state then
self._is_hovered = state
self.on_hover:trigger(state)
end
end
function M.on_input(self, action_id, action)
if action_id ~= const.ACTION_TOUCH then
return
end
if not helper.is_enabled(self.node) then
return false
end
local is_pick = gui.pick_node(self.node, action.x, action.y)
if not is_pick then
set_hover(self, false)
return false
end
if action.released then
set_hover(self, false)
else
set_hover(self, true)
end
end
function M.on_swipe(self)
set_hover(self, false)
end
--- Strict button click area. Useful for
-- no click events outside stencil node
-- @function button:set_click_zone
-- @tparam table self Component instance
-- @tparam node zone Gui node
function M.set_click_zone(self, zone)
self.click_zone = self:get_node(zone)
end
return M