#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_DIRECTIONALPAD_H #define NUCLEX_INPUT_DEVICES_DIRECTIONALPAD_H namespace Nuclex { namespace Input { namespace Devices { // ------------------------------------------------------------------------------------------- // /// State of a directional pad or PoV hat struct DirectionalPad { /// Builds a directional pad state from the angle of a PoV hat /// Angle of the PoV hat /// A directional pad state equivalent to the specified angle public: static DirectionalPad FromAngle(int angle) { DirectionalPad directionalPad; if(angle < 0) { directionalPad.Up = false; directionalPad.Down = false; directionalPad.Left = false; directionalPad.Right = false; } else { directionalPad.Up = ((angle > 270) || (angle < 90)); directionalPad.Down = ((angle > 90) && (angle < 270)); directionalPad.Left = ((angle > 180) && (angle < 360)); directionalPad.Right = ((angle > 0) && (angle < 180)); } return directionalPad; } /// Determines the angle if the directional pad was a PoV hat /// The angle a PoV hat would have, -1 for neutral public: int ToAngle() const { if (this->Up && !this->Down) { if (this->Left && !this->Right) { return 315; // Left up } else if (this->Right && !this->Left) { return 45; // Right up } else { return 0; // Up } } else if (this->Down && !this->Up) { if (this->Left && !this->Right) { return 225; // Left down } else if (this->Right && !this->Left) { return 135; // Right down } else { return 180; // Down } } else if (this->Left && !this->Right) { return 270; // Left } else if (this->Right && !this->Left) { return 90; // Right } else { return -1; // Neutral } } /// Whether the directional pad is being pressed upwards public: bool Up; /// Whether the directional pad is being pressed downwards public: bool Down; /// Whether the directional pad is being pressed to the left public: bool Left; /// Whether the directional pad is being pressed to the right public: bool Right; }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Input::Devices #endif // NUCLEX_INPUT_DEVICES_DIRECTIONALPAD_H