2011-11-03 20:07:07 +01:00
|
|
|
ToolTip = {}
|
|
|
|
|
|
|
|
-- private variables
|
2012-02-05 23:42:35 +01:00
|
|
|
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
|
|
|
|
|
2012-02-05 23:42:35 +01:00
|
|
|
local pos = g_window.getMousePosition()
|
2011-11-03 20:07:07 +01:00
|
|
|
pos.y = pos.y + 1
|
2011-12-03 22:41:37 +01:00
|
|
|
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
|
2012-03-26 00:14:00 +02:00
|
|
|
if widget.tooltip and not Mouse.isPressed() then
|
2012-02-05 23:42:35 +01:00
|
|
|
ToolTip.display(widget.tooltip)
|
|
|
|
currentHoveredWidget = widget
|
|
|
|
end
|
2011-11-14 15:30:35 +01:00
|
|
|
else
|
2012-02-05 23:42:35 +01:00
|
|
|
if widget == currentHoveredWidget then
|
|
|
|
ToolTip:hide()
|
|
|
|
currentHoveredWidget = nil
|
|
|
|
end
|
2011-11-14 15:30:35 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-04 11:26:58 +01:00
|
|
|
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})
|
2012-02-20 03:27:08 +01:00
|
|
|
|
|
|
|
addEvent(function()
|
|
|
|
toolTipLabel = createWidget('Label', rootWidget)
|
|
|
|
toolTipLabel:setId('toolTip')
|
2012-03-23 02:52:31 +01:00
|
|
|
toolTipLabel:setBackgroundColor('#111111cc')
|
|
|
|
toolTipLabel:setTextAlign(AlignCenter)
|
2012-02-20 03:27:08 +01:00
|
|
|
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
|
2012-02-20 03:27:08 +01:00
|
|
|
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()
|
2012-02-07 20:21:53 +01:00
|
|
|
toolTipLabel:enable()
|
2012-02-06 20:19:47 +01:00
|
|
|
moveToolTip(toolTipLabel)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ToolTip.hide()
|
2012-02-20 03:27:08 +01:00
|
|
|
|
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
|
|
|
|
|
2012-02-20 03:27:08 +01:00
|
|
|
ToolTip.init()
|
|
|
|
connect(g_app, { onTerminate = ToolTip.terminate })
|