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:
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)
-- Keep the values within a 0-1 range
val1=math.min(math.max(val1 or 0,0),1);
val2=math.min(math.max(val2 or 0,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
-- Texture crop/padding
cropx1=math.min(math.max(cropx1 or 0,0),1);
cropx2=math.min(math.max(cropx2 or 1,0),1);
cropy1=math.min(math.max(cropy1 or 0,0),1);
cropy2=math.min(math.max(cropy2 or 1,0),1);
-- 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 pw,ph=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
bar:SetPoint(point
,(origL or origR) and (origL and low*pw or -low*pw) or 0-- X offset
,(origT or origB) and (origB and low*ph or -low*ph) or 0-- Y offset
);
-- Texcoord calc
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=low,high;
elseif origR then
l,r=1-high,1-low;
end
if origT then
t,b=low,high;
elseif origB then
t,b=1-high,1-low;
end
-- Process flip (affects which side the bar piece is drawn from too)
if flipH then l,r=1-l,1-r; end
if flipV then t,b=1-t,1-b; end
-- Constrain to crop size
local cropL,cropR,cropT,cropB=math.min(cropx1,cropx2),math.max(cropx1,cropx2),math.min(cropy1,cropy2),math.max(cropy1,cropy2);
local cropW,cropH=cropR-cropL,cropB-cropT;
l,r,t,b=l*cropW+cropL,r*cropW+cropL,t*cropH+cropT,b*cropH+cropT;
-- Apply texcord/size
bar:SetTexCoord(l,r,t,b);
bar:SetWidth(pw*((origL or origR) and math.abs(high-low) or 1));
bar:SetHeight(ph*((origT or origB) and math.abs(high-low) or 1));
end