It is currently October 15th, 2019, 10:46 pm

LoadSkin 3.0.0

Post your work-in-progress and completed skins to share and discuss.
User avatar
raiguard
Posts: 642
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

LoadSkin 3.0.0

raiguard » October 28th, 2018, 5:59 am

loadskin.gif
LoadSkin is a LUA script that I created to ease the creation of skin load/unload buttons. All you need to supply is the config name, and optionally the name of the INI you wish to toggle, and the script will handle detecting which skin is active, and all of the logic necessary to toggle it. You can also specifically load/unload a skin, and in this case, the script prevents warning or error messages from filling the log when activating an already activated skin, and vice versa.

The skin is very simple to use, and it quite easy to figure out just by looking at the example skin, but just in case one wants a more in-depth explanation, I have thoroughly documented everything in the script's code.

There is only one downside to this script: Because of a flaw in how !CommandMeasure works, you must use double backslashes (\\) in all of your config names. There is no timeline for when this issue will be fixed, if ever, so this is a semi-permanent workaround. Sorry!

(And before anyone comments about this, I know you can use inline LUA in place of a !CommandMeasure bang to avoid this problem, but I have had Rainmeter randomly hard-crash for no apparent reason when doing that, so I prefer to use !CommandMeasure bangs...)

The included example skin demonstrates some of the capabilities of the script in various usecases. It is meant to be as easy as possible to learn from and copy, so things are a bit oversimplified.

Here is the script code:

Code: Select all

--[[
--------------------------------------------------

LOADSKIN.LUA
raiguard
v3.0.0

--------------------------------------------------

Release Notes:
v3.0.0 - 2018-10-28
- Redesigned script to simplify the required inputs
- Improved documentation
v2.0.0 - 2018-6-22
- Updated to use the new ConfigActive plugin rather than WebParser
v1.3.0 - 2018-6-21
- The script now gets the input from a WebParser measure, rather than directly parsing
  Rainmeter.ini (for Rainmeter 4.2+ compatibility)
v1.2.0 - 2017-12-27
 - Added ability to specifically load or unload skins, rather than always toggling them
v1.1.0 - 2017-12-7
 - Consolidated LoadConfig() into LoadSkin()
v1.0.0 - 2017-10-2
 - Initial release

--------------------------------------------------

This script loads / unlaods the specified skin or config, and sets parameters for toggle
buttons related to those skins.


INSTRUCTIONS FOR USE:
Copy this file and paste it into your own suite. First, you will need to create a
ConfigActive plugin measure:

	[MeasureConfigActive]
	Measure=Plugin
	Plugin=ConfigActive

That's all you need to add to this measure, the script will handle the rest. Speaking of
the script, that is the next measure you need to create:

	[MeasureLoadSkinScript]
	Measure=Script
	ScriptFile=#@#Scripts\LoadSkin.lua
	ToggleOn=#@#Images\toggle-on.png
	ToggleOff=#@#Images\toggle-off.png
	ToggleGroup=ToggleButtons
	MeasureName=MeasureConfigActive

The 'ToggleOn' and 'ToggleOff' parameters are for the toggle buttons. If you are using
images, these will be the image paths for the buttons' respective on and off states. If
you are using strings, these will be the 'on' and 'off' strings that will show on the
buttons. If you do not include these parameters, the script will default to 'Enabled'
and 'Disabled' respectively.

There are also optional 'RadioOn' and 'RadioOff' options for the script measure. This
allows you to specify a different set of images or strings for 'radio buttons', usually
to have buttons that will load different variants of a config. These options are
optional, and if left unspecified, will be defined as what you set in 'ToggleOn' and
'ToggleOff' respectively.

The 'ToggleGroup' parameter specifies the group that the toggle button meters belong to.
If you do not include this option, it will default to 'SkinToggles'.

Last but not least, the 'MeasureName' option is simply the name of the ConfigActive
measure that you created before. If unspecified, it will default to 'MeasureConfigActive'.

A toggle button meter should look something like this:

	[MeterToggleSystem]
	Meter=Image
	ImageName=[&MeasureLoadSkinScript:GetIcon('illustro\\System')]
	X=5
	Y=5
	W=31
	H=20
	LeftMouseUpAction=[!CommandMeasure MeasureLoadSkinScript "ToggleSkin('illustro\\System')"]
	DynamicVariables=1
	Group=SkinToggles

The toggle buttons get their parameters via inline LUA, which requires that
'DynamicVariables=1' must be set on all the buttons. The buttons must also belong to the
group specified in the script measure.

Obviously, if you are using strings as your buttons, the inline LUA will be contained in
the 'Text' option, rather than 'ImageName'.

--------------------------------------------------
]]--

