mirror of https://github.com/axmolengine/axmol.git
Refactoring game controller API
This commit is contained in:
parent
552a9251c4
commit
6d48cd9523
|
@ -141,6 +141,7 @@ base/ccUTF8.cpp \
|
||||||
base/ccUtils.cpp \
|
base/ccUtils.cpp \
|
||||||
base/etc1.cpp \
|
base/etc1.cpp \
|
||||||
base/s3tc.cpp \
|
base/s3tc.cpp \
|
||||||
|
base/CCController.cpp \
|
||||||
base/CCController-android.cpp \
|
base/CCController-android.cpp \
|
||||||
base/ObjectFactory.cpp \
|
base/ObjectFactory.cpp \
|
||||||
renderer/CCBatchCommand.cpp \
|
renderer/CCBatchCommand.cpp \
|
||||||
|
|
|
@ -26,40 +26,20 @@
|
||||||
#include "CCController.h"
|
#include "CCController.h"
|
||||||
|
|
||||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
||||||
|
#include <functional>
|
||||||
#include "ccMacros.h"
|
#include "ccMacros.h"
|
||||||
#include "CCEventDispatcher.h"
|
|
||||||
#include "CCEventController.h"
|
|
||||||
#include "CCEventListenerController.h"
|
|
||||||
#include "CCDirector.h"
|
#include "CCDirector.h"
|
||||||
|
|
||||||
#include "jni/JniHelper.h"
|
#include "jni/JniHelper.h"
|
||||||
|
|
||||||
|
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
|
||||||
static EventDispatcher* s_eventDispatcher = nullptr;
|
|
||||||
|
|
||||||
class ControllerImpl
|
class ControllerImpl
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ControllerImpl(Controller* controller)
|
ControllerImpl(Controller* controller)
|
||||||
: _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)
|
static std::vector<Controller*>::iterator findController(const std::string& deviceName, int deviceId)
|
||||||
|
@ -77,6 +57,7 @@ public:
|
||||||
auto iter = findController(deviceName, deviceId);
|
auto iter = findController(deviceName, deviceId);
|
||||||
if (iter != Controller::s_allController.end())
|
if (iter != Controller::s_allController.end())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
log("onConnected new device");
|
log("onConnected new device");
|
||||||
// It's a new controller being connected.
|
// It's a new controller being connected.
|
||||||
auto controller = new cocos2d::Controller();
|
auto controller = new cocos2d::Controller();
|
||||||
|
@ -84,8 +65,7 @@ public:
|
||||||
controller->_deviceName = deviceName;
|
controller->_deviceName = deviceName;
|
||||||
Controller::s_allController.push_back(controller);
|
Controller::s_allController.push_back(controller);
|
||||||
|
|
||||||
controller->_impl->_connectEvent->setConnectStatus(true);
|
controller->onConnected();
|
||||||
s_eventDispatcher->dispatchEvent(controller->_impl->_connectEvent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void onDisconnected(const std::string& deviceName, int deviceId)
|
static void onDisconnected(const std::string& deviceName, int deviceId)
|
||||||
|
@ -99,10 +79,7 @@ public:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
(*iter)->_impl->_connectEvent->setConnectStatus(false);
|
(*iter)->onDisconnected();
|
||||||
s_eventDispatcher->dispatchEvent((*iter)->_impl->_connectEvent);
|
|
||||||
|
|
||||||
delete (*iter);
|
|
||||||
Controller::s_allController.erase(iter);
|
Controller::s_allController.erase(iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,13 +94,7 @@ public:
|
||||||
iter = findController(deviceName, deviceId);
|
iter = findController(deviceName, deviceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
(*iter)->_allKeyPrevStatus[keyCode] = (*iter)->_allKeyStatus[keyCode];
|
(*iter)->onButtonEvent(keyCode, isPressed, value, isAnalog);
|
||||||
(*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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void onAxisEvent(const std::string& deviceName, int deviceId, int axisCode, float value, bool 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 = findController(deviceName, deviceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
(*iter)->_allKeyPrevStatus[axisCode] = (*iter)->_allKeyStatus[axisCode];
|
(*iter)->onAxisEvent(axisCode, value, isAnalog);
|
||||||
(*iter)->_allKeyStatus[axisCode].value = value;
|
|
||||||
(*iter)->_allKeyStatus[axisCode].isAnalog = isAnalog;
|
|
||||||
|
|
||||||
(*iter)->_impl->_axisEvent->setKeyCode(axisCode);
|
|
||||||
s_eventDispatcher->dispatchEvent((*iter)->_impl->_axisEvent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Controller* _controller;
|
Controller* _controller;
|
||||||
EventController *_connectEvent;
|
|
||||||
EventController *_keyEvent;
|
|
||||||
EventController *_axisEvent;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
std::vector<Controller*> Controller::s_allController;
|
|
||||||
|
|
||||||
void Controller::startDiscoveryController()
|
void Controller::startDiscoveryController()
|
||||||
{
|
{
|
||||||
// Empty implementation on Android
|
// Empty implementation on Android
|
||||||
|
@ -163,16 +123,13 @@ void Controller::stopDiscoveryController()
|
||||||
// Empty implementation on Android
|
// Empty implementation on Android
|
||||||
}
|
}
|
||||||
|
|
||||||
Controller* Controller::getControllerByTag(int tag)
|
Controller::~Controller()
|
||||||
{
|
{
|
||||||
for (auto controller:Controller::s_allController)
|
delete _impl;
|
||||||
{
|
|
||||||
if (controller->_controllerTag == tag)
|
delete _connectEvent;
|
||||||
{
|
delete _keyEvent;
|
||||||
return controller;
|
delete _axisEvent;
|
||||||
}
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& Controller::getDeviceName()
|
const std::string& Controller::getDeviceName()
|
||||||
|
@ -191,31 +148,11 @@ bool Controller::isConnected() const
|
||||||
Controller::Controller()
|
Controller::Controller()
|
||||||
: _controllerTag(TAG_UNSET)
|
: _controllerTag(TAG_UNSET)
|
||||||
, _impl(new ControllerImpl(this))
|
, _impl(new ControllerImpl(this))
|
||||||
|
, _connectEvent(nullptr)
|
||||||
|
, _keyEvent(nullptr)
|
||||||
|
, _axisEvent(nullptr)
|
||||||
{
|
{
|
||||||
for (int key = Key::JOYSTICK_LEFT_X; key < Key::KEY_MAX; ++key)
|
init();
|
||||||
{
|
|
||||||
_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];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
|
@ -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)
|
|
@ -27,16 +27,16 @@
|
||||||
#define __cocos2d_libs__CCController__
|
#define __cocos2d_libs__CCController__
|
||||||
|
|
||||||
#include "CCPlatformMacros.h"
|
#include "CCPlatformMacros.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <functional>
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
|
||||||
class ControllerImpl;
|
class ControllerImpl;
|
||||||
class EventListenerController;
|
class EventListenerController;
|
||||||
|
class EventController;
|
||||||
|
class EventDispatcher;
|
||||||
|
|
||||||
class Controller
|
class Controller
|
||||||
{
|
{
|
||||||
|
@ -73,6 +73,7 @@ public:
|
||||||
BUTTON_START,
|
BUTTON_START,
|
||||||
BUTTON_SELECT,
|
BUTTON_SELECT,
|
||||||
|
|
||||||
|
BUTTON_PAUSE,
|
||||||
KEY_MAX
|
KEY_MAX
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -107,6 +108,13 @@ private:
|
||||||
Controller();
|
Controller();
|
||||||
virtual ~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> _allKeyStatus;
|
||||||
std::unordered_map<int, KeyStatus> _allKeyPrevStatus;
|
std::unordered_map<int, KeyStatus> _allKeyPrevStatus;
|
||||||
|
|
||||||
|
@ -117,6 +125,11 @@ private:
|
||||||
|
|
||||||
ControllerImpl* _impl;
|
ControllerImpl* _impl;
|
||||||
|
|
||||||
|
EventDispatcher* _eventDispatcher;
|
||||||
|
EventController *_connectEvent;
|
||||||
|
EventController *_keyEvent;
|
||||||
|
EventController *_axisEvent;
|
||||||
|
|
||||||
friend class ControllerImpl;
|
friend class ControllerImpl;
|
||||||
friend class EventListenerController;
|
friend class EventListenerController;
|
||||||
};
|
};
|
||||||
|
|
|
@ -26,12 +26,6 @@
|
||||||
#ifndef __cocos2d_libs__CCGameController__
|
#ifndef __cocos2d_libs__CCGameController__
|
||||||
#define __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 "CCController.h"
|
||||||
#include "CCEventController.h"
|
#include "CCEventController.h"
|
||||||
#include "CCEventListenerController.h"
|
#include "CCEventListenerController.h"
|
||||||
|
|
Loading…
Reference in New Issue