Refactor: Cache findViewById of game surface

This commit is contained in:
Mathias-Boulay 2023-03-01 23:59:57 +01:00
parent 7cc5a24f54
commit 9a403f2926
2 changed files with 14 additions and 4 deletions

View File

@ -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;
}
}

View File

@ -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;