tibia-client/modules/core_lib/ui/tooltip.lua

96 lines
2.1 KiB
Lua
Raw Normal View History

2011-11-03 20:07:07 +01:00
ToolTip = {}
-- private variables
local toolTipLabel
local currentHoveredWidget
2011-11-03 20:07:07 +01:00
-- private functions
local function moveToolTip(tooltip)
2012-02-06 20:19:47 +01:00
if not tooltip:isVisible() then return end
local pos = g_window.getMousePosition()
2011-11-03 20:07:07 +01:00
pos.y = pos.y + 1
local xdif = g_window.getSize().width - (pos.x + tooltip:getWidth())
2011-11-03 20:07:07 +01:00
if xdif < 2 then
pos.x = pos.x - tooltip:getWidth() - 3
else
pos.x = pos.x + 10
end
2012-01-30 01:00:12 +01:00
tooltip:setPosition(pos)
2011-11-03 20:07:07 +01:00
end
2011-11-14 15:30:35 +01:00
local function onWidgetHoverChange(widget, hovered)
if hovered then
if widget.tooltip and not Mouse.isPressed() then
ToolTip.display(widget.tooltip)
currentHoveredWidget = widget
end
2011-11-14 15:30:35 +01:00
else
if widget == currentHoveredWidget then
ToolTip:hide()
currentHoveredWidget = nil
end
2011-11-14 15:30:35 +01:00
end
end
local function onWidgetStyleApply(widget, styleName, styleNode)
if styleNode.tooltip then
widget.tooltip = styleNode.tooltip
2011-11-14 16:01:09 +01:00
end
2011-11-14 15:30:35 +01:00
end
2012-02-06 20:19:47 +01:00
-- public functions
function ToolTip.init()
connect(UIWidget, { onStyleApply = onWidgetStyleApply,
onHoverChange = onWidgetHoverChange})
addEvent(function()
toolTipLabel = createWidget('Label', rootWidget)
toolTipLabel:setId('toolTip')
toolTipLabel:setBackgroundColor('#111111cc')
toolTipLabel:setTextAlign(AlignCenter)
toolTipLabel.onMouseMove = moveToolTip
end)
2012-02-06 20:19:47 +01:00
end
function ToolTip.terminate()
disconnect(UIWidget, { onStyleApply = onWidgetStyleApply,
onHoverChange = onWidgetHoverChange })
currentHoveredWidget = nil
toolTipLabel:destroy()
toolTipLabel = nil
2012-02-08 00:06:52 +01:00
ToolTip = nil
2012-02-06 20:19:47 +01:00
end
function ToolTip.display(text)
if text == nil then return end
if not toolTipLabel then return end
2012-02-06 20:19:47 +01:00
toolTipLabel:setText(text)
toolTipLabel:resizeToText()
toolTipLabel:resize(toolTipLabel:getWidth() + 4, toolTipLabel:getHeight() + 4)
toolTipLabel:show()
toolTipLabel:raise()
toolTipLabel:enable()
2012-02-06 20:19:47 +01:00
moveToolTip(toolTipLabel)
end
function ToolTip.hide()
2012-02-06 20:19:47 +01:00
toolTipLabel:hide()
end
2011-11-14 15:30:35 +01:00
2011-11-14 16:01:09 +01:00
-- UIWidget extensions
2011-11-14 03:40:18 +01:00
function UIWidget:setTooltip(text)
2011-11-14 15:30:35 +01:00
self.tooltip = text
end
function UIWidget:getTooltip()
return self.tooltip
2011-11-14 03:40:18 +01:00
end
ToolTip.init()
connect(g_app, { onTerminate = ToolTip.terminate })