From c0c0b016881c53d80a3b2bc781d439696d8a4f18 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 7 Sep 2022 07:53:55 +0700 Subject: [PATCH] findSlot now actually searches for slot panel --- build.gradle.kts | 2 +- .../mc/otm/client/screen/MatteryScreen.kt | 17 +++++- .../otm/client/screen/panels/EditablePanel.kt | 61 ++++++++++++++++--- .../mc/otm/client/screen/panels/SlotPanel.kt | 4 +- 4 files changed, 70 insertions(+), 14 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index ebda06036..57cf63cc8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -169,7 +169,7 @@ dependencies { // implementation fg.deobf("com.tterrag.registrate:Registrate:MC${mc_version}-${registrate_version}") // Adds registrate as a dependency // Examples using mod jars from ./libs - // implementation fg.deobf("blank:EnderRift-1.18.1:2.4.1") + // implementation(fg.deobf("blank:MouseTweaks-forge-mc1.19:2.23")) // compile against the JEI API but do not include it at runtime //compileOnly fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}:api") 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 e70e3d2f9..a92e1675a 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 @@ -346,7 +346,22 @@ abstract class MatteryScreen(menu: T, inventory: Inventory, tit var returnSlot: Slot? = null override fun findSlot(x: Double, y: Double): Slot? { - return returnSlot + if (returnSlot != null) { + return returnSlot + } + + val xFloat = x.toFloat() + val yFloat = y.toFloat() + + for (panel in panels) { + val (status, slot) = panel.findSlot(xFloat, yFloat) + + if (status) { + return slot + } + } + + return null } override fun render(poseStack: PoseStack, mouseX: Int, mouseY: Int, partialTick: Float) { 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 88c229296..326735829 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 @@ -7,6 +7,7 @@ import net.minecraft.client.Minecraft import net.minecraft.client.gui.Font import net.minecraft.client.gui.components.events.GuiEventListener import net.minecraft.network.chat.Component +import net.minecraft.world.inventory.Slot import org.apache.logging.log4j.LogManager import ru.dbotthepony.mc.otm.client.render.popScissorRect import ru.dbotthepony.mc.otm.client.render.pushScissorRect @@ -35,6 +36,10 @@ enum class DockResizeMode(val changeWidth: Boolean, val changeHeight: Boolean) { ALL(true, true), NONE(false, false), WIDTH(true, false), HEIGHT(false, true) } +interface ISlotPanel { + val slot: S +} + open class EditablePanel @JvmOverloads constructor( val screen: MatteryScreen<*>, parent: EditablePanel?, @@ -433,33 +438,69 @@ open class EditablePanel @JvmOverloads constructor( return depth } - open fun tickHover(mouse_x: Float, mouse_y: Float): Boolean { + open fun tickHover(mouseX: Float, mouseY: Float): Boolean { if ((boundingHeight > height || boundingWidth > width || boundingX < 0 || boundingY < 0) && parent == null) { var hit = false for (child in children) { - if (child.tickHover(mouse_x, mouse_y)) { + if (child.tickHover(mouseX, mouseY)) { hit = true } } - isHovered = mouse_x >= absoluteX && - mouse_x <= absoluteX + width && - mouse_y >= absoluteY && - mouse_y <= absoluteY + height + isHovered = mouseX >= absoluteX && + mouseX <= absoluteX + width && + mouseY >= absoluteY && + mouseY <= absoluteY + height return hit } isHovered = - mouse_x >= absoluteX && - mouse_x <= absoluteX + width && - mouse_y >= absoluteY && - mouse_y <= absoluteY + height + mouseX >= absoluteX && + mouseX <= absoluteX + width && + mouseY >= absoluteY && + mouseY <= absoluteY + height return isHovered } + fun findSlot(mouseX: Float, mouseY: Float): Pair { + if (!acceptMouseInput) { + return (mouseX >= absoluteX && + mouseX <= absoluteX + width && + mouseY >= absoluteY && + mouseY <= absoluteY + height) to null + } + + if (trapMouseInput && this is ISlotPanel<*>) { + return true to this.slot + } + + for (child in children) { + val (status, slot) = child.findSlot(mouseX, mouseY) + + if (status) { + return true to slot + } + } + + if ( + mouseX >= absoluteX && + mouseX <= absoluteX + width && + mouseY >= absoluteY && + mouseY <= absoluteY + height + ) { + if (this is ISlotPanel<*>) { + return true to this.slot + } + + return true to null + } + + return false to null + } + fun renderTooltips(stack: PoseStack, mouse_x: Float, mouse_y: Float, flag: Float): Boolean { if (!isVisible()) { return false diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/SlotPanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/SlotPanel.kt index 6b8c277d1..68d6810d9 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/SlotPanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/SlotPanel.kt @@ -21,13 +21,13 @@ import javax.annotation.Nonnull open class SlotPanel( screen: MatteryScreen<*>, parent: EditablePanel?, - val slot: T, + final override val slot: T, x: Float = 0f, y: Float = 0f, width: Float = AbstractSlotPanel.SIZE, height: Float = AbstractSlotPanel.SIZE, noItemIcon: SkinElement? = null -) : AbstractSlotPanel(screen, parent, x, y, width, height, noItemIcon) { +) : AbstractSlotPanel(screen, parent, x, y, width, height, noItemIcon), ISlotPanel { override fun mouseClickedInner(x: Double, y: Double, button: Int): Boolean { screen.returnSlot = slot return true