Code:
--Globals to locals to prevent errors that could occur when a function is edited.
local GetNumGroupMembers = GetNumGroupMembers
local RAID_CLASS_COLORS = RAID_CLASS_COLORS
local UnitHealthMax = UnitHealthMax
local CreateFrame = CreateFrame
local UnitInRange = UnitInRange
local UnitHealth = UnitHealth
local UnitClass = UnitClass
local IsInGroup = IsInGroup
local UnitName = UnitName
local IsInRaid = IsInRaid
local GetTime = GetTime
local unpack = unpack
local function round( num )
return num >= 0 and math.floor( num + 0.5 ) or math.ceil( num - 0.5 )
end
local function HexToRGBA( I )
I = I:gsub( "#","" ):gsub( " ","" )
return {
r = tonumber( I:sub( 1, 2 ), 16 ) / 255,
g = tonumber( I:sub( 3, 4 ), 16 ) / 255,
b = tonumber( I:sub( 5, 6 ), 16) / 255,
a = string.sub( I, 7, 8 ) ~= "" and tonumber( I:sub( 7 ,8 ), 16 ) / 255 or 1
}
end
local MouseDownColor = HexToRGBA( "ffa500" )
local NoUnitColor = HexToRGBA( "aaaaaa" )
local function Create_Addon( lui )
LowestUnitFrame = LowestUnitFrame or {}
lui:SetPoint( LowestUnitFrame.pos or "CENTER", LowestUnitFrame.x or 0, LowestUnitFrame.y or 0 )
lui:SetFrameLevel( 0 )
lui:SetWidth( 180 )
lui:SetHeight( 20 )
lui:SetMovable( true )
lui.alpha = 0 -- float for alpha channel
lui.fade = false -- int for fade
lui.lui_texture = lui:CreateTexture( nil, "HIGH" ) -- Texture frame
lui.lui_texture:SetTexture( 0, 0, 0, 0 )
lui.lui_texture:SetAllPoints( lui )
lui.text = lui:CreateFontString( nil, "ARTWORK", "NumberFont_Shadow_Med" ) -- Text Frame
lui.text:SetAllPoints( true )
lui.text:SetJustifyH( "LEFT" )
lui:SetScript( "OnMouseDown", function( self, button )
if not self.isMoving then
self:StartMoving()
self.isMoving = true
self.alpha = 1
self.fade = false
self.lui_texture:SetTexture( MouseDownColor.r, MouseDownColor.g, MouseDownColor.b, MouseDownColor.a )
end
end )
lui:SetScript( "OnMouseUp", function( self, button )
if self.isMoving then
self:StopMovingOrSizing()
self.isMoving = false
self.fade = true
self.fadestart = GetTime()
end
end )
lui:SetScript( "OnUpdate", function( self, elapsed )
if self.isMoving then
local pos, _, _, x, y = self:GetPoint()
LowestUnitFrame.pos = pos
LowestUnitFrame.x = round( x )
LowestUnitFrame.y = round( y )
self:SetPoint( LowestUnitFrame.pos, LowestUnitFrame.x, LowestUnitFrame.y )
end
self.t = self.t and self.t + elapsed or elapsed
if self.t >= 0.1 then
self.t = 0 --reset timer
local Unit = ( IsInRaid() and "RAID" ) or ( IsInGroup() and "PARTY" ) or false
local LowestUnit = "player"
local LowestHealth = UnitHealth( LowestUnit ) / UnitHealthMax( LowestUnit ) * 100
if Unit then
-- Get Lowest Unit
for i = 1, GetNumGroupMembers() do
local health = UnitHealth( Unit .. i ) / UnitHealthMax( Unit .. i ) * 100
if UnitInRange( Unit .. i ) and health < LowestHealth and health > 0 then
LowestHealth = health
LowestUnit = Unit .. i
end
end
end
-- Edit frame with new info
if LowestHealth < 100 then
self.text:SetText( string.format( "%s %.2f%%", UnitName( LowestUnit ), LowestHealth ) )
local _, Class = UnitClass( LowestUnit )
local tbl = RAID_CLASS_COLORS[ Class ] -- This makes it ready for the future
self.text:SetTextColor( tbl.r or 0, tbl.g or 0, tbl.b or 0 ) -- nil check just in case
else
self.text:SetText( "Lowest Unit Frame" )
self.text:SetTextColor( NoUnitColor.r, NoUnitColor.g, NoUnitColor.b )
end
end
-- check if we need to fade the frame
if self.fade then
self.alpha = 1 - ( ( GetTime() - self.fadestart ) / 0.5 )
self.lui_texture:SetTexture( MouseDownColor.r, MouseDownColor.g, MouseDownColor.b, self.alpha > 0 and self.alpha or 0 )
if self.alpha < 0 then
self.fade = false
end
end
end )
end
local LowestUnitInterface = CreateFrame( "Frame", UIParent ) -- Main frame
LowestUnitInterface:RegisterEvent( "ADDON_LOADED" )
LowestUnitInterface:SetScript( "OnEvent", function( self, _, name )
if name == "LowestUnitFrame" then -- Make sure it is this addon
self:SetScript( "OnEvent", nil ) -- remove event
self:UnregisterEvent( "ADDON_LOADED" )
Create_Addon( self ) -- continue
end
end )
mods.curse.com/addons/wow/lowestunitframe