issue #4895:Get events from controller to engine[android]

This commit is contained in:
Dhilan007 2014-06-30 03:23:51 +08:00
parent 5ed581758e
commit 74579f19f5
3 changed files with 157 additions and 16 deletions

View File

@ -12,4 +12,5 @@
android.library=true
# Project target.
target=android-10
target=android-16
android.library.reference.1=../ControllerDelegate

View File

@ -24,6 +24,9 @@ THE SOFTWARE.
package org.cocos2dx.lib;
import org.cocos2dx.lib.Cocos2dxHelper.Cocos2dxHelperListener;
import org.cocos2dx.lib.GameControllerDelegate.ControllerEventListener;
import org.cocos2dx.lib.inputmanagercompat.InputManagerCompat;
import org.cocos2dx.lib.inputmanagercompat.InputManagerCompat.InputDeviceListener;
import android.app.Activity;
import android.content.Context;
@ -33,12 +36,15 @@ import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.util.Log;
import android.widget.FrameLayout;
import android.preference.PreferenceManager.OnActivityResultListener;
public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelperListener {
public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelperListener, InputDeviceListener {
// ===========================================================
// Constants
// ===========================================================
@ -50,32 +56,68 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
// ===========================================================
private Cocos2dxGLSurfaceView mGLSurfaceView;
private Cocos2dxHandler mHandler;
private Cocos2dxHandler mHandler;
private static Cocos2dxActivity sContext = null;
private Cocos2dxVideoHelper mVideoHelper = null;
private InputManagerCompat mInputManager = null;
protected GameControllerHelper mControllerHelper = null;
protected GameControllerDelegate mControllerDelegate = null;
public static Context getContext() {
return sContext;
}
protected void onLoadNativeLibraries() {
try {
ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
try {
String libName = bundle.getString("android.app.lib_name");
System.loadLibrary(libName);
} catch (Exception e) {
// ERROR
e.printStackTrace();
}
} catch (PackageManager.NameNotFoundException e) {
// ERROR
String libName = bundle.getString("android.app.lib_name");
System.loadLibrary(libName);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setGameControllerInstance(GameControllerDelegate controllerDelegate) {
if (mControllerDelegate != null) {
mControllerDelegate.onDestroy();
mControllerDelegate = null;
}
mControllerDelegate = controllerDelegate;
mControllerDelegate.setControllerEventListener(mControllerEventListener);
mControllerDelegate.onCreate(this);
}
public GameControllerDelegate getGameControllerInstance(){
return mControllerDelegate;
}
ControllerEventListener mControllerEventListener = new ControllerEventListener() {
@Override
public void onButtonEvent(String vendorName, int controller, int button,
boolean isPressed, float value, boolean isAnalog) {
GameControllerAdapter.onButtonEvent(vendorName, controller, button, isPressed, value, isAnalog);
}
@Override
public void onAxisEvent(String vendorName, int controller, int axisID,
float value, boolean isAnalog) {
GameControllerAdapter.onAxisEvent(vendorName, controller, axisID, value, isAnalog);
}
@Override
public void onConnected(String vendorName, int controller) {
GameControllerAdapter.onConnected(vendorName, controller);
}
@Override
public void onDisconnected(String vendorName, int controller) {
GameControllerAdapter.onDisconnected(vendorName, controller);
}
};
// ===========================================================
// Constructors
// ===========================================================
@ -95,6 +137,16 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
if (mVideoHelper == null) {
mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout);
}
mInputManager = InputManagerCompat.Factory.getInputManager(this);
mInputManager.registerInputDeviceListener(this, null);
if (mControllerDelegate != null) {
mControllerDelegate.onCreate(this);
}
if (mControllerHelper == null) {
mControllerHelper = new GameControllerHelper(this);
}
}
// ===========================================================
@ -105,21 +157,111 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
boolean handled = false;
if (mControllerDelegate != null) {
handled = mControllerDelegate.dispatchKeyEvent(event);
}
else {
handled = mControllerHelper.dispatchKeyEvent(event);
}
return handled || super.dispatchKeyEvent(event);
}
@Override
public boolean dispatchGenericMotionEvent(MotionEvent event) {
boolean handled = false;
if (mControllerDelegate != null) {
handled = mControllerDelegate.dispatchGenericMotionEvent(event);
}else {
handled = mControllerHelper.dispatchGenericMotionEvent(event);
}
return handled || super.dispatchGenericMotionEvent(event);
}
@Override
public void onInputDeviceAdded(int deviceId) {
Log.d(TAG,"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) )
{
GameControllerAdapter.onConnected("Standard", deviceId);
}
}
/*
* This is an unusual case. Input devices don't typically change, but they
* certainly can --- for example a device may have different modes. We use
* this to make sure that the ship has an up-to-date InputDevice.
*
* @see
* com.example.inputmanagercompat.InputManagerCompat.InputDeviceListener
* #onInputDeviceChanged(int)
*/
@Override
public void onInputDeviceChanged(int deviceId) {
Log.d(TAG,"onInputDeviceChanged:" + deviceId);
}
/*
* Remove any ship associated with the ID.
*
* @see
* com.example.inputmanagercompat.InputManagerCompat.InputDeviceListener
* #onInputDeviceRemoved(int)
*/
@Override
public void onInputDeviceRemoved(int deviceId) {
Log.d(TAG,"onInputDeviceRemoved:" + 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) )
{
GameControllerAdapter.onDisconnected("Standard", deviceId);
}
}
@Override
protected void onResume() {
super.onResume();
Cocos2dxHelper.onResume();
this.mGLSurfaceView.onResume();
if (mControllerDelegate != null) {
mControllerDelegate.onResume();
}
}
@Override
protected void onPause() {
if (mControllerDelegate != null) {
mControllerDelegate.onPause();
}
super.onPause();
Cocos2dxHelper.onPause();
this.mGLSurfaceView.onPause();
}
@Override
protected void onDestroy() {
if (mControllerDelegate != null) {
mControllerDelegate.onDestroy();
}
mControllerHelper.destrory();
super.onDestroy();
}
@Override
public void showDialog(final String pTitle, final String pMessage) {

View File

@ -33,9 +33,7 @@ import java.lang.Runnable;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.os.Build;
import android.preference.PreferenceManager.OnActivityResultListener;