SD rightclicking and camera changes + Low detail frame limiting to 60 IDK if this helps with battery life but probably.

This commit is contained in:
downthecrop 2023-08-01 10:46:56 -07:00
parent f28c46638d
commit a0df7b31a4
6 changed files with 47 additions and 35 deletions

View file

@ -54,6 +54,7 @@
android:label="@string/app_short_name" />
<activity
android:name=".DummyLauncher"
android:screenOrientation="sensorLandscape"
android:label="@string/app_short_name" />
<activity
android:name=".ImportControlActivity"

View file

@ -1 +1 @@
1690130848255
1690859739480

View file

@ -1 +1 @@
1690130848291
1690859739525

View file

@ -1 +1 @@
1690130848323
1690859739558

View file

@ -12,28 +12,17 @@ import net.kdt.pojavlaunch.utils.*;
public class AWTCanvasView extends TextureView implements TextureView.SurfaceTextureListener, Runnable {
public static final int AWT_CANVAS_WIDTH = 765;
public static final int AWT_CANVAS_HEIGHT = 503;
private static final int MAX_SIZE = 100;
private static final double NANOS = 1000000000.0;
private boolean mIsDestroyed = false;
private final TextPaint mFpsPaint;
// Temporary count fps https://stackoverflow.com/a/13729241
private final LinkedList<Long> mTimes = new LinkedList<Long>(){{add(System.nanoTime());}};
public AWTCanvasView(Context ctx) {
this(ctx, null);
}
public AWTCanvasView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
mFpsPaint = new TextPaint();
mFpsPaint.setColor(Color.WHITE);
mFpsPaint.setTextSize(20);
setSurfaceTextureListener(this);
post(this::refreshSize);
}
@ -66,20 +55,44 @@ public class AWTCanvasView extends TextureView implements TextureView.SurfaceTex
Surface surface = new Surface(getSurfaceTexture());
Bitmap rgbArrayBitmap = Bitmap.createBitmap(AWT_CANVAS_WIDTH, AWT_CANVAS_HEIGHT, Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
long frameEndNanos;
long frameStartNanos;
long sleepTime;
long sleepMillis;
int sleepNanos;
int[] rgbArray;
// define the frame rate limit
final long frameTimeNanos = (long)(NANOS / 60); // Targeting 60 FPS
long frameDuration;
try {
while (!mIsDestroyed && surface.isValid()) {
frameStartNanos = System.nanoTime();
canvas = surface.lockCanvas(null);
canvas.drawRGB(0, 0, 0);
int[] rgbArray = JREUtils.renderAWTScreenFrame(/* canvas, mWidth, mHeight */);
boolean mDrawing = rgbArray != null;
rgbArray = JREUtils.renderAWTScreenFrame();
if (rgbArray != null) {
canvas.save();
rgbArrayBitmap.setPixels(rgbArray, 0, AWT_CANVAS_WIDTH, 0, 0, AWT_CANVAS_WIDTH, AWT_CANVAS_HEIGHT);
canvas.drawBitmap(rgbArrayBitmap, 0, 0, paint);
canvas.restore();
}
canvas.drawText("FPS: " + (Math.round(fps() * 10) / 10) + ", drawing=" + mDrawing, 0, 20, mFpsPaint);
surface.unlockCanvasAndPost(canvas);
// frame rate limiting
frameEndNanos = System.nanoTime();
frameDuration = frameEndNanos - frameStartNanos;
if (frameDuration < frameTimeNanos) {
sleepTime = frameTimeNanos - frameDuration;
sleepMillis = sleepTime / 1000000;
sleepNanos = (int)(sleepTime - sleepMillis * 1000000);
try {
Thread.sleep(sleepMillis, sleepNanos);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
} catch (Throwable throwable) {
Tools.showError(getContext(), throwable);
@ -88,18 +101,6 @@ public class AWTCanvasView extends TextureView implements TextureView.SurfaceTex
surface.release();
}
/** Calculates and returns frames per second */
private double fps() {
long lastTime = System.nanoTime();
double difference = (lastTime - mTimes.getFirst()) / NANOS;
mTimes.addLast(lastTime);
int size = mTimes.size();
if (size > MAX_SIZE) {
mTimes.removeFirst();
}
return difference > 0 ? mTimes.size() / difference : 0.0;
}
/** Make the view fit the proper aspect ratio of the surface */
private void refreshSize(){
ViewGroup.LayoutParams layoutParams = getLayoutParams();

View file

@ -56,7 +56,8 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTouc
private long lastPress = 0;
private ScaleGestureDetector scaleGestureDetector;
private boolean rcState = false;
private boolean mSkipDetectMod, mIsVirtualMouseEnabled;
private boolean mSkipDetectMod;
private static boolean mIsVirtualMouseEnabled;
@SuppressLint("ClickableViewAccessibility")
@Override
@ -100,7 +101,6 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTouc
float prevX = 0, prevY = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
scaleGestureDetector.onTouchEvent(event);
// MotionEvent reports input details from the touch screen
// and other input controls. In this case, you are only
// interested in events where the touch position changed.
@ -124,6 +124,16 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTouc
placeMouseAt(mouseX, mouseY);
sendScaledMousePosition(mouseX, mouseY);
}
// Check if there are two fingers on the screen
if (action == MotionEvent.ACTION_POINTER_DOWN) {
if (event.getPointerCount() == 2) {
// Right-click event when a second finger touches the screen
// Simulating right-click by sending GLFW_MOUSE_BUTTON_RIGHT event
Log.i("downthecrop","Hi from a rightclick event!");
activateRC();
AWTInputBridge.sendMousePress(AWTInputEvent.BUTTON1_DOWN_MASK);
}
}
}
prevY = y;
@ -133,6 +143,7 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTouc
});
mTextureView.setOnTouchListener((v, event) -> {
scaleGestureDetector.onTouchEvent(event);
float x = event.getX();
float y = event.getY();
if (mGestureDetector.onTouchEvent(event)) {
@ -244,10 +255,9 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTouc
}
break;
case R.id.camera:
AWTInputBridge.sendKey((char) AWTInputEvent.VK_F9, AWTInputEvent.VK_F9);
if (!cameraMode) { // Camera Mode On
Log.i("downthecrop", "Hello from the camrea Button");
//AWTInputBridge.sendKey((char) AWTInputEvent.VK_F9, AWTInputEvent.VK_F9); // Send F9
AWTInputBridge.sendKey((char) AWTInputEvent.VK_F9, AWTInputEvent.VK_F9); // Send F9
cameraMode = true;
} else { // Camera Mode off
AWTInputBridge.sendKey((char) AWTInputEvent.VK_F8, AWTInputEvent.VK_F8);