hide mouse button mod for win32 and CCameraFPSSceneNode

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
ecsos
Posts: 30
Joined: Mon Feb 04, 2008 8:02 am

hide mouse button mod for win32 and CCameraFPSSceneNode

Post by ecsos »

This patch will have you hold down the right mouse button to move the camera. It's very easy to modify, but you'll have to recompile the irrlicht engine.

this patch involves all the WM_ BUTTONDOWN events like WM_LBUTTONDOWN. it will let you set the cursor to hidden after a click - as is not possible otherwise ( because irrlicht uses SetCapture() when you click down a button, and when the mouse is captured, the window does not receive WM_SETCURSOR events. )

Code: Select all

Index: CIrrDeviceWin32.cpp
===================================================================
--- CIrrDeviceWin32.cpp	(revision 1313)
+++ CIrrDeviceWin32.cpp	(working copy)
@@ -140,7 +140,11 @@
 		event.MouseInput.Y = (short)HIWORD(lParam);
 		dev = getDeviceFromHWnd(hWnd);
 		if (dev)
+		{
 			dev->postEventFromUser(event);
+			if (!dev->getCursorControl()->isVisible())
+				SetCursor(NULL);
+		}
 		return 0;
 
 	case WM_LBUTTONUP:
@@ -168,7 +172,11 @@
 		event.MouseInput.Y = (short)HIWORD(lParam);
 		dev = getDeviceFromHWnd(hWnd);
 		if (dev)
+		{
 			dev->postEventFromUser(event);
+			if (!dev->getCursorControl()->isVisible())
+				SetCursor(NULL);
+		}
 		return 0;
 
 	case WM_RBUTTONUP:
@@ -196,7 +204,11 @@
 		event.MouseInput.Y = (short)HIWORD(lParam);
 		dev = getDeviceFromHWnd(hWnd);
 		if (dev)
+		{
 			dev->postEventFromUser(event);
+			if (!dev->getCursorControl()->isVisible())
+				SetCursor(NULL);
+		}
 		return 0;
 
 	case WM_MBUTTONUP:

this code is for the cameraFPS to demonstrate the difference.
basically it just adds a check for the right mouse button being down, and uses setVisible(false) to hide the cursor like normal...

Code: Select all

Index: CCameraFPSSceneNode.cpp
===================================================================
--- CCameraFPSSceneNode.cpp	(revision 1312)
+++ CCameraFPSSceneNode.cpp	(working copy)
@@ -87,8 +87,29 @@
 //! for changing their position, look at target or whatever. 
 bool CCameraFPSSceneNode::OnEvent(const SEvent& event)
 {
+	if (event.EventType == EET_MOUSE_INPUT_EVENT)
+	{
+		if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
+		{
+			CursorControl->setVisible(false);
+			CursorControl->setPosition(0.5f, 0.5f);
+			CenterCursor = CursorControl->getRelativePosition();
+			if ( InputReceiverEnabled ) return true;
+		}
+
+		if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
+		{
+
+			CursorControl->setVisible(true);
+		}
+		if (event.MouseInput.Event == EMIE_MOUSE_WHEEL)
+		{
+
+		}
+	}
 	if (event.EventType == EET_KEY_INPUT_EVENT)
 	{
+
 		const u32 cnt = KeyMap.size();
 		for (u32 i=0; i<cnt; ++i)
 			if (KeyMap[i].keycode == event.KeyInput.Key)
@@ -131,7 +152,7 @@
 
 	if (firstUpdate)
 	{
-		if (CursorControl && camIsMe)
+		if (CursorControl && camIsMe && !CursorControl->isVisible())
 		{
 			CursorControl->setPosition(0.5f, 0.5f);
 			CenterCursor = CursorControl->getRelativePosition();
@@ -160,7 +181,7 @@
 		Target.set(0,0,1);
 
 
-		if (CursorControl && InputReceiverEnabled && camIsMe )
+		if (CursorControl && InputReceiverEnabled && camIsMe && !CursorControl->isVisible())
 		{
 			core::position2d<f32> cursorpos = CursorControl->getRelativePosition();
 
finally, here is the code I use to test it, condensed into one file for ease of use on these forums..

Code: Select all

#include <irrlicht.h>

using namespace irr;
#ifndef NUMBER_OF_MOUSE_BUTTONS
#define NUMBER_OF_MOUSE_BUTTONS 4
#endif

class MyEventReceiver : public irr::IEventReceiver
{
public:
	MyEventReceiver(void);
	virtual ~MyEventReceiver(void);

	enum buttonState
	{
		BS_UP,
		BS_DOWN,
		BS_PRESSED,
		BS_RELEASED
	};

	enum mouseButton
	{
		MBLEFT,
		MBMIDDLE,
		MBRIGHT,
		MBFOUR
	};

		// 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 isMouseButtonUp(mouseButton mb);
	bool isMouseButtonDown(mouseButton mb);
    bool isMouseButtonPressed(mouseButton mb);
    bool isMouseButtonReleased(mouseButton mb);

private:

	buttonState Keys[irr::KEY_KEY_CODES_COUNT];
    buttonState Mouse[NUMBER_OF_MOUSE_BUTTONS]; // number of mouse buttons

	bool OnEvent(const irr::SEvent& event);
};


MyEventReceiver::MyEventReceiver(void)
{
	for(u32 i = 0; i < irr::KEY_KEY_CODES_COUNT; i++)
     Keys[i] = BS_UP;

	// Mouse Key States
	for(u32 i = 0; i < NUMBER_OF_MOUSE_BUTTONS; i++)
		 Mouse[i] = BS_UP;
}

bool MyEventReceiver::OnEvent(const irr::SEvent& event)  {
	switch (event.EventType) 
	{
		case EET_KEY_INPUT_EVENT:
		{
			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;

				 break;
			}
			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;
			}
			return false;
		} // EET_KEY_INPUT_EVENT
		break;
		case EET_MOUSE_INPUT_EVENT:
		{
			switch(event.MouseInput.Event)
			{
				case EMIE_LMOUSE_PRESSED_DOWN:
				{
					if(Mouse[MBLEFT] == BS_UP || Mouse[MBLEFT] == BS_RELEASED)
					Mouse[MBLEFT] = BS_PRESSED;

					else
					Mouse[MBLEFT] = BS_DOWN;

				}
				break;

				// Left Mouse Button Rleased
				case EMIE_LMOUSE_LEFT_UP:
				{
					if(Mouse[MBLEFT] != BS_UP)
					Mouse[MBLEFT] = BS_RELEASED;


				}
				break;

				// Middle Mouse Button Pressed
				case EMIE_MMOUSE_PRESSED_DOWN:
				{
					if(Mouse[MBMIDDLE] == BS_UP || Mouse[MBMIDDLE] == BS_RELEASED)
					Mouse[MBMIDDLE] = BS_PRESSED;

					else
					Mouse[MBMIDDLE] = BS_DOWN;


				}
				break;

				// Middle Mouse Button Rleased
				case EMIE_MMOUSE_LEFT_UP:
				{
					if (Mouse[MBMIDDLE] != BS_UP)
					Mouse[MBMIDDLE] = BS_RELEASED;


				}
				break;

				// Right Mouse Button Pressed
				case EMIE_RMOUSE_PRESSED_DOWN:
				{
					if (Mouse[MBRIGHT] == BS_UP || Mouse[MBRIGHT] == BS_RELEASED)
					Mouse[MBRIGHT] = BS_PRESSED;

					else
					Mouse[MBRIGHT] = BS_DOWN;


				}
				break;
				
				// Right Mouse Button Rleased
				case EMIE_RMOUSE_LEFT_UP:
				{
					if(Mouse[MBRIGHT] != BS_UP)
					Mouse[MBRIGHT] = BS_RELEASED;


				}
				break;
				default:
				break;
			} // switch(event.MouseInput.Event)
		} // EET_MOUSE_INPUT_EVENT
		default:
			break;
	} // switch (event.EventType)
   return false;
}

bool MyEventReceiver::isKeyUp(EKEY_CODE key)
{
   if (Keys[key] == BS_UP)
      return true;
   return false;
}

bool MyEventReceiver::isKeyDown(EKEY_CODE key)
{
   if (Keys[key] == BS_DOWN)
      return true;
   return false;
}

bool MyEventReceiver::isKeyPressed(EKEY_CODE key)
{
  if(Keys[key] == BS_PRESSED)
    return true;
  return false;
}

bool MyEventReceiver::isKeyReleased(EKEY_CODE key)
{
  if(Keys[key] == BS_RELEASED)
    return true;
  return false;
}

bool MyEventReceiver::isMouseButtonUp(mouseButton mb)
{
   if (Mouse[mb] == BS_UP)
      return true;
   return false;
}

bool MyEventReceiver::isMouseButtonDown(mouseButton mb)
{
   if (Mouse[mb] == BS_DOWN)
      return true;
   return false;
}

bool MyEventReceiver::isMouseButtonPressed(mouseButton mb)
{
   if (Mouse[mb] == BS_PRESSED)
      return true;
   return false;
}

bool MyEventReceiver::isMouseButtonReleased(mouseButton mb)
{
   if (Mouse[mb] == BS_RELEASED)
      return true;
   return false;
}

MyEventReceiver::~MyEventReceiver(void)
{
}


class CGame : public virtual IReferenceCounted
{
private:
	MyEventReceiver recv;
	IrrlichtDevice* device;
	video::IVideoDriver* driver;
	scene::ISceneManager* smgr;
	scene::ICameraSceneNode* herocam;
	gui::IGUIEnvironment* gui;
	gui::IGUIFont* font;

protected:
	void run(void);
public:
	CGame(void);
	void createGame(void);
	virtual ~CGame(void);
};

CGame::CGame(void) : device(0),driver(0),smgr(0),herocam(0),gui(0),font(0)
{
}

CGame::~CGame(void)
{
	if (device)	device->drop();
}


void CGame::createGame(void)
{
	//device = createDevice(video::EDT_DIRECT3D9, core::dimension2d<s32>(1024,768),32,false,false,false, &recv);
	device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(1024,768),32,false,false,false, &recv);


	if (device == 0)
		exit(1);

	driver = device->getVideoDriver();
	if (driver == 0)
		exit(1);
	smgr = device->getSceneManager();
	if (smgr == 0)
		exit(1);

	//herocam = smgr->addCameraSceneNode(0, irr::core::vector3df(100.0f,0.0f,0.0f) , irr::core::vector3df(0.0f,0.0f,0.0f), 234);
	herocam = smgr->addCameraSceneNodeFPS(0, 100, 100);

	smgr->setActiveCamera(herocam);

	herocam->setPosition(core::vector3df(-240, 100, 0));
	herocam->setFOV(core::PI / 2.5f);

	gui = smgr->getGUIEnvironment();
	if (gui == 0)
		exit(1);

	font = device->getGUIEnvironment()->getBuiltInFont();

	smgr->setAmbientLight(video::SColorf(video::SColor(255, 0, 0, 0))); // black

	scene::ILightSceneNode* light = smgr->addLightSceneNode(0,core::vector3df(-150, 150, 0),video::SColor(255, 255, 0,  255),10); // magenta

	video::SLight ldata = light->getLightData();

//	std::cout << ldata.Type << std::endl;
	ldata.Type = video::ELT_DIRECTIONAL;
	//ldata.Type = video::ELT_POINT;

	ldata.Direction = core::vector3df(90,0,0)*core::DEGTORAD;

	light->setLightData(ldata);

	light->setDebugDataVisible(scene::EDS_FULL);
		scene::IMeshSceneNode* bldgnode = smgr->addCubeSceneNode(100);
		bldgnode->setMaterialFlag(video::EMF_LIGHTING, true);
		bldgnode->setPosition(core::vector3df(-150, 0, 0));

		
		herocam->setTarget(bldgnode->getPosition());

		bldgnode->setDebugDataVisible(scene::EDS_FULL);

		device->getCursorControl()->setVisible(true);

	this->run();
}

void CGame::run(void)
{
	herocam->setInputReceiverEnabled(true);
	while(device->run() && recv.isKeyPressed(irr::KEY_ESCAPE) == false)
	{
		//if (device->isWindowActive())
	//	{
		driver->beginScene(true, true, irr::video::SColor(255,51,51,51));

		smgr->drawAll();
		gui->drawAll();

		core::vector3df pos = herocam->getAbsolutePosition();
		
		
		core::stringw tmp = L"Camera: X";
		tmp += pos.X;
		tmp += L" Y";
		tmp += pos.Y;
		tmp += L" Z";
		tmp += pos.Z;
		tmp += L" CursorControl: ";
		tmp += device->getCursorControl()->isVisible();
		tmp += L"   FPS: ";
		tmp += driver->getFPS();
		
		if (font)
			font->draw(tmp.c_str(),
				core::rect<s32>(130,10,300,50),
				video::SColor(255,255,255,255));
		

		driver->endScene();
	////	} else {
	//		device->sleep(10, true);
	//	}
	}
}


int main(int argc, char** argv)
{
	CGame* game = new CGame();

	game->createGame();

	game->drop();

	return 0;
}
oh and thanks to those with the eventreceiver code (i think i used some from MAST ?).
Post Reply