axmol/core/base/CCController-android.cpp

216 lines
8.5 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2014 cocos2d-x.org
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
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 "base/CCController.h"
2022-07-15 19:17:01 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
2021-12-25 10:04:45 +08:00
# include <functional>
# include "base/ccMacros.h"
# include "base/CCDirector.h"
# include "platform/android/jni/JniHelper.h"
# include "base/CCEventController.h"
NS_AX_BEGIN
class ControllerImpl
{
public:
2021-12-25 10:04:45 +08:00
ControllerImpl(Controller* controller) : _controller(controller) {}
2021-12-26 23:26:34 +08:00
static std::vector<Controller*>::iterator findController(std::string_view deviceName, int deviceId)
{
2021-12-25 10:04:45 +08:00
auto iter = std::find_if(
Controller::s_allController.begin(), Controller::s_allController.end(), [&](Controller* controller) {
2014-07-04 15:22:53 +08:00
return (deviceName == controller->_deviceName) && (deviceId == controller->_deviceId);
});
2014-07-07 15:04:29 +08:00
2014-07-07 12:06:24 +08:00
return iter;
}
2021-12-26 23:26:34 +08:00
static void onConnected(std::string_view deviceName, int deviceId)
{
// Check whether the controller is already connected.
2022-07-16 10:43:05 +08:00
AXLOG("onConnected %s,%d", deviceName.data(), deviceId);
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
// It's a new controller being connected.
2022-08-08 18:02:17 +08:00
auto controller = new ax::Controller();
2021-12-25 10:04:45 +08:00
controller->_deviceId = deviceId;
2014-07-04 15:22:53 +08:00
controller->_deviceName = deviceName;
Controller::s_allController.emplace_back(controller);
2014-07-04 15:22:53 +08:00
2014-07-04 17:01:41 +08:00
controller->onConnected();
}
2021-12-26 23:26:34 +08:00
static void onDisconnected(std::string_view deviceName, int deviceId)
{
2022-07-16 10:43:05 +08:00
AXLOG("onDisconnected %s,%d", deviceName.data(), deviceId);
2014-07-07 15:04:29 +08:00
2014-07-04 15:22:53 +08:00
auto iter = findController(deviceName, deviceId);
if (iter == Controller::s_allController.end())
{
2022-07-16 10:43:05 +08:00
AXLOGERROR("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);
}
2021-12-26 23:26:34 +08:00
static void onButtonEvent(std::string_view deviceName,
2021-12-25 10:04:45 +08:00
int deviceId,
int keyCode,
bool isPressed,
float value,
bool isAnalog)
{
2014-07-04 15:22:53 +08:00
auto iter = findController(deviceName, deviceId);
if (iter == Controller::s_allController.end())
{
2022-07-16 10:43:05 +08:00
AXLOG("onButtonEvent:connect new controller.");
2014-07-04 15:22:53 +08:00
onConnected(deviceName, deviceId);
iter = findController(deviceName, deviceId);
}
2014-07-04 17:01:41 +08:00
(*iter)->onButtonEvent(keyCode, isPressed, value, isAnalog);
}
2021-12-26 23:26:34 +08:00
static void onAxisEvent(std::string_view deviceName, int deviceId, int axisCode, float value, bool isAnalog)
{
2014-07-04 15:22:53 +08:00
auto iter = findController(deviceName, deviceId);
if (iter == Controller::s_allController.end())
{
2022-07-16 10:43:05 +08:00
AXLOG("onAxisEvent:connect new controller.");
2014-07-04 15:22:53 +08:00
onConnected(deviceName, deviceId);
iter = findController(deviceName, deviceId);
}
2021-12-25 10:04:45 +08:00
2014-07-04 17:01:41 +08:00
(*iter)->onAxisEvent(axisCode, value, isAnalog);
}
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
}
2021-12-25 10:04:45 +08:00
void Controller::registerListeners() {}
2014-07-04 21:29:30 +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-07-04 17:01:41 +08:00
init();
}
2021-12-25 10:04:45 +08:00
void Controller::receiveExternalKeyEvent(int externalKeyCode, bool receive)
{
2022-10-01 16:24:52 +08:00
JniHelper::callStaticVoidMethod("org.axmol.lib.GameControllerHelper", "receiveExternalKeyEvent", _deviceId,
2021-12-25 10:04:45 +08:00
externalKeyCode, receive);
2014-07-07 12:06:24 +08:00
}
NS_AX_END
extern "C" {
2022-10-01 16:24:52 +08:00
JNIEXPORT void JNICALL Java_org_axmol_lib_GameControllerAdapter_nativeControllerConnected(JNIEnv*,
2021-12-25 10:04:45 +08:00
jclass,
jstring deviceName,
jint controllerID)
{
2022-07-16 10:43:05 +08:00
AXLOG("controller id: %d connected!", controllerID);
2022-08-08 18:02:17 +08:00
ax::ControllerImpl::onConnected(ax::JniHelper::jstring2string(deviceName), controllerID);
2021-12-25 10:04:45 +08:00
}
2022-10-01 16:24:52 +08:00
JNIEXPORT void JNICALL Java_org_axmol_lib_GameControllerAdapter_nativeControllerDisconnected(JNIEnv*,
2021-12-25 10:04:45 +08:00
jclass,
jstring deviceName,
jint controllerID)
{
2022-07-16 10:43:05 +08:00
AXLOG("controller id: %d disconnected!", controllerID);
2022-08-08 18:02:17 +08:00
ax::ControllerImpl::onDisconnected(ax::JniHelper::jstring2string(deviceName), controllerID);
2021-12-25 10:04:45 +08:00
}
2022-10-01 16:24:52 +08:00
JNIEXPORT void JNICALL Java_org_axmol_lib_GameControllerAdapter_nativeControllerButtonEvent(JNIEnv*,
2021-12-25 10:04:45 +08:00
jclass,
jstring deviceName,
jint controllerID,
jint button,
jboolean isPressed,
jfloat value,
jboolean isAnalog)
{
2022-08-08 18:02:17 +08:00
ax::ControllerImpl::onButtonEvent(ax::JniHelper::jstring2string(deviceName), controllerID, button,
2021-12-25 10:04:45 +08:00
isPressed, value, isAnalog);
}
2022-10-01 16:24:52 +08:00
JNIEXPORT void JNICALL Java_org_axmol_lib_GameControllerAdapter_nativeControllerAxisEvent(JNIEnv*,
2021-12-25 10:04:45 +08:00
jclass,
jstring deviceName,
jint controllerID,
jint axis,
jfloat value,
jboolean isAnalog)
{
2022-08-08 18:02:17 +08:00
ax::ControllerImpl::onAxisEvent(ax::JniHelper::jstring2string(deviceName), controllerID, axis, value,
2021-12-25 10:04:45 +08:00
isAnalog);
}
2021-12-25 10:04:45 +08:00
} // extern "C" {
2022-07-15 19:17:01 +08:00
#endif // #if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)