mirror of
https://github.com/Insality/druid.git
synced 2025-09-27 10:02:18 +02:00
Add hover on_mouse_hover callback in constructor
Add ON_HOVER_CURSOR and ON_MOUSE_HOVER_CURSOR in hover component in styles
This commit is contained in:
@@ -28,7 +28,8 @@ local Hover = component.create("hover")
|
||||
-- @tparam Hover self @{Hover}
|
||||
-- @tparam node node Gui node
|
||||
-- @tparam function on_hover_callback Hover callback
|
||||
function Hover.init(self, node, on_hover_callback)
|
||||
-- @tparam function on_mouse_hover On mouse hover callback
|
||||
function Hover.init(self, node, on_hover_callback, on_mouse_hover)
|
||||
self.node = self:get_node(node)
|
||||
|
||||
self._is_hovered = false
|
||||
@@ -37,7 +38,7 @@ function Hover.init(self, node, on_hover_callback)
|
||||
self._is_mobile = helper.is_mobile()
|
||||
|
||||
self.on_hover = Event(on_hover_callback)
|
||||
self.on_mouse_hover = Event()
|
||||
self.on_mouse_hover = Event(on_mouse_hover)
|
||||
end
|
||||
|
||||
|
||||
@@ -51,6 +52,19 @@ function Hover.on_late_init(self)
|
||||
end
|
||||
|
||||
|
||||
--- Component style params.
|
||||
-- You can override this component styles params in druid styles table
|
||||
-- or create your own style
|
||||
-- @table style
|
||||
-- @tfield[opt] string ON_HOVER_CURSOR Mouse hover style on node hover
|
||||
-- @tfield[opt] string ON_MOUSE_HOVER_CURSOR Mouse hover style on node mouse hover
|
||||
function Hover.on_style_change(self, style)
|
||||
self.style = {}
|
||||
self.style.ON_HOVER_CURSOR = style.ON_HOVER_CURSOR or nil
|
||||
self.style.ON_MOUSE_HOVER_CURSOR = style.ON_MOUSE_HOVER_CURSOR or nil
|
||||
end
|
||||
|
||||
|
||||
function Hover.on_input(self, action_id, action)
|
||||
if action_id ~= const.ACTION_TOUCH and action_id ~= nil then
|
||||
return false
|
||||
@@ -97,6 +111,10 @@ function Hover.set_hover(self, state)
|
||||
if self._is_hovered ~= state then
|
||||
self._is_hovered = state
|
||||
self.on_hover:trigger(self:get_context(), state, self)
|
||||
|
||||
if defos and self.style.ON_HOVER_CURSOR then
|
||||
self:_set_cursor(3, state and self.style.ON_HOVER_CURSOR or nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -116,6 +134,10 @@ function Hover.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, self)
|
||||
|
||||
if defos and self.style.ON_MOUSE_HOVER_CURSOR then
|
||||
self:_set_cursor(2, state and self.style.ON_MOUSE_HOVER_CURSOR or nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -164,4 +186,31 @@ function Hover.is_enabled(self)
|
||||
end
|
||||
|
||||
|
||||
-- Internal cursor stack
|
||||
local cursor_stack = {}
|
||||
function Hover:_set_cursor(priority, cursor)
|
||||
if not defos then
|
||||
return
|
||||
end
|
||||
|
||||
local uid = self:get_uid()
|
||||
cursor_stack[uid] = cursor_stack[uid] or {}
|
||||
cursor_stack[uid][priority] = cursor
|
||||
|
||||
-- set cursor with high priority via pairs
|
||||
local priority = nil
|
||||
local cursor_to_set = nil
|
||||
for _, stack in pairs(cursor_stack) do
|
||||
for priority, _ in pairs(stack) do
|
||||
if priority > (priority or 0) then
|
||||
priority = priority
|
||||
cursor_to_set = stack[priority]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defos.set_cursor(cursor_to_set)
|
||||
end
|
||||
|
||||
|
||||
return Hover
|
||||
|
@@ -98,12 +98,18 @@ end
|
||||
|
||||
--- Reset initial scale for text
|
||||
local function reset_default_scale(self)
|
||||
self.scale = self.start_scale
|
||||
self.scale.x = self.start_scale.x
|
||||
self.scale.y = self.start_scale.y
|
||||
self.scale.z = self.start_scale.z
|
||||
gui.set_scale(self.node, self.start_scale)
|
||||
gui.set_size(self.node, self.start_size)
|
||||
end
|
||||
|
||||
|
||||
local function is_fit_info_area(self, metrics)
|
||||
return metrics.width * self.scale.x <= self.text_area.x and
|
||||
metrics.height * self.scale.y <= self.text_area.y
|
||||
end
|
||||
--- Setup scale x, but can only be smaller, than start text scale
|
||||
local function update_text_area_size(self)
|
||||
reset_default_scale(self)
|
||||
|
Reference in New Issue
Block a user