Ejemplo n.º 1
0
InputDevice* DeviceManager::mapKeyboardInput( int btnID, InputManager::InputDriverMode mode,
                                              StateManager::ActivePlayer **player /* out */,
                                              PlayerAction *action /* out */ )
{
    const int keyboard_amount = m_keyboards.size();

    //std::cout << "mapKeyboardInput " << btnID << " to " << keyboard_amount << " keyboards\n";

    for (int n=0; n<keyboard_amount; n++)
    {
        KeyboardDevice *keyboard = m_keyboards.get(n);

        if (keyboard->processAndMapInput(btnID, mode, action))
        {
            //std::cout << "   binding found in keyboard #"  << (n+1) << "; action is " << KartActionStrings[*action] << "\n";
            if (m_single_player != NULL)
            {
                //printf("Single player\n");
                *player = m_single_player;
            }
            else if (m_assign_mode == NO_ASSIGN) // Don't set the player in NO_ASSIGN mode
            {
                *player = NULL;
            }
            else
            {
                *player = keyboard->m_player;
            }
            return keyboard;
        }
    }

    return NULL; // no appropriate binding found
}   // mapKeyboardInput
Ejemplo n.º 2
0
/** Helper method, only used internally. Takes care of analyzing keyboard input.
 *  \param[in]   button_id  Id of the key pressed.
 *  \param[in]   mode       Used to determine whether to determine menu actions
 *                          or game actions
 *  \param[out]  player     Which player this input belongs to (only set in 
 *                          ASSIGN mode).
 *  \param[out]  action     Which action is related to this input trigger.
 *  \return                 The device to which this input belongs
 */
InputDevice* DeviceManager::mapKeyboardInput(int button_id,
                                             InputManager::InputDriverMode mode,
                                             StateManager::ActivePlayer **player,
                                             PlayerAction *action /* out */)
{
    const int keyboard_amount = m_keyboards.size();

    for (int n=0; n<keyboard_amount; n++)
    {
        KeyboardDevice *keyboard = m_keyboards.get(n);

        if (keyboard->processAndMapInput(Input::IT_KEYBOARD, button_id, mode, action))
        {
            if (m_single_player != NULL)
            {
                *player = m_single_player;
            }
            else if (m_assign_mode == NO_ASSIGN) // Don't set the player in NO_ASSIGN mode
            {
                *player = NULL;
            }
            else
            {
                *player = keyboard->getPlayer();
            }
            return keyboard;
        }
    }

    return NULL; // no appropriate binding found
}   // mapKeyboardInput
Ejemplo n.º 3
0
	void Update( ) {
		mousedevice.Update( );
		keyboarddevice.Update( );
		if ( keyboarddevice.Pressed( Key::Enter ) ) {
			State& currstate = states.Peek( );
			if ( &currstate != &pausestate ) {
				states.Push( pausestate );
			}
			else {
				states.Pop( );
			}
		}
		states.Update( );
	}
Ejemplo n.º 4
0
/**
 *	This method processes all device events since it was last called. It asks
 *	the input handler to handle each of these events.
 */
bool InputDevices::privateProcessEvents( InputHandler & handler )
{
	BW_GUARD;
	HRESULT hr;

	if (!focus_) return true;

	// Update the Joystick state when this is called.
	this->joystick_.update();

	bool jbLostData = false;
	this->joystick().processEvents( handler, isKeyDown_, &jbLostData );
	if ( jbLostData )
		lostData_ |= JOY_DATA_LOST;

	static DogWatch watchHandleKey( "Keyboard" );

	{ // DogWatch scope
	ScopedDogWatch watcher( watchHandleKey );

	if (pKeyboard_ != NULL)
	{
		DIDEVICEOBJECTDATA didod[ KEYBOARD_BUFFER_SIZE ];
		DWORD dwElements = 0;

		dwElements = KEYBOARD_BUFFER_SIZE;
		if (keyboardAcquired_)
		{
			hr = pKeyboard_->GetDeviceData(
				sizeof(DIDEVICEOBJECTDATA), didod, &dwElements, 0 );
		}
		else
		{
			hr = DIERR_NOTACQUIRED;
			dwElements = 0;
		}

		switch (hr)
		{
		case DI_OK:
			break;

		case DI_BUFFEROVERFLOW:
			// We got an error or we got DI_BUFFEROVERFLOW.
			//
			// Either way, it means that continuous contact with the device has been
			// lost, either due to an external interruption, or because the buffer
			// overflowed and some events were lost.
			//
			DEBUG_MSG( "InputDevices::privateProcessEvents: "
				"keyboard buffer overflow\n" );
			lostData_ |= KEY_DATA_LOST;
			break;

		case DIERR_INPUTLOST:
		case DIERR_NOTACQUIRED:
			// We got an error or we got DI_BUFFEROVERFLOW.
			//
			// Either way, it means that continuous contact with the device has been
			// lost, either due to an external interruption, or because the buffer
			// overflowed and some events were lost.
			//
			keyboardAcquired_ = false;
			/*
			DEBUG_MSG( "InputDevices::privateProcessEvents: "
				"keyboard input not acquired, acquiring\n" );
			*/
			hr = pKeyboard_->Acquire();
			if (FAILED(hr)) {
				/*
				DEBUG_MSG( "InputDevices::privateProcessEvents: "
					"keyboard acquire failed\n" );
				*/
				return false;
			}
			keyboardAcquired_ = true;
			dwElements = KEYBOARD_BUFFER_SIZE;
			hr = pKeyboard_->GetDeviceData( sizeof(DIDEVICEOBJECTDATA),
											didod, &dwElements, 0 );
			lostData_ |= KEY_DATA_LOST;
			break;

		default:
			DEBUG_MSG( "InputDevices::privateProcessEvents: "
				"unhandled keyboard error\n" );
			return false;
		}

		// Handle all those key events then
		for (DWORD i = 0; i < dwElements; i++)
		{
			isKeyDown_[ static_cast<KeyEvent::Key>(didod[ i ].dwOfs) ] = (didod[ i ].dwData & 0x80) ?
					true : false;

			KeyEvent event(
				(didod[ i ].dwData & 0x80) ?
					MFEvent::KEY_DOWN :
					MFEvent::KEY_UP,
				static_cast<KeyEvent::Key>(didod[ i ].dwOfs),
				this->modifiers() );

			handler.handleKeyEvent( event );
		}
	}

	} // DogWatch scope

	// Now handle the mouse events.
	// TODO:PM We should probably do this differently. We should really handle
	// the event in the order that they were generated. That is, get both
	// buffers and then continually handle the earliest until all handled.

	if (pMouse_)
	{
		DIDEVICEOBJECTDATA didod[ MOUSE_BUFFER_SIZE ];
		DWORD dwElements = 0;

		dwElements = MOUSE_BUFFER_SIZE;
		if (mouseAcquired_)
		{
			hr = pMouse_->GetDeviceData(
				sizeof(DIDEVICEOBJECTDATA), didod, &dwElements, 0 );
		}
		else
		{
			hr = DIERR_NOTACQUIRED;
			dwElements = 0;
		}

		switch (hr)
		{
		case DI_OK:
			break;

		case DI_BUFFEROVERFLOW:
			/*
			DEBUG_MSG( "InputDevices::privateProcessEvents: "
				"mouse buffer overflow\n" );
			*/
			lostData_ |= MOUSE_DATA_LOST;
			break;

		case DIERR_INPUTLOST:
		case DIERR_NOTACQUIRED:
			mouseAcquired_ = false;
			/*
			DEBUG_MSG( "InputDevices::privateProcessEvents: "
				"mouse input not acquired, acquiring\n" );
			*/
			hr = pMouse_->Acquire();
			if (FAILED(hr))
			{
				/*
				DEBUG_MSG( "InputDevices::privateProcessEvents: "
					"mouse acquire failed\n" );
				*/
				return false;
			}
			mouseAcquired_ = true;
			dwElements = MOUSE_BUFFER_SIZE;
			hr = pMouse_->GetDeviceData( sizeof(DIDEVICEOBJECTDATA),
											didod, &dwElements, 0 );
			lostData_ |= MOUSE_DATA_LOST;
			break;

		default:
			DEBUG_MSG( "InputDevices::privateProcessEvents: "
				"unhandled mouse error\n" );
			return false;
		}

		// With the keyboard event, we group the mouse movements together and
		// only send at the end or when a button is pressed.

		// Handle all the mouse events
		long dx = 0;
		long dy = 0;
		long dz = 0;

		static DogWatch watchMouse( "Mouse" );

		watchMouse.start();
		for (DWORD i = 0; i < dwElements; i++)
		{
			switch ( didod[i].dwOfs )
			{
			case DIMOFS_BUTTON0:
			case DIMOFS_BUTTON1:
			case DIMOFS_BUTTON2:
			case DIMOFS_BUTTON3:
			case DIMOFS_BUTTON4:
			case DIMOFS_BUTTON5:
			case DIMOFS_BUTTON6:
			case DIMOFS_BUTTON7:
				{
					if ( dx != 0 || dy != 0 || dz != 0 )
					{
						MouseEvent mouseEvent( dx, dy, dz );
						handler.handleMouseEvent( mouseEvent );
						dx = dy = dz = 0;
					}

					KeyEvent keyEvent(
						(didod[ i ].dwData & 0x80) ?
							MFEvent::KEY_DOWN :
							MFEvent::KEY_UP,
						static_cast< KeyEvent::Key >(
							KeyEvent::KEY_MOUSE0 +
								(didod[ i ].dwOfs - DIMOFS_BUTTON0)),
						this->modifiers() );

					isKeyDown_[ keyEvent.key() ] = keyEvent.isKeyDown();

					handler.handleKeyEvent( keyEvent );
				}
				break;

			case DIMOFS_X:
				dx += didod[ i ].dwData;
				break;

			case DIMOFS_Y:
				dy += didod[ i ].dwData;
				break;

			case DIMOFS_Z:
				dz += didod[ i ].dwData;
				break;
			}
		}
		watchMouse.stop();


		if ( dx != 0 || dy != 0 || dz != 0 )
		{
			MouseEvent mouseEvent( dx, dy, dz );
			handler.handleMouseEvent( mouseEvent );
			dx = dy = dz = 0;
		}
	}

	//handle lost data
	if (lostData_ != NO_DATA_LOST)
		handleLostData( handler, lostData_ );

	for (uint i = 0; i < gVirtualKeyboards.size(); i++)
	{
		KeyboardDevice * pKB = gVirtualKeyboards[i];
		pKB->update();

		KeyEvent event;
		while (pKB->next( event ))
		{
			isKeyDown_[ event.key() ] = event.isKeyDown();
			handler.handleKeyEvent( event );
		}
	}

	return true;
}