DEADSOFTWARE

Refactor window controls
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / input / handler / mouse / SelectFurnaceInventoryItemMouseInputHandler.kt
index e11c433ad3749d5f136c66f9bff9f95174124166..f4a00eb9b18d22ed512557393bde5ba1e1342790 100644 (file)
@@ -1,17 +1,11 @@
 package ru.deadsoftware.cavedroid.game.input.handler.mouse
 
-import com.badlogic.gdx.Gdx
 import ru.deadsoftware.cavedroid.game.GameItemsHolder
 import ru.deadsoftware.cavedroid.game.GameScope
 import ru.deadsoftware.cavedroid.game.GameUiWindow
-import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
 import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
-import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey
-import ru.deadsoftware.cavedroid.game.input.isInsideWindow
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.model.item.InventoryItem
 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem.Companion.isNoneOrNull
-import ru.deadsoftware.cavedroid.game.objects.drop.DropController
 import ru.deadsoftware.cavedroid.game.objects.container.Furnace
 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
@@ -24,48 +18,21 @@ class SelectFurnaceInventoryItemMouseInputHandler @Inject constructor(
     private val gameWindowsManager: GameWindowsManager,
     private val mobsController: MobsController,
     private val gameItemsHolder: GameItemsHolder,
-    private val dropController: DropController,
-) : IGameInputHandler<MouseInputAction> {
+) : AbstractInventoryItemsMouseInputHandler(gameItemsHolder, gameWindowsManager, GameUiWindow.FURNACE) {
 
-    private val survivalWindowTexture get() = requireNotNull(Assets.textureRegions["survival"])
-
-    override fun checkConditions(action: MouseInputAction): Boolean {
-        return gameWindowsManager.getCurrentWindow() == GameUiWindow.FURNACE &&
-                isInsideWindow(action, survivalWindowTexture) &&
-                (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Right || action.actionKey is MouseInputActionKey.Screen)
-                && (action.actionKey.touchUp || action.actionKey is MouseInputActionKey.Screen)
-    }
+    override val windowTexture get() = requireNotNull(Assets.textureRegions["furnace"])
 
     private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
         val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
 
-        var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Furnace.itemsInRow))
+        var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Furnace.itemsInRow
         itemIndex += GameWindowsConfigs.Furnace.hotbarCells
 
         if (itemIndex >= mobsController.player.inventory.size) {
             itemIndex -= mobsController.player.inventory.size
         }
 
-        if (action.actionKey is MouseInputActionKey.Screen) {
-            if (!action.actionKey.touchUp) {
-                window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
-            } else {
-                if (action.actionKey.pointer == window.selectItemPointer) {
-                    window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
-                } else {
-                    window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
-                }
-            }
-        } else if (action.actionKey is MouseInputActionKey.Left) {
-            window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex)
-        } else {
-            window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
-        }
-
-        Gdx.app.debug(
-            TAG,
-            "selected item: ${window.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)"
-        )
+        handleInsidePlaceableCell(action, mobsController.player.inventory.items, window, itemIndex)
     }
 
     private fun handleInsideFuel(action: MouseInputAction) {
@@ -75,49 +42,26 @@ class SelectFurnaceInventoryItemMouseInputHandler @Inject constructor(
             return
         }
 
-        if (action.actionKey is MouseInputActionKey.Screen) {
-            if (!action.actionKey.touchUp) {
-                window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.FUEL_INDEX, action.actionKey.pointer)
-            } else {
-                if (action.actionKey.pointer == window.selectItemPointer) {
-                    window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.FUEL_INDEX, action.actionKey.pointer)
-                } else {
-                    window.onRightClick(window.furnace.items as MutableList<InventoryItem?>, Furnace.FUEL_INDEX)
-                }
-            }
-        } else if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
-            window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.FUEL_INDEX)
-        } else {
-            window.onRightClick(window.furnace.items as MutableList<InventoryItem?>, Furnace.FUEL_INDEX)
-        }
+        handleInsidePlaceableCell(action, window.furnace.items, window, Furnace.FUEL_INDEX)
     }
 
     private fun handleInsideInput(action: MouseInputAction) {
         val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
 
-        if (action.actionKey is MouseInputActionKey.Screen) {
-            if (!action.actionKey.touchUp) {
-                window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.INPUT_INDEX, action.actionKey.pointer)
-            } else {
-                if (action.actionKey.pointer == window.selectItemPointer) {
-                    window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.INPUT_INDEX, action.actionKey.pointer)
-                } else {
-                    window.onRightClick(window.furnace.items as MutableList<InventoryItem?>, Furnace.INPUT_INDEX)
-                }
-            }
-        } else if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
-            window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.INPUT_INDEX)
-        } else {
-            window.onRightClick(window.furnace.items as MutableList<InventoryItem?>, Furnace.INPUT_INDEX)
-        }
+        handleInsidePlaceableCell(action, window.furnace.items, window, Furnace.INPUT_INDEX)
     }
 
-    override fun handle(action: MouseInputAction) {
-        val survivalTexture = survivalWindowTexture
+    private fun handleInsideResult(action: MouseInputAction) {
         val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
 
-        val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - survivalTexture.regionWidth / 2)
-        val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - survivalTexture.regionHeight / 2)
+        handleInsideCraftResultCell(action, window.furnace.items, window, Furnace.RESULT_INDEX)
+    }
+
+    override fun handle(action: MouseInputAction) {
+        val texture = windowTexture
+
+        val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - texture.regionWidth / 2)
+        val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - texture.regionHeight / 2)
 
         val xOnGrid = (xOnWindow - GameWindowsConfigs.Furnace.itemsGridMarginLeft) /
                 GameWindowsConfigs.Furnace.itemsGridColWidth
@@ -148,24 +92,9 @@ class SelectFurnaceInventoryItemMouseInputHandler @Inject constructor(
             handleInsideFuel(action)
         } else if (isInsideInput) {
             handleInsideInput(action)
-        } else if (isInsideResult && action.actionKey.touchUp) {
-            val selectedItem = window.selectedItem
-            if (selectedItem == null || selectedItem.item.isNone() ||
-                (selectedItem.item == window.furnace.result?.item && selectedItem.amount + (window.furnace.result?.amount ?: 0) <= selectedItem.item.params.maxStack)) {
-
-                if (selectedItem != null && !selectedItem.item.isNone()) {
-                    selectedItem.amount += (window.furnace.result?.amount ?: 0)
-                } else {
-                    window.selectedItem = window.furnace.result
-                }
-                window.furnace.items[Furnace.RESULT_INDEX] = gameItemsHolder.fallbackItem.toInventoryItem()
-            }
+        } else if (isInsideResult) {
+            handleInsideResult(action)
         }
 
     }
-
-    companion object {
-        private const val TAG = "SelectFurnaceInventoryItemMouseInputHandler"
-
-    }
 }
\ No newline at end of file