function Initialize()

	toggleOn = SELF:GetOption('ToggleOn', 'Enabled')
	toggleOff = SELF:GetOption('ToggleOff', 'Disabled')
	radioOn = SELF:GetOption('RadioOn', toggleOn)
	radioOff = SELF:GetOption('RadioOff', toggleOff)
	toggleGroup = SELF:GetOption('ToggleGroup', 'SkinToggles')
	caMeasureName = SELF:GetOption('MeasureName', 'MeasureConfigActive')
	measureCA = SKIN:GetMeasure(caMeasureName)

end

function Update() end

-- toggles or sets the specified skin or config
function ToggleSkin(configName, iniName, toState)

	--[[
		PLEASE NOTE THAT THE DOUBLE BACKSLASHES ARE ALWAYS REQUIRED BECAUSE OF LUA SHENANIGANS
		configName: the name of the config you wish to toggle (e.g. 'illustro\\Disk')
		iniName (optional): the name of the skin INI you wish to toggle (e.g. '1 Disk.ini')
		toState (optional): the state you wish to set the skin to (1 for active, -1 for inactive)
	]]--

	local configState, activeIni = GetConfigState(configName)
	local toState = toState or iniName and (iniName == activeIni and -1 or 1) or configState * -1

	if toState == 1 then
		if iniName == nil then
			SKIN:Bang('!ActivateConfig', configName)
		elseif iniName ~= activeIni then
			SKIN:Bang('!ActivateConfig', configName, iniName)
		end
	elseif configState == 1 then
		SKIN:Bang('!DeactivateConfig', configName)
	end

	SKIN:Bang('!UpdateMeterGroup', toggleGroup)
	SKIN:Bang('!Redraw')

end

-- returns the corresponding button state
function GetIcon(configName, iniName, radio)

	--[[
		PLEASE NOTE THAT THE DOUBLE BACKSLASHES ARE ALWAYS REQUIRED BECAUSE OF LUA SHENANIGANS
		configName: the name of the relevant config (e.g. 'illustro\\Disk')
		iniName (optional): the name of the relevant INI file (e.g. '1 Disk.ini')
		radio: if set to true, the function will return the 'radioOn' and 'radioOff' options,
			   instead of 'toggleOn' and 'toggleOff'
	]]--
	
	local configState, activeIni = GetConfigState(configName)
	-- determine which icon to provide
	if iniName then
		if configState == 1 and activeIni == iniName then return radio and radioOn or toggleOn
		else return radio and radioOff or toggleOff end
	else
		if configState == 1 then return radio and radioOn or toggleOn
		else return radio and radioOff or toggleOff end
	end

end

-- retrieves config state and active INI
function GetConfigState(configName)

	SKIN:Bang('!SetOption', caMeasureName, 'ConfigName', configName)
	SKIN:Bang('!UpdateMeasure', caMeasureName)
	return measureCA:GetValue(),	   -- active state of the config (1 or -1)
		   measureCA:GetStringValue()  -- name of the currently active INI (if inactive, returns -1)

end
And, of course, the example skin INI:

Code: Select all

[Rainmeter]
MiddleMouseUpAction=[!Refresh]
AccurateText=1

[Metadata]
Name=LoadSkin
Author=raiguard
Information=Demonstrates the capabilities of the LoadSkin.lua script.
Version=3.0.0
License=Creative Commons Attribution - Non - Commercial - Share Alike 3.0

[Variables]

