From 9a403f2926d30d7ca57fece79dd9400b917e78fa Mon Sep 17 00:00:00 2001 From: Mathias-Boulay Date: Wed, 1 Mar 2023 23:59:57 +0100 Subject: [PATCH] Refactor: Cache findViewById of game surface --- .../kdt/pojavlaunch/customcontrols/ControlLayout.java | 10 ++++++++++ .../customcontrols/buttons/ControlButton.java | 8 ++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/ControlLayout.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/ControlLayout.java index fe5d82958..3c6c0b314 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/ControlLayout.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/ControlLayout.java @@ -26,6 +26,8 @@ import net.kdt.pojavlaunch.prefs.*; public class ControlLayout extends FrameLayout { protected CustomControls mLayout; + /* Accessible when inside the game by ControlInterface implementations, cached for perf. */ + private MinecraftGLSurface mGameSurface = null; private boolean mModifiable = false; private CustomControlsActivity mActivity; private boolean mControlVisible = false; @@ -386,4 +388,12 @@ public class ControlLayout extends FrameLayout { public void notifyAppMenu() { if(mMenuListener != null) mMenuListener.onClickedMenu(); } + + /** Cached getter for perf purposes */ + public MinecraftGLSurface getGameSurface(){ + if(mGameSurface == null){ + mGameSurface = findViewById(R.id.main_game_render_view); + } + return mGameSurface; + } } 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 8ecf7518b..f75921d3e 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 @@ -109,8 +109,8 @@ public class ControlButton extends TextView implements ControlInterface { case MotionEvent.ACTION_MOVE: //Send the event to be taken as a mouse action if(getProperties().passThruEnabled && CallbackBridge.isGrabbing()){ - MinecraftGLSurface v = getControlLayoutParent().findViewById(R.id.main_game_render_view); - if (v != null) v.dispatchTouchEvent(event); + View gameSurface = getControlLayoutParent().getGameSurface(); + if(gameSurface != null) gameSurface.dispatchTouchEvent(event); } //If out of bounds @@ -149,8 +149,8 @@ public class ControlButton extends TextView implements ControlInterface { case MotionEvent.ACTION_CANCEL: // 3 case MotionEvent.ACTION_POINTER_UP: // 6 if(getProperties().passThruEnabled){ - MinecraftGLSurface v = getControlLayoutParent().findViewById(R.id.main_game_render_view); - if (v != null) v.dispatchTouchEvent(event); + View gameSurface = getControlLayoutParent().getGameSurface(); + if(gameSurface != null) gameSurface.dispatchTouchEvent(event); } if(mIsPointerOutOfBounds) getControlLayoutParent().onTouch(this, event); mIsPointerOutOfBounds = false;