Thread Tools Display Modes
09-06-10, 08:31 PM   #1
Porsha
A Black Drake
Join Date: Apr 2008
Posts: 88
Abbrev Names in UFs

HI again all...I ripped this piece of code out of Dawns Nameplates that I am using to try and get my target and target of target to show abbrev names in her format:

I.e. (from her config)
names longer than this number will be abbreviated, for example: Sunreaver Mage Guardian becomes S. M. Guardian

I have a code that changes the names to a certain number of characters (thank you Nin) but if the name is longer than I want, it cuts off the last few characters. I wish to have the format like above

here is the working code I use now:

local tmpunitname

if (unit=="pet" or unit == "focus") then
tmpunitname = UnitName(unit)
local count = 4


if tmpunitname and tmpunitname:len() > count then
tmpunitname = tmpunitname:sub(1, count, self)
end
end


if (unit=="target" or unit == "targettarget") then
tmpunitname = UnitName(unit)
local count = 20


if tmpunitname and tmpunitname:len() > count then
tmpunitname = tmpunitname:sub(1, count, self)
end
end


And this is the len string I got from Dawns nameplates:

-- string format
local utf8sub = function(string, i, dots)
local bytes = string:len()
if (bytes <= i) then
return string
else
local len, pos = 0, 1
while(pos <= bytes) do
len = len + 1
local c = string:byte(pos)
if (c > 0 and c <= 127) then
pos = pos + 1
elseif (c >= 192 and c <= 223) then
pos = pos + 2
elseif (c >= 224 and c <= 239) then
pos = pos + 3
elseif (c >= 240 and c <= 247) then
pos = pos + 4
end
if (len == i) then break end
end

if (len == i and pos <= bytes) then
return string:sub(1, pos - 1)..(dots and '...' or '')
else
return string
end
end
end


I have worked for a few hours trying to change words that match the UF layout but I can figure this out and finally gave up coming here to see if anyone can help.

thank you muches!
  Reply With Quote
09-06-10, 10:32 PM   #2
Porsha
A Black Drake
Join Date: Apr 2008
Posts: 88
Well, i found this on a search but it isnt working either:

--this part goes near the top of your layout, before the style function

oUF.Tags["[abbreviatedname]"] = [[function(u)
local name = UnitName(u)
return (string.len(name) > 10) and string.gsub(name, "%s?(.)%S+%s", "%1. ") or name
end
]]
oUF.TagEvents["[abbreviatedname]"] = "UNIT_NAME_UPDATE"


-- snip, next part is inside your style function.

local name = self.Health:CreateFontString(nil, 'OVERLAY', 'GameFontHighlightSmallLeft')
name:SetPoint('LEFT',self.Health) --replace this line with your own positioning code
name:SetPoint('RIGHT',self.Health) --replace this line with your own positioning code
self:Tag(name, '[abbreviatedname]')
self.Name = name


So.....waiting for someone who may can help me out thanks a lot!
  Reply With Quote
09-07-10, 03:44 AM   #3
Taroven
A Cyclonian
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 49
Edit: I totally didn't read the first thread all the way through... really need sleep. That tag seems like it should work. What exactly is the problem you're having with it?
__________________
Former author of EventHorizon Continued and Other Releases.

Last edited by Taroven : 09-07-10 at 03:55 AM.
  Reply With Quote
09-07-10, 04:09 AM   #4
yj589794
A Rage Talon Dragon Guard
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 314
That Tag is for oUF 1.3.x, if you are using oUF 1.4 or above you need to change it to:
Code:
oUF.Tags["abbreviatedname"] = [[function(u)
	local name = UnitName(u)
	return (string.len(name) > 10) and string.gsub(name, "%s?(.)%S+%s", "%1. ") or name
end]]
oUF.TagEvents["abbreviatedname"] = "UNIT_NAME_UPDATE"
with this in your layout:
Code:
local name = self.Health:CreateFontString(nil, 'OVERLAY', 'GameFontHighlightSmallLeft')
name:SetPoint('LEFT',self.Health) --replace this line with your own positioning code
name:SetPoint('RIGHT',self.Health) --replace this line with your own positioning code
self:Tag(name, '[abbreviatedname]')
  Reply With Quote
09-07-10, 08:59 AM   #5
Porsha
A Black Drake
Join Date: Apr 2008
Posts: 88
I'm using oUF 1.3

