update 2:
Added RAID_TARGET_UPDATE to unitless events (I didn't notice this was an issue because I used .frequentUpdates).
update 1:
Simplified code so you don't need tex coords.
Hey.
I wanted to stick a raid icon in a FontString right next to the unit name. So that it would show up something like this:
Icon present:
[+ Name........]
No icon:
[Name..........]
In other words, the name would shift over to make room for the icon, and shift back when the icon was not present. Since it wasn't super straightforward (you can't just use {rtX}) I figured I'd share my solution:
EDIT:
Turns out it was pretty straightforward after all:
Code:
local ristring = '|TInterface\\TargetingFrame\\UI-RaidTargetingIcon_%d:0:0:0:-1|t '
oUF.Tags['tecu:raidicon'] = function(unit)
local i = GetRaidTargetIndex(unit)
if i then
return ristring:format(i)
end
end
oUF.TagEvents['tecu:raidicon'] = 'RAID_TARGET_UPDATE'
oUF.UnitlessTagEvents.RAID_TARGET_UPDATE = true
The ':0:0:0:-1' is ':sizex:sizey:offsetx:offsety'. A zero for the size means 'use the height of the fontstring.'
The rest of the original post:
Saved this because there is some useful info for adding textures to fontstrings.
Code:
local ritex = {}
do
local ristr = [[|TInterface\TargetingFrame\UI-RaidTargetingIcons:0:0:0:-1:256:256:%f:%f:%f:%f|t]]
setmetatable(ritex, {
__index = function(t, k)
-- get coords:
local x1, y1 = ((k - 1) % 4) * 64, (math.floor((k - 1) / 4)) * 64
-- texture escape string:
local r = ristr:format(x1, x1 + 64, y1, y1 + 64)
-- cache it
rawset(t, k, r)
--
return r
end
})
end
oUF.Tags['tecu:raidicon'] = function(unit)
local i = GetRaidTargetIndex(unit)
if i then
return ritex[i]
end
end
oUF.TagEvents['tecu:raidicon'] = 'RAID_TARGET_UPDATE'
oUF.UnitlessTagEvents.RAID_TARGET_UPDATE = true
And some notes:
- Interface\TargetingFrame\UI-RaidTargetingIcons is 256x256, with icons 1-4 in the top row, and 2-8 in the second.
- Each icon takes up a 64x64 square of this texture.
- The numbers in the escape sequence are explained here.
- BUT (and this is the real reason I bothered posting this): the texture coords used are not normalized to [0, 1] but instead to the dimensions of the texture file itself.
Maybe someone will find this useful!