Commit a57e52e0 authored by kenan's avatar kenan

GameActivity

parent b334e11d
......@@ -27,15 +27,6 @@ namespace ygo {
Game *mainGame;
void Game::process(irr::SEvent &event) {
if (event.EventType == EET_MOUSE_INPUT_EVENT) {
s32 x = event.MouseInput.X;
s32 y = event.MouseInput.Y;
event.MouseInput.X = optX(x);
event.MouseInput.Y = optY(y);
}
}
#ifdef _IRR_ANDROID_PLATFORM_
void Game::stopBGM() {
......@@ -123,9 +114,7 @@ bool Game::Initialize(ANDROID_APP app, android::InitOptions *options) {
if (!android::perfromTrick(app)) {
return false;
}
core::position2di appPosition = android::initJavaBridge(app, device);
setPositionFix(appPosition);
device->setProcessReceiver(this);
android::initJavaBridge(app, device);
app->onInputEvent = android::handleInput;
ILogger* logger = device->getLogger();
......@@ -892,6 +881,7 @@ bool Game::Initialize(ANDROID_APP app, android::InitOptions *options) {
wDMQuery = env->addWindow(rect<s32>(490 * xScale, 180 * yScale, 840 * xScale, 340 * yScale), false, dataManager->GetSysString(1460));
wDMQuery->getCloseButton()->setVisible(false);
wDMQuery->setVisible(false);
wDMQuery->setDraggable(false);
ChangeToIGUIImageWindow(wDMQuery, bgDMQuery, imageManager->tDialog_L);
stDMMessage = env->addStaticText(L"", rect<s32>(20 * xScale, 25 * yScale, 290 * xScale, 45 * yScale), false, false, wDMQuery);
stDMMessage2 = env->addStaticText(L"", rect<s32>(20 * xScale, 50 * yScale, 330 * xScale, 90 * yScale), false, false, wDMQuery, -1, true);
......
......@@ -129,7 +129,7 @@ struct FadingUnit {
irr::core::vector2di fadingDiff;
};
class Game :IProcessEventReceiver{
class Game{
public:
#ifdef _IRR_ANDROID_PLATFORM_
......@@ -662,27 +662,6 @@ public:
Signal externalSignal;
static void onHandleAndroidCommand(ANDROID_APP app, int32_t cmd);
#endif
void setPositionFix(core::position2di fix){
InputFix = fix;
}
float optX(float x) {
float x2 = x - InputFix.X;
if (x2 < 0) {
return 0;
}
return x2;
}
float optY(float y) {
float y2 = y - InputFix.Y;
if (y2 < 0) {
return 0;
}
return y2;
}
void process(irr::SEvent &event);
private:
core::position2di InputFix;
};
extern Game *mainGame;
......
......@@ -41,7 +41,6 @@ int GetListBoxIndex(IGUIListBox* listbox, const wchar_t * target){
}
void android_main(ANDROID_APP app) {
app->inputPollSource.process = android::process_input;
app_dummy();
#else
int main(int argc, char* argv[]) {
#endif
......
......@@ -749,9 +749,9 @@ void toggleGlobalIME(ANDROID_APP app, bool pShow) {
app->activity->vm->DetachCurrentThread();
}
core::position2di initJavaBridge(ANDROID_APP app, void* handle) {
void initJavaBridge(ANDROID_APP app, void* handle) {
if (!app || !app->activity || !app->activity->vm)
return core::position2di(0, 0);
return;
JNIEnv* jni = nullptr;
app->activity->vm->AttachCurrentThread(&jni, NULL);
jobject lNativeActivity = app->activity->clazz;
......@@ -760,18 +760,9 @@ core::position2di initJavaBridge(ANDROID_APP app, void* handle) {
"setNativeHandle", "(J)V");
jlong code = (jlong) handle;
jni->CallVoidMethod(lNativeActivity, MethodSetHandle, code);
jmethodID methodX = jni->GetMethodID(ClassNativeActivity,
"getPositionX", "()I");
jint posX = jni->CallIntMethod(lNativeActivity, methodX);
jmethodID methodY = jni->GetMethodID(ClassNativeActivity,
"getPositionY", "()I");
jint posY = jni->CallIntMethod(lNativeActivity, methodY);
jni->DeleteLocalRef(ClassNativeActivity);
app->activity->vm->DetachCurrentThread();
__android_log_print(ANDROID_LOG_INFO, "ygo", "Android command initJavaBridge posX=%d, posY=%d", posX, posY);
return core::position2di((int)posX, (int)posY);
__android_log_print(ANDROID_LOG_INFO, "ygo", "Android command initJavaBridge");
}
InitOptions* getInitOptions(ANDROID_APP app) {
......@@ -868,13 +859,8 @@ void toggleOverlayView(ANDROID_APP app, bool pShow) {
jni->DeleteLocalRef(ClassNativeActivity);
app->activity->vm->DetachCurrentThread();
}
static bool test = false;
void process_input(ANDROID_APP app,
struct android_poll_source* source) {
if(test == false) {
test = true;
ALOGD("inputQueue:%lld", (jlong) app->inputQueue);
}
AInputEvent* event = NULL;
if (AInputQueue_getEvent(app->inputQueue, &event) >= 0) {
int type = AInputEvent_getType(event);
......@@ -912,10 +898,6 @@ void onGameExit(ANDROID_APP app){
}
s32 handleInput(ANDROID_APP app, AInputEvent* androidEvent) {
if(test == false) {
test = true;
ALOGD("inputQueue:%lld", (jlong) app->inputQueue);
}
IrrlichtDevice* device = (IrrlichtDevice*) app->userData;
s32 Status = 0;
......
......@@ -126,7 +126,7 @@ extern void toggleGlobalIME(ANDROID_APP app, bool pShow);
extern void toggleIME(ANDROID_APP app, bool pShow, const char* hint);
//Init Java Irrlicht world.
extern core::position2di initJavaBridge(ANDROID_APP app, void* handle);
extern void initJavaBridge(ANDROID_APP app, void* handle);
//Cause a haptic feedback.
extern void perfromHapticFeedback(ANDROID_APP app);
......
......@@ -294,14 +294,6 @@ static void* join_game_thread(void* param) {
}
}
JNIEXPORT void JNICALL Java_cn_garymb_ygomobile_core_IrrlichtBridge_nativeSetInputFix(
JNIEnv* env, jclass clazz, jlong handle, jint x, jint y) {
if(ygo::mainGame) {
ALOGD("setInputFix posX=%d, posY=%d", x, y);
ygo::mainGame->setPositionFix(core::position2di(x, y));
}
}
static void* cancel_chain_thread(void* param) {
IrrlichtDevice* device = (IrrlichtDevice*) param;
irr::os::Printer::log("before send cancel chain");
......
......@@ -19,6 +19,7 @@ import android.os.Handler;
import android.os.PowerManager;
import android.os.Process;
import android.util.Log;
import android.util.Size;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.InputQueue;
......@@ -38,6 +39,7 @@ import java.util.Arrays;
import cn.garymb.ygodata.YGOGameOptions;
import cn.garymb.ygomobile.controller.InputQueueCompat;
import cn.garymb.ygomobile.controller.NetworkController;
import cn.garymb.ygomobile.core.GameActivity;
import cn.garymb.ygomobile.core.IrrlichtBridge;
import cn.garymb.ygomobile.lib.R;
import cn.garymb.ygomobile.utils.FullScreenUtils;
......@@ -52,7 +54,7 @@ import static cn.garymb.ygomobile.core.IrrlichtBridge.ACTION_SHARE_FILE;
/**
* @author mabin
*/
public class YGOMobileActivity extends NativeActivity implements
public class YGOMobileActivity extends GameActivity implements
IrrlichtBridge.IrrlichtHost,
View.OnClickListener,
PopupWindow.OnDismissListener,
......@@ -78,23 +80,14 @@ public class YGOMobileActivity extends NativeActivity implements
private NetworkController mNetController;
private volatile boolean mOverlayShowRequest = false;
private volatile int mCompatGUIMode;
private static int sChainControlXPostion = -1;
private static int sChainControlYPostion = -1;
// private static int sChainControlXPostion = -1;
// private static int sChainControlYPostion = -1;
private GameApplication mApp;
private Handler handler = new Handler();
private FullScreenUtils mFullScreenUtils;
private volatile int mPositionX, mPositionY;
private FrameLayout mLayout;
private SurfaceView mSurfaceView;
private boolean replaced = false;
private static boolean USE_SURFACE = true;
private String[] mArgV;
private boolean onGameExiting;
//尝试调准触摸事件
private static final boolean USE_MY_INPUT = true;
private InputQueueCompat inputQueueCompat;
// public static int notchHeight;
private GameApplication app() {
if (mApp == null) {
......@@ -111,29 +104,22 @@ public class YGOMobileActivity extends NativeActivity implements
return mApp;
}
@SuppressWarnings("WrongConstant")
@Override
protected void onCreate(Bundle savedInstanceState) {
if (USE_SURFACE) {
mSurfaceView = new SurfaceView(this);
}
if (USE_MY_INPUT) {
inputQueueCompat = new InputQueueCompat();
if (!inputQueueCompat.isValid()) {
inputQueueCompat = null;
}
}
protected void initBeforeOnCreate() {
mFullScreenUtils = new FullScreenUtils(this, app().isImmerSiveMode());
mFullScreenUtils.fullscreen();
mFullScreenUtils.onCreate();
//argv
mArgV = IrrlichtBridge.getArgs(getIntent());
//
super.onCreate(savedInstanceState);
}
@Override
protected void initAfterOnCreate() {
Log.e("YGOStarter", "跳转完成" + System.currentTimeMillis());
if (sChainControlXPostion < 0) {
initPostion();
}
// if (sChainControlXPostion < 0) {
// initPostion();
// }
if (app().isLockSreenOrientation()) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
......@@ -180,14 +166,14 @@ public class YGOMobileActivity extends NativeActivity implements
}
private void initPostion() {
final Resources res = getResources();
sChainControlXPostion = (int) (CHAIN_CONTROL_PANEL_X_POSITION_LEFT_EDGE * app()
.getXScale());
sChainControlYPostion = (int) (app().getSmallerSize()
- CHAIN_CONTROL_PANEL_Y_REVERT_POSITION
* app().getYScale() - (res
.getDimensionPixelSize(R.dimen.chain_control_button_height) * 2 + res
.getDimensionPixelSize(R.dimen.chain_control_margin)));
// final Resources res = getResources();
// sChainControlXPostion = (int) (CHAIN_CONTROL_PANEL_X_POSITION_LEFT_EDGE * app()
// .getXScale());
// sChainControlYPostion = (int) (app().getSmallerSize()
// - CHAIN_CONTROL_PANEL_Y_REVERT_POSITION
// * app().getYScale() - (res
// .getDimensionPixelSize(R.dimen.chain_control_button_height) * 2 + res
// .getDimensionPixelSize(R.dimen.chain_control_margin)));
}
@Override
......@@ -196,11 +182,6 @@ public class YGOMobileActivity extends NativeActivity implements
handleExternalCommand(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private void handleExternalCommand(Intent intent) {
YGOGameOptions options = intent
.getParcelableExtra(YGOGameOptions.YGO_GAME_OPTIONS_BUNDLE_KEY);
......@@ -222,18 +203,15 @@ public class YGOMobileActivity extends NativeActivity implements
}
private void fullscreen() {
//如果是沉浸模式
if (app().isImmerSiveMode()) {
mFullScreenUtils.fullscreen();
app().attachGame(this);
if (USE_SURFACE) {
changeGameSize();
}
}
}
private int[] getGameSize() {
@Override
protected Size getGameWindowSize() {
//调整padding
float xScale = app().getXScale();
float yScale = app().getYScale();
......@@ -245,88 +223,13 @@ public class YGOMobileActivity extends NativeActivity implements
+ ", surface=" + w + "x" + h
+ ", screen=" + sw + "x" + sh
+ ", xScale=" + xScale + ",yScale=" + yScale);
return new int[]{w, h};
return new Size(w, h);
}
@Override
public int getPositionX() {
synchronized (this) {
return mPositionX;
}
}
@Override
public int getPositionY() {
synchronized (this) {
return mPositionY;
}
}
@SuppressLint("ClickableViewAccessibility")
@Override
public void setContentView(View view) {
int[] size = getGameSize();
int w = size[0];
int h = size[1];
mLayout = new FrameLayout(this);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(w, h);
mLayout.setBackgroundColor(Color.BLACK);
lp.gravity = Gravity.CENTER;
if (USE_SURFACE) {
mLayout.addView(mSurfaceView, lp);
mLayout.addView(view, lp);
super.setContentView(mLayout);
app().attachGame(this);
changeGameSize();
getWindow().takeSurface(null);
if (USE_MY_INPUT && inputQueueCompat != null) {
getWindow().takeInputQueue(null);
}
replaced = true;
mSurfaceView.getHolder().addCallback(this);
mSurfaceView.requestFocus();
getWindow().setGravity(Gravity.CENTER);
if (USE_MY_INPUT && inputQueueCompat != null) {
Log.e(IrrlichtBridge.TAG, "use java input queue:" + inputQueueCompat.getNativePtr());
mSurfaceView.setOnTouchListener((v, event) -> {
if (inputQueueCompat != null) {
inputQueueCompat.sendInputEvent(event, v, true);
}
return true;
});
}
} else {
mLayout.addView(view, lp);
getWindow().setGravity(Gravity.CENTER);
super.setContentView(mLayout);
}
}
private void changeGameSize() {
boolean update = false;
if(USE_MY_INPUT && inputQueueCompat != null) {
//Ignore
update = true;
mPositionX = 0;
mPositionY = 0;
} else {
//游戏大小
int[] size = getGameSize();
int w = (int) app().getScreenHeight();
int h = (int) app().getScreenWidth();
int spX = (int) ((w - size[0]) / 2.0f);
int spY = (int) ((h - size[1]) / 2.0f);
synchronized (this) {
if (spX != mPositionX || spY != mPositionY) {
mPositionX = spX;
mPositionY = spY;
update = true;
}
}
}
if (update) {
IrrlichtBridge.setInputFix(mPositionX, mPositionY);
}
super.setContentView(view);
app().attachGame(this);
}
private void initExtraView() {
......@@ -348,15 +251,9 @@ public class YGOMobileActivity extends NativeActivity implements
// Log.e("YGOMobileActivity","窗口变化"+hasFocus);
if (hasFocus) {
fullscreen();
if (inputQueueCompat != null) {
super.onInputQueueCreated(inputQueueCompat.getInputQueue());
}
mContentView.setHapticFeedbackEnabled(true);
} else {
mContentView.setHapticFeedbackEnabled(false);
if (inputQueueCompat != null) {
super.onInputQueueDestroyed(inputQueueCompat.getInputQueue());
}
}
super.onWindowFocusChanged(hasFocus);
}
......@@ -426,7 +323,7 @@ public class YGOMobileActivity extends NativeActivity implements
@Override
public void toggleOverlayView(final boolean isShow) {
if (mOverlayShowRequest != isShow) {
handler.post(new Runnable() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mOverlayShowRequest = isShow;
......@@ -453,7 +350,7 @@ public class YGOMobileActivity extends NativeActivity implements
@Override
public void toggleIME(final String hint, final boolean isShow) {
handler.post(new Runnable() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (isShow) {
......@@ -473,7 +370,7 @@ public class YGOMobileActivity extends NativeActivity implements
@Override
public void showComboBoxCompat(final String[] items, final boolean isShow, final int mode) {
handler.post(new Runnable() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mCompatGUIMode = mode;
......@@ -490,7 +387,7 @@ public class YGOMobileActivity extends NativeActivity implements
@Override
public void performHapticFeedback() {
handler.post(new Runnable() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mContentView.performHapticFeedback(
......@@ -515,48 +412,6 @@ public class YGOMobileActivity extends NativeActivity implements
IrrlichtBridge.sNativeHandle = nativeHandle;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (USE_SURFACE) {
if (!replaced) {
return;
}
// int[] size = getGameSize();
// holder.setFixedSize(size[0], size[1]);
}
super.surfaceCreated(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (USE_SURFACE) {
if (!replaced) {
return;
}
}
super.surfaceChanged(holder, format, width, height);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (USE_SURFACE) {
if (!replaced) {
return;
}
}
super.surfaceDestroyed(holder);
}
@Override
public void surfaceRedrawNeeded(SurfaceHolder holder) {
if (USE_SURFACE) {
if (!replaced) {
return;
}
}
super.surfaceRedrawNeeded(holder);
}
@Override
public void shareFile(final String type, final String name) {
//TODO 分享文件
......@@ -577,29 +432,6 @@ public class YGOMobileActivity extends NativeActivity implements
}
});
}
//
// @Override
// public boolean onKeyDown(int keyCode, KeyEvent event) {
// if(inputQueueCompat != null) {
// if (keyCode == KeyEvent.KEYCODE_BACK) {
// inputQueueCompat.sendInputEvent(event, this, true);
// return true;
// }
// }
// return super.onKeyDown(keyCode, event);
// }
//
// @Override
// public boolean onKeyUp(int keyCode, KeyEvent event) {
// if(inputQueueCompat != null) {
// if (keyCode == KeyEvent.KEYCODE_BACK) {
// inputQueueCompat.sendInputEvent(event, this, true);
// return true;
// }
// }
// return super.onKeyUp(keyCode, event);
// }
@Override
public void onBackPressed() {
......
package cn.garymb.ygomobile.core;
import android.annotation.SuppressLint;
import android.app.NativeActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Size;
import android.view.Gravity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import cn.garymb.ygomobile.controller.InputQueueCompat;
public abstract class GameActivity extends NativeActivity {
protected FrameLayout mLayout;
protected SurfaceView mSurfaceView;
private boolean replaced = false;
//自定义surface,方便控制窗口大小
private static final boolean USE_SURFACE = true;
//精准触摸事件
private static final boolean USE_MY_INPUT = true;
protected InputQueueCompat inputQueueCompat;
@Override
protected final void onCreate(Bundle savedInstanceState) {
if (USE_SURFACE) {
mSurfaceView = new SurfaceView(this);
}
if (USE_MY_INPUT) {
inputQueueCompat = new InputQueueCompat();
if (!inputQueueCompat.isValid()) {
inputQueueCompat = null;
}
}
initBeforeOnCreate();
super.onCreate(savedInstanceState);
initAfterOnCreate();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus) {
if (inputQueueCompat != null) {
super.onInputQueueCreated(inputQueueCompat.getInputQueue());
}
} else {
if (inputQueueCompat != null) {
super.onInputQueueDestroyed(inputQueueCompat.getInputQueue());
}
}
super.onWindowFocusChanged(hasFocus);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public void setContentView(View view) {
Size size = getGameWindowSize();
mLayout = new FrameLayout(this);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(size.getWidth(), size.getHeight());
// mLayout.setBackgroundColor(Color.BLACK);
lp.gravity = Gravity.CENTER;
if (USE_SURFACE) {
mLayout.addView(mSurfaceView, lp);
mLayout.addView(view, lp);
super.setContentView(mLayout);
// app().attachGame(this);
// changeGameSize();
getWindow().takeSurface(null);
if (USE_MY_INPUT && inputQueueCompat != null) {
getWindow().takeInputQueue(null);
}
replaced = true;
mSurfaceView.getHolder().addCallback(this);
mSurfaceView.requestFocus();
getWindow().setGravity(Gravity.CENTER);
if (USE_MY_INPUT && inputQueueCompat != null) {
Log.d(IrrlichtBridge.TAG, "use java input queue:" + inputQueueCompat.getNativePtr());
mSurfaceView.setOnTouchListener((v, event) -> {
if (inputQueueCompat != null) {
inputQueueCompat.sendInputEvent(event, v, true);
}
return true;
});
}
} else {
mLayout.addView(view, lp);
getWindow().setGravity(Gravity.CENTER);
super.setContentView(mLayout);
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (USE_SURFACE) {
if (!replaced) {
return;
}
}
super.surfaceCreated(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (USE_SURFACE) {
if (!replaced) {
return;
}
}
super.surfaceChanged(holder, format, width, height);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (USE_SURFACE) {
if (!replaced) {
return;
}
}
super.surfaceDestroyed(holder);
}
@Override
public void surfaceRedrawNeeded(SurfaceHolder holder) {
if (USE_SURFACE) {
if (!replaced) {
return;
}
}
super.surfaceRedrawNeeded(holder);
}
//
// @Override
// public boolean onKeyDown(int keyCode, KeyEvent event) {
// if(inputQueueCompat != null) {
// if (keyCode == KeyEvent.KEYCODE_BACK) {
// inputQueueCompat.sendInputEvent(event, this, true);
// return true;
// }
// }
// return super.onKeyDown(keyCode, event);
// }
//
// @Override
// public boolean onKeyUp(int keyCode, KeyEvent event) {
// if(inputQueueCompat != null) {
// if (keyCode == KeyEvent.KEYCODE_BACK) {
// inputQueueCompat.sendInputEvent(event, this, true);
// return true;
// }
// }
// return super.onKeyUp(keyCode, event);
// }
protected abstract Size getGameWindowSize();
protected abstract void initBeforeOnCreate();
protected abstract void initAfterOnCreate();
}
......@@ -82,8 +82,6 @@ public final class IrrlichtBridge {
private static native void nativeJoinGame(long handle, ByteBuffer buffer, int length);
private static native void nativeSetInputFix(long handle, int x, int y);
private static final boolean DEBUG = false;
public static void setArgs(Intent intent, String[] args) {
......@@ -155,10 +153,6 @@ public final class IrrlichtBridge {
}
}
public static void setInputFix(int x, int y) {
nativeSetInputFix(sNativeHandle, x, y);
}
public static void cancelChain() {
nativeCancelChain(sNativeHandle);
}
......@@ -244,10 +238,6 @@ public final class IrrlichtBridge {
void setNativeHandle(long nativeHandle);
int getPositionX();
int getPositionY();
void onGameExit();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment