WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   oUF (Otravi Unit Frames) (https://www.wowinterface.com/forums/forumdisplay.php?f=87)
-   -   Highlight Targeted unit (https://www.wowinterface.com/forums/showthread.php?t=56078)

runamonk 03-03-18 01:15 PM

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

p3lim 03-03-18 06:07 PM

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)

runamonk 03-04-18 02:13 AM

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)


aallkkaa 03-04-18 03:05 PM

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)


Phanx 03-04-18 08:02 PM

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.

runamonk 03-05-18 02:24 AM

Good lord I thought I was anal retentive about naming conventions at the office, you're right though. :)

runamonk 03-05-18 03:14 AM

Thanks aallkkaa, I went with something similar. :)

siweia 03-14-18 01:43 AM

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.

runamonk 03-14-18 02:06 AM

Thanks Siweia, I've gotten mine working. :)

Seerah 03-14-18 09:57 AM

Quote:

Originally Posted by siweia (Post 327221)
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. ;)

p3lim 03-14-18 10:06 AM

Quote:

Originally Posted by siweia (Post 327221)
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)

lightspark 03-14-18 01:59 PM

Quote:

Originally Posted by p3lim (Post 327237)
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.

Quote:

Originally Posted by siweia (Post 327221)
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.

siweia 03-14-18 07:38 PM

Quote:

Originally Posted by lightspark (Post 327247)
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.

lightspark 03-15-18 01:59 AM

Quote:

Originally Posted by siweia (Post 327253)
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... >_>

Gilsuk 10-28-18 12:13 PM

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


All times are GMT -6. The time now is 11:55 AM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI