WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   gsub'ing |t and stuff (https://www.wowinterface.com/forums/showthread.php?t=37025)

Grimsin 11-25-10 02:19 AM

gsub'ing |t and stuff
 
Okay so i added raid icons to the tooltip text. Now i may have gone about this all wrong but it works, and fairly well. I had to do a lot of extra gsubs because when you gsub just lastricon it leaves a big blank space... if you gsub |t it then prints the file path to the text. Is there a way to shorten up the gsubing that i did? why do some characters not gsub without another? like why in order to gsub a \ you have to do \\ hmm? this is what my code looks like curious what input everyone has.

It adds raid icons to the tooltip and updates when you add remove them, its hooked to the OnUpdate of the GameToolTip. This is especially nice because i use mouseover raid target icon setting macros.

Code:

local lastricon = 1
function addon:AddRaidIcon(self)
        if addon.settings.showRaidIcon then
                local name, unit = self:GetUnit()
                if unit then
                        local text = GameTooltipTextLeft1:GetText()
                        if text then
                                local ricon = GetRaidTargetIndex(unit)
                                if ricon then
                                        if lastricon ~= 1 then
                                                text = gsub(text, "|T", "")
                                                text = gsub(text, lastricon.."::", "")
                                                text = gsub(text, addon.settings.tooltipIconSize / 10, "")
                                                text = gsub(text, "Interface", "")
                                                text = gsub(text, "TargetingFrame", "")
                                                text = gsub(text, "\\", "")
                                                text = gsub(text, "UI", "")
                                                text = gsub(text, "RaidTargetingIcon", "")
                                                text = gsub(text, "-", "")
                                                text = gsub(text, "_", "")
                                                text = gsub(text, " ", "", 1)
                                        end
                                        lastricon = ricon
                                        if not text:find(ricon) then
                                                GameTooltipTextLeft1:SetText(("|t%s:%s|t %s"):format(ICON_LIST[ricon], addon.settings.tooltipIconSize / 10, text))
                                        GameTooltip:Show()
                                        end
                                end
                                if not ricon or ricon == nil then
                                        if lastricon ~= 1 then
                                                text = gsub(text, "|T", "")
                                                text = gsub(text, lastricon.."::", "")
                                                text = gsub(text, addon.settings.tooltipIconSize / 10, "")
                                                text = gsub(text, "Interface", "")
                                                text = gsub(text, "TargetingFrame", "")
                                                text = gsub(text, "\\", "")
                                                text = gsub(text, "UI", "")
                                                text = gsub(text, "RaidTargetingIcon", "")
                                                text = gsub(text, "-", "")
                                                text = gsub(text, "_", "")
                                                text = gsub(text, " ", "", 1)
                                                GameTooltipTextLeft1:SetText(("%s"):format(text))
                                                lastricon = 1
                                                GameTooltip:Show()
                                        end
                                end
                        end
                end
        end
end


Torhal 11-25-10 04:29 AM

Without knowing what you're trying to extract, and not wanting to reverse-engineer it from the gsub() mess, the only thing I can offer is a simplification of your if-nesting mess:

Code:

local lastricon = 1

