GuiButton register different clicks - possible?

Hello Experts,

Was wondering the possibility of registering different clicks on the same GuiButton?
Obviously, the code below does nothing and I am unable to find a way out.

ChId = GetChartID();
GuiButton( "Hello World", 1, 50, 50, 130, 20, 1 );
clrSelBk = Nz( StaticVarGet( "#clrSelBk" + ChId, False ), ColorLightBlue );
GuiSetColors( 1, 1, 0, colorWhite, colorLightBlue, -1, colorWhite, clrSelBk, -1, colorWhite, colorLightBlue, -1, colorWhite, colorLightBlue, -1 );
mip = Nz( StaticVarGet( "#mip" + ChId, False ), False );
FrstClk = NOT mip AND GuiGetEvent( 0, 0 ) AND GuiGetEvent( 0, 1 );
ScndClk = mip AND GuiGetEvent( 0, 0 ) AND GuiGetEvent( 0, 1 );
if( FrstClk )
{
	StaticVarSet( "#mip" + ChId, True, False );
	StaticVarSet( "#clrSelBk" + ChId, colorGold, False );
	//Plot something Do something.....
}
else if( ScndClk )
{
	StaticVarSet( "#mip" + ChId, Null, False );
	StaticVarSet( "#clrSelBk" + ChId, Null, False );
	//Return to Original previous state (before clicking the button) or Plot something else Do something else.....
}

Thank you

hello @Cougar

In your example you show us two clicks. And for that reason i use GuiToggle()
you may start with this simple example

Version(6.28); // you need upgrade to 6.28 

GuiToggle( "Hello World", 1, 50, 50, 130, 20, 1 );

if( GuiGetCheck(1) ) // if gui with ID=1 is cliced 
{
	//Plot something Do something.....
	GuiSetText( "Button is ON",1);
	
}
else // if gui with ID=1 is NOT cliced 
{

	//Return to Original previous state (before clicking the button) or Plot something else Do something else.....
    
}
2 Likes

Thanks Panos!
I forgot to finish the Else if with another Else. So, I am after three clicks or three different states to be precise for some of my buttons.

Hello
I think is better to use 2-3 button instead to write a 50 lines code.
What you think, is it worth?

There are few examples Button Rotate with GFX, but in your case i would make 2 button, and job done, without so many staticvars.

1 Like

Yes, of course it is possible - For instance in my code below a single button allows to switch between 5 or 6 stages of the game:

... so with the use of Static Variables you can achieve everything.

2 Likes

I don't know how to put this Panos. Few months back painstakingly I wrote over 5000 lines using GFX in order to get this:
Snips
Now with the advent of GUI I was wondering whether I could reduce several lines and make my code faster. When I click on Monthly for the first time it deselects all its corresponding levels and does not allow any click on the levels, second click deselects all but allows to click on the individual levels and third click selects all but still does not allow any click on the levels.

@Milosz I saw your Game. I am new to GUIs, so, it was like reading a Novel without knowing the language. :smile: I shall give it a second try!

I am open to your kind suggestions. Thank you!

1 Like

Thanks for the foto.

Sorry but, According to the video you DONOT need 3 clicks
what you need to use in your code is only

  1. Guitoggle
  2. GuiGetCheck
  3. GuiSetCheck

@Cougar did you see, or did you play the game? That makes a difference. I expect you to play this game to learn new stuff :wink: Just don't play it using AB 6.29 as it will look worse because of the issues with GfxSelectPen() in the latest release.

If you score 50 points on "Insane" level (and paste a screenshot), I will make a sample code for you for free :wink::grin:

... but seriously, it's not difficult at all - you can do it. Just add a counter (using Static variable for that) which will hold the number of clicks - it will be increased each time the button is clicked and reset back to zero at some point - like this ( 0 -1 - 2 - 3 - 0 etc) plus switch/case - similar to my code and it's done ...

But at the same these are only some general advices (without considering your specific code). As @PanoS noted, in your case some other - simpler solutions might be a better choice.

Usually - the simpler the code, the better!

2 Likes

When I started writing the above I knew nothing on GFX. So I devoured manual, KB, mail archives and what not. By the time I finished, I am learning that the Lord has updated AB to a new level itself. Yeah I need to take care of my OCD! And start from scratch again Ha ha ha :laughing:

@Cougar remember that you can also combine mouse clicks or GuiButton clicks with keyboard keys - like Shift or Ctrl pressed etc. That gives you enormous number of combinations with just one button. For instance:

... and 5000 lines of code seems a little too much :wink:

2 Likes

Yes Milosz I am in fact using your techniques in the new piece that I am writing.

