Thread Tools Display Modes
05-18-14, 04:53 AM   #1
Kygo
A Theradrim Guardian
 
Kygo's Avatar
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 64
Unit portraits oUF

I've been trying to use a piece of LUA code to enable portraits within oUF (the layout im using is oUF_Phanx)
but it does not work for some reason. I did try to put the code into the \Interface\AddOns\oUF_Phanx\Elements folder, and into the oUF core folder but non of them work.
Not sure what im doing wrong here.
The code i've been using is the below.

Code:
--[[ Element: Portraits

 Handles updating of the unit's portrait.

 Widget

 Portrait - A PlayerModel or Texture used to represent the unit's portrait.

 Notes

 The quest delivery question mark will be used instead of the unit's model when
 the client doesn't have the model information for the unit.

 Examples

   -- 3D Portrait
   -- Position and size
   local Portrait = CreateFrame('PlayerModel', nil, self)
   Portrait:SetSize(32, 32)
   Portrait:SetPoint('RIGHT', self, 'LEFT')
   
   -- Register it with oUF
   self.Portrait = Portrait

   -- 2D Portrait
   local Portrait = self:CreateTexture(nil, 'OVERLAY')
   Portrait:SetSize(32, 32)
   Portrait:SetPoint('RIGHT', self, 'LEFT')
   
   -- Register it with oUF
   self.Portrait = Portrait

 Hooks

 Override(self) - Used to completely override the internal update function.
                  Removing the table key entry will make the element fall-back
                  to its internal function again.
]]

local parent, ns = ...
local oUF = ns.oUF

local Update = function(self, event, unit)
	if(not unit or not UnitIsUnit(self.unit, unit)) then return end

	local portrait = self.Portrait
	if(portrait.PreUpdate) then portrait:PreUpdate(unit) end

	if(portrait:IsObjectType'Model') then
		local guid = UnitGUID(unit)
		if(not UnitExists(unit) or not UnitIsConnected(unit) or not UnitIsVisible(unit)) then
			portrait:SetCamDistanceScale(0.25)
			portrait:SetPortraitZoom(0)
			portrait:SetPosition(0,0,0.5)
      		portrait:SetOpacity(0.5)
			portrait:ClearModel()
			portrait:SetModel('interface\\buttons\\talktomequestionmark.m2')
			portrait.guid = nil
		elseif(portrait.guid ~= guid or event == 'UNIT_MODEL_CHANGED') then
			portrait:SetCamDistanceScale(1)
			portrait:SetPortraitZoom(1)
			portrait:SetPosition(0,0,0)
			portrait:ClearModel()
			portrait:SetUnit(unit)
			portrait.guid = guid
		end
	else
		SetPortraitTexture(portrait, unit)
	end

	if(portrait.PostUpdate) then
		return portrait:PostUpdate(unit)
	end
end

local Path = function(self, ...)
	return (self.Portrait.Override or Update) (self, ...)
end

local ForceUpdate = function(element)
	return Path(element.__owner, 'ForceUpdate', element.__owner.unit)
end

local Enable = function(self, unit)
	local portrait = self.Portrait
	if(portrait) then
		portrait.__owner = self
		portrait.ForceUpdate = ForceUpdate

		self:RegisterEvent("UNIT_PORTRAIT_UPDATE", Path)
		self:RegisterEvent("UNIT_MODEL_CHANGED", Path)
		self:RegisterEvent('UNIT_CONNECTION', Path)

		-- The quest log uses PARTY_MEMBER_{ENABLE,DISABLE} to handle updating of
		-- party members overlapping quests. This will probably be enough to handle
		-- model updating.
		--
		-- DISABLE isn't used as it fires when we most likely don't have the
		-- information we want.
		if(unit == 'party') then
			self:RegisterEvent('PARTY_MEMBER_ENABLE', Path)
		end

		return true
	end
end

local Disable = function(self)
	local portrait = self.Portrait
	if(portrait) then
		self:UnregisterEvent("UNIT_PORTRAIT_UPDATE", Path)
		self:UnregisterEvent("UNIT_MODEL_CHANGED", Path)
		self:UnregisterEvent('PARTY_MEMBER_ENABLE', Path)
		self:UnregisterEvent('UNIT_CONNECTION', Path)
	end
