DEADSOFTWARE

Update version script
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / input / handler / mouse / SelectChestInventoryItemMouseInputHandler.kt
1 package ru.deadsoftware.cavedroid.game.input.handler.mouse
3 import ru.deadsoftware.cavedroid.misc.annotations.multibinding.BindMouseInputHandler
4 import ru.deadsoftware.cavedroid.game.GameItemsHolder
5 import ru.deadsoftware.cavedroid.game.GameScope
6 import ru.deadsoftware.cavedroid.game.GameUiWindow
7 import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
8 import ru.deadsoftware.cavedroid.game.mobs.MobsController
9 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
10 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
11 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.ChestInventoryWindow
12 import ru.deadsoftware.cavedroid.misc.Assets
13 import javax.inject.Inject
15 @GameScope
16 @BindMouseInputHandler
17 class SelectChestInventoryItemMouseInputHandler @Inject constructor(
18 private val gameWindowsManager: GameWindowsManager,
19 private val mobsController: MobsController,
20 private val gameItemsHolder: GameItemsHolder,
21 ) : AbstractInventoryItemsMouseInputHandler(gameItemsHolder, gameWindowsManager, GameUiWindow.CHEST) {
23 override val windowTexture get() = requireNotNull(Assets.textureRegions["chest"])
25 private fun handleInsideContentGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
26 val window = gameWindowsManager.currentWindow as ChestInventoryWindow
27 val itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Chest.contentsInRow
29 handleInsidePlaceableCell(action, window.chest.items, window, itemIndex)
30 }
32 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
33 val window = gameWindowsManager.currentWindow as ChestInventoryWindow
35 var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Chest.itemsInRow
36 itemIndex += GameWindowsConfigs.Chest.hotbarCells
38 if (itemIndex >= mobsController.player.inventory.size) {
39 itemIndex -= mobsController.player.inventory.size
40 }
42 handleInsidePlaceableCell(action, mobsController.player.inventory.items, window, itemIndex)
43 }
45 override fun handle(action: MouseInputAction) {
46 val texture = windowTexture
48 val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - texture.regionWidth / 2)
49 val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - texture.regionHeight / 2)
51 val xOnGrid = (xOnWindow - GameWindowsConfigs.Chest.itemsGridMarginLeft) /
52 GameWindowsConfigs.Chest.itemsGridColWidth
53 val yOnGrid = (yOnWindow - GameWindowsConfigs.Chest.itemsGridMarginTop) /
54 GameWindowsConfigs.Chest.itemsGridRowHeight
56 val xOnContent = (xOnWindow - GameWindowsConfigs.Chest.contentsMarginLeft) /
57 GameWindowsConfigs.Chest.itemsGridColWidth
58 val yOnContent = (yOnWindow - GameWindowsConfigs.Chest.contentsMarginTop) /
59 GameWindowsConfigs.Chest.itemsGridRowHeight
61 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Chest.itemsInRow &&
62 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Chest.itemsInCol
64 val isInsideContentGrid = xOnContent >= 0 && xOnContent < GameWindowsConfigs.Chest.contentsInRow &&
65 yOnContent >= 0 && yOnContent < GameWindowsConfigs.Chest.contentsInCol
68 if (isInsideInventoryGrid) {
69 handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
70 } else if (isInsideContentGrid) {
71 handleInsideContentGrid(action, xOnContent.toInt(), yOnContent.toInt())
72 }
73 }
74 }