JaviGr

Untitled

Jul 3rd, 2018
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function FadeMeter(meter, startAlpha, endAlpha, speed)
  2.     local startAlpha = startAlpha or 255    --"default" starting alpha
  3.     local endAlpha = endAlpha or 0  --"default ending alpha
  4.     local speed = speed or 0.1
  5.     local meter = SKIN:GetMeter(meter)  --get the meter to fade
  6.    
  7.     if meter == nil then print("Error: "..meter.." is not a valid meter.") return end   --some simple error checking
  8.     if (startAlpha > 255 or startAlpha < 0) or (endAlpha > 255 or endAlpha < 0) then print("Error: Alpha value out of range, must be between 0-255") return end
  9.    
  10.     if startAlpha > endAlpha then speed = -speed end    --counting down instead of up
  11.    
  12.     local meterType = string.lower(meter:GetOption("Meter"))    --get the "type" of meter (ie. String, Bar, etc.)
  13.     local config = SKIN:GetVariable("CURRENTCONFIG")
  14.    
  15.     local solidColor, solidColor2 = nil, nil
  16.     if meter:GetOption("SolidColor") ~= "" then solidColor = stripAlpha(meter:GetOption("SolidColor")) end
  17.     if meter:GetOption("SolidColor2") ~= "" then solidColor2 = stripAlpha(meter:GetOption("SolidColor2")) end
  18.    
  19.     if meterType == "bar" then
  20.         local barColor = stripAlpha(meter:GetOption("BarColor"))
  21.         local imageAlpha = meter:GetOption("ImageAlpha")
  22.         setAlpha(meter:GetName(), config, startAlpha, endAlpha, speed, "SolidColor", solidColor, "SolidColor2", solidColor2, "BarColor", barColor, "ImageAlpha", imageAlpha)
  23.     elseif meterType == "bitmap" or meterType == "button" or meterType == "image" or meterType == "rotator" then
  24.         local imageAlpha = meter:GetOption("ImageAlpha")
  25.         setAlpha(meter:GetName(), config, startAlpha, endAlpha, speed, "SolidColor", solidColor, "SolidColor2", solidColor2, "ImageAlpha", imageAlpha)
  26.     elseif meterType == "histogram" then
  27.         local primaryColor = stripAlpha(meter:GetOption("PrimaryColor"))
  28.         local secondaryColor = stripAlpha(meter:GetOption("SecondaryColor"))
  29.         local bothColor = stripAlpha(meter:GetOption("BothColor"))
  30.         local primaryImageAlpha = meter:GetOption("PrimaryImageAlpha")
  31.         local secondaryImageAlpha = meter:GetOption("SecondaryImageAlpha")
  32.         local bothImageAlpha = meter:GetOption("BothImageAlpha")
  33.         setAlpha(meter:GetName(), config, startAlpha, endAlpha, speed, "SolidColor", solidColor, "SolidColor2", solidColor2, "PrimaryColor", primaryColor, "SecondaryColor", secondaryColor, "BothColor", bothColor, "PrimaryImageAlpha", primaryImageAlpha, "SecondaryImageAlpha", secondaryImageAlpha, "BothImageAlpha", bothImageAlpha)
  34.     elseif meterType == "line" then
  35.         SKIN:Bang("!ShowMeter "..meter:GetName().." "..config)
  36.         local horizontalLineColor = stripAlpha(meter:GetOption("HorizontalLineColor"))
  37.         local lineColor = { }
  38.         lineColor[1] = stripAlpha(meter:GetOption("LineColor"))
  39.         local i = 2
  40.         repeat
  41.             local found = false
  42.             if meter:GetOption("MeasureName"..i) == "" then found = true else lineColor[i] = stripAlpha(meter:GetOption("LineColor"..i)) end
  43.             i = i + 1
  44.         until (found == true)
  45.         for i=startAlpha, endAlpha, speed do
  46.             if ((speed > 0) and ((i + speed) > endAlpha)) or ((speed < 0) and ((i + speed) < endAlpha)) then i = endAlpha end
  47.             if horizontalLineColor ~= nil then SKIN:Bang("!SetOption "..meter:GetName().." HorizontalLineColor "..horizontalLineColor..i.." "..config) end
  48.             if solidColor ~= nil then SKIN:Bang("!SetOption "..meter:GetName().." SolidColor "..solidColor..i.." "..config) end
  49.             if solidColor2 ~= nil then SKIN:Bang("!SetOption "..meter:GetName().." SolidColor2 "..solidColor2..i.." "..config) end
  50.             if lineColor[1] ~= nil then SKIN:Bang("!SetOption "..meter:GetName().." LineColor "..lineColor[1]..i.." "..config) end
  51.             for j=2, #lineColor do SKIN:Bang("!SetOption "..meter:GetName().." LineColor"..j.." "..lineColor[j]..i.." "..config) end
  52.             SKIN:Bang("!UpdateMeter "..meter:GetName().." "..config)
  53.             SKIN:Bang("!Redraw "..config)
  54.         end
  55.     elseif meterType == "roundline" then
  56.         local lineColor = stripAlpha(meter:GetOption("LineColor"))
  57.         setAlpha(meter:GetName(), config, startAlpha, endAlpha, speed, "SolidColor", solidColor, "SolidColor2", solidColor2, "LineColor", lineColor)
  58.     elseif meterType == "string" then
  59.         local fontColor = stripAlpha(meter:GetOption("FontColor"), "0,0,0,")
  60.         local effectColor = stripAlpha(meter:GetOption("FontEffectColor"), "0,0,0,")
  61.         setAlpha(meter:GetName(), config, startAlpha, endAlpha, speed, "SolidColor", solidColor, "SolidColor2", solidColor2, "FontColor", fontColor, "FontEffectColor", effectColor)
  62.     end
  63.    
  64.     if endAlpha == 0 then   --Hide the meter if the Alpha value is 0
  65.         SKIN:Bang("!HideMeter "..meter:GetName().." "..config)
  66.     end
  67. end
  68.  
  69. function stripAlpha(option, defaultValue)
  70.     local defaultValue = defaultValue or "255,255,255,"
  71.     if option == "" or option == nil then
  72.         if string.sub(defaultValue, -1) ~= "," then defaultValue = defaultValue.."," end    --add a comma at the end of the string if none is provided
  73.         option = defaultValue
  74.     elseif string.find(option, ",") then
  75.         option = string.join(string.split(option, ",", 3), ",")..","
  76.     else
  77.         local temp = { }
  78.         local j = 1
  79.         for i=1, 6, 2 do
  80.             temp[j] = tonumber(string.sub(option, i, i+1), 16)
  81.             j = j + 1
  82.         end
  83.         option = string.join(temp, ",")..","
  84.     end
  85.     return option
  86. end
  87.  
  88. function setAlpha(meterName, config, startAlpha, endAlpha, speed, ...)
  89.     SKIN:Bang("!ShowMeter "..meterName.." "..config)
  90.     local i, j
  91.     for i=startAlpha, endAlpha, speed do
  92.         if ((speed > 0) and ((i + speed) > endAlpha)) or ((speed < 0) and ((i + speed) < endAlpha)) then
  93.             i = endAlpha    --close enough to the endAlpha value
  94.         end
  95.         for j=1, arg['n'], 2 do
  96.             if string.find(arg[j], "Alpha") then
  97.                 SKIN:Bang("!SetOption "..meterName.." "..arg[j].." "..i.." "..config)   --if the option is a single number (ie. ImageAlpha)
  98.             elseif arg[j+1] ~= nil then
  99.                 SKIN:Bang("!SetOption "..meterName.." "..arg[j].." "..arg[j+1]..i.." "..config) --if option is a comma separated value (ie. FontColor)
  100.             end
  101.         end
  102.         SKIN:Bang("!UpdateMeter "..meterName.." "..config)
  103.         SKIN:Bang("!Redraw "..config)
  104.     end
  105. end
  106.  
  107. string.join = function(myTable, deliminator)
  108.     if deliminator == nil then return table.concat(myTable)
  109.     else return table.concat(myTable, deliminator) end
  110. end
  111.  
  112. string.split = function(myString, deliminator, maxIndex)
  113.     if string.find(myString, deliminator) == nil then return {myString} end --return the string (as a table) if no deliminator is found
  114.     if maxIndex == nil or maxIndex < 1 then maxIndex = 0 end    --maxIndex is the number of index's to return (0 for unlimited)
  115.     local result = {}
  116.     local pattern = "(.-)"..deliminator.."()"
  117.     local index = 0
  118.     local lastPosition
  119.     for match, position in string.gmatch(myString, pattern) do
  120.         index = index + 1
  121.         result[index] = match
  122.         lastPosition = position
  123.         if index == maxIndex then break end
  124.     end
  125.     if index ~= maxIndex then result[index + 1] = string.sub(myString, lastPosition) end
  126.     return result
  127. end
Add Comment
Please, Sign In to add comment