Merge pull request #7286 from Dhilan007/v3-gc0702

fixed can't get valid value from trigger button.
This commit is contained in:
minggo 2014-07-02 16:24:24 +08:00
commit 1a089f4a18
5 changed files with 59 additions and 14 deletions

View File

@ -121,8 +121,7 @@ public:
void sendEventButton(ControllerButtonInput* button, bool isPressed, float value, bool isAnalog)
{
button->setPressed(isPressed);
if (!isAnalog)
button->setValue(value);
button->setValue(value);
button->setAnalog(isAnalog);
EventController evt(EventController::ControllerEventType::BUTTON_STATUS_CHANGED, _controller, button);
Director::getInstance()->getEventDispatcher()->dispatchEvent(&evt);

View File

@ -1,5 +1,6 @@
package org.cocos2dx.lib;
import android.util.Log;
import android.util.SparseIntArray;
import android.view.InputDevice;
import android.view.KeyEvent;
@ -17,6 +18,7 @@ public class GameControllerHelper {
private static final int AXIS_RZ = 14;
private static final int AXIS_LTRIGGER = 17;
private static final int AXIS_RTRIGGER = 18;
public static final int AXIS_GAS = 22;
private static final int AXIS_BRAKE = 23;
private static final int AXIS_THROTTLE = 19;
@ -63,6 +65,7 @@ public class GameControllerHelper {
private float mOldRightTrigger = 0.0f;
private float mOldThrottle = 0.0f;
private float mOldBrake = 0.0f;
private float mOldGas = 0.0f;
public boolean dispatchGenericMotionEvent(MotionEvent event) {
boolean handled = false;
@ -146,6 +149,17 @@ public class GameControllerHelper {
mOldThrottle = newAXIS_THROTTLE;
handled = true;
}
float newAXIS_GAS = event.getAxisValue(AXIS_GAS);
if (Float.compare(newAXIS_GAS , mOldGas) != 0) {
if (Float.compare(newAXIS_GAS, 0.0f) == 0) {
GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, false, 0.0f, true);
}else {
GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, true, newAXIS_GAS, true);
}
mOldGas = newAXIS_GAS;
handled = true;
}
}
}

View File

@ -21,6 +21,9 @@ public class GameControllerMoga implements ControllerListener, GameControllerDel
private float mOldRightThumbstickX = 0.0f;
private float mOldRightThumbstickY = 0.0f;
private float mOldLeftTrigger = 0.0f;
private float mOldRightTrigger = 0.0f;
private SparseIntArray mKeyMap = null;
public GameControllerMoga() {
@ -61,8 +64,12 @@ public class GameControllerMoga implements ControllerListener, GameControllerDel
}
public void onKeyEvent(KeyEvent event) {
boolean isPressed = event.getAction() == KeyEvent.ACTION_DOWN;
int keycode = event.getKeyCode();
if (keycode == KeyEvent.KEYCODE_BUTTON_L2
|| keycode == KeyEvent.KEYCODE_BUTTON_R2) {
return;
}
boolean isPressed = event.getAction() == KeyEvent.ACTION_DOWN;
boolean isAnalog = false;
if (keycode == KeyEvent.KEYCODE_BUTTON_THUMBL
@ -82,10 +89,12 @@ public class GameControllerMoga implements ControllerListener, GameControllerDel
if (mControllerEventListener == null) {
return;
}
int controllerId = event.getControllerId();
float newLeftThumbstickX = event.getAxisValue(MotionEvent.AXIS_X);
if (newLeftThumbstickX != mOldLeftThumbstickX) {
mControllerEventListener.onAxisEvent(mVendorName,
event.getControllerId(),
controllerId,
GameControllerDelegate.THUMBSTICK_LEFT_X,
newLeftThumbstickX, true);
mOldLeftThumbstickX = newLeftThumbstickX;
@ -94,7 +103,7 @@ public class GameControllerMoga implements ControllerListener, GameControllerDel
float newLeftThumbstickY = event.getAxisValue(MotionEvent.AXIS_Y);
if (newLeftThumbstickY != mOldLeftThumbstickY) {
mControllerEventListener.onAxisEvent(mVendorName,
event.getControllerId(),
controllerId,
GameControllerDelegate.THUMBSTICK_LEFT_Y,
newLeftThumbstickY, true);
mOldLeftThumbstickY = newLeftThumbstickY;
@ -103,7 +112,7 @@ public class GameControllerMoga implements ControllerListener, GameControllerDel
float newRightThumbstickX = event.getAxisValue(MotionEvent.AXIS_Z);
if (newRightThumbstickX != mOldRightThumbstickX) {
mControllerEventListener.onAxisEvent(mVendorName,
event.getControllerId(),
controllerId,
GameControllerDelegate.THUMBSTICK_RIGHT_X,
newRightThumbstickX, true);
mOldRightThumbstickX = newRightThumbstickX;
@ -112,11 +121,39 @@ public class GameControllerMoga implements ControllerListener, GameControllerDel
float newRightThumbstickY = event.getAxisValue(MotionEvent.AXIS_RZ);
if (newRightThumbstickY != mOldRightThumbstickY) {
mControllerEventListener.onAxisEvent(mVendorName,
event.getControllerId(),
controllerId,
GameControllerDelegate.THUMBSTICK_RIGHT_Y,
newRightThumbstickY, true);
mOldRightThumbstickY = newRightThumbstickY;
}
float newLeftTrigger = event.getAxisValue(MotionEvent.AXIS_LTRIGGER);
if (newLeftTrigger != mOldLeftTrigger) {
boolean isPressed = true;
if (Float.compare(newLeftTrigger, 0.0f) == 0) {
isPressed = false;
}
mControllerEventListener.onButtonEvent(mVendorName,
controllerId,
GameControllerDelegate.BUTTON_LEFT_TRIGGER,
isPressed,
newLeftTrigger, true);
mOldLeftTrigger = newLeftTrigger;
}
float newRightTrigger = event.getAxisValue(MotionEvent.AXIS_RTRIGGER);
if (newRightTrigger != mOldRightTrigger) {
boolean isPressed = true;
if (Float.compare(newRightTrigger, 0.0f) == 0) {
isPressed = false;
}
mControllerEventListener.onButtonEvent(mVendorName,
controllerId,
GameControllerDelegate.BUTTON_RIGHT_TRIGGER,
isPressed,
newRightTrigger, true);
mOldRightTrigger = newRightTrigger;
}
}
@Override

View File

@ -10,4 +10,4 @@
# Project target.
target=android-10
android.library.reference.1=../../../cocos/platform/android/ControllerAutoAdapter
android.library.reference.1=../../../cocos/platform/android/ControllerManualAdapter

View File

@ -36,12 +36,7 @@ public class AppActivity extends GameControllerActivity {
//The standard controller,without doing anything special. e.g: Amazon Fire TV
//Automatic adaptation for connect controller.
//Supported Platform: Nibiru / Moga / Ouya TV
this.connectController();
//Manually specify an adapter.
//Requirements: using libControllerDriveAdapter project
//this.connectController(DRIVERTYPE_NIBIRU);
//this.connectController(DRIVERTYPE_MOGA);
//this.connectController(DRIVERTYPE_OUYA);