View Single Post
01-17-15, 01:11 AM   #23
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
I tried to make it behave as consistently as I could between the real tooltip and the pseudo-tooltips:
Code:
local tooltipForLinkType = {
    -- Normal tooltip things:
    achievement  = ItemRefTooltip,
    enchant      = ItemRefTooltip,
    glyph        = ItemRefTooltip,
    item         = ItemRefTooltip,
    instancelock = ItemRefTooltip,
    quest        = ItemRefTooltip,
    spell        = ItemRefTooltip,
    talent       = ItemRefTooltip,
    unit         = ItemRefTooltip,
    currency     = ItemRefTooltip,
    -- Special tooltip things:
    battlepet           = FloatingBattlePetTooltip,
    battlePetAbil       = FloatingPetBattleAbilityTooltip,
    garrfollowerability = FloatingGarrisonFollowerAbilityTooltip,
    garrfollower        = FloatingGarrisonFollowerTooltip,
    garrmission         = FloatingGarrisonMissionTooltip,
}

local allowReposition, data = true, { }

local function RepositionTooltip(tooltip)
    local button = GetMouseFocus()
    if button:IsObjectType('HyperLinkButton') then
        data.tooltip, data.point, data.relFrame, data.relPoint, data.x, data.y = tooltip, tooltip:GetPoint()
        local uiX, uiY = UIParent:GetCenter()
        local x, y = button:GetCenter()
        tooltip:ClearAllPoints()
        if x <= uiX then
            if y <= uiY then
                tooltip:SetPoint('BOTTOMLEFT', button, 'TOPRIGHT', 2, -4)
            else
                tooltip:SetPoint('TOPLEFT', button, 'BOTTOMRIGHT', 2, 0)
            end
        elseif y <= uiY then
            tooltip:SetPoint('BOTTOMRIGHT', button, 'TOPLEFT', 0, -4)
        else
            tooltip:SetPoint('TOPRIGHT', button, 'BOTTOMLEFT', 0, 0)
        end
        if tooltip.CloseButton then
            tooltip.CloseButton:Hide()
        end
    end
end

local function RestoreTooltip(tooltip)
    if tooltip and tooltip == data.tooltip then
        data.tooltip = nil
        tooltip:ClearAllPoints()
        tooltip:SetPoint(data.point, data.relFrame, data.relPoint, data.x, data.y)
        if tooltip.CloseButton then
            tooltip.CloseButton:Show()
        end
        return tooltip
    end
end

local function OnHyperlinkEnter(frame, linkData, link)
    local tooltip = tooltipForLinkType[linkData:match("^(.-):")]
    if tooltip then
        tooltip:Hide()
        SetItemRef(linkData, link, 'LeftButton', frame)
        if allowReposition then
            RepositionTooltip(tooltip)
        end
    end
end

local function OnHyperlinkLeave(frame, linkData, link)
    local tooltip = RestoreTooltip(tooltipForLinkType[linkData:match("^(.-):")])
    if tooltip then
        tooltip:Hide()
    end
end

local function OnHyperlinkClick(frame, linkData, link, button)
    local tooltip = RestoreTooltip(tooltipForLinkType[linkData:match("^(.-):")])
    if tooltip then
        if tooltip:IsObjectType('GameTooltip') then
            allowReposition = nil
            OnHyperlinkEnter(frame, linkData, link)
            allowReposition = true
        else
            tooltip:Show()
        end
    else
        OnHyperlinkEnter(frame, linkData, link)
    end
end

local function RegisterMessageFrame(frame)
    frame:HookScript('OnHyperlinkClick', OnHyperlinkClick)
    frame:SetScript('OnHyperlinkEnter', OnHyperlinkEnter)
    frame:SetScript('OnHyperlinkLeave', OnHyperlinkLeave)
end

local frame = CreateFrame('Frame')
frame:SetScript('OnEvent', function(self, event, name)
    if event == 'PLAYER_LOGIN' then
        for index = 1, NUM_CHAT_WINDOWS do
            RegisterMessageFrame(_G['ChatFrame' .. index])
        end
    end
    if GuildBankMessageFrame then
        RegisterMessageFrame(GuildBankMessageFrame)
        self:UnregisterAllEvents()
        self:SetScript('OnEvent', nil)
    elseif event ~= 'ADDON_LOADED' then
        self:RegisterEvent('ADDON_LOADED')
    end
end)
frame:RegisterEvent('PLAYER_LOGIN')

if ItemRefTooltip and ItemRefCloseButton then
    ItemRefTooltip.CloseButton = ItemRefCloseButton
end
  Reply With Quote