From 32a58c38fe2e184a05a695275dee061d25522b7e Mon Sep 17 00:00:00 2001 From: ImToggle <98242902+ImToggle@users.noreply.github.com> Date: Sun, 4 Aug 2024 10:04:06 +0800 Subject: [PATCH] maybe a better way to cancel rectangle --- .../org/polyfrost/chatting/hook/ChatHook.java | 1 + .../chatting/mixin/GuiChatMixin.java | 5 +- .../polyfrost/chatting/mixin/GuiMixin.java | 21 ++++++ .../chatting/mixin/GuiNewChatMixin.java | 68 ++++++++----------- .../mixin/GuiNewChatMixin_Movable.java | 21 +----- .../mixin/GuiNewChatMixin_Scrolling.java | 17 ++--- .../mixin/GuiNewChatMixin_SmoothMessages.java | 1 - .../chatting/mixin/GuiUtilsMixin.java | 25 ++++--- src/main/resources/mixins.chatting.json | 1 + 9 files changed, 79 insertions(+), 81 deletions(-) create mode 100644 src/main/java/org/polyfrost/chatting/mixin/GuiMixin.java diff --git a/src/main/java/org/polyfrost/chatting/hook/ChatHook.java b/src/main/java/org/polyfrost/chatting/hook/ChatHook.java index 78e150a..607cae3 100644 --- a/src/main/java/org/polyfrost/chatting/hook/ChatHook.java +++ b/src/main/java/org/polyfrost/chatting/hook/ChatHook.java @@ -5,4 +5,5 @@ public class ChatHook { public static ChatLine currentLine = null; public static boolean lineVisible = false; + public static boolean cancelRect = false; } \ No newline at end of file diff --git a/src/main/java/org/polyfrost/chatting/mixin/GuiChatMixin.java b/src/main/java/org/polyfrost/chatting/mixin/GuiChatMixin.java index 80e09d5..227cd8f 100644 --- a/src/main/java/org/polyfrost/chatting/mixin/GuiChatMixin.java +++ b/src/main/java/org/polyfrost/chatting/mixin/GuiChatMixin.java @@ -124,11 +124,12 @@ private void onDrawScreen(int mouseX, int mouseY, float partialTicks, CallbackIn return hook.chatting$isHovering() && x > right * scale && x < (right + 9 * hud.getScale()) * scale; } - @Redirect(method = "drawScreen", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiChat;drawRect(IIIII)V")) - private void cancelBG(int left, int top, int right, int bottom, int color) { + @Inject(method = "drawScreen", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiChat;drawRect(IIIII)V")) + private void drawBG(int mouseX, int mouseY, float partialTicks, CallbackInfo ci) { ChattingConfig config = ChattingConfig.INSTANCE; ChatHooks.INSTANCE.setInputBoxRight(config.getChatInput().getCompactInputBox() ? Math.max((int) config.getChatWindow().getWidth() + 2, ChatHooks.INSTANCE.getInputRight() + (inputField.getText().length() < ModCompatHooks.getChatInputLimit() ? 8 : 2)) : width - 2); config.getChatInput().drawBG(); + ChatHook.cancelRect = true; } @Inject(method = "mouseClicked", at = @At("HEAD")) diff --git a/src/main/java/org/polyfrost/chatting/mixin/GuiMixin.java b/src/main/java/org/polyfrost/chatting/mixin/GuiMixin.java new file mode 100644 index 0000000..690e92b --- /dev/null +++ b/src/main/java/org/polyfrost/chatting/mixin/GuiMixin.java @@ -0,0 +1,21 @@ +package org.polyfrost.chatting.mixin; + +import net.minecraft.client.gui.Gui; +import org.polyfrost.chatting.hook.ChatHook; +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(Gui.class) +public class GuiMixin { + + @Inject(method = "drawRect", at = @At("HEAD"), cancellable = true) + private static void cancelRect(int left, int top, int right, int bottom, int color, CallbackInfo ci) { + if (ChatHook.cancelRect) { + ChatHook.cancelRect = false; + ci.cancel(); + } + } + +} \ No newline at end of file diff --git a/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin.java b/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin.java index bd1519e..be991dd 100644 --- a/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin.java +++ b/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin.java @@ -1,8 +1,8 @@ package org.polyfrost.chatting.mixin; import cc.polyfrost.oneconfig.config.core.OneColor; +import cc.polyfrost.oneconfig.libs.universal.UResolution; import cc.polyfrost.oneconfig.utils.Notifications; -import cc.polyfrost.oneconfig.utils.color.ColorUtils; import net.minecraft.util.IChatComponent; import org.lwjgl.input.Mouse; import org.lwjgl.opengl.GL11; @@ -162,7 +162,7 @@ private void disableScissor(int updateCounter, CallbackInfo ci) { @ModifyArgs(method = "drawChat", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiNewChat;drawRect(IIIII)V", ordinal = 0)) private void captureDrawRect(Args args, int updateCounter) { - args.set(4, ColorUtils.getColor(0, 0, 0, 0)); + ChatHook.cancelRect = true; if (mc.currentScreen instanceof GuiChat) { int left = args.get(0); int top = args.get(1); @@ -174,6 +174,7 @@ private void captureDrawRect(Args args, int updateCounter) { } if (chatting$isHovered(left, top, right - left, 9) || chatting$isHovered(right + 1, top, 9, 9) || chatting$isHovered(right + 11, top, 9, 9)) { args.set(4, chatting$config().getHoveredChatBackgroundColor().getRGB()); + ChatHook.cancelRect = false; } } } @@ -319,57 +320,44 @@ private void chatScale(CallbackInfoReturnable cir) { GlStateManager.translate(chatting$config().getChatWindow().getPaddingX() * chatting$config().getChatWindow().getScale(), 0f, 0f); } if (chatting$config().getChatCopy()) { - mc.getTextureManager().bindTexture(chatting$COPY); - chatting$right = right; - boolean hovered = chatting$isHovered(posLeft, top, posRight - posLeft, 9); - OneColor color = hovered ? chatting$config().getChatButtonHoveredBackgroundColor() : chatting$config().getChatButtonBackgroundColor(); - drawRect(posLeft, top, posRight, top + 9, color.getRGB()); - color = hovered ? chatting$config().getChatButtonHoveredColor() : chatting$config().getChatButtonColor(); - GlStateManager.pushMatrix(); - GlStateManager.enableAlpha(); - GlStateManager.enableBlend(); - GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0); - if (chatting$config().getButtonShadow()) { - GlStateManager.color(0f, 0f, 0f, color.getAlpha() / 255f); - drawModalRectWithCustomSizedTexture(posLeft + 1, top + 1, 0f, 0f, 9, 9, 9, 9); - } - GlStateManager.color(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, color.getAlpha() / 255f); - drawModalRectWithCustomSizedTexture(posLeft, top, 0f, 0f, 9, 9, 9, 9); - GlStateManager.disableAlpha(); - GlStateManager.disableBlend(); - GlStateManager.popMatrix(); + chatting$drawButton(chatting$COPY, posLeft, top, posRight); posLeft += 10; posRight += 10; } if (chatting$config().getChatDelete()) { - mc.getTextureManager().bindTexture(chatting$DELETE); - boolean hovered = chatting$isHovered(posLeft, top, posRight - posLeft, 9); - OneColor color = hovered ? chatting$config().getChatButtonHoveredBackgroundColor() : chatting$config().getChatButtonBackgroundColor(); - drawRect(posLeft, top, posRight, top + 9, color.getRGB()); - color = hovered ? chatting$config().getChatButtonHoveredColor() : chatting$config().getChatButtonColor(); - GlStateManager.pushMatrix(); - GlStateManager.enableAlpha(); - GlStateManager.enableBlend(); - GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0); - if (chatting$config().getButtonShadow()) { - GlStateManager.color(0f, 0f, 0f, color.getAlpha() / 255f); - drawModalRectWithCustomSizedTexture(posLeft + 1, top + 1, 0f, 0f, 9, 9, 9, 9); - } - GlStateManager.color(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, color.getAlpha() / 255f); - drawModalRectWithCustomSizedTexture(posLeft, top, 0f, 0f, 9, 9, 9, 9); - GlStateManager.disableAlpha(); - GlStateManager.disableBlend(); - GlStateManager.popMatrix(); + chatting$drawButton(chatting$DELETE, posLeft, top, posRight); } GlStateManager.disableLighting(); GlStateManager.popMatrix(); } + @Unique + private void chatting$drawButton(ResourceLocation location, int left, int top, int right) { + mc.getTextureManager().bindTexture(chatting$DELETE); + boolean hovered = chatting$isHovered(left, top, 9, 9); + OneColor color = hovered ? chatting$config().getChatButtonHoveredBackgroundColor() : chatting$config().getChatButtonBackgroundColor(); + drawRect(left, top, right, top + 9, color.getRGB()); + color = hovered ? chatting$config().getChatButtonHoveredColor() : chatting$config().getChatButtonColor(); + GlStateManager.pushMatrix(); + GlStateManager.enableAlpha(); + GlStateManager.enableBlend(); + GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0); + if (chatting$config().getButtonShadow()) { + GlStateManager.color(0f, 0f, 0f, color.getAlpha() / 255f); + drawModalRectWithCustomSizedTexture(left + 1, top + 1, 0f, 0f, 9, 9, 9, 9); + } + GlStateManager.color(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, color.getAlpha() / 255f); + drawModalRectWithCustomSizedTexture(left, top, 0f, 0f, 9, 9, 9, 9); + GlStateManager.disableAlpha(); + GlStateManager.disableBlend(); + GlStateManager.popMatrix(); + } + @Override public ChatLine chatting$getHoveredLine(int mouseY) { if (this.getChatOpen()) { ScaledResolution scaledresolution = new ScaledResolution(this.mc); - int i = scaledresolution.getScaleFactor(); + float i = (float) UResolution.getScaleFactor(); ChatWindow hud = chatting$config().getChatWindow(); float f = hud.getScale(); int k = (int) (mouseY / i - (scaledresolution.getScaledHeight() - hud.position.getBottomY() + hud.getPaddingY() * hud.getScale()) + ModCompatHooks.getYOffset()); diff --git a/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_Movable.java b/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_Movable.java index 496cabf..1c7ffc3 100644 --- a/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_Movable.java +++ b/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_Movable.java @@ -2,10 +2,9 @@ import cc.polyfrost.oneconfig.hud.Position; import cc.polyfrost.oneconfig.internal.hud.HudCore; -import net.minecraft.client.Minecraft; +import cc.polyfrost.oneconfig.libs.universal.UResolution; import net.minecraft.client.gui.ChatLine; import net.minecraft.client.gui.GuiNewChat; -import net.minecraft.client.gui.ScaledResolution; import net.minecraft.util.MathHelper; import org.polyfrost.chatting.chat.ChatWindow; import org.polyfrost.chatting.config.ChattingConfig; @@ -38,16 +37,6 @@ private int width(int value) { return MathHelper.ceiling_float_int((float)this.getChatWidth()); } - @ModifyConstant(method = "drawChat", constant = @Constant(intValue = 9, ordinal = 0)) - private int chatMode(int constant) { - return constant; - } - - @Redirect(method = "drawChat", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiNewChat;getChatScale()F")) - private float scale(GuiNewChat instance) { - return ChattingConfig.INSTANCE.getChatWindow().getScale(); - } - @Inject(method = "drawChat", at = @At(value = "HEAD"), cancellable = true) private void exampleChat(int updateCounter, CallbackInfo ci) { if (HudCore.editing) ci.cancel(); @@ -62,9 +51,8 @@ private int mouseX(int constant) { @ModifyConstant(method = "getChatComponent", constant = @Constant(intValue = 27)) private int mouseY(int constant) { - int height = new ScaledResolution(Minecraft.getMinecraft()).getScaledHeight(); ChatWindow hud = ChattingConfig.INSTANCE.getChatWindow(); - return height - (int) (hud.position.getBottomY() - hud.getPaddingY() * hud.getScale() + ModCompatHooks.getChatPosition()); + return UResolution.getScaledHeight() - (int) (hud.position.getBottomY() - hud.getPaddingY() * hud.getScale() + ModCompatHooks.getChatPosition()); } @ModifyVariable(method = "getChatComponent", at = @At("STORE"), ordinal = 0) @@ -78,11 +66,6 @@ private int offset(int value) { return ((ChatLineHook) chatting$currentLine).chatting$hasDetected() || ChattingConfig.INSTANCE.getOffsetNonPlayerMessages() ? ModCompatHooks.getChatHeadOffset() : 0; } - @Redirect(method = "getChatComponent", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiNewChat;getChatScale()F", ordinal = 0)) - private float getScale(GuiNewChat instance) { - return ChattingConfig.INSTANCE.getChatWindow().getScale(); - } - @ModifyArg(method = "getChatComponent", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/MathHelper;floor_float(F)I", ordinal = 2)) private float width(float value) { ChatWindow hud = ChattingConfig.INSTANCE.getChatWindow(); diff --git a/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_Scrolling.java b/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_Scrolling.java index 975dd07..80fde2f 100644 --- a/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_Scrolling.java +++ b/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_Scrolling.java @@ -6,6 +6,7 @@ import net.minecraft.client.gui.Gui; import net.minecraft.client.gui.GuiNewChat; import org.polyfrost.chatting.config.ChattingConfig; +import org.polyfrost.chatting.hook.ChatHook; import org.polyfrost.chatting.utils.EaseOutQuad; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -43,18 +44,14 @@ private int redirectPos(GuiNewChat instance) { return ChattingConfig.INSTANCE.getSmoothScrolling() ? (int) chatting$scrollingAnimation.get() : scrollPos; } - @Redirect(method = "drawChat", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiNewChat;drawRect(IIIII)V", ordinal = 1)) - private void redirectScrollBar(int left, int top, int right, int bottom, int color) { - if (!ChattingConfig.INSTANCE.getRemoveScrollBar()) { - drawRect(left, top, right, bottom, color); - } + @Inject(method = "drawChat", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiNewChat;drawRect(IIIII)V", ordinal = 1)) + private void redirectScrollBar(int updateCounter, CallbackInfo ci) { + if (ChattingConfig.INSTANCE.getRemoveScrollBar()) ChatHook.cancelRect = true; } - @Redirect(method = "drawChat", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiNewChat;drawRect(IIIII)V", ordinal = 2)) - private void redirectScrollBar2(int left, int top, int right, int bottom, int color) { - if (!ChattingConfig.INSTANCE.getRemoveScrollBar()) { - drawRect(left, top, right, bottom, color); - } + @Inject(method = "drawChat", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiNewChat;drawRect(IIIII)V", ordinal = 2)) + private void redirectScrollBar2(int updateCounter, CallbackInfo ci) { + if (ChattingConfig.INSTANCE.getRemoveScrollBar()) ChatHook.cancelRect = true; } } diff --git a/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_SmoothMessages.java b/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_SmoothMessages.java index 0c7cb2d..baeabc8 100644 --- a/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_SmoothMessages.java +++ b/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_SmoothMessages.java @@ -1,6 +1,5 @@ package org.polyfrost.chatting.mixin; -import org.polyfrost.chatting.Chatting; import org.polyfrost.chatting.chat.ChatSearchingManager; import org.polyfrost.chatting.chat.ChatTabs; import org.polyfrost.chatting.config.ChattingConfig; diff --git a/src/main/java/org/polyfrost/chatting/mixin/GuiUtilsMixin.java b/src/main/java/org/polyfrost/chatting/mixin/GuiUtilsMixin.java index 1e2d0e6..525e507 100644 --- a/src/main/java/org/polyfrost/chatting/mixin/GuiUtilsMixin.java +++ b/src/main/java/org/polyfrost/chatting/mixin/GuiUtilsMixin.java @@ -4,22 +4,21 @@ import net.minecraft.client.gui.FontRenderer; import net.minecraftforge.fml.client.config.GuiUtils; import org.polyfrost.chatting.config.ChattingConfig; +import org.polyfrost.chatting.hook.ChatHook; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import java.util.List; @Mixin(value = GuiUtils.class, remap = false) public class GuiUtilsMixin { - @Shadow - public static void drawGradientRect(int zLevel, int left, int top, int right, int bottom, int startColor, int endColor) { - } - @Redirect(method = "drawHoveringText", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/client/config/GuiUtils;drawGradientRect(IIIIIII)V")) - private static void redirectBackground(int zLevel, int left, int top, int right, int bottom, int startColor, int endColor) { - if (!ChattingConfig.INSTANCE.getRemoveTooltipBackground()) { - drawGradientRect(zLevel, left, top, right, bottom, startColor, endColor); - } + @Inject(method = "drawHoveringText", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/client/config/GuiUtils;drawGradientRect(IIIIIII)V")) + private static void redirectBackground(List textLines, int mouseX, int mouseY, int screenWidth, int screenHeight, int maxTextWidth, FontRenderer font, CallbackInfo ci) { + if (ChattingConfig.INSTANCE.getRemoveTooltipBackground()) ChatHook.cancelRect = true; } @Redirect(method = "drawHoveringText", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/FontRenderer;drawStringWithShadow(Ljava/lang/String;FFI)I")) @@ -33,4 +32,12 @@ private static int redirectText(FontRenderer instance, String text, float x, flo return instance.drawStringWithShadow(text, x, y, color); } } + + @Inject(method = "drawGradientRect", at = @At("HEAD"), cancellable = true) + private static void cancelRect(int zLevel, int left, int top, int right, int bottom, int startColor, int endColor, CallbackInfo ci) { + if (ChatHook.cancelRect) { + ChatHook.cancelRect = false; + ci.cancel(); + } + } } diff --git a/src/main/resources/mixins.chatting.json b/src/main/resources/mixins.chatting.json index 2bdc703..f43a84e 100644 --- a/src/main/resources/mixins.chatting.json +++ b/src/main/resources/mixins.chatting.json @@ -15,6 +15,7 @@ "GuiContainerCreativeMixin", "GuiIngameForgeAccessor", "GuiIngameForgeMixin", + "GuiMixin", "GuiNewChatAccessor", "GuiNewChatMixin", "GuiNewChatMixin_ChatHeight",