Geyser tooltips

Share your scripts and packages with other Mudlet users.
Post Reply
davidwiththenicehat
Posts: 14
Joined: Tue Jul 02, 2019 4:29 pm

Geyser tooltips

Post by davidwiththenicehat » Fri Dec 27, 2019 3:09 pm

Provides the option for tooltips over geyser objects.

Usage:
call function `SetTooltip(labelNeedingToolTip, TooltipText)` where labelNeedingToolTips is the label you want to tooltip to appear over and TooltipText is the text you want to appear in the tooltip box.
Alternatively the function trackTooltip(TooltipText) can be placed into any event call.

The tooltip will appear just above and to the right of the mouse cursor. If there is no room above or to the right of the cursor it will correct and move the tooltip. I mention this because putting it into an event that does not directly involve the mouse, it will still appear where the mouse is.

Here is an example of a tooltip over a GUIframe tab.
Image

Origional code supplied from: https://github.com/Mudlet/Mudlet/issues/3247

Code: Select all

--References for this script
--label echo| https://www.mudlet.org/geyser/files/geyser/GeyserLabel.html
--calculate window width| https://wiki.mudlet.org/w/Manual:Lua_Functions#string.len.2C_utf8.len
--Timers https://wiki.mudlet.org/w/Manual:Timer_Engine

function configTooltips()
  if not tooltips then --if tooltips table has not been created
    tooltips = {}
    tooltips.fontSize = 10 --size of the font that will appear in the Tooltip
    tooltips.timeout = 2 --how long tip stays active after mouse leaves label
    tooltips.name = "Tooltip" --name of the label
    tooltips.fontfamily = "Arial"
    tooltips.styleSheet = [[
     background-color: "#aadddd99";
     border: 2px solid #555533;
     font-family: ]]..tooltips.fontfamily..[[;
     ]]
    local _, ttTextHeight = calcFontSize(tooltips.fontSize, tooltips.fontfamily) --get width of a single font character
    tooltips.height = ttTextHeight + 8 --Height of the tool tip.
  end --if not tooltips
end --function configTooltips()
if tooltips then
  tooltips[tooltips.name] = nil
  tooltips = nil
end --Reset project data on save, so changes above are reflected after saving.

--Apply message and resize label
local function resizeToolTiplabel(TooltipText)
  --Write message on tooltip label
  --tooltips[tooltips.name]:echo(TooltipText,"black","c"..tostring(tooltips.fontSize))
  tooltips[tooltips.name]:echo(TooltipText,"black","c"..tostring(tooltips.fontSize))
  local ttTextWidth, ttTextHeight = calcFontSize(tooltips.fontSize, tooltips.fontfamily) --get width of a single font character
  --local toolTextLabelWidth = ttTextWidth * (utf8.len(TooltipText) + 2) --calc width of the Tooltip label
  local toolTextLabelWidth = ttTextWidth * (utf8.len(TooltipText) + 2) --calc width of the Tooltip label
  tooltips[tooltips.name]:resize(toolTextLabelWidth, tooltips.height) --Resize tool tip to fit TooltipText
  
  if false then --set true for debug messaging. in error window.
    local Pos_X, Pos_Y = getMousePosition()
    local tmpMainWidth, tmpMainHeight = getMainWindowSize()
    debugc("function moveTooltip, Right side of tool tip label: "
      ..(Pos_X + tooltips[tooltips.name]:get_width())
      .." main window width: "..tmpMainWidth
      .." Pos_X: "..Pos_X.." tooltip label width: "..tooltips[tooltips.name]:get_width()
      .." Width the tooltip label should be: "..toolTextLabelWidth
      .."\n\tHeight of the tooltip should be: "..tooltips.height.." Height of the tooltip is: "..tooltips[tooltips.name]:get_height())
  end --if getTECDisplayDebugMode
end --function resizeToolTipLabel

