Hi,
I have updated the class again.
I added support for:
- Key pressed/released events for mouse and keyboard;
- Gui events (atm only buttons);
I'll updated the Gui events again soon. So that it's returning the ID of the component that caused the event.
Right now it's done manually by just checking in the event class which ID of the button caused the event to fire.
Header file:
Code: Select all
/******************************************************************************
* CIrrEventReceiver
* =================
*
* CIrrEventReceiver has no restrictions. Credit would be appreciated, but not
* required.
******************************************************************************/
#ifndef __CIRREVENTRECEIVER_H__
#define __CIRREVENTRECEIVER_H__
#include <Irrlicht.h>
#include <iostream>
#include <sstream>
using namespace irr;
using namespace core;
using namespace gui;
using namespace io;
using namespace std;
/* I doubt the number of keys will go above 255 ;), but it is there for compatibility issues */
#define NUMBER_OF_KEYS KEY_KEY_CODES_COUNT
#define NUMBER_OF_MOUSE_BUTTONS 3
enum buttonState
{
BS_UP,
BS_DOWN,
BS_PRESSED,
BS_RELEASED
};
enum mouseButtonState
{
MBS_UP,
MBS_DOWN,
MBS_PRESSED,
MBS_RELEASED
};
enum mouseButton
{
MB_LEFT,
MB_MIDDLE,
MB_RIGHT
};
struct mouseInformation
{
s32 x, y, lx, ly, cx, cy;
f32 wheelPos, lwheelPos;
};
// Enumeration for Event Handling State.
enum ProcessEventState
{
STARTED,
ENDED
};
class CIrrEventReceiver : public IEventReceiver
{
public:
CIrrEventReceiver();
~CIrrEventReceiver();
// for buttons GUI:
bool isButtonOnePressed();
bool isButtonTwoPressed();
bool isButtonThreePressed();
void resetButtonOnePressed();
void resetButtonTwoPressed();
void resetButtonThreePressed();
// Keyboard events:
bool isKeyUp(EKEY_CODE key);
bool isKeyDown(EKEY_CODE key);
bool isKeyPressed(EKEY_CODE key);
bool isKeyReleased(EKEY_CODE key);
// Mouse events:
bool isMouseButtonPressed(mouseButton mb);
bool isMouseButtonReleased(mouseButton mb);
bool isMouseButtonDown(mouseButton mb);
bool isMouseButtonUp(mouseButton mb);
bool mouseMoved();
// Processing functions:
void startEventProcess();
void endEventProcess();
int getDeltaMousePosX();
int getDeltaMousePosY();
inline int getMouseX() { return MouseData.x; }
inline int getMouseY() { return MouseData.y; }
inline int getLastMouseX() { return MouseData.lx; }
inline int getLastMouseY() { return MouseData.ly; }
inline s32 getDeltaMouseX()
{
s32 a = MouseData.x - MouseData.lx;
return (s32)(a < 0 ? -a : a);
}
inline s32 getDeltaMouseY()
{
s32 a = MouseData.y - MouseData.ly;
return (s32)(a < 0 ? -a : a);
}
inline u32 getClickedMouseX() { return MouseData.cx; }
inline u32 getClickedMouseY() { return MouseData.cy; }
inline f32 getMouseWheelPosition() { return MouseData.wheelPos; }
inline f32 getLastMouseWheelPosition() { return MouseData.lwheelPos; }
inline f32 getDeltaMouseWheelPosition()
{
f32 a = MouseData.wheelPos - MouseData.lwheelPos;
return (f32)(a < 0 ? -a : a);
}
bool OnEvent(const SEvent& event);
protected:
buttonState Keys[NUMBER_OF_KEYS];
mouseButtonState Mouse[NUMBER_OF_MOUSE_BUTTONS];
mouseInformation MouseData;
ProcessEventState procesState;
bool mouseHasMoved;
// for buttons GUI:
bool btnOnePressed;
bool btnTwoPressed;
bool btnThreePressed;
int deltaMouseX;
int deltaMouseY;
// for converting anything to string (good for debug to console).
template <class T>
inline std::string ToString(const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
};
#endif /* __CIRREVENTRECEIVER_HEADER__ */
The Source file:
Code: Select all
/******************************************************************************
* CIrrEventReceiver
* =================
*
* CIrrEventReceiver has no restrictions. Credit would be appreciated, but not
* required.
******************************************************************************/
#include <stdio.h>
#include "CIrrEventReceiver.h"
CIrrEventReceiver::CIrrEventReceiver()
{
for(u32 i = 0; i < NUMBER_OF_KEYS; i++)
Keys[i] = BS_UP;
Mouse[MB_LEFT] = Mouse[MB_MIDDLE] = Mouse[MB_RIGHT] = MBS_UP;
MouseData.x = MouseData.y = MouseData.lx = MouseData.ly = MouseData.cx = MouseData.cy = 0;
MouseData.wheelPos = MouseData.lwheelPos = 0;
deltaMouseX = deltaMouseY = 0;
mouseHasMoved = false;
btnOnePressed = btnTwoPressed = btnThreePressed = false;
}
CIrrEventReceiver::~CIrrEventReceiver()
{
}
bool CIrrEventReceiver::isKeyUp(EKEY_CODE key)
{
if (Keys[key] == BS_UP)
return true;
return false;
}
bool CIrrEventReceiver::isKeyDown(EKEY_CODE key)
{
if (Keys[key] == BS_DOWN)
return true;
return false;
}
bool CIrrEventReceiver::isKeyPressed(EKEY_CODE key)
{
if(Keys[key] == BS_PRESSED)
return true;
else
return false;
}
bool CIrrEventReceiver::isKeyReleased(EKEY_CODE key)
{
if(Keys[key] == BS_RELEASED)
return true;
else
return false;
}
bool CIrrEventReceiver::isMouseButtonUp(mouseButton mb)
{
if (Mouse[mb] == MBS_UP)
return true;
return false;
}
bool CIrrEventReceiver::isMouseButtonDown(mouseButton mb)
{
if (Mouse[mb] == MBS_DOWN)
return true;
return false;
}
bool CIrrEventReceiver::isMouseButtonPressed(mouseButton mb)
{
if (Mouse[mb] == MBS_PRESSED)
return true;
return false;
}
bool CIrrEventReceiver::isMouseButtonReleased(mouseButton mb)
{
if (Mouse[mb] == MBS_RELEASED)
return true;
return false;
}
int CIrrEventReceiver::getDeltaMousePosX()
{
return deltaMouseX;
}
int CIrrEventReceiver::getDeltaMousePosY()
{
return deltaMouseY;
}
bool CIrrEventReceiver::mouseMoved()
{
if(mouseHasMoved)
{
// set its state back to false.
mouseHasMoved = false;
return true;
}
else
return false;
}
// for graphical user interface (just the buttons for now):
bool CIrrEventReceiver::isButtonOnePressed()
{
return btnOnePressed;
}
bool CIrrEventReceiver::isButtonTwoPressed()
{
return btnTwoPressed;
}
bool CIrrEventReceiver::isButtonThreePressed()
{
return btnThreePressed;
}
void CIrrEventReceiver::resetButtonOnePressed()
{
// set its state back to false.
btnOnePressed = false;
}
void CIrrEventReceiver::resetButtonTwoPressed()
{
// set its state back to false.
btnTwoPressed = false;
}
void CIrrEventReceiver::resetButtonThreePressed()
{
// set its state back to false.
btnThreePressed = false;
}
// end gui button functions
// 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 CIrrEventReceiver::startEventProcess()
{
procesState = STARTED;
// Keyboard Key States
for(int i = 0; i < KEY_KEY_CODES_COUNT; i++)
{
if(Keys[i] == BS_RELEASED)
Keys[i] = BS_UP;
if(Keys[i] == BS_PRESSED)
Keys[i] = BS_DOWN;
}
// Mouse Key States
for(int i = 0; i < NUMBER_OF_MOUSE_BUTTONS; i++)
{
if(Mouse[i] == MBS_RELEASED)
Mouse[i] = MBS_UP;
if(Mouse[i] == MBS_PRESSED)
Mouse[i] = MBS_DOWN;
}
}
// 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 CIrrEventReceiver::endEventProcess()
{
procesState = ENDED;
}
bool CIrrEventReceiver::OnEvent(const SEvent& event)
{
switch (event.EventType)
{
case EET_KEY_INPUT_EVENT:
{
if(procesState == STARTED)
{
if(event.KeyInput.PressedDown)
{
// If key was not down before
if(Keys[event.KeyInput.Key] != BS_DOWN)
Keys[event.KeyInput.Key] = BS_PRESSED; // Set key to Pressed
else
Keys[event.KeyInput.Key] = BS_DOWN;
}
else
{
// if the key is down
if(Keys[event.KeyInput.Key] != BS_UP)
Keys[event.KeyInput.Key] = BS_RELEASED; // Set key to Released
else
Keys[event.KeyInput.Key] = BS_UP;
}
}
break;
}
case EET_MOUSE_INPUT_EVENT:
{
switch(event.MouseInput.Event)
{
case EMIE_MOUSE_MOVED:
{
deltaMouseX = event.MouseInput.X - MouseData.lx;
deltaMouseY = event.MouseInput.Y - MouseData.ly;
MouseData.x = event.MouseInput.X;
MouseData.y = event.MouseInput.Y;
MouseData.lx = MouseData.x;
MouseData.ly = MouseData.y;
mouseHasMoved = true;
break;
}
case EMIE_MOUSE_WHEEL:
{
MouseData.lwheelPos = MouseData.wheelPos;
MouseData.wheelPos += event.MouseInput.Wheel;
break;
}
case EMIE_LMOUSE_PRESSED_DOWN:
{
Mouse[MB_LEFT] = MBS_DOWN;
MouseData.cx = event.MouseInput.X;
MouseData.cy = event.MouseInput.Y;
break;
}
case EMIE_LMOUSE_LEFT_UP:
{
Mouse[MB_LEFT] = MBS_UP;
break;
}
case EMIE_MMOUSE_PRESSED_DOWN:
{
Mouse[MB_MIDDLE] = MBS_DOWN;
MouseData.cx = event.MouseInput.X;
MouseData.cy = event.MouseInput.Y;
break;
}
case EMIE_MMOUSE_LEFT_UP:
{
Mouse[MB_MIDDLE] = MBS_UP;
break;
}
case EMIE_RMOUSE_PRESSED_DOWN:
{
Mouse[MB_RIGHT] = MBS_DOWN;
MouseData.cx = event.MouseInput.X;
MouseData.cy = event.MouseInput.Y;
break;
}
case EMIE_RMOUSE_LEFT_UP:
{
Mouse[MB_RIGHT] = MBS_UP;
break;
}
}
}
case EET_GUI_EVENT:
{
switch(event.GUIEvent.EventType)
{
case EGET_BUTTON_CLICKED:
{
if(event.GUIEvent.Caller->getID() == 103) // you might wanna chance 103 to your own button ID.
btnOnePressed = true;
if(event.GUIEvent.Caller->getID() == 104) // you might wanna chance 104 to your own button ID.
btnTwoPressed = true;
if(event.GUIEvent.Caller->getID() == 105) // you might wanna chance 105 to your own button ID.
btnThreePressed = true;
}
}
}
break;
}
return false;
}
I copied these functions:
Code: Select all
void startEventProcess();
void endEventProcess();
From the Masteventreceive class (hope he doesn't mind).
Put the endEventProcess() function at the beginning of your mainloop.
And put the startEventProcess() function at the end of your mainloop.
This is needed for getting the Pressed/Released events from keyboard and mouse buttons.