1.fixed lose disconnected event.

2.fixed incorrect device name and device id;
This commit is contained in:
Dhilan007 2014-07-07 17:47:07 +08:00
parent ddfe82ae08
commit d1034fdaae
6 changed files with 151 additions and 83 deletions

View File

@ -99,7 +99,9 @@ public:
const KeyStatus& getKeyStatus(int keyCode); const KeyStatus& getKeyStatus(int keyCode);
//Setting up receives external key which not contained within enum Key. //Activate receives key event from external key. e.g. back,menu.
//Controller receives only standard key which contained within enum Key by default.
//The API only work on the android platform for support diversified game controller.
void receiveExternalKeyEvent(int externalKeyCode,bool receive); void receiveExternalKeyEvent(int externalKeyCode,bool receive);
void setTag(int tag) { _controllerTag = tag;} void setTag(int tag) { _controllerTag = tag;}

View File

@ -31,11 +31,9 @@ import org.cocos2dx.lib.inputmanagercompat.InputManagerCompat.InputDeviceListene
import org.cocos2dx.lib.Cocos2dxActivity; import org.cocos2dx.lib.Cocos2dxActivity;
import android.os.Bundle; import android.os.Bundle;
import android.view.InputDevice;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.util.Log; import android.util.Log;
import android.util.SparseArray;
public abstract class GameControllerActivity extends Cocos2dxActivity implements InputDeviceListener { public abstract class GameControllerActivity extends Cocos2dxActivity implements InputDeviceListener {
// =========================================================== // ===========================================================
@ -224,30 +222,11 @@ public abstract class GameControllerActivity extends Cocos2dxActivity implements
return handled || super.dispatchGenericMotionEvent(event); return handled || super.dispatchGenericMotionEvent(event);
} }
protected SparseArray<String> mGameController = null;
@Override @Override
public void onInputDeviceAdded(int deviceId) { public void onInputDeviceAdded(int deviceId) {
Log.d(TAG,"onInputDeviceAdded:" + deviceId); Log.d(TAG,"onInputDeviceAdded:" + deviceId);
try { mControllerHelper.onInputDeviceAdded(deviceId);
InputDevice device = InputDevice.getDevice(deviceId);
int deviceSource = device.getSources();
if ( ((deviceSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
|| ((deviceSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) )
{
if (mGameController == null) {
mGameController = new SparseArray<String>();
}
String deviceName = device.getName();
mGameController.append(deviceId, deviceName);
GameControllerAdapter.onConnected(deviceName, deviceId);
}
} catch (Exception e) {
e.printStackTrace();
}
} }
/* /*
* This is an unusual case. Input devices don't typically change, but they * This is an unusual case. Input devices don't typically change, but they
@ -274,10 +253,7 @@ public abstract class GameControllerActivity extends Cocos2dxActivity implements
public void onInputDeviceRemoved(int deviceId) { public void onInputDeviceRemoved(int deviceId) {
Log.d(TAG,"onInputDeviceRemoved:" + deviceId); Log.d(TAG,"onInputDeviceRemoved:" + deviceId);
if (mGameController != null && mGameController.get(deviceId) != null) { mControllerHelper.onInputDeviceRemoved(deviceId);
GameControllerAdapter.onDisconnected(mGameController.get(deviceId), deviceId);
mGameController.delete(deviceId);
}
} }
@Override @Override
@ -293,8 +269,10 @@ public abstract class GameControllerActivity extends Cocos2dxActivity implements
if (mControllerOuya != null) { if (mControllerOuya != null) {
mControllerOuya.onResume(); mControllerOuya.onResume();
} }
mControllerHelper.gatherControllers();
} }
@Override @Override
protected void onPause() { protected void onPause() {
if (mControllerNibiru != null) { if (mControllerNibiru != null) {

View File

@ -55,7 +55,6 @@ public class GameControllerHelper {
ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_START, GameControllerDelegate.BUTTON_START); ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_START, GameControllerDelegate.BUTTON_START);
ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_SELECT, GameControllerDelegate.BUTTON_SELECT); ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_SELECT, GameControllerDelegate.BUTTON_SELECT);
//KEYCODE_BUTTON_MODE
} }
private float mOldLeftThumbstickX = 0.0f; private float mOldLeftThumbstickX = 0.0f;
@ -78,68 +77,71 @@ public class GameControllerHelper {
|| ((eventSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) ) || ((eventSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) )
{ {
if (event.getAction() == MotionEvent.ACTION_MOVE) { if (event.getAction() == MotionEvent.ACTION_MOVE) {
int devicedId = event.getDeviceId(); int deviceId = event.getDeviceId();
String deviceName = event.getDevice().getName(); String deviceName = event.getDevice().getName();
if(mGameController.get(deviceId) == null){
mGameController.append(deviceId, deviceName);
}
float newAXIS_LX = event.getAxisValue(AXIS_X); float newAXIS_LX = event.getAxisValue(AXIS_X);
if (Float.compare(newAXIS_LX , mOldLeftThumbstickX) != 0) { if (Float.compare(newAXIS_LX , mOldLeftThumbstickX) != 0) {
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.THUMBSTICK_LEFT_X, newAXIS_LX, true); GameControllerAdapter.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_LEFT_X, newAXIS_LX, true);
mOldLeftThumbstickX = newAXIS_LX; mOldLeftThumbstickX = newAXIS_LX;
handled = true; handled = true;
} }
float newAXIS_LY = event.getAxisValue(AXIS_Y); float newAXIS_LY = event.getAxisValue(AXIS_Y);
if (Float.compare(newAXIS_LY , mOldLeftThumbstickY) != 0) { if (Float.compare(newAXIS_LY , mOldLeftThumbstickY) != 0) {
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.THUMBSTICK_LEFT_Y, newAXIS_LY, true); GameControllerAdapter.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_LEFT_Y, newAXIS_LY, true);
mOldLeftThumbstickY = newAXIS_LY; mOldLeftThumbstickY = newAXIS_LY;
handled = true; handled = true;
} }
float newAXIS_RX = event.getAxisValue(AXIS_Z); float newAXIS_RX = event.getAxisValue(AXIS_Z);
if (Float.compare(newAXIS_RX , mOldRightThumbstickX) != 0) { if (Float.compare(newAXIS_RX , mOldRightThumbstickX) != 0) {
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.THUMBSTICK_RIGHT_X, newAXIS_RX, true); GameControllerAdapter.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_RIGHT_X, newAXIS_RX, true);
mOldRightThumbstickX = newAXIS_RX; mOldRightThumbstickX = newAXIS_RX;
handled = true; handled = true;
} }
float newAXIS_RY = event.getAxisValue(AXIS_RZ); float newAXIS_RY = event.getAxisValue(AXIS_RZ);
if (Float.compare(newAXIS_RY , mOldRightThumbstickY) != 0) { if (Float.compare(newAXIS_RY , mOldRightThumbstickY) != 0) {
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.THUMBSTICK_RIGHT_Y, newAXIS_RY, true); GameControllerAdapter.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_RIGHT_Y, newAXIS_RY, true);
mOldRightThumbstickY = newAXIS_RY; mOldRightThumbstickY = newAXIS_RY;
handled = true; handled = true;
} }
float newAXIS_LTRIGGER = event.getAxisValue(AXIS_LTRIGGER); float newAXIS_LTRIGGER = event.getAxisValue(AXIS_LTRIGGER);
if (Float.compare(newAXIS_LTRIGGER , mOldLeftTrigger) != 0) { if (Float.compare(newAXIS_LTRIGGER , mOldLeftTrigger) != 0) {
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.BUTTON_LEFT_TRIGGER, newAXIS_LTRIGGER, true); GameControllerAdapter.onAxisEvent(deviceName, deviceId, GameControllerDelegate.BUTTON_LEFT_TRIGGER, newAXIS_LTRIGGER, true);
mOldLeftTrigger = newAXIS_LTRIGGER; mOldLeftTrigger = newAXIS_LTRIGGER;
handled = true; handled = true;
} }
float newAXIS_RTRIGGER = event.getAxisValue(AXIS_RTRIGGER); float newAXIS_RTRIGGER = event.getAxisValue(AXIS_RTRIGGER);
if (Float.compare(newAXIS_RTRIGGER , mOldRightTrigger) != 0) { if (Float.compare(newAXIS_RTRIGGER , mOldRightTrigger) != 0) {
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, newAXIS_RTRIGGER, true); GameControllerAdapter.onAxisEvent(deviceName, deviceId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, newAXIS_RTRIGGER, true);
mOldRightTrigger = newAXIS_RTRIGGER; mOldRightTrigger = newAXIS_RTRIGGER;
handled = true; handled = true;
} }
float newAXIS_BRAKE = event.getAxisValue(AXIS_BRAKE); float newAXIS_BRAKE = event.getAxisValue(AXIS_BRAKE);
if (Float.compare(newAXIS_BRAKE , mOldBrake) != 0) { if (Float.compare(newAXIS_BRAKE , mOldBrake) != 0) {
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.BUTTON_LEFT_TRIGGER, newAXIS_BRAKE, true); GameControllerAdapter.onAxisEvent(deviceName, deviceId, GameControllerDelegate.BUTTON_LEFT_TRIGGER, newAXIS_BRAKE, true);
mOldBrake = newAXIS_BRAKE; mOldBrake = newAXIS_BRAKE;
handled = true; handled = true;
} }
float newAXIS_THROTTLE = event.getAxisValue(AXIS_THROTTLE); float newAXIS_THROTTLE = event.getAxisValue(AXIS_THROTTLE);
if (Float.compare(newAXIS_THROTTLE , mOldThrottle) != 0) { if (Float.compare(newAXIS_THROTTLE , mOldThrottle) != 0) {
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, newAXIS_THROTTLE, true); GameControllerAdapter.onAxisEvent(deviceName, deviceId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, newAXIS_THROTTLE, true);
mOldThrottle = newAXIS_THROTTLE; mOldThrottle = newAXIS_THROTTLE;
handled = true; handled = true;
} }
float newAXIS_GAS = event.getAxisValue(AXIS_GAS); float newAXIS_GAS = event.getAxisValue(AXIS_GAS);
if (Float.compare(newAXIS_GAS , mOldGas) != 0) { if (Float.compare(newAXIS_GAS , mOldGas) != 0) {
GameControllerAdapter.onAxisEvent(deviceName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, newAXIS_GAS, true); GameControllerAdapter.onAxisEvent(deviceName, deviceId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, newAXIS_GAS, true);
mOldGas = newAXIS_GAS; mOldGas = newAXIS_GAS;
handled = true; handled = true;
} }
@ -162,6 +164,12 @@ public class GameControllerHelper {
|| ((eventSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) ) || ((eventSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) )
{ {
int deviceId = event.getDeviceId(); int deviceId = event.getDeviceId();
String deviceName = event.getDevice().getName();
if(mGameController.get(deviceId) == null){
mGameController.append(deviceId, deviceName);
}
if (controllerKey == 0) { if (controllerKey == 0) {
if (mControllerExtendKey.get(deviceId) != null && mControllerExtendKey.get(deviceId).contains(keyCode)) { if (mControllerExtendKey.get(deviceId) != null && mControllerExtendKey.get(deviceId).contains(keyCode)) {
controllerKey = keyCode; controllerKey = keyCode;
@ -173,10 +181,10 @@ public class GameControllerHelper {
int action = event.getAction(); int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) { if (action == KeyEvent.ACTION_DOWN) {
handled = true; handled = true;
GameControllerAdapter.onButtonEvent(event.getDevice().getName(),event.getDeviceId(), controllerKey,true, 1.0f, false); GameControllerAdapter.onButtonEvent(deviceName, deviceId, controllerKey,true, 1.0f, false);
}else if (action == KeyEvent.ACTION_UP) { }else if (action == KeyEvent.ACTION_UP) {
handled = true; handled = true;
GameControllerAdapter.onButtonEvent(event.getDevice().getName(),event.getDeviceId(), controllerKey,false, 0.0f, false); GameControllerAdapter.onButtonEvent(deviceName, deviceId, controllerKey,false, 0.0f, false);
} }
} }
@ -195,4 +203,53 @@ public class GameControllerHelper {
} }
} }
} }
SparseArray<String> mGameController = new SparseArray<String>();
void onInputDeviceAdded(int deviceId){
try {
InputDevice device = InputDevice.getDevice(deviceId);
int deviceSource = device.getSources();
if ( ((deviceSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
|| ((deviceSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) )
{
String deviceName = device.getName();
mGameController.append(deviceId, deviceName);
GameControllerAdapter.onConnected(deviceName, deviceId);
}
} catch (Exception e) {
e.printStackTrace();
}
}
void onInputDeviceChanged(int deviceId){
gatherControllers();
}
void onInputDeviceRemoved(int deviceId) {
if (mGameController.get(deviceId) != null) {
GameControllerAdapter.onDisconnected(mGameController.get(deviceId), deviceId);
mGameController.delete(deviceId);
}
}
void gatherControllers(){
int controllerCount = mGameController.size();
for (int i = 0; i < controllerCount; i++) {
try {
int controllerDeveceId = mGameController.keyAt(i);
InputDevice device = InputDevice.getDevice(controllerDeveceId);
if (device == null) {
GameControllerAdapter.onDisconnected(mGameController.get(controllerDeveceId), controllerDeveceId);
mGameController.delete(controllerDeveceId);
}
} catch (Exception e) {
int controllerDeveceId = mGameController.keyAt(i);
GameControllerAdapter.onDisconnected(mGameController.get(controllerDeveceId), controllerDeveceId);
mGameController.delete(controllerDeveceId);
e.printStackTrace();
}
}
}
} }

View File

@ -27,7 +27,6 @@ public class GameControllerNibiru implements OnControllerSeviceListener, OnKeyLi
OnSimpleStickListener, OnAccListener, OnGyroListener, OnStateListener, GameControllerDelegate { OnSimpleStickListener, OnAccListener, OnGyroListener, OnStateListener, GameControllerDelegate {
private static final String TAG = "NibiruTag"; private static final String TAG = "NibiruTag";
private static final String mVendorName = "Nibiru";
private Context mContext; private Context mContext;
private SparseIntArray mKeyMap; private SparseIntArray mKeyMap;
@ -132,7 +131,14 @@ OnSimpleStickListener, OnAccListener, OnGyroListener, OnStateListener, GameContr
} }
if (mControllerEventListener != null) { if (mControllerEventListener != null) {
mControllerEventListener.onButtonEvent(mVendorName, playerOrder, mKeyMap.get(keyCode), true, 1.0f, false); try {
ControllerDevice controllerDevice = mControllerService.getDeviceByPlayerOrder(playerOrder);
mControllerEventListener.onButtonEvent(controllerDevice.getDeviceName(), controllerDevice.getDeviceId(),
mKeyMap.get(keyCode), true, 1.0f, false);
} catch (ControllerServiceException e) {
e.printStackTrace();
}
} }
} }
@ -144,28 +150,52 @@ OnSimpleStickListener, OnAccListener, OnGyroListener, OnStateListener, GameContr
} }
if (mControllerEventListener != null) { if (mControllerEventListener != null) {
mControllerEventListener.onButtonEvent(mVendorName, playerOrder, try {
mKeyMap.get(keyCode), false, 0.0f, false); ControllerDevice controllerDevice = mControllerService.getDeviceByPlayerOrder(playerOrder);
mControllerEventListener.onButtonEvent(controllerDevice.getDeviceName(), controllerDevice.getDeviceId(),
mKeyMap.get(keyCode), false, 0.0f, false);
} catch (ControllerServiceException e) {
e.printStackTrace();
}
} }
} }
@Override @Override
public void onLeftStickChanged(int playerOrder, float x, float y) { public void onLeftStickChanged(int playerOrder, float x, float y) {
if (mControllerEventListener != null) { if (mControllerEventListener != null) {
mControllerEventListener.onAxisEvent(mVendorName, playerOrder, try {
GameControllerDelegate.THUMBSTICK_LEFT_X, x, true); ControllerDevice controllerDevice = mControllerService.getDeviceByPlayerOrder(playerOrder);
mControllerEventListener.onAxisEvent(mVendorName, playerOrder,
GameControllerDelegate.THUMBSTICK_LEFT_Y, y, true); String deviceName = controllerDevice.getDeviceName();
int deviceId = controllerDevice.getDeviceId();
mControllerEventListener.onAxisEvent(deviceName, deviceId,
GameControllerDelegate.THUMBSTICK_LEFT_X, x, true);
mControllerEventListener.onAxisEvent(deviceName, deviceId,
GameControllerDelegate.THUMBSTICK_LEFT_Y, y, true);
} catch (ControllerServiceException e) {
e.printStackTrace();
}
} }
} }
@Override @Override
public void onRightStickChanged(int playerOrder, float x, float y) { public void onRightStickChanged(int playerOrder, float x, float y) {
if (mControllerEventListener != null) { if (mControllerEventListener != null) {
mControllerEventListener.onAxisEvent(mVendorName, playerOrder, try {
GameControllerDelegate.THUMBSTICK_RIGHT_X, x, true); ControllerDevice controllerDevice = mControllerService.getDeviceByPlayerOrder(playerOrder);
mControllerEventListener.onAxisEvent(mVendorName, playerOrder,
GameControllerDelegate.THUMBSTICK_RIGHT_Y, y, true); String deviceName = controllerDevice.getDeviceName();
int deviceId = controllerDevice.getDeviceId();
mControllerEventListener.onAxisEvent(deviceName, deviceId,
GameControllerDelegate.THUMBSTICK_RIGHT_X, x, true);
mControllerEventListener.onAxisEvent(deviceName, deviceId,
GameControllerDelegate.THUMBSTICK_RIGHT_Y, y, true);
} catch (ControllerServiceException e) {
e.printStackTrace();
}
} }
} }
@ -174,11 +204,11 @@ OnSimpleStickListener, OnAccListener, OnGyroListener, OnStateListener, GameContr
if (mControllerEventListener != null) { if (mControllerEventListener != null) {
if (state == ControllerDevice.STATE_CONN) if (state == ControllerDevice.STATE_CONN)
{ {
mControllerEventListener.onConnected(mVendorName, playerOrder); mControllerEventListener.onConnected(device.getDeviceName(), device.getDeviceId());
} }
else if (state == ControllerDevice.STATE_DISCONN) else if (state == ControllerDevice.STATE_DISCONN)
{ {
mControllerEventListener.onDisconnected(mVendorName, playerOrder); mControllerEventListener.onDisconnected(device.getDeviceName(), device.getDeviceId());
} }
} }
} }

View File

@ -10,8 +10,6 @@ import android.view.KeyEvent;
import android.view.MotionEvent; import android.view.MotionEvent;
public class GameControllerOuya implements GameControllerDelegate{ public class GameControllerOuya implements GameControllerDelegate{
public static final String sVendorName = "Ouya";
private SparseIntArray mKeyMap; private SparseIntArray mKeyMap;
@ -61,27 +59,28 @@ public class GameControllerOuya implements GameControllerDelegate{
if (handled && mControllerEventListener != null) if (handled && mControllerEventListener != null)
{ {
OuyaController c = OuyaController.getControllerByDeviceId(event.getDeviceId()); int deviceId = event.getDeviceId();
int controllerID = OuyaController.getPlayerNumByDeviceId(event.getDeviceId()); String deviceName = event.getDevice().getName();
OuyaController c = OuyaController.getControllerByDeviceId(deviceId);
float newLeftTrigger = c.getAxisValue(OuyaController.AXIS_L2); float newLeftTrigger = c.getAxisValue(OuyaController.AXIS_L2);
if (Float.compare(newLeftTrigger, mOldLeftTrigger) != 0) { if (Float.compare(newLeftTrigger, mOldLeftTrigger) != 0) {
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.BUTTON_LEFT_TRIGGER, newLeftTrigger, true); mControllerEventListener.onAxisEvent(deviceName, deviceId, GameControllerDelegate.BUTTON_LEFT_TRIGGER, newLeftTrigger, true);
mOldLeftTrigger = newLeftTrigger; mOldLeftTrigger = newLeftTrigger;
} }
float newRightTrigger = c.getAxisValue(OuyaController.AXIS_R2); float newRightTrigger = c.getAxisValue(OuyaController.AXIS_R2);
if (Float.compare(newRightTrigger, mOldRightTrigger) != 0) { if (Float.compare(newRightTrigger, mOldRightTrigger) != 0) {
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, newRightTrigger, true); mControllerEventListener.onAxisEvent(deviceName, deviceId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, newRightTrigger, true);
mOldRightTrigger = newRightTrigger; mOldRightTrigger = newRightTrigger;
} }
float newLeftThumbstickX = c.getAxisValue(OuyaController.AXIS_LS_X); float newLeftThumbstickX = c.getAxisValue(OuyaController.AXIS_LS_X);
if (Float.compare(newLeftThumbstickX, mOldLeftThumbstickX) != 0) { if (Float.compare(newLeftThumbstickX, mOldLeftThumbstickX) != 0) {
if (Float.compare(newLeftThumbstickX, 0.0f) == 0) { if (Float.compare(newLeftThumbstickX, 0.0f) == 0) {
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.THUMBSTICK_LEFT_X, 0.0f, true); mControllerEventListener.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_LEFT_X, 0.0f, true);
}else { }else {
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.THUMBSTICK_LEFT_X, newLeftThumbstickX, true); mControllerEventListener.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_LEFT_X, newLeftThumbstickX, true);
} }
mOldLeftThumbstickX = newLeftThumbstickX; mOldLeftThumbstickX = newLeftThumbstickX;
} }
@ -89,9 +88,9 @@ public class GameControllerOuya implements GameControllerDelegate{
float newLeftThumbstickY = c.getAxisValue(OuyaController.AXIS_LS_Y); float newLeftThumbstickY = c.getAxisValue(OuyaController.AXIS_LS_Y);
if (Float.compare(newLeftThumbstickY, mOldLeftThumbstickY) != 0) { if (Float.compare(newLeftThumbstickY, mOldLeftThumbstickY) != 0) {
if (Float.compare(newLeftThumbstickY, 0.0f) == 0) { if (Float.compare(newLeftThumbstickY, 0.0f) == 0) {
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.THUMBSTICK_LEFT_Y, 0.0f, true); mControllerEventListener.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_LEFT_Y, 0.0f, true);
}else { }else {
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.THUMBSTICK_LEFT_Y, newLeftThumbstickY, true); mControllerEventListener.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_LEFT_Y, newLeftThumbstickY, true);
} }
mOldLeftThumbstickY = newLeftThumbstickY; mOldLeftThumbstickY = newLeftThumbstickY;
} }
@ -99,9 +98,9 @@ public class GameControllerOuya implements GameControllerDelegate{
float newRightThumbstickX = c.getAxisValue(OuyaController.AXIS_RS_X); float newRightThumbstickX = c.getAxisValue(OuyaController.AXIS_RS_X);
if (Float.compare(newRightThumbstickX, mOldRightThumbstickX) != 0) { if (Float.compare(newRightThumbstickX, mOldRightThumbstickX) != 0) {
if (Float.compare(newRightThumbstickX, 0.0f) == 0) { if (Float.compare(newRightThumbstickX, 0.0f) == 0) {
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.THUMBSTICK_RIGHT_X, 0.0f, true); mControllerEventListener.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_RIGHT_X, 0.0f, true);
}else { }else {
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.THUMBSTICK_RIGHT_X, newRightThumbstickX, true); mControllerEventListener.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_RIGHT_X, newRightThumbstickX, true);
} }
mOldRightThumbstickX = newRightThumbstickX; mOldRightThumbstickX = newRightThumbstickX;
} }
@ -109,9 +108,9 @@ public class GameControllerOuya implements GameControllerDelegate{
float newRightThumbstickY = c.getAxisValue(OuyaController.AXIS_RS_Y); float newRightThumbstickY = c.getAxisValue(OuyaController.AXIS_RS_Y);
if (Float.compare(newRightThumbstickY, mOldRightThumbstickY) != 0) { if (Float.compare(newRightThumbstickY, mOldRightThumbstickY) != 0) {
if (Float.compare(newRightThumbstickY, 0.0f) == 0) { if (Float.compare(newRightThumbstickY, 0.0f) == 0) {
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.THUMBSTICK_RIGHT_Y, 0.0f, true); mControllerEventListener.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_RIGHT_Y, 0.0f, true);
}else { }else {
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.THUMBSTICK_RIGHT_Y, newRightThumbstickY, true); mControllerEventListener.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_RIGHT_Y, newRightThumbstickY, true);
} }
mOldRightThumbstickY = newRightThumbstickY; mOldRightThumbstickY = newRightThumbstickY;
} }
@ -122,9 +121,14 @@ public class GameControllerOuya implements GameControllerDelegate{
public boolean dispatchKeyEvent(KeyEvent event) { public boolean dispatchKeyEvent(KeyEvent event) {
boolean handled = false; boolean handled = false;
int action = event.getAction(); int action = event.getAction();
int keyCode = event.getKeyCode(); int keyCode = event.getKeyCode();
if (keyCode == KeyEvent.KEYCODE_BUTTON_L2 || keyCode == KeyEvent.KEYCODE_BUTTON_R2) {
return true;
}
if (action == KeyEvent.ACTION_DOWN) { if (action == KeyEvent.ACTION_DOWN) {
handled = OuyaController.onKeyDown(keyCode, event); handled = OuyaController.onKeyDown(keyCode, event);
} }
@ -139,11 +143,10 @@ public class GameControllerOuya implements GameControllerDelegate{
isAnalog = true; isAnalog = true;
} }
int controllerID = OuyaController.getPlayerNumByDeviceId(event.getDeviceId());
if (action == KeyEvent.ACTION_DOWN) { if (action == KeyEvent.ACTION_DOWN) {
mControllerEventListener.onButtonEvent(sVendorName, controllerID, mKeyMap.get(keyCode), true, 1.0f, isAnalog); mControllerEventListener.onButtonEvent(event.getDevice().getName(), event.getDeviceId(), mKeyMap.get(keyCode), true, 1.0f, isAnalog);
}else { }else {
mControllerEventListener.onButtonEvent(sVendorName, controllerID, mKeyMap.get(keyCode), false, 0.0f, isAnalog); mControllerEventListener.onButtonEvent(event.getDevice().getName(), event.getDeviceId(), mKeyMap.get(keyCode), false, 0.0f, isAnalog);
} }
} }

View File

@ -73,9 +73,9 @@ void GameControllerTest::onConnectController(Controller* controller, Event* even
this->addChild(_secondHolder._holderNode); this->addChild(_secondHolder._holderNode);
} }
char deviceId[20]; char deviceInfo[50];
sprintf(deviceId,"device id:%d",controller->getDeviceId()); sprintf(deviceInfo,"%s id:%d",controller->getDeviceName().c_str(), controller->getDeviceId());
_secondHolder._deviceLabel->setString(deviceId); _secondHolder._deviceLabel->setString(deviceInfo);
} }
else else
{ {
@ -93,9 +93,9 @@ void GameControllerTest::onConnectController(Controller* controller, Event* even
_firstHolder._holderNode->setPosition(Vec2(_visibleThreeQuarterX, _visibleCentreY)); _firstHolder._holderNode->setPosition(Vec2(_visibleThreeQuarterX, _visibleCentreY));
this->addChild(_firstHolder._holderNode); this->addChild(_firstHolder._holderNode);
} }
char deviceId[20]; char deviceInfo[50];
sprintf(deviceId,"device id:%d",controller->getDeviceId()); sprintf(deviceInfo,"%s id:%d",controller->getDeviceName().c_str(), controller->getDeviceId());
_firstHolder._deviceLabel->setString(deviceId); _firstHolder._deviceLabel->setString(deviceInfo);
} }
} }
@ -202,7 +202,7 @@ void GameControllerTest::showButtonState(cocos2d::Controller *controller, int ke
default: default:
{ {
char ketStatus[30]; char ketStatus[30];
sprintf(ketStatus,"External Key Down:%d",keyCode); sprintf(ketStatus,"Key Down:%d",keyCode);
holder->_externalKeyLabel->setString(ketStatus); holder->_externalKeyLabel->setString(ketStatus);
break; break;
} }
@ -245,7 +245,7 @@ void GameControllerTest::showButtonState(cocos2d::Controller *controller, int ke
default: default:
{ {
char ketStatus[30]; char ketStatus[30];
sprintf(ketStatus,"External Key Up:%d",keyCode); sprintf(ketStatus,"Key Up:%d",keyCode);
holder->_externalKeyLabel->setString(ketStatus); holder->_externalKeyLabel->setString(ketStatus);
break; break;
} }
@ -265,8 +265,6 @@ 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);
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;
@ -367,7 +365,7 @@ void GameControllerTest::createControllerSprite(ControllerHolder& holder)
holder._deviceLabel->setTextColor(Color4B::RED); holder._deviceLabel->setTextColor(Color4B::RED);
holder._holderNode->addChild(holder._deviceLabel); holder._holderNode->addChild(holder._deviceLabel);
holder._externalKeyLabel = Label::createWithTTF("External Key event","fonts/Marker Felt.ttf",36); holder._externalKeyLabel = Label::createWithTTF("Key event","fonts/Marker Felt.ttf",36);
holder._externalKeyLabel->setPosition(Vec2(499,500)); holder._externalKeyLabel->setPosition(Vec2(499,500));
holder._externalKeyLabel->setTextColor(Color4B::RED); holder._externalKeyLabel->setTextColor(Color4B::RED);
holder._holderNode->addChild(holder._externalKeyLabel); holder._holderNode->addChild(holder._externalKeyLabel);