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:
Insality 2024-09-04 01:58:20 +03:00
parent 4a095a2a24
commit f93d0c7d40
5 changed files with 67 additions and 6 deletions

View File

@ -9,7 +9,8 @@
"before", "before",
"after", "after",
"it", "it",
"utf8" "utf8",
"defos"
], ],
"Lua.workspace.checkThirdParty": false, "Lua.workspace.checkThirdParty": false,
"Lua.diagnostics.neededFileStatus": { "Lua.diagnostics.neededFileStatus": {

View File

@ -28,7 +28,8 @@ local Hover = component.create("hover")
-- @tparam Hover self @{Hover} -- @tparam Hover self @{Hover}
-- @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 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.node = self:get_node(node)
self._is_hovered = false self._is_hovered = false
@ -37,7 +38,7 @@ function Hover.init(self, node, on_hover_callback)
self._is_mobile = helper.is_mobile() self._is_mobile = helper.is_mobile()
self.on_hover = Event(on_hover_callback) self.on_hover = Event(on_hover_callback)
self.on_mouse_hover = Event() self.on_mouse_hover = Event(on_mouse_hover)
end end
@ -51,6 +52,19 @@ function Hover.on_late_init(self)
end 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) function Hover.on_input(self, action_id, action)
if action_id ~= const.ACTION_TOUCH and action_id ~= nil then if action_id ~= const.ACTION_TOUCH and action_id ~= nil then
return false return false
@ -97,6 +111,10 @@ function Hover.set_hover(self, state)
if self._is_hovered ~= state then if self._is_hovered ~= state then
self._is_hovered = state self._is_hovered = state
self.on_hover:trigger(self:get_context(), state, self) 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
end end
@ -116,6 +134,10 @@ function Hover.set_mouse_hover(self, state)
if self._is_mouse_hovered ~= state then if self._is_mouse_hovered ~= state then
self._is_mouse_hovered = state self._is_mouse_hovered = state
self.on_mouse_hover:trigger(self:get_context(), state, self) 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
end end
@ -164,4 +186,31 @@ function Hover.is_enabled(self)
end 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 return Hover

View File

@ -98,12 +98,18 @@ end
--- Reset initial scale for text --- Reset initial scale for text
local function reset_default_scale(self) 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_scale(self.node, self.start_scale)
gui.set_size(self.node, self.start_size) gui.set_size(self.node, self.start_size)
end 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 --- Setup scale x, but can only be smaller, than start text scale
local function update_text_area_size(self) local function update_text_area_size(self)
reset_default_scale(self) reset_default_scale(self)

View File

@ -54,6 +54,10 @@ M["button"] = {
end end
} }
M["hover"] = {
ON_HOVER_CURSOR = nil,
ON_MOUSE_HOVER_CURSOR = nil,
}
M["drag"] = { M["drag"] = {
DRAG_DEADZONE = 10, -- Size in pixels of drag deadzone DRAG_DEADZONE = 10, -- Size in pixels of drag deadzone

View File

@ -590,9 +590,10 @@ end
-- @tparam DruidInstance self -- @tparam DruidInstance self
-- @tparam string|node node The node_id or gui.get_node(node_id) -- @tparam string|node node The node_id or gui.get_node(node_id)
-- @tparam function|nil on_hover_callback Hover callback -- @tparam function|nil on_hover_callback Hover callback
-- @tparam function|nil on_mouse_hover_callback Mouse hover callback
-- @treturn Hover @{Hover} component -- @treturn Hover @{Hover} component
function DruidInstance.new_hover(self, node, on_hover_callback) function DruidInstance.new_hover(self, node, on_hover_callback, on_mouse_hover_callback)
return DruidInstance.new(self, hover, node, on_hover_callback) return DruidInstance.new(self, hover, node, on_hover_callback, on_mouse_hover_callback)
end end