2014-06-30 00:31:45 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 cocos2d-x.org
|
|
|
|
Copyright (c) 2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCController.h"
|
|
|
|
#include "base/CCPlatformConfig.h"
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
|
|
|
|
#include "CCGamepad.h"
|
|
|
|
#include "CCControllerDirectionPad.h"
|
|
|
|
#include "CCControllerButtonInput.h"
|
|
|
|
#include "CCControllerAxisInput.h"
|
|
|
|
#include "CCControllerThumbstick.h"
|
|
|
|
|
|
|
|
#include "ccMacros.h"
|
|
|
|
#include "CCEventDispatcher.h"
|
|
|
|
#include "CCEventController.h"
|
|
|
|
#include "CCEventListenerController.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
|
|
|
|
#import <GameController/GameController.h>
|
|
|
|
|
|
|
|
@interface GCControllerConnectionEventHandler : NSObject
|
|
|
|
|
|
|
|
typedef void (^GCControllerConnectionBlock)(GCController* controller);
|
|
|
|
@property (copy) GCControllerConnectionBlock _connectionBlock;
|
|
|
|
|
|
|
|
typedef void (^GCControllerDisconnectionBlock)(GCController* controller);
|
|
|
|
@property (copy) GCControllerDisconnectionBlock _disconnectionBlock;
|
|
|
|
|
|
|
|
+(GCControllerConnectionEventHandler*) getInstance;
|
|
|
|
+(void) destroyInstance;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GCControllerConnectionEventHandler
|
|
|
|
|
|
|
|
|
|
|
|
static GCControllerConnectionEventHandler* __instance = nil;
|
|
|
|
|
|
|
|
+(GCControllerConnectionEventHandler*) getInstance {
|
|
|
|
|
|
|
|
if (__instance == nil)
|
|
|
|
{
|
|
|
|
__instance = [[GCControllerConnectionEventHandler alloc] init];
|
|
|
|
}
|
|
|
|
return __instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
+(void) destroyInstance {
|
|
|
|
if (__instance)
|
|
|
|
{
|
|
|
|
[__instance release];
|
|
|
|
__instance = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) observerConnection: (GCControllerConnectionBlock) connectBlock disconnection: (GCControllerDisconnectionBlock) disconnectBlock {
|
|
|
|
self._connectionBlock = connectBlock;
|
|
|
|
self._disconnectionBlock = disconnectBlock;
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onControllerConnected:) name:GCControllerDidConnectNotification object:nil];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onControllerDisconnected:) name:GCControllerDidDisconnectNotification object:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)dealloc {
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) onControllerConnected :(NSNotification *)connectedNotification {
|
|
|
|
GCController *controller =(GCController *)[connectedNotification object];
|
|
|
|
|
|
|
|
self._connectionBlock(controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) onControllerDisconnected :(NSNotification *)connectedNotification {
|
2014-06-30 12:53:26 +08:00
|
|
|
|
2014-06-30 00:31:45 +08:00
|
|
|
GCController *controller =(GCController *)[connectedNotification object];
|
|
|
|
self._disconnectionBlock(controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
#define sendEventButton(dstID, srcID) \
|
|
|
|
dstID->setPressed(srcID.isPressed); \
|
|
|
|
dstID->setValue(srcID.value); \
|
|
|
|
dstID->setAnalog(srcID.isAnalog); \
|
2014-06-30 12:53:26 +08:00
|
|
|
EventController evt(EventController::ControllerEventType::BUTTON_STATUS_CHANGED, _gamepad->_controller, dstID); \
|
2014-06-30 00:31:45 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);
|
|
|
|
|
|
|
|
|
|
|
|
#define sendEventAxis(dstID, srcID) \
|
|
|
|
\
|
|
|
|
dstID->setValue(srcID.value); \
|
|
|
|
dstID->setAnalog(srcID.isAnalog); \
|
|
|
|
\
|
2014-06-30 12:53:26 +08:00
|
|
|
EventController evt(EventController::ControllerEventType::AXIS_STATUS_CHANGED, _gamepad->_controller, dstID); \
|
2014-06-30 00:31:45 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);
|
|
|
|
|
|
|
|
class ControllerImpl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ControllerImpl(Controller* controller)
|
|
|
|
: _controller(controller)
|
|
|
|
, _gcController(nil)
|
2014-06-30 12:53:26 +08:00
|
|
|
, _lazyRegisterListener(true)
|
2014-06-30 00:31:45 +08:00
|
|
|
{
|
2014-06-30 12:53:26 +08:00
|
|
|
|
2014-06-30 00:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~ControllerImpl()
|
|
|
|
{
|
2014-06-30 12:53:26 +08:00
|
|
|
|
2014-06-30 00:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Controller* _controller;
|
|
|
|
GCController* _gcController;
|
2014-06-30 12:53:26 +08:00
|
|
|
bool _lazyRegisterListener;
|
2014-06-30 00:31:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<Controller*> Controller::_controllers;
|
|
|
|
|
|
|
|
const std::vector<Controller*>& Controller::getControllers()
|
|
|
|
{
|
|
|
|
return _controllers;
|
|
|
|
}
|
|
|
|
|
2014-06-30 12:53:26 +08:00
|
|
|
void Controller::startDiscoveryController()
|
2014-06-30 00:31:45 +08:00
|
|
|
{
|
2014-06-30 12:53:26 +08:00
|
|
|
[GCController startWirelessControllerDiscoveryWithCompletionHandler: nil];
|
|
|
|
|
|
|
|
[[GCControllerConnectionEventHandler getInstance] observerConnection: ^(GCController* gcController) {
|
|
|
|
auto controller = new Controller();
|
|
|
|
controller->_impl->_gcController = gcController;
|
|
|
|
|
|
|
|
gcController.controllerPausedHandler = ^(GCController* gcCon){
|
|
|
|
|
|
|
|
auto iter = std::find_if(_controllers.begin(), _controllers.end(), [gcCon](Controller* c){ return c->_impl->_gcController == gcCon; });
|
|
|
|
|
|
|
|
CCASSERT(iter != _controllers.end(), "Could not find the controller");
|
|
|
|
|
|
|
|
auto button = (*iter)->getGamepad()->getButtonPause();
|
|
|
|
button->setPressed(true);
|
|
|
|
EventController evt(EventController::ControllerEventType::BUTTON_STATUS_CHANGED, (*iter), button);
|
|
|
|
Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);
|
|
|
|
|
|
|
|
// Reset the pause button status to unpressed.
|
|
|
|
button->setPressed(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
_controllers.push_back(controller);
|
|
|
|
|
|
|
|
|
|
|
|
EventController evt(EventController::ControllerEventType::CONNECTION, controller, true);
|
|
|
|
Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);
|
|
|
|
|
|
|
|
} disconnection: ^(GCController* gcController) {
|
|
|
|
|
|
|
|
auto iter = std::find_if(_controllers.begin(), _controllers.end(), [gcController](Controller* c){ return c->_impl->_gcController == gcController; });
|
|
|
|
|
|
|
|
CCASSERT(iter != _controllers.end(), "Could not find the controller");
|
|
|
|
|
|
|
|
EventController evt(EventController::ControllerEventType::CONNECTION, *iter, false);
|
|
|
|
Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);
|
|
|
|
|
|
|
|
delete (*iter);
|
|
|
|
_controllers.erase(iter);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::stopDiscoveryController()
|
|
|
|
{
|
|
|
|
[GCController stopWirelessControllerDiscovery];
|
|
|
|
}
|
|
|
|
|
|
|
|
Controller::Controller()
|
|
|
|
{
|
|
|
|
_playerIndex = PLAYER_INDEX_UNSET;
|
|
|
|
_gamepad = new Gamepad();
|
|
|
|
_gamepad->_controller = this;
|
|
|
|
_impl = new ControllerImpl(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Controller::~Controller()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_impl);
|
|
|
|
CC_SAFE_DELETE(_gamepad);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& Controller::getVendorName()
|
|
|
|
{
|
|
|
|
if (_vendorName.empty())
|
|
|
|
{
|
|
|
|
_vendorName = [_impl->_gcController.vendorName UTF8String];
|
|
|
|
}
|
|
|
|
return _vendorName;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Controller::isConnected() const
|
|
|
|
{
|
|
|
|
return _impl->_gcController.isAttachedToDevice == YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Controller::getPlayerIndex() const
|
|
|
|
{
|
|
|
|
return _playerIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::setPlayerIndex(int playerIndex)
|
|
|
|
{
|
|
|
|
_playerIndex = playerIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
Gamepad* Controller::getGamepad() const
|
|
|
|
{
|
|
|
|
if (_impl->_gcController == nil)
|
|
|
|
return nullptr;
|
|
|
|
if(_impl->_gcController.gamepad == nil && _impl->_gcController.extendedGamepad == nil)
|
|
|
|
{
|
|
|
|
CCASSERT(false, "No gamepad was found!");
|
|
|
|
}
|
|
|
|
else if (_impl->_lazyRegisterListener)
|
|
|
|
{
|
|
|
|
_impl->_lazyRegisterListener = false;
|
|
|
|
if (_impl->_gcController.extendedGamepad != nil)
|
2014-06-30 00:31:45 +08:00
|
|
|
{
|
2014-06-30 12:53:26 +08:00
|
|
|
_impl->_gcController.extendedGamepad.dpad.up.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
2014-06-30 00:31:45 +08:00
|
|
|
sendEventButton(_gamepad->getDirectionPad()->getUp(), button);
|
|
|
|
};
|
|
|
|
|
2014-06-30 12:53:26 +08:00
|
|
|
_impl->_gcController.extendedGamepad.dpad.down.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
2014-06-30 00:31:45 +08:00
|
|
|
sendEventButton(_gamepad->getDirectionPad()->getDown(), button);
|
|
|
|
};
|
|
|
|
|
2014-06-30 12:53:26 +08:00
|
|
|
_impl->_gcController.extendedGamepad.dpad.left.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
2014-06-30 00:31:45 +08:00
|
|
|
sendEventButton(_gamepad->getDirectionPad()->getLeft(), button);
|
|
|
|
};
|
|
|
|
|
2014-06-30 12:53:26 +08:00
|
|
|
_impl->_gcController.extendedGamepad.dpad.right.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
2014-06-30 00:31:45 +08:00
|
|
|
sendEventButton(_gamepad->getDirectionPad()->getRight(), button);
|
|
|
|
};
|
|
|
|
|
2014-06-30 12:53:26 +08:00
|
|
|
_impl->_gcController.extendedGamepad.leftThumbstick.xAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
|
2014-06-30 00:31:45 +08:00
|
|
|
sendEventAxis(_gamepad->getLeftThumbstick()->getAxisX(), axis);
|
|
|
|
};
|
|
|
|
|
2014-06-30 12:53:26 +08:00
|
|
|
_impl->_gcController.extendedGamepad.leftThumbstick.yAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
|
2014-06-30 00:31:45 +08:00
|
|
|
sendEventAxis(_gamepad->getLeftThumbstick()->getAxisY(), axis);
|
|
|
|
};
|
|
|
|
|
2014-06-30 12:53:26 +08:00
|
|
|
_impl->_gcController.extendedGamepad.rightThumbstick.xAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
|
2014-06-30 00:31:45 +08:00
|
|
|
sendEventAxis(_gamepad->getRightThumbstick()->getAxisX(), axis);
|
|
|
|
};
|
|
|
|
|
2014-06-30 12:53:26 +08:00
|
|
|
_impl->_gcController.extendedGamepad.rightThumbstick.yAxis.valueChangedHandler = ^(GCControllerAxisInput *axis, float value){
|
2014-06-30 00:31:45 +08:00
|
|
|
sendEventAxis(_gamepad->getRightThumbstick()->getAxisY(), axis);
|
|
|
|
};
|
|
|
|
|
2014-06-30 12:53:26 +08:00
|
|
|
_impl->_gcController.extendedGamepad.valueChangedHandler = ^(GCExtendedGamepad *gamepad, GCControllerElement *element){
|
2014-06-30 00:31:45 +08:00
|
|
|
if (element == gamepad.buttonA)
|
|
|
|
{
|
|
|
|
sendEventButton(_gamepad->getButtonA(), gamepad.buttonA);
|
|
|
|
}
|
|
|
|
else if (element == gamepad.buttonB)
|
|
|
|
{
|
|
|
|
sendEventButton(_gamepad->getButtonB(), gamepad.buttonB);
|
|
|
|
}
|
|
|
|
else if (element == gamepad.buttonX)
|
|
|
|
{
|
|
|
|
sendEventButton(_gamepad->getButtonX(), gamepad.buttonX);
|
|
|
|
}
|
|
|
|
else if (element == gamepad.buttonY)
|
|
|
|
{
|
|
|
|
sendEventButton(_gamepad->getButtonY(), gamepad.buttonY);
|
|
|
|
}
|
|
|
|
else if (element == gamepad.leftShoulder)
|
|
|
|
{
|
|
|
|
sendEventButton(_gamepad->getLeftShoulder(), gamepad.leftShoulder);
|
|
|
|
}
|
|
|
|
else if (element == gamepad.rightShoulder)
|
|
|
|
{
|
|
|
|
sendEventButton(_gamepad->getRightShoulder(), gamepad.rightShoulder);
|
|
|
|
}
|
|
|
|
else if (element == gamepad.leftTrigger)
|
|
|
|
{
|
|
|
|
sendEventButton(_gamepad->getLeftTrigger(), gamepad.leftTrigger);
|
|
|
|
}
|
|
|
|
else if (element == gamepad.rightTrigger)
|
|
|
|
{
|
|
|
|
sendEventButton(_gamepad->getRightTrigger(), gamepad.rightTrigger);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2014-06-30 12:53:26 +08:00
|
|
|
else
|
2014-06-30 00:31:45 +08:00
|
|
|
{
|
2014-06-30 12:53:26 +08:00
|
|
|
_impl->_gcController.gamepad.dpad.up.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
2014-06-30 00:31:45 +08:00
|
|
|
sendEventButton(_gamepad->getDirectionPad()->getUp(), button);
|
|
|
|
};
|
2014-06-30 12:53:26 +08:00
|
|
|
|
|
|
|
_impl->_gcController.gamepad.dpad.down.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
2014-06-30 00:31:45 +08:00
|
|
|
sendEventButton(_gamepad->getDirectionPad()->getDown(), button);
|
|
|
|
};
|
|
|
|
|
2014-06-30 12:53:26 +08:00
|
|
|
_impl->_gcController.gamepad.dpad.left.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
2014-06-30 00:31:45 +08:00
|
|
|
sendEventButton(_gamepad->getDirectionPad()->getLeft(), button);
|
|
|
|
};
|
|
|
|
|
2014-06-30 12:53:26 +08:00
|
|
|
_impl->_gcController.gamepad.dpad.right.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed){
|
2014-06-30 00:31:45 +08:00
|
|
|
sendEventButton(_gamepad->getDirectionPad()->getRight(), button);
|
|
|
|
};
|
|
|
|
|
2014-06-30 12:53:26 +08:00
|
|
|
_impl->_gcController.gamepad.valueChangedHandler = ^(GCGamepad *gamepad, GCControllerElement *element){
|
2014-06-30 00:31:45 +08:00
|
|
|
|
|
|
|
if (element == gamepad.buttonA)
|
|
|
|
{
|
|
|
|
sendEventButton(_gamepad->getButtonA(), gamepad.buttonA);
|
|
|
|
}
|
|
|
|
else if (element == gamepad.buttonB)
|
|
|
|
{
|
|
|
|
sendEventButton(_gamepad->getButtonB(), gamepad.buttonB);
|
|
|
|
}
|
|
|
|
else if (element == gamepad.buttonX)
|
|
|
|
{
|
|
|
|
sendEventButton(_gamepad->getButtonX(), gamepad.buttonX);
|
|
|
|
}
|
|
|
|
else if (element == gamepad.buttonY)
|
|
|
|
{
|
|
|
|
sendEventButton(_gamepad->getButtonY(), gamepad.buttonY);
|
|
|
|
}
|
|
|
|
else if (element == gamepad.leftShoulder)
|
|
|
|
{
|
|
|
|
sendEventButton(_gamepad->getLeftShoulder(), gamepad.leftShoulder);
|
|
|
|
}
|
|
|
|
else if (element == gamepad.rightShoulder)
|
|
|
|
{
|
|
|
|
sendEventButton(_gamepad->getRightShoulder(), gamepad.rightShoulder);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _gamepad;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|