Page 1 of 1

Toggle problem

Posted: September 9th, 2017, 6:17 am
by BerlingSwe
Hi. I am in the process of expanding the original chili framework to support GUI (menus, buttons etc)
But there is one small thing that has been bugging me for days and I have tried various different ways to solve it but none have worked.

Code: Select all

		if (mouse.LeftIsPressed())
		{
			isClicked = !isClicked;
		}
Basically I want the button to toggle every time it is clicked once, but if I hold down the left mouse button, the button toggles back and fourth many times quickly, which is not what i want. I want it to toggle once no matter how long you are holding the left mouse button for.

Any help appreciated, thanks!

Re: Toggle problem

Posted: September 9th, 2017, 9:48 am
by ceofil

Re: Toggle problem

Posted: September 9th, 2017, 1:22 pm
by Slidy
Not sure if that wiki page helped you, but my solution was to run it when mouse is depressed instead of pressed. Can be done by keeping track of bool.
e.g.

Code: Select all

if( wnd.mouse.LeftIsPressed() )
{
	wasPressed = true;
}
else
{
	if( wasPressed )
	{
		// do code
	}
	wasPressed = false;
}

Re: Toggle problem

Posted: September 9th, 2017, 2:17 pm
by BerlingSwe
Slidy wrote:Not sure if that wiki page helped you, but my solution was to run it when mouse is depressed instead of pressed. Can be done by keeping track of bool.
e.g.

Code: Select all

if( wnd.mouse.LeftIsPressed() )
{
	wasPressed = true;
}
else
{
	if( wasPressed )
	{
		// do code
	}
	wasPressed = false;
}
Thank you kind sir

Re: Toggle problem

Posted: September 10th, 2017, 1:45 am
by albinopapa
Just want to point out that the info on the wiki is a better fit for what you are trying to do.

Code: Select all

	while( !wnd.mouse.IsEmpty() )
	{
		const auto e = wnd.mouse.Read();

		if( e.GetType() == Mouse::Event::Type::LPress )
		{
			// Handle press event
		}
		else if( e.GetType() == Mouse::Event::Type::LRelease )
		{
			// Handle release event
		}
	}