Refactoring game controller API

This commit is contained in:
Dhilan007 2014-07-04 17:01:41 +08:00
parent 552a9251c4
commit 6d48cd9523
5 changed files with 149 additions and 87 deletions

View File

@ -141,6 +141,7 @@ base/ccUTF8.cpp \
base/ccUtils.cpp \
base/etc1.cpp \
base/s3tc.cpp \
base/CCController.cpp \
base/CCController-android.cpp \
base/ObjectFactory.cpp \
renderer/CCBatchCommand.cpp \

View File

@ -26,40 +26,20 @@
#include "CCController.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <functional>
#include "ccMacros.h"
#include "CCEventDispatcher.h"
#include "CCEventController.h"
#include "CCEventListenerController.h"
#include "CCDirector.h"
#include "jni/JniHelper.h"
NS_CC_BEGIN
static EventDispatcher* s_eventDispatcher = nullptr;
class ControllerImpl
{
public:
ControllerImpl(Controller* controller)
: _controller(controller)
, _connectEvent()
{
if (s_eventDispatcher == nullptr)
{
s_eventDispatcher = Director::getInstance()->getEventDispatcher();
}
_connectEvent = new EventController(EventController::ControllerEventType::CONNECTION, controller, false);
_keyEvent = new EventController(EventController::ControllerEventType::BUTTON_STATUS_CHANGED, controller, 0);
_axisEvent = new EventController(EventController::ControllerEventType::AXIS_STATUS_CHANGED, controller, 0);
}
~ControllerImpl()
{
delete _connectEvent;
delete _keyEvent;
delete _axisEvent;
}
static std::vector<Controller*>::iterator findController(const std::string& deviceName, int deviceId)
@ -77,6 +57,7 @@ public:
auto iter = findController(deviceName, deviceId);
if (iter != Controller::s_allController.end())
return;
log("onConnected new device");
// It's a new controller being connected.
auto controller = new cocos2d::Controller();
@ -84,8 +65,7 @@ public:
controller->_deviceName = deviceName;
Controller::s_allController.push_back(controller);
controller->_impl->_connectEvent->setConnectStatus(true);
s_eventDispatcher->dispatchEvent(controller->_impl->_connectEvent);
controller->onConnected();
}
static void onDisconnected(const std::string& deviceName, int deviceId)
@ -99,10 +79,7 @@ public:
return;
}
(*iter)->_impl->_connectEvent->setConnectStatus(false);
s_eventDispatcher->dispatchEvent((*iter)->_impl->_connectEvent);
delete (*iter);
(*iter)->onDisconnected();
Controller::s_allController.erase(iter);
}
@ -117,13 +94,7 @@ public:
iter = findController(deviceName, deviceId);
}
(*iter)->_allKeyPrevStatus[keyCode] = (*iter)->_allKeyStatus[keyCode];
(*iter)->_allKeyStatus[keyCode].isPressed = isPressed;
(*iter)->_allKeyStatus[keyCode].value = value;
(*iter)->_allKeyStatus[keyCode].isAnalog = isAnalog;
(*iter)->_impl->_keyEvent->setKeyCode(keyCode);
s_eventDispatcher->dispatchEvent((*iter)->_impl->_keyEvent);
(*iter)->onButtonEvent(keyCode, isPressed, value, isAnalog);
}
static void onAxisEvent(const std::string& deviceName, int deviceId, int axisCode, float value, bool isAnalog)
@ -135,24 +106,13 @@ public:
iter = findController(deviceName, deviceId);
}
(*iter)->_allKeyPrevStatus[axisCode] = (*iter)->_allKeyStatus[axisCode];
(*iter)->_allKeyStatus[axisCode].value = value;
(*iter)->_allKeyStatus[axisCode].isAnalog = isAnalog;
(*iter)->_impl->_axisEvent->setKeyCode(axisCode);
s_eventDispatcher->dispatchEvent((*iter)->_impl->_axisEvent);
(*iter)->onAxisEvent(axisCode, value, isAnalog);
}
private:
Controller* _controller;
EventController *_connectEvent;
EventController *_keyEvent;
EventController *_axisEvent;
};
std::vector<Controller*> Controller::s_allController;
void Controller::startDiscoveryController()
{
// Empty implementation on Android
@ -163,16 +123,13 @@ void Controller::stopDiscoveryController()
// Empty implementation on Android
}
Controller* Controller::getControllerByTag(int tag)
Controller::~Controller()
{
for (auto controller:Controller::s_allController)
{
if (controller->_controllerTag == tag)
{
return controller;
}
}
return nullptr;
delete _impl;
delete _connectEvent;
delete _keyEvent;
delete _axisEvent;
}
const std::string& Controller::getDeviceName()
@ -191,31 +148,11 @@ bool Controller::isConnected() const
Controller::Controller()
: _controllerTag(TAG_UNSET)
, _impl(new ControllerImpl(this))
, _connectEvent(nullptr)
, _keyEvent(nullptr)
, _axisEvent(nullptr)
{
for (int key = Key::JOYSTICK_LEFT_X; key < Key::KEY_MAX; ++key)
{
_allKeyStatus[key].isPressed = false;
_allKeyStatus[key].value = 0.0f;
_allKeyPrevStatus[key].isPressed = false;
_allKeyPrevStatus[key].value = 0.0f;
}
}
Controller::~Controller()
{
CC_SAFE_DELETE(_impl);
}
const Controller::KeyStatus& Controller::getKeyStatus(int keyCode)
{
if (_allKeyStatus.find(keyCode) == _allKeyStatus.end())
{
_allKeyStatus[keyCode].isPressed = false;
_allKeyStatus[keyCode].value = 0.0f;
}
return _allKeyStatus[keyCode];
init();
}
NS_CC_END