_SECTION_BEGIN( "Pivot System" );
	 //0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
	 //Begin "Enabler" Drawings
	 //0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
	 ChId = GetChartID();
	 xPos = yPos = 0;
	 MsBtnPressed = GetCursorMouseButtons();
	 MsLftJstClikd = MsBtnPressed & 9;
	 CtrlPressed =  GetAsyncKeyState( 17 ) < 0;
	 MousePxX = GetCursorXPosition( 1 );
	 MousePxY = GetCursorYPosition( 1 );
	 if( CtrlPressed )
	 {
		 if( MsBtnPressed & 9 ) //Ctrl + Left Click
		 {
			 StaticVarSet( "xPos" + ChId, MousePxX, False );
			 StaticVarSet( "yPos" + ChId, MousePxY, False );
		 }
		 if( MsBtnPressed & 4 ) //Ctrl + Mouse Middle Button Click
		 {
			 StaticVarSet( "xPos" + ChId, 0, False );
			 StaticVarSet( "yPos" + ChId, 0, False );
		 }
		 RequestMouseMoveRefresh();
	 }
	 P2xPos = StaticVarGet( "xPos" + ChId, False );
	 P2yPos = StaticVarGet( "yPos" + ChId, False );
	 VisEncapRec = Nz( StaticVarGet( "#VisEncapRec" + ChId, False ), 0 );
	 GfxSetZOrder( 5 );
	 RndRctX1 = 0;
	 RndRctY1 = 0;
	 RndRctX2 = 0;
	 RndRctY2 = 0;
	 //++++++++++++++++++++Positioning++++++++++++++++++++
	 if( P2xPos != 0 OR P2yPos != 0 )
	 {
		 wdthRect = 18;
		 hghtRect = 18;
		 RecClr = ColorRGB( 179, 189, 208 );
		 GfxSelectPen( RecClr );
		 GfxSelectSolidBrush( RecClr );
		 RndRctX1 = P2xPos;
		 RndRctY1 = P2yPos;
		 RndRctX2 = P2xPos + wdthRect;
		 RndRctY2 = P2yPos + hghtRect;
		 GfxRoundRect( RndRctX1, RndRctY1, RndRctX2, RndRctY2, 18, 18 );
		 
		 if( VisEncapRec )
		 {
			 //Drawing Encapsulating Rectangular Block
			 GfxSetZOrder( 4 );
			 wdthEncapRec = 100;
			 hghtEncapRec = 100;
			 EncapRectClr = ColorRGB( 74, 93, 128 );
			 GfxSelectPen( EncapRectClr );
			 GfxSelectSolidBrush( EncapRectClr );
			 EncapRectX1 = ( RndRctX1 + RndRctX2 ) / 2;
			 EncapRectY1 = ( RndRctY1 + RndRctY2 ) / 2;
			 EncapRectX2 = EncapRectX1 + wdthEncapRec;
			 EncapRectY2 = EncapRectY1 + hghtEncapRec;
			 GfxRoundRect( EncapRectX1, EncapRectY1, EncapRectX2, EncapRectY2, 18, 18 );
			 
			 GuiToggle( "Camarilla Pivot System", 1, EncapRectX1 + 5, EncapRectY1 + 5, 130, 20, 1 );
			 clrSelTxtCmrPvtSys = Nz( StaticVarGet( "#clrSelTxtCmrPvtSys" + ChId, False ), colorWhite );
			 clrSelBkCmrPvtSys = Nz( StaticVarGet( "#clrSelBkCmrPvtSys" + ChId, False ), EncapRectClr );			 
			 GuiSetColors( 1, 1, 0, colorWhite, EncapRectClr, EncapRectClr, clrSelTxtCmrPvtSys, clrSelBkCmrPvtSys, clrSelBkCmrPvtSys, colorWhite, RecClr, RecClr, colorWhite, EncapRectClr, EncapRectClr );
			 if( GuiGetCheck( 1 ) )
			 {
				 StaticVarSet( "#clrSelTxtCmrPvtSys" + ChId, False, colorBlack );
				 StaticVarSet( "#clrSelBkCmrPvtSys" + ChId, ColorRGB( 255, 232, 166 ), False );
			 }
			 else
			 {
				 StaticVarSet( "#clrSelTxtCmrPvtSys" + ChId, False, Null );
				 StaticVarSet( "#clrSelBkCmrPvtSys" + ChId, Null, False );
			 }
		 }
	 }
	 //----------------------------------------------------	 
	 //++++++++++++++++++++Specifying OnClick++++++++++++++
	 CrsrInRndRct = MousePxX >= RndRctX1 AND MousePxY >= RndRctY1 AND MousePxX <= RndRctX2 AND MousePxY <= RndRctY2;
	 mipRndRct = Nz( StaticVarGet( "#mipRndRct" + ChId, False ), False );
	 FrstClkRndRct = NOT mipRndRct AND CrsrInRndRct AND MsLftJstClikd;
	 ScndClkRndRct = mipRndRct AND CrsrInRndRct AND MsLftJstClikd;
	 if( FrstClkRndRct ) //First Click on the button
	 {
		 StaticVarSet( "#mipRndRct" + ChId, True, False );
		 StaticVarSet( "#VisEncapRec" + ChId, 1, False );
	 }
	 else if( ScndClkRndRct ) //Second Click on the button
	 {
		 StaticVarSet( "#mipRndRct" + ChId, Null, False );
		 StaticVarSet( "#VisEncapRec" + ChId, 0, False );
	 }
	 //----------------------------------------------------
	 //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	 //End "Enabler" Drawings
	 //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	 RequestMouseMoveRefresh();
