WoW:Tooltip Pseudo Code

From AddOn Studio
Jump to navigation Jump to search

Tooltips are notoriously difficult to handle in WoW UI code. This page contains Lua-style pseudo code that attempts to represent the behaviour and interaction of various GameTooltip widget methods. (Currently it's not complete, yet to include details about line-wrapping behaviour). See UIOBJECT GameTooltip for notes about GameTooltips. See Widget API for a reference list about GameTooltip methods and their uses.

Note: This is not actual working Lua code, you can't copy/paste it. It is a representation of what the game's engine does internally, to help understand tooltips' behaviour. No such Lua code actually exists anywhere.

(Valid for patch 1.10)

 -- Hiding a tooltip's parent causes tooltip:ClearTooltip() to be called
 -- Showing a tooltip's parent causes tooltip:Show() to be called
 
 tooltip = {
     owner = nil;
     anchor = nil;
     fading = nil;
     numLines = 0;
 }
 
 -- Note: this function is not accessible via the Widget API, only available internally
 function tooltip:ClearTooltip()
     self.owner = nil;
     self.fading = nil;
     self:ClearLines();
 end
 
 function tooltip:SetOwner(owner, anchor)
     self:Hide()
     self:ClearAllPoints();
     
     self.owner = owner;
     self.anchor = anchor;
 end
 
 function tooltip:SetX(info)
     if (not self.owner) then
         self:Hide();
         return;
     end
 
     -- Note: CheckValidity() varies slightly between SetX methods
     -- * Empty Bag slots are considered invalid
     -- * Empty Inventory are considered invalid
     -- * Empty Actions *are* considered valid, and therefore clear the lines of the tooltip
     -- details on others not researched...
     if (CheckValidity(info)) then
         self:ClearLines();
         FillInTooltipData(info);    -- Fill in TextLeftX and TextRightX fontstrings
         self.numLines = NumberOfFilledInLines();
         if (self.numLines > 0) then
             self:Show();
         end
     end
 end
 
 function tooltip:Show()
     if (not self.owner) then
         MakeTooltipNotShown();    -- Hides the tooltip, IsShown returns 0.
         return;
     end
     
     if (self.numLines > 0) then
         if (self.anchor ~= "ANCHOR_NONE") then
             self:ClearAllPoints();
             SetPointsBasedOnAnchor(anchor);    -- Call SetPoint appropriate to the anchoring
         elseif (NoAnchorPointExists())    -- Check if any points have been set yet
             -- Note that this SetPoint with the infinite x-offset is responsible for making a tooltip
             -- "hidden" in the sense of not appearing on-screen, despite both IsVisible/IsShown returning 1.
             -- It also causes the tooltip to appear corrupt on screen if SetPoint is explicitly called on
             -- it with anything other than "TOPLEFT" after this, because of conflicting Points constraints.
             -- In all cases the tooltip's fontstrings can still be read properly.
             self:SetPoint("TOPLEFT", UIParent, 1.#INF, 0);
         end
         LayoutTooltip();    -- Resize tooltip based on shown fontstrings
         MakeTooltipShown();    -- Shows the tooltip, IsShown returns 1
         self.fading = nil;
     else
         self.owner = nil;
         MakeTooltipNotShown();    -- Hides the tooltip, IsShown returns 0
     end
 end
 
 function tooltip:Hide()
     self:ClearTooltip();
     MakeTooltipNotShown();    -- Hides the tooltip, IsShown returns 0
 end
 
 function tooltip:FadeOut()
     self.fading = true;
     GraduallyStartFadingOutTooltip();
     if (TimeIsUp() and self.fading) then
         self:Hide();
     end
 end
 
 function tooltip:AppendText(text, etc)
     AddToFirstLine(text)    -- Appends the text to the first line
     self:Show()
 end
 
 function tooltip:AddLine(text, etc)
     AddExtraLine(text, etc);    -- Fills out the next TextLefti and show it
     self.numLines = self.numLines + 1;
 end
 
 function tooltip:AddDoubleLine(data)
     AddExtraLineLeftRight(data);    -- Fills out the next TextLefti and TextRighti and show them
     self.numLines = self.numLines + 1;
 end
 
 function tooltip:ClearLines()
     for i=1, MAX do
         tooltipTextLefti:Hide();
         tooltipTextLefti:SetText(nil);
         tooltipTextRighti:Hide();
     end
     self.numLines = 0;
 end
 
 function tooltip:NumLines()
     return self.numLines;
 end
 
 function tooltip:IsOwned(frame)
     if (frame == self.owner) then
         return 1;
     end
 end