[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 d5858ac0d70822b6f1aa2a317669273c75f4e203..58687fcbc1ae0bf5063ed516d55cf52bc19d8265 100644 (file)
package ru.deadsoftware.cavedroid.game.input.handler.mouse
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.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.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.mobs.MobsController
-import ru.deadsoftware.cavedroid.game.model.item.InventoryItem
-import ru.deadsoftware.cavedroid.game.objects.DropController
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.game.ui.windows.GameWindowsConfigs
import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
import ru.deadsoftware.cavedroid.game.ui.windows.inventory.SurvivalInventoryWindow
import javax.inject.Inject
@GameScope
import javax.inject.Inject
@GameScope
+@BindMouseInputHandler
class SelectSurvivalInventoryItemMouseInputHandler @Inject constructor(
private val gameWindowsManager: GameWindowsManager,
private val mobsController: MobsController,
private val gameItemsHolder: GameItemsHolder,
class SelectSurvivalInventoryItemMouseInputHandler @Inject constructor(
private val gameWindowsManager: GameWindowsManager,
private val mobsController: MobsController,
private val gameItemsHolder: GameItemsHolder,
- private val dropController: DropController,
-) : IGameInputHandler<MouseInputAction> {
+) : AbstractInventoryItemsMouseInputHandler(gameItemsHolder, gameWindowsManager, GameUiWindow.SURVIVAL_INVENTORY) {
- private val survivalWindowTexture 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.Right || action.actionKey is MouseInputActionKey.Screen)
- && (action.actionKey.touchUp || action.actionKey is MouseInputActionKey.Screen)
- }
+ override val windowTexture get() = requireNotNull(Assets.textureRegions["survival"])
private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
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
}
itemIndex += GameWindowsConfigs.Survival.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 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!!
}
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!!
- if (action.actionKey is MouseInputActionKey.Screen) {
- if (!action.actionKey.touchUp) {
- window.onLeftCLick(window.craftingItems, gameItemsHolder, index, action.actionKey.pointer)
- } else {
- if (action.actionKey.pointer == window.selectItemPointer) {
- window.onLeftCLick(window.craftingItems, gameItemsHolder, index, action.actionKey.pointer)
- } else {
- window.onRightClick(window.craftingItems, index)
- }
- }
- } else if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
- window.onLeftCLick(window.craftingItems, gameItemsHolder, index)
- } else {
- window.onRightClick(window.craftingItems, index)
- }
+ handleInsidePlaceableCell(action, window.craftingItems, window, index)
- window.craftResult =
- gameItemsHolder.craftItem(window.craftingItems.map { it?.item ?: gameItemsHolder.fallbackItem })
+ updateCraftResult(window)
}
}
- override fun handle(action: MouseInputAction) {
- val survivalTexture = survivalWindowTexture
+ private fun handleInsideCraftResult(action: MouseInputAction) {
val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
- 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.craftResultList, window, 0)
+
+ updateCraftResult(window)
+ }
+
+ 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 xOnGrid = (xOnWindow - GameWindowsConfigs.Survival.itemsGridMarginLeft) /
GameWindowsConfigs.Survival.itemsGridColWidth
handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
} else if (isInsideCraftGrid) {
handleInsideCraft(action, xOnCraft.toInt(), yOnCraft.toInt())
handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
} else if (isInsideCraftGrid) {
handleInsideCraft(action, xOnCraft.toInt(), yOnCraft.toInt())
- } else if (isInsideCraftResult && action.actionKey.touchUp) {
- val selectedItem = window.selectedItem
- if (selectedItem == null || selectedItem.item.isNone() ||
- (selectedItem.item == window.craftResult?.item && selectedItem.amount + (window.craftResult?.amount ?: 0) <= selectedItem.item.params.maxStack)) {
- for (i in window.craftingItems.indices) {
- if ((window.craftingItems[i]?.amount ?: 0) > 1) {
- window.craftingItems[i]?.amount = window.craftingItems[i]?.amount!! - 1
- } else {
- window.craftingItems[i] = null
- }
- }
- if (selectedItem != null && !selectedItem.item.isNone()) {
- selectedItem.amount += (window.craftResult?.amount ?: 0)
- } else {
- window.selectedItem = window.craftResult
- }
- window.craftResult = gameItemsHolder.craftItem(window.craftingItems
- .map { it?.item ?: gameItemsHolder.fallbackItem })
- }
- } else if (action.actionKey.touchUp) {
- window.selectedItem?.let { selectedItem ->
- dropController.addDrop(
- /* x = */ mobsController.player.x + (32f * mobsController.player.direction.basis),
- /* y = */ mobsController.player.y,
- /* item = */ selectedItem.item,
- /* count = */ selectedItem.amount,
- )
- window.selectedItem = null
- }
+ } else if (isInsideCraftResult) {
+ handleInsideCraftResult(action)
}
}
}
}
-
- companion object {
- private const val TAG = "SelectSurvivalInventoryItemMouseInputHandler"
-
- }
}
\ No newline at end of file
}
\ No newline at end of file