mirror of
https://github.com/Insality/druid
synced 2025-06-27 10:27:48 +02:00
#49 add on_mouse_hover to hover component
This commit is contained in:
parent
75a1369997
commit
0cc848b53f
@ -72,4 +72,8 @@ Druid 0.4.0:
|
||||
|
||||
- _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)
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -6,6 +6,7 @@ local M = {}
|
||||
|
||||
M["button"] = {
|
||||
HOVER_SCALE = vmath.vector3(0.02, 0.02, 1),
|
||||
HOVER_MOUSE_SCALE = vmath.vector3(0.01, 0.01, 1),
|
||||
HOVER_TIME = 0.04,
|
||||
SCALE_CHANGE = vmath.vector3(0.035, 0.035, 1),
|
||||
BTN_SOUND = "click",
|
||||
@ -23,6 +24,13 @@ M["button"] = {
|
||||
anims.hover_scale(self, target_scale, M.button.HOVER_TIME)
|
||||
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)
|
||||
local scale_to = self.start_scale + M.button.SCALE_CHANGE
|
||||
anims.tap_scale_animation(self, node, scale_to)
|
||||
|
@ -8,13 +8,18 @@ M["button"] = {
|
||||
ENABLED_COLOR = vmath.vector4(1),
|
||||
LONGTAP_TIME = 0.4,
|
||||
DOUBLETAP_TIME = 0.4,
|
||||
HOVER_IMAGE = "button_yellow",
|
||||
HOVER_MOUSE_IMAGE = "button_yellow",
|
||||
DEFAULT_IMAGE = "button_blue",
|
||||
CLICK_IMAGE = "button_red",
|
||||
HOVER_IMAGE = "button_red",
|
||||
|
||||
on_hover = function(self, node, state)
|
||||
local anim = state and M.button.HOVER_IMAGE or M.button.DEFAULT_IMAGE
|
||||
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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user