mirror of https://github.com/axmolengine/axmol.git
issue #4895:Get events from controller to engine[android]
This commit is contained in:
parent
5ed581758e
commit
74579f19f5
|
@ -12,4 +12,5 @@
|
||||||
|
|
||||||
android.library=true
|
android.library=true
|
||||||
# Project target.
|
# Project target.
|
||||||
target=android-10
|
target=android-16
|
||||||
|
android.library.reference.1=../ControllerDelegate
|
||||||
|
|
|
@ -24,6 +24,9 @@ THE SOFTWARE.
|
||||||
package org.cocos2dx.lib;
|
package org.cocos2dx.lib;
|
||||||
|
|
||||||
import org.cocos2dx.lib.Cocos2dxHelper.Cocos2dxHelperListener;
|
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.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
@ -33,12 +36,15 @@ import android.content.pm.PackageManager;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
|
import android.view.InputDevice;
|
||||||
|
import android.view.KeyEvent;
|
||||||
|
import android.view.MotionEvent;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.FrameLayout;
|
import android.widget.FrameLayout;
|
||||||
import android.preference.PreferenceManager.OnActivityResultListener;
|
import android.preference.PreferenceManager.OnActivityResultListener;
|
||||||
|
|
||||||
public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelperListener {
|
public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelperListener, InputDeviceListener {
|
||||||
// ===========================================================
|
// ===========================================================
|
||||||
// Constants
|
// Constants
|
||||||
// ===========================================================
|
// ===========================================================
|
||||||
|
@ -53,29 +59,65 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
|
||||||
private Cocos2dxHandler mHandler;
|
private Cocos2dxHandler mHandler;
|
||||||
private static Cocos2dxActivity sContext = null;
|
private static Cocos2dxActivity sContext = null;
|
||||||
private Cocos2dxVideoHelper mVideoHelper = null;
|
private Cocos2dxVideoHelper mVideoHelper = null;
|
||||||
|
private InputManagerCompat mInputManager = null;
|
||||||
|
|
||||||
|
protected GameControllerHelper mControllerHelper = null;
|
||||||
|
protected GameControllerDelegate mControllerDelegate = null;
|
||||||
|
|
||||||
public static Context getContext() {
|
public static Context getContext() {
|
||||||
return sContext;
|
return sContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected void onLoadNativeLibraries() {
|
protected void onLoadNativeLibraries() {
|
||||||
try {
|
try {
|
||||||
ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
|
ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
|
||||||
Bundle bundle = ai.metaData;
|
Bundle bundle = ai.metaData;
|
||||||
try {
|
|
||||||
String libName = bundle.getString("android.app.lib_name");
|
String libName = bundle.getString("android.app.lib_name");
|
||||||
System.loadLibrary(libName);
|
System.loadLibrary(libName);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// ERROR
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
} catch (PackageManager.NameNotFoundException e) {
|
|
||||||
// ERROR
|
|
||||||
e.printStackTrace();
|
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
|
// Constructors
|
||||||
// ===========================================================
|
// ===========================================================
|
||||||
|
@ -95,6 +137,16 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
|
||||||
if (mVideoHelper == null) {
|
if (mVideoHelper == null) {
|
||||||
mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout);
|
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,22 +157,112 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
|
||||||
// Methods for/from SuperClass/Interfaces
|
// 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
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
Cocos2dxHelper.onResume();
|
Cocos2dxHelper.onResume();
|
||||||
this.mGLSurfaceView.onResume();
|
this.mGLSurfaceView.onResume();
|
||||||
|
|
||||||
|
if (mControllerDelegate != null) {
|
||||||
|
mControllerDelegate.onResume();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
|
if (mControllerDelegate != null) {
|
||||||
|
mControllerDelegate.onPause();
|
||||||
|
}
|
||||||
|
|
||||||
super.onPause();
|
super.onPause();
|
||||||
|
|
||||||
Cocos2dxHelper.onPause();
|
Cocos2dxHelper.onPause();
|
||||||
this.mGLSurfaceView.onPause();
|
this.mGLSurfaceView.onPause();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDestroy() {
|
||||||
|
if (mControllerDelegate != null) {
|
||||||
|
mControllerDelegate.onDestroy();
|
||||||
|
}
|
||||||
|
mControllerHelper.destrory();
|
||||||
|
|
||||||
|
super.onDestroy();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void showDialog(final String pTitle, final String pMessage) {
|
public void showDialog(final String pTitle, final String pMessage) {
|
||||||
Message msg = new Message();
|
Message msg = new Message();
|
||||||
|
|
|
@ -33,9 +33,7 @@ import java.lang.Runnable;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.ActivityInfo;
|
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.content.res.AssetManager;
|
import android.content.res.AssetManager;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.preference.PreferenceManager.OnActivityResultListener;
|
import android.preference.PreferenceManager.OnActivityResultListener;
|
||||||
|
|
Loading…
Reference in New Issue