Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend: GuiContainerEvent.BeforeDraw #1510

Merged
merged 16 commits into from
May 3, 2024
3 changes: 3 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import at.hannibal2.skyhanni.data.GardenComposterUpgradesData
import at.hannibal2.skyhanni.data.GardenCropMilestones
import at.hannibal2.skyhanni.data.GardenCropMilestonesCommunityFix
import at.hannibal2.skyhanni.data.GardenCropUpgrades
import at.hannibal2.skyhanni.data.GuiData
import at.hannibal2.skyhanni.data.GuiEditManager
import at.hannibal2.skyhanni.data.GuildAPI
import at.hannibal2.skyhanni.data.HighlightOnHoverSlot
Expand Down Expand Up @@ -539,6 +540,7 @@ class SkyHanniMod {
loadModule(TrackerManager)
loadModule(ScoreboardPattern)
loadModule(UtilsPatterns)
loadModule(GuiData)
loadModule(BossbarData)
loadModule(EntityUtils)
loadModule(ChatUtils)
Expand Down Expand Up @@ -959,6 +961,7 @@ class SkyHanniMod {
screenTicks++
if (screenTicks == 5) {
Minecraft.getMinecraft().thePlayer.closeScreen()
OtherInventoryData.close()
Minecraft.getMinecraft().displayGuiScreen(screenToOpen)
screenTicks = 0
screenToOpen = null
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/data/GuiData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package at.hannibal2.skyhanni.data

import at.hannibal2.skyhanni.events.GuiContainerEvent
import at.hannibal2.skyhanni.events.InventoryCloseEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.events.NEURenderEvent
import at.hannibal2.skyhanni.utils.DelayedRun
import at.hannibal2.skyhanni.utils.KeyboardManager.isKeyHeld
import io.github.moulberry.notenoughupdates.NEUApi
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.inventory.GuiChest
import net.minecraftforge.client.event.GuiOpenEvent
import net.minecraftforge.client.event.GuiScreenEvent
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.network.FMLNetworkEvent
import org.lwjgl.input.Keyboard

object GuiData {

var preDrawEventCanceled = false

@SubscribeEvent(priority = EventPriority.HIGH)
fun onNeuRenderEvent(event: NEURenderEvent) {
if (preDrawEventCanceled) event.cancel()
}

@SubscribeEvent(priority = EventPriority.HIGH)
fun onClick(event: GuiContainerEvent.SlotClickEvent) {
if (preDrawEventCanceled) event.cancel()
}

@SubscribeEvent(priority = EventPriority.HIGH)
fun onGuiClick(event: GuiScreenEvent.MouseInputEvent.Pre) {
if (preDrawEventCanceled) event.isCanceled = true
}

@SubscribeEvent(priority = EventPriority.HIGH)
fun onGuiKeyPress(event: GuiScreenEvent.KeyboardInputEvent.Pre) {
val (escKey, invKey) = Minecraft.getMinecraft().gameSettings.let {
Keyboard.KEY_ESCAPE to it.keyBindInventory.keyCode
}
if (escKey.isKeyHeld() || invKey.isKeyHeld()) return
if (preDrawEventCanceled) event.isCanceled = true
}

@SubscribeEvent
fun onInventoryClose(event: InventoryCloseEvent) {
DelayedRun.runNextTick {
if (Minecraft.getMinecraft().currentScreen !is GuiChest) {
preDrawEventCanceled = false
}
}
}

@SubscribeEvent
fun onWorldChange(event: LorenzWorldChangeEvent) {
preDrawEventCanceled = false
}

@SubscribeEvent
fun onDisconnect(event: FMLNetworkEvent.ClientDisconnectionFromServerEvent) {
preDrawEventCanceled = false
}

@SubscribeEvent(priority = EventPriority.LOW)
fun onGuiOpen(event: GuiOpenEvent) {
if (preDrawEventCanceled) {
NEUApi.setInventoryButtonsToDisabled()
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/events/GuiContainerEvent.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package at.hannibal2.skyhanni.events

import at.hannibal2.skyhanni.utils.GuiRenderUtils
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.inventory.GuiContainer
import net.minecraft.inventory.Container
Expand All @@ -17,6 +18,18 @@ abstract class GuiContainerEvent(open val gui: GuiContainer, open val container:
val partialTicks: Float,
) : GuiContainerEvent(gui, container)

@Cancelable
data class BeforeDraw(
override val gui: GuiContainer,
override val container: Container,
val mouseX: Int,
val mouseY: Int,
val partialTicks: Float,
) : GuiContainerEvent(gui, container) {
fun drawDefaultBackground() =
GuiRenderUtils.drawGradientRect(0, 0, gui.width, gui.height, -1072689136, -804253680, 0.0)
}

@Cancelable
data class CloseWindowEvent(override val gui: GuiContainer, override val container: Container) :
GuiContainerEvent(gui, container)
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/events/NEURenderEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package at.hannibal2.skyhanni.events

import net.minecraftforge.fml.common.eventhandler.Cancelable

@Cancelable
class NEURenderEvent : LorenzEvent()
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package at.hannibal2.skyhanni.mixins.hooks

import at.hannibal2.skyhanni.data.GuiData
import at.hannibal2.skyhanni.events.DrawScreenAfterEvent
import at.hannibal2.skyhanni.events.GuiContainerEvent
import at.hannibal2.skyhanni.events.GuiContainerEvent.CloseWindowEvent
import at.hannibal2.skyhanni.events.GuiContainerEvent.SlotClickEvent
import at.hannibal2.skyhanni.test.SkyHanniDebugsAndTests
import io.github.moulberry.notenoughupdates.NEUApi
import net.minecraft.client.gui.inventory.GuiContainer
import net.minecraft.inventory.Slot
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo
Expand All @@ -26,6 +28,22 @@ class GuiContainerHook(guiAny: Any) {
GuiContainerEvent.BackgroundDrawnEvent(gui, gui.inventorySlots, mouseX, mouseY, partialTicks).postAndCatch()
}

fun preDraw(
mouseX: Int,
mouseY: Int,
partialTicks: Float,
ci: CallbackInfo,
) {
if (!SkyHanniDebugsAndTests.globalRender) return
if (GuiContainerEvent.BeforeDraw(gui, gui.inventorySlots, mouseX, mouseY, partialTicks).postAndCatch()) {
NEUApi.setInventoryButtonsToDisabled()
GuiData.preDrawEventCanceled = true
ci.cancel()
} else {
GuiData.preDrawEventCanceled = false
}
}

fun foregroundDrawn(mouseX: Int, mouseY: Int, partialTicks: Float) {
GuiContainerEvent.ForegroundDrawnEvent(gui, gui.inventorySlots, mouseX, mouseY, partialTicks).postAndCatch()
}
Expand All @@ -52,4 +70,5 @@ class GuiContainerHook(guiAny: Any) {
) {
if (DrawScreenAfterEvent(mouseX, mouseY, ci).postAndCatch()) ci.cancel()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package at.hannibal2.skyhanni.mixins.transformers;

import at.hannibal2.skyhanni.events.NEURenderEvent;
import io.github.moulberry.notenoughupdates.NEUOverlay;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(NEUOverlay.class)
public class MixinNEUOverlay {

@Inject(method = "render", at = @At("HEAD"), cancellable = true, remap = false)
private void render(boolean hoverInv, CallbackInfo ci) {
if (new NEURenderEvent().postAndCatch()) {
ci.cancel();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ private void backgroundDrawn(int mouseX, int mouseY, float partialTicks, Callbac
skyHanni$hook.backgroundDrawn(mouseX, mouseY, partialTicks);
}

@Inject(method = "drawScreen", at = @At("HEAD"), cancellable = true)
private void preDraw(int mouseX, int mouseY, float partialTicks, CallbackInfo ci) {
skyHanni$hook.preDraw(mouseX, mouseY, partialTicks, ci);
}

@Inject(method = "drawScreen", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/inventory/GuiContainer;drawGuiContainerForegroundLayer(II)V", shift = At.Shift.AFTER))
private void onForegroundDraw(int mouseX, int mouseY, float partialTicks, CallbackInfo ci) {
skyHanni$hook.foregroundDrawn(mouseX, mouseY, partialTicks);
Expand Down
41 changes: 40 additions & 1 deletion src/main/java/at/hannibal2/skyhanni/utils/GuiRenderUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import net.minecraft.client.gui.FontRenderer
import net.minecraft.client.gui.GuiScreen
import net.minecraft.client.renderer.GlStateManager
import net.minecraft.client.renderer.RenderHelper
import net.minecraft.client.renderer.Tessellator
import net.minecraft.client.renderer.vertex.DefaultVertexFormats
import net.minecraft.item.ItemStack
import org.lwjgl.opengl.GL11
import java.awt.Color
Expand Down Expand Up @@ -310,7 +312,7 @@ object GuiRenderUtils {
color: Color,
useChroma: Boolean,
texture: SkillProgressBarConfig.TexturedBar.UsedTexture,
height: Float
height: Float,
) {
GlStateManager.pushMatrix()
GlStateManager.translate(x, y, 0f)
Expand Down Expand Up @@ -377,4 +379,41 @@ object GuiRenderUtils {
}
GlStateManager.popMatrix()
}

/**@Mojang */
fun drawGradientRect(
left: Int,
top: Int,
right: Int,
bottom: Int,
startColor: Int,
endColor: Int,
zLevel: Double,
) {
val f = (startColor shr 24 and 255).toFloat() / 255.0f
val g = (startColor shr 16 and 255).toFloat() / 255.0f
val h = (startColor shr 8 and 255).toFloat() / 255.0f
val i = (startColor and 255).toFloat() / 255.0f
val j = (endColor shr 24 and 255).toFloat() / 255.0f
val k = (endColor shr 16 and 255).toFloat() / 255.0f
val l = (endColor shr 8 and 255).toFloat() / 255.0f
val m = (endColor and 255).toFloat() / 255.0f
GlStateManager.disableTexture2D()
GlStateManager.enableBlend()
GlStateManager.disableAlpha()
GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0)
GlStateManager.shadeModel(7425)
val tessellator = Tessellator.getInstance()
val worldRenderer = tessellator.worldRenderer
worldRenderer.begin(7, DefaultVertexFormats.POSITION_COLOR)
worldRenderer.pos(right.toDouble(), top.toDouble(), zLevel).color(g, h, i, f).endVertex()
worldRenderer.pos(left.toDouble(), top.toDouble(), zLevel).color(g, h, i, f).endVertex()
worldRenderer.pos(left.toDouble(), bottom.toDouble(), zLevel).color(k, l, m, j).endVertex()
worldRenderer.pos(right.toDouble(), bottom.toDouble(), zLevel).color(k, l, m, j).endVertex()
tessellator.draw()
GlStateManager.shadeModel(7424)
GlStateManager.disableBlend()
GlStateManager.enableAlpha()
GlStateManager.enableTexture2D()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package at.hannibal2.skyhanni.utils.renderables

import at.hannibal2.skyhanni.config.core.config.gui.GuiPositionEditor
import at.hannibal2.skyhanni.config.features.skillprogress.SkillProgressBarConfig
import at.hannibal2.skyhanni.data.GuiData
import at.hannibal2.skyhanni.data.HighlightOnHoverSlot
import at.hannibal2.skyhanni.data.ToolTipData
import at.hannibal2.skyhanni.features.chroma.ChromaShaderManager
Expand Down Expand Up @@ -231,7 +232,7 @@ interface Renderable {
}
val isGuiPositionEditor = Minecraft.getMinecraft().currentScreen !is GuiPositionEditor
val isNotInSignAndOnSlot = if (Minecraft.getMinecraft().currentScreen !is GuiEditSign) {
ToolTipData.lastSlot == null
ToolTipData.lastSlot == null || GuiData.preDrawEventCanceled
} else true
val isConfigScreen = Minecraft.getMinecraft().currentScreen !is GuiScreenElementWrapper

Expand Down
Loading