mirror of https://github.com/axmolengine/axmol.git
issue #4689: support external key.
This commit is contained in:
parent
2b3de76819
commit
68bd717ea7
|
@ -47,6 +47,7 @@ public:
|
|||
auto iter = std::find_if(Controller::s_allController.begin(), Controller::s_allController.end(), [&](Controller* controller){
|
||||
return (deviceName == controller->_deviceName) && (deviceId == controller->_deviceId);
|
||||
});
|
||||
return iter;
|
||||
}
|
||||
|
||||
static void onConnected(const std::string& deviceName, int deviceId)
|
||||
|
@ -99,13 +100,16 @@ public:
|
|||
|
||||
static void onAxisEvent(const std::string& deviceName, int deviceId, int axisCode, float value, bool isAnalog)
|
||||
{
|
||||
log("onAxisEvent:%s,%d,%d,%f",deviceName.c_str(),deviceId,axisCode,value);
|
||||
|
||||
auto iter = findController(deviceName, deviceId);
|
||||
if (iter == Controller::s_allController.end())
|
||||
{
|
||||
log("onAxisEvent: not find,connect new");
|
||||
onConnected(deviceName, deviceId);
|
||||
iter = findController(deviceName, deviceId);
|
||||
}
|
||||
|
||||
|
||||
(*iter)->onAxisEvent(axisCode, value, isAnalog);
|
||||
}
|
||||
|
||||
|
@ -154,6 +158,16 @@ Controller::Controller()
|
|||
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
|
||||
|
||||
extern "C" {
|
||||
|
|
|
@ -311,6 +311,10 @@ bool Controller::isConnected() const
|
|||
return _impl->_gcController.isAttachedToDevice == YES;
|
||||
}
|
||||
|
||||
void Controller::receiveExternalKeyEvent(int externalKeyCode,bool receive)
|
||||
{
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
||||
|
|
|
@ -99,6 +99,9 @@ public:
|
|||
|
||||
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;}
|
||||
int getTag() const { return _controllerTag;}
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package org.cocos2dx.lib;
|
||||
|
||||
import android.util.Log;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.util.SparseArray;
|
||||
import android.util.SparseIntArray;
|
||||
import android.view.InputDevice;
|
||||
import android.view.KeyEvent;
|
||||
|
@ -147,6 +149,8 @@ public class GameControllerHelper {
|
|||
return handled;
|
||||
}
|
||||
|
||||
private static SparseArray<ArrayList<Integer>> mControllerExtendKey = new SparseArray<ArrayList<Integer>>();
|
||||
|
||||
public boolean dispatchKeyEvent(KeyEvent event) {
|
||||
boolean handled = false;
|
||||
|
||||
|
@ -154,9 +158,18 @@ public class GameControllerHelper {
|
|||
int keyCode = event.getKeyCode();
|
||||
int controllerKey = ControllerKeyMap.get(keyCode);
|
||||
|
||||
if (controllerKey != 0 && (((eventSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
|
||||
|| ((eventSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) ))
|
||||
if (((eventSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
|
||||
|| ((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();
|
||||
if (action == KeyEvent.ACTION_DOWN) {
|
||||
handled = true;
|
||||
|
@ -169,4 +182,17 @@ public class GameControllerHelper {
|
|||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,6 +32,13 @@ void GameControllerTest::onConnectController(Controller* controller, Event* even
|
|||
{
|
||||
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)
|
||||
{
|
||||
|
@ -41,12 +48,12 @@ void GameControllerTest::onConnectController(Controller* controller, Event* even
|
|||
if (_firstHolder._holderNode)
|
||||
{
|
||||
_firstHolder.controller = controller;
|
||||
_firstHolder._deviceIdLabel->setString(deviceId);
|
||||
_firstHolder._deviceLabel->setString(deviceId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_secondHolder.controller = controller;
|
||||
_secondHolder._deviceIdLabel->setString(deviceId);
|
||||
_secondHolder._deviceLabel->setString(deviceId);
|
||||
}
|
||||
}
|
||||
else if(_secondHolder.controller == nullptr)
|
||||
|
@ -68,7 +75,7 @@ void GameControllerTest::onConnectController(Controller* controller, Event* even
|
|||
|
||||
char deviceId[20];
|
||||
sprintf(deviceId,"device id:%d",controller->getDeviceId());
|
||||
_secondHolder._deviceIdLabel->setString(deviceId);
|
||||
_secondHolder._deviceLabel->setString(deviceId);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -88,7 +95,7 @@ void GameControllerTest::onConnectController(Controller* controller, Event* even
|
|||
}
|
||||
char deviceId[20];
|
||||
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._rightJoystick->setPosition(Vec2(606,293));
|
||||
holder._deviceIdLabel->setString("Disconnected");
|
||||
holder._deviceLabel->setString("Disconnected");
|
||||
}
|
||||
|
||||
void GameControllerTest::showButtonState(cocos2d::Controller *controller, int keyCode, bool isPressed)
|
||||
|
@ -193,7 +200,12 @@ void GameControllerTest::showButtonState(cocos2d::Controller *controller, int ke
|
|||
holder->_buttonR1->setColor(Color3B(19,231,238));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
{
|
||||
char ketStatus[30];
|
||||
sprintf(ketStatus,"External Key Down:%d",keyCode);
|
||||
holder->_externalKeyLabel->setString(ketStatus);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -231,7 +243,12 @@ void GameControllerTest::showButtonState(cocos2d::Controller *controller, int ke
|
|||
holder->_buttonR1->setColor(Color3B::WHITE);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
{
|
||||
char ketStatus[30];
|
||||
sprintf(ketStatus,"External Key Up:%d",keyCode);
|
||||
holder->_externalKeyLabel->setString(ketStatus);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -249,6 +266,7 @@ void GameControllerTest::onKeyUp(cocos2d::Controller *controller, int keyCode, c
|
|||
void GameControllerTest::onAxisEvent(cocos2d::Controller* controller, int keyCode, cocos2d::Event* event)
|
||||
{
|
||||
//onConnectController(controller,nullptr);
|
||||
log("controller:%d,keyCode:%d",controller,keyCode);
|
||||
ControllerHolder* holder = nullptr;
|
||||
if (controller == _firstHolder.controller)
|
||||
holder = &_firstHolder;
|
||||
|
@ -344,12 +362,16 @@ void GameControllerTest::createControllerSprite(ControllerHolder& holder)
|
|||
holder._rightJoystick->setPosition(Vec2(606,293));
|
||||
holder._holderNode->addChild(holder._rightJoystick);
|
||||
|
||||
holder._deviceIdLabel = Label::createWithTTF("Disconnected","fonts/Marker Felt.ttf",36);
|
||||
holder._deviceIdLabel->setPosition(Vec2(499,460));
|
||||
holder._deviceIdLabel->setTextColor(Color4B::RED);
|
||||
holder._holderNode->addChild(holder._deviceIdLabel);
|
||||
holder._deviceLabel = Label::createWithTTF("Disconnected","fonts/Marker Felt.ttf",36);
|
||||
holder._deviceLabel->setPosition(Vec2(499,460));
|
||||
holder._deviceLabel->setTextColor(Color4B::RED);
|
||||
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 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._holderNode->addChild(holder._buttonR2);
|
||||
//-----------------------------------------------------------------
|
||||
//750 460,70
|
||||
holder._buttonX = Sprite::create("X.png");
|
||||
holder._buttonX->setPosition(Vec2(750 - 70,460));
|
||||
holder._holderNode->addChild(holder._buttonX);
|
||||
|
|
|
@ -53,7 +53,8 @@ private:
|
|||
cocos2d::Sprite* _buttonL2;
|
||||
cocos2d::Sprite* _buttonR2;
|
||||
|
||||
cocos2d::Label* _deviceIdLabel;
|
||||
cocos2d::Label* _deviceLabel;
|
||||
cocos2d::Label* _externalKeyLabel;
|
||||
}ControllerHolder;
|
||||
|
||||
ControllerHolder _firstHolder;
|
||||
|
|
Loading…
Reference in New Issue