No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tooltip.lua 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. -- @docclass
  2. g_tooltip = {}
  3. -- private variables
  4. local toolTipLabel
  5. local currentHoveredWidget
  6. -- private functions
  7. local function moveToolTip(first)
  8. if not first and (not toolTipLabel:isVisible() or toolTipLabel:getOpacity() < 0.1) then return end
  9. local pos = g_window.getMousePosition()
  10. local windowSize = g_window.getSize()
  11. local labelSize = toolTipLabel:getSize()
  12. pos.x = pos.x + 1
  13. pos.y = pos.y + 1
  14. if windowSize.width - (pos.x + labelSize.width) < 10 then
  15. pos.x = pos.x - labelSize.width - 3
  16. else
  17. pos.x = pos.x + 10
  18. end
  19. if windowSize.height - (pos.y + labelSize.height) < 10 then
  20. pos.y = pos.y - labelSize.height - 3
  21. else
  22. pos.y = pos.y + 10
  23. end
  24. toolTipLabel:setPosition(pos)
  25. end
  26. local function onWidgetHoverChange(widget, hovered)
  27. if hovered then
  28. if widget.tooltip and not g_mouse.isPressed() then
  29. g_tooltip.display(widget.tooltip)
  30. currentHoveredWidget = widget
  31. end
  32. else
  33. if widget == currentHoveredWidget then
  34. g_tooltip.hide()
  35. currentHoveredWidget = nil
  36. end
  37. end
  38. end
  39. local function onWidgetStyleApply(widget, styleName, styleNode)
  40. if styleNode.tooltip then
  41. widget.tooltip = styleNode.tooltip
  42. end
  43. end
  44. -- public functions
  45. function g_tooltip.init()
  46. connect(UIWidget, { onStyleApply = onWidgetStyleApply,
  47. onHoverChange = onWidgetHoverChange})
  48. addEvent(function()
  49. toolTipLabel = g_ui.createWidget('UILabel', rootWidget)
  50. toolTipLabel:setId('toolTip')
  51. toolTipLabel:setBackgroundColor('#111111cc')
  52. toolTipLabel:setTextAlign(AlignCenter)
  53. toolTipLabel:hide()
  54. toolTipLabel.onMouseMove = function() moveToolTip() end
  55. end)
  56. end
  57. function g_tooltip.terminate()
  58. disconnect(UIWidget, { onStyleApply = onWidgetStyleApply,
  59. onHoverChange = onWidgetHoverChange })
  60. currentHoveredWidget = nil
  61. toolTipLabel:destroy()
  62. toolTipLabel = nil
  63. g_tooltip = nil
  64. end
  65. function g_tooltip.display(text)
  66. if text == nil or text:len() == 0 then return end
  67. if not toolTipLabel then return end
  68. toolTipLabel:setText(text)
  69. toolTipLabel:resizeToText()
  70. toolTipLabel:resize(toolTipLabel:getWidth() + 4, toolTipLabel:getHeight() + 4)
  71. toolTipLabel:show()
  72. toolTipLabel:raise()
  73. toolTipLabel:enable()
  74. g_effects.fadeIn(toolTipLabel, 100)
  75. moveToolTip(true)
  76. end
  77. function g_tooltip.hide()
  78. g_effects.fadeOut(toolTipLabel, 100)
  79. end
  80. -- @docclass UIWidget @{
  81. -- UIWidget extensions
  82. function UIWidget:setTooltip(text)
  83. self.tooltip = text
  84. end
  85. function UIWidget:removeTooltip()
  86. self.tooltip = nil
  87. end
  88. function UIWidget:getTooltip()
  89. return self.tooltip
  90. end
  91. -- @}
  92. g_tooltip.init()
  93. connect(g_app, { onTerminate = g_tooltip.terminate })