Thread Tools Display Modes
08-16-12, 11:58 PM   #1
Cralus
A Defias Bandit
Join Date: Apr 2012
Posts: 2
Creating Buff Timers

(I managed to resolve the buff timers problem, but didn't want create a separate thread for this question.)
Contrary to the thread title, I actually have a question about my raid fontstring.

How can I truncate the names on my raid frames so they don't spill out over the sides?


Code:
lib.gen_fontstring = function(f, name, size, outline)
	local fs = f:CreateFontString(nil, "OVERLAY")
    fs:SetFont(name, size, outline)
    return fs
end
Code:
lib.raid_fontstrings = function(f)
    local name = lib.gen_fontstring(f.Health, config.font, 8, "OUTLINEMONOCHROME")
    name:SetPoint("CENTER", f.Health, "CENTER", 0, 0)
    name:SetJustifyH("LEFT")
	
	f:Tag(name, "[unitcolor][name]")
end
thanks

Last edited by Cralus : 08-17-12 at 12:23 AM.
  Reply With Quote
08-17-12, 01:27 AM   #2
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
You can do that by using SetPoints. Remove the "center" SetPoint.

Add two SetPoints:
Lua Code:
  1. name:SetPoint("LEFT", f.Health, , 2, 0)
  2. name:SetPoint("RIGHT", f.Health, , -2, 0)
Now your name will stick inside your health plate.

You can even combine health and your name string:
Lua Code:
  1. --name object
  2. name:SetPoint("LEFT", f.Health, "LEFT", 2, 0)
  3. name:SetJustifyH("LEFT")
  4. --health value object
  5. hpval:SetPoint("RIGHT", f.Health, "RIGHT", -2, 0)
  6. name:SetJustifyH("RIGHT")
  7. --connect name and hpvalue (right point of name will be -5px to the left point of hpval)
  8. name:SetPoint("RIGHT", hpval, "LEFT", -5, 0)
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)
  Reply With Quote
08-17-12, 01:45 PM   #3
Cralus
A Defias Bandit
Join Date: Apr 2012
Posts: 2
Thanks, Zork!

And yeah, I usually have a consolidated fontstring for Name/HP/PP but my raid frames ONLY have Name displayed.
  Reply With Quote
08-17-12, 02:03 PM   #4
Senit24
A Fallenroot Satyr
AddOn Compiler - Click to view compilations
Join Date: Jul 2012
Posts: 28
I'm trying to accomplish a similar thing, but this gave me a "..." after the name is cut off. Is there any way to remove that?
  Reply With Quote
08-22-12, 06:20 AM   #5
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
To truncate a string to a certain number of characters you can use string.sub. For example name:sub(1,3) would just return the first 3 letters of the name.
  Reply With Quote
08-22-12, 08:57 AM   #6
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
The problem with truncation is variable-width fonts - you can't reliably determine an appropriate cutoff point.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
09-03-12, 08:00 PM   #7
Senit24
A Fallenroot Satyr
AddOn Compiler - Click to view compilations
Join Date: Jul 2012
Posts: 28
Semlar, I tried adding "name:sub(1,3)" as a test, but it returned "sub is a nil value". Know why?
Code:
lib.tar_fontstrings = function(f)
    local name, hpval
    --health/name text strings
    if not f.hidename then
		name = lib.gen_fontstring(f.Health, config.font, 8, "OUTLINEMONOCHROME")
		name:SetPoint("LEFT", f.Health, "LEFT", 19, -16)
		name:SetJustifyH("LEFT")
		name:sub(1,3)
    end
  Reply With Quote
09-03-12, 11:30 PM   #8
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
A FontString is not the same as a Lua string object - there is no "sub" method.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
09-04-12, 01:13 AM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
If you want to truncate the value, you have to do it every time you set the text value:

Code:
fontstring:SetText(text:sub(1, 3))
On a side note, strsub(text, 1,3) is faster than text:sub(1, 3), especially if you upvalue strsub. If you're using it in code that runs very frequently (eg. in response to UNIT_HEALTH) you may want to switch notation. If you're using it in code that doesn't run very often, or doesn't run in combat (eg. in response to the user changing an option) then it doesn't really matter, so use whichever you think is more aesthetically pleasing.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
09-05-12, 08:51 PM   #10
Senit24
A Fallenroot Satyr
AddOn Compiler - Click to view compilations
Join Date: Jul 2012
Posts: 28
Originally Posted by Phanx View Post
If you want to truncate the value, you have to do it every time you set the text value:

Code:
fontstring:SetText(text:sub(1, 3))
On a side note, strsub(text, 1,3) is faster than text:sub(1, 3), especially if you upvalue strsub. If you're using it in code that runs very frequently (eg. in response to UNIT_HEALTH) you may want to switch notation. If you're using it in code that doesn't run very often, or doesn't run in combat (eg. in response to the user changing an option) then it doesn't really matter, so use whichever you think is more aesthetically pleasing.
Love you Phanx, thanks! And I'm using it for Raid Names, so I think either will work.
  Reply With Quote
09-06-12, 01:59 AM   #11
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
It will not. Raid frames can make use of multibyte characters. (UTF-8)

Someone like Ûlrîc or Ôéâ ... (not to forget asian players) etc. If I remember correctly sub does cut one byte. If the current character looked at has multibyte characters in the name the sub function has to adept or it will be possible to cut multibyte characters in half which will destroy the name.

