272a17874b69897af5f4b3376131cdc61cfca5ea
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.ui.windows.GameWindowsManager
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.ui.windows.GameWindowsConfigs
13 import ru.deadsoftware.cavedroid.misc.Assets
14 import javax.inject.Inject
16 @GameScope
17 class SelectCreativeInventoryItemMouseInputHandler @Inject constructor(
18 private val gameItemsHolder: GameItemsHolder,
19 private val gameWindowsManager: GameWindowsManager,
20 private val mobsController: MobsController,
21 ) : IGameInputHandler<MouseInputAction> {
23 private val creativeInventoryTexture get() = requireNotNull(Assets.textureRegions["creative"])
25 override fun checkConditions(action: MouseInputAction): Boolean {
26 return gameWindowsManager.getCurrentWindow() == GameUiWindow.CREATIVE_INVENTORY &&
27 !gameWindowsManager.isDragging &&
28 (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) &&
29 action.actionKey.touchUp && isInsideWindow(action, creativeInventoryTexture)
30 }
32 override fun handle(action: MouseInputAction) {
33 val creativeTexture = creativeInventoryTexture
34 val xOnGrid = (action.screenX - (action.cameraViewport.width / 2 - creativeTexture.regionWidth / 2 +
35 GameWindowsConfigs.Creative.itemsGridMarginLeft)) /
36 GameWindowsConfigs.Creative.itemsGridColWidth
37 val yOnGrid = (action.screenY - (action.cameraViewport.height / 2 - creativeTexture.regionHeight / 2 +
38 GameWindowsConfigs.Creative.itemsGridMarginTop)) /
39 GameWindowsConfigs.Creative.itemsGridRowHeight
41 if (xOnGrid < 0 || xOnGrid >= GameWindowsConfigs.Creative.itemsInRow ||
42 yOnGrid < 0 || yOnGrid >= GameWindowsConfigs.Creative.itemsInCol) {
43 return
44 }
46 val itemIndex = (gameWindowsManager.creativeScrollAmount * GameWindowsConfigs.Creative.itemsInRow +
47 (xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Creative.itemsInRow))
48 val item = gameItemsHolder.getItemFromCreativeInventory(itemIndex)
49 mobsController.player.inventory.addItem(item)
50 }
52 }