tibia-client/modules/core_widgets/tooltip/tooltip.lua

74 lines
1.6 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)
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
-- public functions
function ToolTip.display(text)
if text == nil then return end
ToolTip.hide()
toolTipLabel = createWidget('Label', rootWidget)
toolTipLabel:setId('toolTip')
toolTipLabel:setBackgroundColor('#111111bb')
toolTipLabel:setText(text)
toolTipLabel:resizeToText()
toolTipLabel:resize(toolTipLabel:getWidth() + 4, toolTipLabel:getHeight() + 4)
toolTipLabel.onMouseMove = moveToolTip
moveToolTip(toolTipLabel)
2011-11-03 20:07:07 +01:00
end
function ToolTip.hide()
if toolTipLabel then
toolTipLabel:destroy()
toolTipLabel = nil
2011-11-03 20:07:07 +01:00
end
end
2011-11-03 23:28:10 +01:00
2011-11-14 16:01:09 +01:00
-- UIWidget hooks
2011-11-14 15:30:35 +01:00
local function onWidgetHoverChange(widget, hovered)
if hovered then
if widget.tooltip 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-01-17 06:36:25 +01:00
connect(UIWidget, { onStyleApply = onWidgetStyleApply,
onHoverChange = onWidgetHoverChange})
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