diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatteryScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatteryScreen.kt index a7799c023..e723ad13e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatteryScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/MatteryScreen.kt @@ -434,6 +434,16 @@ abstract class MatteryScreen(menu: T, inventory: Inventory, tit return null } + fun isMouseGrabbed(): Boolean { + for (panel in panels) { + if (panel.isGrabbingMouseInput()) { + return true + } + } + + return false + } + override fun render(poseStack: PoseStack, mouseX: Int, mouseY: Int, partialTick: Float) { val mouseXf = mouseX.toFloat() val mouseYf = mouseY.toFloat() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/ButtonPanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/ButtonPanel.kt index 8cd19dfb3..3ec7bc0db 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/ButtonPanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/ButtonPanel.kt @@ -51,8 +51,11 @@ open class ButtonPanel( return true } + if (!tryToGrabMouseInput()) { + return true + } + pressed = true - trapMouseInput = true playGuiClickSound() return true } @@ -62,7 +65,7 @@ open class ButtonPanel( return true } - trapMouseInput = false + grabMouseInput = false pressed = false if (isHovered) { @@ -83,7 +86,7 @@ open class ButtonPanel( if (field != value) { if (!value) { pressed = false - trapMouseInput = false + grabMouseInput = false } field = value @@ -142,7 +145,7 @@ abstract class RectangleButtonPanel( if (field != value) { if (!value) { pressed = false - trapMouseInput = false + grabMouseInput = false } field = value @@ -167,8 +170,9 @@ abstract class RectangleButtonPanel( playGuiClickSound() } - pressed = true - trapMouseInput = true + if (tryToGrabMouseInput()) { + pressed = true + } } return true @@ -183,7 +187,7 @@ abstract class RectangleButtonPanel( } } - trapMouseInput = false + grabMouseInput = false return true } @@ -319,7 +323,7 @@ abstract class EnumRectangleButtonPanel>( } override fun innerRenderTooltips(stack: PoseStack, mouseX: Float, mouseY: Float, partialTick: Float): Boolean { - if (!isHovered && !isTrappingMouseInput()) { + if (!isHovered && !isGrabbingMouseInput()) { return super.innerRenderTooltips(stack, mouseX, mouseY, partialTick) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/DiscreteScrollBarPanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/DiscreteScrollBarPanel.kt index 22eef39fa..3ee5a7e42 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/DiscreteScrollBarPanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/DiscreteScrollBarPanel.kt @@ -3,7 +3,6 @@ package ru.dbotthepony.mc.otm.client.screen.panels import com.mojang.blaze3d.platform.InputConstants import com.mojang.blaze3d.vertex.PoseStack import net.minecraft.client.gui.screens.Screen -import ru.dbotthepony.mc.otm.client.screen.MatteryScreen import kotlin.math.roundToInt open class DiscreteScrollBarPanel @JvmOverloads constructor( @@ -39,9 +38,8 @@ open class DiscreteScrollBarPanel @JvmOverloads constructor( return true } - if (button == InputConstants.MOUSE_BUTTON_LEFT) { + if (button == InputConstants.MOUSE_BUTTON_LEFT && tryToGrabMouseInput()) { isScrolling = true - trapMouseInput = true rememberScroll = scroll rememberY = y } @@ -52,7 +50,7 @@ open class DiscreteScrollBarPanel @JvmOverloads constructor( override fun mouseReleasedInner(x: Double, y: Double, button: Int): Boolean { if (isScrolling && button == InputConstants.MOUSE_BUTTON_LEFT) { isScrolling = false - trapMouseInput = false + grabMouseInput = false return true } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/DraggableCanvasPanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/DraggableCanvasPanel.kt index b19873851..ca49b34a5 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/DraggableCanvasPanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/DraggableCanvasPanel.kt @@ -1,7 +1,6 @@ package ru.dbotthepony.mc.otm.client.screen.panels import net.minecraft.client.gui.screens.Screen -import ru.dbotthepony.mc.otm.client.screen.MatteryScreen open class DraggableCanvasPanel @JvmOverloads constructor( screen: S, @@ -15,13 +14,17 @@ open class DraggableCanvasPanel @JvmOverloads constructor( override fun mouseClickedInner(x: Double, y: Double, button: Int): Boolean { dragging = true - trapMouseInput = true + + if (tryToGrabMouseInput()) { + grabMouseInput = true + } + return true } override fun mouseReleasedInner(x: Double, y: Double, button: Int): Boolean { dragging = false - trapMouseInput = false + grabMouseInput = false return true } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt index 6a5c03dbe..269e01ccd 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt @@ -244,7 +244,25 @@ open class EditablePanel @JvmOverloads constructor( var acceptMouseInput = true var acceptKeyboardInput = true - var trapMouseInput = false + var grabMouseInput = false + + fun tryToGrabMouseInput(): Boolean { + if (grabMouseInput) { + return true + } + + if (screen !is MatteryScreen<*>) { + grabMouseInput = true + return true + } + + if (screen.isMouseGrabbed()) { + return false + } + + grabMouseInput = true + return true + } var tooltip: Component? = null set(value) { @@ -541,7 +559,7 @@ open class EditablePanel @JvmOverloads constructor( mouseY <= absoluteY + height) to null } - if (trapMouseInput && this is ISlotPanel<*>) { + if (grabMouseInput && this is ISlotPanel<*>) { return true to this.slot } @@ -581,7 +599,7 @@ open class EditablePanel @JvmOverloads constructor( mouseY <= absoluteY + height) to ItemStack.EMPTY } - if (trapMouseInput && this is IItemStackPanel) { + if (grabMouseInput && this is IItemStackPanel) { return true to this.itemStack } @@ -1016,13 +1034,13 @@ open class EditablePanel @JvmOverloads constructor( return children[index] } - fun isTrappingMouseInput(): Boolean { - if (trapMouseInput) { + fun isGrabbingMouseInput(): Boolean { + if (grabMouseInput) { return true } for (child in children) { - if (child.isTrappingMouseInput()) { + if (child.isGrabbingMouseInput()) { return true } } @@ -1072,7 +1090,14 @@ open class EditablePanel @JvmOverloads constructor( final override fun mouseClicked(x: Double, y: Double, button: Int): Boolean { if (!isVisible() || !acceptMouseInput) return false - if (trapMouseInput) return mouseClickedInner(x, y, button) + if (grabMouseInput) return mouseClickedInner(x, y, button) + + for (child in children) { + if (child.isGrabbingMouseInput() && child.mouseClickedChecked(x, y, button)) { + killFocusForEverythingExcept(child) + return true + } + } for (child in children) { if (child.mouseClickedChecked(x, y, button)) { @@ -1087,7 +1112,7 @@ open class EditablePanel @JvmOverloads constructor( fun mouseClickedChecked(x: Double, y: Double, button: Int): Boolean { if (!isVisible() || !acceptMouseInput) return false - if (isTrappingMouseInput() || withinBounds(x, y)) { + if (isGrabbingMouseInput() || withinBounds(x, y)) { if (acceptMouseInput && parent == null) popup() return mouseClicked(x, y, button) } else if (withinExtendedBounds(x, y)) { @@ -1113,7 +1138,13 @@ open class EditablePanel @JvmOverloads constructor( final override fun mouseReleased(x: Double, y: Double, button: Int): Boolean { if (!isVisible() || !acceptMouseInput) return false - if (trapMouseInput) return mouseReleasedInner(x, y, button) + if (grabMouseInput) return mouseReleasedInner(x, y, button) + + for (child in children) { + if (child.isGrabbingMouseInput() && child.mouseReleasedChecked(x, y, button)) { + return true + } + } for (child in children) { if (child.mouseReleasedChecked(x, y, button)) { @@ -1127,7 +1158,7 @@ open class EditablePanel @JvmOverloads constructor( fun mouseReleasedChecked(x: Double, y: Double, button: Int): Boolean { if (!isVisible() || !acceptMouseInput) return false - if (isTrappingMouseInput() || withinBounds(x, y)) { + if (isGrabbingMouseInput() || withinBounds(x, y)) { return mouseReleased(x, y, button) } else if (withinExtendedBounds(x, y)) { for (child in children) { @@ -1147,7 +1178,13 @@ open class EditablePanel @JvmOverloads constructor( final override fun mouseDragged(x: Double, y: Double, button: Int, xDelta: Double, yDelta: Double): Boolean { if (!isVisible() || !acceptMouseInput) return false - if (trapMouseInput) return mouseDraggedInner(x, y, button, xDelta, yDelta) + if (grabMouseInput) return mouseDraggedInner(x, y, button, xDelta, yDelta) + + for (child in children) { + if (child.isGrabbingMouseInput() && child.mouseDraggedChecked(x, y, button, xDelta, yDelta)) { + return true + } + } for (child in children) { if (child.mouseDraggedChecked(x, y, button, xDelta, yDelta)) { @@ -1161,7 +1198,7 @@ open class EditablePanel @JvmOverloads constructor( fun mouseDraggedChecked(x: Double, y: Double, button: Int, xDelta: Double, yDelta: Double): Boolean { if (!isVisible() || !acceptMouseInput) return false - if (isTrappingMouseInput() || withinBounds(x, y)) { + if (isGrabbingMouseInput() || withinBounds(x, y)) { return mouseDragged(x, y, button, xDelta, yDelta) } else if (withinExtendedBounds(x, y)) { for (child in children) { @@ -1182,7 +1219,13 @@ open class EditablePanel @JvmOverloads constructor( final override fun mouseScrolled(x: Double, y: Double, scroll: Double): Boolean { if (!isVisible() || !acceptMouseInput) return false - if (trapMouseInput) return mouseScrolledInner(x, y, scroll) + if (grabMouseInput) return mouseScrolledInner(x, y, scroll) + + for (child in children) { + if (child.isGrabbingMouseInput() && child.mouseScrolledChecked(x, y, scroll)) { + return true + } + } for (child in children) { if (child.mouseScrolledChecked(x, y, scroll)) { @@ -1196,7 +1239,7 @@ open class EditablePanel @JvmOverloads constructor( fun mouseScrolledChecked(x: Double, y: Double, scroll: Double): Boolean { if (!isVisible() || !acceptMouseInput) return false - if (isTrappingMouseInput() || withinBounds(x, y)) { + if (isGrabbingMouseInput() || withinBounds(x, y)) { return mouseScrolled(x, y, scroll) } else if (withinExtendedBounds(x, y)) { for (child in children) { @@ -1272,7 +1315,7 @@ open class EditablePanel @JvmOverloads constructor( final override fun isMouseOver(x: Double, y: Double): Boolean { // called to check whenever we are hovering at this if (!isVisible() || !acceptMouseInput) return false - if (isTrappingMouseInput()) return true + if (isGrabbingMouseInput()) return true val pos = localToScreen() return x >= pos.x && x <= pos.x + width && y >= pos.y && y + height <= pos.y diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/FramePanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/FramePanel.kt index 48e9e733d..22b8d59a6 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/FramePanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/FramePanel.kt @@ -140,8 +140,7 @@ open class FramePanel( override fun mouseClickedInner(x: Double, y: Double, button: Int): Boolean { val (_, y1) = screenToLocal(x, y) - if (parent == null && y1 >= 0 && y1 <= 12) { - trapMouseInput = true + if (parent == null && y1 >= 0 && y1 <= 12 && tryToGrabMouseInput()) { dragging = true return true } @@ -151,7 +150,7 @@ open class FramePanel( override fun mouseReleasedInner(x: Double, y: Double, button: Int): Boolean { if (dragging) { - trapMouseInput = false + grabMouseInput = false dragging = false return true }