It is currently April 26th, 2024, 7:45 am

Setting meter text with Lua?

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

Re: Setting meter text with Lua?

Post by Kaelri »

jsmorley wrote:As to the second bit, you can update the skin with SKIN:Bang('!Update') just as you have it, but it doesn't make any sense to do that from Lua. EVERYTHING in the Lua script that talks to Rainmeter happens when the script is done and returns to Rainmeter, and thus on the next Update anyway... You might want to consider something like:

LeftMouseUpAction=!Execute [!CommandMeasure "MeasureLua" "nextCard()"][!Update]

Which should execute the Lua and immediately update.
Just a small quibble, but I don't think this is true. If I have a button that runs a script function with SKIN:Bang('!Update') in it, I get an instantaneous response even when the skin has Update=-1. I'm not sure why this wasn't working in the original script, but I guess it's a moot point now. :)
User avatar
jsmorley
Developer
Posts: 22630
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Setting meter text with Lua?

Post by jsmorley »

Kaelri wrote: Just a small quibble, but I don't think this is true. If I have a button that runs a script function with SKIN:Bang('!Update') in it, I get an instantaneous response even when the skin has Update=-1. I'm not sure why this wasn't working in the original script, but I guess it's a moot point now. :)
You may be right. I know that things that talk to Rainmeter in the Update() section of the .lua are ALL executed when the Lua returns control to the skin after all of Update() has run, which is why you can't put a loop in a .lua file like:

for 1 = 1, 255 do
SKIN:Bang('!SetOption MeterOne FontColor 255,255,255,'..i)
end

and get a nice fade effect on a meter. The only Bang that you will see Rainmeter execute is the last one, which will be 255.

However, if you use !CommandMeasure to directly call a Lua function, it will no doubt return as soon as that function is done and in that case an !Update in the Lua may well happen before the normal Update= in [Rainmeter]. Even on a regular Measure=Script that runs a Lua file that doesn't have all that much work to do in Update() may well return before the next Update= and an !Update bang in it may well make things snappier.

On balance, in this case I think I still like the [!Update] after the command measure in the skin, as it at worst accomplishes the same thing, and feels cleaner than remote controlling the update from Lua.... But then I like cornflakes, and you eat rice crispies.

;-)
FatherCode
Posts: 21
Joined: October 8th, 2011, 4:01 pm

Re: Setting meter text with Lua?

Post by FatherCode »

Oh, sweet!
I finally got it to add cards (Yeah, been working at it since the last post =/)!
Trying to synchronize the Lua and Skin was hard ><
(Even with the help you gave me)

But I can't get implement a method to edit flash cards now =/
I've been trying to for a while but it won't work right.

Stuff updates..
But it's the wrong card that gets updated =/
I go to the first card and edit it.
But the second or last card gets updated instead.

So, card.cur has to be wrong; the thing is, I added a Calc measure to track the value of card.cur but it's correct.
I just don't see why it updates wrongly =/

Skin.ini

Code: Select all

[Rainmeter]
	Update			=1000
	Author			=JustinAnyhowStep
	BackgroundMode		=2
	SolidColor		=00000066

[Metadata]
	Name			=FlashCards
	Config			=
	Description		=A simple flash card skin to help you memorize things
	Instructions		=
	Version			=1.0
	Tags			=
	License			=
	Variant			=
	Preview			=

[Variables]
	@include		=#CURRENTPATH#\variables.inc

