It is currently April 19th, 2024, 11:28 am

Example of toggling a variable "state"

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

Example of toggling a variable "state"

Post by jsmorley »

This is a little example that demonstrates how you might use a "check box" to allow a user to set some state for your skin. Turning on or off some functionality etc. This change not only happens dynamically, but is "saved" with the skin so the state is remembered when the skin is refreshed or reloaded.
ToggleCheckBox_1.0.rmskin
test.gif

Code: Select all

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

[Metadata]
Name=ToggleCheckBox
Author=JSMorley
Information=Demonstration of toggling a variable between 0 and 1, and using Unicode characters in a skin.||Note that the skin .ini file must be encoded as UTF-16 Little Endian / Unicode.
License=Creative Commons Attribution-Non-Commercial-Share Alike 3.0
Version=Jan 30, 2015

[Variables]
State=0
UnChecked=☐
Checked=☑

[MeasureState]
Measure=Calc
Formula=#State#
DynamicVariables=1
IfEqualValue=0
IfEqualAction=[!SetOption MeterCheckBox Text "#UnChecked#"][!SetOption MeterState Text "Box Clear"][!SetOption MeterState FontColor "200,200,200,255"][!UpdateMeter *][!Redraw]
IfAboveValue=0
IfAboveAction=[!SetOption MeterCheckBox Text "#Checked#"][!SetOption MeterState Text "Box Checked"][!SetOption MeterState FontColor "215,252,217,255"][!UpdateMeter *][!Redraw]

[MeterToggle]
Meter=String
FontSize=50
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Toggle

[MeterCheckBox]
Meter=String
X=R
FontSize=43
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=[MeasureState]
DynamicVariables=1
LeftMouseUpAction=[Play "#@#Sounds\Click.wav"][!WriteKeyValue Variables State "(1-#State#)"][!SetVariable State "(1-#State#)"][!UpdateMeasure MeasureState]

[MeterState]
Meter=String
X=([MeterCheckBox:X]+[MeterCheckBox:W])
Y=R
FontSize=20
FontColor=255,255,255,255
SolidColor=0,0,0,1
Padding=5,5,5,5
StringAlign=Right
AntiAlias=1
DynamicVariables=1
So the real key to the functionality is the LeftMouseUpAction where we use:

[!SetVariable State "(1-#State#)"]

What that does is subtract the current value of the variable #State# from 1. If #State# is "0", then "1-0" is "1". So the variable value will change to "1". If #State# is "1", then "1-1" is "0", so the variable value will change to "0". It simply toggles between "0" and "1", or in a sense, "off" and "on".

We can then check the value of the #State# variable in a Calc measure, and using IfAction or IfConditon options, take any actions we want based on the current "state" of the selection.

We also use !WriteKeyValue at the same time, so the state is "saved" with the skin and will be remembered.

Note that to use those Unicode checkbox "glyphs", we need to encode the skin .ini file as Unicode, which is:

Unicode (Windows Notepad.exe)
UCS-2 Little Endian (Notepad++)
UTF-16 LE (Sublime Text)

Encode the skin and save it before you paste in any Unicode characters.
You do not have the required permissions to view the files attached to this post.
User avatar
JamesSF69
Posts: 48
Joined: January 19th, 2015, 4:48 am

Re: Example of toggling a variable "state"

Post by JamesSF69 »

Damn you! LOL Now I something else to maybe use in my meanderings LOL
Thanks for the tip!!

James