From f19dc4e0a9a84e5d3b3d303ffe02d4965f654091 Mon Sep 17 00:00:00 2001 From: dgis Date: Fri, 30 Nov 2018 21:01:14 +0000 Subject: [PATCH] --- app/src/main/cpp/emu48-jni.c | 19 +- .../com/regis/cosnier/emu48/MainActivity.java | 16 +- .../regis/cosnier/emu48/MainScreenView.java | 259 ++++++++++++++++++ 3 files changed, 291 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/regis/cosnier/emu48/MainScreenView.java diff --git a/app/src/main/cpp/emu48-jni.c b/app/src/main/cpp/emu48-jni.c index 5423d8b..c769cc2 100644 --- a/app/src/main/cpp/emu48-jni.c +++ b/app/src/main/cpp/emu48-jni.c @@ -5,6 +5,9 @@ #include #include #include +#include + +#include "pch.h" JNIEXPORT jstring JNICALL Java_com_regis_cosnier_emu48_MainActivity_stringFromJNI( @@ -18,7 +21,21 @@ Java_com_regis_cosnier_emu48_MainActivity_stringFromJNI( extern void emu48Start(); extern AAssetManager * assetManager; -JNIEXPORT void JNICALL Java_com_regis_cosnier_emu48_MainActivity_emu48Start(JNIEnv *env, jobject thisz, jobject assetMgr) { +JNIEXPORT void JNICALL Java_com_regis_cosnier_emu48_MainActivity_emu48Start(JNIEnv *env, jobject thisz, jobject assetMgr, jobject bitmapMainScreen) { + + AndroidBitmapInfo androidBitmapInfo; + void * pixels; + int ret; + if ((ret = AndroidBitmap_getInfo(env, bitmapMainScreen, &androidBitmapInfo)) < 0) { + LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret); + return; + } +// if ((ret = AndroidBitmap_lockPixels(env, bitmapMainScreen, &pixels)) < 0) { +// LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret); +// } +// +// AndroidBitmap_unlockPixels(env, bitmapMainScreen); + assetManager = AAssetManager_fromJava(env, assetMgr); emu48Start(); } diff --git a/app/src/main/java/com/regis/cosnier/emu48/MainActivity.java b/app/src/main/java/com/regis/cosnier/emu48/MainActivity.java index b5fbd3c..470ebe5 100644 --- a/app/src/main/java/com/regis/cosnier/emu48/MainActivity.java +++ b/app/src/main/java/com/regis/cosnier/emu48/MainActivity.java @@ -1,7 +1,10 @@ package com.regis.cosnier.emu48; import android.content.res.AssetManager; +import android.graphics.Bitmap; +import android.graphics.Canvas; import android.os.Bundle; +import android.util.DisplayMetrics; import android.widget.TextView; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; @@ -13,6 +16,9 @@ import android.view.MenuItem; public class MainActivity extends AppCompatActivity { + private Bitmap bitmapMainScreen; + MainScreenView mainScreenView; + // Used to load the 'native-lib' library on application startup. static { System.loadLibrary("native-lib"); @@ -39,7 +45,13 @@ public class MainActivity extends AppCompatActivity { tv.setText(stringFromJNI()); AssetManager mgr = getResources().getAssets(); - emu48Start(mgr); + + DisplayMetrics displayMetrics = new DisplayMetrics(); + getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); + bitmapMainScreen = Bitmap.createBitmap(displayMetrics.widthPixels, displayMetrics.heightPixels, Bitmap.Config.ARGB_8888); + emu48Start(mgr, bitmapMainScreen); + + mainScreenView = new MainScreenView(this); //, currentProject); } @Override @@ -70,5 +82,5 @@ public class MainActivity extends AppCompatActivity { */ public native String stringFromJNI(); - public native void emu48Start(AssetManager mgr); + public native void emu48Start(AssetManager mgr, Bitmap bitmapMainScreen); } diff --git a/app/src/main/java/com/regis/cosnier/emu48/MainScreenView.java b/app/src/main/java/com/regis/cosnier/emu48/MainScreenView.java new file mode 100644 index 0000000..1f91904 --- /dev/null +++ b/app/src/main/java/com/regis/cosnier/emu48/MainScreenView.java @@ -0,0 +1,259 @@ +package com.regis.cosnier.emu48; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.support.v4.view.ViewCompat; +import android.view.GestureDetector; +import android.view.InputDevice; +import android.view.KeyEvent; +import android.view.MotionEvent; +import android.view.ScaleGestureDetector; +import android.view.SurfaceView; +import android.view.View; +import android.widget.OverScroller; + +public class MainScreenView extends SurfaceView { + protected static final String TAG = "MainScreenView"; + + public MainScreenView(Context context) { + super(context); + + //commonInitialize(context, new Project()); + } + +// public MainScreenView(Context context, final Project currentProject) { +// super(context); +// +// commonInitialize(context, currentProject); +// } + +// /** +// * Common initialize method. +// * @param context The activity context. +// * @param currentProject The current project. +// */ +// private void commonInitialize(Context context, final Project currentProject) { +// //this.mContext = context; +// this.currentProject = currentProject; +// +// // Prevent the out of memory in OpenGL with Tegra 2 devices +// //if(!Utils.hasHighMemory(context)) +// setLayerType(View.LAYER_TYPE_SOFTWARE, null); +// //mAntiAliased = Utils.hasHighMemory(context); +// mScreenDensity = getResources().getDisplayMetrics().density; +// +// mVectorsCanvasRenderer = new VectorsCanvasRenderer(); +// mVectorsCanvasRenderer.setStrokeColor(0xff000000); // Black +// Paint paint = mVectorsCanvasRenderer.getPaint(); +// paint.setColor(Color.BLACK); +// paint.setStyle(Paint.Style.STROKE); +// paint.setStrokeWidth(1.0f * mScreenDensity); +// paint.setAntiAlias(mAntiAliased); +// +// mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { +// +// @Override +// public boolean onDoubleTap(MotionEvent e) { +// float scaleFactorPrevious = currentProject.viewScaleFactor; +// currentProject.viewScaleFactor *= 2f; +// if(currentProject.viewScaleFactor > mScaleFactorMax) +// currentProject.viewScaleFactor = mScaleFactorMin; +// else { +// constrainScale(); +// scaleWithFocus(e.getX(), e.getY(), scaleFactorPrevious); +// } +// constrainPan(); +// invalidate(); +// return true; +// } +// +// @Override +// public boolean onDown(MotionEvent e) { +// //Log.d(TAG, "GestureDetector.onDown()"); +// if(!mOverScroller.isFinished()) { +// mOverScroller.forceFinished(true); +// //ViewCompat.postInvalidateOnAnimation(CanvasView.this); +// //invalidate(); +// } +// return true; +// } +// +// }); +// +// this.setFocusable(true); +// this.setFocusableInTouchMode(true); +// +// // This call is necessary, or else the +// // draw method will not be called. +// setWillNotDraw(false); +// } + +// +// @SuppressLint("ClickableViewAccessibility") +// public boolean onTouchEvent(MotionEvent event) { +// // Let the ScaleGestureDetector inspect all events. +// boolean retValScaleDetector = mScaleDetector.onTouchEvent(event); +// boolean retValGestureDetector = mGestureDetector.onTouchEvent(event); +// boolean retVal = retValGestureDetector || retValScaleDetector; +// +// int touchCount = event.getPointerCount(); +// if(touchCount == 1) { +// int action = event.getAction(); +// switch (action) { +//// case MotionEvent.ACTION_DOWN: +//// +//// mPanPrevious.x = event.getX(0); +//// mPanPrevious.y = event.getY(0); +//// mIsPanning = true; +//// +//// //Log.d(TAG, "ACTION_DOWN count: " + touchCount + ", PanPrevious: " + mPanPrevious.toString()); +//// +//// break; +//// case MotionEvent.ACTION_MOVE: +//// float panCurrentX = event.getX(0); +//// float panCurrentY = event.getY(0); +//// if(mIsPanning && Math.abs(panCurrentX - mPanPrevious.x) > 1.0f && Math.abs(panCurrentY - mPanPrevious.y) > 1.0f) { +//// mPanCurrent.x = panCurrentX; +//// mPanCurrent.y = panCurrentY; +//// currentProject.viewPanOffsetX += mPanCurrent.x - mPanPrevious.x; +//// currentProject.viewPanOffsetY += mPanCurrent.y - mPanPrevious.y; +//// +//// //Log.d(TAG, "Before viewPanOffsetX: " + currentProject.viewPanOffsetX + ", viewPanOffsetY: " + currentProject.viewPanOffsetY); +//// +//// constrainScale(); +//// constrainPan(); +//// +//// //Log.d(TAG, "After viewPanOffsetX: " + currentProject.viewPanOffsetX + ", viewPanOffsetY: " + currentProject.viewPanOffsetY); +//// +//// //Log.d(TAG, "ACTION_MOVE count: " + touchCount + ", PanPrevious: " + mPanPrevious.toString() + ", PanCurrent: " + mPanCurrent.toString()); +//// +//// mPanPrevious.x = mPanCurrent.x; +//// mPanPrevious.y = mPanCurrent.y; +//// +//// invalidate(); +//// } +//// break; +// case MotionEvent.ACTION_UP: +// if(mIsPanning) { +// //Log.d(TAG, "Stop PANNING"); +// mIsPanning = false; +// if(!mIsFlinging) +// invalidate(); +// } +// break; +//// case MotionEvent.ACTION_CANCEL: +//// break; +//// case MotionEvent.ACTION_OUTSIDE: +//// break; +// default: +// } +// return true; +// } +// return retVal || super.onTouchEvent(event); +// } +// +// @Override +// public boolean onGenericMotionEvent(MotionEvent event) { +// if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { +// switch (event.getAction()) { +// case MotionEvent.ACTION_SCROLL: +// float wheelDelta = event.getAxisValue(MotionEvent.AXIS_VSCROLL); +// if(wheelDelta > 0f) +// scaleByStep(mScaleStep, event.getX(), event.getY()); +// else if(wheelDelta < 0f) +// scaleByStep(1.0f / mScaleStep, event.getX(), event.getY()); +// +// return true; +// } +// } +// return super.onGenericMotionEvent(event); +// } +// +// @Override +// public boolean onKeyUp(int keyCode, KeyEvent event) { +// char character = (char)event.getUnicodeChar(); +// if(character == '+') { +// scaleByStep(mScaleStep, getWidth() / 2.0f, getHeight() / 2.0f); +// return true; +// } else if(character == '-') { +// scaleByStep(1.0f / mScaleStep, getWidth() / 2.0f, getHeight() / 2.0f); +// return true; +// } +// return super.onKeyUp(keyCode, event); +// } + + @Override + protected void onSizeChanged(int viewWidth, int viewHeight, int oldViewWidth, int oldViewHeight) { + super.onSizeChanged(viewWidth, viewHeight, oldViewWidth, oldViewHeight); + +// mViewBounds.set(0.0f, 0.0f, viewWidth, viewHeight); +// resetViewport((float)viewWidth, (float)viewHeight); + } + + @Override + protected void onDraw(Canvas canvas) { + //Log.d(TAG, "onDraw() mIsScaling: " + mIsScaling + ", mIsPanning: " + mIsPanning + ", mIsFlinging: " + mIsFlinging); + +// //renderPlasma(mBitmap, System.currentTimeMillis() - mStartTime); +// canvas.drawBitmap(bitmapMainScreen, 0, 0, null); + +// Paint paint = mVectorsCanvasRenderer.getPaint(); +// +// canvas.drawColor(Color.rgb(224, 224, 224)); +// +// canvas.save(); +// canvas.translate(currentProject.viewPanOffsetX, currentProject.viewPanOffsetY); +// canvas.scale(currentProject.viewScaleFactor, currentProject.viewScaleFactor); +// paint.setColor(Color.BLACK); +// mVectorsCanvasRenderer.setCanvas(canvas); +// mVectorsCanvasRenderer.setShowLabels(currentProject.showLabels); +// if(mIsPanning || mIsScaling || mIsFlinging) +// { +// // We are scaling or panning, trying to optimize... +// if(mVectorsCanvasRenderer.getBackground() == null) +// // Display polygons only if there is no background map +// mVectorsCanvasRenderer.drawVectors(false, true, true, false); +// else +// // Best optimization +// mVectorsCanvasRenderer.drawBackgroundMap(); +// } +// else +// // Draw everything following the parameters +// mVectorsCanvasRenderer.drawVectors(); +// canvas.restore(); +// +// if(currentProject.viewScaleFactor > mScaleFactorMin) { +// // Draw the scale thumbnail +// paint.setColor(Color.RED); +// paint.setStrokeWidth(1.0f * mScreenDensity); +// +// float scale = 0.2f; +// if(mImageSize.x > mImageSize.y) +// scale *= mViewBounds.width() / mImageSize.x; +// else +// scale *= mViewBounds.height() / mImageSize.y; +// float marginX = 10f * mScreenDensity, marginY = 10f * mScreenDensity; +// mRectScaleImage.set(mViewBounds.right - marginX - scale * mImageSize.x, +// mViewBounds.bottom - marginY - scale * mImageSize.y, +// mViewBounds.right - marginX, +// mViewBounds.bottom - marginY +// ); +// canvas.drawRect(mRectScaleImage, paint); +// if(backgroundMapIndex >= 0 && mBitmap != null) { +// //Bitmap background = nativePNP.getBitmap(ParamImageSourceMat); +// mRectBitmapSource.set(0, 0, (int)mImageSize.x, (int)mImageSize.y); +// canvas.drawBitmap(mBitmap, mRectBitmapSource, mRectScaleImage, paint); +// } +// +// mRectScaleView.set(mRectScaleImage.left + scale * (-currentProject.viewPanOffsetX / currentProject.viewScaleFactor), +// mRectScaleImage.top + scale * (-currentProject.viewPanOffsetY / currentProject.viewScaleFactor), +// mRectScaleImage.left + scale * (mViewBounds.width() - currentProject.viewPanOffsetX) / currentProject.viewScaleFactor, +// mRectScaleImage.top + scale * (mViewBounds.height() - currentProject.viewPanOffsetY) / currentProject.viewScaleFactor +// ); +// canvas.drawRect(mRectScaleView, paint); + } + } +}