117
cocos/base/CCController.cpp Normal file
View File

@ -0,0 +1,117 @@
/****************************************************************************
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"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "ccMacros.h"
#include "CCEventDispatcher.h"
#include "CCEventController.h"
#include "CCEventListenerController.h"
#include "CCDirector.h"
NS_CC_BEGIN
std::vector<Controller*> Controller::s_allController;
Controller* Controller::getControllerByTag(int tag)
{
for (auto controller:Controller::s_allController)
{
if (controller->_controllerTag == tag)
{
return controller;
}
}
return nullptr;
}
void Controller::init()
{
for (int key = Key::JOYSTICK_LEFT_X; key < Key::KEY_MAX; ++key)
{
_allKeyStatus[key].isPressed = false;
_allKeyStatus[key].value = 0.0f;
_allKeyPrevStatus[key].isPressed = false;
_allKeyPrevStatus[key].value = 0.0f;
}
_eventDispatcher = Director::getInstance()->getEventDispatcher();
_connectEvent = new EventController(EventController::ControllerEventType::CONNECTION, this, false);
_keyEvent = new EventController(EventController::ControllerEventType::BUTTON_STATUS_CHANGED, this, 0);
_axisEvent = new EventController(EventController::ControllerEventType::AXIS_STATUS_CHANGED, this, 0);
}
const Controller::KeyStatus& Controller::getKeyStatus(int keyCode)
{
if (_allKeyStatus.find(keyCode) == _allKeyStatus.end())
{
_allKeyStatus[keyCode].isPressed = false;
_allKeyStatus[keyCode].value = 0.0f;
}
return _allKeyStatus[keyCode];
}
void Controller::onConnected()
{
_connectEvent->setConnectStatus(true);
_eventDispatcher->dispatchEvent(_connectEvent);
}
void Controller::onDisconnected()
{
_connectEvent->setConnectStatus(false);
_eventDispatcher->dispatchEvent(_connectEvent);
delete this;
}
void Controller::onButtonEvent(int keyCode, bool isPressed, float value, bool isAnalog)
{
_allKeyPrevStatus[keyCode] = _allKeyStatus[keyCode];
_allKeyStatus[keyCode].isPressed = isPressed;
_allKeyStatus[keyCode].value = value;
_allKeyStatus[keyCode].isAnalog = isAnalog;
_keyEvent->setKeyCode(keyCode);
_eventDispatcher->dispatchEvent(_keyEvent);
}
void Controller::onAxisEvent(int axisCode, float value, bool isAnalog)
{
_allKeyPrevStatus[axisCode] = _allKeyStatus[axisCode];
_allKeyStatus[axisCode].value = value;
_allKeyStatus[axisCode].isAnalog = isAnalog;
_axisEvent->setKeyCode(axisCode);
_eventDispatcher->dispatchEvent(_axisEvent);
}
NS_CC_END
#endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

View File

@ -27,16 +27,16 @@
#define __cocos2d_libs__CCController__
#include "CCPlatformMacros.h"
#include <string>
#include <vector>
#include <functional>
#include <unordered_map>
NS_CC_BEGIN
class ControllerImpl;
class EventListenerController;
class EventController;
class EventDispatcher;
class Controller
{
@ -73,6 +73,7 @@ public:
BUTTON_START,
BUTTON_SELECT,
BUTTON_PAUSE,
KEY_MAX
};
@ -107,6 +108,13 @@ private:
Controller();
virtual ~Controller();
void init();
void onConnected();
void onDisconnected();
void onButtonEvent(int keyCode, bool isPressed, float value, bool isAnalog);
void onAxisEvent(int axisCode, float value, bool isAnalog);
std::unordered_map<int, KeyStatus> _allKeyStatus;
std::unordered_map<int, KeyStatus> _allKeyPrevStatus;
@ -117,6 +125,11 @@ private:
ControllerImpl* _impl;
EventDispatcher* _eventDispatcher;
EventController *_connectEvent;
EventController *_keyEvent;
EventController *_axisEvent;
friend class ControllerImpl;
friend class EventListenerController;
};

View File

@ -26,12 +26,6 @@
#ifndef __cocos2d_libs__CCGameController__
#define __cocos2d_libs__CCGameController__
#include "CCControllerElement.h"
#include "CCControllerButtonInput.h"
#include "CCControllerAxisInput.h"
#include "CCControllerDirectionPad.h"
#include "CCControllerThumbstick.h"
#include "CCGamepad.h"
#include "CCController.h"
#include "CCEventController.h"
#include "CCEventListenerController.h"