It is currently September 29th, 2024, 3:31 pm

Step counter on button click

General topics related to Rainmeter.
User avatar
rbriddickk84
Rainmeter Sage
Posts: 276
Joined: February 17th, 2014, 12:39 pm
Location: Hungary

Step counter on button click

Post by rbriddickk84 »

Hi there!

The more i try to think about the method, the more my brain stuck.
I want to make a button bar. Minimum 1 button, max 20 button. (btn.ini)
I have a global preferences ini (pref.ini), where i want to set the number of the buttons on the buttons bar. (i also have a settings.txt file, where i store variables for refresh)

I made two button, which are pressed would add + 1 to a variable, and then store that number in the settings txt file. The step counting until 20 is reached, then the button would be inactive. The down button obviously for down or back-count until 1.

I thought about if press up button one time, it counts one step and stop,then write variable in settings then refresh the button bar. The button numbers calculated and related on the variable in settings. (dat2.txt)

Button bar code:

Code: Select all


[Variables]
@Include=..\@Resources\data\dat2.txt
vy=0

[HScrCalc]
Measure=Calc
Formula=(#SCREENAREAHEIGHT#)/2

[BarCalc]
Measure=Calc
Formula=#btnum#
ifCondition=BarCalc = 1
ifTrueAction=[!SetVariable vy 22][!HideMeterGroup bxb][!HideMeterGroup bxc][!HideMeterGroup bxbd][!HideMeterGroup bxe]
ifCondition=BarCalc = 2
ifTrueAction=[!SetVariable vy 72][!ShowMeterGroup bxb][!HideMeterGroup bxc][!HideMeterGroup bxbd][!HideMeterGroup bxe]
ifCondition=BarCalc = 3
ifTrueAction=[!SetVariable vy 122][!ShowMeterGroup bxb][!ShowMeterGroup bxc][!HideMeterGroup bxbd][!HideMeterGroup bxe]
ifCondition=BarCalc = 4
ifTrueAction=[!SetVariable vy 172][!ShowMeterGroup bxb][!ShowMeterGroup bxc][!ShowMeterGroup bxbd][!HideMeterGroup bxe]
ifCondition=BarCalc = 5
ifTrueAction=[!SetVariable vy 222][!ShowMeterGroup bxb][!ShowMeterGroup bxc][!ShowMeterGroup bxbd][!ShowMeterGroup bxe]

[BarAlign]
Measure=Calc
Formula=(HScrCalc - #vy#)
DynamicVariables=1

[MainBtnA]
Meter=Button
ButtonImage=..\@Resources\images\btns\btn.png
X=0
Y=[BarAlign]
LeftMouseUpAction=!execute ["#tpha#"]
DynamicVariables=1

[MainBtnB]
Meter=Button
ButtonImage=..\@Resources\images\btns\btn.png
X=0
Y=50r
LeftMouseUpAction=!execute ["#tphb#"]
DynamicVariables=1
Group=bxb
Hidden=1

[MainBtnC]
Meter=Button
ButtonImage=..\@Resources\images\btns\btn.png
X=0
Y=50r
LeftMouseUpAction=!execute ["#tphc#"]
DynamicVariables=1
Group=bxc
Hidden=1

[MainBtnD]
Meter=Button
ButtonImage=..\@Resources\images\btns\btn.png
X=0
Y=50r
LeftMouseUpAction=!execute ["#tphd#"]
DynamicVariables=1
Group=bxd
Hidden=1

[MainBtnE]
Meter=Button
ButtonImage=..\@Resources\images\btns\btn.png
X=0
Y=50r
LeftMouseUpAction=!execute ["#tphe#"]
DynamicVariables=1
Group=bxe
Hidden=1

And here is the preferences ini code:

Code: Select all


[Variables]
@Include=..\@Resources\data\dat2.txt

[MainImg]
Meter=Image
ImageName=..\@Resources\images\main\m.png
X=0
Y=0

[AddBtn]
Meter=Image
SolidColor=0,0,0,5
W=24
H=18
X=100
Y=400
LeftMouseDownAction=
;LeftMouseUpAction=[!WriteKeyValue Variables btnum 1 #SKINSPATH#CleanLight\@Resources\data\dat2.txt]

In teh
You do not have the required permissions to view the files attached to this post.
User avatar
jsmorley
Developer
Posts: 22790
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Step counter on button click

Post by jsmorley »

I'm not entirely sure I follow what you are trying to do, or more particularly how you are trying to do it, but here is one approach I might use given an overall concept of:

Have a skin with buttons. The number of buttons that are displayed is driven by a variable in an @Include file. If that variable is "1" then one button is shown. If it is "3" then three buttons are shown.

Have a settings skin that displays that count of buttons (that variable in the include file) and allows you to use arrows to "step" through the numbers from 1 to XX (5 in my example). The arrows should have enough intelligence that the down arrow can't be used if the number is "1" and the up arrow can't be used if the number is 5.


So I have two skins and one include file:
1.jpg
ButtonBar\@Resources\ButtonCount.inc

Code: Select all

[Variables]
ButtonCount=1
ButtonBar.ini

Code: Select all

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

[Variables]
@Include=#@#ButtonCount.inc

[MeasureShowButtons]
Measure=Calc
Formula=#ButtonCount#
OnUpdateAction=[!HideMeterGroup 5][!ShowMeterGroup #ButtonCount#][!UpdateMeter *][!Redraw]
UpdateDivider=-1

[MeterButton1]
Meter=Image
Group=1 | 2 | 3 | 4 | 5
W=30
H=30
SolidColor=157,245,167,255
LeftMouseUpAction=[!Log "Clicked Button1"]

[MeterButton2]
Meter=Image
Group=2 | 3 | 4 | 5
X=3R
W=30
H=30
SolidColor=157,245,167,255
LeftMouseUpAction=[!Log "Clicked Button2"]

[MeterButton3]
Meter=Image
Group=3 | 4 | 5
X=3R
W=30
H=30
SolidColor=157,245,167,255
LeftMouseUpAction=[!Log "Clicked Button3"]

[MeterButton4]
Meter=Image
Group=4 | 5
X=3R
W=30
H=30
SolidColor=157,245,167,255
LeftMouseUpAction=[!Log "Clicked Button4"]

[MeterButton5]
Meter=Image
Group=5
X=3R
W=30
H=30
SolidColor=157,245,167,255
LeftMouseUpAction=[!Log "Clicked Button5"]
The key to this skin is that when the skin is loaded or refreshed, it gets the value of that variable "ButtonCount" from the include file. It first hides all button meters, then shows the appropriate ones based on the value of that variable. If #ButtonCount# is 2, then the "Groups" 1 and 2 are shown. If #ButtonCount# is 4 then "Groups" 1, 2, 3 and 4 are shown.

Then we have our skin to set the count of buttons to show. We put that in a sub-folder of ButtonBar, so both skins can be loaded at once.

ButtonBar\ButtonSet\ButtonSet.ini

Code: Select all

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

[Variables]
@Include=#@#ButtonCount.inc

[MeasureManage]
Measure=Calc
Formula=#ButtonCount# < 2 ? 1 : (#ButtonCount# > 4 ? 5 : #ButtonCount#)
IfCondition=MeasureManage < 2
IfTrueAction=[!SetOption MeterDown ImageTint "170,170,170,255"][!SetOption MeterUp ImageTint ""][!UpdateMeter *][!Redraw]
IfCondition2=MeasureManage > 4
IfTrueAction2=[!SetOption MeterUp ImageTint "170,170,170,255"][!SetOption MeterDown ImageTint ""][!UpdateMeter *][!Redraw]
IfCondition3=(MeasureManage > 1) && (MeasureManage < 5)
IfTrueAction3=[!SetOption MeterUp ImageTint ""][!SetOption MeterDown ImageTint ""][!UpdateMeter *][!Redraw]
IfConditionMode=1
DynamicVariables=1
UpdateDivider=-1
OnUpdateAction=[!SetVariable ButtonCount [MeasureManage]][!WriteKeyValue Variables ButtonCount [MeasureManage] "#@#ButtonCount.inc"][!Refresh "ButtonBar"]

[MeterUp]
Meter=Image
Group=Arrows
ImageName=#@#Images\ButtonArrow.jpg
X=1
AntiAlias=1
LeftMouseUpAction=[!SetVariable ButtonCount (#ButtonCount#+1)][!UpdateMeasure MeasureManage][!UpdateMeter *][!Redraw]
DynamicVariables=1

[MeterCountBox]
Meter=Image
ImageName=#@#Images\ButtonCount.jpg
X=0
Y=2R
AntiAlias=1

[MeterCountNumber]
Meter=String
MeasureName=MeasureManage
X=16
Y=21
FontSize=14
FontColor=0,0,0,255
StringAlign=Center
StringStyle=Bold
AntiAlias=1

[MeterDown]
Meter=Image
Group=Arrows
ImageName=#@#Images\ButtonArrow.jpg
ImageFlip=Vertical
X=1
Y=9R
AntiAlias=1
LeftMouseUpAction=[!SetVariable ButtonCount (#ButtonCount#-1)][!UpdateMeasure MeasureManage][!UpdateMeter *][!Redraw]
DynamicVariables=1
This is a little more complex, but simple enough in principle.

What we do is again get the value of the variable #ButtonCount# from that ButtonCount.inc include file.

Then, skipping that [MeasureMange] measure for a second (we will come back to it) we just have the meters that will display the "up" arrow, the "down" arrow, and a box with the "count" between them.

The arrow meters have a LeftMouseUpAction that either adds 1 to #ButtonCount# (up arrow) or subtracts 1 from #ButtonCount# (down arrow). They then use !UpdateMeasure to update our waiting [MeasureManage] measure.

So what [MeasureManage] does is:

1) Look at the current value of #ButtonCount#. If it is below 2, keep it at 1. Don't let it go below 1. If it is above 4, keep it at 5, don't let it go above 5.

Formula=#ButtonCount# < 2 ? 1 : (#ButtonCount# > 4 ? 5 : #ButtonCount#)

2) Check to see its own current value (which is also the current value of #ButtonCount#) is less than 2 (equal to 1 in other words). If so, use ImageTint to visibly "turn off" the down arrow meter, and "turn on" the up arrow meter.

3) Check to see its own current value (which is also the current value of #ButtonCount#) is greater than 4 (equal to 5 in other words). If so, use ImageTint to visibly "turn off" the up arrow meter, and "turn on" the down arrow meter.

4) If the current value is between 2 and 4 then use ImageTint to visibly "turn on" both arrow meters.

Then the meat and potatoes of the idea:

OnUpdateAction=[!SetVariable ButtonCount [MeasureManage]][!WriteKeyValue Variables ButtonCount [MeasureManage] "#@#ButtonCount.inc"][!Refresh "ButtonBar"]

When the measure is updated, we set the variable #ButtonCount# to the current value of the measure, write the value to our ButtonCount.inc include file, and remotely refresh our ButtonBar skin. That skin will then display our changed number of buttons.
ButtonBar.gif
This requires the latest 3.1 beta of Rainmeter from http://rainmeter.net.
ButtonBar_1.0.rmskin
You do not have the required permissions to view the files attached to this post.
User avatar
rbriddickk84
Rainmeter Sage
Posts: 276
Joined: February 17th, 2014, 12:39 pm
Location: Hungary

Re: Step counter on button click

Post by rbriddickk84 »

I updated the Rainmeter, and i will try soon you advice. Sorry, but i have time to translate correctly english, so i didn't do thet yet.

Simply i just thought on basically a counter or something, which is count ++ only when a button is pressed. But no matter how long the button pressed, that counts only once per buttonpress. All i needed is a variable, which's value can be ++ or -- with mouseClick command.
Sorry for my bad english!
User avatar
rbriddickk84
Rainmeter Sage
Posts: 276
Joined: February 17th, 2014, 12:39 pm
Location: Hungary

Re: Step counter on button click

Post by rbriddickk84 »

Okay, what you put together is just awesome, works perfectly, I really appretiate it!!!
Here is a part from your code which is not very clear some things:

Code: Select all


LeftMouseUpAction=[!SetVariable ButtonCount (#ButtonCount#+1)][!UpdateMeasure MeasureManage][!UpdateMeter *][!Redraw]

In this after the !SetVariable command the first "ButtonCount" word is indicates the ButtonCount.inc file? If it is, then i tried with not *.inc, but with *.txt and it not worked.
Is there that big difference between those file extensions, or just i did something wrong? (probably me)
User avatar
jsmorley
Developer
Posts: 22790
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Step counter on button click

Post by jsmorley »

At the core of this what it does is just increment the #ButtonCount# variable, either +1 or -1 on each click. It then writes that value to the @Include file, which can really be any extension, as long as it is formatted as a .ini file, with:

[Variables]
SomeVar=SomeValue

We recommend using .inc for @Include files, as it just makes sense, but any extension will work.

Most of the work my skin is doing is "managing" the #ButtonCount#. By that I mean if you click "down" and it is already at "1", don't want it to go to "0, -1, -2" etc. That would break things. Conversely, you don't want it to go higher than "5" when you click the "up" arrow. So the first 2/3 of what [MeasureManage] does is related to that.

The last bit, and the most important part for the overall functionality, is having it write the current value of #ButtonCount# to our .inc file, and refresh the ButtonBar skin. Since both the ButtonSet and ButtonBar skin share that @Include file, we can keep things sync'd up between them.
User avatar
jsmorley
Developer
Posts: 22790
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Step counter on button click

Post by jsmorley »

rbriddickk84 wrote: Simply i just thought on basically a counter or something, which is count ++ only when a button is pressed. But no matter how long the button pressed, that counts only once per buttonpress. All i needed is a variable, which's value can be ++ or -- with mouseClick command.
That is more or less what this is doing, although there is no support in Rainmeter for the C construct of Var++ or Var--. Instead, we use (#Var# + 1) or (#Var# -1), which is the same thing really.

It should also be noted that you never have to worry about how "long" a meter with a mouse action is pressed. Whether it is LeftMouseUpAction, LeftMouseDownAction, or ButtonCommand, Rainmeter has no concept at all of a mouse that is "held down", and you always get only one result per mouse action.

This could be done with a Calc measure that simply acts as a "counter" as you say, with an approach where you set the Formula of the [MeasureCount] measure as Formula=(MeasureCount + 1) or Formula=(MeasureCount - 1) with a !SetOption in the LeftMouseUpActions on the arrow meters. However, since the synchronization between this skin and the ButtonBar skin really must be done through setting a shared variable in a permanent way using !WriteKeyValue, it makes sense to just use the variable to keep track of things. You are going to end up setting and using the variable anyway.
User avatar
rbriddickk84
Rainmeter Sage
Posts: 276
Joined: February 17th, 2014, 12:39 pm
Location: Hungary

Re: Step counter on button click

Post by rbriddickk84 »

jsmorley wrote: That is more or less what this is doing, although there is no support in Rainmeter for the C construct of Var++ or Var--. Instead, we use (#Var# + 1) or (#Var# -1), which is the same thing really.

This could be done with a Calc measure that simply acts as a "counter" as you say, with an approach where you set the Formula of the [MeasureCount] measure as Formula=(MeasureCount + 1) or Formula=(MeasureCount - 1) with a !SetOption in the LeftMouseUpActions on the arrow meters. However, since the synchronization between this skin and the ButtonBar skin really must be done through setting a shared variable in a permanent way using !WriteKeyValue, it makes sense to just use the variable to keep track of things. You are going to end up setting and using the variable anyway.
So i did some test on one button, tried the whole thing to build up from scratch:

Code: Select all


[Variables]
@Include=..\@Resources\dat2.txt

[AddBtn]
Meter=Image
SolidColor=0,0,0,5
W=24
H=18
X=130
Y=326
LeftMouseUpAction=[!SetVariable dat2 (#btnum#+1)]

This is not working, in the dat2.txt file the btnum variable is remain the same, no mater what.
User avatar
jsmorley
Developer
Posts: 22790
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Step counter on button click

Post by jsmorley »

rbriddickk84 wrote: So i did some test on one button, tried the whole thing to build up from scratch:

Code: Select all


[Variables]
@Include=..\@Resources\dat2.txt

[AddBtn]
Meter=Image
SolidColor=0,0,0,5
W=24
H=18
X=130
Y=326
LeftMouseUpAction=[!SetVariable dat2 (#btnum#+1)]

This is not working, in the dat2.txt file the btnum variable is remain the same, no mater what.
That is because you are only using !SetVariable to change the value of the variable. That is fine in and of itself, it will change the value of the variable #dat2# in memory in the skin that runs it. (which is not even what you want, see below...) However, it has NOTHING to do with changing the value in any include file. For that you need to use !WriteKeyValue to permanently write the value to the btnum variable in the [Variables] section in dat2.txt. You will also need to !Refresh any skin(s) that are depending on the current value from that dat2.txt file, as @Include files are only read when a skin is loaded or refreshed.

Normally in a situation like this, folks will use BOTH !SetVariable and !WriteKeyValue. The !SetVariable sets the value of the variable in memory in the running skin, and the !WriteKeyValue makes that change both permanent, and in the case of an @Include file, something that can be "shared" between skins.

In any case, I don't think you are understanding how !SetVariable works.

LeftMouseUpAction=[!SetVariable dat2 (#btnum#+1)]

This sets a variable called "dat2" to a value that is the value of another variable "btnum" + 1. It has nothing to do with any file called dat2.???

The format of !SetVariable is !SetVariable "VarName" "Value". That is what it does, and all that it does. It sets or creates a variable in the memory of the currently running skin, setting the value of that variable.
User avatar
jsmorley
Developer
Posts: 22790
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Step counter on button click

Post by jsmorley »

So what I suspect is closer to what you want is:

LeftMouseUpAction=[!SetVariable dat2 (#btnum#+1)]

LeftMouseUpAction=[!WriteKeyValue Variables btnum (#btnum#+1) "#@#dat2.txt"][!SetVariable btnum (#btnum#+1)]

I should warn however, that that approach alone has no "controls" on it at all, as I noted above when explaining my skin, some work is required to set and maintain some "boundaries" for the value. In that code above, when you click the "up" button, it will increase. Not just from 1 to 5 or 10 or any other number, but forever... Same with "down", it will happy change to -1, -2 and so on.

That is why I first increment the variable, then in a sense pass control with !UpdateMeasure to that Calc measure that uses IFCondition to "manage" the value, and ensure that it stays in the desired boundaries, (in my case 1-5) before writing it to the include file.
User avatar
rbriddickk84
Rainmeter Sage
Posts: 276
Joined: February 17th, 2014, 12:39 pm
Location: Hungary

Re: Step counter on button click

Post by rbriddickk84 »

I think there is something misunderstanding, it is my fault, sorry for that!!!

I have the @include file: "dat2.txt" and here is the containings:

dat2.txt :

Code: Select all


[Variables]
btnum=1

And that's why i was wrong, i thought about your code wrongly:

Code: Select all

[!SetVariable ButtonCount (#ButtonCount#+1)]

[color=#000080];["!set the var" in "ButtonCount.inc" "(#ButtonCount variable in the inc file# and + 1)"]
;I thought this sheme would be good for me for start[/color]

[!SetVariable dat2 (#btnum#+1)]

[color=#000080];["!set the var" in "dat2.txt" "(#btnum variable in the txt file# and + 1)"][/color]

I didn't understood fully the functionality of little parts from that line.
I thought probably since a lots of update there was some changes, maybe "!SetVariable" was reduced and do both (memory and writeKeyvalue)...i know, silly thougts. :)
Just becouse i didn't saw any declaration in the Variable section in the ButtonSet.ini file about "ButtonCount" variable, but you used the MouseClickAction with !SetVariable.

I know and thank you for the warning, i just go step by step, but i look over every exceptions and error probabilites for controls, and infinite loops anytime.