end

oUF:AddElement('Portrait', Path, Enable, Disable)
  Reply With Quote
05-18-14, 05:58 AM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
1. If you add a file, you have to tell WoW to load it by adding the path to it in the addon's TOC file.

2. The code you posted is just oUF's default portrait element. It's already in oUF, so you don't need to duplicate it in the layout addon.

3. An element is like an engine. If you actually want to drive somewhere, you have to bolt the engine into a car. In this case, this means that you need to add code in the layout to add a portrait object to the frames, so that the element has something to do. It can't do anything by itself.

4. Here is some example code that will add a 50px square portrait above each frame. You'll want to add it in Frames.lua.

Find this:
Code:
	---------------------------
	-- Name text, Level text --
	---------------------------
And right before it, add this:
Code:
	local portrait = self:CreateTexture(nil, "OVERLAY")
	portrait:SetSize(50, 50)
	portrait:SetPoint("BOTTOM", self, "TOP", 10, 0)
	self.Portrait = portrait
__________________
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
05-18-14, 07:48 AM   #3
Kygo
A Theradrim Guardian
 
Kygo's Avatar
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 64
Your code worked flawlessly as usuall!

Now I need to figure out how make it "3D" ish and add it to your options Panel (if I'm allowed to modify your code of course!) But one thing at the time, still learning LUA ^^,

But when I tried to swap out

Code:
local portrait = self:CreateTexture (nil, "OVERLAY")
portrait:SetSize(50, 50)
portrait:SetPoint("BOTTOM", self, "TOP", 10, 0)
self.Portrait = portrait
with

Code:
local Portrait = CreateFrame('PlayerModel', nil, self)
portrait:SetSize(50, 50)
portrait:SetPoint("BOTTOM", self, "TOP", 10, 0)
self.Portrait = portrait
it did not load any portraits. Not sure if it's the wrong code or just completey wrong in oUF overall.
Did hear by a in-game mate that CreateFrame is needed when you want a "3D" portrait.
Code:
---------------------------
	 -- Name text, Level text --
	 ---------------------------

Last edited by Kygo : 05-18-14 at 07:50 AM.
  Reply With Quote
05-18-14, 08:28 AM   #4
ObbleYeah
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Sep 2008
Posts: 210
Code:
local Portrait = CreateFrame('PlayerModel', nil, self)
portrait:SetSize(50, 50)
portrait:SetPoint("BOTTOM", self, "TOP", 10, 0)
self.Portrait = portrait

does

Lua Code:
  1. local portrait = CreateFrame("PlayerModel", nil, self)
  2. portrait:SetScript("OnShow", function(self) self:SetCamera(0) end)
  3. portrait:SetSize(50, 50)
  4. portrait:SetPoint("BOTTOM", self, "TOP", 10, 0)
  5. portrait.type = "3D"
  6. self.Portrait = portrait

work? it's been a while since i used oUF.
  Reply With Quote
05-18-14, 10:15 AM   #5
Kygo
A Theradrim Guardian
 
Kygo's Avatar
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 64
Originally Posted by ObbleYeah View Post
Code:
local Portrait = CreateFrame('PlayerModel', nil, self)
portrait:SetSize(50, 50)
portrait:SetPoint("BOTTOM", self, "TOP", 10, 0)
self.Portrait = portrait

does

Lua Code:
  1. local portrait = CreateFrame("PlayerModel", nil, self)
  2. portrait:SetScript("OnShow", function(self) self:SetCamera(0) end)
  3. portrait:SetSize(50, 50)
  4. portrait:SetPoint("BOTTOM", self, "TOP", 10, 0)
  5. portrait.type = "3D"
  6. self.Portrait = portrait

work? it's been a while since i used oUF.
Your code worked perfectly! Thank you so much!
Did add portrait:SetAlpha(0.6) on my own tho!
Now I need to figure out how to seperate Player portrait from Target and disable Target of Target so I can move them seperatly and a CreateCheckbox for player and target portrait Will google around see what I can find on how to do it!


Edit: Did mess around with if and elseif statements to position the portraits for Player and Target only. Looks alright at this point! Now I need to add options for the portraits of Player and Target in Phanx's options panel with X and Y position and size!

Screenshot of the frames with portraits: http://i.imgur.com/YiQ3fG4.jpg
(Ignore everything else.. It's just a pure mockup for a temp position of the Unitframes )
Code for those who wanna see my solution:
Code:
	local portrait = CreateFrame("PlayerModel", nil, self)
	       portrait:SetScript("OnShow", function(self) self:SetCamera(0) end)
	if unit == "player" then
	       portrait:SetPoint("BOTTOM", self, "TOP", -65, -60)
	elseif unit == "target" then
               portrait:SetPoint("BOTTOM", self, "TOP", -65, -60)
	end
               portrait:SetSize(158, 60)
	       portrait:SetAlpha(0.6)
	--portrait:SetPoint("BOTTOM", self, "TOP", -65, -60)
	       portrait.type = "3D"
	       self.Portrait = portrait

Last edited by Kygo : 05-18-14 at 04:11 PM. Reason: Added screenshot link + solution for 1 issue
  Reply With Quote
05-19-14, 12:27 AM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
The reason your first try didn't work is that Lua is case-sensitive, so "Portrait" and "portrait" are not the same thing.

Also, your current solution works, but is fairly inefficient since it's actually creating a portrait on every frame -- you can't see it on most frames because you don't set any points, but it's still there, being updated.

Also:

- Since you use the same points for both units, you don't actually need to do two separate statements there either -- just wrap the whole block in a single check.

- oUF doesn't do anything with a "type" member on the portrait object, so there's no reason to set one.

- Rather than setting an OnShow script, you should use oUF hooks so that your function gets called each time the portrait is updated, rather than each time it's shown -- otherwise, your function won't run if you switch from one target directly to another, for example.

- Rather than explicitly setting the height of the portrait in pixels, I'd suggest using anchors so that it automatically adjusts its height to match the health bar, since that's what it looks like you want to happen.

Code:
if unit == "player" or unit == "target" then
	local portrait = CreateFrame("PlayerModel", nil, self)
	portrait:SetPoint("TOPLEFT", health)
	portrait:SetPoint("BOTTOMLEFT", health)
	portrait:SetWidth(158)
	portrait:SetAlpha(0.6)

	portrait.PostUpdate = function(portrait, unit)
		portrait:SetCamera(0)
	end

	self.Portrait = portrait
end
If you want it to cover the whole health bar, just do:

Code:
portrait:SetAllPoints(health)
and remove all of the SetPoint and SetWidth lines.
__________________
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 : 05-19-14 at 12:29 AM.
  Reply With Quote
07-09-14, 02:52 PM   #7
Kygo
A Theradrim Guardian
 
Kygo's Avatar
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 64
Thought I would share the finished code which my addon comp is using with the unit frames oUF_Phanx! (it's been done for a while!) Thanks to Phanx for the help!

Lua Code:
  1. ---------------------------
  2.     --         Portrait            --
  3.     ---------------------------
  4.     if unit == "player" then
  5.     local portrait = CreateFrame("PlayerModel", nil, self)
  6.     portrait:SetPoint("TOPLEFT", self, "TOP", -280, -0)
  7.     portrait:SetPoint("BOTTOMLEFT", self, "BOTTOM", -280, -0)
  8.     portrait:SetWidth(128)
  9.     portrait:SetAlpha(1)
  10.    
  11.     portrait.PostUpdate = function(portrait, unit)
  12.         portrait:SetCamera(0)
  13.     end
  14.  
  15.     self.Portrait = portrait
  16. end
  17.  
  18.     if unit == "target" then
  19.     local portrait = CreateFrame("PlayerModel", nil, self)
  20.     portrait:SetPoint("TOPLEFT", self, "TOP", 145, -0)
  21.     portrait:SetPoint("BOTTOMLEFT", self, "BOTTOM", 145, -0)
  22.     portrait:SetWidth(128)
  23.     portrait:SetAlpha(1)
  24.    
  25.     portrait.PostUpdate = function(portrait, unit)
  26.         portrait:SetCamera(0)
  27.     end
  28.    
  29.     self.Portrait = portrait
  30. end
  Reply With Quote

WoWInterface » Featured Projects » oUF (Otravi Unit Frames) » Unit portraits oUF


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