CCController.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /****************************************************************************
  2. Copyright (c) 2014-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __cocos2d_libs__CCController__
  22. #define __cocos2d_libs__CCController__
  23. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 )
  24. #include "platform/CCPlatformMacros.h"
  25. #include <string>
  26. #include <vector>
  27. #include <unordered_map>
  28. NS_CC_BEGIN
  29. class ControllerImpl;
  30. class EventListenerController;
  31. class EventController;
  32. class EventDispatcher;
  33. /**
  34. * @addtogroup base
  35. * @{
  36. */
  37. /**
  38. * @class Controller
  39. * @brief A Controller object represents a connected physical game controller.
  40. * @js NA
  41. */
  42. class CC_DLL Controller
  43. {
  44. public:
  45. /**
  46. * Controllers' standard key
  47. * Controller receives only standard key which contained within enum Key by default.
  48. */
  49. enum Key
  50. {
  51. KEY_NONE = 0,
  52. JOYSTICK_LEFT_X = 1000,
  53. JOYSTICK_LEFT_Y,
  54. JOYSTICK_RIGHT_X,
  55. JOYSTICK_RIGHT_Y,
  56. BUTTON_A,
  57. BUTTON_B,
  58. BUTTON_C,
  59. BUTTON_X,
  60. BUTTON_Y,
  61. BUTTON_Z,
  62. BUTTON_DPAD_UP,
  63. BUTTON_DPAD_DOWN,
  64. BUTTON_DPAD_LEFT,
  65. BUTTON_DPAD_RIGHT,
  66. BUTTON_DPAD_CENTER,
  67. BUTTON_LEFT_SHOULDER,
  68. BUTTON_RIGHT_SHOULDER,
  69. AXIS_LEFT_TRIGGER,
  70. AXIS_RIGHT_TRIGGER,
  71. BUTTON_LEFT_THUMBSTICK,
  72. BUTTON_RIGHT_THUMBSTICK,
  73. BUTTON_START,
  74. BUTTON_SELECT,
  75. BUTTON_PAUSE,
  76. KEY_MAX
  77. };
  78. /**
  79. * @struct KeyStatus
  80. *
  81. */
  82. typedef struct _keyStatus
  83. {
  84. /** A Boolean value that indicates whether the key is considered pressed. */
  85. bool isPressed;
  86. /** The value of key.This value is used in conjunction with the isPressed parameter. */
  87. float value;
  88. /** A Boolean value that indicates whether the value of key is analog.
  89. * If isAnalog is true, the key value might be a float from -1 to 1.
  90. * If isAnalog is false, the key value would be contain one number: 0 or 1.
  91. */
  92. bool isAnalog;
  93. }KeyStatus;
  94. static const int TAG_UNSET = -1;
  95. /**
  96. * Gets all Controller objects.
  97. */
  98. static const std::vector<Controller*>& getAllController(){ return s_allController;}
  99. /**
  100. * Gets a Controller object with tag.
  101. *
  102. * @param tag An identifier to find the controller.
  103. * @return A Controller object.
  104. */
  105. static Controller* getControllerByTag(int tag);
  106. /**
  107. * Gets a Controller object with device ID.
  108. *
  109. * @param deviceId A unique identifier to find the controller.
  110. * @return A Controller object.
  111. */
  112. static Controller* getControllerByDeviceId(int deviceId);
  113. /**
  114. * Start discovering new controllers.
  115. *
  116. * @warning The API has an empty implementation on Android.
  117. */
  118. static void startDiscoveryController();
  119. /**
  120. * Stop the discovery process.
  121. *
  122. * @warning The API has an empty implementation on Android.
  123. */
  124. static void stopDiscoveryController();
  125. /**
  126. * Gets the name of this Controller object.
  127. */
  128. const std::string& getDeviceName() const { return _deviceName;}
  129. /**
  130. * Gets the Controller id.
  131. */
  132. int getDeviceId() const { return _deviceId;}
  133. /**
  134. * Indicates whether the Controller is connected.
  135. */
  136. bool isConnected() const;
  137. /**
  138. *
  139. */
  140. const KeyStatus& getKeyStatus(int keyCode);
  141. /**
  142. * Activate receives key event from external key. e.g. back,menu.
  143. * Controller receives only standard key which contained within enum Key by default.
  144. *
  145. * @warning The API only work on the android platform for support diversified game controller.
  146. *
  147. * @param externalKeyCode External key code.
  148. * @param receive True if external key event on this controller should be receive, false otherwise.
  149. */
  150. void receiveExternalKeyEvent(int externalKeyCode,bool receive);
  151. /**
  152. * Changes the tag that is used to identify the controller easily.
  153. * @param tag A integer that identifies the controller.
  154. */
  155. void setTag(int tag) { _controllerTag = tag;}
  156. /**
  157. * Returns a tag that is used to identify the controller easily.
  158. *
  159. * @return An integer that identifies the controller.
  160. */
  161. int getTag() const { return _controllerTag;}
  162. private:
  163. static std::vector<Controller*> s_allController;
  164. Controller();
  165. virtual ~Controller();
  166. void init();
  167. void onConnected();
  168. void onDisconnected();
  169. void onButtonEvent(int keyCode, bool isPressed, float value, bool isAnalog);
  170. void onAxisEvent(int axisCode, float value, bool isAnalog);
  171. void registerListeners();
  172. std::unordered_map<int, KeyStatus> _allKeyStatus;
  173. std::unordered_map<int, KeyStatus> _allKeyPrevStatus;
  174. std::string _deviceName;
  175. int _deviceId;
  176. int _controllerTag;
  177. ControllerImpl* _impl;
  178. EventDispatcher* _eventDispatcher;
  179. EventController *_connectEvent;
  180. EventController *_keyEvent;
  181. EventController *_axisEvent;
  182. #if ( CC_TARGET_PLATFORM == CC_PLATFORM_LINUX || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 )
  183. //FIXME: Once GLFW 3.3 is bundled with cocos2d-x, remove these unordered
  184. //maps. They won't be needed. We will only need to provide a mapping from
  185. //the GLFW gamepad key codes to the Controller::Key.
  186. // Attach the controller profiles from CCController-linux-win32.cpp to each
  187. // of the Controller variables in order to minimize profile lookup time.
  188. // Note: this increases memory usage unnecessarily since the same maps are
  189. // already stored on ControllerImpl within the static member variable
  190. // "s_controllerProfiles", but on these platforms the increase in memory
  191. // usage is negligible. Peformance over memory optimization was
  192. // consciously chosen.
  193. std::unordered_map<int,int> _buttonInputMap;
  194. std::unordered_map<int,int> _axisInputMap;
  195. #endif
  196. friend class ControllerImpl;
  197. friend class EventListenerController;
  198. };
  199. // end group
  200. /// @}
  201. NS_CC_END
  202. #endif
  203. #endif /* defined(__cocos2d_libs__CCController__) */