From ab3f0bb705ce7d243f5732ae9e2c1dcf917a8963 Mon Sep 17 00:00:00 2001 From: fredboy Date: Tue, 23 Apr 2024 10:08:18 +0700 Subject: [PATCH] Fix touch enywhere --- .../cavedroid/game/GameRenderer.java | 17 +++++++++-------- .../input/action/keys/MouseInputActionKey.kt | 4 ++++ .../mouse/CloseGameWindowMouseInputHandler.kt | 2 +- .../handler/mouse/HotbarMouseInputHandler.kt | 4 +++- .../game/input/mapper/MouseInputActionMapper.kt | 1 + 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java index 6295011..7647e69 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java @@ -29,6 +29,8 @@ import java.util.Set; @GameScope public class GameRenderer extends Renderer { + private static final TouchButton nullButton = new TouchButton(null, -1, true); + private final MainConfig mMainConfig; private final MobsController mMobsController; private final List mRenderers; @@ -120,10 +122,10 @@ public class GameRenderer extends Renderer { float touchY = transformScreenY(screenY); if (mMainConfig.isTouch()) { - @CheckForNull TouchButton touchedKey = getTouchedKey(touchX, touchY); - if (touchedKey != null && touchedKey.isMouse()) { + TouchButton touchedKey = getTouchedKey(touchX, touchY); + if (touchedKey.isMouse()) { return onMouseActionEvent(screenX, screenY, touchedKey.getCode(), true); - } else if (touchedKey != null) { + } else { return keyUp(touchedKey.getCode()); } } @@ -131,7 +133,6 @@ public class GameRenderer extends Renderer { return onMouseActionEvent(screenX, screenY, button, true); } - @CheckForNull private TouchButton getTouchedKey(float touchX, float touchY) { for (ObjectMap.Entry entry : Assets.guiMap) { TouchButton button = entry.value; @@ -139,7 +140,7 @@ public class GameRenderer extends Renderer { return button; } } - return null; + return nullButton; } @Override @@ -151,10 +152,10 @@ public class GameRenderer extends Renderer { mTouchDownY = touchY; if (mMainConfig.isTouch()) { - @CheckForNull TouchButton touchedKey = getTouchedKey(touchX, touchY); - if (touchedKey != null && touchedKey.isMouse()) { + TouchButton touchedKey = getTouchedKey(touchX, touchY); + if (touchedKey.isMouse()) { return onMouseActionEvent(screenX, screenY, touchedKey.getCode(), false); - } else if (touchedKey != null) { + } else { return keyDown(touchedKey.getCode()); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/action/keys/MouseInputActionKey.kt b/core/src/ru/deadsoftware/cavedroid/game/input/action/keys/MouseInputActionKey.kt index ce5bdc2..5822760 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/action/keys/MouseInputActionKey.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/action/keys/MouseInputActionKey.kt @@ -26,6 +26,10 @@ sealed interface MouseInputActionKey { override val touchUp: Boolean ) : MouseInputActionKey + data class Touch( + override val touchUp: Boolean + ) : MouseInputActionKey + data class Scroll( val amountX: Float, val amountY: Float diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/CloseGameWindowMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/CloseGameWindowMouseInputHandler.kt index 2929c42..1f76039 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/CloseGameWindowMouseInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/CloseGameWindowMouseInputHandler.kt @@ -21,7 +21,7 @@ class CloseGameWindowMouseInputHandler @Inject constructor( override fun checkConditions(action: MouseInputAction): Boolean { return gameWindowsManager.getCurrentWindow() != GameUiWindow.NONE && - action.actionKey is MouseInputActionKey.Left && + (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch) && !action.actionKey.touchUp && !isInsideWindow(action, getCurrentWindowTexture()) } diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/HotbarMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/HotbarMouseInputHandler.kt index 3c2a163..5f983c5 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/HotbarMouseInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/HotbarMouseInputHandler.kt @@ -24,7 +24,9 @@ class HotbarMouseInputHandler @Inject constructor( override fun checkConditions(action: MouseInputAction): Boolean { return buttonHoldTask?.isScheduled == true || - (action.actionKey is MouseInputActionKey.Left && isInsideHotbar(action) || action.actionKey is MouseInputActionKey.Scroll) && + ((action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch) + && isInsideHotbar(action) + || action.actionKey is MouseInputActionKey.Scroll) && gameWindowsManager.getCurrentWindow() == GameUiWindow.NONE } diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/mapper/MouseInputActionMapper.kt b/core/src/ru/deadsoftware/cavedroid/game/input/mapper/MouseInputActionMapper.kt index 666154e..254cee2 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/mapper/MouseInputActionMapper.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/mapper/MouseInputActionMapper.kt @@ -64,6 +64,7 @@ class MouseInputActionMapper @Inject constructor( Input.Buttons.LEFT -> MouseInputActionKey.Left(touchUp) Input.Buttons.RIGHT -> MouseInputActionKey.Right(touchUp) Input.Buttons.MIDDLE -> MouseInputActionKey.Middle(touchUp) + -1 -> MouseInputActionKey.Touch(touchUp) else -> null } } -- 2.29.2