DEADSOFTWARE

Fix touch enywhere
authorfredboy <fredboy@protonmail.com>
Tue, 23 Apr 2024 03:08:18 +0000 (10:08 +0700)
committerfredboy <fredboy@protonmail.com>
Tue, 23 Apr 2024 03:09:51 +0000 (10:09 +0700)
core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java
core/src/ru/deadsoftware/cavedroid/game/input/action/keys/MouseInputActionKey.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/CloseGameWindowMouseInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/HotbarMouseInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/mapper/MouseInputActionMapper.kt

index 62950114bf1ac90246e108989a06d11e062b67b3..7647e69c2a6c0ad9d782de5b5533ccc75123fec1 100644 (file)
@@ -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<IGameRenderer> 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<String, TouchButton> 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());
             }
         }
index ce5bdc2047f62dbc881552e21bcd346ea421c0f9..58227603ae8b08f2b295fbf14f89b0e8adc05ffa 100644 (file)
@@ -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
index 2929c4297d793acbeb310fdf5ac31d1f93ee22e5..1f76039c78fe1575c5fcf6c878e1f69d68cfb30d 100644 (file)
@@ -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())
     }
index 3c2a16317ce6628946aecab8a152916c778e3257..5f983c5967feb860175e45c78effe82b919f46c4 100644 (file)
@@ -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
     }
 
index 666154e81c813f14ecff7f42531c31e5847e14bf..254cee2f49f2cda6ccdc5d48ee34264ed00556e0 100644 (file)
@@ -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
         }
     }