Maybe I didnt give all the infor so this may be a long copy and paste, sorry.

This is what the layout has now concerning names:

-- -------------------------------------------------------------------------------------------------
-- NAME UPDATE
-- -------------------------------------------------------------------------------------------------

local updateName = function(self, event, unit)
if(self.unit ~= unit) then return end
local name = UnitName(unit)
self.Name:SetText(name)

local color
if UnitIsPlayer(unit) then
local _, c = UnitClass(unit)
color = RAID_CLASS_COLORS[c]

else
color = UnitReactionColor[UnitReaction(unit, "player")]
end

if color then
self.Name:SetTextColor(color.r,color.g,color.b,1)
end


if unit=="target" or unit=="targettarget" or unit=="focus" or unit=="pet" then
updateLevel(self, unit, name)
end
end

-- -------------------------------------------------------------------------------------------------
-- HEALTH UPDATE
-- -------------------------------------------------------------------------------------------------

local updateHealth = function(self, event, unit, bar, min, max)
local cur, maxhp = min, max
local missing = maxhp-cur
local d = floor(cur/maxhp*100)
local color
local tmpunitname

if (unit=="pet" or unit == "focus") then
tmpunitname = UnitName(unit)
local count = 4


if tmpunitname and tmpunitname:len() > count then
tmpunitname = tmpunitname:sub(1, count, self)
end
end


if (unit=="target" or unit == "targettarget") then
tmpunitname = UnitName(unit)
local count = 20


if tmpunitname and tmpunitname:len() > count then
tmpunitname = tmpunitname:sub(1, count, self)
end
end

if UnitIsPlayer(unit) then
local _, c = UnitClass(unit)
color = RAID_CLASS_COLORS[c]

else
color = UnitReactionColor[UnitReaction(unit, "player")]
end

if color then
self.Healthpanel:SetBackdropColor(0,0,0,0)
self.Health2:SetVertexColor(color.r,color.g,color.b,1)
self.Name2:SetTextColor(color.r,color.g,color.b,1)
end

if(unit=="pet" or unit == "focus") then
if(self.unit ~= unit) then return end
local min, max = UnitHealth(unit), UnitHealthMax(unit)
self.Health2:SetPoint('LEFT', 56.2 * (min / max), 0)
self.Health2:SetVertexColor(self.ColorGradient(min / max, unpack(self.colors.smooth)))

else
self.Health2:SetPoint('LEFT', 201 * (min / max), 0)
end

if(UnitIsDead(unit)) then
bar.value:SetText"|cffffffffDead|cffff0000!"
self.Health2:SetVertexColor(0,0,0,0)
self.Health:SetStatusBarColor(0,0,0,0)

elseif(UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit)) then
bar.value:SetText"|cffffffffTapped|cffff0000!"


elseif(UnitIsGhost(unit)) then
bar.value:SetText"|cffffffffGhost|cffff0000!"
self.Health2:SetVertexColor(0,0,0,0)
self.Health:SetStatusBarColor(0,0,0,0)

elseif(not UnitIsConnected(unit)) then
bar.value:SetText"|cffffffffOff|cffff0000!"
self.Health2:SetVertexColor(0,0,0,0)
self.Health:SetStatusBarColor(0,0,0,0)

elseif(unit == "player") then
if(min ~= max) then
bar.value:SetText("|cffff0000-|r"..numberize(missing))
else
bar.value:SetText("|cff98fb98"..numberize(cur))
end

elseif(unit=="target") then
if(min ~= max) then
bar.value:SetFormattedText('|cffffffff%s|r |cff00ff00: |r%d',numberize(cur), (min/max)*100)
self.Name2:SetText(tmpunitname)
else
bar.value:SetText("|cff98fb98"..numberize(cur))
self.Name2:SetText(tmpunitname)
end
elseif(unit=="targettarget") then
if(min ~= max) then
bar.value:SetText("|cffff0000-|r"..numberize(missing))
self.Name2:SetText(tmpunitname)

else
self.Name2:SetText(tmpunitname)
bar.value:SetText("|cff98fb98"..numberize(cur))
end

elseif(unit == "focus") then
if(min ~= max) then
bar.value:SetText("|cffff0000-|r"..numberize(missing))
self.Name2:SetText(tmpunitname)

