It is currently October 9th, 2024, 5:01 am

Smooth Meter Fade In/Out

Discuss the use of Lua in Script measures.
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Smooth Meter Fade In/Out

Post by Kaelri »

Just another way to get a "fading" transition on a meter. I know there are a couple of these out there now, but one more can't hurt. Credit goes to TheCompWiz for giving me the idea.

Template

Code: Select all

[Fade]
Measure=Script
ScriptFile=Fade.lua
; OPTIONAL SETTINGS:
Min=0
Max=255
Step=10
Start=255
MeterName=Meter1|Meter2|Meter3
Usage

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]
Commands

Normally, the measure value stays constant. The transition is triggered by a bang:
  • !CommandMeasure "Fade" "Activate()"
The script is smart enough to fade in when the current value is closer to transparent, and fade out when it's closer to opaque. However, you can also force one action or the other with a parameter:
  • !CommandMeasure "Fade" "Activate('In')"
  • !CommandMeasure "Fade" "Activate('Out')"
It can also be interrupted with
  • !CommandMeasure "Fade" "Deactivate()"
Options

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
Demo

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. :)
Signature_0.1.rmskin
You do not have the required permissions to view the files attached to this post.
User avatar
KreAch3R
Posts: 608
Joined: February 7th, 2011, 7:27 pm
Location: Thessaloniki, GR

Re: Smooth Meter Fade In/Out

Post by KreAch3R »

....... O.O The smoothness is unbelievably good and my 6year old pc running win7 isn't complaining at all!
Inactive, due to life changes. Send me a PM for any question.

Desktop DeviartArt
Image
User avatar
TweaknFreak
Posts: 217
Joined: July 14th, 2012, 7:26 am

Re: Smooth Meter Fade In/Out

Post by TweaknFreak »

Hey,I used your script and its AWSUM!!! But I aint satisfied with this. Now I want my custom made Start orb(to be used in Rainmeter only,not a real start orb) to fade out to value 50. Then again it starts again to fade in,i.e, some kinda pulsating effect. What I could manage to code was this.

Code: Select all

[Rainmeter]
BackgroundMode=1
Author=TweaknFreak
Update=1000
MiddleMouseDownAction=[!Refresh]

[MeasureFade]
Measure=Script
ScriptFile=Fade.lua
MeterName=Start1|Start2
Min=50
Max=255
Step=5
Start=255
IfBelowlValue=51
IfBelowAction=!Execute [!ShowMeter Start2][!HideMeter Start1]
IfAboveValue=254
IfAboveAction=!Execute [!ShowMeter Start1][!HideMeter Start2]

[MeasureRAM]
Measure=PhysicalMemory
IfAboveValue=1
IfAboveAction=!CommandMeasure "MeasureFade" "Activate()"

[Start1]
Meter=Button
ButtonImage=start1.png
x=44
y=48
ImageTint=0,245,255,[MeasureFade]
Antialias=1
ButtonCommand=!Execute ["#CURRENTPATH#OpenStartMenu.exe"]
Hidden=1
DynamicVariables=1

[Start2]
Meter=Button
ButtonImage=start1.png
x=44
y=48
ImageTint=0,245,255,(255-[MeasureFade])
Antialias=1
ButtonCommand=!Execute ["#CURRENTPATH#OpenStartMenu.exe"]
Hidden=1
DynamicVariables=1
The problem is,it starts but after reaching value 50,it doesnt increase to give a fade in effect.
What I tried to do was :
1. Measure Physical memory which is always greater than 1(atleast I thought so!).
2. This triggers your script.
3. Now I have two start orb meters using the same button image,coordinates and width.
4. first time Start1 meter is triggered in association with your script.This fades out.
5. When it reaches value 50,I disable this meter and start the Start2 meter which has ImageAlpha set to (255-[MeasureFade]).This is supposed to bring the fade in effect(But, :( it doesnt).
6. Then this meter after reaching 50 disables itself and triggers the Start1 meter again.
7. the cycle is repeated from STEP 4.

Please suggest me how to overcome this problem.Thanx in advance.
Be one of the iNEViTABLES - Storm, Earthquake, Lightning, Flood - and the world will bow down to you.

My current desktop
[hsimg]http://i1313.photobucket.com/albums/t559/TweaknFreak/6-22-20149-46-40PM_zps7bcbc1f6.png[/hsimg]
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: Smooth Meter Fade In/Out

Post by Kaelri »

You misspelled "IfBelowValue." :)

Code: Select all

[MeasureFade]
Measure=Script
ScriptFile=Fade.lua
MeterName=Start1|Start2
Min=50
Max=255
Step=5
Start=255
IfBelowValue=51
IfBelowAction=!CommandMeasure MeasureFade Activate()
IfAboveValue=254
IfAboveAction=!CommandMeasure MeasureFade Activate()
User avatar
TweaknFreak
Posts: 217
Joined: July 14th, 2012, 7:26 am

Re: Smooth Meter Fade In/Out

Post by TweaknFreak »

Kaelri wrote:You misspelled "IfBelowValue." :)

Code: Select all

