bf6ba3721e5e9f685f223aae67dd44040d872e88
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.ui.windows.GameWindowsManager
9 import ru.deadsoftware.cavedroid.game.input.IMouseInputHandler
10 import ru.deadsoftware.cavedroid.game.input.MouseInputHandler
11 import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
12 import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey
13 import ru.deadsoftware.cavedroid.game.input.isInsideWindow
14 import ru.deadsoftware.cavedroid.misc.Assets
15 import javax.inject.Inject
16 import kotlin.math.abs
18 @GameScope
19 @MouseInputHandler
20 class CreativeInventoryScrollMouseInputHandler @Inject constructor(
21 private val mainConfig: MainConfig,
22 private val gameWindowsManager: GameWindowsManager,
23 private val gameItemsHolder: GameItemsHolder,
24 ) : IMouseInputHandler {
26 private val creativeInventoryTexture get() = requireNotNull(Assets.textureRegions["creative"])
28 private var dragStartY = 0f
30 override fun checkConditions(action: MouseInputAction): Boolean {
31 return gameWindowsManager.getCurrentWindow() == GameUiWindow.CREATIVE_INVENTORY &&
32 (gameWindowsManager.isDragging || isInsideWindow(action, creativeInventoryTexture)) &&
33 (checkStartDragConditions(action) || checkEndDragConditions(action) ||
34 checkDragConditions(action) || action.actionKey is MouseInputActionKey.Scroll)
36 }
38 private fun checkStartDragConditions(action: MouseInputAction): Boolean {
39 return (action.actionKey is MouseInputActionKey.Screen) &&
40 !action.actionKey.touchUp && !gameWindowsManager.isDragging
41 }
43 private fun checkEndDragConditions(action: MouseInputAction): Boolean {
44 return action.actionKey is MouseInputActionKey.Screen &&
45 action.actionKey.touchUp && gameWindowsManager.isDragging
46 }
48 private fun checkDragConditions(action: MouseInputAction): Boolean {
49 return mainConfig.isTouch && action.actionKey is MouseInputActionKey.Dragged &&
50 abs(action.screenY - dragStartY) >= DRAG_SENSITIVITY
51 }
53 private fun clampScrollAmount() {
54 gameWindowsManager.creativeScrollAmount =
55 MathUtils.clamp(gameWindowsManager.creativeScrollAmount, 0, gameItemsHolder.getMaxCreativeScrollAmount())
56 }
58 private fun handleStartOrEndDrag(action: MouseInputAction) {
59 if (gameWindowsManager.isDragging) {
60 gameWindowsManager.isDragging = false
61 } else {
62 dragStartY = action.screenY
63 }
64 }
66 private fun handleDrag(action: MouseInputAction) {
67 gameWindowsManager.isDragging = true
68 gameWindowsManager.creativeScrollAmount += ((dragStartY - action.screenY) / DRAG_SENSITIVITY).toInt()
69 clampScrollAmount()
70 dragStartY = action.screenY
71 }
73 private fun handleScroll(action: MouseInputAction) {
74 gameWindowsManager.creativeScrollAmount += (action.actionKey as MouseInputActionKey.Scroll).amountY.toInt()
75 clampScrollAmount()
76 }
78 override fun handle(action: MouseInputAction) {
79 when (action.actionKey) {
80 is MouseInputActionKey.Screen -> handleStartOrEndDrag(action)
81 is MouseInputActionKey.Dragged -> handleDrag(action)
82 is MouseInputActionKey.Scroll -> handleScroll(action)
83 else -> return
84 }
85 }
87 companion object {
88 private const val DRAG_SENSITIVITY = 16f
89 }
90 }