2012-06-26 00:13:30 +02:00
|
|
|
-- @docclass
|
|
|
|
g_tooltip = {}
|
2011-11-03 20:07:07 +01:00
|
|
|
|
|
|
|
-- private variables
|
2012-02-05 23:42:35 +01:00
|
|
|
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
|
|
|
|
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
|
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)
|
2012-02-05 23:42:35 +01:00
|
|
|
currentHoveredWidget = widget
|
|
|
|
end
|
2011-11-14 15:30:35 +01:00
|
|
|
else
|
2012-02-05 23:42:35 +01:00
|
|
|
if widget == currentHoveredWidget then
|
2012-06-26 00:13:30 +02:00
|
|
|
g_tooltip.hide()
|
2012-02-05 23:42:35 +01:00
|
|
|
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
|
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})
|
2012-02-20 03:27:08 +01:00
|
|
|
|
|
|
|
addEvent(function()
|
2012-06-26 00:13:30 +02:00
|
|
|
toolTipLabel = g_ui.createWidget('UILabel', rootWidget)
|
2012-02-20 03:27:08 +01:00
|
|
|
toolTipLabel:setId('toolTip')
|
2012-03-23 02:52:31 +01:00
|
|
|
toolTipLabel:setBackgroundColor('#111111cc')
|
|
|
|
toolTipLabel:setTextAlign(AlignCenter)
|
2012-06-01 21:39:09 +02:00
|
|
|
toolTipLabel:hide()
|
2012-08-19 11:13:37 +02:00
|
|
|
toolTipLabel.onMouseMove = function() moveToolTip() end
|
2012-02-20 03:27:08 +01:00
|
|
|
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
|
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-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
|
|
|
|
|
2012-08-19 03:19:43 +02:00
|
|
|
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 })
|