DEADSOFTWARE

Update version script
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / input / handler / mouse / AbstractInventoryItemsMouseInputHandler.kt
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.IMouseInputHandler
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.ui.windows.GameWindowsManager
13 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.AbstractInventoryWindow
14 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.AbstractInventoryWindowWithCraftGrid
16 abstract class AbstractInventoryItemsMouseInputHandler(
17 private val gameItemsHolder: GameItemsHolder,
18 private val gameWindowsManager: GameWindowsManager,
19 private val windowType: GameUiWindow,
20 ) : IMouseInputHandler {
22 protected abstract val windowTexture: TextureRegion
24 override fun checkConditions(action: MouseInputAction): Boolean {
25 return gameWindowsManager.getCurrentWindow() == windowType &&
26 isInsideWindow(action, windowTexture) &&
27 (action.actionKey is MouseInputActionKey.Left ||
28 action.actionKey is MouseInputActionKey.Right ||
29 action.actionKey is MouseInputActionKey.Screen)
30 && (action.actionKey.touchUp || action.actionKey is MouseInputActionKey.Screen)
31 }
33 protected fun updateCraftResult(window: AbstractInventoryWindowWithCraftGrid) {
34 window.craftResult = gameItemsHolder.craftItem(window.craftingItems.map(InventoryItem::item))
35 ?: gameItemsHolder.fallbackItem.toInventoryItem()
36 }
38 private fun reduceCraftItems(window: AbstractInventoryWindowWithCraftGrid) {
39 for (i in window.craftingItems.indices) {
40 if (window.craftingItems[i].amount > 1) {
41 window.craftingItems[i].amount--
42 } else {
43 window.craftingItems[i] = gameItemsHolder.fallbackItem.toInventoryItem()
44 }
45 }
46 }
48 protected fun handleInsidePlaceableCell(
49 action: MouseInputAction,
50 items: MutableList<InventoryItem>,
51 window: AbstractInventoryWindow,
52 index: Int
53 ) {
54 if (action.actionKey is MouseInputActionKey.Screen) {
55 if (!action.actionKey.touchUp) {
56 window.onLeftCLick(items, gameItemsHolder, index, action.actionKey.pointer)
57 } else {
58 if (action.actionKey.pointer == window.selectItemPointer) {
59 window.onLeftCLick(items, gameItemsHolder, index, action.actionKey.pointer)
60 } else {
61 window.onRightClick(items, gameItemsHolder, index)
62 }
63 }
64 } else if (action.actionKey is MouseInputActionKey.Left) {
65 window.onLeftCLick(items, gameItemsHolder, index)
66 } else {
67 window.onRightClick(items, gameItemsHolder, index)
68 }
69 }
71 protected fun handleInsideCraftResultCell(
72 action: MouseInputAction,
73 items: MutableList<InventoryItem>,
74 window: AbstractInventoryWindow,
75 index: Int
76 ) {
77 val selectedItem = window.selectedItem
79 if (!selectedItem.isNoneOrNull() && (selectedItem.item != items[index].item ||
80 !selectedItem.canBeAdded(items[index].amount))) {
81 return
82 }
84 if (!selectedItem.isNoneOrNull()) {
85 selectedItem.amount += items[index].amount
86 items[index] = gameItemsHolder.fallbackItem.toInventoryItem()
87 } else {
88 if (action.actionKey is MouseInputActionKey.Screen) {
89 if (!action.actionKey.touchUp) {
90 window.onLeftCLick(items, gameItemsHolder, index, action.actionKey.pointer)
91 }
92 } else if (action.actionKey is MouseInputActionKey.Left) {
93 window.onLeftCLick(items, gameItemsHolder, index)
94 }
95 }
97 if (window is AbstractInventoryWindowWithCraftGrid) {
98 reduceCraftItems(window)
99 }