diff --git a/.vscode/settings.json b/.vscode/settings.json index 770671b..8ba31a8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,7 +9,8 @@ "before", "after", "it", - "utf8" + "utf8", + "defos" ], "Lua.workspace.checkThirdParty": false, "Lua.diagnostics.neededFileStatus": { diff --git a/druid/base/hover.lua b/druid/base/hover.lua index bbb7a0c..cd147f7 100644 --- a/druid/base/hover.lua +++ b/druid/base/hover.lua @@ -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 diff --git a/druid/base/text.lua b/druid/base/text.lua index 689b20c..d831309 100755 --- a/druid/base/text.lua +++ b/druid/base/text.lua @@ -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) diff --git a/druid/styles/default/style.lua b/druid/styles/default/style.lua index 7a61d0a..336efa4 100644 --- a/druid/styles/default/style.lua +++ b/druid/styles/default/style.lua @@ -54,6 +54,10 @@ M["button"] = { end } +M["hover"] = { + ON_HOVER_CURSOR = nil, + ON_MOUSE_HOVER_CURSOR = nil, +} M["drag"] = { DRAG_DEADZONE = 10, -- Size in pixels of drag deadzone diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index 6beeb16..c27d114 100755 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -590,9 +590,10 @@ end -- @tparam DruidInstance self -- @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_mouse_hover_callback Mouse hover callback -- @treturn Hover @{Hover} component -function DruidInstance.new_hover(self, node, on_hover_callback) - return DruidInstance.new(self, hover, 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, on_mouse_hover_callback) end