DEADSOFTWARE

e11c433ad3749d5f136c66f9bff9f95174124166
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / input / handler / mouse / SelectFurnaceInventoryItemMouseInputHandler.kt
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.model.item.InventoryItem.Companion.isNoneOrNull
14 import ru.deadsoftware.cavedroid.game.objects.drop.DropController
15 import ru.deadsoftware.cavedroid.game.objects.container.Furnace
16 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
17 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
18 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.FurnaceInventoryWindow
19 import ru.deadsoftware.cavedroid.misc.Assets
20 import javax.inject.Inject
22 @GameScope
23 class SelectFurnaceInventoryItemMouseInputHandler @Inject constructor(
24 private val gameWindowsManager: GameWindowsManager,
25 private val mobsController: MobsController,
26 private val gameItemsHolder: GameItemsHolder,
27 private val dropController: DropController,
28 ) : IGameInputHandler<MouseInputAction> {
30 private val survivalWindowTexture get() = requireNotNull(Assets.textureRegions["survival"])
32 override fun checkConditions(action: MouseInputAction): Boolean {
33 return gameWindowsManager.getCurrentWindow() == GameUiWindow.FURNACE &&
34 isInsideWindow(action, survivalWindowTexture) &&
35 (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Right || action.actionKey is MouseInputActionKey.Screen)
36 && (action.actionKey.touchUp || action.actionKey is MouseInputActionKey.Screen)
37 }
39 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
40 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
42 var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Furnace.itemsInRow))
43 itemIndex += GameWindowsConfigs.Furnace.hotbarCells
45 if (itemIndex >= mobsController.player.inventory.size) {
46 itemIndex -= mobsController.player.inventory.size
47 }
49 if (action.actionKey is MouseInputActionKey.Screen) {
50 if (!action.actionKey.touchUp) {
51 window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
52 } else {
53 if (action.actionKey.pointer == window.selectItemPointer) {
54 window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
55 } else {
56 window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
57 }
58 }
59 } else if (action.actionKey is MouseInputActionKey.Left) {
60 window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex)
61 } else {
62 window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
63 }
65 Gdx.app.debug(
66 TAG,
67 "selected item: ${window.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)"
68 )
69 }
71 private fun handleInsideFuel(action: MouseInputAction) {
72 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
74 if (!window.selectedItem.isNoneOrNull() && window.selectedItem?.item?.params?.burningTimeMs == null) {
75 return
76 }
78 if (action.actionKey is MouseInputActionKey.Screen) {
79 if (!action.actionKey.touchUp) {
80 window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.FUEL_INDEX, action.actionKey.pointer)
81 } else {
82 if (action.actionKey.pointer == window.selectItemPointer) {
83 window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.FUEL_INDEX, action.actionKey.pointer)
84 } else {
85 window.onRightClick(window.furnace.items as MutableList<InventoryItem?>, Furnace.FUEL_INDEX)
86 }
87 }
88 } else if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
89 window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.FUEL_INDEX)
90 } else {
91 window.onRightClick(window.furnace.items as MutableList<InventoryItem?>, Furnace.FUEL_INDEX)
92 }
93 }
95 private fun handleInsideInput(action: MouseInputAction) {
96 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
98 if (action.actionKey is MouseInputActionKey.Screen) {
99 if (!action.actionKey.touchUp) {
100 window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.INPUT_INDEX, action.actionKey.pointer)
101 } else {
102 if (action.actionKey.pointer == window.selectItemPointer) {
103 window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.INPUT_INDEX, action.actionKey.pointer)
104 } else {
105 window.onRightClick(window.furnace.items as MutableList<InventoryItem?>, Furnace.INPUT_INDEX)
108 } else if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
109 window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.INPUT_INDEX)
110 } else {
111 window.onRightClick(window.furnace.items as MutableList<InventoryItem?>, Furnace.INPUT_INDEX)
115 override fun handle(action: MouseInputAction) {
116 val survivalTexture = survivalWindowTexture
117 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
119 val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - survivalTexture.regionWidth / 2)
120 val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - survivalTexture.regionHeight / 2)
122 val xOnGrid = (xOnWindow - GameWindowsConfigs.Furnace.itemsGridMarginLeft) /
123 GameWindowsConfigs.Furnace.itemsGridColWidth
124 val yOnGrid = (yOnWindow - GameWindowsConfigs.Furnace.itemsGridMarginTop) /
125 GameWindowsConfigs.Furnace.itemsGridRowHeight
127 val isInsideInput = xOnWindow > GameWindowsConfigs.Furnace.smeltInputMarginLeft &&
128 xOnWindow < GameWindowsConfigs.Furnace.smeltInputMarginLeft + GameWindowsConfigs.Furnace.itemsGridColWidth &&
129 yOnWindow > GameWindowsConfigs.Furnace.smeltInputMarginTop &&
130 yOnWindow < GameWindowsConfigs.Furnace.smeltInputMarginTop + GameWindowsConfigs.Furnace.itemsGridRowHeight
132 val isInsideFuel = xOnWindow > GameWindowsConfigs.Furnace.smeltFuelMarginLeft &&
133 xOnWindow < GameWindowsConfigs.Furnace.smeltFuelMarginLeft + GameWindowsConfigs.Furnace.itemsGridColWidth &&
134 yOnWindow > GameWindowsConfigs.Furnace.smeltFuelMarginTop &&
135 yOnWindow < GameWindowsConfigs.Furnace.smeltFuelMarginTop + GameWindowsConfigs.Furnace.itemsGridRowHeight
137 val isInsideResult = xOnWindow > GameWindowsConfigs.Furnace.smeltResultOffsetX &&
138 xOnWindow < GameWindowsConfigs.Furnace.smeltResultOffsetX + GameWindowsConfigs.Furnace.itemsGridColWidth &&
139 yOnWindow > GameWindowsConfigs.Furnace.smeltResultOffsetY &&
140 yOnWindow < GameWindowsConfigs.Furnace.smeltResultOffsetY + GameWindowsConfigs.Furnace.itemsGridRowHeight
142 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Furnace.itemsInRow &&
143 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Furnace.itemsInCol
145 if (isInsideInventoryGrid) {
146 handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
147 } else if (isInsideFuel) {
148 handleInsideFuel(action)
149 } else if (isInsideInput) {
150 handleInsideInput(action)
151 } else if (isInsideResult && action.actionKey.touchUp) {
152 val selectedItem = window.selectedItem
153 if (selectedItem == null || selectedItem.item.isNone() ||
154 (selectedItem.item == window.furnace.result?.item && selectedItem.amount + (window.furnace.result?.amount ?: 0) <= selectedItem.item.params.maxStack)) {
156 if (selectedItem != null && !selectedItem.item.isNone()) {
157 selectedItem.amount += (window.furnace.result?.amount ?: 0)
158 } else {
159 window.selectedItem = window.furnace.result
161 window.furnace.items[Furnace.RESULT_INDEX] = gameItemsHolder.fallbackItem.toInventoryItem()
167 companion object {
168 private const val TAG = "SelectFurnaceInventoryItemMouseInputHandler"