local frame=CreateFrame("Frame",nil,UIParent);
frame:SetPoint("CENTER");-- Place frame wherever
frame:SetWidth(128);-- Width
frame:SetHeight(16);-- Height
-- Note: It's generally a good idea to use the same dimensions as your image file for your frame size
-- Background texture
frame.Background=frame:CreateTexture(nil,"BACKGROUND");
frame.Background:SetAllPoints(frame);-- We want the texture spread to match the dimensions of the frame.
frame.Background:SetTexture("Interface\\AddOns\\AddOnName\\ImageName");-- Your bar image
frame.Background:SetVertexColor(0,0,0,0.25)-- Special suprize (black background at a quarter alpha)
-- Health texture
frame.HealthBar=frame:CreateTexture(nil,"ARTWORK");-- Notice the change in layers, this makes the bar draw on top of the background
frame.HealthBar:SetPoint("LEFT");-- We need only one point set, in this example, the bar fills toward the right, so we need it left-aligned
frame.HealthBar:SetWidth(128);-- Match frame width for now
frame.HealthBar:SetHeight(16);-- Match frame height for now
frame.HealthBar:SetTexture("Interface\\AddOns\\AddOnName\\ImageName");-- Be sure to use the same image or one derived from the background
-- Heal prediction texture (this'll be my example of a partial bar)
frame.HealBar=frame:CreateTexture(nil,"ARTWORK");-- Use the same layer as Healthbar
frame.HealBar:SetPoint("LEFT");-- Use the same point for the HealthBar
frame.HealthBar:SetWidth(128);-- Match frame width for now
frame.HealthBar:SetHeight(16);-- Match frame height for now
frame.HealBar:SetTexture("Interface\\AddOns\\AddOnName\\ImageName");-- Be sure to use the same image or one derived from the background
-- Bar positioning func
local function SetBarValue(bar,val1,val2)-- Accepts 3 values, bar texture, start value, and end value
-- Keep the values within a 0-1 range
val1=math.min(math.max(val1,0),1);
val2=math.min(math.max(val2,0),1);
-- Sometimes textures glitch when trying to set a width or height of 0, so handle it here
if val1==val2 then
bar:Hide();
return;
else
bar:Show();
end
-- Set our low and high values
local low,high=math.min(val1,val2),math.max(val1,val2);
-- Find our parent width and height
local parent=bar:GetParent();
local w,h=parent:GetWidth(),parent:GetHeight();
-- Get our point set, we'll use this to get our bar orientation
local point=bar:GetPoint(1);
if not point then return; end-- Handle if point isn't set, perhaps cleared?
-- These are our temp origin variables (note one of these pairs can have both vars nil, just use default texcoords for those)
local origL=point:find("LEFT$");
local origR=point:find("RIGHT$");
local origT=point:find("^TOP");
local origB=point:find("^BOTTOM");
-- Reset point (logic nightmare)
bar:SetPoint(point
,(origL or origR) and (origL and low*w or -low*w) or 0-- X offset
,(origT or origB) and (origB and low*h or -low*h) or 0-- Y offset
);
-- Texcoord/Size set (this is actually the easy part)
local l,r,t,b=0,1,0,1;-- Texcoords are in the range of 0-1 with their origin at top left
if origL then
l,r=high,low;
elseif origR then
l,r=1-high,1-low;
end
if origT then
t,b=high,low;
elseif origB then
t,b=1-high,1-low;
end
bar:SetTexCoord(l,r,t,b);
bar:SetWidth(w*(r-l));
bar:SetHeight(h*(b-t));
end
frame:RegisterEvent("UNIT_HEALTH");
frame:RegisterEvent("UNIT_HEALTH_PREDICTION");
frame:SetScript("OnEvent",function(self,event,unit)
-- Stop handler if unit not player
if not UnitIsUnit(unit,"player") then return; end
-- Temp vars
local hcur,hmax,heal=UnitHealth(unit),UnitHealthMax(unit),UnitGetIncomingHeals(unit) or 0;-- UnitGetIncomingHeals() can return nil for unsupported units
local hppcnt=hcur/math.max(1,hmax);
local healpcnt=heal/math.max(1,hmax);
-- Set our bars (notice the HealBar is a partial bar since it has a start value greater than zero)
SetBarValue(self.HealthBar,0,hppcnt);
SetBarValue(self.HealBar,hppcnt,hppcnt+healpcnt);
-- Color HealthBar (we're keeping HealBar white)
-- This color formula has been reused from my HUD projects, it ranges in color from green, to yellow, to red
self.HealthBar:SetVertexColor(math.min(2-2*hppcnt,1),math.min(2*hppcnt,1),0);
end