Thread Tools Display Modes
03-03-18, 01:15 PM   #1
runamonk
A Theradrim Guardian
 
runamonk's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 61
Smile Highlight Targeted unit

So I'm wondering is there a good way to highlight the currently targeted unit frame (using oUF to do Nameplates)?

I've been testing it out and I have it working nicely to show nameplates but the issue I have is I would really like to slap a border on the currently targeted units nameplate. I thought maybe I could use the callback event but it doesn't fire (always) fire an event when changing through targets.

Any ideas?

Thanks,

R
  Reply With Quote
03-03-18, 06:07 PM   #2
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
Pass a function as the second parameter to oUF:SpawnNamePlates, it will be called whenever a new nameplate unit is used or when the player changes targets.
Then use UnitIsUnit('target', unit) in that function to check if they match (unit being the 3rd argument passed to the function), and proceed to show/hide your border.

Drycoded example:
Lua Code:
  1. oUF:SpawnNamePlate(nil, function(self, _, unit)
  2.     if(UnitExists('target') and UnitIsUnit('target', unit)) then
  3.         self.border:Show()
  4.     else
  5.         self.border:Hide()
  6.     end
  7. end)

Shorter version
Lua Code:
  1. oUF:SpawnNamePlate(nil, function(self, _, unit)
  2.     self.border:SetShown(UnitExists('target') and UnitIsUnit('target', unit))
  3. end)

Last edited by p3lim : 03-03-18 at 06:12 PM.
  Reply With Quote
03-04-18, 02:13 AM   #3
runamonk
A Theradrim Guardian
 
runamonk's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 61
Thanks for the response. That's almost identical to what I am doing. If I attack more than one mob at a time, so I tag one it will highlight properly (don't kill it), then I tag another unrelated mob (don't kill it) both mobs stay highlighted. It's so close! lol. Thanks.


Code:
local OnSpawnNamePlates = function(self, event, unit)
    if (UnitExists('target') and UnitIsUnit('target', unit)) then
        self:SetBackdropColor(1, 1, 1, 1)
    else
        self:SetBackdropColor(0, 0, 0, 1)
    end
end

oUF:SpawnNamePlates("mnkNames", OnSpawnNamePlates, cvars)
  Reply With Quote
03-04-18, 03:05 PM   #4
aallkkaa
A Warpwood Thunder Caller
 
aallkkaa's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2017
Posts: 98
It's been years since I worked on a oUF layout, but in this case, I guess it would be as simple as this:
Code:
local oldTarget
local OnSpawnNamePlates = function(self, event, unit)
    if (UnitExists('target') and UnitIsUnit('target', unit)) then
        if oldTarget then
                oldTarget:SetBackdropColor(0, 0, 0, 1)
        end
        self:SetBackdropColor(1, 1, 1, 1)
        oldTarget = self
    else
        self:SetBackdropColor(0, 0, 0, 1)
    end
end

oUF:SpawnNamePlates("mnkNames", OnSpawnNamePlates, cvars)
  Reply With Quote
03-04-18, 08:02 PM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Just a semantic nitpick, but if you're going to name your function instead of just passing it anonymously to oUF, you should change "OnSpawnNamePlates" to something that more accurately reflects when the function is actually called, like "OnAcquireNamePlate". In oUF land, "spawn" is something that happens once per frame, the first time the object is created, but the nameplate callback happens every time a nameplate frame (whether it's newly created or pulled from the pool of existing frames to be reused) gets assigned a unit.

It may not make much difference now, but when you're going back to your code in 6 months or 3 years, having that function named "spawn" is going to confuse you.
__________________
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
03-05-18, 02:24 AM   #6
runamonk
A Theradrim Guardian
 
runamonk's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 61
Good lord I thought I was anal retentive about naming conventions at the office, you're right though.
  Reply With Quote
03-05-18, 03:14 AM   #7
runamonk
A Theradrim Guardian
 
runamonk's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 61
Thanks aallkkaa, I went with something similar.
  Reply With Quote
03-14-18, 01:43 AM   #8
siweia
A Flamescale Wyrmkin
 
siweia's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2011
Posts: 126
If I just add it in the spawn fieid, it would not work properly for me. When I change units, the old mark won't go away.

This is what I currently do.
Code:
local function UpdateTargetMark(self)
	local mark = self.targetMark
	if not mark then return end

	if UnitIsUnit(self.unit, "target") and not UnitIsUnit(self.unit, "player") then
		mark:SetAlpha(1)
	else
		mark:SetAlpha(0)
	end
end

oUF:RegisterStyle("Nameplates", function(self, unit)
	...
	local arrow = self:CreateTexture(nil, "OVERLAY")
	arrow:SetSize(50, 50)
	arrow:SetTexture(arrowTexture)
	arrow:SetPoint("BOTTOM", self, "TOP", 0, 14)
	arrow:SetAlpha(0)
	self.targetMark = arrow
	self:RegisterEvent("PLAYER_TARGET_CHANGED", UpdateTargetMark)
	...
end)

