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