It is currently April 19th, 2024, 7:39 am

Need help with Lua (if, elseif)

Discuss the use of Lua in Script measures.
Alex Becherer

Need help with Lua (if, elseif)

Post by Alex Becherer »

this is what i am currently doing:
Unbenannt-1.png
i display several pictures. i use a second image for each to display a border behind them. as the images have different aspect ratios i let them have a fixed height, get the width with a lua script and adjust the with of the image with the border.

Code: Select all

[Rainmeter]
Author=Alex Becherer
AppVersion=2003000
Update=1000
BackgroundMode=1

[Variables]
@Include="#ROOTCONFIGPATH#SETTINGS\variables.inc"

[MeasureGallery1]
Measure=Plugin
Plugin=Plugins\QuotePlugin.dll
PathName="#GalleryPath#"
FileFilter=*.jpg;*.jpeg;*.gif;*.bmp
Subfolders=1
UpdateDivider=300

[MeasureGallery2]
Measure=Plugin
Plugin=Plugins\QuotePlugin.dll
PathName="#GalleryPath#"
FileFilter=*.jpg;*.jpeg;*.gif;*.bmp
Subfolders=1
UpdateDivider=300

[MeasureGallery3]
Measure=Plugin
Plugin=Plugins\QuotePlugin.dll
PathName="#GalleryPath#"
FileFilter=*.jpg;*.jpeg;*.gif;*.bmp
Subfolders=1
UpdateDivider=300

[MeasureGallery4]
Measure=Plugin
Plugin=Plugins\QuotePlugin.dll
PathName="#GalleryPath#"
FileFilter=*.jpg;*.jpeg;*.gif;*.bmp
Subfolders=1
UpdateDivider=300

[MeasureLuaScript]
Measure=Script
ScriptFile=gallery.lua



[MeterBack]
Meter=Image
ImageName=back.png
X=0
Y=0
Hidden=0

[MeterImageBack3]
Meter=Image
ImageName=photo.png
X=83
Y=38
H=96
W=96
Antialias=1
ScaleMargins=10,10,15,15

[MeterImage3]
Meter=Image
MeasureName=MeasureGallery3
X=90
Y=45
Antialias=1
PreserveAspectRatio=1
H=80

[MeterImageBack1]
Meter=Image
ImageName=photo.png
X=23
Y=18
H=96
W=96
Antialias=1
ScaleMargins=10,10,15,15

[MeterImage1]
Meter=Image
MeasureName=MeasureGallery1
X=30
Y=25
Antialias=1
PreserveAspectRatio=1
H=80


[MeterImageBack4]
Meter=Image
ImageName=photo.png
X=46
Y=148
H=96
W=96
Antialias=1
ScaleMargins=10,10,15,15

[MeterImage4]
Meter=Image
MeasureName=MeasureGallery4
X=53
Y=155
Antialias=1
PreserveAspectRatio=1
H=80

[MeterImageBack2]
Meter=Image
ImageName=photo.png
X=93
Y=93
H=96
W=96
Antialias=1
ScaleMargins=10,10,15,15

[MeterImage2]
Meter=Image
MeasureName=MeasureGallery2
X=100
Y=100
Antialias=1
PreserveAspectRatio=1
H=80


Code: Select all

function Update()

	Meter = SKIN:GetMeter('MeterImage1')
	Width = Meter:GetW()
        NewWidth = Width +14
	SKIN:Bang('!SetOption', 'MeterImageBack1', 'W', NewWidth)
	
	
	Meter = SKIN:GetMeter('MeterImage2')
	Width = Meter:GetW()
        NewWidth = Width +14
	SKIN:Bang('!SetOption', 'MeterImageBack2', 'W', NewWidth)
	
	Meter = SKIN:GetMeter('MeterImage3')
	Width = Meter:GetW()
        NewWidth = Width +14
	SKIN:Bang('!SetOption', 'MeterImageBack3', 'W', NewWidth)
	
		
	Meter = SKIN:GetMeter('MeterImage4')
	Width = Meter:GetW()
        NewWidth = Width +14
	SKIN:Bang('!SetOption', 'MeterImageBack4', 'W', NewWidth)
    

end



this is all nice with images taken in landscape mode. but images in portrait mode are of course far to small.

i wonder would it be possible to execute different actions depending on the width of a meter. lets say if the width is higher than 80 just proceed as it does now, but if it is smaller then do something different?
User avatar
KreAch3R
Posts: 608
Joined: February 7th, 2011, 7:27 pm
Location: Thessaloniki, GR

Re: Need help with Lua (if, elseif)

Post by KreAch3R »

I haven't tested it, but try this:

Code: Select all

function Update()

	for i = 1, 4 do
   Meter = SKIN:GetMeter('MeterImage'..i)
   Width = Meter:GetW()
	   if Width > 80 then
			NewWidth = Width +14
			SKIN:Bang('!SetOption', 'MeterImageBack'..i, 'W', NewWidth)
		elseif 
			DO THINGS HERE
		end
	end


end
I simplified your code a bit, you didn't need to have that part copied 4 times, you can use a for loop.
Inactive, due to life changes. Send me a PM for any question.

Desktop DeviartArt
Image
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Need help with Lua (if, elseif)

Post by smurfier »

Let's add some "local" variables for very minor performance reasons, and test for true Landscape and Portrait modes.

Code: Select all

function Update()

	for i = 1, 4 do
		local Meter = SKIN:GetMeter('MeterImage'..i)
		local Width = Meter:GetW()
		local Height = Meter:GetH()
		if Width > Height then -- Landscape Mode
			local NewWidth = Width +14
			SKIN:Bang('!SetOption', 'MeterImageBack'..i, 'W', NewWidth)
		elseif Height > Width then -- Portrait Mode
			DO THINGS HERE
		else -- It's a Square
			Do Something Else
		end
	end


end
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
Alex Becherer

Re: Need help with Lua (if, elseif)

Post by Alex Becherer »

thanks a lot!
that works.
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Need help with Lua (if, elseif)

Post by jsmorley »

This should all be worked up into a "Tips & Tricks" post.