DEADSOFTWARE

3d1bfae969db22a0ea3594c6766e173d64951721
[cavedroid.git] /
1 package ru.deadsoftware.cavedroid.game.input.handler.mouse
3 import com.badlogic.gdx.Gdx
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.IGameInputHandler
8 import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
9 import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey
10 import ru.deadsoftware.cavedroid.game.input.isInsideWindow
11 import ru.deadsoftware.cavedroid.game.mobs.MobsController
12 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem
13 import ru.deadsoftware.cavedroid.game.objects.drop.DropController
14 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
15 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
16 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.ChestInventoryWindow
17 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.SurvivalInventoryWindow
18 import ru.deadsoftware.cavedroid.misc.Assets
19 import javax.inject.Inject
21 @GameScope
22 class SelectChestInventoryItemMouseInputHandler @Inject constructor(
23 private val gameWindowsManager: GameWindowsManager,
24 private val mobsController: MobsController,
25 private val gameItemsHolder: GameItemsHolder,
26 private val dropController: DropController,
27 ) : IGameInputHandler<MouseInputAction> {
29 private val chestWindowTexture get() = requireNotNull(Assets.textureRegions["chest"])
31 override fun checkConditions(action: MouseInputAction): Boolean {
32 return gameWindowsManager.getCurrentWindow() == GameUiWindow.CHEST &&
33 isInsideWindow(action, chestWindowTexture) &&
34 (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Right || action.actionKey is MouseInputActionKey.Screen)
35 && (action.actionKey.touchUp || action.actionKey is MouseInputActionKey.Screen)
36 }
38 private fun handleInsideContentGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
39 val window = gameWindowsManager.currentWindow as ChestInventoryWindow
41 val itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Chest.contentsInRow))
43 if (action.actionKey is MouseInputActionKey.Screen) {
44 if (!action.actionKey.touchUp) {
45 window.onLeftCLick(window.chest.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
46 } else {
47 if (action.actionKey.pointer == window.selectItemPointer) {
48 window.onLeftCLick(window.chest.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
49 } else {
50 window.onRightClick(window.chest.items as MutableList<InventoryItem?>, itemIndex)
51 }
52 }
53 } else if (action.actionKey is MouseInputActionKey.Left) {
54 window.onLeftCLick(window.chest.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex)
55 } else {
56 window.onRightClick(window.chest.items as MutableList<InventoryItem?>, itemIndex)
57 }
59 Gdx.app.debug(
60 TAG,
61 "selected item: ${window.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)"
62 )
63 }
65 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
66 val window = gameWindowsManager.currentWindow as ChestInventoryWindow
68 var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Chest.itemsInRow))
69 itemIndex += GameWindowsConfigs.Chest.hotbarCells
71 if (itemIndex >= mobsController.player.inventory.size) {
72 itemIndex -= mobsController.player.inventory.size
73 }
75 if (action.actionKey is MouseInputActionKey.Screen) {
76 if (!action.actionKey.touchUp) {
77 window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
78 } else {
79 if (action.actionKey.pointer == window.selectItemPointer) {
80 window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
81 } else {
82 window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
83 }
84 }
85 } else if (action.actionKey is MouseInputActionKey.Left) {
86 window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex)
87 } else {
88 window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
89 }
91 Gdx.app.debug(
92 TAG,
93 "selected item: ${window.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)"
94 )
95 }
97 override fun handle(action: MouseInputAction) {
98 val chestTexture = chestWindowTexture
100 val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - chestTexture.regionWidth / 2)
101 val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - chestTexture.regionHeight / 2)
103 val xOnGrid = (xOnWindow - GameWindowsConfigs.Chest.itemsGridMarginLeft) /
104 GameWindowsConfigs.Chest.itemsGridColWidth
105 val yOnGrid = (yOnWindow - GameWindowsConfigs.Chest.itemsGridMarginTop) /
106 GameWindowsConfigs.Chest.itemsGridRowHeight
108 val xOnContent = (xOnWindow - GameWindowsConfigs.Chest.contentsMarginLeft) /
109 GameWindowsConfigs.Chest.itemsGridColWidth
110 val yOnContent = (yOnWindow - GameWindowsConfigs.Chest.contentsMarginTop) /
111 GameWindowsConfigs.Chest.itemsGridRowHeight
113 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Chest.itemsInRow &&
114 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Chest.itemsInCol
116 val isInsideContentGrid = xOnContent >= 0 && xOnContent < GameWindowsConfigs.Chest.contentsInRow &&
117 yOnContent >= 0 && yOnContent < GameWindowsConfigs.Chest.contentsInCol
120 if (isInsideInventoryGrid) {
121 handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
122 } else if (isInsideContentGrid) {
123 handleInsideContentGrid(action, xOnContent.toInt(), yOnContent.toInt())
127 companion object {
128 private const val TAG = "SelectChestInventoryItemMouseInputHandler"