Commit 8664e10a authored by kenan's avatar kenan

坐标校准

parent bd22e195
......@@ -1654,7 +1654,7 @@ bool ClientField::OnEvent(const irr::SEvent& event) {
case irr::EMIE_LMOUSE_PRESSED_DOWN: {
if(!mainGame->dInfo.isStarted)
break;
if(mainGame->gameConf.control_mode == 1 && event.MouseInput.X > 300) {
if(mainGame->gameConf.control_mode == 1 && event.MouseInput.X > 300 * mainGame->xScale) {
mainGame->always_chain = event.MouseInput.isLeftPressed();
mainGame->ignore_chain = false;
mainGame->chain_when_avail = false;
......@@ -1665,7 +1665,7 @@ bool ClientField::OnEvent(const irr::SEvent& event) {
case irr::EMIE_RMOUSE_PRESSED_DOWN: {
if(!mainGame->dInfo.isStarted)
break;
if(mainGame->gameConf.control_mode == 1 && event.MouseInput.X > 300) {
if(mainGame->gameConf.control_mode == 1 && event.MouseInput.X > 300 * mainGame->xScale) {
mainGame->ignore_chain = event.MouseInput.isRightPressed();
mainGame->always_chain = false;
mainGame->chain_when_avail = false;
......
......@@ -15,6 +15,7 @@
#include <android/CAndroidGUIEditBox.h>
#include <android/CAndroidGUIComboBox.h>
#include <android/CAndroidGUISkin.h>
#include <Android/CIrrDeviceAndroid.h>
#include <COGLES2ExtensionHandler.h>
#include <COGLESExtensionHandler.h>
#include <COGLES2Driver.h>
......@@ -25,7 +26,18 @@ const unsigned short PRO_VERSION = 0x134B;
namespace ygo {
Game* mainGame;
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);
// __android_log_print(ANDROID_LOG_DEBUG, "ygo", "Android comman process %d,%d -> %d,%d", x, y,
// event.MouseInput.X, event.MouseInput.Y);
}
}
#ifdef _IRR_ANDROID_PLATFORM_
bool Game::Initialize(ANDROID_APP app) {
......@@ -80,10 +92,13 @@ bool Game::Initialize() {
if(!device)
return false;
#ifdef _IRR_ANDROID_PLATFORM_
device->setProcessReceiver(this);
if (!android::perfromTrick(app)) {
return false;
}
android::initJavaBridge(app, device);
core::position2di appPosition = android::initJavaBridge(app, device);
setPositionFix(appPosition);
soundEffectPlayer = new AndroidSoundEffectPlayer(app);
soundEffectPlayer->setSEEnabled(options->isSoundEffectEnabled());
app->onInputEvent = android::handleInput;
......
......@@ -107,7 +107,7 @@ struct FadingUnit {
irr::core::vector2di fadingDiff;
};
class Game {
class Game :IProcessEventReceiver{
public:
#ifdef _IRR_ANDROID_PLATFORM_
......@@ -543,10 +543,31 @@ public:
irr::android::CustomShaderConstantSetCallBack customShadersCallback;
Signal externalSignal;
#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;
extern Game* mainGame;
}
......
......@@ -539,6 +539,15 @@ public:
virtual bool OnEvent(const SEvent& event) = 0;
};
class IProcessEventReceiver
{
public:
//! Destructor
virtual ~IProcessEventReceiver() {}
virtual void process(SEvent& event) = 0;
};
//! Information on a joystick, returned from @ref irr::IrrlichtDevice::activateJoysticks()
struct SJoystickInfo
......
......@@ -307,6 +307,8 @@ namespace irr
//! Get context manager
virtual video::IContextManager* getContextManager() =0;
virtual void setProcessReceiver(IProcessEventReceiver* receiver) = 0;
//! Set the maximal elapsed time between 2 clicks to generate doubleclicks for the mouse. It also affects tripleclick behavior.
/** When set to 0 no double- and tripleclicks will be generated.
\param timeMs maximal time in milliseconds for two consecutive clicks to be recognized as double click
......
......@@ -224,10 +224,15 @@ u32 CIrrDeviceStub::checkSuccessiveClicks(s32 mouseX, s32 mouseY, EMOUSE_INPUT_E
//! send the event to the right receiver
bool CIrrDeviceStub::postEventFromUser(const SEvent& event)
bool CIrrDeviceStub::postEventFromUser(const SEvent& _event)
{
SEvent event = _event;
bool absorbed = false;
if(ProcessReceiver){
ProcessReceiver->process(event);
}
if (UserReceiver)
absorbed = UserReceiver->OnEvent(event);
......
......@@ -178,6 +178,9 @@ namespace irr
//! Resize the render window.
virtual void setWindowSize(const irr::core::dimension2d<u32>& size) _IRR_OVERRIDE_ {}
virtual void setProcessReceiver(IProcessEventReceiver* receiver) _IRR_OVERRIDE_{
ProcessReceiver = receiver;
}
protected:
void createGUIAndScene();
......@@ -200,6 +203,7 @@ namespace irr
ITimer* Timer;
gui::ICursorControl* CursorControl;
IEventReceiver* UserReceiver;
IProcessEventReceiver* ProcessReceiver;
CLogger* Logger;
IOSOperator* Operator;
IRandomizer* Randomizer;
......
......@@ -703,20 +703,29 @@ void toggleGlobalIME(ANDROID_APP app, bool pShow) {
app->activity->vm->DetachCurrentThread();
}
void initJavaBridge(ANDROID_APP app, void* handle) {
core::position2di initJavaBridge(ANDROID_APP app, void* handle) {
if (!app || !app->activity || !app->activity->vm)
return;
return core::position2di(0, 0);
JNIEnv* jni = 0;
app->activity->vm->AttachCurrentThread(&jni, NULL);
jobject lNativeActivity = app->activity->clazz;
jclass ClassNativeActivity = jni->GetObjectClass(lNativeActivity);
jmethodID MethodSetHandle = jni->GetMethodID(ClassNativeActivity,
"setNativeHandle", "(I)V");
"setNativeHandle", "(I)V");
jint code = (int) 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();
return;
__android_log_print(ANDROID_LOG_INFO, "ygo", "Android command initJavaBridge posX=%d, posY=%d", posX, posY);
return core::position2di((int)posX, (int)posY);
}
InitOptions* getInitOptions(ANDROID_APP app) {
......
......@@ -84,6 +84,10 @@ struct SDisplayMetrics {
irr::f32 xdpi;
irr::f32 ydpi;
};
typedef struct{
float posX;
float posY;
} AppPosition;
/* jni utils*/
// Access SDisplayMetrics
extern float getScreenWidth(ANDROID_APP app);
......@@ -115,7 +119,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 void initJavaBridge(ANDROID_APP app, void* handle);
extern core::position2di initJavaBridge(ANDROID_APP app, void* handle);
//Cause a haptic feedback.
extern void perfromHapticFeedback(ANDROID_APP app);
......
......@@ -7,6 +7,7 @@
#include <Android/CIrrDeviceAndroid.h>
#include "../android/YGOGameOptions.h"
#include "../Classes/gframe/game.h"
#include <android/log.h>
using namespace irr;
using namespace gui;
......@@ -293,6 +294,14 @@ static void* join_game_thread(void* param) {
}
}
JNIEXPORT void JNICALL Java_cn_garymb_ygomobile_core_IrrlichtBridge_nativeSetInputFix(
JNIEnv* env, jclass clazz, jint handle, jint x, jint y) {
if(ygo::mainGame) {
__android_log_print(ANDROID_LOG_INFO, "ygo", "setInputFix posX=%d, posY=%d", x, y);
ygo::mainGame->setPositionFix(core::position2di(x, y));
}
}
//touch事件
JNIEXPORT void JNICALL Java_cn_garymb_ygomobile_core_IrrlichtBridge_nativeSendTouch(
JNIEnv* env, jclass clazz, jint handle, jint action, jint id, jfloat x, jfloat y) {
......
......@@ -14,14 +14,11 @@ import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.PowerManager;
import android.util.Log;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.InputQueue;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
......@@ -85,9 +82,6 @@ public class YGOMobileActivity extends NativeActivity implements
private volatile int mPositionX, mPositionY;
private boolean mPaused;
private SurfaceView mSurfaceView;
private HandlerThread mThread;
private Handler mWorker;
// public static int notchHeight;
......@@ -111,9 +105,8 @@ public class YGOMobileActivity extends NativeActivity implements
protected void onCreate(Bundle savedInstanceState) {
mSurfaceView = new SurfaceView(this);
mSurfaceView.getHolder().addCallback(this);
mThread = new HandlerThread("ygo_work_"+hashCode());
mThread.start();
mWorker = new Handler(mThread.getLooper());
app().attachGame(this);
getGameSize();
super.onCreate(savedInstanceState);
Log.e("YGOStarter","跳转完成"+System.currentTimeMillis());
mFullScreenUtils = new FullScreenUtils(this, app().isImmerSiveMode());
......@@ -239,12 +232,40 @@ public class YGOMobileActivity extends NativeActivity implements
yScale = sW;
}
int w = (int)(1024.0*xScale);
int h = (int)(640.0*yScale);
mPositionX = (int)((sH - w)/2.0f);
mPositionY = (int)((sW - h)/2.0f);
int h = (int) (640.0 * yScale);
int spX = (int) ((screenH - w) / 2.0f);
int spY = (int) ((screenW - h) / 2.0f);
Log.i("ygo", "Android command setInputFix1:posX=" + spX + ",posY=" + spY);
boolean update = false;
synchronized (this) {
if (spX != mPositionX || spY != mPositionY) {
mPositionX = spX;
mPositionY = spY;
update = true;
}
}
if (update) {
Log.i("ygo", "Android command setInputFix2:posX=" + spX + ",posY=" + spY);
IrrlichtBridge.setInputFix(mPositionX, mPositionY);
}
return new int[]{w, h};
}
@Override
public int getPositionX() {
synchronized (this) {
return mPositionX;
}
}
@Override
public int getPositionY() {
synchronized (this) {
return mPositionY;
}
}
private void fullscreen() {
//如果是沉浸模式
......@@ -252,7 +273,7 @@ public class YGOMobileActivity extends NativeActivity implements
mFullScreenUtils.fullscreen();
app().attachGame(this);
//游戏大小
// int[] size = getGameSize();
int[] size = getGameSize();
// getWindow().setLayout(size[0], size[1]);
}
}
......@@ -263,43 +284,10 @@ public class YGOMobileActivity extends NativeActivity implements
int[] size = getGameSize();
int w = size[0];
int h = size[1];
getWindow().takeInputQueue(null);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(w, h);
lp.gravity = Gravity.CENTER;
layout.addView(mSurfaceView, lp);
layout.addView(view, lp);
view.setLongClickable(true);
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v,final MotionEvent event) {
if(mPaused){
return false;
}
int eventType = event.getAction() & MotionEvent.ACTION_MASK;
switch (eventType) {
case MotionEvent.ACTION_DOWN:
// case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
// case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL:
break;
default:
return false;
}
final int action = event.getAction();
final float x = event.getX();
final float y = event.getY();
doWork(new Runnable() {
@Override
public void run() {
IrrlichtBridge.sendTouch(action, x, y, 0);
}
});
return true;
}
});
// getWindow().setLayout(w, h);
// getWindow().setGravity(Gravity.CENTER);
super.setContentView(layout);
......@@ -307,48 +295,6 @@ public class YGOMobileActivity extends NativeActivity implements
mSurfaceView.requestFocus();
}
@Override
public void onInputQueueCreated(InputQueue queue) {
// super.onInputQueueCreated(queue);
}
@Override
public void onInputQueueDestroyed(InputQueue queue) {
// super.onInputQueueDestroyed(queue);
}
@Override
public boolean onKeyDown(final int keyCode, KeyEvent event) {
if(keyCode != KeyEvent.KEYCODE_BACK){
doWork(new Runnable() {
@Override
public void run() {
IrrlichtBridge.sendKey(keyCode, true);
}
});
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(final int keyCode, KeyEvent event) {
if(keyCode != KeyEvent.KEYCODE_BACK){
doWork(new Runnable() {
@Override
public void run() {
IrrlichtBridge.sendKey(keyCode, false);
}
});
return true;
}
return super.onKeyUp(keyCode, event);
}
private void doWork(Runnable runnable){
mWorker.post(runnable);
}
@Override
public void onBackPressed() {
Toast.makeText(this, "请在游戏里面退出", Toast.LENGTH_SHORT).show();
......
......@@ -63,6 +63,8 @@ public final class IrrlichtBridge {
private static native void nativeSendTouch(int handle, int action, int id, float x, float y);
private static native void nativeSetInputFix(int handle, int x, int y);
private static final boolean DEBUG = false;
private static final String TAG = IrrlichtBridge.class.getSimpleName();
......@@ -133,6 +135,10 @@ public final class IrrlichtBridge {
}
}
public static void setInputFix(int x, int y){
nativeSetInputFix(sNativeHandle, x, y);
}
public static void cancelChain() {
nativeCancelChain(getHandle());
}
......@@ -193,7 +199,7 @@ public final class IrrlichtBridge {
void playSoundEffect(String path);
void runWindbot(String args);
// float getSmallerSize();
// float getXScale();
// float getYScale();
......@@ -221,5 +227,9 @@ public final class IrrlichtBridge {
int getLocalAddress();
void setNativeHandle(int nativeHandle);
int getPositionX();
int getPositionY();
}
}
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