#49 add on_mouse_hover to hover component

This commit is contained in:
Insality
2020-05-04 01:12:25 +03:00
parent 75a1369997
commit 0cc848b53f
5 changed files with 52 additions and 8 deletions

View File

@@ -58,6 +58,15 @@ local function on_button_hover(self, hover_state)
end
local function on_button_mouse_hover(self, hover_state)
if not self._style.on_mouse_hover then
return
end
self._style.on_mouse_hover(self, self.anim_node, hover_state)
end
local function on_button_click(self)
if self._style.on_click then
self._style.on_click(self, self.anim_node)
@@ -156,6 +165,7 @@ function M.init(self, node, callback, params, anim_node)
self.start_pos = gui.get_position(self.anim_node)
self.params = params
self.hover = self.druid:new_hover(node, on_button_hover)
self.hover.on_mouse_hover:subscribe(on_button_mouse_hover)
self.click_zone = nil
self.is_repeated_started = false
self.last_pressed_time = 0

View File

@@ -3,7 +3,8 @@
--- Component events
-- @table Events
-- @tfield druid_event on_hover On hover callback
-- @tfield druid_event on_hover On hover callback (Touch pressed)
-- @tfield druid_event on_mouse_hover On mouse hover callback (Touch over without action_id)
local Event = require("druid.event")
local const = require("druid.const")
@@ -18,20 +19,24 @@ local M = component.create("hover", { const.ON_INPUT })
-- @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(on_hover_callback)
self.on_mouse_hover = Event()
end
function M.on_input(self, action_id, action)
if action_id ~= const.ACTION_TOUCH then
if action_id ~= const.ACTION_TOUCH and action_id ~= nil then
return
end
if not action_id and helper.is_mobile() then
return false
end
if not helper.is_enabled(self.node) then
return false
end
@@ -41,15 +46,17 @@ function M.on_input(self, action_id, action)
is_pick = is_pick and gui.pick_node(self.click_zone, action.x, action.y)
end
local hover_function = action_id and M.set_hover or M.set_mouse_hover
if not is_pick then
M.set_hover(self, false)
hover_function(self, false)
return false
end
if action.released then
M.set_hover(self, false)
hover_function(self, false)
else
M.set_hover(self, true)
hover_function(self, true)
end
end
@@ -69,6 +76,16 @@ function M.set_hover(self, state)
end
end
--- Set mouse hover state
-- @function hover:set_mouse_hover
-- @tparam bool state The mouse hover state
function M.set_mouse_hover(self, state)
if self._is_mouse_hovered ~= state then
self._is_mouse_hovered = state
self.on_mouse_hover:trigger(self:get_context(), state)
end
end
--- Strict hover click area. Useful for
-- no click events outside stencil node