2014-06-30 03:20:22 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 cocos2d-x.org
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2014-2017 Chukong Technologies Inc.
|
2014-06-30 03:20:22 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "base/CCController.h"
|
2014-06-30 03:20:22 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
2014-07-04 17:01:41 +08:00
|
|
|
#include <functional>
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "platform/android/jni/JniHelper.h"
|
2014-07-11 17:45:15 +08:00
|
|
|
#include "base/CCEventController.h"
|
2014-06-30 03:20:22 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class ControllerImpl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ControllerImpl(Controller* controller)
|
|
|
|
: _controller(controller)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-04 15:22:53 +08:00
|
|
|
static std::vector<Controller*>::iterator findController(const std::string& deviceName, int deviceId)
|
2014-06-30 03:20:22 +08:00
|
|
|
{
|
2014-07-04 15:22:53 +08:00
|
|
|
auto iter = std::find_if(Controller::s_allController.begin(), Controller::s_allController.end(), [&](Controller* controller){
|
|
|
|
return (deviceName == controller->_deviceName) && (deviceId == controller->_deviceId);
|
2014-06-30 03:20:22 +08:00
|
|
|
});
|
2014-07-07 15:04:29 +08:00
|
|
|
|
2014-07-07 12:06:24 +08:00
|
|
|
return iter;
|
2014-06-30 03:20:22 +08:00
|
|
|
}
|
|
|
|
|
2014-07-04 15:22:53 +08:00
|
|
|
static void onConnected(const std::string& deviceName, int deviceId)
|
2014-06-30 03:20:22 +08:00
|
|
|
{
|
|
|
|
// Check whether the controller is already connected.
|
2014-07-07 15:04:29 +08:00
|
|
|
CCLOG("onConnected %s,%d", deviceName.c_str(),deviceId);
|
2014-06-30 03:20:22 +08:00
|
|
|
|
2014-07-04 15:22:53 +08:00
|
|
|
auto iter = findController(deviceName, deviceId);
|
|
|
|
if (iter != Controller::s_allController.end())
|
|
|
|
return;
|
2014-07-04 17:01:41 +08:00
|
|
|
|
2014-06-30 03:20:22 +08:00
|
|
|
// It's a new controller being connected.
|
|
|
|
auto controller = new cocos2d::Controller();
|
2014-07-04 15:22:53 +08:00
|
|
|
controller->_deviceId = deviceId;
|
|
|
|
controller->_deviceName = deviceName;
|
|
|
|
Controller::s_allController.push_back(controller);
|
|
|
|
|
2014-07-04 17:01:41 +08:00
|
|
|
controller->onConnected();
|
2014-06-30 03:20:22 +08:00
|
|
|
}
|
|
|
|
|
2014-07-04 15:22:53 +08:00
|
|
|
static void onDisconnected(const std::string& deviceName, int deviceId)
|
2014-06-30 03:20:22 +08:00
|
|
|
{
|
2014-07-07 15:04:29 +08:00
|
|
|
CCLOG("onDisconnected %s,%d", deviceName.c_str(),deviceId);
|
|
|
|
|
2014-07-04 15:22:53 +08:00
|
|
|
auto iter = findController(deviceName, deviceId);
|
|
|
|
if (iter == Controller::s_allController.end())
|
2014-06-30 03:20:22 +08:00
|
|
|
{
|
|
|
|
CCLOGERROR("Could not find the controller!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-04 17:01:41 +08:00
|
|
|
(*iter)->onDisconnected();
|
2014-07-04 15:22:53 +08:00
|
|
|
Controller::s_allController.erase(iter);
|
2014-06-30 03:20:22 +08:00
|
|
|
}
|
|
|
|
|
2014-07-04 15:22:53 +08:00
|
|
|
static void onButtonEvent(const std::string& deviceName, int deviceId, int keyCode, bool isPressed, float value, bool isAnalog)
|
2014-06-30 03:20:22 +08:00
|
|
|
{
|
2014-07-04 15:22:53 +08:00
|
|
|
auto iter = findController(deviceName, deviceId);
|
|
|
|
if (iter == Controller::s_allController.end())
|
2014-06-30 03:20:22 +08:00
|
|
|
{
|
2014-07-07 15:04:29 +08:00
|
|
|
CCLOG("onButtonEvent:connect new controller.");
|
2014-07-04 15:22:53 +08:00
|
|
|
onConnected(deviceName, deviceId);
|
|
|
|
iter = findController(deviceName, deviceId);
|
2014-06-30 03:20:22 +08:00
|
|
|
}
|
|
|
|
|
2014-07-04 17:01:41 +08:00
|
|
|
(*iter)->onButtonEvent(keyCode, isPressed, value, isAnalog);
|
2014-06-30 03:20:22 +08:00
|
|
|
}
|
|
|
|
|
2014-07-04 15:22:53 +08:00
|
|
|
static void onAxisEvent(const std::string& deviceName, int deviceId, int axisCode, float value, bool isAnalog)
|
2014-06-30 03:20:22 +08:00
|
|
|
{
|
2014-07-04 15:22:53 +08:00
|
|
|
auto iter = findController(deviceName, deviceId);
|
|
|
|
if (iter == Controller::s_allController.end())
|
2014-06-30 03:20:22 +08:00
|
|
|
{
|
2014-07-07 15:04:29 +08:00
|
|
|
CCLOG("onAxisEvent:connect new controller.");
|
2014-07-04 15:22:53 +08:00
|
|
|
onConnected(deviceName, deviceId);
|
|
|
|
iter = findController(deviceName, deviceId);
|
2014-06-30 03:20:22 +08:00
|
|
|
}
|
2014-07-07 12:06:24 +08:00
|
|
|
|
2014-07-04 17:01:41 +08:00
|
|
|
(*iter)->onAxisEvent(axisCode, value, isAnalog);
|
2014-06-30 03:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Controller* _controller;
|
|
|
|
};
|
|
|
|
|
|
|
|
void Controller::startDiscoveryController()
|
|
|
|
{
|
|
|
|
// Empty implementation on Android
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::stopDiscoveryController()
|
|
|
|
{
|
|
|
|
// Empty implementation on Android
|
|
|
|
}
|
|
|
|
|
2014-07-04 17:01:41 +08:00
|
|
|
Controller::~Controller()
|
2014-07-04 15:22:53 +08:00
|
|
|
{
|
2014-07-04 17:01:41 +08:00
|
|
|
delete _impl;
|
|
|
|
|
|
|
|
delete _connectEvent;
|
|
|
|
delete _keyEvent;
|
|
|
|
delete _axisEvent;
|
2014-07-04 15:22:53 +08:00
|
|
|
}
|
|
|
|
|
2014-07-04 21:29:30 +08:00
|
|
|
void Controller::registerListeners()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-30 03:20:22 +08:00
|
|
|
bool Controller::isConnected() const
|
|
|
|
{
|
|
|
|
// If there is a controller instance, it means that the controller is connected.
|
|
|
|
// If a controller is disconnected, the instance will be destroyed.
|
|
|
|
// So always returns true for this method.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-04 15:22:53 +08:00
|
|
|
Controller::Controller()
|
|
|
|
: _controllerTag(TAG_UNSET)
|
|
|
|
, _impl(new ControllerImpl(this))
|
2014-07-04 17:01:41 +08:00
|
|
|
, _connectEvent(nullptr)
|
|
|
|
, _keyEvent(nullptr)
|
|
|
|
, _axisEvent(nullptr)
|
2014-06-30 03:20:22 +08:00
|
|
|
{
|
2014-07-04 17:01:41 +08:00
|
|
|
init();
|
2014-06-30 03:20:22 +08:00
|
|
|
}
|
|
|
|
|
2015-11-27 17:00:33 +08:00
|
|
|
void Controller::receiveExternalKeyEvent(int externalKeyCode,bool receive) {
|
|
|
|
JniHelper::callStaticVoidMethod("org/cocos2dx/lib/GameControllerHelper", "receiveExternalKeyEvent", _deviceId, externalKeyCode, receive);
|
2014-07-07 12:06:24 +08:00
|
|
|
}
|
|
|
|
|
2014-06-30 03:20:22 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2014-07-04 15:22:53 +08:00
|
|
|
void Java_org_cocos2dx_lib_GameControllerAdapter_nativeControllerConnected(JNIEnv* env, jobject thiz, jstring deviceName, jint controllerID)
|
2014-06-30 03:20:22 +08:00
|
|
|
{
|
|
|
|
CCLOG("controller id: %d connected!", controllerID);
|
2014-07-04 15:22:53 +08:00
|
|
|
cocos2d::ControllerImpl::onConnected(cocos2d::JniHelper::jstring2string(deviceName), controllerID);
|
2014-06-30 03:20:22 +08:00
|
|
|
}
|
|
|
|
|
2014-07-04 15:22:53 +08:00
|
|
|
void Java_org_cocos2dx_lib_GameControllerAdapter_nativeControllerDisconnected(JNIEnv* env, jobject thiz, jstring deviceName, jint controllerID)
|
2014-06-30 03:20:22 +08:00
|
|
|
{
|
|
|
|
CCLOG("controller id: %d disconnected!", controllerID);
|
2014-07-04 15:22:53 +08:00
|
|
|
cocos2d::ControllerImpl::onDisconnected(cocos2d::JniHelper::jstring2string(deviceName), controllerID);
|
2014-06-30 03:20:22 +08:00
|
|
|
}
|
|
|
|
|
2014-07-04 15:22:53 +08:00
|
|
|
void Java_org_cocos2dx_lib_GameControllerAdapter_nativeControllerButtonEvent(JNIEnv* env, jobject thiz, jstring deviceName, jint controllerID, jint button, jboolean isPressed, jfloat value, jboolean isAnalog)
|
2014-06-30 03:20:22 +08:00
|
|
|
{
|
2014-07-04 15:22:53 +08:00
|
|
|
cocos2d::ControllerImpl::onButtonEvent(cocos2d::JniHelper::jstring2string(deviceName), controllerID, button, isPressed, value, isAnalog);
|
2014-06-30 03:20:22 +08:00
|
|
|
}
|
|
|
|
|
2014-07-04 15:22:53 +08:00
|
|
|
void Java_org_cocos2dx_lib_GameControllerAdapter_nativeControllerAxisEvent(JNIEnv* env, jobject thiz, jstring deviceName, jint controllerID, jint axis, jfloat value, jboolean isAnalog)
|
2014-06-30 03:20:22 +08:00
|
|
|
{
|
2014-07-04 15:22:53 +08:00
|
|
|
cocos2d::ControllerImpl::onAxisEvent(cocos2d::JniHelper::jstring2string(deviceName), controllerID, axis, value, isAnalog);
|
2014-06-30 03:20:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // extern "C" {
|
|
|
|
|
|
|
|
|
|
|
|
#endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|