else
self.Name2:SetText(tmpunitname)
bar.value:SetText("|cff98fb98"..numberize(cur))
end

elseif(unit == "pet") then
bar.value:SetText("|cff98fb98"..numberize(cur))
end
self:UNIT_NAME_UPDATE(event, unit)
end



Now with this setup I can truncate the name to whatever length but it cuts if off at the end of the name.

I guess im not sure where to put the proper codes to get them to work. When I delete everything about (tmpunitname) and add in that code then replace this original line that calls the code (this is the original thats in the layout now before any changes):

-- -------------------------------------------------------------------------------------------------
-- NAME's
-- -------------------------------------------------------------------------------------------------
self.Name = self.Health:CreateFontString(nil, 'OVERLAY')
self.Name:SetWidth(200)
self.Name:SetHeight(fontsize)
self.Name:SetFont(font,fontsize, "OUTLINEMONOCHROME")
self.Name:SetShadowOffset(1,-1)
self.Name:SetShadowColor(0,0,0)

-- -------------------------------------------------------------------------------------------------
-- NAME's
-- -------------------------------------------------------------------------------------------------

self.Name2 = self.Health:CreateFontString(nil, "OVERLAY")
self.Name2:SetWidth(200)
self.Name2:SetHeight(fontsize)
self.Name2:SetFont(font,fontsize, "OUTLINEMONOCHROME")
self.Name2:SetShadowOffset(1,-1)
self.Name2:SetShadowColor(0,0,0)

I get an error that tells me its trying to call "name" but found nil?


I guess I need to know what to delete or change and where to do that ><

I still want my focus and pet to be truncated to 4 characters though.

Geez, I hope this isnt confusing - I am using 1.3 oUF (as that is what Nins is coded from).


thanks all for the help so far!

Maybe thats why Dawns didnt work in mine! Shes using 1.4 - sigh well I dont know what to change to make it work with 1.3


BTW, this code turns to red text in note++ (i thought that meant it was commented out? so.....and I suppose to remove the brackets? I do that - maybe thats why it didnt work? Im so confused ><

oUF.Tags["abbreviatedname"] = [[function(u)
local name = UnitName(u)
return (string.len(name) > 10) and string.gsub(name, "%s?(.)%S+%s", "%1. ") or name
end]]
oUF.TagEvents["abbreviatedname"] = "UNIT_NAME_UPDATE"

Last edited by Porsha : 09-07-10 at 09:19 AM.
  Reply With Quote
09-07-10, 09:40 AM   #6
yj589794
A Rage Talon Dragon Guard
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 314
OK, you code is using custom update routines so, unless you want to rewrite your entire layout, do not look at Tags.

======

Going back to your original post:

The utf8sub function is a custom routine to handle special characters (certain accented characters and other non-standard latin characters). The standard Lua sub routine does not handle UTF8 characters gracefully, so it is effectively a direct replacement.
For what you are trying to do, it is not required.

change the following and it should do what you require:
Code:
local tmpunitname

if (unit=="pet" or unit == "focus") then
	tmpunitname = UnitName(unit)
	local count = 4

	tmpunitname = (string.len(tmpunitname) > count) and string.gsub(tmpunitname, "%s?(.)%S+%s", "%1. ") or tmpunitname

	if tmpunitname and tmpunitname:len() > count then
		tmpunitname = tmpunitname:sub(1, count, self)
	end
end


if (unit=="target" or unit == "targettarget") then
	tmpunitname = UnitName(unit)
	local count = 20

	tmpunitname = (string.len(tmpunitname) > count) and string.gsub(tmpunitname, "%s?(.)%S+%s", "%1. ") or tmpunitname

	if tmpunitname and tmpunitname:len() > count then
		tmpunitname = tmpunitname:sub(1, count, self)
	end
end
  Reply With Quote
09-07-10, 09:58 AM   #7
Porsha
A Black Drake
Join Date: Apr 2008
Posts: 88
/squeels

thank you SO much! And of course the game is down /sigh but yes...I see what you're saying - I had the right idea but didnt know what to do with it LOL

/hugs

edit: WOOT it works! thanks again (just had to remove the second call for Name2 and change all the self.Name2 to self.Name)

Last edited by Porsha : 09-07-10 at 12:04 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Abbrev Names in UFs

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off