_SECTION_END();

I know :tired_face:

1 Like

I haven't analysed your above code in details, but I would start from removing the second RequestMouseMoveRefresh(). For the first time it was used conditionally --> only when Ctrl key is pressed (as in my example) and for the second time unconditionally. Remove the second one, the code will become much "lighter".

Best.

2 Likes

@Cougar something like that

Version(6.28); // you need upgrade to 6.28 

Xaxis= 50;
Yaxis= 10;

GuiToggle( "Camarilla Pivot System", 1, 50, 50, 180, 20, 1 );  
GuiToggle( "Daily", 2, 50, Yaxis+60, 50, 20, 1 );
GuiToggle( "Weekly", 3, 50, Yaxis+80, 50, 20, 1 );

GuiToggle( "R6", 4, Xaxis+50, Yaxis+60, 30, 20, 1 );
GuiToggle( "R5", 5, Xaxis+80, Yaxis+60, 30, 20, 1 );
GuiToggle( "R4", 6, Xaxis+110, Yaxis+60, 30, 20, 1 );
GuiToggle( "R3", 7, Xaxis+140, Yaxis+60, 30, 20, 1 );

GuiToggle( "R6", 8, Xaxis+50, Yaxis+80, 30, 20, 1 );
GuiToggle( "R5", 9, Xaxis+80, Yaxis+80, 30, 20, 1 );
GuiToggle( "R4", 10, Xaxis+110, Yaxis+80, 30, 20, 1 );
GuiToggle( "R3", 11, Xaxis+140, Yaxis+80, 30, 20, 1 );



if( GuiGetCheck( 1 ) ) // if gui with ID=1 is clicked "Camarilla Pivot System"
{
    for( id = 4; id <= 11 ; id++ )  // Enable all the Pivot 
    {
        GuiSetCheck( id, 1 );
    }
    
    GuiSetCheck( 1,0 ); // Run the loop Once 
}


if( GuiGetCheck( 2 ) ) // if gui-Daily with ID=2 is clicked
{

    for( id = 4; id <= 7 ; id++ )  // Disable all the Daily Pivot 
    {
        GuiSetCheck( id, 0 );
    }
	GuiSetCheck( 2, 0 );  //  "UnCheck" the gui-Daily

}


if( GuiGetCheck( 4 ) ) // if Daily-R6 is clicked
{
  Plot( C, "Price", colorDefault, styleCandle );
}
3 Likes

Thanks @Panos, yes you were right! 3 clicks not required. I was unnecessarily thinking too much. Here is what I have got so far:

