88e362005316a756b7e0aaf829493fe20d5a3747
1 package ru.deadsoftware.cavedroid.game.input.handler.mouse
3 import ru.deadsoftware.cavedroid.game.GameItemsHolder
4 import ru.deadsoftware.cavedroid.game.GameScope
5 import ru.deadsoftware.cavedroid.game.GameUiWindow
6 import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
7 import ru.deadsoftware.cavedroid.game.mobs.MobsController
8 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
9 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
10 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.CraftingInventoryWindow
11 import ru.deadsoftware.cavedroid.misc.Assets
12 import javax.inject.Inject
14 @GameScope
15 class SelectCraftingInventoryItemMouseInputHandler @Inject constructor(
16 private val gameWindowsManager: GameWindowsManager,
17 private val mobsController: MobsController,
18 private val gameItemsHolder: GameItemsHolder,
19 ) : AbstractInventoryItemsMouseInputHandler(gameItemsHolder, gameWindowsManager, GameUiWindow.CRAFTING_TABLE) {
21 override val windowTexture get() = requireNotNull(Assets.textureRegions["crafting_table"])
23 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
24 val window = gameWindowsManager.currentWindow as CraftingInventoryWindow
26 var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Crafting.itemsInRow
27 itemIndex += GameWindowsConfigs.Crafting.hotbarCells
29 if (itemIndex >= mobsController.player.inventory.size) {
30 itemIndex -= mobsController.player.inventory.size
31 }
33 handleInsidePlaceableCell(action, mobsController.player.inventory.items, window, itemIndex)
34 }
36 private fun handleInsideCraft(action: MouseInputAction, xOnCraft: Int, yOnCraft: Int) {
37 val window = gameWindowsManager.currentWindow as CraftingInventoryWindow
38 val index = xOnCraft + yOnCraft * GameWindowsConfigs.Crafting.craftGridSize
40 handleInsidePlaceableCell(action, window.craftingItems, window, index)
42 updateCraftResult(window)
43 }
45 private fun handleInsideCraftResult(action: MouseInputAction) {
46 val window = gameWindowsManager.currentWindow as CraftingInventoryWindow
48 handleInsideCraftResultCell(action, window.craftResultList, window, 0)
50 updateCraftResult(window)
51 }
53 override fun handle(action: MouseInputAction) {
54 val texture = windowTexture
56 val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - texture.regionWidth / 2)
57 val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - texture.regionHeight / 2)
59 val xOnGrid = (xOnWindow - GameWindowsConfigs.Crafting.itemsGridMarginLeft) /
60 GameWindowsConfigs.Crafting.itemsGridColWidth
61 val yOnGrid = (yOnWindow - GameWindowsConfigs.Crafting.itemsGridMarginTop) /
62 GameWindowsConfigs.Crafting.itemsGridRowHeight
64 val xOnCraft = (xOnWindow - GameWindowsConfigs.Crafting.craftOffsetX) /
65 GameWindowsConfigs.Crafting.itemsGridColWidth
66 val yOnCraft = (yOnWindow - GameWindowsConfigs.Crafting.craftOffsetY) /
67 GameWindowsConfigs.Crafting.itemsGridRowHeight
69 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Crafting.itemsInRow &&
70 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Crafting.itemsInCol
72 val isInsideCraftGrid = xOnCraft >= 0 && xOnCraft < GameWindowsConfigs.Crafting.craftGridSize &&
73 yOnCraft >= 0 && yOnCraft < GameWindowsConfigs.Crafting.craftGridSize
75 val isInsideCraftResult = xOnWindow > GameWindowsConfigs.Crafting.craftResultOffsetX &&
76 xOnWindow < GameWindowsConfigs.Crafting.craftResultOffsetX + GameWindowsConfigs.Crafting.itemsGridColWidth &&
77 yOnWindow > GameWindowsConfigs.Crafting.craftResultOffsetY &&
78 yOnWindow < GameWindowsConfigs.Crafting.craftResultOffsetY + GameWindowsConfigs.Crafting.itemsGridRowHeight
80 if (isInsideInventoryGrid) {
81 handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
82 } else if (isInsideCraftGrid) {
83 handleInsideCraft(action, xOnCraft.toInt(), yOnCraft.toInt())
84 } else if (isInsideCraftResult) {
85 handleInsideCraftResult(action)
86 }
88 }
89 }