Template
Code: Select all
[Fade]
Measure=Script
ScriptFile=Fade.lua
; OPTIONAL SETTINGS:
Min=0
Max=255
Step=10
Start=255
MeterName=Meter1|Meter2|Meter3
This measure returns a number value. It's meant to be added to meters as a dynamic variable, as in:
- ImageAlpha=[Fade]
- FontColor=255,255,255,[Fade]
- SolidColor=32,64,128,[Fade]
Normally, the measure value stays constant. The transition is triggered by a bang:
- !CommandMeasure "Fade" "Activate()"
- !CommandMeasure "Fade" "Activate('In')"
- !CommandMeasure "Fade" "Activate('Out')"
- !CommandMeasure "Fade" "Deactivate()"
Min and Max set the range of the transition. They default to 0 and 255, which means the meter will change from 0 (fully transparent) to 255 (fully opaque) and back.
Step is the amount that the value changes each update. The update speed follows the global Update and local UpdateDivider.
Start is the initial value when the skin is loaded. Defaults to 255 (fully opaque).
Obviously, a smooth transition requires a very high update speed - around 50 or 100 ms. However, your meters do not need to update at the same speed as the script. If you add them to MeterNames, separated by pipes (|), the script will automatically update them at high speed, but only during the transition. Afterward, they will return to their original update speed according to their UpdateDividers. This will save on resources if you are fading a large number of meters, or very heavy images.
Script
Code: Select all
function Initialize()
MeterName = SELF:GetOption('MeterName', '')
--The name of the meter or meters that will be faded.
Meters = {}
for a in string.gmatch(MeterName, '[^%|]+') do
table.insert(Meters, a)
end
Max = SELF:GetNumberOption('Max', 255)
Min = SELF:GetNumberOption('Min', 0)
--The upper and lower bounds of the alpha range. Default to 255 and 0, respectively.
Alpha = SELF:GetNumberOption('Start', Max)
--The original alpha of the meter.
Step = SELF:GetNumberOption('Step', 1)
--The amount by which the meter alpha will increase or decrease each update. Defaults to 1.
Activated = 0
--When this variable is changed to 1 from the skin, the animation will begin.
end
function Activate(ForceType)
if ForceType == 'In' or ((math.abs(Min - Alpha) < math.abs(Max - Alpha)) and ForceType ~= 'Out') then
Direction = 1
Target = Max
else
Direction = -1
Target = Min
end
Activated = 1
end
function Update()
if Activated == 1 then
Alpha = Alpha + Step * Direction
if Alpha < Min or Alpha > Max then
Alpha = Target
Activated=2
end
for i,v in ipairs(Meters) do SKIN:Bang('!UpdateMeter', v) end
elseif Activated == 2 then
for i,v in ipairs(Meters) do SKIN:Bang('!UpdateMeter', v) end
Deactivate()
-- The "Activated == 2" case makes sure that the meter is updated one more time after the transition is deactivated.
end
return Alpha
end
function Deactivate()
Activated = 0
end
I recycled an old skin of mine to demonstrate the fading effect. It reads lines from a file and cross-fades the text from one line to the next. It's simple, but I have to say the effect is surprisingly smooth. :)