Click System

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Click System

Post by chili » May 9th, 2017, 1:11 am

egizz983 wrote: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 .
This is a pretty shitty attitude towards somebody who is a) trying to help and b) trying to learn.
Chili

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

Re: Click System

Post by egizz983 » May 9th, 2017, 8:21 am

@Chili but i didt say anything wrong :/ and i know hes trying to learn thats why i said that i cannot do that way because GUI have to deal with it

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

Re: Click System

Post by albinopapa » May 9th, 2017, 10:21 am

@egizz983, You didn't really say anything wrong, it just came across as condescending and rude. Some of that might just be the language barrier or cultural difference.

Worked with a guy from Africa one time who told a coworker she was getting fat. He meant it in a sincere way, or so he says, but here telling someone they are getting fat is offensive. It was a culture thing.

Anyway, just be little kinder in your replies and thank those for their suggestions, even when it isn't the outcome you wanted. They did take the time to look through your code and offer a solution after all.

By the way, you have put in a lot of time on this code, can't believe you've been only been coding for 8 or 9 months. Good work man.
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 9th, 2017, 10:34 pm

Tnx for tips Alibinopapa , i havent been coding this project that long :D , last two month as was just looking trough the code when ever i had time so i would not forget whats going on , i think i was starting on december or january :/

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

Re: Click System

Post by albinopapa » May 10th, 2017, 3:50 am

Well, you've been apart of the community since Aug 27, 2016 and I figure you have been working on the GUI system at least 5-6 months if not longer. You made that space shooter before that.

Anyway, hope things work out for you, let us know if there is anything else.
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 10th, 2017, 10:01 am

That space shooter was just first game , i would be making it differently now , i was thinking to recode that one but i am interested in networking stuff so i rather code multiplayer

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

Re: Click System

Post by egizz983 » May 10th, 2017, 3:17 pm

i cant think any other solution just to reset Click for all elements once new frame begins

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

Re: Click System

Post by albinopapa » May 10th, 2017, 6:05 pm

Look, your elements only have a clicked or unclicked state. This works for event driven environments like the Windows GUI, but won't cut it with a system that checks state all the time. This is why Windows and chili's Mouse/Keyboard class use a message queue. Perhaps incorporating a messaging system is needed.

If you need to check state, and that state is going to persist with undesired results between frames, then you need to have another state as I mentioned before. The other possibilities would be to have buttons do something more than just set Click to true or false like passing in a functor that gets fired off when OnLeftRelease is called. You can have the Buttons take in a function object such as a lambda, an std::function or a class with the operator() overloaded ( which is what a lambda really is under the hood ), then when OnLeftReleased is called, the Button objects would call this function. This way you wouldn't be relying on Click.

Example:

Code: Select all

class GUIElement
{
public:
	virtual void OnLeftPress() {}
	virtual void OnLeftRelease() {}
	void Enable( bool Val ) { Enabled = Val; }
	void Activate( bool Val ) { Active = Val; }

protected:
	bool Enabled = false;
	bool Active = false;
};

class GUIElemStateHandler
{
public:
	GUIElemStateHandler( GUIElement *pElement ) : m_pElement( pElement ) {}
	virtual void operator()() {}

protected:
	GUIElement *m_pElement = nullptr;

};
class GUIEnabler : public GUIElemStateHandler
{
public:
	GUIEnabler( GUIElement *pElement ) : GUIElemStateHandler( pElement ) {}
	void operator()()override
	{
		m_pElement->Enable( true );
	}
};
class GUIDisabler : public GUIElemStateHandler
{
public:
	GUIDisabler( GUIElement *pElement ) : GUIElemStateHandler( pElement ) {}
	void operator()()override
	{
		m_pElement->Enable( false );
	}

};
class GUIGroup : public GUIElement
{
public:
	
};
class GUIButton : public GUIElement
{
public:
	GUIButton( GUIEnabler *pEnabler, GUIDisabler *pDisabler )
		:m_pEnabler( pEnabler ), m_pDisabler( pDisabler ) 
	{}
	void OnLeftPress() override
	{
		( *m_pEnabler )( );
	}
	void OnLeftRelease()override
	{
		( *m_pDisabler )( );
	}
private:
	GUIElemStateHandler *m_pEnabler, *m_pDisabler;
};
class Mouse
{
public:
	enum class EventType
	{
		LPress, LRelease
	};
	class Event
	{
	public:
		EventType GetType()const
		{
			return eType;
		}
	private:
		EventType eType;
	};
	Event Read()
	{
		return e;
	}
private:
	Event e;
};
class GUIManager
{
public:
	GUIManager()
	{
		GUIGroup grp;
		GUIEnabler en( &grp );
		GUIDisabler dis( &grp );
		GUIButton btn( &en, &dis );
		m_pElements.push_back( &btn );
	}
	void MouseHandler(Mouse mouse)
	{
		const auto evnt = mouse.Read();

		for( auto &elem : m_pElements )
		{
			switch( evnt.GetType() )
			{
				case Mouse::EventType::LPress:
					elem->OnLeftPress();
					break;
				case Mouse::EventType::LRelease:
					elem->OnLeftRelease();
					break;
			}
		}
	}

private:
	std::vector<GUIElement*> m_pElements;
};
This is just an example, a very bad example at that since you don't want to store address of temporaries, but it should give you some ideas.
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