[StyleStringButton]
FontFace=Roboto
FontColor=240,240,240
FontSize=10
AntiAlias=1
X=6
Y=4R
MouseOverAction=[!SetOption #CURRENTSECTION# FontColor "200,50,50"][!UpdateMeter #CURRENTSECTION#][!Redraw]
MouseLeaveAction=[!SetOption #CURRENTSECTION# FontColor ""][!UpdateMeter #CURRENTSECTION#][!Redraw]

[StyleStringLabel]
FontFace=Roboto
FontColor=240,240,240
FontSize=10
AntiAlias=1
X=3R
Y=-1r

[StyleToggleButton]
FontFace=ElegantIcons
FontColor=137,190,250
FontSize=12
AntiAlias=1
X=6
Y=4R
Group=SkinToggles
DynamicVariables=1
MouseOverAction=[!SetOption #CURRENTSECTION# FontColor "200,50,50"][!UpdateMeter #CURRENTSECTION#][!Redraw]
MouseLeaveAction=[!SetOption #CURRENTSECTION# FontColor ""][!UpdateMeter #CURRENTSECTION#][!Redraw]

; ==================================================
; MEASURES
; ==================================================

[MeasureConfigActive]
Measure=Plugin
Plugin=ConfigActive

[MeasureScript]
Measure=Script
ScriptFile=#@#Scripts\LoadSkin.lua
ToggleOn=[\x5a]
ToggleOff=[\x56]
RadioOn=[\x5c]
RadioOff=[\x5b]
ToggleGroup=SkinToggles
MeasureName=MeasureConfigActive

; ==================================================
; METERS
; ==================================================

[MeterBackground]
Meter=Shape
Shape=Rectangle 1,1,173,120,3 | Fill Color 15,15,15 | Stroke Color 50,50,50 | StrokeWidth 2

[MeterToggleSystemButton]
Meter=String
MeterStyle=StyleToggleButton
Y=6
Text=[&MeasureScript:GetIcon('illustro\\System')]
LeftMouseUpAction=[!CommandMeasure MeasureScript "ToggleSkin('illustro\\System')"]

[MeterToggleSystemButtonLabel]
Meter=String
MeterStyle=StyleStringLabel
Text=Toggle 'illustro\System'

[MeterToggleDiskButton]
Meter=String
MeterStyle=StyleToggleButton
Text=[&MeasureScript:GetIcon('illustro\\Disk')]
LeftMouseUpAction=[!CommandMeasure MeasureScript "ToggleSkin('illustro\\Disk')"]

[MeterToggleDiskButtonLabel]
Meter=String
MeterStyle=StyleStringLabel
Text=Toggle 'illustro\Disk'

[MeterLoad1DiskButton]
Meter=String
MeterStyle=StyleToggleButton
X=23
Text=[&MeasureScript:GetIcon('illustro\\Disk', '1 Disk.ini', true)]
LeftMouseUpAction=[!CommandMeasure MeasureScript "ToggleSkin('illustro\\Disk', '1 Disk.ini', 1)"]

[MeterLoad1DiskButtonLabel]
Meter=String
MeterStyle=StyleStringLabel
Text=Load '1 Disk.ini'

[MeterLoad2DiskButton]
Meter=String
MeterStyle=StyleToggleButton
X=23
Text=[&MeasureScript:GetIcon('illustro\\Disk', '2 Disks.ini', true)]
LeftMouseUpAction=[!CommandMeasure MeasureScript "ToggleSkin('illustro\\Disk', '2 Disks.ini', 1)"]

[MeterLoad2DiskButtonLabel]
Meter=String
MeterStyle=StyleStringLabel
Text=Load '2 Disks.ini'

[MeterLoadClockButton]
Meter=String
MeterStyle=StyleStringButton
Text=Load 'illustro\Clock'
LeftMouseUpAction=[!CommandMeasure MeasureScript "ToggleSkin('illustro\\Clock', nil, 1)"]

[MeterUnloadClockButton]
Meter=String
MeterStyle=StyleStringButton
Text=Unload 'illustro\Clock'
LeftMouseUpAction=[!CommandMeasure MeasureScript "ToggleSkin('illustro\\Clock', nil, -1)"]
You do not have the required permissions to view the files attached to this post.
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017