CCController-apple.mm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /****************************************************************************
  2. Copyright (c) 2014 cocos2d-x.org
  3. Copyright (c) 2014-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "base/CCController.h"
  23. #include "platform/CCPlatformConfig.h"
  24. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  25. #include "base/ccMacros.h"
  26. #include "base/CCEventDispatcher.h"
  27. #include "base/CCEventController.h"
  28. #include "base/CCEventListenerController.h"
  29. #include "base/CCDirector.h"
  30. #include "2d/CCLabel.h"
  31. #import <GameController/GameController.h>
  32. typedef void (^GCControllerConnectionBlock)(GCController* controller);
  33. typedef void (^GCControllerDisconnectionBlock)(GCController* controller);
  34. @interface GCControllerConnectionEventHandler : NSObject
  35. {
  36. }
  37. @property (copy) GCControllerConnectionBlock _connectionBlock;
  38. @property (copy) GCControllerDisconnectionBlock _disconnectionBlock;
  39. +(GCControllerConnectionEventHandler*) getInstance;
  40. +(void) destroyInstance;
  41. @end
  42. @implementation GCControllerConnectionEventHandler
  43. static GCControllerConnectionEventHandler* __instance = nil;
  44. +(GCControllerConnectionEventHandler*) getInstance {
  45. if (__instance == nil)
  46. {
  47. __instance = [[GCControllerConnectionEventHandler alloc] init];
  48. }
  49. return __instance;
  50. }
  51. +(void) destroyInstance {
  52. if (__instance)
  53. {
  54. [__instance release];
  55. __instance = nil;
  56. }
  57. }
  58. -(void) observerConnection: (GCControllerConnectionBlock) connectBlock disconnection: (GCControllerDisconnectionBlock) disconnectBlock {
  59. self._connectionBlock = connectBlock;
  60. self._disconnectionBlock = disconnectBlock;
  61. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onControllerConnected:) name:GCControllerDidConnectNotification object:nil];
  62. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onControllerDisconnected:) name:GCControllerDidDisconnectNotification object:nil];
  63. }
  64. -(void)dealloc {
  65. [[NSNotificationCenter defaultCenter] removeObserver:self];
  66. // Have to reset 'copy' property to nil value to avoid memory leak.
  67. self._connectionBlock = nil;
  68. self._disconnectionBlock = nil;
  69. [super dealloc];
  70. }
  71. -(void) onControllerConnected :(NSNotification *)connectedNotification {
  72. GCController *controller =(GCController *)[connectedNotification object];
  73. self._connectionBlock(controller);
  74. }
  75. -(void) onControllerDisconnected :(NSNotification *)connectedNotification {
  76. GCController *controller =(GCController *)[connectedNotification object];
  77. self._disconnectionBlock(controller);
  78. }
  79. @end
  80. NS_CC_BEGIN
  81. class ControllerImpl
  82. {
  83. public:
  84. ControllerImpl(Controller* controller)
  85. : _controller(controller)
  86. , _gcController(nil)
  87. {
  88. }
  89. Controller* _controller;
  90. GCController* _gcController;
  91. };
  92. void Controller::startDiscoveryController()
  93. {
  94. if (NSClassFromString(@"GCController") == nil) {
  95. return;
  96. }
  97. [GCController startWirelessControllerDiscoveryWithCompletionHandler: nil];
  98. [[GCControllerConnectionEventHandler getInstance] observerConnection: ^(GCController* gcController) {
  99. auto controller = new (std::nothrow) Controller();
  100. controller->_impl->_gcController = gcController;
  101. controller->_deviceName = [gcController.vendorName UTF8String];
  102. s_allController.push_back(controller);
  103. controller->registerListeners();
  104. controller->getDeviceName();
  105. controller->onConnected();
  106. } disconnection: ^(GCController* gcController) {
  107. auto iter = std::find_if(s_allController.begin(), s_allController.end(), [gcController](Controller* c){ return c->_impl->_gcController == gcController; });
  108. if(iter == s_allController.end())
  109. {
  110. log("disconnect:Could not find the controller");
  111. return;
  112. }
  113. (*iter)->onDisconnected();
  114. s_allController.erase(iter);
  115. }];
  116. }
  117. void Controller::stopDiscoveryController()
  118. {
  119. if (NSClassFromString(@"GCController") == nil) {
  120. return;
  121. }
  122. [GCController stopWirelessControllerDiscovery];
  123. }
  124. Controller::Controller()
  125. : _controllerTag(TAG_UNSET)
  126. , _impl(new ControllerImpl(this))
  127. , _connectEvent(nullptr)
  128. , _keyEvent(nullptr)
  129. , _axisEvent(nullptr)
  130. , _deviceId(0)
  131. {
  132. init();
  133. }
  134. Controller::~Controller()
  135. {
  136. delete _impl;
  137. delete _connectEvent;
  138. delete _keyEvent;
  139. delete _axisEvent;
  140. }
  141. void Controller::registerListeners()
  142. {
  143. if (_impl->_gcController.extendedGamepad != nil)
  144. {
  145. _impl->_gcController.extendedGamepad.dpad.up.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
  146. onButtonEvent(Key::BUTTON_DPAD_UP, pressed, value, button.isAnalog);
  147. };
  148. _impl->_gcController.extendedGamepad.dpad.down.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
  149. onButtonEvent(Key::BUTTON_DPAD_DOWN, pressed, value, button.isAnalog);
  150. };
  151. _impl->_gcController.extendedGamepad.dpad.left.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
  152. onButtonEvent(Key::BUTTON_DPAD_LEFT, pressed, value, button.isAnalog);
  153. };
  154. _impl->_gcController.extendedGamepad.dpad.right.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
  155. onButtonEvent(Key::BUTTON_DPAD_RIGHT, pressed, value, button.isAnalog);
  156. };
  157. _impl->_gcController.extendedGamepad.leftThumbstick.xAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
  158. onAxisEvent(Key::JOYSTICK_LEFT_X, value, axis.isAnalog);
  159. };
  160. _impl->_gcController.extendedGamepad.leftThumbstick.yAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
  161. onAxisEvent(Key::JOYSTICK_LEFT_Y, -value, axis.isAnalog);
  162. };
  163. _impl->_gcController.extendedGamepad.rightThumbstick.xAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
  164. onAxisEvent(Key::JOYSTICK_RIGHT_X, value, axis.isAnalog);
  165. };
  166. _impl->_gcController.extendedGamepad.rightThumbstick.yAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
  167. onAxisEvent(Key::JOYSTICK_RIGHT_Y, -value, axis.isAnalog);
  168. };
  169. _impl->_gcController.extendedGamepad.valueChangedHandler = ^(GCExtendedGamepad *gamepad, GCControllerElement *element){
  170. if (element == gamepad.buttonA)
  171. {
  172. onButtonEvent(Key::BUTTON_A, gamepad.buttonA.isPressed, gamepad.buttonA.value, gamepad.buttonA.isAnalog);
  173. }
  174. else if (element == gamepad.buttonB)
  175. {
  176. onButtonEvent(Key::BUTTON_B, gamepad.buttonB.isPressed, gamepad.buttonB.value, gamepad.buttonB.isAnalog);
  177. }
  178. else if (element == gamepad.buttonX)
  179. {
  180. onButtonEvent(Key::BUTTON_X, gamepad.buttonX.isPressed, gamepad.buttonX.value, gamepad.buttonX.isAnalog);
  181. }
  182. else if (element == gamepad.buttonY)
  183. {
  184. onButtonEvent(Key::BUTTON_Y, gamepad.buttonY.isPressed, gamepad.buttonY.value, gamepad.buttonY.isAnalog);
  185. }
  186. else if (element == gamepad.leftShoulder)
  187. {
  188. onButtonEvent(Key::BUTTON_LEFT_SHOULDER, gamepad.leftShoulder.isPressed, gamepad.leftShoulder.value, gamepad.leftShoulder.isAnalog);
  189. }
  190. else if (element == gamepad.rightShoulder)
  191. {
  192. onButtonEvent(Key::BUTTON_RIGHT_SHOULDER, gamepad.rightShoulder.isPressed, gamepad.rightShoulder.value, gamepad.rightShoulder.isAnalog);
  193. }
  194. else if (element == gamepad.leftTrigger)
  195. {
  196. onAxisEvent(Key::AXIS_LEFT_TRIGGER, gamepad.leftTrigger.value, gamepad.leftTrigger.isAnalog);
  197. }
  198. else if (element == gamepad.rightTrigger)
  199. {
  200. onAxisEvent(Key::AXIS_RIGHT_TRIGGER, gamepad.rightTrigger.value, gamepad.rightTrigger.isAnalog);
  201. }
  202. };
  203. }
  204. else if (_impl->_gcController.gamepad != nil)
  205. {
  206. _impl->_gcController.gamepad.dpad.up.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
  207. onButtonEvent(Key::BUTTON_DPAD_UP, pressed, value, button.isAnalog);
  208. };
  209. _impl->_gcController.gamepad.dpad.down.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
  210. onButtonEvent(Key::BUTTON_DPAD_DOWN, pressed, value, button.isAnalog);
  211. };
  212. _impl->_gcController.gamepad.dpad.left.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
  213. onButtonEvent(Key::BUTTON_DPAD_LEFT, pressed, value, button.isAnalog);
  214. };
  215. _impl->_gcController.gamepad.dpad.right.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
  216. onButtonEvent(Key::BUTTON_DPAD_RIGHT, pressed, value, button.isAnalog);
  217. };
  218. _impl->_gcController.gamepad.valueChangedHandler = ^(GCGamepad *gamepad, GCControllerElement *element){
  219. if (element == gamepad.buttonA)
  220. {
  221. onButtonEvent(Key::BUTTON_A, gamepad.buttonA.isPressed, gamepad.buttonA.value, gamepad.buttonA.isAnalog);
  222. }
  223. else if (element == gamepad.buttonB)
  224. {
  225. onButtonEvent(Key::BUTTON_B, gamepad.buttonB.isPressed, gamepad.buttonB.value, gamepad.buttonB.isAnalog);
  226. }
  227. else if (element == gamepad.buttonX)
  228. {
  229. onButtonEvent(Key::BUTTON_X, gamepad.buttonX.isPressed, gamepad.buttonX.value, gamepad.buttonX.isAnalog);
  230. }
  231. else if (element == gamepad.buttonY)
  232. {
  233. onButtonEvent(Key::BUTTON_Y, gamepad.buttonY.isPressed, gamepad.buttonY.value, gamepad.buttonY.isAnalog);
  234. }
  235. else if (element == gamepad.leftShoulder)
  236. {
  237. onButtonEvent(Key::BUTTON_LEFT_SHOULDER, gamepad.leftShoulder.isPressed, gamepad.leftShoulder.value, gamepad.leftShoulder.isAnalog);
  238. }
  239. else if (element == gamepad.rightShoulder)
  240. {
  241. onButtonEvent(Key::BUTTON_RIGHT_SHOULDER, gamepad.rightShoulder.isPressed, gamepad.rightShoulder.value, gamepad.rightShoulder.isAnalog);
  242. }
  243. };
  244. }
  245. #if defined(CC_TARGET_OS_TVOS)
  246. else if (_impl->_gcController.microGamepad != nil)
  247. {
  248. _impl->_gcController.microGamepad.dpad.up.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
  249. onButtonEvent(Key::BUTTON_DPAD_UP, pressed, value, button.isAnalog);
  250. };
  251. _impl->_gcController.microGamepad.dpad.down.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
  252. onButtonEvent(Key::BUTTON_DPAD_DOWN, pressed, value, button.isAnalog);
  253. };
  254. _impl->_gcController.microGamepad.dpad.left.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
  255. onButtonEvent(Key::BUTTON_DPAD_LEFT, pressed, value, button.isAnalog);
  256. };
  257. _impl->_gcController.microGamepad.dpad.right.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
  258. onButtonEvent(Key::BUTTON_DPAD_RIGHT, pressed, value, button.isAnalog);
  259. };
  260. _impl->_gcController.microGamepad.valueChangedHandler = ^(GCMicroGamepad *gamepad, GCControllerElement *element){
  261. if (element == gamepad.buttonA)
  262. {
  263. onButtonEvent(Key::BUTTON_A, gamepad.buttonA.isPressed, gamepad.buttonA.value, gamepad.buttonA.isAnalog);
  264. }
  265. else if (element == gamepad.buttonX)
  266. {
  267. onButtonEvent(Key::BUTTON_X, gamepad.buttonX.isPressed, gamepad.buttonX.value, gamepad.buttonX.isAnalog);
  268. }
  269. };
  270. }
  271. #endif
  272. _impl->_gcController.controllerPausedHandler = ^(GCController* gcCon){
  273. auto iter = std::find_if(s_allController.begin(), s_allController.end(), [gcCon](Controller* c){ return c->_impl->_gcController == gcCon; });
  274. if(iter == s_allController.end())
  275. {
  276. log("Could not find the controller");
  277. return;
  278. }
  279. onButtonEvent(Key::BUTTON_PAUSE, true, 1.0f, false);
  280. onButtonEvent(Key::BUTTON_PAUSE, false, 0.0f, false);
  281. };
  282. }
  283. bool Controller::isConnected() const
  284. {
  285. return _impl->_gcController.isAttachedToDevice == YES;
  286. }
  287. void Controller::receiveExternalKeyEvent(int externalKeyCode,bool receive)
  288. {
  289. }
  290. NS_CC_END
  291. #endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)