oUF:SpawnNamePlates("oUF_NPs", function(self, _, unit)
	...
	UpdateTargetMark(self)
	...
end)
I have no idea why I need to update it in two places to make it work.
  Reply With Quote
03-14-18, 02:06 AM   #9
runamonk
A Theradrim Guardian
 
runamonk's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 61
Thanks Siweia, I've gotten mine working.
  Reply With Quote
03-14-18, 09:57 AM   #10
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Originally Posted by siweia View Post
I have no idea why I need to update it in two places to make it work.
Because, as Phanx mentioned above, frames are only "spawned" once. If you want it to change/update, you need to run the function again.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
03-14-18, 10:06 AM   #11
p3lim
A Pyroguard Emberseer
 
p3lim's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 1,710
Originally Posted by siweia View Post
I have no idea why I need to update it in two places to make it work.
The function that you pass as a parameter to oUF:SpawnNameplates only fires when a new nameplate is created OR when a nameplate becomes the new target.
This does not mean the function will also run for the nameplate that was the previous target, hence the solution you had to come up with (which IMO is the correct solution).

To sum it up in code:
Lua Code:
  1. local function OnTargetChanged(frame)
  2.     -- do stuff when the target changed, regardless if this frame represents the new target
  3.     if UnitIsUnit(frame.unit, 'target') then
  4.         -- the frame is the new target
  5.     else
  6.         -- the frame is something else than the new target
  7.     end
  8. end
  9.  
  10. oUF:RegisterStyle('MyStyle', function(self, unit)
  11.     self:RegisterEvent('PLAYER_TARGET_CHANGED', OnTargetChanged)
  12. end)
  13.  
  14. oUF:SpawnNamePlates('MyStyle', OnTargetChanged)

Last edited by p3lim : 03-14-18 at 10:10 AM.
  Reply With Quote
03-14-18, 01:59 PM   #12
lightspark
A Rage Talon Dragon Guard
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 341
Originally Posted by p3lim View Post
The function that you pass as a parameter to oUF:SpawnNameplates only fires when a new nameplate is created OR when a nameplate becomes the new target.
Minor correction, it's called whenever a nameplate is added (either created or shown again), removed (hidden), or when it becomes the new target.

Originally Posted by siweia View Post
I have no idea why I need to update it in two places to make it work.
Well, it works because you update every single nameplate when PLAYER_TARGET_CHANGED fires, you can actually remove UpdateTargetMark from your spawn callback.

-- edit #1

aallkkaa's solution is a good one if you want to use only spawn callback. You need to update old target and new target nameplates, so you have to track the old target.

On a side note, we could probably remove PLAYER_TARGET_CHANGE handling from oUF nameplate driver entirely and make layout devs handle it on their own.
__________________

Last edited by lightspark : 03-14-18 at 02:29 PM.
  Reply With Quote
03-14-18, 07:38 PM   #13
siweia
A Flamescale Wyrmkin
 
siweia's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2011
Posts: 126
Originally Posted by lightspark View Post
you can actually remove UpdateTargetMark from your spawn callback.
It has to be in spawn callback as well.
If you set the cvar nameplateMaxDistance in 40 yards, but you select a unit outside 40 yards, the highlight border would not show up properly.
  Reply With Quote
03-15-18, 01:59 AM   #14
lightspark
A Rage Talon Dragon Guard
 
lightspark's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2012
Posts: 341
Originally Posted by siweia View Post
It has to be in spawn callback as well.
If you set the cvar nameplateMaxDistance in 40 yards, but you select a unit outside 40 yards, the highlight border would not show up properly.
Oh well... >_>
__________________

Last edited by lightspark : 03-15-18 at 02:01 AM.
  Reply With Quote
10-28-18, 12:13 PM   #15
Gilsuk
A Murloc Raider
Join Date: Sep 2018
Posts: 4
Lua Code:
  1. local GetNamePlateForUnit = C_NamePlate.GetNamePlateForUnit
  2. local previousTarget
  3.  
  4. local function OnTargetChanged()
  5.     local parent = GetNamePlateForUnit('target')
  6.     local currentTarget = parent and parent.unitFrame
  7.  
  8.     if currentTarget == previousTarget then return end
  9.  
  10.     if previousTarget then
  11.         previousTarget:SetBackdropBorderColor(0, 0, 0)
  12.         previousTarget = nil
  13.     end
  14.  
  15.     if currentTarget then
  16.         currentTarget:SetBackdropBorderColor(1, 1, 1)
  17.         previousTarget = currentTarget
  18.     end
  19. end
  20.  
  21. oUF:SpawnNamePlates('MyStyle', OnTargetChanged)
I use this code above and don't have any issues
  Reply With Quote

WoWInterface » Featured Projects » oUF (Otravi Unit Frames) » Highlight Targeted unit

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