Let's let Moga start actually handling Moga events, Fix Rjoy

This commit is contained in:
TwistedUmbrella 2014-02-24 22:23:04 -05:00
parent 326fcec485
commit fa2b229c95
3 changed files with 121 additions and 85 deletions

View File

@ -272,11 +272,7 @@ public class GL2JNIActivity extends Activity {
if (playerNum == null || playerNum == -1)
return false;
if (pad.isActiveMoga[playerNum]) {
return false;
}
if (!pad.compat[playerNum]) {
if (!pad.compat[playerNum] && !pad.isActiveMoga[playerNum]) {
// TODO: Moga should handle this locally
// Joystick
@ -303,14 +299,15 @@ public class GL2JNIActivity extends Activity {
if (prefs.getBoolean("right_buttons", true)) {
if (RS_Y > 0.5) {
handle_key(playerNum, pad.map[playerNum][0], true);
handle_key(playerNum, pad.map[playerNum][1], false);
handle_key(playerNum, pad.map[playerNum][0]/* A */, true);
pad.wasKeyStick[playerNum] = true;
} else if (RS_Y < 0.5) {
handle_key(playerNum, pad.map[playerNum][0], false);
handle_key(playerNum, pad.map[playerNum][1], true);
} else {
handle_key(playerNum, pad.map[playerNum][1]/* B */, true);
pad.wasKeyStick[playerNum] = true;
} else if (pad.wasKeyStick[playerNum]){
handle_key(playerNum, pad.map[playerNum][0], false);
handle_key(playerNum, pad.map[playerNum][1], false);
pad.wasKeyStick[playerNum] = false;
}
} else {
if (RS_Y > 0.5) {
@ -320,9 +317,8 @@ public class GL2JNIActivity extends Activity {
}
}
}
mView.pushInput();
}
mView.pushInput();
if ((pad.globalLS_X[playerNum] == pad.previousLS_X[playerNum] && pad.globalLS_Y[playerNum] == pad.previousLS_Y[playerNum])
|| (pad.previousLS_X[playerNum] == 0.0f && pad.previousLS_Y[playerNum] == 0.0f))
// Only handle Left Stick on an Xbox 360 controller if there was
@ -379,7 +375,7 @@ public class GL2JNIActivity extends Activity {
* };
*/
boolean handle_key(Integer playerNum, int kc, boolean down) {
public boolean handle_key(Integer playerNum, int kc, boolean down) {
if (playerNum == null || playerNum == -1)
return false;
if (pad.isActiveMoga[playerNum]) {
@ -461,6 +457,10 @@ public class GL2JNIActivity extends Activity {
LayoutParams.WRAP_CONTENT);
}
public GL2JNIView getGameView() {
return mView;
}
public boolean onKeyUp(int keyCode, KeyEvent event) {
Integer playerNum = Arrays.asList(pad.name).indexOf(event.getDeviceId());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD && playerNum == -1) {
@ -470,7 +470,7 @@ public class GL2JNIActivity extends Activity {
playerNum = -1;
}
if (playerNum != null && playerNum != -1) {
if (playerNum != null && playerNum != -1 && !pad.isActiveMoga[playerNum]) {
if (pad.compat[playerNum] || pad.custom[playerNum]) {
String id = pad.portId[playerNum];
if (keyCode == prefs.getInt("l_button" + id,
@ -482,8 +482,15 @@ public class GL2JNIActivity extends Activity {
}
}
return handle_key(playerNum, keyCode, false)
|| super.onKeyUp(keyCode, event);
if (!pad.isActiveMoga[playerNum]) {
return handle_key(playerNum, keyCode, false)
|| super.onKeyUp(keyCode, event);
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP
|| keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
return super.onKeyUp(keyCode, event);
} else {
return true;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
@ -494,8 +501,8 @@ public class GL2JNIActivity extends Activity {
} else {
playerNum = -1;
}
if (playerNum != null && playerNum != -1) {
if (playerNum != null && playerNum != -1 && !pad.isActiveMoga[playerNum]) {
if (pad.compat[playerNum] || pad.custom[playerNum]) {
String id = pad.portId[playerNum];
if (keyCode == prefs.getInt("l_button" + id, KeyEvent.KEYCODE_BUTTON_L1)) {
@ -507,10 +514,12 @@ public class GL2JNIActivity extends Activity {
}
}
if (handle_key(playerNum, keyCode, true)) {
if (playerNum == 0)
JNIdc.hide_osd();
return true;
if (!pad.isActiveMoga[playerNum]) {
if (handle_key(playerNum, keyCode, true)) {
if (playerNum == 0)
JNIdc.hide_osd();
return true;
}
}
if (keyCode == KeyEvent.KEYCODE_BUTTON_SELECT) {
@ -530,7 +539,14 @@ public class GL2JNIActivity extends Activity {
return showMenu();
}
}
return super.onKeyDown(keyCode, event);
if (!pad.isActiveMoga[playerNum]) {
return super.onKeyDown(keyCode, event);
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP
|| keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
return super.onKeyUp(keyCode, event);
} else {
return true;
}
}
private boolean showMenu() {

View File

@ -21,6 +21,7 @@ public class Gamepad {
public int[] name = { -1, -1, -1, -1 };
public float[] globalLS_X = new float[4], globalLS_Y = new float[4],
previousLS_X = new float[4], previousLS_Y = new float[4];
public boolean[] wasKeyStick = { false, false, false, false };
public int map[][] = new int[4][];
public SparseArray<String> deviceId_deviceDescriptor = new SparseArray<String>();

View File

@ -3,13 +3,14 @@ package com.reicast.emulator.periph;
/******************************************************************************/
import java.util.HashMap;
import java.util.Arrays;
import tv.ouya.console.api.OuyaController;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.SparseArray;
import android.widget.Toast;
import com.bda.controller.Controller;
@ -17,6 +18,7 @@ import com.bda.controller.ControllerListener;
import com.bda.controller.KeyEvent;
import com.bda.controller.MotionEvent;
import com.bda.controller.StateEvent;
import com.reicast.emulator.GL2JNIActivity;
import com.reicast.emulator.R;
import com.reicast.emulator.emu.GL2JNIView;
import com.reicast.emulator.emu.JNIdc;
@ -133,83 +135,100 @@ public class MOGAInput
{
public void onKeyEvent(KeyEvent event)
{
Integer playerNum = pad.deviceDescriptor_PlayerNum.get(pad.deviceId_deviceDescriptor.get(event.getControllerId()));
if (playerNum == null)
Integer playerNum = Arrays.asList(pad.name).indexOf(event.getControllerId());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD && playerNum == -1) {
playerNum = pad.deviceDescriptor_PlayerNum
.get(pad.deviceId_deviceDescriptor.get(event.getControllerId()));
} else {
playerNum = -1;
}
if (playerNum == null || playerNum == -1)
return;
String id = pad.portId[playerNum];
if (pad.custom[playerNum]) {
if (event.getKeyCode() == prefs.getInt("l_button" + id, KeyEvent.KEYCODE_BUTTON_L1)) {
if (playerNum != null && playerNum != -1) {
String id = pad.portId[playerNum];
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (event.getKeyCode() == prefs.getInt("l_button" + id, KeyEvent.KEYCODE_BUTTON_L1)) {
simulatedTouchEvent(playerNum, 1.0f, 0.0f);
simulatedTouchEvent(playerNum, 0.0f, 0.0f);
}
if (event.getKeyCode() == prefs.getInt("r_button" + id, KeyEvent.KEYCODE_BUTTON_R1)) {
} else if (event.getKeyCode() == prefs.getInt("r_button" + id, KeyEvent.KEYCODE_BUTTON_R1)) {
simulatedTouchEvent(playerNum, 0.0f, 1.0f);
simulatedTouchEvent(playerNum, 0.0f, 0.0f);
} else if (((GL2JNIActivity) act).handle_key(playerNum, event.getKeyCode(), true)) {
if (playerNum == 0)
JNIdc.hide_osd();
}
}
if (event.getAction() == KeyEvent.ACTION_UP) {
if (event.getKeyCode() == prefs.getInt("l_button" + id,
KeyEvent.KEYCODE_BUTTON_L1)
|| event.getKeyCode() == prefs.getInt("r_button" + id,
KeyEvent.KEYCODE_BUTTON_R1)) {
simulatedTouchEvent(playerNum, 0.0f, 0.0f);
} else {
((GL2JNIActivity) act).handle_key(playerNum, event.getKeyCode(), false);
}
}
if(playerNum == 0)
JNIdc.hide_osd();
for (int i = 0; i < pad.map.length; i += 2) {
if (pad.map[playerNum][i + 0] == event.getKeyCode()) {
if (event.getAction() == 0) //FIXME to const
GL2JNIView.kcode_raw[playerNum] &= ~pad.map[playerNum][i + 1];
else
GL2JNIView.kcode_raw[playerNum] |= pad.map[playerNum][i + 1];
break;
}
}
}
public void simulatedTouchEvent(int playerNum, float L2, float R2) {
if(playerNum == 0)
JNIdc.hide_osd();
pad.previousLS_X[playerNum] = pad.globalLS_X[playerNum];
pad.previousLS_Y[playerNum] = pad.globalLS_Y[playerNum];
pad.globalLS_X[playerNum] = 0;
pad.globalLS_Y[playerNum] = 0;
GL2JNIView.lt[playerNum] = (int) (L2 * 255);
GL2JNIView.rt[playerNum] = (int) (R2 * 255);
GL2JNIView.jx[playerNum] = (int) (0 * 126);
GL2JNIView.jy[playerNum] = (int) (0 * 126);
}
public void onMotionEvent(MotionEvent event)
{
Integer playerNum = pad.deviceDescriptor_PlayerNum.get(pad.deviceId_deviceDescriptor.get(event.getControllerId()));
if (playerNum == null)
Integer playerNum = Arrays.asList(pad.name).indexOf(event.getControllerId());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD && playerNum == -1) {
playerNum = pad.deviceDescriptor_PlayerNum
.get(pad.deviceId_deviceDescriptor.get(event.getControllerId()));
} else {
playerNum = -1;
}
if (playerNum == null || playerNum == -1)
return;
if (!pad.compat[playerNum]) {
// Joystick
// do other things with joystick
float LS_X = event.getAxisValue(OuyaController.AXIS_LS_X);
float LS_Y = event.getAxisValue(OuyaController.AXIS_LS_Y);
float RS_X = event.getAxisValue(OuyaController.AXIS_RS_X);
float RS_Y = event.getAxisValue(OuyaController.AXIS_RS_Y);
float L2 = event.getAxisValue(OuyaController.AXIS_L2);
float R2 = event.getAxisValue(OuyaController.AXIS_R2);
if(playerNum == 0)
JNIdc.hide_osd();
pad.previousLS_X[playerNum] = pad.globalLS_X[playerNum];
pad.previousLS_Y[playerNum] = pad.globalLS_Y[playerNum];
pad.globalLS_X[playerNum] = LS_X;
pad.globalLS_Y[playerNum] = LS_Y;
float S_X = event.getAxisValue(MotionEvent.AXIS_X);
float S_Y = event.getAxisValue(MotionEvent.AXIS_Y);
float L2 = event.getAxisValue(MotionEvent.AXIS_LTRIGGER);
float R2 = event.getAxisValue(MotionEvent.AXIS_RTRIGGER);
GL2JNIView.lt[playerNum] = (int) (L2 * 255);
GL2JNIView.rt[playerNum] = (int) (R2 * 255);
pad.previousLS_X[playerNum] = pad.globalLS_X[playerNum];
pad.previousLS_Y[playerNum] = pad.globalLS_Y[playerNum];
pad.globalLS_X[playerNum] = S_X;
pad.globalLS_Y[playerNum] = S_Y;
GL2JNIView.jx[playerNum] = (int) (LS_X * 126);
GL2JNIView.jy[playerNum] = (int) (LS_Y * 126);
if (prefs.getBoolean("right_buttons", true)) {
if (RS_Y > 0.5) {
((GL2JNIActivity) act).handle_key(playerNum, pad.map[playerNum][0]/* A */, true);
pad.wasKeyStick[playerNum] = true;
} else if (RS_Y < 0.5) {
((GL2JNIActivity) act).handle_key(playerNum, pad.map[playerNum][1]/* B */, true);
pad.wasKeyStick[playerNum] = true;
} else if (pad.wasKeyStick[playerNum]){
((GL2JNIActivity) act).handle_key(playerNum, pad.map[playerNum][0], false);
((GL2JNIActivity) act).handle_key(playerNum, pad.map[playerNum][1], false);
pad.wasKeyStick[playerNum] = false;
}
} else {
if (RS_Y > 0.5) {
GL2JNIView.rt[playerNum] = (int) (RS_Y * 255);
} else if (RS_Y < 0.5) {
GL2JNIView.lt[playerNum] = (int) (-(RS_Y) * 255);
}
}
((GL2JNIActivity) act).getGameView().pushInput();
}
}
public boolean simulatedTouchEvent(int playerNum, float L2, float R2) {
GL2JNIView.lt[playerNum] = (int) (L2 * 255);
GL2JNIView.rt[playerNum] = (int) (R2 * 255);
GL2JNIView.jx[playerNum] = (int) (S_X * 126);
GL2JNIView.jy[playerNum] = (int) (S_Y * 126);
/*
for(final Entry<Integer, ExampleFloat> entry : mMotions.entrySet())
{
final int key = entry.getKey();
final ExampleFloat value = entry.getValue();
value.mValue = event.getAxisValue(key);
}*/
((GL2JNIActivity) act).getGameView().pushInput();
return true;
}
public void onStateEvent(StateEvent event)