mirror of https://github.com/axmolengine/axmol.git
1.fixed lose disconnected event.
2.fixed incorrect device name and device id;
This commit is contained in:
parent
ddfe82ae08
commit
d1034fdaae
|
@ -99,7 +99,9 @@ public:
|
|||
|
||||
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 setTag(int tag) { _controllerTag = tag;}
|
||||
|
|
|
@ -31,11 +31,9 @@ import org.cocos2dx.lib.inputmanagercompat.InputManagerCompat.InputDeviceListene
|
|||
import org.cocos2dx.lib.Cocos2dxActivity;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.InputDevice;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
import android.util.Log;
|
||||
import android.util.SparseArray;
|
||||
|
||||
public abstract class GameControllerActivity extends Cocos2dxActivity implements InputDeviceListener {
|
||||
// ===========================================================
|
||||
|
@ -224,30 +222,11 @@ public abstract class GameControllerActivity extends Cocos2dxActivity implements
|
|||
return handled || super.dispatchGenericMotionEvent(event);
|
||||
}
|
||||
|
||||
protected SparseArray<String> mGameController = null;
|
||||
|
||||
@Override
|
||||
public void onInputDeviceAdded(int deviceId) {
|
||||
Log.d(TAG,"onInputDeviceAdded:" + 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) )
|
||||
{
|
||||
if (mGameController == null) {
|
||||
mGameController = new SparseArray<String>();
|
||||
}
|
||||
String deviceName = device.getName();
|
||||
mGameController.append(deviceId, deviceName);
|
||||
GameControllerAdapter.onConnected(deviceName, deviceId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
mControllerHelper.onInputDeviceAdded(deviceId);
|
||||
}
|
||||
/*
|
||||
* 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) {
|
||||
Log.d(TAG,"onInputDeviceRemoved:" + deviceId);
|
||||
|
||||
if (mGameController != null && mGameController.get(deviceId) != null) {
|
||||
GameControllerAdapter.onDisconnected(mGameController.get(deviceId), deviceId);
|
||||
mGameController.delete(deviceId);
|
||||
}
|
||||
mControllerHelper.onInputDeviceRemoved(deviceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -293,8 +269,10 @@ public abstract class GameControllerActivity extends Cocos2dxActivity implements
|
|||
if (mControllerOuya != null) {
|
||||
mControllerOuya.onResume();
|
||||
}
|
||||
|
||||
mControllerHelper.gatherControllers();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
if (mControllerNibiru != null) {
|
||||
|
|
|
@ -55,7 +55,6 @@ public class GameControllerHelper {
|
|||
|
||||
ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_START, GameControllerDelegate.BUTTON_START);
|
||||
ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_SELECT, GameControllerDelegate.BUTTON_SELECT);
|
||||
//KEYCODE_BUTTON_MODE
|
||||
}
|
||||
|
||||
private float mOldLeftThumbstickX = 0.0f;
|
||||
|
@ -78,68 +77,71 @@ public class GameControllerHelper {
|
|||
|| ((eventSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) )
|
||||
{
|
||||
if (event.getAction() == MotionEvent.ACTION_MOVE) {
|
||||
int devicedId = event.getDeviceId();
|
||||
int deviceId = event.getDeviceId();
|
||||
String deviceName = event.getDevice().getName();
|
||||
if(mGameController.get(deviceId) == null){
|
||||
mGameController.append(deviceId, deviceName);
|
||||
}
|
||||
|
||||
float newAXIS_LX = event.getAxisValue(AXIS_X);
|
||||
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;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_LY = event.getAxisValue(AXIS_Y);
|
||||
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;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_RX = event.getAxisValue(AXIS_Z);
|
||||
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;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_RY = event.getAxisValue(AXIS_RZ);
|
||||
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;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_LTRIGGER = event.getAxisValue(AXIS_LTRIGGER);
|
||||
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;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_RTRIGGER = event.getAxisValue(AXIS_RTRIGGER);
|
||||
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;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_BRAKE = event.getAxisValue(AXIS_BRAKE);
|
||||
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;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_THROTTLE = event.getAxisValue(AXIS_THROTTLE);
|
||||
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;
|
||||
handled = true;
|
||||
}
|
||||
|
||||
float newAXIS_GAS = event.getAxisValue(AXIS_GAS);
|
||||
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;
|
||||
handled = true;
|
||||
}
|
||||
|
@ -162,6 +164,12 @@ public class GameControllerHelper {
|
|||
|| ((eventSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) )
|
||||
{
|
||||
int deviceId = event.getDeviceId();
|
||||
String deviceName = event.getDevice().getName();
|
||||
|
||||
if(mGameController.get(deviceId) == null){
|
||||
mGameController.append(deviceId, deviceName);
|
||||
}
|
||||
|
||||
if (controllerKey == 0) {
|
||||
if (mControllerExtendKey.get(deviceId) != null && mControllerExtendKey.get(deviceId).contains(keyCode)) {
|
||||
controllerKey = keyCode;
|
||||
|
@ -173,10 +181,10 @@ public class GameControllerHelper {
|
|||
int action = event.getAction();
|
||||
if (action == KeyEvent.ACTION_DOWN) {
|
||||
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) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,6 @@ public class GameControllerNibiru implements OnControllerSeviceListener, OnKeyLi
|
|||
OnSimpleStickListener, OnAccListener, OnGyroListener, OnStateListener, GameControllerDelegate {
|
||||
|
||||
private static final String TAG = "NibiruTag";
|
||||
private static final String mVendorName = "Nibiru";
|
||||
|
||||
private Context mContext;
|
||||
private SparseIntArray mKeyMap;
|
||||
|
@ -132,7 +131,14 @@ OnSimpleStickListener, OnAccListener, OnGyroListener, OnStateListener, GameContr
|
|||
}
|
||||
|
||||
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) {
|
||||
mControllerEventListener.onButtonEvent(mVendorName, playerOrder,
|
||||
mKeyMap.get(keyCode), false, 0.0f, false);
|
||||
try {
|
||||
ControllerDevice controllerDevice = mControllerService.getDeviceByPlayerOrder(playerOrder);
|
||||
|
||||
mControllerEventListener.onButtonEvent(controllerDevice.getDeviceName(), controllerDevice.getDeviceId(),
|
||||
mKeyMap.get(keyCode), false, 0.0f, false);
|
||||
} catch (ControllerServiceException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLeftStickChanged(int playerOrder, float x, float y) {
|
||||
if (mControllerEventListener != null) {
|
||||
mControllerEventListener.onAxisEvent(mVendorName, playerOrder,
|
||||
GameControllerDelegate.THUMBSTICK_LEFT_X, x, true);
|
||||
mControllerEventListener.onAxisEvent(mVendorName, playerOrder,
|
||||
GameControllerDelegate.THUMBSTICK_LEFT_Y, y, true);
|
||||
try {
|
||||
ControllerDevice controllerDevice = mControllerService.getDeviceByPlayerOrder(playerOrder);
|
||||
|
||||
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
|
||||
public void onRightStickChanged(int playerOrder, float x, float y) {
|
||||
if (mControllerEventListener != null) {
|
||||
mControllerEventListener.onAxisEvent(mVendorName, playerOrder,
|
||||
GameControllerDelegate.THUMBSTICK_RIGHT_X, x, true);
|
||||
mControllerEventListener.onAxisEvent(mVendorName, playerOrder,
|
||||
GameControllerDelegate.THUMBSTICK_RIGHT_Y, y, true);
|
||||
try {
|
||||
ControllerDevice controllerDevice = mControllerService.getDeviceByPlayerOrder(playerOrder);
|
||||
|
||||
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 (state == ControllerDevice.STATE_CONN)
|
||||
{
|
||||
mControllerEventListener.onConnected(mVendorName, playerOrder);
|
||||
mControllerEventListener.onConnected(device.getDeviceName(), device.getDeviceId());
|
||||
}
|
||||
else if (state == ControllerDevice.STATE_DISCONN)
|
||||
{
|
||||
mControllerEventListener.onDisconnected(mVendorName, playerOrder);
|
||||
mControllerEventListener.onDisconnected(device.getDeviceName(), device.getDeviceId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,8 +10,6 @@ import android.view.KeyEvent;
|
|||
import android.view.MotionEvent;
|
||||
|
||||
public class GameControllerOuya implements GameControllerDelegate{
|
||||
|
||||
public static final String sVendorName = "Ouya";
|
||||
|
||||
private SparseIntArray mKeyMap;
|
||||
|
||||
|
@ -61,27 +59,28 @@ public class GameControllerOuya implements GameControllerDelegate{
|
|||
|
||||
if (handled && mControllerEventListener != null)
|
||||
{
|
||||
OuyaController c = OuyaController.getControllerByDeviceId(event.getDeviceId());
|
||||
int controllerID = OuyaController.getPlayerNumByDeviceId(event.getDeviceId());
|
||||
int deviceId = event.getDeviceId();
|
||||
String deviceName = event.getDevice().getName();
|
||||
OuyaController c = OuyaController.getControllerByDeviceId(deviceId);
|
||||
|
||||
float newLeftTrigger = c.getAxisValue(OuyaController.AXIS_L2);
|
||||
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;
|
||||
}
|
||||
|
||||
float newRightTrigger = c.getAxisValue(OuyaController.AXIS_R2);
|
||||
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;
|
||||
}
|
||||
|
||||
float newLeftThumbstickX = c.getAxisValue(OuyaController.AXIS_LS_X);
|
||||
if (Float.compare(newLeftThumbstickX, mOldLeftThumbstickX) != 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 {
|
||||
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.THUMBSTICK_LEFT_X, newLeftThumbstickX, true);
|
||||
mControllerEventListener.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_LEFT_X, newLeftThumbstickX, true);
|
||||
}
|
||||
mOldLeftThumbstickX = newLeftThumbstickX;
|
||||
}
|
||||
|
@ -89,9 +88,9 @@ public class GameControllerOuya implements GameControllerDelegate{
|
|||
float newLeftThumbstickY = c.getAxisValue(OuyaController.AXIS_LS_Y);
|
||||
if (Float.compare(newLeftThumbstickY, mOldLeftThumbstickY) != 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 {
|
||||
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.THUMBSTICK_LEFT_Y, newLeftThumbstickY, true);
|
||||
mControllerEventListener.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_LEFT_Y, newLeftThumbstickY, true);
|
||||
}
|
||||
mOldLeftThumbstickY = newLeftThumbstickY;
|
||||
}
|
||||
|
@ -99,9 +98,9 @@ public class GameControllerOuya implements GameControllerDelegate{
|
|||
float newRightThumbstickX = c.getAxisValue(OuyaController.AXIS_RS_X);
|
||||
if (Float.compare(newRightThumbstickX, mOldRightThumbstickX) != 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 {
|
||||
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.THUMBSTICK_RIGHT_X, newRightThumbstickX, true);
|
||||
mControllerEventListener.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_RIGHT_X, newRightThumbstickX, true);
|
||||
}
|
||||
mOldRightThumbstickX = newRightThumbstickX;
|
||||
}
|
||||
|
@ -109,9 +108,9 @@ public class GameControllerOuya implements GameControllerDelegate{
|
|||
float newRightThumbstickY = c.getAxisValue(OuyaController.AXIS_RS_Y);
|
||||
if (Float.compare(newRightThumbstickY, mOldRightThumbstickY) != 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 {
|
||||
mControllerEventListener.onAxisEvent(sVendorName, controllerID, GameControllerDelegate.THUMBSTICK_RIGHT_Y, newRightThumbstickY, true);
|
||||
mControllerEventListener.onAxisEvent(deviceName, deviceId, GameControllerDelegate.THUMBSTICK_RIGHT_Y, newRightThumbstickY, true);
|
||||
}
|
||||
mOldRightThumbstickY = newRightThumbstickY;
|
||||
}
|
||||
|
@ -122,9 +121,14 @@ public class GameControllerOuya implements GameControllerDelegate{
|
|||
|
||||
public boolean dispatchKeyEvent(KeyEvent event) {
|
||||
boolean handled = false;
|
||||
|
||||
int action = event.getAction();
|
||||
int keyCode = event.getKeyCode();
|
||||
|
||||
if (keyCode == KeyEvent.KEYCODE_BUTTON_L2 || keyCode == KeyEvent.KEYCODE_BUTTON_R2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (action == KeyEvent.ACTION_DOWN) {
|
||||
handled = OuyaController.onKeyDown(keyCode, event);
|
||||
}
|
||||
|
@ -139,11 +143,10 @@ public class GameControllerOuya implements GameControllerDelegate{
|
|||
isAnalog = true;
|
||||
}
|
||||
|
||||
int controllerID = OuyaController.getPlayerNumByDeviceId(event.getDeviceId());
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -73,9 +73,9 @@ void GameControllerTest::onConnectController(Controller* controller, Event* even
|
|||
this->addChild(_secondHolder._holderNode);
|
||||
}
|
||||
|
||||
char deviceId[20];
|
||||
sprintf(deviceId,"device id:%d",controller->getDeviceId());
|
||||
_secondHolder._deviceLabel->setString(deviceId);
|
||||
char deviceInfo[50];
|
||||
sprintf(deviceInfo,"%s id:%d",controller->getDeviceName().c_str(), controller->getDeviceId());
|
||||
_secondHolder._deviceLabel->setString(deviceInfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -93,9 +93,9 @@ void GameControllerTest::onConnectController(Controller* controller, Event* even
|
|||
_firstHolder._holderNode->setPosition(Vec2(_visibleThreeQuarterX, _visibleCentreY));
|
||||
this->addChild(_firstHolder._holderNode);
|
||||
}
|
||||
char deviceId[20];
|
||||
sprintf(deviceId,"device id:%d",controller->getDeviceId());
|
||||
_firstHolder._deviceLabel->setString(deviceId);
|
||||
char deviceInfo[50];
|
||||
sprintf(deviceInfo,"%s id:%d",controller->getDeviceName().c_str(), controller->getDeviceId());
|
||||
_firstHolder._deviceLabel->setString(deviceInfo);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -202,7 +202,7 @@ void GameControllerTest::showButtonState(cocos2d::Controller *controller, int ke
|
|||
default:
|
||||
{
|
||||
char ketStatus[30];
|
||||
sprintf(ketStatus,"External Key Down:%d",keyCode);
|
||||
sprintf(ketStatus,"Key Down:%d",keyCode);
|
||||
holder->_externalKeyLabel->setString(ketStatus);
|
||||
break;
|
||||
}
|
||||
|
@ -245,7 +245,7 @@ void GameControllerTest::showButtonState(cocos2d::Controller *controller, int ke
|
|||
default:
|
||||
{
|
||||
char ketStatus[30];
|
||||
sprintf(ketStatus,"External Key Up:%d",keyCode);
|
||||
sprintf(ketStatus,"Key Up:%d",keyCode);
|
||||
holder->_externalKeyLabel->setString(ketStatus);
|
||||
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)
|
||||
{
|
||||
//onConnectController(controller,nullptr);
|
||||
log("controller:%d,keyCode:%d",controller,keyCode);
|
||||
ControllerHolder* holder = nullptr;
|
||||
if (controller == _firstHolder.controller)
|
||||
holder = &_firstHolder;
|
||||
|
@ -367,7 +365,7 @@ void GameControllerTest::createControllerSprite(ControllerHolder& holder)
|
|||
holder._deviceLabel->setTextColor(Color4B::RED);
|
||||
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->setTextColor(Color4B::RED);
|
||||
holder._holderNode->addChild(holder._externalKeyLabel);
|
||||
|
|
Loading…
Reference in New Issue