From bc4e6e7c163a23120fc282f5a492103498cada6d Mon Sep 17 00:00:00 2001 From: SerpentSpirale Date: Thu, 1 Jul 2021 14:50:37 +0200 Subject: [PATCH] Clean up code. --- .../net/kdt/pojavlaunch/BaseMainActivity.java | 12 +-- .../pojavlaunch/JavaGUILauncherActivity.java | 12 +-- .../customcontrols/buttons/ControlButton.java | 81 +++++++++---------- .../customcontrols/buttons/ControlDrawer.java | 24 +++--- .../customcontrols/gamepad/Gamepad.java | 4 +- 5 files changed, 64 insertions(+), 69 deletions(-) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/BaseMainActivity.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/BaseMainActivity.java index ac5f5b0a5..16e52bdb5 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/BaseMainActivity.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/BaseMainActivity.java @@ -250,8 +250,8 @@ public class BaseMainActivity extends LoggableActivity { float x = event.getX(); float y = event.getY(); - float mouseX = mousePointer.getTranslationX(); - float mouseY = mousePointer.getTranslationY(); + float mouseX = mousePointer.getX(); + float mouseY = mousePointer.getY(); if (gestureDetector.onTouchEvent(event)) { mouse_x = (mouseX * scaleFactor); @@ -797,13 +797,13 @@ public class BaseMainActivity extends LoggableActivity { } public void placeMouseAdd(float x, float y) { - this.mousePointer.setTranslationX(mousePointer.getTranslationX() + x); - this.mousePointer.setTranslationY(mousePointer.getTranslationY() + y); + this.mousePointer.setX(mousePointer.getX() + x); + this.mousePointer.setY(mousePointer.getY() + y); } public void placeMouseAt(float x, float y) { - this.mousePointer.setTranslationX(x); - this.mousePointer.setTranslationY(y); + this.mousePointer.setX(x); + this.mousePointer.setY(y); } public void toggleMouse(View view) { diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JavaGUILauncherActivity.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JavaGUILauncherActivity.java index 77308e4da..cc3966114 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JavaGUILauncherActivity.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JavaGUILauncherActivity.java @@ -108,8 +108,8 @@ public class JavaGUILauncherActivity extends LoggableActivity implements View.On prevX = x; prevY = y; } - float mouseX = mousePointer.getTranslationX(); - float mouseY = mousePointer.getTranslationY(); + float mouseX = mousePointer.getX(); + float mouseY = mousePointer.getY(); if (gestureDetector.onTouchEvent(event)) { @@ -250,13 +250,13 @@ public class JavaGUILauncherActivity extends LoggableActivity implements View.On } public void placeMouseAdd(float x, float y) { - this.mousePointer.setTranslationX(mousePointer.getTranslationX() + x); - this.mousePointer.setTranslationY(mousePointer.getTranslationY() + y); + this.mousePointer.setX(mousePointer.getX() + x); + this.mousePointer.setY(mousePointer.getY() + y); } public void placeMouseAt(float x, float y) { - this.mousePointer.setTranslationX(x); - this.mousePointer.setTranslationY(y); + this.mousePointer.setX(x); + this.mousePointer.setY(y); } void sendScaledMousePosition(float x, float y){ diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/buttons/ControlButton.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/buttons/ControlButton.java index b9ba453d0..0a6afdf36 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/buttons/ControlButton.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/buttons/ControlButton.java @@ -98,8 +98,8 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp setText(properties.name); if (changePos) { - setTranslationX(moveX = properties.x); - setTranslationY(moveY = properties.y); + setX(properties.x); + setY(properties.y); } if (properties.specialButtonListener == null) { @@ -172,8 +172,8 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp // Re-calculate position mProperties.update(); - setTranslationX(mProperties.x); - setTranslationY(mProperties.y); + setX(mProperties.x); + setY(mProperties.y); setModified(true); } @@ -184,8 +184,8 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp } @Override - public void setTranslationX(float x) { - super.setTranslationX(x); + public void setX(float x) { + super.setX(x); if (!mProperties.isDynamicBtn) { mProperties.x = x; @@ -195,8 +195,8 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp } @Override - public void setTranslationY(float y) { - super.setTranslationY(y); + public void setY(float y) { + super.setY(y); if (!mProperties.isDynamicBtn) { mProperties.y = y; @@ -238,27 +238,23 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp return mCanTriggerLongClick; } - protected float moveX, moveY; protected float downX, downY; @Override public boolean onTouchEvent(MotionEvent event) { - System.out.println("IS BEING TOUCHED"); - if (!mModifiable) { + if(!mModifiable){ mCanTriggerLongClick = false; - if (event.getAction() == MotionEvent.ACTION_MOVE && CallbackBridge.isGrabbing() && mProperties.passThruEnabled) { - MinecraftGLView v = ((ControlLayout) this.getParent()).findViewById(R.id.main_game_render_view); - if (v != null) { - v.dispatchTouchEvent(event); - return true; - } - } - - switch (event.getActionMasked()) { + switch (event.getActionMasked()){ case MotionEvent.ACTION_MOVE: + //Send the event to be taken as a mouse action + if(mProperties.passThruEnabled && CallbackBridge.isGrabbing()){ + MinecraftGLView v = ((ControlLayout) this.getParent()).findViewById(R.id.main_game_render_view); + if (v != null) v.dispatchTouchEvent(event); + } + //If out of bounds if(event.getX() < getLeft() || event.getX() > getRight() || - event.getY() < getTop() || event.getY() > getBottom()){ + event.getY() < getTop() || event.getY() > getBottom()){ if(mProperties.isSwipeable && !isPointerOutOfBounds){ //Remove keys if(!triggerToggle(event)) { @@ -290,6 +286,7 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp sendKeyPresses(event, true); } break; + case MotionEvent.ACTION_UP: // 1 case MotionEvent.ACTION_CANCEL: // 3 case MotionEvent.ACTION_POINTER_UP: // 6 @@ -306,32 +303,30 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp return false; } return true; + } - } else { - if (mGestureDetector.onTouchEvent(event)) { + //If the button can be modified/moved + if (mGestureDetector.onTouchEvent(event)) { + mCanTriggerLongClick = true; + onLongClick(this); + } + + switch (event.getActionMasked()) { + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_DOWN: mCanTriggerLongClick = true; - onLongClick(this); - } + downX = event.getRawX() - getX(); + downY = event.getRawY() - getY(); + break; - switch (event.getActionMasked()) { - case MotionEvent.ACTION_UP: - case MotionEvent.ACTION_DOWN: - mCanTriggerLongClick = true; - downX = event.getX(); - downY = event.getY(); - break; + case MotionEvent.ACTION_MOVE: + mCanTriggerLongClick = false; - case MotionEvent.ACTION_MOVE: - mCanTriggerLongClick = false; - moveX += event.getX() - downX; - moveY += event.getY() - downY; - - if (!mProperties.isDynamicBtn) { - setTranslationX(moveX); - setTranslationY(moveY); - } - break; - } + if (!mProperties.isDynamicBtn) { + setX(event.getRawX() - downX); + setY(event.getRawY() - downY); + } + break; } return super.onTouchEvent(event); diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/buttons/ControlDrawer.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/buttons/ControlDrawer.java index 187ca94bd..09dbba963 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/buttons/ControlDrawer.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/buttons/ControlDrawer.java @@ -78,23 +78,23 @@ public class ControlDrawer extends ControlButton { for(int i=0; i < buttons.size(); ++i){ switch (drawerData.orientation){ case RIGHT: - buttons.get(i).setTranslationX(drawerData.properties.x + (drawerData.properties.getWidth() + Tools.dpToPx(2))*(i+1) ); - buttons.get(i).setTranslationY(drawerData.properties.y); + buttons.get(i).setX(drawerData.properties.x + (drawerData.properties.getWidth() + Tools.dpToPx(2))*(i+1) ); + buttons.get(i).setY(drawerData.properties.y); break; case LEFT: - buttons.get(i).setTranslationX(drawerData.properties.x - (drawerData.properties.getWidth() + Tools.dpToPx(2))*(i+1) ); - buttons.get(i).setTranslationY(drawerData.properties.y); + buttons.get(i).setX(drawerData.properties.x - (drawerData.properties.getWidth() + Tools.dpToPx(2))*(i+1) ); + buttons.get(i).setY(drawerData.properties.y); break; case UP: - buttons.get(i).setTranslationY(drawerData.properties.y - (drawerData.properties.getHeight() + Tools.dpToPx(2))*(i+1) ); - buttons.get(i).setTranslationX(drawerData.properties.x); + buttons.get(i).setY(drawerData.properties.y - (drawerData.properties.getHeight() + Tools.dpToPx(2))*(i+1) ); + buttons.get(i).setX(drawerData.properties.x); break; case DOWN: - buttons.get(i).setTranslationY(drawerData.properties.y + (drawerData.properties.getHeight() + Tools.dpToPx(2))*(i+1) ); - buttons.get(i).setTranslationX(drawerData.properties.x); + buttons.get(i).setY(drawerData.properties.y + (drawerData.properties.getHeight() + Tools.dpToPx(2))*(i+1) ); + buttons.get(i).setX(drawerData.properties.x); break; } buttons.get(i).updateProperties(); @@ -146,14 +146,14 @@ public class ControlDrawer extends ControlButton { } @Override - public void setTranslationX(float x) { - super.setTranslationX(x); + public void setX(float x) { + super.setX(x); alignButtons(); } @Override - public void setTranslationY(float y) { - super.setTranslationY(y); + public void setY(float y) { + super.setY(y); alignButtons(); } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java index f32969ac2..d5468f668 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java @@ -298,8 +298,8 @@ public class Gamepad { } private void placePointerView(int x, int y){ - pointerView.setTranslationX(x-32); - pointerView.setTranslationY(y-32); + pointerView.setX(x-32); + pointerView.setY(y-32); }