DEADSOFTWARE

52e9f627071ea971fc8c148ed97730f316be6514
[cavedroid.git] /
1 package ru.deadsoftware.cavedroid.game.input.handler.mouse
3 import com.badlogic.gdx.math.MathUtils
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.windows.GameWindowsManager
8 import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
9 import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
10 import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey
11 import ru.deadsoftware.cavedroid.game.input.isInsideWindow
12 import ru.deadsoftware.cavedroid.misc.Assets
13 import javax.inject.Inject
14 import kotlin.math.abs
16 @GameScope
17 class CreativeInventoryScrollMouseInputHandler @Inject constructor(
18 private val gameWindowsManager: GameWindowsManager,
19 private val gameItemsHolder: GameItemsHolder,
20 ) : IGameInputHandler<MouseInputAction> {
22 private val creativeInventoryTexture get() = requireNotNull(Assets.textureRegions["creative"])
24 private var dragStartY = 0f
26 override fun checkConditions(action: MouseInputAction): Boolean {
27 return gameWindowsManager.getCurrentWindow() == GameUiWindow.CREATIVE_INVENTORY &&
28 (gameWindowsManager.isDragging || isInsideWindow(action, creativeInventoryTexture)) &&
29 (checkStartDragConditions(action) || checkEndDragConditions(action) ||
30 checkDragConditions(action) || action.actionKey is MouseInputActionKey.Scroll)
32 }
34 private fun checkStartDragConditions(action: MouseInputAction): Boolean {
35 return action.actionKey is MouseInputActionKey.Left &&
36 !action.actionKey.touchUp && !gameWindowsManager.isDragging
37 }
39 private fun checkEndDragConditions(action: MouseInputAction): Boolean {
40 return action.actionKey is MouseInputActionKey.Left &&
41 action.actionKey.touchUp && gameWindowsManager.isDragging
42 }
44 private fun checkDragConditions(action: MouseInputAction): Boolean {
45 return action.actionKey is MouseInputActionKey.Dragged &&
46 abs(action.screenY - dragStartY) >= DRAG_SENSITIVITY
47 }
49 private fun clampScrollAmount() {
50 gameWindowsManager.creativeScrollAmount =
51 MathUtils.clamp(gameWindowsManager.creativeScrollAmount, 0, gameItemsHolder.getMaxCreativeScrollAmount())
52 }
54 private fun handleStartOrEndDrag(action: MouseInputAction) {
55 if (gameWindowsManager.isDragging) {
56 gameWindowsManager.isDragging = false
57 } else {
58 dragStartY = action.screenY
59 }
60 }
62 private fun handleDrag(action: MouseInputAction) {
63 gameWindowsManager.isDragging = true
64 gameWindowsManager.creativeScrollAmount += ((dragStartY - action.screenY) / DRAG_SENSITIVITY).toInt()
65 clampScrollAmount()
66 dragStartY = action.screenY
67 }
69 private fun handleScroll(action: MouseInputAction) {
70 gameWindowsManager.creativeScrollAmount += (action.actionKey as MouseInputActionKey.Scroll).amountY.toInt()
71 clampScrollAmount()
72 }
74 override fun handle(action: MouseInputAction) {
75 when (action.actionKey) {
76 is MouseInputActionKey.Left -> handleStartOrEndDrag(action)
77 is MouseInputActionKey.Dragged -> handleDrag(action)
78 is MouseInputActionKey.Scroll -> handleScroll(action)
79 else -> return
80 }
81 }
83 companion object {
84 private const val DRAG_SENSITIVITY = 16f
85 }
86 }