DEADSOFTWARE

f7fa6a5e874405346bd03db187a9351b8aea1497
[cavedroid.git] /
1 package ru.deadsoftware.cavedroid.game.input.handler.mouse
3 import com.badlogic.gdx.graphics.g2d.TextureRegion
4 import ru.deadsoftware.cavedroid.game.GameItemsHolder
5 import ru.deadsoftware.cavedroid.game.GameUiWindow
6 import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
7 import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
8 import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey
9 import ru.deadsoftware.cavedroid.game.input.isInsideWindow
10 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem
11 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem.Companion.isNoneOrNull
12 import ru.deadsoftware.cavedroid.game.objects.container.Furnace
13 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
14 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.AbstractInventoryWindow
15 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.AbstractInventoryWindowWithCraftGrid
17 abstract class AbstractInventoryItemsMouseInputHandler(
18 private val gameItemsHolder: GameItemsHolder,
19 private val gameWindowsManager: GameWindowsManager,
20 private val windowType: GameUiWindow,
21 ) : IGameInputHandler<MouseInputAction> {
23 protected abstract val windowTexture: TextureRegion
25 override fun checkConditions(action: MouseInputAction): Boolean {
26 return gameWindowsManager.getCurrentWindow() == windowType &&
27 isInsideWindow(action, windowTexture) &&
28 (action.actionKey is MouseInputActionKey.Left ||
29 action.actionKey is MouseInputActionKey.Right ||
30 action.actionKey is MouseInputActionKey.Screen)
31 && (action.actionKey.touchUp || action.actionKey is MouseInputActionKey.Screen)
32 }
34 protected fun updateCraftResult(window: AbstractInventoryWindowWithCraftGrid) {
35 window.craftResult = gameItemsHolder.craftItem(window.craftingItems.map(InventoryItem::item))
36 ?: gameItemsHolder.fallbackItem.toInventoryItem()
37 }
39 private fun reduceCraftItems(window: AbstractInventoryWindowWithCraftGrid) {
40 for (i in window.craftingItems.indices) {
41 if (window.craftingItems[i].amount > 1) {
42 window.craftingItems[i].amount--
43 } else {
44 window.craftingItems[i] = gameItemsHolder.fallbackItem.toInventoryItem()
45 }
46 }
47 }
49 protected fun handleInsidePlaceableCell(
50 action: MouseInputAction,
51 items: MutableList<InventoryItem>,
52 window: AbstractInventoryWindow,
53 index: Int
54 ) {
55 if (action.actionKey is MouseInputActionKey.Screen) {
56 if (!action.actionKey.touchUp) {
57 window.onLeftCLick(items, gameItemsHolder, index, action.actionKey.pointer)
58 } else {
59 if (action.actionKey.pointer == window.selectItemPointer) {
60 window.onLeftCLick(items, gameItemsHolder, index, action.actionKey.pointer)
61 } else {
62 window.onRightClick(items, gameItemsHolder, index)
63 }
64 }
65 } else if (action.actionKey is MouseInputActionKey.Left) {
66 window.onLeftCLick(items, gameItemsHolder, index)
67 } else {
68 window.onRightClick(items, gameItemsHolder, index)
69 }
70 }
72 protected fun handleInsideCraftResultCell(
73 action: MouseInputAction,
74 items: MutableList<InventoryItem>,
75 window: AbstractInventoryWindow,
76 index: Int
77 ) {
78 val selectedItem = window.selectedItem
80 if (!selectedItem.isNoneOrNull() && (selectedItem.item != items[index].item ||
81 !selectedItem.canBeAdded(items[index].amount))) {
82 return
83 }
85 if (!selectedItem.isNoneOrNull()) {
86 selectedItem.amount += items[index].amount
87 items[index] = gameItemsHolder.fallbackItem.toInventoryItem()
88 } else {
89 if (action.actionKey is MouseInputActionKey.Screen) {
90 if (!action.actionKey.touchUp) {
91 window.onLeftCLick(items, gameItemsHolder, Furnace.RESULT_INDEX, action.actionKey.pointer)
92 }
93 } else if (action.actionKey is MouseInputActionKey.Left) {
94 window.onLeftCLick(items, gameItemsHolder, index)
95 }
96 }
98 if (window is AbstractInventoryWindowWithCraftGrid) {
99 reduceCraftItems(window)