axmol/tests/game-controller-test/Classes/GameControllerTest.cpp

444 lines
16 KiB
C++
Raw Normal View History

2014-06-30 03:25:49 +08:00
#include "GameControllerTest.h"
#include "AppMacros.h"
USING_NS_CC;
GameControllerTest::~GameControllerTest()
{
Controller::stopDiscoveryController();
}
2014-07-04 15:22:53 +08:00
void GameControllerTest::registerControllerListener()
2014-06-30 03:25:49 +08:00
{
2014-07-04 15:22:53 +08:00
_listener = EventListenerController::create();
2014-06-30 03:25:49 +08:00
2014-07-04 15:22:53 +08:00
_listener->onConnected = CC_CALLBACK_2(GameControllerTest::onConnectController,this);
_listener->onDisconnected = CC_CALLBACK_2(GameControllerTest::onDisconnectedController,this);
_listener->onKeyDown = CC_CALLBACK_3(GameControllerTest::onKeyDown, this);
_listener->onKeyUp = CC_CALLBACK_3(GameControllerTest::onKeyUp, this);
_listener->onAxisEvent = CC_CALLBACK_3(GameControllerTest::onAxisEvent, this);
2014-06-30 03:25:49 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(_listener, this);
Controller::startDiscoveryController();
//get game pad status in polling mode
2014-07-04 15:22:53 +08:00
//scheduleUpdate();
2014-06-30 03:25:49 +08:00
}
2014-07-04 15:22:53 +08:00
void GameControllerTest::onConnectController(Controller* controller, Event* event)
2014-06-30 03:25:49 +08:00
{
2014-07-04 15:22:53 +08:00
if (controller == nullptr || controller == _firstHolder.controller || controller == _secondHolder.controller)
2014-06-30 03:25:49 +08:00
{
return;
}
2014-07-07 12:06:24 +08:00
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
//receive back key
controller->receiveExternalKeyEvent(4,true);
//receive menu key
controller->receiveExternalKeyEvent(82,true);
#endif
2014-06-30 03:25:49 +08:00
2014-07-07 17:57:08 +08:00
char deviceInfo[50];
sprintf(deviceInfo,"%s id:%d",controller->getDeviceName().c_str(), controller->getDeviceId());
2014-07-04 15:22:53 +08:00
2014-07-07 17:57:08 +08:00
if (_firstHolder.controller == nullptr && _secondHolder.controller == nullptr)
{
2014-07-04 15:22:53 +08:00
if (_firstHolder._holderNode)
{
_firstHolder.controller = controller;
2014-07-07 17:57:08 +08:00
_firstHolder._deviceLabel->setString(deviceInfo);
2014-07-04 15:22:53 +08:00
}
else
{
_secondHolder.controller = controller;
2014-07-07 17:57:08 +08:00
_secondHolder._deviceLabel->setString(deviceInfo);
2014-07-04 15:22:53 +08:00
}
2014-06-30 03:25:49 +08:00
}
2014-07-04 15:22:53 +08:00
else if(_secondHolder.controller == nullptr)
2014-06-30 03:25:49 +08:00
{
2014-07-04 15:22:53 +08:00
_secondHolder.controller = controller;
if (_secondHolder._holderNode == nullptr)
{
createControllerSprite(_secondHolder);
_firstHolder._holderNode->runAction(ScaleTo::create(0.3f,0.5f,0.5f));
_firstHolder._holderNode->runAction(MoveTo::create(0.3f,Vec2(_visibleQuarterX, _visibleCentreY)));
_secondHolder._holderNode->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
_secondHolder._holderNode->setScale(0.1f);
_secondHolder._holderNode->runAction(ScaleTo::create(0.3f,0.5f,0.5f));
_secondHolder._holderNode->setPosition(Vec2(_visibleThreeQuarterX, _visibleCentreY));
this->addChild(_secondHolder._holderNode);
}
_secondHolder._deviceLabel->setString(deviceInfo);
2014-06-30 03:25:49 +08:00
}
2014-07-04 15:22:53 +08:00
else
2014-06-30 03:25:49 +08:00
{
2014-07-04 15:22:53 +08:00
_firstHolder.controller = controller;
if (_firstHolder._holderNode == nullptr)
{
createControllerSprite(_firstHolder);
_secondHolder._holderNode->runAction(ScaleTo::create(0.3f,0.5f,0.5f));
_secondHolder._holderNode->runAction(MoveTo::create(0.3f,Vec2(_visibleQuarterX, _visibleCentreY)));
_firstHolder._holderNode->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
_firstHolder._holderNode->setScale(0.1f);
_firstHolder._holderNode->runAction(ScaleTo::create(0.3f,0.5f,0.5f));
_firstHolder._holderNode->setPosition(Vec2(_visibleThreeQuarterX, _visibleCentreY));
this->addChild(_firstHolder._holderNode);
}
2014-07-07 17:57:08 +08:00
_firstHolder._deviceLabel->setString(deviceInfo);
2014-06-30 03:25:49 +08:00
}
2014-07-04 15:22:53 +08:00
}
2014-06-30 03:25:49 +08:00
2014-07-04 15:22:53 +08:00
void GameControllerTest::onDisconnectedController(Controller* controller, Event* event)
{
log("onDisconnectedController:%d",controller->getDeviceId());
if (_firstHolder.controller == controller)
2014-06-30 03:25:49 +08:00
{
2014-07-04 15:22:53 +08:00
_firstHolder.controller = nullptr;
if (_secondHolder.controller != nullptr)
{
_firstHolder._holderNode->runAction(MoveBy::create(0.3f,Vec2(0,_visibleSize.height)));
_secondHolder._holderNode->runAction(MoveTo::create(0.3f,Vec2(_visibleCentreX,_visibleCentreY) ));
_secondHolder._holderNode->runAction(ScaleTo::create(0.3f,1.0f,1.0f));
}
else
{
resetControllerHolderState(_firstHolder);
}
2014-06-30 03:25:49 +08:00
}
2014-07-04 15:22:53 +08:00
else if (_secondHolder.controller == controller)
2014-06-30 03:25:49 +08:00
{
2014-07-04 15:22:53 +08:00
_secondHolder.controller = nullptr;
if (_firstHolder.controller != nullptr)
{
_secondHolder._holderNode->runAction(MoveBy::create(0.3f,Vec2(0,_visibleSize.height)));
_firstHolder._holderNode->runAction(MoveTo::create(0.3f,Vec2(_visibleCentreX,_visibleCentreY) ));
_firstHolder._holderNode->runAction(ScaleTo::create(0.3f,1.0f,1.0f));
}
else
{
resetControllerHolderState(_secondHolder);
}
2014-06-30 03:25:49 +08:00
}
2014-07-04 15:22:53 +08:00
}
2014-06-30 03:25:49 +08:00
2014-07-04 15:22:53 +08:00
void GameControllerTest::resetControllerHolderState(ControllerHolder& holder)
{
holder._buttonA->setColor(Color3B::WHITE);
holder._buttonB->setColor(Color3B::WHITE);
holder._buttonX->setColor(Color3B::WHITE);
holder._buttonY->setColor(Color3B::WHITE);
holder._dpadUp->setColor(Color3B::WHITE);
holder._dpadDown->setColor(Color3B::WHITE);
holder._dpadLeft->setColor(Color3B::WHITE);
holder._dpadRight->setColor(Color3B::WHITE);
holder._buttonL1->setColor(Color3B::WHITE);
holder._buttonR1->setColor(Color3B::WHITE);
holder._leftJoystick->setPosition(Vec2(238,460));
holder._rightJoystick->setPosition(Vec2(606,293));
2014-07-07 12:06:24 +08:00
holder._deviceLabel->setString("Disconnected");
2014-06-30 03:25:49 +08:00
}
2014-07-04 15:22:53 +08:00
void GameControllerTest::showButtonState(cocos2d::Controller *controller, int keyCode, bool isPressed)
2014-06-30 03:25:49 +08:00
{
2014-07-04 15:22:53 +08:00
onConnectController(controller,nullptr);
ControllerHolder* holder = nullptr;
if (controller == _firstHolder.controller)
holder = &_firstHolder;
else if(controller == _secondHolder.controller)
holder = &_secondHolder;
else
2014-06-30 03:25:49 +08:00
return;
2014-07-04 15:22:53 +08:00
if (isPressed)
2014-06-30 03:25:49 +08:00
{
2014-07-04 15:22:53 +08:00
switch (keyCode)
{
case Controller::Key::BUTTON_A:
holder->_buttonA->setColor(Color3B(250,103,93));
break;
case Controller::Key::BUTTON_B:
holder->_buttonB->setColor(Color3B(92,214,183));
break;
case Controller::Key::BUTTON_X:
holder->_buttonX->setColor(Color3B(96,113,192));
break;
case Controller::Key::BUTTON_Y:
holder->_buttonY->setColor(Color3B(199,222,118));
break;
case Controller::Key::BUTTON_DPAD_UP:
holder->_dpadUp->setColor(Color3B(0,115,158));
break;
case Controller::Key::BUTTON_DPAD_DOWN:
holder->_dpadDown->setColor(Color3B(0,115,158));
break;
case Controller::Key::BUTTON_DPAD_LEFT:
holder->_dpadLeft->setColor(Color3B(170,216,0));
break;
case Controller::Key::BUTTON_DPAD_RIGHT:
holder->_dpadRight->setColor(Color3B(170,216,0));
break;
case Controller::Key::BUTTON_LEFT_SHOULDER:
holder->_buttonL1->setColor(Color3B(19,231,238));
break;
case Controller::Key::BUTTON_RIGHT_SHOULDER:
holder->_buttonR1->setColor(Color3B(19,231,238));
break;
default:
2014-07-07 12:06:24 +08:00
{
char ketStatus[30];
sprintf(ketStatus,"Key Down:%d",keyCode);
2014-07-07 12:06:24 +08:00
holder->_externalKeyLabel->setString(ketStatus);
break;
}
2014-07-04 15:22:53 +08:00
}
}
else
2014-06-30 03:25:49 +08:00
{
2014-07-04 15:22:53 +08:00
switch (keyCode)
{
case Controller::Key::BUTTON_A:
holder->_buttonA->setColor(Color3B::WHITE);
break;
case Controller::Key::BUTTON_B:
holder->_buttonB->setColor(Color3B::WHITE);
break;
case Controller::Key::BUTTON_X:
holder->_buttonX->setColor(Color3B::WHITE);
break;
case Controller::Key::BUTTON_Y:
holder->_buttonY->setColor(Color3B::WHITE);
break;
case Controller::Key::BUTTON_DPAD_UP:
holder->_dpadUp->setColor(Color3B::WHITE);
break;
case Controller::Key::BUTTON_DPAD_DOWN:
holder->_dpadDown->setColor(Color3B::WHITE);
break;
case Controller::Key::BUTTON_DPAD_LEFT:
holder->_dpadLeft->setColor(Color3B::WHITE);
break;
case Controller::Key::BUTTON_DPAD_RIGHT:
holder->_dpadRight->setColor(Color3B::WHITE);
break;
case Controller::Key::BUTTON_LEFT_SHOULDER:
holder->_buttonL1->setColor(Color3B::WHITE);
break;
case Controller::Key::BUTTON_RIGHT_SHOULDER:
holder->_buttonR1->setColor(Color3B::WHITE);
break;
default:
2014-07-07 12:06:24 +08:00
{
char ketStatus[30];
sprintf(ketStatus,"Key Up:%d",keyCode);
2014-07-07 12:06:24 +08:00
holder->_externalKeyLabel->setString(ketStatus);
break;
}
2014-07-04 15:22:53 +08:00
}
2014-06-30 03:25:49 +08:00
}
2014-07-04 15:22:53 +08:00
}
2014-06-30 03:25:49 +08:00
2014-07-04 15:22:53 +08:00
void GameControllerTest::onKeyDown(cocos2d::Controller *controller, int keyCode, cocos2d::Event *event)
{
showButtonState(controller, keyCode, true);
}
2014-06-30 03:25:49 +08:00
2014-07-04 15:22:53 +08:00
void GameControllerTest::onKeyUp(cocos2d::Controller *controller, int keyCode, cocos2d::Event *event)
{
showButtonState(controller, keyCode, false);
2014-06-30 03:25:49 +08:00
}
2014-07-04 15:22:53 +08:00
void GameControllerTest::onAxisEvent(cocos2d::Controller* controller, int keyCode, cocos2d::Event* event)
2014-06-30 03:25:49 +08:00
{
2014-07-04 15:22:53 +08:00
ControllerHolder* holder = nullptr;
if (controller == _firstHolder.controller)
holder = &_firstHolder;
else if(controller == _secondHolder.controller)
holder = &_secondHolder;
else
2014-06-30 03:25:49 +08:00
return;
2014-07-04 15:22:53 +08:00
const auto& ketStatus = controller->getKeyStatus(keyCode);
switch (keyCode)
2014-06-30 03:25:49 +08:00
{
2014-07-04 15:22:53 +08:00
case Controller::Key::JOYSTICK_LEFT_X:
holder->_leftJoystick->setPositionX(238 + ketStatus.value * 24);
break;
case Controller::Key::JOYSTICK_LEFT_Y:
holder->_leftJoystick->setPositionY(460 - ketStatus.value * 24);
break;
case Controller::Key::JOYSTICK_RIGHT_X:
holder->_rightJoystick->setPositionX(606 + ketStatus.value * 24);
break;
case Controller::Key::JOYSTICK_RIGHT_Y:
holder->_rightJoystick->setPositionY(293 - ketStatus.value * 24);
break;
case Controller::Key::AXIS_LEFT_TRIGGER:
holder->_buttonL2->setOpacity(200 * controller->getKeyStatus(keyCode).value);
break;
case Controller::Key::AXIS_RIGHT_TRIGGER:
holder->_buttonR2->setOpacity(200 * controller->getKeyStatus(keyCode).value);
break;
default:
break;
2014-06-30 03:25:49 +08:00
}
}
2014-07-04 15:22:53 +08:00
bool GameControllerTest::init()
{
if ( !Layer::init() )
return false;
_currControllerCount = 0;
_visibleSize = Director::getInstance()->getVisibleSize();
_visibleOrigin = Director::getInstance()->getVisibleOrigin();
auto tmpPos = Vec2(_visibleSize / 2) + _visibleOrigin;
_visibleCentreX = _visibleOrigin.x + _visibleSize.width / 2;
_visibleCentreY = _visibleOrigin.y + _visibleSize.height / 2;
_visibleQuarterX = _visibleOrigin.x + _visibleSize.width / 4;
_visibleThreeQuarterX = _visibleOrigin.x + _visibleSize.width * 0.75f;
_firstHolder.controller = nullptr;
_secondHolder.controller = nullptr;
createControllerSprite(_firstHolder);
_firstHolder._holderNode->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
_firstHolder._holderNode->setPosition(tmpPos);
this->addChild(_firstHolder._holderNode);
auto closeItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(GameControllerTest::menuCloseCallback, this));
closeItem->setPosition(_visibleOrigin + _visibleSize - closeItem->getContentSize() / 2);
auto menu = Menu::create(closeItem,nullptr);
menu->setPosition(Vec2::ZERO);
this->addChild(menu);
2014-07-04 15:22:53 +08:00
registerControllerListener();
return true;
}
void GameControllerTest::createControllerSprite(ControllerHolder& holder)
{
holder._holderNode = Node::create();
holder._holderNode->setContentSize(Size(998,1000));
auto controllerBg1 = Sprite::create("controller-1.png");
controllerBg1->setPosition(Vec2::ZERO);
controllerBg1->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
holder._holderNode->addChild(controllerBg1);
auto controllerBg2 = Sprite::create("controller-2.png");
controllerBg2->setPosition(Vec2(499,1000));
controllerBg2->setAnchorPoint(Vec2::ANCHOR_MIDDLE_TOP);
holder._holderNode->addChild(controllerBg2);
holder._leftJoystick = Sprite::create("joystick.png");
holder._leftJoystick->setPosition(Vec2(238,460));
holder._holderNode->addChild(holder._leftJoystick);
holder._rightJoystick = Sprite::create("joystick.png");
holder._rightJoystick->setPosition(Vec2(606,293));
holder._holderNode->addChild(holder._rightJoystick);
2014-07-07 12:06:24 +08:00
holder._deviceLabel = Label::createWithTTF("Disconnected","fonts/Marker Felt.ttf",36);
2014-07-07 17:57:08 +08:00
holder._deviceLabel->setPosition(Vec2(499,650));
2014-07-07 12:06:24 +08:00
holder._deviceLabel->setTextColor(Color4B::RED);
holder._holderNode->addChild(holder._deviceLabel);
holder._externalKeyLabel = Label::createWithTTF("Key event","fonts/Marker Felt.ttf",36);
2014-07-07 12:06:24 +08:00
holder._externalKeyLabel->setPosition(Vec2(499,500));
holder._externalKeyLabel->setTextColor(Color4B::RED);
holder._holderNode->addChild(holder._externalKeyLabel);
2014-07-04 15:22:53 +08:00
//-----------------------------------------------------------------
auto dPadTexture = Director::getInstance()->getTextureCache()->addImage("dPad.png");
2014-07-04 15:22:53 +08:00
auto dPadCenter = Sprite::createWithTexture(dPadTexture,Rect(60,60,68,68));
dPadCenter->setPosition(Vec2(371,294));
holder._holderNode->addChild(dPadCenter);
holder._dpadLeft = Sprite::createWithTexture(dPadTexture,Rect(0,60,60,60));
holder._dpadLeft->setPosition(Vec2(371 - 64,296));
holder._holderNode->addChild(holder._dpadLeft);
holder._dpadRight = Sprite::createWithTexture(dPadTexture,Rect(128,60,60,60));
holder._dpadRight->setPosition(Vec2(371 + 64,296));
holder._holderNode->addChild(holder._dpadRight);
holder._dpadUp = Sprite::createWithTexture(dPadTexture,Rect(60,0,60,60));
holder._dpadUp->setPosition(Vec2(369,294 + 64));
holder._holderNode->addChild(holder._dpadUp);
holder._dpadDown = Sprite::createWithTexture(dPadTexture,Rect(60,128,60,60));
holder._dpadDown->setPosition(Vec2(369,294 - 64));
holder._holderNode->addChild(holder._dpadDown);
//-----------------------------------------------------------------
holder._buttonL1 = Sprite::create("L1.png");
holder._buttonL1->setPosition(Vec2(290,792));
holder._holderNode->addChild(holder._buttonL1);
holder._buttonR1 = Sprite::create("R1.png");
holder._buttonR1->setPosition(Vec2(998 - 290,792));
holder._holderNode->addChild(holder._buttonR1);
auto buttonL2 = Sprite::create("L2.png");
buttonL2->setPosition(Vec2(220,910));
holder._holderNode->addChild(buttonL2);
auto buttonR2 = Sprite::create("R2.png");
buttonR2->setPosition(Vec2(998-220,910));
holder._holderNode->addChild(buttonR2);
holder._buttonL2 = Sprite::create("L2.png");
holder._buttonL2->setOpacity(0);
holder._buttonL2->setColor(Color3B::RED);
holder._buttonL2->setPosition(Vec2(220,910));
holder._holderNode->addChild(holder._buttonL2);
holder._buttonR2 = Sprite::create("R2.png");
holder._buttonR2->setOpacity(0);
holder._buttonR2->setColor(Color3B::RED);
holder._buttonR2->setPosition(Vec2(998-220,910));
holder._holderNode->addChild(holder._buttonR2);
//-----------------------------------------------------------------
holder._buttonX = Sprite::create("X.png");
holder._buttonX->setPosition(Vec2(750 - 70,460));
holder._holderNode->addChild(holder._buttonX);
holder._buttonY = Sprite::create("Y.png");
holder._buttonY->setPosition(Vec2(750,460 + 70));
holder._holderNode->addChild(holder._buttonY);
holder._buttonA = Sprite::create("A.png");
holder._buttonA->setPosition(Vec2(750,460 - 70));
holder._holderNode->addChild(holder._buttonA);
holder._buttonB = Sprite::create("B.png");
holder._buttonB->setPosition(Vec2(750 + 70,460));
holder._holderNode->addChild(holder._buttonB);
2014-06-30 03:25:49 +08:00
}
void GameControllerTest::menuCloseCallback(Ref* sender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}