It is currently September 17th, 2021, 1:27 pm

[Solved] Create Meter/Measure in Lua

Discuss the use of Lua in Script measures.
Kittoes
Posts: 20
Joined: June 4th, 2011, 8:39 pm

[Solved] Create Meter/Measure in Lua

Post by Kittoes »

Finally got back to working on Rainmeter this week (Zelda: OOT 3D got in the way for a while) and more or less solved this. I have everything pretty much working how I want it to so naturally I'm posting the results for some feedback.

I wanted to write a skin that would display my unread GMail items (From, Subject, Etc). The problem is that I'm really lazy and if I wanted to change my mind and display more or less items then I would have to go in and write/delete a decent chunk of code. I wanted to be able to make the change simply by editing an integer. So, that's what I did.

In addition, the rest of the skin needs to scale with the amount of items. I thought this was going to be rather trivial but a major problem arises when you change the font size. There's no easy way to figure out what that font translates to in terms of actual pixels. I toyed around with A LOT of ideas and nothing really worked 100% of the time (Varelse wrote an excellent workshop on this). However, if no height is specified then the height will be exactly what is required by the Font Type and Font Size. This isn't immediately helpful since you need H and W specified for ClipString to work and you need to know the Height of each string meter to calculate the Height of your background meter..

To solve this I created a DummyString meter that has no height specified and has an Alpha of 0. The DummyString meter ends up having the exact height that I need for that particular Font Type/Size combo. In Lua I get the height of that meter and save it to a variable and use that variable throughout the code that is written to the .inc file. Anyways, enough of me talking. Here's a screenshot of the results, the only changes made were to #FONT_SIZE# in Variables and NumItems in the InitializeGMail Script Meter:

3 Emails, Font Size= 16
3 Emails.PNG
5 Emails, Font Size= 10
5 Emails.PNG
Here's KittUI_GMail.ini, GMail.lua, and source.txt:
KittUI_GMail.zip
You do not have the required permissions to view the files attached to this post.
Last edited by Kittoes on June 29th, 2011, 11:42 pm, edited 4 times in total.
User avatar
jsmorley
Developer
Posts: 22270
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Create Meter/Measure in Lua

Post by jsmorley »

There is no way to dynamically create a meter or measure in Rainmeter directly from Lua. However, you can use Lua to edit the skin's .ini file and add meters or measures. However, you will HAVE to send a bang to completely refresh the skin after you create them, or Rainmeter will not see them.
Kittoes
Posts: 20
Joined: June 4th, 2011, 8:39 pm

Re: Create Meter/Measure in Lua

Post by Kittoes »

jsmorley wrote:There is no way to dynamically create a meter or measure in Rainmeter directly from Lua. However, you can use Lua to edit the skin's .ini file and add meters or measures. However, you will HAVE to send a bang to completely refresh the skin after you create them, or Rainmeter will not see them.
This I understand. There is no functionality directly in Lua for me to create anything and any changes I do somehow make to the .ini will require a refresh. I'm curious as to how I would write into the .ini as WriteKeyValue just doesn't seem appropriate.

Maybe create a .inc text file via some Lua code? And write the .inc into the .ini with an @include?

I don't really want to permanently alter the .ini as I want to keep it very simple. Rather than defining a whole bunch of meters to create an RSS SKIN I would like to have a script that might be like:

Code: Select all

[ScriptRSS]
Measure=Script
ScriptFile=#SKINSPATH#SomePath\ScriptRSS.lua
TableName=RSSTable
FeedSource=SomeURL
RegEx=RegEx_SomeURL
Items=X

@include=#SKINSPATH#SomePath\ScriptRSS.inc

ScriptRSS.inc would start out blank. In the script I would somehow generate all the meters/measures to make the RSS work, save them to a .inc file, refresh, and then delete the contents of the .inc file.

That's kinda what I'm going for, I'm just a bit lost on how to save them to a .inc.
User avatar
jsmorley
Developer
Posts: 22270
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Create Meter/Measure in Lua

Post by jsmorley »

Kittoes wrote: This I understand. There is no functionality directly in Lua for me to create anything and any changes I do somehow make to the .ini will require a refresh. I'm curious as to how I would write into the .ini as WriteKeyValue just doesn't seem appropriate.

Maybe create a .inc text file via some Lua code? And write the .inc into the .ini with an @include?

I don't really want to permanently alter the .ini as I want to keep it very simple. Rather than defining a whole bunch of meters to create an RSS SKIN I would like to have a script that might be like:

Code: Select all

[ScriptRSS]
Measure=Script
ScriptFile=#SKINSPATH#SomePath\ScriptRSS.lua
TableName=RSSTable
FeedSource=SomeURL
RegEx=RegEx_SomeURL
Items=X

@include=#SKINSPATH#SomePath\ScriptRSS.inc

ScriptRSS.inc would start out blank. In the script I would somehow generate all the meters/measures to make the RSS work, save them to a .inc file, refresh, and then delete the contents of the .inc file.

That's kinda what I'm going for, I'm just a bit lost on how to save them to a .inc.
Well, this isn't the right place to learn Lua itself, there are plenty of sites with manuals and tutorials for that, like HERE, or HERE. Lua is perfectly capable of opening an existing or new file, writing anything you want to it, and closing it. Then just use Lua to send a !RainmeterRefresh bang and there you go. In general, your idea of using a .inc should work fine.

However, you will want to be careful that you don't get into an endless loop of "get values, call Lua, write to file, refresh skin, get values, call Lua, write to file, refresh skin, get...." If you see what I mean. Might take some clever thinking to avoid that in these circumstances. I'd probably be tempted to have the Lua check the current values in the measures against a copy of them saved in an array or variables in Lua's memory (might even have to be to a temporary file if refreshing the skin also completely restarts the Lua, I'm not sure) and only if they are now different than they were would I take any action.
Kittoes
Posts: 20
Joined: June 4th, 2011, 8:39 pm

Re: Create Meter/Measure in Lua

Post by Kittoes »

jsmorley wrote:Well, this isn't the right place to learn Lua itself, there are plenty of sites with manuals and tutorials for that, like HERE, or HERE. Lua is perfectly capable of opening an existing or new file, writing anything you want to it, and closing it. Then just use Lua to send a !RainmeterRefresh bang and there you go. In general, your idea of using a .inc should work fine.

However, you will want to be careful that you don't get into an endless loop of "get values, call Lua, write to file, refresh skin, get values, call Lua, write to file, refresh skin, get...." If you see what I mean. Might take some clever thinking to avoid that in these circumstances. I'd probably be tempted to have the Lua check the current values in the measures against a copy of them saved in an array or variables in Lua's memory (might even have to be to a temporary file if refreshing the skin also completely restarts the Lua, I'm not sure) and only if they are now different than they were would I take any action.
Awesome. Yeah, not trying to have anyone here teach me Lua just was curious if it was possible. I'll be spending a lot of my time this weekend with The Google in order to do that. I don't want this particular script to loop at all since it's sole purpose is to generate meters/measures for the skin. So, I'll probably just have everything in the Initialize() section with a Bang to disable the script upon completion. Though... that might be tricky since a refresh will restart the script...

I'll figure out the details. Thanks for the response!
User avatar
jsmorley
Developer
Posts: 22270
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Create Meter/Measure in Lua

Post by jsmorley »

Glad to help.
Kittoes
Posts: 20
Joined: June 4th, 2011, 8:39 pm

Re: Create Meter/Measure in Lua

Post by Kittoes »

Updated for teh feedbacks.