[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / input / handler / mouse / SelectSurvivalInventoryItemMouseInputHandler.kt
diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt
index 559ccfc79388a09157838d3238f0326709764f24..58687fcbc1ae0bf5063ed516d55cf52bc19d8265 100644 (file)
package ru.deadsoftware.cavedroid.game.input.handler.mouse
-import com.badlogic.gdx.Gdx
+import ru.deadsoftware.cavedroid.misc.annotations.multibinding.BindMouseInputHandler
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.windows.GameWindowsConfigs
-import ru.deadsoftware.cavedroid.game.windows.GameWindowsManager
+import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
+import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
+import ru.deadsoftware.cavedroid.game.ui.windows.inventory.SurvivalInventoryWindow
import ru.deadsoftware.cavedroid.misc.Assets
import javax.inject.Inject
@GameScope
+@BindMouseInputHandler
class SelectSurvivalInventoryItemMouseInputHandler @Inject constructor(
private val gameWindowsManager: GameWindowsManager,
private val mobsController: MobsController,
private val gameItemsHolder: GameItemsHolder,
-) : IGameInputHandler<MouseInputAction> {
+) : AbstractInventoryItemsMouseInputHandler(gameItemsHolder, gameWindowsManager, GameUiWindow.SURVIVAL_INVENTORY) {
- private val survivalWindowTexture get() = requireNotNull(Assets.textureRegions["survival"])
+ override val windowTexture get() = requireNotNull(Assets.textureRegions["survival"])
- override fun checkConditions(action: MouseInputAction): Boolean {
- return gameWindowsManager.getCurrentWindow() == GameUiWindow.SURVIVAL_INVENTORY &&
- isInsideWindow(action, survivalWindowTexture) &&
- (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch)
- && action.actionKey.touchUp
- }
-
- override fun handle(action: MouseInputAction) {
- val survivalTexture = survivalWindowTexture
- val xOnGrid = (action.screenX - (action.cameraViewport.width / 2 - survivalTexture.regionWidth / 2 +
- GameWindowsConfigs.Survival.itemsGridMarginLeft)) /
- GameWindowsConfigs.Survival.itemsGridColWidth
- val yOnGrid = (action.screenY - (action.cameraViewport.height / 2 - survivalTexture.regionHeight / 2 +
- GameWindowsConfigs.Survival.itemsGridMarginTop)) /
- GameWindowsConfigs.Survival.itemsGridRowHeight
-
- if (xOnGrid < 0 || xOnGrid >= GameWindowsConfigs.Survival.itemsInRow ||
- yOnGrid < 0 || yOnGrid > GameWindowsConfigs.Survival.itemsInCol) {
- return
- }
+ private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
+ val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
- var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Survival.itemsInRow))
+ var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Survival.itemsInRow
itemIndex += GameWindowsConfigs.Survival.hotbarCells
if (itemIndex >= mobsController.player.inventory.size) {
itemIndex -= mobsController.player.inventory.size
}
- val item = mobsController.player.inventory[itemIndex]
- mobsController.player.inventory[itemIndex] = gameWindowsManager.selectedItem ?: gameItemsHolder.fallbackItem.toInventoryItem()
- gameWindowsManager.selectedItem = item
+ handleInsidePlaceableCell(action, mobsController.player.inventory.items, window, itemIndex)
+ }
+
+ private fun handleInsideCraft(action: MouseInputAction, xOnCraft: Int, yOnCraft: Int) {
+ val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
+ val index = xOnCraft + yOnCraft * GameWindowsConfigs.Crafting.craftGridSize // this is crafting on purpose!!
+
+ handleInsidePlaceableCell(action, window.craftingItems, window, index)
+
+ updateCraftResult(window)
+ }
+
+ private fun handleInsideCraftResult(action: MouseInputAction) {
+ val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
- Gdx.app.debug(TAG, "selected item: ${gameWindowsManager.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)")
+ handleInsideCraftResultCell(action, window.craftResultList, window, 0)
+
+ updateCraftResult(window)
}
- companion object {
- private const val TAG = "SelectSurvivalInventoryItemMouseInputHandler"
+ override fun handle(action: MouseInputAction) {
+ val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - windowTexture.regionWidth / 2)
+ val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - windowTexture.regionHeight / 2)
+
+ val xOnGrid = (xOnWindow - GameWindowsConfigs.Survival.itemsGridMarginLeft) /
+ GameWindowsConfigs.Survival.itemsGridColWidth
+ val yOnGrid = (yOnWindow - GameWindowsConfigs.Survival.itemsGridMarginTop) /
+ GameWindowsConfigs.Survival.itemsGridRowHeight
+
+ val xOnCraft = (xOnWindow - GameWindowsConfigs.Survival.craftOffsetX) /
+ GameWindowsConfigs.Survival.itemsGridColWidth
+ val yOnCraft = (yOnWindow - GameWindowsConfigs.Survival.craftOffsetY) /
+ GameWindowsConfigs.Survival.itemsGridRowHeight
+
+ val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Survival.itemsInRow &&
+ yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Survival.itemsInCol
+
+ val isInsideCraftGrid = xOnCraft >= 0 && xOnCraft < GameWindowsConfigs.Survival.craftGridSize &&
+ yOnCraft >= 0 && yOnCraft < GameWindowsConfigs.Survival.craftGridSize
+
+ val isInsideCraftResult = xOnWindow > GameWindowsConfigs.Survival.craftResultOffsetX &&
+ xOnWindow < GameWindowsConfigs.Survival.craftResultOffsetX + GameWindowsConfigs.Survival.itemsGridColWidth &&
+ yOnWindow > GameWindowsConfigs.Survival.craftResultOffsetY &&
+ yOnWindow < GameWindowsConfigs.Survival.craftResultOffsetY + GameWindowsConfigs.Survival.itemsGridRowHeight
+
+ if (isInsideInventoryGrid) {
+ handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
+ } else if (isInsideCraftGrid) {
+ handleInsideCraft(action, xOnCraft.toInt(), yOnCraft.toInt())
+ } else if (isInsideCraftResult) {
+ handleInsideCraftResult(action)
+ }
}
}
\ No newline at end of file