It is currently March 29th, 2024, 9:38 am

[SOLVED] Mouse actions linked via if statement

Get help with creating, editing & fixing problems with skins
User avatar
KaLeun
Posts: 12
Joined: December 11th, 2019, 11:01 pm
Location: Germany

[SOLVED] Mouse actions linked via if statement

Post by KaLeun »

Hello everybody,

I'm currently making my very first rainmeter skin and I'm having some troubles with the code:

The skin is a simple kill switch, as in a flip switch with a cover, as typically seen in (military) aircraft. To work properly this switch needs to have a couple states:
1. Closed: In this case hovering over should open the hood, but not actuate the switch. If the mouse stops hovering over the skin the hood should close again
2. Pressed: If the switch is clicked (while being hovered) the switch should.. well switch and stay in that state (Meaning state 1 is no longer in effect). Also some executables shall be executed.
3. Closing: If the switch is activated again by clicking it, the hood should slam down, disabling the switch again and resetting everything.

ImageImage Image

Visually these three states are distinguished by three renders of a switch i modeled. Since I'm unfortunately quite daft if it comes to coding, this is my current progress:

Code: Select all


[Metadata]
Name=DCS Set Up
Author=Herr_KaLeun
Information=Stuff
Version=0.1 20191209
License=Creative Commons Attribution - Non - Commercial - Share Alike 3.0

[Variables]
Path = PATH
ImageOn = #Path#\On.png
ImageOff = #Path#\Off.png
ImageClosed = #Path#\Closed.png
SoundOpen = #Path#\Open.wav
SoundClose = #Path#\Close.wav
SoundSwitch = #Path#\On.wav
SoundHoodClose = #Path#\HoodClose.wav
###Dynamic Variable to check whether it was clicked
Pressed = 1
DynamicVariables=1

[MeterBackground]
Pressed = 1
Meter = Image
ImageName = #ImageClosed#
W = 250
H = 250
GreyScale = 0
ImageTint = 255,255,255,255

