Fix grabbing mouse input wasn't true grab

This commit is contained in:
DBotThePony 2022-09-16 21:51:58 +07:00
parent 0432fa0ed9
commit f3cb4772aa
Signed by: DBot
GPG Key ID: DCC23B5715498507
6 changed files with 90 additions and 33 deletions

View File

@ -434,6 +434,16 @@ abstract class MatteryScreen<T : MatteryMenu>(menu: T, inventory: Inventory, tit
return null 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) { override fun render(poseStack: PoseStack, mouseX: Int, mouseY: Int, partialTick: Float) {
val mouseXf = mouseX.toFloat() val mouseXf = mouseX.toFloat()
val mouseYf = mouseY.toFloat() val mouseYf = mouseY.toFloat()

View File

@ -51,8 +51,11 @@ open class ButtonPanel<out S : Screen>(
return true return true
} }
if (!tryToGrabMouseInput()) {
return true
}
pressed = true pressed = true
trapMouseInput = true
playGuiClickSound() playGuiClickSound()
return true return true
} }
@ -62,7 +65,7 @@ open class ButtonPanel<out S : Screen>(
return true return true
} }
trapMouseInput = false grabMouseInput = false
pressed = false pressed = false
if (isHovered) { if (isHovered) {
@ -83,7 +86,7 @@ open class ButtonPanel<out S : Screen>(
if (field != value) { if (field != value) {
if (!value) { if (!value) {
pressed = false pressed = false
trapMouseInput = false grabMouseInput = false
} }
field = value field = value
@ -142,7 +145,7 @@ abstract class RectangleButtonPanel<out S : Screen>(
if (field != value) { if (field != value) {
if (!value) { if (!value) {
pressed = false pressed = false
trapMouseInput = false grabMouseInput = false
} }
field = value field = value
@ -167,8 +170,9 @@ abstract class RectangleButtonPanel<out S : Screen>(
playGuiClickSound() playGuiClickSound()
} }
if (tryToGrabMouseInput()) {
pressed = true pressed = true
trapMouseInput = true }
} }
return true return true
@ -183,7 +187,7 @@ abstract class RectangleButtonPanel<out S : Screen>(
} }
} }
trapMouseInput = false grabMouseInput = false
return true return true
} }
@ -319,7 +323,7 @@ abstract class EnumRectangleButtonPanel<out S : Screen, T : Enum<T>>(
} }
override fun innerRenderTooltips(stack: PoseStack, mouseX: Float, mouseY: Float, partialTick: Float): Boolean { 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) return super.innerRenderTooltips(stack, mouseX, mouseY, partialTick)
} }

View File