There are some scripts on this already. Others may have the links, I do not.

I do not understand why the hassle. Just do the "..." thing and you will have no trouble without having to do any computing.
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)

Last edited by zork : 09-06-12 at 02:03 AM.
  Reply With Quote
09-06-12, 02:22 PM   #12
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
The simplest method would be to include the small UTF8 library in your addon, and then do:

Lua Code:
  1. -- upvalue at/near the top of your file:
  2. local utf8sub = string.utf8sub
  3.  
  4. -- call utf8sub instead of strsub to trim the text:
  5. fontstring:SetText(utf8sub(text, 1, 3))
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
09-06-12, 06:54 PM   #13
Senit24
A Fallenroot Satyr
AddOn Compiler - Click to view compilations
Join Date: Jul 2012
Posts: 28
I'll try that

Originally Posted by zork View Post
I do not understand why the hassle. Just do the "..." thing and you will have no trouble without having to do any computing.
Because "..." takes up even more space, leaving us with [ Kh... ] [ Ba... ] [ Tr... ] etc. So ugly!
*The unit frames are going to be pretty small as it is
  Reply With Quote
09-09-12, 12:04 AM   #14
Senit24
A Fallenroot Satyr
AddOn Compiler - Click to view compilations
Join Date: Jul 2012
Posts: 28
I've made this work in a slightly different way than you described, creating a new Tag for name:
Code:
oUF.Tags["playername"] = function(u)
		return UnitName(r or u):sub(1, 3)
	end
oUF.TagEvents["playername"] = "UNIT_NAME_UPDATE"
So I've installed the UTF8 directory, but I'm unsure how to implement it. Is there a way to make it fit with my Tag, or an easier method in general? -Thanks

Last edited by Senit24 : 09-09-12 at 12:07 AM.
  Reply With Quote
09-09-12, 12:40 AM   #15
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Senit24 View Post
So I've installed the UTF8 directory, but I'm unsure how to implement it. Is there a way to make it fit with my Tag, or an easier method in general? -Thanks
If you're planning to release your layout, you should embed the UTF8 library inside your addon. If you're using SVN, you should add the library to your svn:externals; click on the "Repository" tab on the UTF8 project page I linked earlier to get the SVN URL. If you're not using SVN, just make a new folder inside your layout folder named "Libs", and copy the UTF8 folder inside the Libs folder. Either way, add the appropriate path in your layout's TOC file to load the library as part of the addon:

Code:
## Interface: 50001
## Title: oUF_Senit
## Dependencies: oUF
## OptionalDependencies: UTF8

Libs\UTF8\utf8.lua

oUF_Senit.lua
Once you've done that, or if you're just using the layout for yourself and have UTF8 installed as a standalone addon, just change your tag function to use the UTF8 version of string.sub:

Code:
oUF.Tags["playername"] = function(u)
	return UnitName(r or u):utf8sub(1, 3)
end
oUF.TagEvents["playername"] = "UNIT_NAME_UPDATE"
If you want to make it (very slightly) faster, use this instead:

Code:
local utf8sub = string.utf8sub
oUF.Tags["playername"] = function(u)
	return utf8sub((UnitName(r or u)), 1, 3)
end
oUF.TagEvents["playername"] = "UNIT_NAME_UPDATE"
On a side note, why are you calling UnitName(r or u) instead of UnitName(u)? Where is r coming from, and why are you prioritizing it over the actual unit whose name change event fired?
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
09-09-12, 02:32 AM   #16
Senit24
A Fallenroot Satyr
AddOn Compiler - Click to view compilations
Join Date: Jul 2012
Posts: 28
I'm replying from the phone so I can't check whether this works, but I'm sure it will. Great detail, thank you.

The (r) arugment was deprecated when I rewrote my reaction function. I saw that and took it out right after posting my last response.
  Reply With Quote
09-09-12, 09:08 AM   #17
Senit24
A Fallenroot Satyr
AddOn Compiler - Click to view compilations
Join Date: Jul 2012
Posts: 28
Tested - works great! I was doing the exact same thing, but I was installing the UTF8 directory wrong. I forgot to include it in my TOC! >.<
  Reply With Quote
09-09-12, 02:57 PM   #18
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
That's how haste defines the default name tag.

u = self.unit, r = fontstring.overrideUnit and self.realUnit (self is the parent frame of the fontstring for the tag)

oUF uses realUnit as a frame unit: i.e. when you enter a vehicle unit becomes "vehicle" and realUnit becomes "player" for the player frame (as it then represents the vehicle the player has entered) and for the pet frame unit = "player" and realUnit = "pet". If unit is the same as realUnit then realUnit is set to nil.

So, as long as one does not set self.overrideUnit = true, it is safe to keep the call like that as r is always nil.

Edit: if would be interesting to set overrideUnit = true for the pet frame and enter a vehicle on a character without a pet. Or am I missing something?
Edit2: well, then you get the vehicle name instead of the player's. It seems WoW considers your vehicle to be your pet too.

Last edited by Rainrider : 09-09-12 at 03:40 PM. Reason: overrideUnit has to be set for the fontstring not the frame that holds it.
  Reply With Quote

WoWInterface » Featured Projects » oUF (Otravi Unit Frames) » Creating Buff Timers


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