Skip to content

Commit

Permalink
#49 add on_mouse_hover to hover component
Browse files Browse the repository at this point in the history
  • Loading branch information
Insality committed May 3, 2020
1 parent 75a1369 commit 0cc848b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
4 changes: 4 additions & 0 deletions docs_md/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 10 additions & 0 deletions druid/base/button.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
29 changes: 23 additions & 6 deletions druid/base/hover.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions druid/styles/default/style.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions druid/styles/sprites/style.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 0cc848b

Please sign in to comment.