View Single Post
11-26-10, 07:06 AM   #14
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Here's an example of some some code that I've used in my HUD addons. I've made a couple that haven't made it to release phase due to laziness or I abandoned it in mid-project (both happen a lot ). This was rewritten from scratch, but it follows the same idea. I haven't run it through the game client, so there might be some glitches. I don't have access to an active WoW account right now.

lua Code:
  1. local frame=CreateFrame("Frame",nil,UIParent);
  2. frame:SetPoint("CENTER");-- Place frame wherever
  3. frame:SetWidth(128);--      Width
  4. frame:SetHeight(16);--      Height
  5. --  Note: It's generally a good idea to use the same dimensions as your image file for your frame size
  6.  
  7. --  Background texture
  8. frame.Background=frame:CreateTexture(nil,"BACKGROUND");
  9. frame.Background:SetAllPoints(frame);--                     We want the texture spread to match the dimensions of the frame.
  10. frame.Background:SetTexture("Interface\\AddOns\\AddOnName\\ImageName");--   Your bar image
  11. frame.Background:SetVertexColor(0,0,0,0.25)--                   Special suprize (black background at a quarter alpha)
  12.  
  13. --  Health texture
  14. frame.HealthBar=frame:CreateTexture(nil,"ARTWORK");--               Notice the change in layers, this makes the bar draw on top of the background
  15. 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
  16. frame.HealthBar:SetWidth(128);--                        Match frame width for now
  17. frame.HealthBar:SetHeight(16);--                        Match frame height for now
  18. frame.HealthBar:SetTexture("Interface\\AddOns\\AddOnName\\ImageName");--    Be sure to use the same image or one derived from the background
  19.  
  20. --  Heal prediction texture (this'll be my example of a partial bar)
  21. frame.HealBar=frame:CreateTexture(nil,"ARTWORK");--         Use the same layer as Healthbar
  22. frame.HealBar:SetPoint("LEFT");--                   Use the same point for the HealthBar
  23. frame.HealthBar:SetWidth(128);--                    Match frame width for now
  24. frame.HealthBar:SetHeight(16);--                    Match frame height for now
  25. frame.HealBar:SetTexture("Interface\\AddOns\\AddOnName\\ImageName");--  Be sure to use the same image or one derived from the background
  26.  
  27. --  Bar positioning func
  28. local function SetBarValue(bar,val1,val2)-- Accepts 3 values, bar texture, start value, and end value
  29. --  Keep the values within a 0-1 range
  30.     val1=math.min(math.max(val1,0),1);
  31.     val2=math.min(math.max(val2,0),1);
  32.  
  33. --  Sometimes textures glitch when trying to set a width or height of 0, so handle it here
  34.     if val1==val2 then
  35.         bar:Hide();
  36.         return;
  37.     else
  38.         bar:Show();
  39.     end
  40.  
  41. --  Set our low and high values
  42.     local low,high=math.min(val1,val2),math.max(val1,val2);
  43.  
  44. --  Find our parent width and height
  45.     local parent=bar:GetParent();
  46.     local w,h=parent:GetWidth(),parent:GetHeight();
  47.  
  48. --  Get our point set, we'll use this to get our bar orientation
  49.     local point=bar:GetPoint(1);
  50.     if not point then return; end-- Handle if point isn't set, perhaps cleared?
  51.  
  52. --  These are our temp origin variables (note one of these pairs can have both vars nil, just use default texcoords for those)
  53.     local origL=point:find("LEFT$");
  54.     local origR=point:find("RIGHT$");
  55.     local origT=point:find("^TOP");
  56.     local origB=point:find("^BOTTOM");
  57.  
  58. --  Reset point (logic nightmare)
  59.     bar:SetPoint(point
  60.         ,(origL or origR) and (origL and low*w or -low*w) or 0--    X offset
  61.         ,(origT or origB) and (origB and low*h or -low*h) or 0--    Y offset
  62.     );
  63.  
  64. --  Texcoord/Size set (this is actually the easy part)
  65.     local l,r,t,b=0,1,0,1;--    Texcoords are in the range of 0-1 with their origin at top left
  66.     if origL then
  67.         l,r=high,low;
  68.     elseif origR then
  69.         l,r=1-high,1-low;
  70.     end
  71.     if origT then
  72.         t,b=high,low;
  73.     elseif origB then
  74.         t,b=1-high,1-low;
  75.     end
  76.     bar:SetTexCoord(l,r,t,b);
  77.     bar:SetWidth(w*(r-l));
  78.     bar:SetHeight(h*(b-t));
  79. end
  80.  
  81. frame:RegisterEvent("UNIT_HEALTH");
  82. frame:RegisterEvent("UNIT_HEALTH_PREDICTION");
  83. frame:SetScript("OnEvent",function(self,event,unit)
  84. --  Stop handler if unit not player
  85.     if not UnitIsUnit(unit,"player") then return; end
  86.  
  87. --  Temp vars
  88.     local hcur,hmax,heal=UnitHealth(unit),UnitHealthMax(unit),UnitGetIncomingHeals(unit) or 0;--    UnitGetIncomingHeals() can return nil for unsupported units
  89.     local hppcnt=hcur/math.max(1,hmax);
  90.     local healpcnt=heal/math.max(1,hmax);
  91.  
  92. --  Set our bars (notice the HealBar is a partial bar since it has a start value greater than zero)
  93.     SetBarValue(self.HealthBar,0,hppcnt);
  94.     SetBarValue(self.HealBar,hppcnt,hppcnt+healpcnt);
  95.  
  96. --  Color HealthBar (we're keeping HealBar white)
  97. --  This color formula has been reused from my HUD projects, it ranges in color from green, to yellow, to red
  98.     self.HealthBar:SetVertexColor(math.min(2-2*hppcnt,1),math.min(2*hppcnt,1),0);
  99. end
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 11-27-10 at 10:40 PM.
  Reply With Quote