_SECTION_BEGIN( "MA System" );
	 SetChartOptions( 0, chartShowDates );
	 Plot( C, "", colorDefault, styleCandle );

	 //0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
	 //Begin "Enabler" Drawings
	 //0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
	 ChId = GetChartID();
	 xPos = yPos = 0;
	 MsBtnPressed = GetCursorMouseButtons();
	 MsLftJstClikd = MsBtnPressed & 9;
	 CtrlPressed =  GetAsyncKeyState( 17 ) < 0;
	 MousePxX = GetCursorXPosition( 1 );
	 MousePxY = GetCursorYPosition( 1 );
	 if( CtrlPressed )
	 {
		 if( MsBtnPressed & 9 ) //Ctrl + Left Click
		 {
			 StaticVarSet( "xPos" + ChId, MousePxX, False );
			 StaticVarSet( "yPos" + ChId, MousePxY, False );
		 }
		 if( MsBtnPressed & 4 ) //Ctrl + Mouse Middle Button Click
		 {
			 StaticVarSet( "xPos" + ChId, 0, False );
			 StaticVarSet( "yPos" + ChId, 0, False );
		 }
		 //RequestMouseMoveRefresh();
	 }
	 P2xPos = StaticVarGet( "xPos" + ChId, False );
	 P2yPos = StaticVarGet( "yPos" + ChId, False );
	 VisEncapRec = Nz( StaticVarGet( "#VisEncapRec" + ChId, False ), 0 );
	 GfxSetZOrder( 5 );
	 RndRctX1 = 0;
	 RndRctY1 = 0;
	 RndRctX2 = 0;
	 RndRctY2 = 0;
	 //++++++++++++++++++++Positioning++++++++++++++++++++
	 if( P2xPos != 0 OR P2yPos != 0 )
	 {
		 wdthRect = 18;
		 hghtRect = 18;
		 RecClr = ColorRGB( 179, 189, 208 );
		 GfxSelectPen( RecClr );
		 GfxSelectSolidBrush( RecClr );
		 RndRctX1 = P2xPos;
		 RndRctY1 = P2yPos;
		 RndRctX2 = P2xPos + wdthRect;
		 RndRctY2 = P2yPos + hghtRect;
		 GfxRoundRect( RndRctX1, RndRctY1, RndRctX2, RndRctY2, 18, 18 );
		 
		 if( VisEncapRec )
		 {
			 //Drawing Encapsulating Rectangular Block
			 GfxSetZOrder( 4 );
			 wdthEncapRec = 155;
			 hghtEncapRec = 75;
			 EncapRectClr = ColorRGB( 74, 93, 128 );
			 GfxSelectPen( EncapRectClr );
			 GfxSelectSolidBrush( EncapRectClr );
			 EncapRectX1 = ( RndRctX1 + RndRctX2 ) / 2;
			 EncapRectY1 = ( RndRctY1 + RndRctY2 ) / 2;
			 EncapRectX2 = EncapRectX1 + wdthEncapRec;
			 EncapRectY2 = EncapRectY1 + hghtEncapRec;
			 GfxRoundRect( EncapRectX1, EncapRectY1, EncapRectX2, EncapRectY2, 18, 18 );
			 
			 //-----------------------Drawing GUIs---------------------------
			 height = 20;
			 xMAs = EncapRectX1 + 8;
			 yMAs = EncapRectY1 + 5;
			 GuiToggle( "Moving Averages", 1, xMAs, yMAs, 140, height, 1 );			 
			 
			 widthSMA = 50;
			 pXSMA = xMAs;
			 pYSMA = yMAs + 20;
			 GuiToggle( "SMA", 2, pXSMA, pYSMA, widthSMA, height, 1 );
			 
			 widthiSMA = 30;
			 pXiSMA = pXSMA + widthSMA;
			 for( iSMA = 3; iSMA <= 5; iSMA++ )
			 {
				 if( iSMA == 3 ) TxtBtn = 8;
				 else if( iSMA == 4 ) TxtBtn = 13;
				 else TxtBtn = 21;
				 
				 GuiToggle( NumToStr( TxtBtn, 1.0 ), iSMA, pXiSMA, pYSMA, widthiSMA, height, 1 );
				 pXiSMA = pXiSMA + widthiSMA;
			 }
			 
			 widthEMA = 50;
			 pXEMA = xMAs;
			 pYEMA = yMAs + 40;
			 GuiToggle( "EMA", 6, pXEMA, pYEMA, widthEMA, height, 1 );
			 
			 widthiEMA = 30;
			 pXiEMA = pXEMA + widthEMA;
			 for( iEMA = 7; iEMA <= 9; iEMA++ )
			 {
				 if( iEMA == 7 ) TxtBtn = 8;
				 else if( iEMA == 8 ) TxtBtn = 13;
				 else TxtBtn = 21;
				 
				 GuiToggle( NumToStr( TxtBtn, 1.0 ), iEMA, pXiEMA, pYEMA, widthiEMA, height, 1 );
				 pXiEMA = pXiEMA + widthiEMA;
			 }
			 //------------------------------------------------------------
			 
			 GuiSetColors( 1, 1, 0, colorWhite, EncapRectClr, EncapRectClr, colorBlue, colorGold, colorGold, colorWhite, RecClr, RecClr, colorWhite, EncapRectClr, EncapRectClr );
			 if( GuiGetCheck( 1 ) )
			 {
				 GuiSetColors( 2, 2, 0, colorWhite, EncapRectClr, EncapRectClr, colorBlue, ColorRGB( 255, 232, 166 ), ColorRGB( 255, 232, 166 ), colorWhite, RecClr, RecClr, colorWhite, EncapRectClr, EncapRectClr );
				 GuiSetColors( 6, 6, 0, colorWhite, EncapRectClr, EncapRectClr, colorBlue, ColorRGB( 255, 232, 166 ), ColorRGB( 255, 232, 166 ), colorWhite, RecClr, RecClr, colorWhite, EncapRectClr, EncapRectClr );
			 }
			 else
			 {
				 GuiSetColors( 2, 9, 0, colorWhite, EncapRectClr, EncapRectClr, colorWhite, EncapRectClr, EncapRectClr, colorWhite, EncapRectClr, EncapRectClr, colorWhite, EncapRectClr, EncapRectClr );
				 for( iMA = 2; iMA <= 9; iMA++ )
				 {
					 GuiSetCheck( iMA, 0 );
				 }
			 }
			 
			 if( GuiGetCheck( 2 ) )
			 {
				 GuiSetColors( 3, 5, 0, colorWhite, EncapRectClr, EncapRectClr, colorBlue, ColorRGB( 255, 243, 210 ), ColorRGB( 255, 243, 210 ), colorWhite, RecClr, RecClr, colorWhite, EncapRectClr, EncapRectClr );
			 }
			 else
			 {
				 GuiSetColors( 3, 5, 0, colorWhite, EncapRectClr, EncapRectClr, colorWhite, EncapRectClr, EncapRectClr, colorWhite, EncapRectClr, EncapRectClr, colorWhite, EncapRectClr, EncapRectClr );
				 for( iSMA = 3; iSMA <= 5; iSMA++ )
				 {
					 GuiSetCheck( iSMA, 0 );
				 }
			 }
			 
			 if( GuiGetCheck( 6 ) )
			 {
				 GuiSetColors( 7, 9, 0, colorWhite, EncapRectClr, EncapRectClr, colorBlue, ColorRGB( 255, 243, 210 ), ColorRGB( 255, 243, 210 ), colorWhite, RecClr, RecClr, colorWhite, EncapRectClr, EncapRectClr );
			 }
			 else
			 {
				 GuiSetColors( 7, 9, 0, colorWhite, EncapRectClr, EncapRectClr, colorWhite, EncapRectClr, EncapRectClr, colorWhite, EncapRectClr, EncapRectClr, colorWhite, EncapRectClr, EncapRectClr );
				 for( iEMA = 7; iEMA <= 9; iEMA++ )
				 {
					 GuiSetCheck( iEMA, 0 );
				 }
			 }
			 
			 for( iSMA = 3; iSMA <= 5; iSMA++ )
			 {
				 if( GuiGetCheck( iSMA ) )
				 {
					 Plot( MA( C, StrToNum( GuiGetText( iSMA ) ) ), "", ColorRGB( 0, 0, 255 - ( 4 * iSMA ) ), styleLine | styleNoRescale );
				 }
			 }
			 
			 for( iEMA = 7; iEMA <= 9; iEMA++ )
			 {
				 if( GuiGetCheck( iEMA ) )
				 {
					 Plot( EMA( C, StrToNum( GuiGetText( iEMA ) ) ), "", ColorRGB( 0, 255 - ( 4 * iEMA ), 0 ), styleLine | styleNoRescale );
				 }
			 }
		 }
	 }
	 //----------------------------------------------------	 
	 //++++++++++++++++++++Specifying OnClick Using GFX++++++++++++++
	 CrsrInRndRct = MousePxX >= RndRctX1 AND MousePxY >= RndRctY1 AND MousePxX <= RndRctX2 AND MousePxY <= RndRctY2;
	 mipRndRct = Nz( StaticVarGet( "#mipRndRct" + ChId, False ), False );
	 FrstClkRndRct = NOT mipRndRct AND CrsrInRndRct AND MsLftJstClikd;
	 ScndClkRndRct = mipRndRct AND CrsrInRndRct AND MsLftJstClikd;
	 if( FrstClkRndRct ) //First Click on the button
	 {
		 StaticVarSet( "#mipRndRct" + ChId, True, False );
		 StaticVarSet( "#VisEncapRec" + ChId, 1, False );
	 }
	 else if( ScndClkRndRct ) //Second Click on the button
	 {
		 StaticVarSet( "#mipRndRct" + ChId, Null, False );
		 StaticVarSet( "#VisEncapRec" + ChId, 0, False );
	 }
	 //--------------------------------------------------------------
	 //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	 //End "Enabler" Drawings
	 //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	 RequestMouseMoveRefresh();
_SECTION_END();

Many many thanks to @Milosz as well for his uncountable contributions!

2 Likes