#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

@ -72,4 +72,8 @@ Druid 0.4.0:
- _Grid_ anchor by default equals to node pivot (so, more gui settings in _.gui_ settings) - _Grid_ anchor by default equals to node pivot (so, more gui settings in _.gui_ settings)
- _Hover_ component now have two hover events:
- _on_hover_ is old hover event. Trigger only if touch pressed on node
- _on_mouse_hover_ action on node without action_id (desktop mouse over). Works only on desktop platform
- **Fix:** Blocker component bug (blocker had very high priority, so it's block even button components, created after bloker) - **Fix:** Blocker component bug (blocker had very high priority, so it's block even button components, created after bloker)

View File

@ -58,6 +58,15 @@ local function on_button_hover(self, hover_state)
end 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) local function on_button_click(self)
if self._style.on_click then if self._style.on_click then
self._style.on_click(self, self.anim_node) 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.start_pos = gui.get_position(self.anim_node)
self.params = params self.params = params
self.hover = self.druid:new_hover(node, on_button_hover) 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.click_zone = nil
self.is_repeated_started = false self.is_repeated_started = false
self.last_pressed_time = 0 self.last_pressed_time = 0

View File

@ -3,7 +3,8 @@
--- Component events --- Component events
-- @table 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 Event = require("druid.event")
local const = require("druid.const") local const = require("druid.const")
@ -18,20 +19,24 @@ local M = component.create("hover", { const.ON_INPUT })
-- @tparam node node Gui node -- @tparam node node Gui node
-- @tparam function on_hover_callback Hover callback -- @tparam function on_hover_callback Hover callback
function M.init(self, node, on_hover_callback) function M.init(self, node, on_hover_callback)
self.style = self:get_style()
self.node = self:get_node(node) self.node = self:get_node(node)
self._is_hovered = false self._is_hovered = false
self.on_hover = Event(on_hover_callback) self.on_hover = Event(on_hover_callback)
self.on_mouse_hover = Event()
end end
function M.on_input(self, action_id, action) 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 return
end end
if not action_id and helper.is_mobile() then
return false
end
if not helper.is_enabled(self.node) then if not helper.is_enabled(self.node) then
return false return false
end 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) is_pick = is_pick and gui.pick_node(self.click_zone, action.x, action.y)
end end
local hover_function = action_id and M.set_hover or M.set_mouse_hover
if not is_pick then if not is_pick then
M.set_hover(self, false) hover_function(self, false)
return false return false
end end
if action.released then if action.released then
M.set_hover(self, false) hover_function(self, false)
else else
M.set_hover(self, true) hover_function(self, true)
end end
end end
@ -69,6 +76,16 @@ function M.set_hover(self, state)
end end
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 --- Strict hover click area. Useful for
-- no click events outside stencil node -- no click events outside stencil node

View File

@ -6,6 +6,7 @@ local M = {}
M["button"] = { M["button"] = {
HOVER_SCALE = vmath.vector3(0.02, 0.02, 1), HOVER_SCALE = vmath.vector3(0.02, 0.02, 1),
HOVER_MOUSE_SCALE = vmath.vector3(0.01, 0.01, 1),
HOVER_TIME = 0.04, HOVER_TIME = 0.04,
SCALE_CHANGE = vmath.vector3(0.035, 0.035, 1), SCALE_CHANGE = vmath.vector3(0.035, 0.035, 1),
BTN_SOUND = "click", BTN_SOUND = "click",
@ -23,6 +24,13 @@ M["button"] = {
anims.hover_scale(self, target_scale, M.button.HOVER_TIME) anims.hover_scale(self, target_scale, M.button.HOVER_TIME)
end, end,
on_mouse_hover = function(self, node, state)
local scale_to = self.start_scale + M.button.HOVER_MOUSE_SCALE
local target_scale = state and scale_to or self.start_scale
anims.hover_scale(self, target_scale, M.button.HOVER_TIME)
end,
on_click = function(self, node) on_click = function(self, node)
local scale_to = self.start_scale + M.button.SCALE_CHANGE local scale_to = self.start_scale + M.button.SCALE_CHANGE
anims.tap_scale_animation(self, node, scale_to) anims.tap_scale_animation(self, node, scale_to)

View File

@ -8,13 +8,18 @@ M["button"] = {
ENABLED_COLOR = vmath.vector4(1), ENABLED_COLOR = vmath.vector4(1),
LONGTAP_TIME = 0.4, LONGTAP_TIME = 0.4,
DOUBLETAP_TIME = 0.4, DOUBLETAP_TIME = 0.4,
HOVER_IMAGE = "button_yellow", HOVER_MOUSE_IMAGE = "button_yellow",
DEFAULT_IMAGE = "button_blue", DEFAULT_IMAGE = "button_blue",
CLICK_IMAGE = "button_red", HOVER_IMAGE = "button_red",
on_hover = function(self, node, state) on_hover = function(self, node, state)
local anim = state and M.button.HOVER_IMAGE or M.button.DEFAULT_IMAGE local anim = state and M.button.HOVER_IMAGE or M.button.DEFAULT_IMAGE
gui.play_flipbook(node, anim) gui.play_flipbook(node, anim)
end,
on_mouse_hover = function(self, node, state)
local anim = state and M.button.HOVER_MOUSE_IMAGE or M.button.DEFAULT_IMAGE
gui.play_flipbook(node, anim)
end end
} }