Click System

The Partridge Family were neither partridges nor a family. Discuss.
coolmoon
Posts: 7
Joined: April 7th, 2017, 12:17 am

Re: Click System

Post by coolmoon » May 8th, 2017, 3:23 am

I am just trying to learn, and this is probably like the shitty work around you don't want to do but what if you created a setter in GUIElement.h like

Code: Select all

virtual void Released() {Click = false;}
then call it in

Code: Select all

void GameStateGamePlay::ButtonHandler()
{
	GameState::GlobalButtonHandler();
	//main scene button handler 
	if (GUI.GetElement("CharMenu")->IsClicked())
	{
		if (!GUI.GetElement("CharMenu")->IsActive())
		{
			GUI.GetElement("CharMenu")->SetActive(true);
			GUI.GetElement("UserMenuGroup")->Enable();
		}
		else
		{
			GUI.GetElement("CharMenu")->SetActive(false);
			GUI.GetElement("UserMenuGroup")->Disable();
		}
		GUI.GetElement("CharMenu")->Released();
	}
}
I know it's not triggered by OnLeftRelease event like you want,but is it the outcome what you want?

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: Click System

Post by egizz983 » May 8th, 2017, 7:24 am

Man thas the GUI job to handle this if i would need to make this each time i setup a button whats the point of my GUI system then .

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: Click System

Post by egizz983 » May 8th, 2017, 7:26 am

AlibinoPapa , yes thats the way i am currently handling it , one LeftPress event Click = true; on Release click = false; , so again i am not sure if i should try to do this with GUI system or do some kind a changes to Mouse class ?

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: Click System

Post by egizz983 » May 8th, 2017, 1:22 pm

Also AlibinoPapa you said i only get single Lpress and Lrelease messages how else i can get them ?

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Click System

Post by albinopapa » May 8th, 2017, 6:51 pm

What I mean is when you press the left mouse button, the Mouse class isn't receiving LPress messages each frame it is held down. It's only receiving them for the initial press, and not again until you release and press it again.

With all the inheritance and the 37 GUI elements it's difficult to nail down.

Let's think about this:
  • You press the left mouse button on the CharMenu button and a single Mouse::Event::LPress is created and stored in the mouse object.
  • When you process the mouse queue and come across a mouse LPress, you call GUIElement::OnLeftIsPressed and it sets Click to true.
  • During that frame, you check if charmenu is clicked, if not active, you set active and if active you set not active.
That last one is where your problem lies. Caching the click would have the same effect as just calling Mouse::LeftIsPressed instead of Mouse::Read. As others have stated, once you process the click, you'd need to clear the Click to false as if it were a message in a queue. So the problem isn't in the mouse class, it's in your logic of handling the click after that's in your GUIElement.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Click System

Post by albinopapa » May 8th, 2017, 6:56 pm

Think about how the Mouse::Read and the Mouse::LeftIsPressed functions work. The Mouse class stores a bool if the left button is pressed and LeftIsPressed returns that bool. The bool doesn't get changed until you release the left mouse button. Mouse::Read creates a single LPress Mouse::Event and once read, that event is removed from the queue so it doesn't retain an LPress state.

The way you store the Click would be the same way the Mouse class stores it's bool that determines if LeftIsPressed, since you don't set Click to false until you release the mouse button, the code block

Code: Select all

	if ( charmenu->IsClicked())
	{
		if (!charmenu->IsActive())
		{
			charmenu->SetActive(true);
			userMenuGroup->Enable();
		}
		else
		{
			charmenu->SetActive(false);
			userMenuGroup->Disable();
		}		
	}

will always set active then inactive for each frame that Click is true.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Click System

Post by albinopapa » May 8th, 2017, 7:13 pm

So here's an alternative to the other suggestions on here:

Code: Select all

if (i->IsEnabled()) 
{
	if (i->IsHover())
	{
		i->OnLeftPressed();
		Condition = true;
		for (auto& a : Elements)
		{
			if (a == i) 
			{
				continue;
			}
			a->SetActive( false );
		}

		if( !i->IsActive() )
			i->SetActive( true );
		else
			i->SetActive( false );
	}
	else 
	{
		i->SetActive(false);
	}
}
This is in the GUIManager::MouseHandler function

And here are the other changes

Code: Select all

void GameStateGamePlay::ButtonHandler()
{
	GameState::GlobalButtonHandler();

	//main scene button handler 
	auto charmenu = GUI.GetElement( "CharMenu" );
	auto userMenuGroup = GUI.GetElement( "UserMenuGroup" );

	charmenu->IsActive() ?
		userMenuGroup->Enable() : 
		userMenuGroup->Disable();
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Click System

Post by albinopapa » May 8th, 2017, 7:18 pm

I cached the charmenu because during debug stepping through the loops while trying to find "CharMenu" for each function call was too time consuming.

During development, finding elements by name multiple times is fine, but I would consider caching or finding an alternative to finding the element you are dealing with for the release version.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: Click System

Post by egizz983 » May 8th, 2017, 10:45 pm

hmm interesting that's makes a sense actually . i was thinking that Mouse was sending multiple events until you release the button not a single as u said , that change a lot i guess . the alternative you gave actually would work for toggle , but that would not solve a thing that button will be proceed before you release i mean , as example you put login details and click on Login button you dont need to release LMB and its till gets proceed , its not like a big deal but knowing that mouse send only one event over click i might work a better way to handle it

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Click System

Post by albinopapa » May 9th, 2017, 12:58 am

Well, you could make a state for the button where while the left button is down, it does nothing but maintain that state and draw that state. Then when left is released, the state changes and then stuff is processed. You'd need three states, Idle, Pressed and Released.

During the Idle state, you are checking for the Click event and if Click change to the Pressed state.
During the Pressed state, you are checking for the !Click event and if !Click change to the Released state.
During the Release state you would do the processing then return to the Idle state.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply