Page 1 of 1

Event Reciever not picking up GUI events

Posted: Sat Nov 29, 2008 3:27 am
by Catprog
I am trying to change the event receiver from here
http://www.irrlicht3d.org/wiki/index.ph ... ntReceiver
so that I can use it for GUI events as well

Code: Select all

int main()
{
     MastEventReceiver* receiver;

	 receiver = new MastEventReceiver();

	receiver->init();


	dimension2d<s32> sizeOfScreen = dimension2d<s32>(640, 480);

	 IrrlichtDevice *device =
		createDevice( video::EDT_OPENGL,sizeOfScreen , 16,false, false, false, receiver);

	if (!device)
		return 1;

	device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");

	IVideoDriver* driver = device->getVideoDriver();

	
	device->getGUIEnvironment()->addButton(rect<s32>(0,0,0+60,0+30), 0, 101, L"Quit", L"Exits Program");


	//StateManager* sM = new StateManager(device, receiver); 

	while(device->run())// && !sM->ExitProgram())
    {

		receiver->endEventProcess();

		
        driver->beginScene(true, true, SColor(255,100,101,140));

		//sM->updateFrame(); 
		//sM->drawFrame();

		device->getGUIEnvironment()->drawAll(); 
		
        driver->endScene();

		//sM->reciveMessages();

		receiver->startEventProcess();

    }


    device->drop();

    return 0;

}

Code: Select all


	virtual bool OnEvent(const SEvent & event)
	{

		bool eventprocessed = false;
		
		guiEvent.id = -100;

		printf("%i \n",event.EventType);

		//////////////////////////////
		// Keyboard Input Event
		//////////////////////////////
		if (event.EventType == EET_KEY_INPUT_EVENT)	{
			if (processState == STARTED)
			{
				// if key is Pressed Down
				if (event.KeyInput.PressedDown == true)
				{
					// If key was not down before
					if (keyState[event.KeyInput.Key] != DOWN)
					{
						keyState[event.KeyInput.Key] = PRESSED; // Set to Pressed
					}
					else
					{
						// if key was down before
						keyState[event.KeyInput.Key] = DOWN; // Set to Down
					}
				}
				else
				{

						// if the key is down
						if (keyState[event.KeyInput.Key] != UP)
						{
							keyState[event.KeyInput.Key] = RELEASED; // Set to Released
						}
				}
			}


			eventprocessed = true;
		}else	if (event.EventType == EET_GUI_EVENT){

		//////////////////////////////
		// GUI EVENT
		//////////////////////////////
			s32 id = event.GUIEvent.Caller->getID();

			printf("ID %i \n",id );

			switch(event.GUIEvent.EventType){
				case EGET_BUTTON_CLICKED:
					guiEvent.id = id;
					guiEvent.evnt_Type = event.GUIEvent.EventType;
					eventprocessed = true;
					break;
				default:
					eventprocessed = false;
			}
		}
I have added the EET_GUI_EVENT branch but all that is printed on the console is 1 (THE EET_MOUSE_INPUT_EVENT).

Why is is not creating a GUI event when I click on the quit button.

Posted: Sat Nov 29, 2008 7:22 am
by Murcho
Well for starters, and this is a guess because you haven't added the code for your MastEventReceiver, you are stopping input at the start of your main loop, and then re-starting at the end, which would leave a very small amount of time to pickup the events.

Also, the OnEvent function you provided doesn't look to be part of the MastEventReceiver class you create at the top of main. The event receiver class needs to inherit from the IEventReceiver class.

Have a look at the GUI tutorial, it shows how to set up GUI event handling properly. Also check this link for a state machine setup that links in with the input receiver.

Posted: Sat Nov 29, 2008 9:21 am
by Catprog
all of the code for the event reciever

EDIT:

I found if I return false just after the mouse branch. the gui events are picked up.

Code: Select all

/// ==================================================================================================
/// MastEventReceiver code is © (Copyright) Robert E. Demarest, AKA Mastiff or Mastiff Odit
/// This file may be used in any non-commercial or commercial project as long as the following conditions are met:
/// You may not claim this code as being your own.
/// You may not use this code for any harmful, malicious or otherwise damaging programs.
///
/// This is version 1.2a of the class.
/// This class is designed for use with the Irrlicht Engine, it was written for version 1.3 of the engine.
/// ==================================================================================================

//Taken from http://www.irrlicht3d.org/wiki/index.php?n=Main.MastEventReceiver 

//////////////////////////////////////////////////////////////////////////////////////////////////////
//
// To use this Class just add  #include "MastEventReceiver.cpp"  to the end of your includes list. (or add the class in-line into your program)
// Then create an instance of it like so: MastEventReceiver eventReceiver;
// Then call the initialization fucntion like so: eventReceiver.init();
// Then inside your Main Game Loop place "eventReceiver.endEventProcess();" in the beginning of your game loop, before anything -
// that would require input, then put "eventReceiver.startEventProcess();" at the very end of your Main Game Loop.
// yeah I know it's confusing, but it makes alot more sense in the internals of the class.
//
//////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once

#ifndef _EVENTRECIEVER_H
#define _EVENTRECIEVER_H

// Change this to the path where your Irrlicht Header Files are.
#include <irrlicht.h>

using namespace std;
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

/// ==============================
/// MastEventReceiver
/// ==============================
class MastEventReceiver : public IEventReceiver
{
	public:
		
	struct GUI_EVENT_STRUCT{
		int id;
		gui::EGUI_EVENT_TYPE evnt_Type;
	};


	protected:
	// Enumeration for UP, DOWN, PRESSED and RELEASED key states. Also used for mouse button states.
	enum keyStatesENUM {UP, DOWN, PRESSED, RELEASED};

	// Enumeration for Event Handling State.
	enum processStateENUM {STARTED, ENDED};

	// Mouse button states.
	keyStatesENUM mouseButtonState[2]; //Left(0), Middle(1) and Right(2) Buttons.

	// Keyboard key states.
	keyStatesENUM keyState[KEY_KEY_CODES_COUNT];

	GUI_EVENT_STRUCT guiEvent;

	// Mouse X/Y coordinates and Wheel data.
	struct mouseData
	{
	int X;
	int Y;
	float wheel; //wheel is how far the wheel has moved
	};
	struct mouseData mouse;

	processStateENUM processState; // STARTED = handling events, ENDED = not handling events

	virtual bool OnEvent(const SEvent & event)
	{

		guiEvent.id = -1;

		if (event.EventType == EET_GUI_EVENT){
			
			
			s32 id = event.GUIEvent.Caller->getID();

			switch(event.GUIEvent.EventType){
				case EGET_BUTTON_CLICKED:
					guiEvent.id = id;

					printf("%i \n",id);
					
					return false;
					break;
				default:
					
					return false;
					break;
			}


		}else{
			
					

			//////////////////////////////
			// Keyboard Input Event
			//////////////////////////////
			if (event.EventType == EET_KEY_INPUT_EVENT)	{
				if (processState == STARTED)
				{
					// if key is Pressed Down
					if (event.KeyInput.PressedDown == true)
					{
						// If key was not down before
						if (keyState[event.KeyInput.Key] != DOWN)
						{
							keyState[event.KeyInput.Key] = PRESSED; // Set to Pressed
						}
						else
						{
							// if key was down before
							keyState[event.KeyInput.Key] = DOWN; // Set to Down
						}
					}
					else
					{

							// if the key is down
							if (keyState[event.KeyInput.Key] != UP)
							{
								keyState[event.KeyInput.Key] = RELEASED; // Set to Released
							}
					}
				}


			}else	if (event.EventType == EET_MOUSE_INPUT_EVENT)	{

				
			return false;

			//////////////////////////////
			// Mouse Input Event
			//////////////////////////////

				if (processState == STARTED)
				{
					//Mouse changed position
					if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
					{
						mouse.Y = event.MouseInput.Y;
						mouse.X = event.MouseInput.X;
					}

					//Wheel moved.
					if (event.MouseInput.Event == EMIE_MOUSE_WHEEL)
					{
						mouse.wheel += event.MouseInput.Wheel;
					}

					//Left Mouse Button Pressed
					if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
					{
						//
						if (mouseButtonState[0] == UP || mouseButtonState[0] == RELEASED)
						{
							mouseButtonState[0] = PRESSED;
						}
						else
						{
							mouseButtonState[0] = DOWN;
						}
					}

					//Left Mouse Button Rleased
					if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
					{
						//
						if (mouseButtonState[0] != UP)
						{
							mouseButtonState[0] = RELEASED;
						}
					}

					//Middle Mouse Button Pressed
					if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
					{
						//
						if (mouseButtonState[1] == UP || mouseButtonState[1] == RELEASED)
						{
							mouseButtonState[1] = PRESSED;
						}
						else
						{
							mouseButtonState[1] = DOWN;
						}
					}

					//Middle Mouse Button Rleased
					if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
					{
						//
						if (mouseButtonState[1] != UP)
						{
							mouseButtonState[1] = RELEASED;
						}
					}

					//Right Mouse Button Pressed
					if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
					{
						//
						if (mouseButtonState[2] == UP || mouseButtonState[2] == RELEASED)
						{
							mouseButtonState[2] = PRESSED;
						}
						else
						{
							mouseButtonState[2] = DOWN;
						}
					}

					//Right Mouse Button Rleased
					if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
					{
						//
						if (mouseButtonState[2] != UP)
						{
							mouseButtonState[2] = RELEASED;
						}
					}
				}


			}


			return false;
		}
	}


	//////////////////////
	// Public functions
	//////////////////////
	public:

	float mouseWheel()
	{
		return mouse.wheel;
	}

	int mouseX()
	{
		return mouse.X;
	}

	int mouseY()
	{
		return mouse.Y;
	}

	bool leftMouseReleased()
	{
		if (mouseButtonState[0] == RELEASED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool leftMouseUp()
	{
		if (mouseButtonState[0] == RELEASED || mouseButtonState[0] == UP)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool leftMousePressed()
	{
		if (mouseButtonState[0] == PRESSED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool leftMouseDown()
	{
		if (mouseButtonState[0] == PRESSED || mouseButtonState[0] == DOWN)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool middleMouseReleased()
	{
		if (mouseButtonState[1] == RELEASED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool middleMouseUp()
	{
		if (mouseButtonState[1] == RELEASED || mouseButtonState[1] == UP)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool middleMousePressed()
	{
		if (mouseButtonState[1] == PRESSED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool middleMouseDown()
	{
		if (mouseButtonState[1] == PRESSED || mouseButtonState[1] == DOWN)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool rightMouseReleased()
	{
		if (mouseButtonState[2] == RELEASED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool rightMouseUp()
	{
		if (mouseButtonState[2] == RELEASED || mouseButtonState[2] == UP)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool rightMousePressed()
	{
		if (mouseButtonState[2] == PRESSED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool rightMouseDown()
	{
		if (mouseButtonState[2] == PRESSED || mouseButtonState[2] == DOWN)
		{
			return true;
		}
		else
		{
			return false;
		}
	}//

	bool keyPressed(char keycode)
	{
		if (keyState[keycode] == PRESSED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool keyDown(char keycode)
	{
		if (keyState[keycode] == DOWN || keyState[keycode] == PRESSED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool keyUp(char keycode)
	{
		if (keyState[keycode] == UP || keyState[keycode] == RELEASED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool keyReleased(char keycode)
	{
		if (keyState[keycode] == RELEASED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	GUI_EVENT_STRUCT getGuiEvent(){
		return guiEvent;
	}


	// This is used so that the Key States will not be changed during execution of your Main game loop.
	// Place this at the very START of your Main Loop
	void endEventProcess()
	{
		processState = ENDED;
		
	}

	// This is used so that the Key States will not be changed during execution of your Main game loop.
	// Place this function at the END of your Main Loop.
	void startEventProcess()
	{

		processState = STARTED;
		//Keyboard Key States
		for (int i = 0; i < KEY_KEY_CODES_COUNT; i++)
		{
			if (keyState[i] == RELEASED)
			{
				keyState[i] = UP;
			}

			if (keyState[i] == PRESSED)
			{
				keyState[i] = DOWN;
			}
		}
		//Mouse Button States
		for (int i = 0; i <= 2; i++)
		{
			if (mouseButtonState[i] == RELEASED)
			{
				mouseButtonState[i] = UP;
			}

			if (mouseButtonState[i] == PRESSED)
			{
				mouseButtonState[i] = DOWN;
			}
		}
		//Mouse Wheel state
		mouse.wheel = 0.0f;

	}

	void init()
	{
		//KeyBoard States.
		for (int i = 0; i <= KEY_KEY_CODES_COUNT; i++)
		{
			keyState[i] = UP;
		}
		//Mouse states
		for (int i = 0; i <= 2; i++)
		{
			mouseButtonState[i] = UP;
		}
		//Mouse X/Y coordenates.
		mouse.X = 0;
		mouse.Y = 0;
		mouse.wheel = 0.0f;

		guiEvent.id = -100;
	}


};
/// ==========================================
/// END OF MastEventReceiver
/// ==========================================

#endif

Posted: Sun Nov 30, 2008 3:31 am
by Murcho
You should be returning true after you handle an event and only returning false if you don't handle the event. This tells the Engine that it can discard the event.

Posted: Sun Nov 30, 2008 8:27 am
by Ion Dune
Returning true on a keyboard event, for example, will prevent that event from being sent to your GUI elements. Only return true on an event if you have handled it entirely (and don't want it to be sent to anything else).