nehegeb
nehegeb

Reputation: 193

WoW Addon - Dynamic tooltip while key is pressed

I've got a tooltip and I want it to show more detailed information if the SHIFT key is pressed/held. The following code works so far:

function myAddonFrame_OnEnter(self)
    myAddon_GenerateTooltip(self)
end

function myAddonFrame_OnLeave(self)
    GameTooltip:Hide()
end

function myAddon_GenerateTooltip(self)
    GameTooltip:SetOwner(self or UIParent, "ANCHOR_LEFT")
    GameTooltip:SetText(“myAddon”, 0.7, 0, 1)
    GameTooltip:AddLine(" ", 1, 1, 1)
    if IsShiftKeyDown() then
        -- Show some more details, if the SHIFT key is pressed.
        GameTooltip:AddLine(“Some detailed stuff about god and the world.”, 1, 1, 1)
        GameTooltip:AddLine(“Even more great stuff to read.”, 0.9, 0.8, 0.1, 1)
    else
        -- Basic information to be displayed when no button is pressed.
        GameTooltip:AddLine(“Some basic information. Yaaay!“, 1, 1, 1)
        GameTooltip:AddLine(" ", 1, 1, 1)
        GameTooltip:AddLine(“Hold SHIFT for more information…”, 0.5, 0.5, 0.5)
    end
    GameTooltip:Show()
end

But this only works if the SHIFT key is held before the mouse hovers the myAddonFrame. It also will display the detailed information even after releasing SHIFT as long as the mouse cursor remains on the myAddonFrame. But I want it dynamic!

My question here is:
How can I make the tooltip to refresh itself according to the SHIFT key when the mouse cursor stays on the frame?
I have in mind something like when I hover over an item in WoW and only as long as I hold the SHIFT key, that item will be compared with what I currently wear. I want exactly this effect, just within the same tooltip.

I hope somebody can push me in the right direction. I have tried using MODIFIER_STATE_CHANGED but I didn't get it working.

Oh, and while we are talking about toolips...here's a bonus question:
Is it possible to format them a bit? Bold, italics, fontsize, colorchanges for a single word in a line, etc. ? I didn't find anything particular here (wowprogramming.com), but maybe I overlooked it.

--- SOLVED ---
For the ones interested in the solution:

I used the code snippet of Nathanyel and adjusted it a bit. Because I use one XML file for all the frames and one corresponding LUA file for just the functions, I didn't wanted to create a new frame there. But I figgured out a way to use the existing myAddonFrame for the dynamic tooltip as well.
My code from above works perfectly and I just had to add the following:

-- [ALL THE CODE ABOVE!]

function myAddonFrame_OnLoad(frame)
    frame:RegisterEvent("MODIFIER_STATE_CHANGED")   -- Needed for the dynamic tool tip.
end

function myAddonFrame_OnEvent(frame, event, ...)
    -- Fired, when any keyboard key is pressed.
    local key, state = select(1, ...)
    if (event == "MODIFIER_STATE_CHANGED") then
        -- Switch the dynamic tooltip when the SHIFT key is held.
        if myAddonFrame:IsMouseOver() and ((key == "LSHIFT") or (key == "RSHIFT")) then
            myAddon_GenerateTooltip(frame)
        end
    end
end

That's it! Thanks to Nathanyel for the food for thought. :)

Upvotes: 3

Views: 2298

Answers (1)

Nathanyel
Nathanyel

Reputation: 424

That event is indeed the key, but as you need to register it to a frame, preferably the one affected by the code, you should use a new frame for your tooltip:

local myFrame = CreateFrame("GameTooltip","myFrame",UIParent,"GameTooltipTemplate")

myFrame:SetScript("OnEvent",function(self, event, arg, ...)
  if myFrame:IsShown()
  and event == "MODIFIER_STATE_CHANGED"
  and (arg = "LSHIFT" or arg = "RSHIFT") then
    myAddon_GenerateTooltip() -- might need a parameter
  end
end

myFrame:RegisterEvent("MODIFIER_STATE_CHANGED")

This simply re-populates the tooltip when either Shift key is pressed, and your function can react to the new state of the key.
Napkin code, slash some copy&paste from an addon where I used this method, so it might not be perfect, but enough to convey the concept.

Upvotes: 1

Related Questions