kesava-wow / kuinameplates-customs

A small collection of customisations for Kui_Nameplates.
19 stars 6 forks source link

Enhanced custom.target-name.lua with player character class color and show on non friendly nameplates only #7

Open ceylina opened 7 years ago

ceylina commented 7 years ago

So I initially had you help me to figure out how to show target names on hostile units only (ended up on just NOT friendly units so it would also show on neutral).

As it went along using this I found it invaluable but really started wanting it to show the target, if a player, to be a class color. I had a little hiccup where I couldn't decide how to determine if it was a player character. Finally figured it out and it is working great. I have modified it to show NPC targets as grey to not confuse with class colors (hostility isn't really a concern with what the target is targeting).

I also added a text shadow so it is readable and modified the offset of the font (of course not needed but I wanted it in a certain position and offset).

-- add the unit's target's name above their name. Difficult to describe in a
-- coherent sentence.
local folder,ns=...
local addon = KuiNameplates
local kui = LibStub('Kui-1.0')
local core = KuiNameplatesCore
local mod = addon:NewPlugin('TargetName',101)

local UPDATE_INTERVAL = .1
local interval = 0

local update = CreateFrame('Frame')
update:SetScript('OnUpdate',function(self,elap)
    -- units changing target doesn't fire an event, so we have to check constantly
    interval = interval + elap

    if interval >= UPDATE_INTERVAL then
        interval = 0

        for _,f in addon:Frames() do
            if f:IsShown() and f.unit and (not f.state.friend) then         
                f.TargetName:SetText(UnitName(f.unit..'target'))

                    if UnitPlayerControlled(f.unit..'target') then
                        local r,g,b = kui.GetClassColour(f.unit..'target',2)
                        f.TargetName:SetTextColor(r,g,b)
                    else
                        f.TargetName:SetTextColor(0.6, 0.6, 0.6)
                    end         
            else
                f.TargetName:SetText('')
            end
        end
    end
end)

function mod:Create(frame)
    local tn = frame:CreateFontString(nil,'OVERLAY')
    tn:SetFont(frame.NameText:GetFont())
    tn:SetShadowColor(0,0,0,1)
    tn:SetShadowOffset(1,-1)
    tn:SetPoint('RIGHT',frame,'TOPRIGHT',-5,10)
    -- tn:SetPoint('CENTER',frame,'TOP')

    frame.TargetName = tn
end
function mod:Initialise()
    self:RegisterMessage('Create')
end