View Single Post
11-28-10, 06:43 PM   #33
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,327
Here's the modified function. The default values for the arguments noted below will apply if an argument is missing or is nil. Values marked required are as such.

Syntax:
SetBarValue(bar,val1,val2,flipH,flipV,cropx1,cropx2,cropy1,cropy2)

bar = Bar texture (required)
val1, val2 = Upper and lower values for the bar to be drawn (default 0)
flipH, flipV = Boolean true/false, flips texture horizontally/vertically if true (default false)
cropx1, cropx2, cropy1, cropy2 = Crop values, swapping high and low values within the xy pairs will not affect flipping (default 0, 1, 0, 1)


lua Code:
  1. local function SetBarValue(bar,val1,val2,flipH,flipV,cropx1,cropx2,cropy1,cropy2)-- Accepts 9 values, bar texture, start value, end value, flip flags, and crop offsets (any value exceptbar can be nil)
  2. --  Keep the values within a 0-1 range
  3.     val1=math.min(math.max(val1 or 0,0),1);
  4.     val2=math.min(math.max(val2 or 0,0),1);
  5.  
  6. --  Sometimes textures glitch when trying to set a width or height of 0, so handle it here
  7.     if val1==val2 then
  8.         bar:Hide();
  9.         return;
  10.     else
  11.         bar:Show();
  12.     end
  13.  
  14. --  Texture crop/padding
  15.     cropx1=math.min(math.max(cropx1 or 0,0),1);
  16.     cropx2=math.min(math.max(cropx2 or 1,0),1);
  17.     cropy1=math.min(math.max(cropy1 or 0,0),1);
  18.     cropy2=math.min(math.max(cropy2 or 1,0),1);
  19.  
  20. --  Set our low and high values
  21.     local low,high=math.min(val1,val2),math.max(val1,val2);
  22.  
  23. --  Find our parent width and height
  24.     local parent=bar:GetParent();
  25.     local pw,ph=parent:GetWidth(),parent:GetHeight();
  26.  
  27. --  Get our point set, we'll use this to get our bar orientation
  28.     local point=bar:GetPoint(1);
  29.     if not point then return; end-- Handle if point isn't set, perhaps cleared?
  30.  
  31. --  These are our temp origin variables (note one of these pairs can have both vars nil, just use default texcoords for those)
  32.     local origL=point:find("LEFT$");
  33.     local origR=point:find("RIGHT$");
  34.     local origT=point:find("^TOP");
  35.     local origB=point:find("^BOTTOM");
  36.  
  37. --  Reset point
  38.     bar:SetPoint(point
  39.         ,(origL or origR) and (origL and low*pw or -low*pw) or 0--  X offset
  40.         ,(origT or origB) and (origB and low*ph or -low*ph) or 0--  Y offset
  41.     );
  42.  
  43. --  Texcoord calc
  44.     local l,r,t,b=0,1,0,1;--    Texcoords are in the range of 0-1 with their origin at top left
  45.     if origL then
  46.         l,r=low,high;
  47.     elseif origR then
  48.         l,r=1-high,1-low;
  49.     end
  50.     if origT then
  51.         t,b=low,high;
  52.     elseif origB then
  53.         t,b=1-high,1-low;
  54.     end
  55.  
  56. --  Process flip (affects which side the bar piece is drawn from too)
  57.     if flipH then l,r=1-l,1-r; end
  58.     if flipV then t,b=1-t,1-b; end
  59.  
  60. --  Constrain to crop size
  61.     local cropL,cropR,cropT,cropB=math.min(cropx1,cropx2),math.max(cropx1,cropx2),math.min(cropy1,cropy2),math.max(cropy1,cropy2);
  62.     local cropW,cropH=cropR-cropL,cropB-cropT;
  63.     l,r,t,b=l*cropW+cropL,r*cropW+cropL,t*cropH+cropT,b*cropH+cropT;
  64.  
  65. --  Apply texcord/size
  66.     bar:SetTexCoord(l,r,t,b);
  67.     bar:SetWidth(pw*((origL or origR) and math.abs(high-low) or 1));
  68.     bar:SetHeight(ph*((origT or origB) and math.abs(high-low) or 1));
  69. 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)
  Reply With Quote