local function moveTooltip(Pos_X, Pos_Y, TooltipText)
  if not Pos_X or not Pos_Y or not TooltipText then --if data collection error occured
    debugc("function, moveTooltip: data collection error.")
    return --end call function will have unwanted outcome.
  end --if not Pos_X or not Pos_Y or not TooltipText
  local tmpMainWidth, tmpMainHeight = getMainWindowSize()
  
  resizeToolTiplabel(TooltipText) --Apply message and resize label
 
  --move tooltip label. Preventing it from scrolling out of the mudlet window where it can not be drawn
  if Pos_Y > tooltips.height then --If the mouse is NOT at the top of the window
    --if the tooltip would go off the screen
    if Pos_X + tooltips[tooltips.name]:get_width() > tmpMainWidth then
      --if the tooltip would go off the right side of the screen
      Pos_X = Pos_X - tooltips[tooltips.name]:get_width()
    end --if Pos_X + tooltips[tooltips.name]:get_width() > tmpMainWidth
    tooltips[tooltips.name]:move(Pos_X+5, (Pos_Y-tooltips.height-5)) --position tooltip just above mouse
  else --if the mouse is at the top of the screen
    --if the tooltip would go off the right side of the screen
    if Pos_X + tooltips[tooltips.name]:get_width() > tmpMainWidth then
      --Draw tooltip label to the left of the cursor
      Pos_X = Pos_X - tooltips[tooltips.name]:get_width()
    end --if Pos_X + tooltips[tooltips.name]:get_width() > tmpMainWidth
    tooltips[tooltips.name]:move(Pos_X+5, (Pos_Y+tooltips.height+5)) --position tooltip just below the mouse
  end --if Pos_Y > tooltips.height else

  raiseWindow(tooltips.name) -- make certain Tooltip stays on top of other labels.
  showWindow(tooltips.name) --show tool tip
end --function moveTooltip

function trackTooltip(TooltipText)
  configTooltips() --configures tooltip table if it is not already
  local Pos_X, Pos_Y = getMousePosition()
  if tooltips.timer then --if the timer exists.
    killTimer(tooltips.timer) --delete the timer to reset it at end of this function
    tooltips.timer = nil
  end--if tooltips.timer
  
  if tooltips[tooltips.name] then --if the tool tip has previously been created
    moveTooltip(Pos_X, Pos_Y, TooltipText)
  else --if the tool window has not been created.
    --create tooltip label, we will reuse this same label by moving it and hiding it.
    tooltips[tooltips.name] = Geyser.Label:new({
      name = tooltips.name,
      x = Pos_X + 5,
      y = Pos_Y - tooltips.height,
      height = tooltips.height,
      fgColor = "#000000",
      })
    resizeToolTiplabel(TooltipText) --Apply message and resize label
    setLabelStyleSheet(tooltips.name, tooltips.styleSheet)
    enableClickthrough(tooltips.name) --Do not allow label to be clicked.
    raiseWindow(tooltips.name) -- make certain Tooltip stays on top of other labels.
  end --if Tooltip
tooltips.timer = tempTimer(tooltips.timeout, [[deleteTooltip()]]) --delete tip after x+1 seconds.
end --function createTooltip(TooltipText)

function deleteTooltip()
  hideWindow(tooltips.name) --hide the tooltip label.
  killTimer(tooltips.timer) --delete the completed timer.
  tooltips.timer = nil --remove the timer ID
end

--easy method to create tool tip timer.
function SetTooltip(labelNeedingToolTip, TooltipText)
  --labelNeedingToolTip is the name of the geyser label that needs a tooltip
  --TooltipText is text to put on the tooltip label

  --occurs when mouse hovers over geyser object that needs a tool tip.
  setLabelOnEnter(labelNeedingToolTip, "trackTooltip", TooltipText)
  --hide tooltip immediately after the mouse leaves label the tool tip is for.
  --setLabelOnLeave(geyserObjectNeedingToolTip, "DeleteTooltip")
  --track mouse movement when cursor is over label.
  setLabelMoveCallback(labelNeedingToolTip, "trackTooltip", TooltipText)
end

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests