MPMA Framework 0.4
|
00001 00002 //Luke Lenhart, 2011 00003 //See /docs/License.txt for details on how this code may be used. 00004 00005 #pragma once 00006 00007 #include "../base/Setup.h" 00008 00009 #if defined(MPMA_COMPILE_INPUT) && defined(ENABLE_GAME_DEVICE_INPUT) 00010 00011 #include "../base/Types.h" 00012 00013 #include <string> 00014 #include <list> 00015 #include <vector> 00016 00017 namespace INPUT 00018 { 00019 namespace GAME 00020 { 00021 class Device; 00022 } 00023 } 00024 00025 namespace INPUT_INTERNAL 00026 { 00027 struct PlatformDeviceData; 00028 struct PlatformObjectInformation; 00029 struct PlatformAxesInformation; 00030 00031 #if defined(_WIN32) || defined(_WIN64) 00032 //windows treats hats differently, so we need to track them internally separately 00033 class Hat 00034 { 00035 public: 00036 const std::string& GetName() const; 00037 const std::string& GetXName() const; 00038 const std::string& GetYName() const; 00039 const std::string& GetZName() const; 00040 bool IsPressed() const; 00041 float GetRotation() const; //0 to 2*PI 00042 00043 Hat(INPUT_INTERNAL::PlatformObjectInformation *objectInfo); 00044 Hat(Hat &&o); 00045 ~Hat(); 00046 00047 private: 00048 INPUT_INTERNAL::PlatformObjectInformation *info; 00049 std::string nameX; 00050 std::string nameY; 00051 std::string nameZ; 00052 bool isPressed; 00053 float value; 00054 00055 Hat(const Hat &o); //not implemented 00056 void SetState(char *rawdata); 00057 00058 friend class INPUT::GAME::Device; 00059 }; 00060 #endif 00061 } 00062 00063 namespace INPUT 00064 { 00066 namespace GAME 00067 { 00069 class AxisSet 00070 { 00071 public: 00073 const std::string GetCombinedName() const; 00075 const std::string& GetPersistentIdentifier() const; 00076 00077 bool IsXPresent() const; 00078 bool IsYPresent() const; 00079 bool IsZPresent() const; 00080 const std::string& GetXName() const; 00081 const std::string& GetYName() const; 00082 const std::string& GetZName() const; 00083 float GetXValue() const; 00084 float GetYValue() const; 00085 float GetZValue() const; 00086 00087 void SetXDeadZone(float amount); 00088 void SetYDeadZone(float amount); 00089 void SetZDeadZone(float amount); 00090 float GetXDeadZone() const; 00091 float GetYDeadZone() const; 00092 float GetZDeadZone() const; 00093 00095 const Device* GetDevice() const; 00096 00097 AxisSet(INPUT_INTERNAL::PlatformAxesInformation *objectInfo, Device *device); 00098 AxisSet(AxisSet &&o); 00099 ~AxisSet(); 00100 00101 private: 00102 std::string persistentIdentifier; 00103 INPUT_INTERNAL::PlatformAxesInformation *info; 00104 float xValue, yValue, zValue; 00105 float xDead, yDead, zDead; 00106 Device *parentDevice; 00107 00108 AxisSet(const AxisSet &o); //not implemented 00109 void SetState(char *rawdata); 00110 void NormalizeMaximum(); 00111 00112 float ApplyDeadZone(float val, float dead); 00113 00114 friend class Device; 00115 }; 00116 00118 class Button 00119 { 00120 public: 00122 const std::string& GetName() const; 00124 const std::string& GetPersistentIdentifier() const; 00126 bool IsPressed() const; 00128 const Device* GetDevice() const; 00129 00130 //the application is not expected to use these directly 00131 Button(INPUT_INTERNAL::PlatformObjectInformation *objectInfo, Device *device); 00132 Button(Button &&o); 00133 ~Button(); 00134 00135 private: 00136 std::string persistentIdentifier; 00137 INPUT_INTERNAL::PlatformObjectInformation *info; 00138 bool isPressed; 00139 Device *parentDevice; 00140 00141 Button(const Button &o); //not implemented 00142 void SetState(char *rawdata); 00143 00144 friend class Device; 00145 }; 00146 00148 class Device 00149 { 00150 public: 00152 const std::string& GetName() const; 00154 const std::string& GetPersistentIdentifier() const; 00155 00156 //Updates the data in all axes and buttons. The application should not call this directly but should instead call PollAllDevices, which will also update global pressed states. 00157 void Poll(); 00158 00160 const std::list<AxisSet>& GetAxes() const; 00162 std::list<AxisSet>& GetAxes(); 00164 const std::list<Button>& GetButtons() const; 00166 std::list<Button>& GetButtons(); 00167 00168 //the application is not expected to use these directly 00169 Device(INPUT_INTERNAL::PlatformDeviceData *platformData, const std::string &name, const std::string &persistentId); 00170 Device(Device &&o); 00171 ~Device(); 00172 00173 #if defined(linux) 00174 //linux joy api doesn't provide relations between a device's axes. MPMA will autodetect based on a known list, but this API allows apps to override the default layout for other devices 00175 struct AxisSetMap 00176 { 00177 inline AxisSetMap(): XNumber(-1), YNumber(-1), ZNumber(-1) {} 00178 00179 std::string Name; 00180 int XNumber; 00181 int YNumber; 00182 int ZNumber; 00183 }; 00184 00185 //retrieves the current axis map 00186 const std::vector<AxisSetMap>& GetAxitMaps() const; 00187 00188 //replaces the current axes map. note that this frees all existing Axes on the device, so callers should beware any pointers they might have. 00189 void SetAxitMaps(const std::vector<AxisSetMap> &maps); 00190 00191 //retrieves the number of device axes 00192 int GetAxisCount() const; 00193 #endif 00194 00195 private: 00196 std::string friendlyName; 00197 std::string persistentIdentifier; 00198 INPUT_INTERNAL::PlatformDeviceData *data; 00199 std::list<AxisSet> axes; 00200 std::list<Button> buttons; 00201 00202 #if defined(_WIN32) || defined(_WIN64) 00203 //hats are tracked internally separately on windows 00204 std::list<INPUT_INTERNAL::Hat> hats; 00205 #elif defined(linux) 00206 std::vector<AxisSetMap> axisMaps; 00207 #endif 00208 00209 Device(const Device &o); //do not use 00210 }; 00211 00212 00214 std::list<Device>& GetDevices(); 00215 00217 void EnableAutoPoll(bool enable); 00218 00220 void PollAllDevices(); 00221 00223 const std::vector<const Button*>& GetCurrentlyPressedButtons(); 00224 00226 const std::vector<const Button*>& GetNewlyPressedButtons(); 00227 00229 const std::vector<const AxisSet*>& GetCurrentlyPressedAxes(); 00230 00232 const std::vector<const AxisSet*>& GetNewlyPressedAxes(); 00233 00235 Device* FindDevice(const std::string &identifierToFind); 00236 00238 Button* FindButton(const std::string &identifierToFind); 00239 00241 AxisSet* FindAxisSet(const std::string &identifierToFind); 00242 } 00243 } 00244 00245 #endif //#if defined(MPMA_COMPILE_INPUT) && defined(ENABLE_GAME_DEVICE_INPUT)