#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2013 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_INPUT_DEVICES_XINPUTGAMEPAD_H #define NUCLEX_INPUT_DEVICES_XINPUTGAMEPAD_H #include "Nuclex/Input/Devices/GamePad.h" #include "../../ConcurrentQueue.h" #include #include namespace Nuclex { namespace Input { namespace Devices { // ------------------------------------------------------------------------------------------- // /// Implements a game pad in top of the XINPUT API class XInputGamePad : public GamePad { /// Initializes a new XInput game pad for the specified player /// Index of the player whose game pad will be queried public: XInputGamePad(std::size_t playerIndex); /// Frees all resources owned by the instance public: virtual ~XInputGamePad(); /// Whether the input device is currently attached /// True if the input device is attached, otherwise false public: bool IsAttached() const { return this->isAttached; } /// Retrieves the human-readable name of the input device /// The human-readable name of the input device public: const std::string &GetName() const { return this->name; } /// Retrieves the current state of the input device /// The input device's current state public: const GamePadState &GetState() const { return this->currentState; } /// Updates the state of the input device protected: void Update(); /// Takes a snapshot of the input device's current state protected: void TakeSnapshot(); /// Removes all snapshots the device has taken protected: void ClearSnapshots(); /// Reads the current state of the game pad private: void readState(XINPUT_STATE &state); /// Generates events to report the changes between the two states /// Previous state of the game pad /// Current state of the game pad private: void generateEvents(const XINPUT_STATE &previous, const XINPUT_STATE ¤t); /// Generates a button event for the specified button /// /// True if the button has been pressed, false if it has been released /// /// Index of the button that has changed state private: void generateButtonEvent(bool pressed, std::size_t buttonIndex); /// Converts the current XINPUT state to a game pad state private: void convertState(); /// Converts the XINPUT triggers to the game pad triggers private: void convertTriggers(); /// Converts the left XINPUT thumb stick to a game pad axis private: void convertLeftThumbStick(); /// Converts the right XINPUT thumb stick to a game pad axis private: void convertRightThumbStick(); /// Converts the XINPUT buttons to the game pad buttons private: void convertButtons(); /// Converts the XINPUT directional pad to the game pad directional pad private: void convertDirectionalPad(); private: XInputGamePad(const XInputGamePad &); private: XInputGamePad &operator =(const XInputGamePad &); /// Index of the player represented by this game pad private: DWORD playerIndex; /// Stores the current state of the XInput game pad private: XINPUT_STATE *xInputState; /// Current state of the game pad in our own structure private: GamePadState currentState; /// Snapshots that have been recorded for the game pad private: ConcurrentQueue snapshots; /// Pool containing already allocated snapshots private: ConcurrentQueue pool; /// Whether the game pad is currently attached private: bool isAttached; /// The name of this game pad private: std::string name; }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Input::Devices #endif // NUCLEX_INPUT_DEVICES_XINPUTGAMEPAD_H