[MeasureFade]
Measure=Script
ScriptFile=Fade.lua
MeterName=Start1|Start2
Min=50
Max=255
Step=5
Start=255
IfBelowValue=51
IfBelowAction=!CommandMeasure MeasureFade Activate()
IfAboveValue=254
IfAboveAction=!CommandMeasure MeasureFade Activate()
Nah,its not working. I think you misunderstood me. I have my Start1 and Start2 meters to show fade out and fade in effect respectively in alternate turns.Your code didnt provide any way how to switch between them for the transition.
All I want is to fade out Start1 and fade in Start2 successively to have the full,pulsating effect.But thanx for replying.
Be one of the iNEViTABLES - Storm, Earthquake, Lightning, Flood - and the world will bow down to you.

My current desktop
[hsimg]http://i1313.photobucket.com/albums/t559/TweaknFreak/6-22-20149-46-40PM_zps7bcbc1f6.png[/hsimg]
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: Smooth Meter Fade In/Out

Post by Kaelri »

Ah, I see now. (I had thought you wanted to cross-fade the meters simultaneously.)

I'm not really sure why you're using two meters, since they appear to be the same image and color. Nonetheless, you can just use two instances of the Fade meter to do this. Something like:

Code: Select all

[MeasureFade1]
Measure=Script
ScriptFile=Fade.lua
MeterName=Start1
Min=50
Step=5
IfAboveValue=254
IfAboveAction=!CommandMeasure MeasureFade1 Activate()
IfBelowValue=51
IfBelowAction=[!HideMeter Start1][!ShowMeter Start2][!CommandMeasure MeasureFade2 Activate()]

[MeasureFade2]
Measure=Script
ScriptFile=Fade.lua
MeterName=Start2
Min=50
Step=5
Start=51
IfAboveValue=254
IfAboveAction=!CommandMeasure MeasureFade2 Activate()
IfBelowValue=51
IfBelowAction=[!HideMeter Start2][!ShowMeter Start1][!CommandMeasure MeasureFade1 Activate()]

[Start1]
Meter=Button
ButtonImage=start1.png
x=44
y=48
ImageTint=0,245,255,[MeasureFade1]
Antialias=1
ButtonCommand=!Execute ["#CURRENTPATH#OpenStartMenu.exe"]
DynamicVariables=1

[Start2]
Meter=Button
MeterStyle=Start1
ImageTint=0,245,255,[MeasureFade2]
Hidden=1
(Also, just in case you weren't aware, you will need an Update rate higher than 1000 to achieve a smooth frame rate.)
User avatar
TweaknFreak
Posts: 217
Joined: July 14th, 2012, 7:26 am

Re: Smooth Meter Fade In/Out

Post by TweaknFreak »

That works,great! thanx again, and thanx a lot. yeah,I tuned the update rate to 20 and I get what I expected. but there is only 1 bug - after every two cycles,i.e,2 fade outs + 2 fade ins, the thing pauses for a moment and then starts again. The delay reminds me of whales, coming out of water after certain period of time to breathe. Its quite funny, and it doesnt bother me that much though it will be perfect if you can help me in this case too.

And a personal thought - YOU are rescuing me everytime I get subdued by errors and YOU are doing a fabulous job.Thanx again.
Be one of the iNEViTABLES - Storm, Earthquake, Lightning, Flood - and the world will bow down to you.

My current desktop
[hsimg]http://i1313.photobucket.com/albums/t559/TweaknFreak/6-22-20149-46-40PM_zps7bcbc1f6.png[/hsimg]
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: Smooth Meter Fade In/Out

Post by Kaelri »

TweaknFreak wrote:after every two cycles,i.e,2 fade outs + 2 fade ins, the thing pauses for a moment and then starts again. The delay reminds me of whales, coming out of water after certain period of time to breathe. Its quite funny, and it doesnt bother me that much though it will be perfect if you can help me in this case too.
There was a little typo in my last post. You may have copied the code before I fixed it. On [MeasureFade2], make sure you have:

IfAboveAction=!CommandMeasure MeasureFade2 Activate()

Sorry about that.
TweaknFreak wrote:And a personal thought - YOU are rescuing me everytime I get subdued by errors and YOU are doing a fabulous job.Thanx again.
Glad I can help. :)
User avatar
TweaknFreak
Posts: 217
Joined: July 14th, 2012, 7:26 am

Re: Smooth Meter Fade In/Out

Post by TweaknFreak »

Yeah,that fixes it. :)
Be one of the iNEViTABLES - Storm, Earthquake, Lightning, Flood - and the world will bow down to you.

My current desktop
[hsimg]http://i1313.photobucket.com/albums/t559/TweaknFreak/6-22-20149-46-40PM_zps7bcbc1f6.png[/hsimg]
ElderGoose
Posts: 16
Joined: August 15th, 2012, 11:55 pm

Re: Smooth Meter Fade In/Out

Post by ElderGoose »

While using this script, I keep getting an error that says:

"Script: Fade.lua:25: attempt to perform arithmetic on global 'Min' (a nil value)"

It was working fine for me before, but when I restarted my computer this morning my skin using this script wasn't working properly.
Any idea on what would cause this?