mirror of https://github.com/axmolengine/axmol.git
Merge pull request #7326 from Dhilan007/v3-gc-refactor
issue #4689: support external key.
This commit is contained in:
commit
cd77690032
|
@ -47,18 +47,19 @@ public:
|
||||||
auto iter = std::find_if(Controller::s_allController.begin(), Controller::s_allController.end(), [&](Controller* controller){
|
auto iter = std::find_if(Controller::s_allController.begin(), Controller::s_allController.end(), [&](Controller* controller){
|
||||||
return (deviceName == controller->_deviceName) && (deviceId == controller->_deviceId);
|
return (deviceName == controller->_deviceName) && (deviceId == controller->_deviceId);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return iter;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void onConnected(const std::string& deviceName, int deviceId)
|
static void onConnected(const std::string& deviceName, int deviceId)
|
||||||
{
|
{
|
||||||
// Check whether the controller is already connected.
|
// Check whether the controller is already connected.
|
||||||
log("onConnected %s,%d", deviceName.c_str(),deviceId);
|
CCLOG("onConnected %s,%d", deviceName.c_str(),deviceId);
|
||||||
|
|
||||||
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");
|
|
||||||
// It's a new controller being connected.
|
// It's a new controller being connected.
|
||||||
auto controller = new cocos2d::Controller();
|
auto controller = new cocos2d::Controller();
|
||||||
controller->_deviceId = deviceId;
|
controller->_deviceId = deviceId;
|
||||||
|
@ -70,11 +71,11 @@ public:
|
||||||
|
|
||||||
static void onDisconnected(const std::string& deviceName, int deviceId)
|
static void onDisconnected(const std::string& deviceName, int deviceId)
|
||||||
{
|
{
|
||||||
log("onDisconnected %s,%d", deviceName.c_str(),deviceId);
|
CCLOG("onDisconnected %s,%d", deviceName.c_str(),deviceId);
|
||||||
|
|
||||||
auto iter = findController(deviceName, deviceId);
|
auto iter = findController(deviceName, deviceId);
|
||||||
if (iter == Controller::s_allController.end())
|
if (iter == Controller::s_allController.end())
|
||||||
{
|
{
|
||||||
log("Could not find the controller!");
|
|
||||||
CCLOGERROR("Could not find the controller!");
|
CCLOGERROR("Could not find the controller!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -85,11 +86,10 @@ public:
|
||||||
|
|
||||||
static void onButtonEvent(const std::string& deviceName, int deviceId, int keyCode, bool isPressed, float value, bool isAnalog)
|
static void onButtonEvent(const std::string& deviceName, int deviceId, int keyCode, bool isPressed, float value, bool isAnalog)
|
||||||
{
|
{
|
||||||
log("onButtonEvent %s,%d", deviceName.c_str(),deviceId);
|
|
||||||
auto iter = findController(deviceName, deviceId);
|
auto iter = findController(deviceName, deviceId);
|
||||||
if (iter == Controller::s_allController.end())
|
if (iter == Controller::s_allController.end())
|
||||||
{
|
{
|
||||||
log("onButtonEvent new connect");
|
CCLOG("onButtonEvent:connect new controller.");
|
||||||
onConnected(deviceName, deviceId);
|
onConnected(deviceName, deviceId);
|
||||||
iter = findController(deviceName, deviceId);
|
iter = findController(deviceName, deviceId);
|
||||||
}
|
}
|
||||||
|
@ -102,6 +102,7 @@ public:
|
||||||
auto iter = findController(deviceName, deviceId);
|
auto iter = findController(deviceName, deviceId);
|
||||||
if (iter == Controller::s_allController.end())
|
if (iter == Controller::s_allController.end())
|
||||||
{
|
{
|
||||||
|
CCLOG("onAxisEvent:connect new controller.");
|
||||||
onConnected(deviceName, deviceId);
|
onConnected(deviceName, deviceId);
|
||||||
iter = findController(deviceName, deviceId);
|
iter = findController(deviceName, deviceId);
|
||||||
}
|
}
|
||||||
|
@ -154,6 +155,16 @@ Controller::Controller()
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Controller::receiveExternalKeyEvent(int externalKeyCode,bool receive)
|
||||||
|
{
|
||||||
|
JniMethodInfo t;
|
||||||
|
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/GameControllerHelper", "receiveExternalKeyEvent", "(IIZ)V")) {
|
||||||
|
|
||||||
|
t.env->CallStaticVoidMethod(t.classID, t.methodID, _deviceId, externalKeyCode, receive);
|
||||||
|
t.env->DeleteLocalRef(t.classID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
|
@ -311,6 +311,10 @@ bool Controller::isConnected() const
|
||||||
return _impl->_gcController.isAttachedToDevice == YES;
|
return _impl->_gcController.isAttachedToDevice == YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Controller::receiveExternalKeyEvent(int externalKeyCode,bool receive)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
||||||
#endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
#endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
||||||
|
|
|
@ -99,6 +99,9 @@ public:
|
||||||
|
|
||||||
const KeyStatus& getKeyStatus(int keyCode);
|
const KeyStatus& getKeyStatus(int keyCode);
|
||||||
|
|
||||||
|
//Setting up receives external key which not contained within enum Key.
|
||||||
|
void receiveExternalKeyEvent(int externalKeyCode,bool receive);
|
||||||
|
|
||||||
void setTag(int tag) { _controllerTag = tag;}
|
void setTag(int tag) { _controllerTag = tag;}
|
||||||
int getTag() const { return _controllerTag;}
|
int getTag() const { return _controllerTag;}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package org.cocos2dx.lib;
|
package org.cocos2dx.lib;
|
||||||
|
|
||||||
import android.util.Log;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import android.util.SparseArray;
|
||||||
import android.util.SparseIntArray;
|
import android.util.SparseIntArray;
|
||||||
import android.view.InputDevice;
|
import android.view.InputDevice;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
|
@ -147,6 +149,8 @@ public class GameControllerHelper {
|
||||||
return handled;
|
return handled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static SparseArray<ArrayList<Integer>> mControllerExtendKey = new SparseArray<ArrayList<Integer>>();
|
||||||
|
|
||||||
public boolean dispatchKeyEvent(KeyEvent event) {
|
public boolean dispatchKeyEvent(KeyEvent event) {
|
||||||
boolean handled = false;
|
boolean handled = false;
|
||||||
|
|
||||||
|
@ -154,9 +158,18 @@ public class GameControllerHelper {
|
||||||
int keyCode = event.getKeyCode();
|
int keyCode = event.getKeyCode();
|
||||||
int controllerKey = ControllerKeyMap.get(keyCode);
|
int controllerKey = ControllerKeyMap.get(keyCode);
|
||||||
|
|
||||||
if (controllerKey != 0 && (((eventSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
|
if (((eventSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
|
||||||
|| ((eventSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) ))
|
|| ((eventSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) )
|
||||||
{
|
{
|
||||||
|
int deviceId = event.getDeviceId();
|
||||||
|
if (controllerKey == 0) {
|
||||||
|
if (mControllerExtendKey.get(deviceId) != null && mControllerExtendKey.get(deviceId).contains(keyCode)) {
|
||||||
|
controllerKey = keyCode;
|
||||||
|
}else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int action = event.getAction();
|
int action = event.getAction();
|
||||||
if (action == KeyEvent.ACTION_DOWN) {
|
if (action == KeyEvent.ACTION_DOWN) {
|
||||||
handled = true;
|
handled = true;
|
||||||
|
@ -169,4 +182,17 @@ public class GameControllerHelper {
|
||||||
|
|
||||||
return handled;
|
return handled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void receiveExternalKeyEvent(int deviceId,int externalKeyCode,boolean receive) {
|
||||||
|
if (receive) {
|
||||||
|
if (mControllerExtendKey.get(deviceId) == null) {
|
||||||
|
mControllerExtendKey.put(deviceId, new ArrayList<Integer>());
|
||||||
|
}
|
||||||
|
mControllerExtendKey.get(deviceId).add(externalKeyCode);
|
||||||
|
} else {
|
||||||
|
if (mControllerExtendKey.get(deviceId) != null) {
|
||||||
|
mControllerExtendKey.get(deviceId).remove(Integer.valueOf(externalKeyCode));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,13 @@ void GameControllerTest::onConnectController(Controller* controller, Event* even
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
||||||
|
//receive back key
|
||||||
|
controller->receiveExternalKeyEvent(4,true);
|
||||||
|
//receive menu key
|
||||||
|
controller->receiveExternalKeyEvent(82,true);
|
||||||
|
#endif
|
||||||
|
|
||||||
if (_firstHolder.controller == nullptr && _secondHolder.controller == nullptr)
|
if (_firstHolder.controller == nullptr && _secondHolder.controller == nullptr)
|
||||||
{
|
{
|
||||||
char deviceId[20];
|
char deviceId[20];
|
||||||
|
@ -41,12 +48,12 @@ void GameControllerTest::onConnectController(Controller* controller, Event* even
|
||||||
if (_firstHolder._holderNode)
|
if (_firstHolder._holderNode)
|
||||||
{
|
{
|
||||||
_firstHolder.controller = controller;
|
_firstHolder.controller = controller;
|
||||||
_firstHolder._deviceIdLabel->setString(deviceId);
|
_firstHolder._deviceLabel->setString(deviceId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_secondHolder.controller = controller;
|
_secondHolder.controller = controller;
|
||||||
_secondHolder._deviceIdLabel->setString(deviceId);
|
_secondHolder._deviceLabel->setString(deviceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(_secondHolder.controller == nullptr)
|
else if(_secondHolder.controller == nullptr)
|
||||||
|
@ -68,7 +75,7 @@ void GameControllerTest::onConnectController(Controller* controller, Event* even
|
||||||
|
|
||||||
char deviceId[20];
|
char deviceId[20];
|
||||||
sprintf(deviceId,"device id:%d",controller->getDeviceId());
|
sprintf(deviceId,"device id:%d",controller->getDeviceId());
|
||||||
_secondHolder._deviceIdLabel->setString(deviceId);
|
_secondHolder._deviceLabel->setString(deviceId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -88,7 +95,7 @@ void GameControllerTest::onConnectController(Controller* controller, Event* even
|
||||||
}
|
}
|
||||||
char deviceId[20];
|
char deviceId[20];
|
||||||
sprintf(deviceId,"device id:%d",controller->getDeviceId());
|
sprintf(deviceId,"device id:%d",controller->getDeviceId());
|
||||||
_firstHolder._deviceIdLabel->setString(deviceId);
|
_firstHolder._deviceLabel->setString(deviceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,7 +151,7 @@ void GameControllerTest::resetControllerHolderState(ControllerHolder& holder)
|
||||||
|
|
||||||
holder._leftJoystick->setPosition(Vec2(238,460));
|
holder._leftJoystick->setPosition(Vec2(238,460));
|
||||||
holder._rightJoystick->setPosition(Vec2(606,293));
|
holder._rightJoystick->setPosition(Vec2(606,293));
|
||||||
holder._deviceIdLabel->setString("Disconnected");
|
holder._deviceLabel->setString("Disconnected");
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameControllerTest::showButtonState(cocos2d::Controller *controller, int keyCode, bool isPressed)
|
void GameControllerTest::showButtonState(cocos2d::Controller *controller, int keyCode, bool isPressed)
|
||||||
|
@ -193,9 +200,14 @@ void GameControllerTest::showButtonState(cocos2d::Controller *controller, int ke
|
||||||
holder->_buttonR1->setColor(Color3B(19,231,238));
|
holder->_buttonR1->setColor(Color3B(19,231,238));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
{
|
||||||
|
char ketStatus[30];
|
||||||
|
sprintf(ketStatus,"External Key Down:%d",keyCode);
|
||||||
|
holder->_externalKeyLabel->setString(ketStatus);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
switch (keyCode)
|
switch (keyCode)
|
||||||
|
@ -231,10 +243,15 @@ void GameControllerTest::showButtonState(cocos2d::Controller *controller, int ke
|
||||||
holder->_buttonR1->setColor(Color3B::WHITE);
|
holder->_buttonR1->setColor(Color3B::WHITE);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
{
|
||||||
|
char ketStatus[30];
|
||||||
|
sprintf(ketStatus,"External Key Up:%d",keyCode);
|
||||||
|
holder->_externalKeyLabel->setString(ketStatus);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void GameControllerTest::onKeyDown(cocos2d::Controller *controller, int keyCode, cocos2d::Event *event)
|
void GameControllerTest::onKeyDown(cocos2d::Controller *controller, int keyCode, cocos2d::Event *event)
|
||||||
{
|
{
|
||||||
|
@ -249,6 +266,7 @@ void GameControllerTest::onKeyUp(cocos2d::Controller *controller, int keyCode, c
|
||||||
void GameControllerTest::onAxisEvent(cocos2d::Controller* controller, int keyCode, cocos2d::Event* event)
|
void GameControllerTest::onAxisEvent(cocos2d::Controller* controller, int keyCode, cocos2d::Event* event)
|
||||||
{
|
{
|
||||||
//onConnectController(controller,nullptr);
|
//onConnectController(controller,nullptr);
|
||||||
|
log("controller:%d,keyCode:%d",controller,keyCode);
|
||||||
ControllerHolder* holder = nullptr;
|
ControllerHolder* holder = nullptr;
|
||||||
if (controller == _firstHolder.controller)
|
if (controller == _firstHolder.controller)
|
||||||
holder = &_firstHolder;
|
holder = &_firstHolder;
|
||||||
|
@ -344,12 +362,16 @@ void GameControllerTest::createControllerSprite(ControllerHolder& holder)
|
||||||
holder._rightJoystick->setPosition(Vec2(606,293));
|
holder._rightJoystick->setPosition(Vec2(606,293));
|
||||||
holder._holderNode->addChild(holder._rightJoystick);
|
holder._holderNode->addChild(holder._rightJoystick);
|
||||||
|
|
||||||
holder._deviceIdLabel = Label::createWithTTF("Disconnected","fonts/Marker Felt.ttf",36);
|
holder._deviceLabel = Label::createWithTTF("Disconnected","fonts/Marker Felt.ttf",36);
|
||||||
holder._deviceIdLabel->setPosition(Vec2(499,460));
|
holder._deviceLabel->setPosition(Vec2(499,460));
|
||||||
holder._deviceIdLabel->setTextColor(Color4B::RED);
|
holder._deviceLabel->setTextColor(Color4B::RED);
|
||||||
holder._holderNode->addChild(holder._deviceIdLabel);
|
holder._holderNode->addChild(holder._deviceLabel);
|
||||||
|
|
||||||
|
holder._externalKeyLabel = Label::createWithTTF("External Key event","fonts/Marker Felt.ttf",36);
|
||||||
|
holder._externalKeyLabel->setPosition(Vec2(499,500));
|
||||||
|
holder._externalKeyLabel->setTextColor(Color4B::RED);
|
||||||
|
holder._holderNode->addChild(holder._externalKeyLabel);
|
||||||
//-----------------------------------------------------------------
|
//-----------------------------------------------------------------
|
||||||
//371,294 64
|
|
||||||
auto dPadTexture = Director::getInstance()->getTextureCache()->addImage("dPad.png");
|
auto dPadTexture = Director::getInstance()->getTextureCache()->addImage("dPad.png");
|
||||||
|
|
||||||
auto dPadCenter = Sprite::createWithTexture(dPadTexture,Rect(60,60,68,68));
|
auto dPadCenter = Sprite::createWithTexture(dPadTexture,Rect(60,60,68,68));
|
||||||
|
@ -400,7 +422,6 @@ void GameControllerTest::createControllerSprite(ControllerHolder& holder)
|
||||||
holder._buttonR2->setPosition(Vec2(998-220,910));
|
holder._buttonR2->setPosition(Vec2(998-220,910));
|
||||||
holder._holderNode->addChild(holder._buttonR2);
|
holder._holderNode->addChild(holder._buttonR2);
|
||||||
//-----------------------------------------------------------------
|
//-----------------------------------------------------------------
|
||||||
//750 460,70
|
|
||||||
holder._buttonX = Sprite::create("X.png");
|
holder._buttonX = Sprite::create("X.png");
|
||||||
holder._buttonX->setPosition(Vec2(750 - 70,460));
|
holder._buttonX->setPosition(Vec2(750 - 70,460));
|
||||||
holder._holderNode->addChild(holder._buttonX);
|
holder._holderNode->addChild(holder._buttonX);
|
||||||
|
|
|
@ -53,7 +53,8 @@ private:
|
||||||
cocos2d::Sprite* _buttonL2;
|
cocos2d::Sprite* _buttonL2;
|
||||||
cocos2d::Sprite* _buttonR2;
|
cocos2d::Sprite* _buttonR2;
|
||||||
|
|
||||||
cocos2d::Label* _deviceIdLabel;
|
cocos2d::Label* _deviceLabel;
|
||||||
|
cocos2d::Label* _externalKeyLabel;
|
||||||
}ControllerHolder;
|
}ControllerHolder;
|
||||||
|
|
||||||
ControllerHolder _firstHolder;
|
ControllerHolder _firstHolder;
|
||||||
|
|
Loading…
Reference in New Issue