function addon:AddRaidIcon(self)
        if not addon.settings.showRaidIcon then
                return
        end
        local name, unit = self:GetUnit()

        if not unit then
                return
        end
        local text = GameTooltipTextLeft1:GetText()

        if not text then
                return
        end
        local ricon = GetRaidTargetIndex(unit)

        if not ricon then
                return
        end

        if lastricon ~= 1 then
                text = gsub(text, "|T", "")
                text = gsub(text, lastricon.."::", "")
                text = gsub(text, addon.settings.tooltipIconSize / 10, "")
                text = gsub(text, "Interface", "")
                text = gsub(text, "TargetingFrame", "")
                text = gsub(text, "\\", "")
                text = gsub(text, "UI", "")
                text = gsub(text, "RaidTargetingIcon", "")
                text = gsub(text, "-", "")
                text = gsub(text, "_", "")
                text = gsub(text, " ", "", 1)
        end
        lastricon = ricon

        if not text:find(ricon) then
                GameTooltipTextLeft1:SetText(("|t%s:%s|t %s"):format(ICON_LIST[ricon], addon.settings.tooltipIconSize / 10, text))
                GameTooltip:Show()
        end

        if not ricon then
                if lastricon ~= 1 then
                        text = gsub(text, "|T", "")
                        text = gsub(text, lastricon.."::", "")
                        text = gsub(text, addon.settings.tooltipIconSize / 10, "")
                        text = gsub(text, "Interface", "")
                        text = gsub(text, "TargetingFrame", "")
                        text = gsub(text, "\\", "")
                        text = gsub(text, "UI", "")
                        text = gsub(text, "RaidTargetingIcon", "")
                        text = gsub(text, "-", "")
                        text = gsub(text, "_", "")
                        text = gsub(text, " ", "", 1)
                        GameTooltipTextLeft1:SetText(("%s"):format(text))
                        lastricon = 1
                        GameTooltip:Show()
                end
        end
end

The only other change I made was getting rid of "or ricon == nil", since "not ricon" already achieves this. I'm also unsure of the overall logic, since you check for something, show the GameTooltip, then check for something else and show GameTooltip again.

Xubera 11-25-10 04:42 AM

if your gsub above removes a texture... like

|TTexturePath:otherjunk:morejunk|t

is what your trying to remove, just do a

text = text:gsub("\124T.*\124t", "")

that removes the |T and |t and everything in between

Grimsin 11-25-10 07:18 AM

Quote:

Originally Posted by Xubera (Post 219664)
if your gsub above removes a texture... like

|TTexturePath:otherjunk:morejunk|t

is what your trying to remove, just do a

text = text:gsub("\124T.*\124t", "")

that removes the |T and |t and everything in between

BINGO :)

Thats exactly what its doing, removing the icon texture and all the garble in between. Thanks.

Torhal - I love how you refer to it all as a mess :)
The reason you do the gametooltip:show more then once is to make it update the gametooltip's size according to if the icon is shown or not, and since you can keep the cursor over a target and add/remove raid icons you need the size to update without cursoring off of the target. Best way i found to do that was to just make it show tooltip again. Another way although i could not get it to work is to get it the gametooltip onupdate script again but i tried get script and just running bliz's function and it did not work like one would think.

Xinhuan 11-25-10 01:47 PM

Quote:

Originally Posted by Xubera (Post 219664)
if your gsub above removes a texture... like

|TTexturePath:otherjunk:morejunk|t

is what your trying to remove, just do a

text = text:gsub("\124T.*\124t", "")

that removes the |T and |t and everything in between

Careful there. You should use

text = text:gsub("\124T.-\124t", "")

instead to capture the smallest possible amount of text between |T and |t. This is because .* captures the longest possible amount of text and if the text contains 2 texture links, you will get rid of all the text between the 2 textures.

Grimsin 11-25-10 02:10 PM

Quote:

Originally Posted by Xinhuan (Post 219760)
Careful there. You should use

text = text:gsub("\124T.-\124t", "")

instead to capture the smallest possible amount of text between |T and |t. This is because .* captures the longest possible amount of text and if the text contains 2 texture links, you will get rid of all the text between the 2 textures.

So what your saying is if i had two icons at either end of the text line it would get rid of everything? For the moment this is not the case but i was going to add char portraits to the tooltips which then maybe will have two textures in one text line...

Xinhuan 11-25-10 05:09 PM

Yes, that's exactly what I am saying.

Torhal 11-26-10 04:53 AM

Quote:

Originally Posted by Grimsin (Post 219682)
<snip>

Torhal - I love how you refer to it all as a mess :)
<snip>

Sorry - I'd just woken up and my extended vocabulary hadn't yet kicked in. :D

Xubera 11-26-10 11:48 AM

i thought .+ meant it was the longest possible string

edit::

oh .+ is 1 or more
.* is 0 or more, but the longest
.- is 0 or more, but the shortest.


All times are GMT -6. The time now is 06:23 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI