Example #1
0
	bool SPKeyboard::UpdateDevice()
	{
		if(!device)
		{
			return false;
		}

		// Save old state for input comparing.
		memcpy(lastKeyState, keyState, sizeof(keyState));

		// If error getting device state, reacquire.
		if(FAILED(device->GetDeviceState(sizeof(keyState), (LPVOID)keyState)))
		{
			if(FAILED(device->Acquire()))
			{
				return false;
			}

			if(FAILED(device->GetDeviceState(sizeof(keyState), (LPVOID)keyState)))
			{
				return false;
			}
		}

#ifdef CEGUI
		for (UINT key = DIK_ESCAPE; key <= DIK_DELETE; key++)
		{
			if (ButtonJustDown(key))
			{
				CEGUI::System::getSingleton().injectKeyDown(key);
			}

			if (ButtonJustUp(key))
			{
				CEGUI::System::getSingleton().injectKeyUp(key);
			}
		}
#endif

		return true;
	}
Example #2
0
	bool SPMouse::UpdateDevice()
	{
		// Get the device state.
		if(!device)
		{
			return false;
		}

		// Save old state for input comparing.
		memcpy(&lastMouseState, &mouseState, sizeof(mouseState));

		// If error getting device state, reacquire.
		if(FAILED(device->GetDeviceState(sizeof(DIMOUSESTATE), &mouseState)))
		{
			if(FAILED(device->Acquire()))
			{
				return false;
			}

			if(FAILED(device->GetDeviceState(sizeof(DIMOUSESTATE), &mouseState)))
			{
				return false;
			}
		}

		// Get absolute mouse position.

		POINT mousePos;
		GetCursorPos(&mousePos);
		ScreenToClient(SPWindow::GetSingleton()->GetHWnd(), 
			&mousePos);

		// Change the mouse position into relative position.

		RECT winRect;
		GetClientRect(SPWindow::GetSingleton()->GetHWnd(), &winRect);

		float xRate = (float)SPConfigManager::GetSingleton()->GetCurrentConfig()->workingWidth / 
			SPConfigManager::GetSingleton()->GetCurrentConfig()->windowWidth;
		float yRate = (float) SPConfigManager::GetSingleton()->GetCurrentConfig()->workingHeight /
			SPConfigManager::GetSingleton()->GetCurrentConfig()->windowHeight ;

		lastXPos = xPos;
		lastYPos = yPos;

		if (winRect.right - winRect.left == 0)
		{
			xPos = 0;
		}
		else
		{
			xPos = mousePos.x * SPWindow::GetSingleton()->GetWidth() 
				/ (winRect.right - winRect.left) * xRate;
		}

		if (winRect.bottom - winRect.top == 0)
		{
			yPos = 0;
		}
		else
		{
			yPos = mousePos.y * SPWindow::GetSingleton()->GetHeight()
				/ (winRect.bottom - winRect.top) * yRate;
		}		

		// Update wheel position.

		wheelPos = mouseState.lZ;

#ifdef CEGUI
		// Handle UI input
		CEGUI::System::getSingleton().injectMousePosition(GetPosition().x, GetPosition().y);

		if (ButtonJustDown(CEGUI::LeftButton))
		{
			CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);
		}

		if (ButtonJustUp(CEGUI::LeftButton))
		{
			CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::LeftButton);
		}

		if (ButtonJustDown(CEGUI::RightButton))
		{
			CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);
		}

		if (ButtonJustUp(CEGUI::RightButton))
		{
			CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::RightButton);
		}

		if (ButtonJustDown(CEGUI::MiddleButton))
		{
			CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);
		}

		if (ButtonJustUp(CEGUI::MiddleButton))
		{
			CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::MiddleButton);
		}

		if (ScrollUp())
		{
			CEGUI::System::getSingleton().injectMouseWheelChange(1);
		}

		if (ScrollDown())
		{
			CEGUI::System::getSingleton().injectMouseWheelChange(-1);
		}
#endif

		return true;
	}