From e580023cce085c84361a00a5b6115fceac325259 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sun, 12 Nov 2023 05:30:36 +0100 Subject: Actually render inventory buttons --- .../nea/firmament/mixins/MixinHandledScreen.java | 11 ++- .../firmament/events/HandledScreenClickEvent.kt | 14 ++++ .../events/HandledScreenForegroundEvent.kt | 6 +- .../moe/nea/firmament/events/ScreenClickEvent.kt | 14 ---- .../moe/nea/firmament/features/FeatureManager.kt | 2 + .../inventory/buttons/FragmentGuiScreen.kt | 88 --------------------- .../features/inventory/buttons/InventoryButton.kt | 45 +++++++++++ .../inventory/buttons/InventoryButtonEditor.kt | 41 ++++------ .../features/inventory/buttons/InventoryButtons.kt | 61 +++++++++++++++ .../inventory/buttons/MoulConfigFragment.kt | 43 ----------- .../moe/nea/firmament/rei/FirmamentReiPlugin.kt | 1 - .../moe/nea/firmament/util/FragmentGuiScreen.kt | 89 ++++++++++++++++++++++ .../moe/nea/firmament/util/MoulConfigFragment.kt | 43 +++++++++++ .../firmament/gui/button_editor_fragment.xml | 2 +- 14 files changed, 285 insertions(+), 175 deletions(-) create mode 100644 src/main/kotlin/moe/nea/firmament/events/HandledScreenClickEvent.kt delete mode 100644 src/main/kotlin/moe/nea/firmament/events/ScreenClickEvent.kt delete mode 100644 src/main/kotlin/moe/nea/firmament/features/inventory/buttons/FragmentGuiScreen.kt create mode 100644 src/main/kotlin/moe/nea/firmament/features/inventory/buttons/InventoryButton.kt create mode 100644 src/main/kotlin/moe/nea/firmament/features/inventory/buttons/InventoryButtons.kt delete mode 100644 src/main/kotlin/moe/nea/firmament/features/inventory/buttons/MoulConfigFragment.kt create mode 100644 src/main/kotlin/moe/nea/firmament/util/FragmentGuiScreen.kt create mode 100644 src/main/kotlin/moe/nea/firmament/util/MoulConfigFragment.kt diff --git a/src/main/java/moe/nea/firmament/mixins/MixinHandledScreen.java b/src/main/java/moe/nea/firmament/mixins/MixinHandledScreen.java index 3972ffc..35856cb 100644 --- a/src/main/java/moe/nea/firmament/mixins/MixinHandledScreen.java +++ b/src/main/java/moe/nea/firmament/mixins/MixinHandledScreen.java @@ -35,6 +35,10 @@ public abstract class MixinHandledScreen { @Shadow public abstract T getScreenHandler(); + @Shadow + protected int y; + @Shadow + protected int x; @Unique PlayerInventory playerInventory; @@ -52,14 +56,17 @@ public abstract class MixinHandledScreen { @Inject(method = "mouseClicked", at = @At("HEAD"), cancellable = true) public void onMouseClicked(double mouseX, double mouseY, int button, CallbackInfoReturnable cir) { - if (ScreenClickEvent.Companion.publish(new ScreenClickEvent((HandledScreen) (Object) this, mouseX, mouseY, button)).getCancelled()) { + if (HandledScreenClickEvent.Companion.publish(new HandledScreenClickEvent((HandledScreen) (Object) this, mouseX, mouseY, button)).getCancelled()) { cir.setReturnValue(true); } } @Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;drawForeground(Lnet/minecraft/client/gui/DrawContext;II)V", shift = At.Shift.AFTER)) public void onAfterRenderForeground(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) { - HandledScreenForegroundEvent.Companion.publish(new HandledScreenForegroundEvent((HandledScreen) (Object) this, mouseX, mouseY, delta)); + context.getMatrices().push(); + context.getMatrices().translate(-x, -y, 0); + HandledScreenForegroundEvent.Companion.publish(new HandledScreenForegroundEvent((HandledScreen) (Object) this, context, mouseX, mouseY, delta)); + context.getMatrices().pop(); } @Inject(method = "onMouseClick(Lnet/minecraft/screen/slot/Slot;IILnet/minecraft/screen/slot/SlotActionType;)V", at = @At("HEAD"), cancellable = true) diff --git a/src/main/kotlin/moe/nea/firmament/events/HandledScreenClickEvent.kt b/src/main/kotlin/moe/nea/firmament/events/HandledScreenClickEvent.kt new file mode 100644 index 0000000..0c4d4f6 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/events/HandledScreenClickEvent.kt @@ -0,0 +1,14 @@ +/* + * SPDX-FileCopyrightText: 2023 Linnea Gräf + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.events + +import net.minecraft.client.gui.screen.ingame.HandledScreen + +data class HandledScreenClickEvent(val screen: HandledScreen<*>, val mouseX: Double, val mouseY: Double, val button: Int) : + FirmamentEvent.Cancellable() { + companion object : FirmamentEventBus() +} diff --git a/src/main/kotlin/moe/nea/firmament/events/HandledScreenForegroundEvent.kt b/src/main/kotlin/moe/nea/firmament/events/HandledScreenForegroundEvent.kt index d45b484..95f9b70 100644 --- a/src/main/kotlin/moe/nea/firmament/events/HandledScreenForegroundEvent.kt +++ b/src/main/kotlin/moe/nea/firmament/events/HandledScreenForegroundEvent.kt @@ -6,11 +6,15 @@ package moe.nea.firmament.events +import net.minecraft.client.gui.DrawContext import net.minecraft.client.gui.screen.ingame.HandledScreen data class HandledScreenForegroundEvent( val screen: HandledScreen<*>, - val mouseX: Int, val mouseY: Int, val delta: Float + val context: DrawContext, + val mouseX: Int, + val mouseY: Int, + val delta: Float ) : FirmamentEvent() { companion object : FirmamentEventBus() } diff --git a/src/main/kotlin/moe/nea/firmament/events/ScreenClickEvent.kt b/src/main/kotlin/moe/nea/firmament/events/ScreenClickEvent.kt deleted file mode 100644 index 0977971..0000000 --- a/src/main/kotlin/moe/nea/firmament/events/ScreenClickEvent.kt +++ /dev/null @@ -1,14 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2023 Linnea Gräf - * - * SPDX-License-Identifier: GPL-3.0-or-later - */ - -package moe.nea.firmament.events - -import net.minecraft.client.gui.screen.ingame.HandledScreen - -data class ScreenClickEvent(val screen: HandledScreen<*>, val mouseX: Double, val mouseY: Double, val button: Int) : - FirmamentEvent.Cancellable() { - companion object : FirmamentEventBus() -} diff --git a/src/main/kotlin/moe/nea/firmament/features/FeatureManager.kt b/src/main/kotlin/moe/nea/firmament/features/FeatureManager.kt index 2683775..4888f54 100644 --- a/src/main/kotlin/moe/nea/firmament/features/FeatureManager.kt +++ b/src/main/kotlin/moe/nea/firmament/features/FeatureManager.kt @@ -23,6 +23,7 @@ import moe.nea.firmament.features.inventory.ItemRarityCosmetics import moe.nea.firmament.features.inventory.PriceData import moe.nea.firmament.features.inventory.SaveCursorPosition import moe.nea.firmament.features.inventory.SlotLocking +import moe.nea.firmament.features.inventory.buttons.InventoryButtons import moe.nea.firmament.features.inventory.storageoverlay.StorageOverlay import moe.nea.firmament.features.texturepack.CustomSkyBlockTextures import moe.nea.firmament.features.world.FairySouls @@ -56,6 +57,7 @@ object FeatureManager : DataHolder(serializer(), "feature loadFeature(CraftingOverlay) loadFeature(PowerUserTools) loadFeature(ChatLinks) + loadFeature(InventoryButtons) loadFeature(CompatibliltyFeatures) loadFeature(QuickCommands) loadFeature(SaveCursorPosition) diff --git a/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/FragmentGuiScreen.kt b/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/FragmentGuiScreen.kt deleted file mode 100644 index 4f38f99..0000000 --- a/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/FragmentGuiScreen.kt +++ /dev/null @@ -1,88 +0,0 @@ -package moe.nea.firmament.features.inventory.buttons - -import io.github.moulberry.moulconfig.gui.GuiContext -import me.shedaniel.math.Dimension -import me.shedaniel.math.Point -import me.shedaniel.math.Rectangle -import net.minecraft.client.gui.DrawContext -import net.minecraft.client.gui.screen.Screen -import net.minecraft.text.Text - -abstract class FragmentGuiScreen( - val dismissOnOutOfBounds: Boolean = true -) : Screen(Text.literal("")) { - var popup: MoulConfigFragment? = null - - fun createPopup(context: GuiContext, position: Point) { - popup = MoulConfigFragment(context, position) { popup = null } - } - - override fun render(context: DrawContext, mouseX: Int, mouseY: Int, delta: Float) { - super.render(context, mouseX, mouseY, delta) - popup?.render(context, mouseX, mouseY, delta) - } - - private inline fun ifPopup(ifYes: (MoulConfigFragment) -> Unit): Boolean { - val p = popup ?: return false - ifYes(p) - return true - } - - override fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean { - return ifPopup { - it.keyPressed(keyCode, scanCode, modifiers) - } - } - - override fun keyReleased(keyCode: Int, scanCode: Int, modifiers: Int): Boolean { - return ifPopup { - it.keyReleased(keyCode, scanCode, modifiers) - } - } - - override fun mouseMoved(mouseX: Double, mouseY: Double) { - ifPopup { it.mouseMoved(mouseX, mouseY) } - } - - override fun mouseReleased(mouseX: Double, mouseY: Double, button: Int): Boolean { - return ifPopup { - it.mouseReleased(mouseX, mouseY, button) - } - } - - override fun mouseDragged(mouseX: Double, mouseY: Double, button: Int, deltaX: Double, deltaY: Double): Boolean { - return ifPopup { - it.mouseDragged(mouseX, mouseY, button, deltaX, deltaY) - } - } - - override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean { - return ifPopup { - if (!Rectangle( - it.position, - Dimension(it.context.root.width, it.context.root.height) - ).contains(Point(mouseX, mouseY)) - && dismissOnOutOfBounds - ) { - popup = null - } else { - it.mouseClicked(mouseX, mouseY, button) - } - } - } - - override fun charTyped(chr: Char, modifiers: Int): Boolean { - return ifPopup { it.charTyped(chr, modifiers) } - } - - override fun mouseScrolled( - mouseX: Double, - mouseY: Double, - horizontalAmount: Double, - verticalAmount: Double - ): Boolean { - return ifPopup { - it.mouseScrolled(mouseX, mouseY, verticalAmount) - } - } -} diff --git a/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/InventoryButton.kt b/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/InventoryButton.kt new file mode 100644 index 0000000..e2cc00e --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/InventoryButton.kt @@ -0,0 +1,45 @@ +package moe.nea.firmament.features.inventory.buttons + +import me.shedaniel.math.Dimension +import me.shedaniel.math.Point +import me.shedaniel.math.Rectangle +import kotlinx.serialization.Serializable +import net.minecraft.item.ItemStack +import moe.nea.firmament.repo.ItemCache.asItemStack +import moe.nea.firmament.repo.RepoManager +import moe.nea.firmament.util.SkyblockId + +@Serializable +data class InventoryButton( + val x: Int, + val y: Int, + val anchorRight: Boolean, + val anchorBottom: Boolean, + var icon: String?, + var command: String?, +) { + companion object { + val dimensions = Dimension(18, 18) + fun getItemForName(icon: String): ItemStack { + return RepoManager.getNEUItem(SkyblockId(icon)).asItemStack(idHint = SkyblockId(icon)) + } + } + + fun isValid() = !icon.isNullOrBlank() && !command.isNullOrBlank() + + fun getPosition(guiRect: Rectangle): Point { + return Point( + (if (anchorRight) guiRect.maxX else guiRect.minX) + x, + (if (anchorBottom) guiRect.maxY else guiRect.minY) + y, + ) + } + + fun getBounds(guiRect: Rectangle): Rectangle { + return Rectangle(getPosition(guiRect), dimensions) + } + + fun getItem(): ItemStack { + return getItemForName(icon ?: "") + } + +} diff --git a/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/InventoryButtonEditor.kt b/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/InventoryButtonEditor.kt index f269242..61dbdc6 100644 --- a/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/InventoryButtonEditor.kt +++ b/src/main/kotlin/moe/nea/firmament/features/inventory/buttons/InventoryButtonEditor.kt @@ -3,20 +3,20 @@ package moe.nea.firmament.features.inventory.buttons import io.github.moulberry.moulconfig.common.IItemStack import io.github.moulberry.moulconfig.xml.Bind import io.github.notenoughupdates.moulconfig.platform.ModernItemStack -import me.shedaniel.math.Dimension import me.shedaniel.math.Point import me.shedaniel.math.Rectangle import org.lwjgl.glfw.GLFW import net.minecraft.client.gui.DrawContext import moe.nea.firmament.repo.ItemCache.asItemStack import moe.nea.firmament.repo.RepoManager +import moe.nea.firmament.util.FragmentGuiScreen import moe.nea.firmament.util.MoulConfigUtils import moe.nea.firmament.util.SkyblockId class InventoryButtonEditor( val lastGuiRect: Rectangle, ) : FragmentGuiScreen() { - class Editor(val originalButton: Button) { + class Editor(val originalButton: InventoryButton) { @field:Bind var command: String = originalButton.command ?: "" @@ -26,7 +26,7 @@ class InventoryButtonEditor( @Bind fun getItemIcon(): IItemStack { save() - return ModernItemStack.of(RepoManager.getNEUItem(SkyblockId(icon)).asItemStack(idHint = SkyblockId(icon))) + return ModernItemStack.of(InventoryButton.getItemForName(icon)) } fun save() { @@ -35,29 +35,14 @@ class InventoryButtonEditor( } } - data class Button( - val x: Int, - val y: Int, - val anchorRight: Boolean, - val anchorBottom: Boolean, - var icon: String?, - var command: String?, - ) { - fun isValid() = !icon.isNullOrBlank() && !command.isNullOrBlank() + val buttons: MutableList = + InventoryButtons.DConfig.data.buttons.map { it.copy() }.toMutableList() - fun getPosition(guiRect: Rectangle): Point { - return Point( - (if (anchorRight) guiRect.maxX else guiRect.minX) + x, - (if (anchorBottom) guiRect.maxY else guiRect.minY) + y, - ) - } - - fun getBounds(guiRect: Rectangle): Rectangle { - return Rectangle(getPosition(guiRect), Dimension(18, 18)) - } + override fun close() { + InventoryButtons.DConfig.data.buttons = buttons + super.close() } - val buttons = mutableListOf