It is currently March 28th, 2024, 8:06 pm

Toggle two colors on a meter with a click.

Tips and Tricks from the Rainmeter Community
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Toggle two colors on a meter with a click.

Post by jsmorley »

It can be tricky to toggle two colors (say "on" and "off") on a meter with a single left click, as you have to use numeric values as a variable, and a formula to toggle between say "0" and "1". Then it's tricky how you use that numeric value to set a FontColor (for instance).

Just thought I would show one way to do it, that I find pleasing.

SKIN:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[MeasureToggleColor]
Measure=Script
ScriptFile=ToggleColor.lua
UpdateDivider=-1

[MeterToToggle]
Meter=String
FontSize=11
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Hello World
LeftMouseUpAction=[!CommandMeasure MeasureToggleColor "ToggleColor('#CURRENTSECTION#', '255,255,255,255','255,0,0,255')"]
LUA:

Code: Select all

function ToggleColor(meterName,color1,color2)

	myMeter = SKIN:GetMeter(meterName)
	currentColor = myMeter:GetOption('FontColor')
	
	if currentColor == color1 then
		newColor = color2
	else
		newColor = color1
	end
	
	SKIN:Bang('!SetOption', meterName, 'FontColor', newColor)
	SKIN:Bang('!UpdateMeter', '*')
	SKIN:Bang('!Redraw')
	
end
So you just pass the name of the meter (#CURRENTSECTION#) and the two colors you want to toggle between to the Lua ToggleColor() function.

It gets the current value of FontColor from the meter, and tests if it is color1 or color2. It then switches them, and sends a !SetOption bang back to the meter to change it.

Quite simple, and can be used in as many meters in the skin as you need, since it is not specific to any particular meter, or sets of colors. It's all defined in the !CommandMeasure call to the script function.

Nothing says this has to be just FontColor, it would work just as well for any meter option you want to toggle. FontSize, StringStyle, pretty much anything.
ToggleColor_1.0.rmskin
You do not have the required permissions to view the files attached to this post.
User avatar
ikarus1969
Posts: 571
Joined: February 28th, 2011, 3:20 pm
Location: Vienna, Austria

Re: Toggle two colors on a meter with a click.

Post by ikarus1969 »

just a remark: in the LUA-code i guess instead of

Code: Select all

SKIN:Bang('!UpdateMeter', '*')
you can write

Code: Select all

SKIN:Bang('!UpdateMeter', meterName)
so only the affected meter is updated ;-)
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Toggle two colors on a meter with a click.

Post by jsmorley »

ikarus1969 wrote:just a remark: in the LUA-code i guess instead of

Code: Select all

SKIN:Bang('!UpdateMeter', '*')
you can write

Code: Select all

SKIN:Bang('!UpdateMeter', meterName)
so only the affected meter is updated ;-)
Certainly, and the decision to use [!UpdateMeter "MeterName"] or [!UpdateMeterGroup "GroupName"] or [!UpdateMeter "*"] is entirely dependent on the skin. I generally lean toward updating all meters when I take some action that impacts a single meter, since I don't want to have to think about issues with relative positioning and such, but if you have a lot of meters in a skin, it can be marginally more efficient to update just one meter or group of meters.