Thread Tools Display Modes
Today, 09:48 AM   #1
Mathinator
Join Date: Oct 2024
Posts: 1
Question How to tooltip?

Hello everyone,

I'm new to addon developing. I have years of experience in .net and general application development but eso addons and pythong is new to me.

I started with a "simple" addon that would show a tooltip when hovering over an item with that item's id in it.

This is mainly because I want to create an addon based on Dolgubon's LazyAlchemyLearner bit expended upon it and also for enchanting glyph's. Since I would need Item Id's for this and I couldn't find a list online with Id's of items I thought making an addon that would display the Id's in a tooltip on hovering would help me a lot.

I almost got it to work.
I can display the name and the Id of an item in chat when hovering over that item in inventory or craftbag.
When trying to place it in a tooltip however things stop working. No error, just nothing showing up.

Code:
-- Addon Name
MyItemIDViewer = {}
MyItemIDViewer.name = "MyItemIDViewer"

local tooltipControl

-- Function to print item name and ID to the custom tooltip
local function ShowTooltip(itemName, itemId, mouseX, mouseY)
    tooltipControl:SetHidden(false)
    --tooltipControl:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, mouseX + 10, mouseY + 10) -- Position it near the mouse
	tooltipControl:SetAnchor(LEFT, GuiMouse, RIGHT, 32, 0)
	tooltipControl:SetText("test")
    --tooltipControl:SetText("Item Name: " .. itemName .. "\nItem ID: " .. itemId)
end

-- Function to hide the custom tooltip
local function HideTooltip()
    tooltipControl:SetHidden(true)
end

-- Hook function for mouse exit to hide the tooltip
local function OnMouseExit()
    HideTooltip()
end

-- Function to print item name and ID to chat
local function PrintItemInfo(bagId, slotId)
    -- Get the item name and item ID from the item link
    local itemLink = GetItemLink(bagId, slotId)
    local itemName = GetItemLinkName(itemLink)
    local itemId = GetItemLinkItemId(itemLink) -- Use this method to get the item ID from the item link

    -- Show the custom tooltip at the mouse position
    local mouseX, mouseY = GetUIMousePosition()
    ShowTooltip(itemName, itemId, mouseX, mouseY)
end

-- Hook function for when the tooltip is set
local function OnTooltipSetItem(tooltipControl, bagId, slotId)
    PrintItemInfo(bagId, slotId)
end
 
-- Hook into the tooltip functions
local function HookBagTips()
    ZO_PreHook(ItemTooltip, "SetBagItem", OnTooltipSetItem) -- Inventory bags
	ItemTooltip:SetHandler("OnMouseLeave", OnMouseExit) -- Hide tooltip on mouse leave
end
 
-- Function that runs when the addon is loaded
local function OnAddOnLoaded(event, name)
    if name ~= MyItemIDViewer.name then return end -- If it's not our addon loaded, don't run this. Would get errors.

	-- Create the tooltip control
    tooltipControl = WINDOW_MANAGER:CreateControl("MyItemIDViewerTooltip", GuiRoot, CT_LABEL)
	tooltipControl:SetFont("ZoFontHeader")
	tooltipControl:SetColor(1, 1, 1, 1)
	tooltipControl:SetText("Test Text")
	tooltipControl:SetDimensions(300, 100)
	tooltipControl:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, 800, 800)
	tooltipControl:SetDrawLayer(DL_OVERLAY)
	tooltipControl:SetHidden(false)

    HookBagTips()
    EVENT_MANAGER:UnregisterForEvent(MyItemIDViewer.name, event) -- We no longer want this to run as the game loads addons. It only needs to run once.
end
 
-- Register for the event when the addon is loaded
EVENT_MANAGER:RegisterForEvent(MyItemIDViewer.name, EVENT_ADD_ON_LOADED, OnAddOnLoaded) -- As the game loads, the event manager registers our addon for an event.
The tooltipControl code in OnAddOnLoaded are for testing purposes, but nothing is shown on screen.
Could someone push me in the right direction?

Kind Regards,
Mathinator

Last edited by Mathinator : Today at 09:48 AM. Reason: forgot to mention it only worked in chat
  Reply With Quote
Today, 12:52 PM   #2
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,190
You need to create a TopLevelControl fist and then add (parent) your tooltip control (label) to it!
Then you can hide and show that TLC as tooltip control.
https://github.com/esoui/esoui/blob/...balvars.lua#L9

Lua Code:
  1. -- Create a top-level window.
  2.     local myAddonTLC = WINDOW_MANAGER:CreateTopLevelWindow()
  3.     myAddonTLC:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, 0, 0) --leave it at 0 width and height so it does not disturb the mouse if not used! ResizeToFitDescendents should make it resize to your label size so update the lable size when it shows and the TLC resizes with it
  4.     myAddonTLC:SetResizeToFitDescendents(true)
  5.     myAddonTLC:SetHidden(true)
  6.  
  7.     local tooltipControl = WINDOW_MANAGER:CreateControl("MyItemIDViewerTooltip", myAddonTLC, CT_LABEL)
  8.  
  9.    --At your ShowTooltip() func
  10.    myAddonTLC:SetHidden(false)
  11.    tooltipControl:SetWidth(neededWidth)
  12.    tooltipControl:SetHeight(neededHeight)


You cannot just create a label control and show and hide it without an underlaying TLC or other controls that belong to a TLC.

Another idea but I guess the missing TLC is the reason here:
Most probably your PreHook here is calling your code, showing the tooltip but then hides it again as the original function "SetBagItem" is called after your "pre hook"?

ZO_PreHook(ItemTooltip, "SetBagItem", OnTooltipSetItem) -- Inventory bags
Try to change that to a SecurePostHook, maybe that shows the normal tooltip then and your tooltip in addition.
Add debug messages d("got here") to see how far your code get's executed and see what works, and what is never called.

Also try to add:
tooltipControl:ClearAnchors()
as first line of your ShowTooltip function

Last edited by Baertram : Today at 01:01 PM.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » How to tooltip?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off