[ButtonStyle]
	W			=#btn.width#
	H			=#btn.height#
	Y			=(#skin.height#-#btn.height#-#skin.padding#)
	
[TextStyle]
	W			=(#skin.width#-(#skin.padding#*2))
	H			=(#skin.height#-(#skin.padding#*2)-#btn.height#)
	X			=(#skin.width#/2)
	Y			=#skin.padding#
	FontColor		=FFFFFFFF
	StringAlign		=CENTER

[MeasureCalc]
	Measure			=Calc
	Formula			=#card.cur#
	DynamicVariables	=1

[MeasureLua]
	Measure			=Script
	ScriptFile		=#CURRENTPATH#\FlashCards.lua

[MeasureInputText]
	Measure			=Plugin
	MeasureStyle		=InputTextStyle
	Plugin			=Plugins\InputText.dll
	;Command1		=!Execute [!SetOption MeterInvisString Text "$UserInput$"]
	Command1		=!Execute [!WriteKeyValue Variables "fCard#card.amt#" "$UserInput$" "#CURRENTPATH#\variables.inc"]
	Command2		=!Execute [!WriteKeyValue Variables "bCard#card.amt#" "$UserInput$" "#CURRENTPATH#\variables.inc"]
	Command3		=!Execute [!WriteKeyValue Variables "fCard#card.cur#" "$UserInput$" "#CURRENTPATH#\variables.inc"]
	Command4		=!Execute [!WriteKeyValue Variables "bCard#card.cur#" "$UserInput$" "#CURRENTPATH#\variables.inc"]
	X			=#skin.padding#
	Y			=#skin.padding#
	W			=(#skin.width#-(#skin.padding#*2))
	H			=(#skin.height#-(#skin.padding#*2)-#btn.height#)
	SolidColor		=000000FF
	FontColor		=FFFFFFFF
	StringAlign		=CENTER
	DynamicVariables	=1
	
[MeterFlashCard]
	Meter			=String
	MeterStyle		=TextStyle
	MeasureName		=MeasureLua
	Text			=%1

[MeterFlipCard]
	Meter			=String
	MeterStyle		=TextStyle
	Text			=Flip Card
	LeftMouseUpAction	=!Execute [!CommandMeasure "MeasureLua" "flipCard()"]
	W			=(#skin.width#/4)
	H			=#btn.height#
	Y			=(#skin.height#-#btn.height#)
	ToolTipText		=Flip Card

[MeterNextCard]
	Meter			=Image
	MeterStyle		=ButtonStyle
	ImageName		=#btn.nxt#
	X			=(#skin.width#-#skin.padding#-#btn.width#)
	DyanamicVariables	=1
	LeftMouseUpAction	=!Execute [!CommandMeasure "MeasureLua" "nextCard()"]
	ToolTipText		=Next Card

[MeterPrevCard]
	Meter			=Image
	MeterStyle		=ButtonStyle
	ImageName		=#btn.prv#
	X			=#skin.padding#
	DyanamicVariables	=1
	LeftMouseUpAction	=!Execute [!CommandMeasure "MeasureLua" "prevCard()"]
	ToolTipText		=Prev Card

[MeterAddCard]
	Meter			=Image
	MeterStyle		=ButtonStyle
	ImageName		=#btn.add#
	X			=#skin.padding#
	Y			=#skin.padding#
	DyanamicVariables	=1
	LeftMouseUpAction	=!Execute [!CommandMeasure "MeasureLua" "addCard()"]
	ToolTipText		=Add Card

[MeterEditCard]
	Meter			=Image
	MeterStyle		=ButtonStyle
	ImageName		=#btn.add#
	X			=(#skin.width#-#skin.padding#-#btn.width#)
	Y			=#skin.padding#
	DyanamicVariables	=1
	LeftMouseUpAction	=!Execute [!CommandMeasure "MeasureLua" "editCard()"]
	ToolTipText		=Edit Current Card
FlashCard.lua

Code: Select all

DECK = {cur=0, state=0}
STATE = {[0]="f", [1]="b"}
PROPERTIES = {
}
function Initialize ()
	DECK.cur	= SKIN:GetVariable("card.cur")
	getCardValue(DECK.cur, DECK.state)
	updateCard()
end

function Update ()
	return 1
end

function nextCard ()
	if (DECK.cur+1==deckSize()) then
		DECK.cur = 0
	else
		DECK.cur = DECK.cur+1
	end
	updateCard()
end

function prevCard ()
	if (DECK.cur-1<0) then
		DECK.cur = deckSize()-1
	else
		DECK.cur = DECK.cur-1
	end
	updateCard()
end

function flipCard ()
	DECK.state = 1 - DECK.state
	updateCard()
end

function updateCard ()
	SKIN:Bang("!SetVariable card.cur "..DECK.cur)
	SKIN:Bang("!WriteKeyValue Variables card.cur "..DECK.cur.." \"#CURRENTPATH#\\variables.inc\"")
	SKIN:Bang("!SetOption MeterFlashCard Text \""..getCardValue(DECK.state, DECK.cur).."\"")
	SKIN:Bang("!Update")
end

function deckSize ()
	local f		=io.open(SKIN:GetVariable("CURRENTPATH").."\\variables.inc")
	f:read("*l")
	local str	=f:read("*l")
	local first,last=string.find(str, "card.amt\t=")
	str		=string.sub(str, last+1)
	str		=string.sub(str, 1, string.find(str, "\n"))

	return tonumber(str)
end

function addCard ()
	SKIN:Bang("!CommandMeasure \"MeasureInputText\" \"ExecuteBatch 1-2\"")
	SKIN:Bang("!SetVariable card.amt (1+#card.amt#)")
	SKIN:Bang("!WriteKeyValue Variables card.amt (#card.amt#) \"#CURRENTPATH#\\variables.inc\"")
end

function getCardValue (card, state)
	print("DECK.state="..DECK.state..";DECK.cur="..DECK.cur)
	local f		=io.open(SKIN:GetVariable("CURRENTPATH").."\\variables.inc")
	local str	=f:read("*a")
	local first,last=string.find(str, STATE[DECK.state].."Card"..DECK.cur.."=")
	str		=string.sub(str, last+1)
	str		=string.sub(str, 1, string.find(str, "\n"))


	return str
end

function editCard ()
	SKIN:Bang("!CommandMeasure \"MeasureInputText\" \"ExecuteBatch 3-4\"")
	updateCard()
end
variables.inc

Code: Select all

[Variables]
	card.amt	=5
	card.cur	=4

	btn.add		=#CURRENTPATH#\Active.png
	btn.nxt		=#CURRENTPATH#\Active.png
	btn.prv		=#CURRENTPATH#\Active.png
	btn.width	=14
	btn.height	=14
	
	skin.width	=200
	skin.height	=200
	skin.padding	=3

fCard0=FRONT #0
bCard0=BACK #0
fCard1=FRONT #1
bCard1=BACK #1
fCard2=FRONT #2
bCard2=BACK #2
fCard3=FRONT #3
bCard3=BACK #3
fCard4=FRONT #4
bCard4=BACK #4

User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: Setting meter text with Lua?

Post by Kaelri »

InputText is a plugin measure, which means that you can't use dynamic variables (such as your #card.cur#) with it. You could get around that by sending the command through Lua. For example:

Code: Select all

Command3      =!CommandMeasure MeasureLua "Edit3('$UserInput$')"

Code: Select all

function Edit3(sUserInput)
   local DECK.cur   = SKIN:GetVariable("card.cur")
   SKIN:Bang('!WriteKeyValue Variables "fCard'..DECK.cur..'" "'..sUserInput..'" "#CURRENTPATH#\variables.inc"')
end
FatherCode
Posts: 21
Joined: October 8th, 2011, 4:01 pm

Re: Setting meter text with Lua?

Post by FatherCode »

Huh, it says that it can't find "MeasureLua"
!CommandMeasure [MeasureLua] not found
But I'm pretty sure it's there =/

Code: Select all

[MeasureLua]
	Measure			=Script
	ScriptFile		=#CURRENTPATH#\FlashCards.lua

[MeasureInputText]
	Measure			=Plugin
	MeasureStyle		=InputTextStyle
	Plugin			=Plugins\InputText.dll
	Command1		=!Execute [!WriteKeyValue Variables "fCard#card.amt#" "$UserInput$" "#CURRENTPATH#\variables.inc"]
	Command2		=!Execute [!WriteKeyValue Variables "bCard#card.amt#" "$UserInput$" "#CURRENTPATH#\variables.inc"]
	Command3		=!Execute [!CommandMeasure MeasureLua "editCardFront('$InputText$')"][!CommandMeasure MeasureLua "editCardBack('$InputText$')"]
	X			=#skin.padding#
	Y			=#skin.padding#
	W			=(#skin.width#-(#skin.padding#*2))
	H			=(#skin.height#-(#skin.padding#*2)-#btn.height#)
	SolidColor		=000000FF
	FontColor		=FFFFFFFF
	StringAlign		=CENTER
	DynamicVariables	=1

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

Re: Setting meter text with Lua?

Post by smurfier »

Code: Select all

Command3      =!CommandMeasure MeasureLua "Edit3('$UserInput$')" #CURRENTCONFIG#
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 . . .
FatherCode
Posts: 21
Joined: October 8th, 2011, 4:01 pm

Re: Setting meter text with Lua?

Post by FatherCode »

Oh, thanks, I made it:

Code: Select all

	Command3		=!CommandMeasure "MeasureLua" "editCardFront('$InputText$')" #CURRENTCONFIG#
	Command4		=!CommandMeasure "MeasureLua" "editCardBack('$InputText$')" #CURRENTCONFIG#
And for the button:

Code: Select all

	LeftMouseUpAction	=!Execute [!CommandMeasure "MeasureInputText" "ExecuteBatch 3-4"]
However, the InputText box doesn't appear.
The Plugin just calls editCardFront() and editCardBack() and passes the string "$InputText$"
So my flash card becomes updated to "$InputText" =/
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: Setting meter text with Lua?

Post by Kaelri »

You always use $UserInput$ for the InputText plugin.

Code: Select all

	Command3		=!CommandMeasure "MeasureLua" "editCardFront('$UserInput$')" #CURRENTCONFIG#
	Command4		=!CommandMeasure "MeasureLua" "editCardBack('$UserInput$')" #CURRENTCONFIG#
The parameter name in Rainmeter doesn't need to be the same as the parameter name in Lua. As long as the function (e.g.) editCardFront receives a parameter, it will be defined as whatever it's called there. :)
FatherCode
Posts: 21
Joined: October 8th, 2011, 4:01 pm

Re: Setting meter text with Lua?

Post by FatherCode »

*facepalm*
Oh, my gawd.
Thanks, Kaelri =)


All this while..
It was.. That..
I think I need to be in a corner for a bit =/

[EDIT]
The InputText Plugin has a "H" option, that sets its height, right?
And I can't set this with a formula, right?
Like: (#skin.height#-(#skin.padding#*2)-#btn.height#)
It only seems to work if I input literals =/
(Guess I'll hack it with Lua ><)

[EDIT=2]
I tried but failed =/

Code: Select all

	--Define some locals
		local skin_w	= tonumber(SKIN:GetVariable("skin.width"))
		local skin_h	= tonumber(SKIN:GetVariable("skin.height"))
		local skin_p	= tonumber(SKIN:GetVariable("skin.padding"))
		local btn_w	= tonumber(SKIN:GetVariable("btn.width"))
		local btn_h 	= tonumber(SKIN:GetVariable("btn.height"))
	--Set the size of the InputText Box
		SKIN:Bang	("!SetOption MeasureInputText H "..(skin_h-(skin_p*2)-btn_h))
I could just use a literal but then, I would have to recalculate the size every time I want to change my skin's width and height =/