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

107 lines
2.4 KiB
Lua
Raw Normal View History

2012-06-26 00:13:30 +02:00
-- @docclass
g_tooltip = {}
2011-11-03 20:07:07 +01:00
-- private variables
local toolTipLabel
local currentHoveredWidget
2011-11-03 20:07:07 +01:00
-- private functions
2012-08-19 11:13:37 +02:00
local function moveToolTip(first)
if not first and (not toolTipLabel:isVisible() or toolTipLabel:getOpacity() < 0.1) then return end
2012-02-06 20:19:47 +01:00
local pos = g_window.getMousePosition()
2011-11-03 20:07:07 +01:00
pos.y = pos.y + 1
2012-08-19 11:13:37 +02:00
local xdif = g_window.getSize().width - (pos.x + toolTipLabel:getWidth())
2011-11-03 20:07:07 +01:00
if xdif < 2 then
2012-08-19 11:13:37 +02:00
pos.x = pos.x - toolTipLabel:getWidth() - 3
2011-11-03 20:07:07 +01:00
else
pos.x = pos.x + 10
end
2012-08-19 11:13:37 +02:00
toolTipLabel: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
2012-06-26 00:13:30 +02:00
if widget.tooltip and not g_mouse.isPressed() then
g_tooltip.display(widget.tooltip)
currentHoveredWidget = widget
end
2011-11-14 15:30:35 +01:00
else
if widget == currentHoveredWidget then
2012-06-26 00:13:30 +02:00
g_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
2012-06-26 00:13:30 +02:00
function g_tooltip.init()
2012-02-06 20:19:47 +01:00
connect(UIWidget, { onStyleApply = onWidgetStyleApply,
onHoverChange = onWidgetHoverChange})
addEvent(function()
2012-06-26 00:13:30 +02:00
toolTipLabel = g_ui.createWidget('UILabel', rootWidget)
toolTipLabel:setId('toolTip')
toolTipLabel:setBackgroundColor('#111111cc')
toolTipLabel:setTextAlign(AlignCenter)
toolTipLabel:hide()
2012-08-19 11:13:37 +02:00
toolTipLabel.onMouseMove = function() moveToolTip() end
end)
2012-02-06 20:19:47 +01:00
end
2012-06-26 00:13:30 +02:00
function g_tooltip.terminate()
2012-02-06 20:19:47 +01:00
disconnect(UIWidget, { onStyleApply = onWidgetStyleApply,
onHoverChange = onWidgetHoverChange })
currentHoveredWidget = nil
toolTipLabel:destroy()
toolTipLabel = nil
2012-02-08 00:06:52 +01:00
2012-06-26 00:13:30 +02:00
g_tooltip = nil
2012-02-06 20:19:47 +01:00
end
2012-06-26 00:13:30 +02:00
function g_tooltip.display(text)
2012-02-06 20:19:47 +01:00
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-06-26 00:13:30 +02:00
g_effects.fadeIn(toolTipLabel, 100)
2012-08-19 11:13:37 +02:00
moveToolTip(true)
2012-02-06 20:19:47 +01:00
end
2012-06-26 00:13:30 +02:00
function g_tooltip.hide()
g_effects.fadeOut(toolTipLabel, 100)
2012-02-06 20:19:47 +01:00
end
2011-11-14 15:30:35 +01:00
2012-06-26 00:13:30 +02:00
-- @docclass UIWidget @{
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:removeTooltip()
self.tooltip = nil
end
2011-11-14 15:30:35 +01:00
function UIWidget:getTooltip()
return self.tooltip
2011-11-14 03:40:18 +01:00
end
2012-06-26 00:13:30 +02:00
-- @}
g_tooltip.init()
connect(g_app, { onTerminate = g_tooltip.terminate })