###Open and Close when the mouse hovers over
MouseOverAction = !Execute [Play "#SoundOpen#"][!SetOption #CURRENTSECTION# ImageName "#ImageOff#"][!UpdateMeter #CURRENTSECTION#][!Redraw]
MouseLeaveAction = !Execute [Play "#SoundHoodClose#"][!SetOption #CURRENTSECTION# ImageName "#ImageClosed#"][!UpdateMeter #CURRENTSECTION#][!Redraw]
###Switch the switch and execute the executables
LeftMouseDownAction = !Execute [!SetVariable Pressed = "1"][Play "#SoundSwitch#"]["DIFFERENT PATH"]["DIFFERENT PATH"]["DIFFERENT PATH"]["WINDOWS SETTINGS][!SetOption #CURRENTSECTION# ImageName="#ImageOn#"][!UpdateMeter #CURRENTSECTION#][!Redraw]
###Close the switch, resetting everything
LeftMouseDownAction = !Execute [!SetVariable Pressed = "0"][Play "#SoundClose#"][!SetOption #CURRENTSECTION# ImageName="#ImageClosed#"][!UpdateMeter #CURRENTSECTION#][!Redraw]


What does work? The state number 1 works 100%, the state number 2 does work in that it executes the programs. Sadly it does not change the image and there is no "latching" of the switch. State 3 does not work at all, since I can't really progress on that front as long as number 2 is not figured out.

I've tried implementing an if statement in various ways, sadly none of that's working.

That's it. I hope the solution isn't too too obvious ;)

Greetings and many thanks,
Herr KaLeun
Last edited by KaLeun on December 12th, 2019, 5:02 pm, edited 2 times in total.
User avatar
Yamajac
Posts: 134
Joined: June 30th, 2014, 8:44 am

Re: Mouse actions linked via if statement

Post by Yamajac »

First up, get rid of the !execute, that's not needed.


You also can't have two LeftMouseDownActions.
User avatar
Jeff
Posts: 326
Joined: September 3rd, 2018, 11:18 am

Re: Mouse actions linked via if statement

Post by Jeff »

Beaside that there are some fundamental things that will never work, [!SetVariable Pressed = "0"] the = should not exist there and neither in [!SetOption #CURRENTSECTION# ImageName="#ImageClosed#"] (seems like a copy paste job since you had the same bang fine a few lines above lol).
If you want to have separate mouse actions like that make another meter, you will basically have a meter for when the button is OFF and when the button is ON on which you asigned the MouseActions you have there.
Beaside that everything looks fine at first glance.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5384
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Mouse actions linked via if statement

Post by eclectic-tech »

KaLeun wrote: December 11th, 2019, 11:16 pm Hello everybody,

I'm currently making my very first rainmeter skin and I'm having some troubles with the code:

The skin is a simple kill switch, as in a flip switch with a cover, as typically seen in (military) aircraft. To work properly this switch needs to have a couple states:
1. Closed: In this case hovering over should open the hood, but not actuate the switch. If the mouse stops hovering over the skin the hood should close again
2. Pressed: If the switch is clicked (while being hovered) the switch should.. well switch and stay in that state (Meaning state 1 is no longer in effect). Also some executables shall be executed.
3. Closing: If the switch is activated again by clicking it, the hood should slam down, disabling the switch again and resetting everything.

What does work? The state number 1 works 100%, the state number 2 does work in that it executes the programs. Sadly it does not change the image and there is no "latching" of the switch. State 3 does not work at all, since I can't really progress on that front as long as number 2 is not figured out.

I've tried implementing an if statement in various ways, sadly none of that's working.

That's it. I hope the solution isn't too too obvious ;)

Greetings and many thanks,
Herr KaLeun
Okay, I corrected some syntax that were either deprecated (!Execute) or improperly formatted (!SetOption).
It is not allowed to have more than one type of each mouse action, so to "toggle" the state of your "Pressed" variable, I created a new On.png (with the word "ON" below the switch) and changed the action to a calculation that will toggle "Pressed" between 1 and 0.

Here is the new "On.png" (optional)
On.png
This code will preform as you mentioned. I did not add any tasks to perform when the switch is clicked, if you add them to the LeftMouseUpAction they will fire on both the "On" and "Off" actions... to prevent this you may need to create a measure that monitors the state of "Pressed" and update that measure when the mouse is clicked besides changing the image (which is all this code does). I changed the mouse button action to UP to allow the skin to be dragged and positioned.

EDIT: I added an example measure that can be used to control if actions are taken when the switch is ON/OFF. It will show in the log if actions were performed.
click2.gif

Code: Select all

[Metadata]
Name=DCS Set Up
Author=Herr_KaLeun
Information=Stuff
Version=0.1 20191209
License=Creative Commons Attribution - Non - Commercial - Share Alike 3.0

[Variables]
Path=PATH
Image1=#Path#\On.png
Image0=#Path#\Off.png
ImageClosed=#Path#\Closed.png
SoundOpen=#Path#\Open.wav
SoundClose=#Path#\Close.wav
SoundSwitch=#Path#\On.wav
SoundHoodClose=#Path#\HoodClose.wav
; Dynamic Variable to check whether it was clicked
Pressed=0

[MeterBackground]
DynamicVariables=1
Meter=Image
ImageName=#ImageClosed#
W=250
H=250
; GreyScale=0
; ImageTint=255,255,255,255
SolidColor=0,0,0,255

; Open and Close when the mouse hovers over
MouseOverAction=[Play "#SoundOpen#"][!SetOption #CURRENTSECTION# ImageName "[#Image[#Pressed]]"][!UpdateMeter #CURRENTSECTION#][!Redraw]
MouseLeaveAction=[!SetVariable Pressed "0"][Play "#SoundHoodClose#"][!SetOption #CURRENTSECTION# ImageName "#ImageClosed#"][!UpdateMeter #CURRENTSECTION#][!Redraw]

; Switch the switch and execute the executables
LeftMouseUpAction=[!SetVariable Pressed (1-#Pressed#)][Play "#SoundSwitch#"][!SetOption #CURRENTSECTION# ImageName "[#Image[#Pressed]]"][!UpdateMeter #CURRENTSECTION#][!Redraw][!UpdateMeasure mPressed]
; ["DIFFERENT PATH"]["DIFFERENT PATH"]["DIFFERENT PATH"]["WINDOWS SETTINGS]

[mPressed]
DynamicVariables=1
Measure=Calc
Formula=#Pressed#
IfCondition=(#Pressed#=1)
; Actions to happen when alarm is ON
IfTrueAction=[!Log "Actions performed!"]
; Actions to happen when alarm is OFF
IfFalseAction=[][!Log "NO Actions performed!"]
; Do not update until told to do so by !UpdateMeasure bang
UpdateDivider=-1

Feel free to ask any questions.
You do not have the required permissions to view the files attached to this post.
User avatar
KaLeun
Posts: 12
Joined: December 11th, 2019, 11:01 pm
Location: Germany

Re: Mouse actions linked via if statement

Post by KaLeun »

Thank you everyone for the replies. One thing is apparent: I don't know a thing about coding ;)

The reason why there was a second LeftMouseDownAction was to later implement the if statement and then differentiating between the two LeftMouseDownActions via the if.
Thank you for making the On.png, but it's not really needed. The images try to mimic a switch in an aircraft, the white text is painted on and stamped into the sheetmetal: The off will never become an on. It just show's where the OFF position is.
The current design is a placeholder to finish the coding. I'm a way better 3D and 2D designer than a coder (:

I will try your code @ecletic-tech and get back to you (:


EDIT:
The new code mostly works, it definetly distinguishes between the two states "on" and "off". What doesn't work yet is that the switch stays open when pressed = 1 and only closes (slams) when you click it again. This was the point of the second LeftMouseDownAction.

So as long as Pressed = 1 the HoverActions should not work anymore and the switch should stay in the On position. When it's clicked again (With the second LeftMouseDownAction, only available at Pressed = 1) and Pressed reverts to Pressed = 0 the switch should reset itself, again allowing the HoverActions and the first LeftMouseDownAction (executing executables)

This means that all of the MouseActions have to be behind the If-statement, since both the HoverAction and the LeftMouseDownAction are dependend on the variable and do different things for different values of Pressed.

Thanks in advance!


EDIT2: Is it enough to paste the actions into the [mPressed] Section after the appropiate IfTrueAction or IfFalseAction?
User avatar
KaLeun
Posts: 12
Joined: December 11th, 2019, 11:01 pm
Location: Germany

Re: Mouse actions linked via if statement

Post by KaLeun »

ImageImageImage


This is the new design and may clarify things. The stripes are supposed to look like hand painted, I was going for the F-14 look

Image
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5384
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Mouse actions linked via if statement

Post by eclectic-tech »

KaLeun wrote: December 12th, 2019, 12:18 pm EDIT2: Is it enough to paste the actions into the [mPressed] Section after the appropiate IfTrueAction or IfFalseAction?
Yeah, I didn't need to change your graphics; what you have makes perfect sense. :oops:

Yes, [mPressed] is where you want to change possible actions.
Replace the conditional statements in that measure to also enable/disable mouse hover actions when the switch has been toggled.

Code: Select all

[mPressed]
DynamicVariables=1
Measure=Calc
Formula=#Pressed#

IfCondition=(#Pressed#=1)
; Actions to happen when alarm is ON
IfTrueAction=[!DisableMouseAction MeterBackground "MouseOverAction|MouseLeaveAction"]["DIFFERENT PATH"]["DIFFERENT PATH"]["DIFFERENT PATH"]["WINDOWS SETTINGS][!Log "Actions performed!"]

; Actions to happen when alarm is OFF
IfFalseAction=[!EnableMouseAction MeterBackground "MouseOverAction|MouseLeaveAction"][!Log "NO Actions performed!"]

; Do not update until told to do so by !UpdateMeasure bang
UpdateDivider=-1
This will disable all mouse hover actions when #Pressed# equals 1 and then enable them when it equals zero.
User avatar
KaLeun
Posts: 12
Joined: December 11th, 2019, 11:01 pm
Location: Germany

Re: Mouse actions linked via if statement

Post by KaLeun »

Thanks for the reply

This works really well. I changed a few things around and tried to implement the LeftMouseUpAction into the If statement, sadly it doesn't seem to work the way I'm doing it.
Probably just a small mistake:

Code: Select all

[mPressed]
DynamicVariables=1
Measure=Calc
Formula=#Pressed#

IfCondition=(#Pressed#=0)
; Actions to happen when switch is SAFE
IfTrueAction=[!EnableMouseAction MeterBackground "MouseOverAction|MouseLeaveAction"][LeftMouseUpAction = [Play "#SoundSwitch#"]["G:\Programme\DCS-SimpleRadio-Standalone\SR-ClientRadio.exe"]["C:\Program Files\Logitech\X52\ST.exe"]["G:\Programme\TrackIR\TrackIR5.exe"]["%windir%\System32\cmd.exe" /c start joy.cpl][!SetOption #CURRENTSECTION# ImageName="#Image1#"]][!Log "Actions performed!"][!SetVariable Pressed = "1"]

; Actions to happen when switch is ARM
IfFalseAction=[!DisableMouseAction MeterBackground "MouseOverAction|MouseLeaveAction"]LeftMouseDownAction = [!SetVariable Pressed "0"][Play "#SoundHoodClose#"][!SetOption #CURRENTSECTION# ImageName="#ImageClosed#"][!Log "NO Actions performed!"]

; Do not update until told to do so by !UpdateMeasure bang
UpdateDivider=-1

What keeps the LeftMouseUp action on IfTrueAction to be played? Is it not possible to link Actions this way? Can I set multiple LeftMouseDown/LeftMouseUp Actions and enable/disable them? Or even just a LeftMouseUp and a LeftMouseDown, that are enabled or disabled?
User avatar
KaLeun
Posts: 12
Joined: December 11th, 2019, 11:01 pm
Location: Germany

Re: Mouse actions linked via if statement

Post by KaLeun »

Code: Select all

[MeterBackground]
DynamicVariables=1
Meter=Image
ImageName=#ImageClosed#
W=300
H=300
;GreyScale=0
ImageTint=255,255,255,255
;SolidColor=0,0,0,255

; Open and Close when the mouse hovers over
MouseOverAction=[Play "#SoundOpen#"][!SetOption #CURRENTSECTION# ImageName "#Image0#"][!UpdateMeter #CURRENTSECTION#][!Redraw]
MouseLeaveAction=[Play "#SoundHoodClose#"][!SetOption #CURRENTSECTION# ImageName "#ImageClosed#"][!UpdateMeter #CURRENTSECTION#][!Redraw]

; Switch the switch and execute the executables
LeftMouseDownAction=[!SetVariable Pressed "1"][Play "#SoundSwitch#"][!SetOption #CURRENTSECTION# ImageName "[#Image[#Pressed]]"][!UpdateMeter #CURRENTSECTION#][!Redraw][!UpdateMeasure mPressed]
LeftMouseUpAction=[!SetVariable Pressed "0"][Play "#SoundClose#"][!SetOption #CURRENTSECTION# ImageName "[#Image[#Pressed]]"][!UpdateMeter #CURRENTSECTION#][!Redraw][!UpdateMeasure mPressed]
; ["DIFFERENT PATH"]["DIFFERENT PATH"]["DIFFERENT PATH"]["WINDOWS SETTINGS]


[mPressed]
DynamicVariables=1
Measure=Calc
Formula=#Pressed#

IfCondition=(#Pressed#=0)
; Actions to happen when switch is SAFE
IfTrueAction=[!EnableMouseAction MeterBackground "MouseOverAction|MouseLeaveAction|LeftMouseDownAction"][!DisableMouseAction MeterBackround "LeftMouseUpAction"][!Log "Actions performed!"]

; Actions to happen when switch is ARM
IfFalseAction=[!DisableMouseAction MeterBackground "MouseOverAction|MouseLeaveAction|LeftMouseDownAction"][!EnableMouseAction MeterBackround "LeftMouseUpAction"][!Log "NO Actions performed!"]

; Do not update until told to do so by !UpdateMeasure bang
UpdateDivider=-1

This now kinda works, it's just super janky since both a standard click on the switch will execute both LeftMouseUp and LeftMouseDown...
User avatar
KaLeun
Posts: 12
Joined: December 11th, 2019, 11:01 pm
Location: Germany

Re: Mouse actions linked via if statement

Post by KaLeun »

And another change:

Double click now closes the switch together with the cover. Also I introduced another variable to keep the MouseLeaveAction from executing after the switch was closed by the double click.

Code: Select all

[MeterBackground]
DynamicVariables=1
Meter=Image
ImageName=#ImageClosed#
W=300
H=300
;GreyScale=0
ImageTint=255,255,255,255
;SolidColor=0,0,0,255

; Open and Close when the mouse hovers over
MouseOverAction=[Play "#SoundOpen#"][!SetOption #CURRENTSECTION# ImageName "#Image0#"][!UpdateMeter #CURRENTSECTION#][!SetVariable Open "1"][!Redraw][!UpdateMeasure mOpen]
MouseLeaveAction=[Play "#SoundHoodClose#"][!SetOption #CURRENTSECTION# ImageName "#ImageClosed#"][!UpdateMeter #CURRENTSECTION#][!SetVariable Open "0"][!Redraw][!UpdateMeasure mOpen]

; Switch the switch and execute the executables
LeftMouseDownAction=[!SetVariable Pressed "1"][Play "#SoundSwitch#"][!SetOption #CURRENTSECTION# ImageName "[#Image[#Pressed]]"][!UpdateMeter #CURRENTSECTION#][!Redraw][!UpdateMeasure mPressed]
LeftMouseDoubleClickAction=[!SetVariable Pressed "0"][!SetVariable Open "0"][Play "#SoundClose#"][!SetOption #CURRENTSECTION# ImageName "[#ImageClosed]"][!UpdateMeter #CURRENTSECTION#][!Redraw][!UpdateMeasure mPressed][!UpdateMeasure mOpen]
; ["DIFFERENT PATH"]["DIFFERENT PATH"]["DIFFERENT PATH"]["WINDOWS SETTINGS]


[mPressed]
DynamicVariables=1
Measure=Calc
Formula=#Pressed#

IfCondition=(#Pressed#=0)
; Actions to happen when switch is SAFE
IfTrueAction=[!EnableMouseAction MeterBackground "MouseOverAction|MouseLeaveAction|LeftMouseDownAction"][!DisableMouseAction MeterBackround "LeftMouseDoubleClickAction"][!Log "Actions performed!"]

; Actions to happen when switch is ARM
IfFalseAction=[!DisableMouseAction MeterBackground "MouseOverAction|MouseLeaveAction|LeftMouseDownAction"][!EnableMouseAction MeterBackround "LeftMouseDoubleClickAction"][!Log "NO Actions performed!"]

; Do not update until told to do so by !UpdateMeasure bang
UpdateDivider=-1

[mOpen]
DynamicVariables=1
Measure=Calc
Formula=#Open#

IfCondition=(#Open#=0)
; Actions to happen when switch is SAFE
IfTrueAction=[!DisableMouseAction MeterBackground "MouseLeaveAction"]

; Actions to happen when switch is ARM
IfFalseAction=[!EnableMouseAction MeterBackground "MouseLeaveAction"]

; Do not update until told to do so by !UpdateMeasure bang
UpdateDivider=-1