Thread Tools Display Modes
12-04-13, 02:31 AM   #1
skarie
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Jun 2011
Posts: 37
New Portrait help !!

Hello guys,

I am trying to implement my own portrait display. I already have image files for each class/gender/race based on this http://wowpedia.org/Gallery_of_player_avatars.

However I can't figure out what is a good way to update the image, specially for party members.

Here is what i have so far.
Code:
local function createTarget(self)
..omitted on purpose
        local localizedClass, englishClass, classIndex = UnitClass("target")
	local race = UnitRace("target")
	local gender_code = {"neuter or unknown","male","female"}
	local gender = gender_code[UnitSex("target")]
	local facetexture
	if (gender == "male") then
		facetexture = "Interface\\AddOns\\oUF_HL2\\faces\\"..englishClass.."-"..race.."-m-80.tga"
	elseif (gender == "female") then
		facetexture = "Interface\\AddOns\\oUF_HL2\\faces\\"..englishClass.."-"..race.."-f-80.tga"
	end


	local fframe = CreateFrame("Frame",nil,self)
	fframe:SetFrameStrata("BACKGROUND")
	fframe:SetWidth(64)
	fframe:SetHeight(64)

	local t = fframe:CreateTexture(nil,"BACKGROUND")
	t:SetTexture(facetexture)
	t:SetAllPoints(fframe)
	fframe.texture = t
	
	self.fframe = fframe
        self.fframe:SetPoint("LEFT",self,"RIGHT")

end
Now it seems to be working for player and target frame. But I cant make it work for party member, since unit value has to be partyN (where N is 1,2,....10..). Also it wont update the image. Any ideas guys?

Thanks in advance.
  Reply With Quote
12-04-13, 12:20 PM   #2
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
GROUP_ROSTER_UPDATE sounds like a good event for this. If self in your code is an oUF frame passed to your createTarget function, then simply use self.unit instead of "target". If self is another oUF element, then you normaly would get the unit by self.__owner.unit
  Reply With Quote
12-04-13, 07:16 PM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
There is an event specifically for updating portraits:
UNIT_PORTRAT_UPDATE

But, you do not need to implement all that stuff yourself. oUF already provides a Portrait element. You can easily use that, plus an override function to set your own texture:

Create the portrait, inside your frame's Spawn function:
Code:
local portrait = self:CreateTexture(nil, "ARTWORK")
portrait:SetPoint("LEFT", self, "RIGHT")
portrait:SetSize(64, 64)
portrait.Override = UpdatePortrait
self.Portrait = portrait
Then add an override function somewhere before your Spawn function:
Code:
local function UpdatePortrait(self, event, unit)
	if not unit or not UnitIsUnit(self.unit, unit) then return end

	if not UnitIsPlayer(unit) then
		-- This unit isn't a player.
		-- Use a fallback texture, or just hide the portrait.
		return
	end

	local _, class = UnitClass(unit)
	local _, race = UnitRace(unit)
	if not class or not race then
		-- Class and/or race information is not available for some reason.
		-- Use a fallback texture, or just hide the portrait.
		return
	end

	local texture
	if UnitSex(unit) == 3 then
		-- Female
		texture = "Interface\\AddOns\\oUF_HL2\\faces\\" .. class .. "-" .. race .. "-f-80.tga"
	else
		-- Male (or unknown)
		texture = "Interface\\AddOns\\oUF_HL2\\faces\\" .. class .. "-" .. race .. "-m-80.tga"
	end
	
	self.Portrait:SetTexture(texture)
end
Some other observations:

(1) The first return from UnitRace is localized. You need to use the second return, which is locale-independent, so your addon works in all locales, not just your own. Rename your images accordingly. See this page for a list of locale-independent race names to use.

(2) There's no need to convert the numeric return from UnitSex into a string. Just use the number directly and save yourself the table lookup.
__________________
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.

Last edited by Phanx : 12-04-13 at 07:31 PM.
  Reply With Quote
12-04-13, 11:32 PM   #4
skarie
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Jun 2011
Posts: 37
Thank you guys. The codes work beautifully.

Now, I am curious about when you register and set active a style for ouf unit frame like this
Code:
oUF:RegisterStyle("oUF_HL2party", createParty)
oUF:SetActiveStyle("oUF_HL2party")
Is createParty called once (and only once) for every party frame? Lets say you have 5 members in your party, so 5 calls will be made to createParty. Then when party is disbanded, and a new party is made, will createParty be called again for each of the member? Or it just knows to use the old frames/data that were made previously?
  Reply With Quote
12-04-13, 11:56 PM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
createParty is called once for each frame, when the frame is created. It is not called again every time the frame is hidden and re-shown. Remember, frame objects in WoW cannot be destroyed, only hidden. When a frame disappears from the screen, it is only hidden, not destroyed. When it re-appears later, it is the same frame being shown again, not a new frame being created.

Edit:
I should clarify that you could create a second copy of, for example, the party4 frame -- but then you would have two frames, both displaying the party4 unit, one sitting on top of the other. And, you would have to do it on purpose, by calling Spawn twice for the party4 unit, or SpawnHeader twice if you're using group headers.
__________________
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.

Last edited by Phanx : 12-04-13 at 11:59 PM.
  Reply With Quote
12-06-13, 11:30 AM   #6
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
To clarify what Phanx said, the same frame is used, but its elements are updated according to the update functions you provided. That's one reason to override the update function of oUF's own portrait element, as oUF will then handle the update for you when the frame is shown.
  Reply With Quote
12-06-13, 09:48 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Yes, that's why you should not put any "update" code in your spawn function (or any helper functions for creating elements that you call from your spawn function). Updating is separate from creating.

Using your smartphone as an analogy, the phone is only created once. When you turn off the screen, you're not destroying the screen. When you turn on the screen, it's the same screen as before, and the phone updates the screen to show different icons, apps, whatever. When you recieve a notification or open a new app, the phone updates the screen again.

Likewise, your target frame is only created once. When you "turn off" (or clear) your target, you're not destroying the frame, you're just hiding it. When you "turn on" your target (acquire one), it's the same frame as before, and oUF updates it to show different names, colors, etc. When your target's health changes or you pick a new target, oUF updates the frame again.

Creating your own element is like plugging an Xbox controller into your phone. Your phone will just ignore it, because it has no idea what it is or what to do with it. If you want your phone to do something with your Xbox controller, you need to write a driver so the phone recognizes that the controller is an input device and can communicate with it.

Likewise, if you add a custom texture or fontstring to your frame and don't label it as a supported element, oUF will just ignore it. If you want oUF to do something with your custom element, you need to "write a driver" so oUF knows your texture or fontstring is an element, and knows when and how to update it.

You could also handle all the updates yourself (by registering for events, detecting when the frame is hidden or shown, etc.) but this is more work and not generally recommended -- imagine if you wanted your Xbox controller to vibrate when you got a new text message. You could add a cell radio to the controller, write your own code for communicating with your phone carrier, periodically check for text messages, and vibrate the controller -- but it would be easier just to tell your phone to send a "vibrate" signal to the controller when a text message arrived.
__________________
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

WoWInterface » Featured Projects » oUF (Otravi Unit Frames) » New Portrait help !!


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