@ -3,7 +3,6 @@ package ru.dbotthepony.mc.otm.client.screen.panels
import com.mojang.blaze3d.platform.InputConstants import com.mojang.blaze3d.platform.InputConstants
import com.mojang.blaze3d.vertex.PoseStack import com.mojang.blaze3d.vertex.PoseStack
import net.minecraft.client.gui.screens.Screen import net.minecraft.client.gui.screens.Screen
import ru.dbotthepony.mc.otm.client.screen.MatteryScreen
import kotlin.math.roundToInt import kotlin.math.roundToInt
open class DiscreteScrollBarPanel<S : Screen> @JvmOverloads constructor( open class DiscreteScrollBarPanel<S : Screen> @JvmOverloads constructor(
@ -39,9 +38,8 @@ open class DiscreteScrollBarPanel<S : Screen> @JvmOverloads constructor(
return true return true
} }
if (button == InputConstants.MOUSE_BUTTON_LEFT) { if (button == InputConstants.MOUSE_BUTTON_LEFT && tryToGrabMouseInput()) {
isScrolling = true isScrolling = true
trapMouseInput = true
rememberScroll = scroll rememberScroll = scroll
rememberY = y rememberY = y
} }
@ -52,7 +50,7 @@ open class DiscreteScrollBarPanel<S : Screen> @JvmOverloads constructor(
override fun mouseReleasedInner(x: Double, y: Double, button: Int): Boolean { override fun mouseReleasedInner(x: Double, y: Double, button: Int): Boolean {
if (isScrolling && button == InputConstants.MOUSE_BUTTON_LEFT) { if (isScrolling && button == InputConstants.MOUSE_BUTTON_LEFT) {
isScrolling = false isScrolling = false
trapMouseInput = false grabMouseInput = false
return true return true
} }

View File

@ -1,7 +1,6 @@
package ru.dbotthepony.mc.otm.client.screen.panels package ru.dbotthepony.mc.otm.client.screen.panels
import net.minecraft.client.gui.screens.Screen import net.minecraft.client.gui.screens.Screen
import ru.dbotthepony.mc.otm.client.screen.MatteryScreen
open class DraggableCanvasPanel<out S : Screen> @JvmOverloads constructor( open class DraggableCanvasPanel<out S : Screen> @JvmOverloads constructor(
screen: S, screen: S,
@ -15,13 +14,17 @@ open class DraggableCanvasPanel<out S : Screen> @JvmOverloads constructor(
override fun mouseClickedInner(x: Double, y: Double, button: Int): Boolean { override fun mouseClickedInner(x: Double, y: Double, button: Int): Boolean {
dragging = true dragging = true
trapMouseInput = true
if (tryToGrabMouseInput()) {
grabMouseInput = true
}
return true return true
} }
override fun mouseReleasedInner(x: Double, y: Double, button: Int): Boolean { override fun mouseReleasedInner(x: Double, y: Double, button: Int): Boolean {
dragging = false dragging = false
trapMouseInput = false grabMouseInput = false
return true return true
} }

View File

@ -244,7 +244,25 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
var acceptMouseInput = true var acceptMouseInput = true
var acceptKeyboardInput = 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 var tooltip: Component? = null
set(value) { set(value) {
@ -541,7 +559,7 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
mouseY <= absoluteY + height) to null mouseY <= absoluteY + height) to null
} }
if (trapMouseInput && this is ISlotPanel<*>) { if (grabMouseInput && this is ISlotPanel<*>) {
return true to this.slot return true to this.slot
} }
@ -581,7 +599,7 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
mouseY <= absoluteY + height) to ItemStack.EMPTY mouseY <= absoluteY + height) to ItemStack.EMPTY
} }
if (trapMouseInput && this is IItemStackPanel) { if (grabMouseInput && this is IItemStackPanel) {
return true to this.itemStack return true to this.itemStack
} }
@ -1016,13 +1034,13 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
return children[index] return children[index]
} }
fun isTrappingMouseInput(): Boolean { fun isGrabbingMouseInput(): Boolean {
if (trapMouseInput) { if (grabMouseInput) {
return true return true
} }
for (child in children) { for (child in children) {
if (child.isTrappingMouseInput()) { if (child.isGrabbingMouseInput()) {
return true return true
} }
} }
@ -1072,7 +1090,14 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
final override fun mouseClicked(x: Double, y: Double, button: Int): Boolean { final override fun mouseClicked(x: Double, y: Double, button: Int): Boolean {
if (!isVisible() || !acceptMouseInput) return false 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) { for (child in children) {
if (child.mouseClickedChecked(x, y, button)) { if (child.mouseClickedChecked(x, y, button)) {
@ -1087,7 +1112,7 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
fun mouseClickedChecked(x: Double, y: Double, button: Int): Boolean { fun mouseClickedChecked(x: Double, y: Double, button: Int): Boolean {
if (!isVisible() || !acceptMouseInput) return false if (!isVisible() || !acceptMouseInput) return false
if (isTrappingMouseInput() || withinBounds(x, y)) { if (isGrabbingMouseInput() || withinBounds(x, y)) {
if (acceptMouseInput && parent == null) popup() if (acceptMouseInput && parent == null) popup()
return mouseClicked(x, y, button) return mouseClicked(x, y, button)
} else if (withinExtendedBounds(x, y)) { } else if (withinExtendedBounds(x, y)) {
@ -1113,7 +1138,13 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
final override fun mouseReleased(x: Double, y: Double, button: Int): Boolean { final override fun mouseReleased(x: Double, y: Double, button: Int): Boolean {
if (!isVisible() || !acceptMouseInput) return false 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) { for (child in children) {
if (child.mouseReleasedChecked(x, y, button)) { if (child.mouseReleasedChecked(x, y, button)) {
@ -1127,7 +1158,7 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
fun mouseReleasedChecked(x: Double, y: Double, button: Int): Boolean { fun mouseReleasedChecked(x: Double, y: Double, button: Int): Boolean {
if (!isVisible() || !acceptMouseInput) return false if (!isVisible() || !acceptMouseInput) return false
if (isTrappingMouseInput() || withinBounds(x, y)) { if (isGrabbingMouseInput() || withinBounds(x, y)) {
return mouseReleased(x, y, button) return mouseReleased(x, y, button)
} else if (withinExtendedBounds(x, y)) { } else if (withinExtendedBounds(x, y)) {
for (child in children) { for (child in children) {
@ -1147,7 +1178,13 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
final override fun mouseDragged(x: Double, y: Double, button: Int, xDelta: Double, yDelta: Double): Boolean { final override fun mouseDragged(x: Double, y: Double, button: Int, xDelta: Double, yDelta: Double): Boolean {
if (!isVisible() || !acceptMouseInput) return false 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) { for (child in children) {
if (child.mouseDraggedChecked(x, y, button, xDelta, yDelta)) { if (child.mouseDraggedChecked(x, y, button, xDelta, yDelta)) {
@ -1161,7 +1198,7 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
fun mouseDraggedChecked(x: Double, y: Double, button: Int, xDelta: Double, yDelta: Double): Boolean { fun mouseDraggedChecked(x: Double, y: Double, button: Int, xDelta: Double, yDelta: Double): Boolean {
if (!isVisible() || !acceptMouseInput) return false if (!isVisible() || !acceptMouseInput) return false
if (isTrappingMouseInput() || withinBounds(x, y)) { if (isGrabbingMouseInput() || withinBounds(x, y)) {
return mouseDragged(x, y, button, xDelta, yDelta) return mouseDragged(x, y, button, xDelta, yDelta)
} else if (withinExtendedBounds(x, y)) { } else if (withinExtendedBounds(x, y)) {
for (child in children) { for (child in children) {
@ -1182,7 +1219,13 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
final override fun mouseScrolled(x: Double, y: Double, scroll: Double): Boolean { final override fun mouseScrolled(x: Double, y: Double, scroll: Double): Boolean {
if (!isVisible() || !acceptMouseInput) return false 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) { for (child in children) {
if (child.mouseScrolledChecked(x, y, scroll)) { if (child.mouseScrolledChecked(x, y, scroll)) {
@ -1196,7 +1239,7 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
fun mouseScrolledChecked(x: Double, y: Double, scroll: Double): Boolean { fun mouseScrolledChecked(x: Double, y: Double, scroll: Double): Boolean {
if (!isVisible() || !acceptMouseInput) return false if (!isVisible() || !acceptMouseInput) return false
if (isTrappingMouseInput() || withinBounds(x, y)) { if (isGrabbingMouseInput() || withinBounds(x, y)) {
return mouseScrolled(x, y, scroll) return mouseScrolled(x, y, scroll)
} else if (withinExtendedBounds(x, y)) { } else if (withinExtendedBounds(x, y)) {
for (child in children) { for (child in children) {
@ -1272,7 +1315,7 @@ open class EditablePanel<out S : Screen> @JvmOverloads constructor(
final override fun isMouseOver(x: Double, y: Double): Boolean { // called to check whenever we are hovering at this final override fun isMouseOver(x: Double, y: Double): Boolean { // called to check whenever we are hovering at this
if (!isVisible() || !acceptMouseInput) return false if (!isVisible() || !acceptMouseInput) return false
if (isTrappingMouseInput()) return true if (isGrabbingMouseInput()) return true
val pos = localToScreen() val pos = localToScreen()
return x >= pos.x && x <= pos.x + width && y >= pos.y && y + height <= pos.y return x >= pos.x && x <= pos.x + width && y >= pos.y && y + height <= pos.y

View File

@ -140,8 +140,7 @@ open class FramePanel<out S : Screen>(
override fun mouseClickedInner(x: Double, y: Double, button: Int): Boolean { override fun mouseClickedInner(x: Double, y: Double, button: Int): Boolean {
val (_, y1) = screenToLocal(x, y) val (_, y1) = screenToLocal(x, y)
if (parent == null && y1 >= 0 && y1 <= 12) { if (parent == null && y1 >= 0 && y1 <= 12 && tryToGrabMouseInput()) {
trapMouseInput = true
dragging = true dragging = true
return true return true
} }
@ -151,7 +150,7 @@ open class FramePanel<out S : Screen>(
override fun mouseReleasedInner(x: Double, y: Double, button: Int): Boolean { override fun mouseReleasedInner(x: Double, y: Double, button: Int): Boolean {
if (dragging) { if (dragging) {
trapMouseInput = false grabMouseInput = false
dragging = false dragging = false
return true return true
} }