Skip to content

Commit

Permalink
maybe a better way to cancel rectangle
Browse files Browse the repository at this point in the history
  • Loading branch information
ImToggle committed Aug 4, 2024
1 parent f25e54b commit 32a58c3
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 81 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/polyfrost/chatting/hook/ChatHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
public class ChatHook {
public static ChatLine currentLine = null;
public static boolean lineVisible = false;
public static boolean cancelRect = false;
}
5 changes: 3 additions & 2 deletions src/main/java/org/polyfrost/chatting/mixin/GuiChatMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/polyfrost/chatting/mixin/GuiMixin.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

}
68 changes: 28 additions & 40 deletions src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}
}
Expand Down Expand Up @@ -319,57 +320,44 @@ private void chatScale(CallbackInfoReturnable<Float> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/org/polyfrost/chatting/mixin/GuiUtilsMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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"))
Expand All @@ -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();
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/mixins.chatting.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"GuiContainerCreativeMixin",
"GuiIngameForgeAccessor",
"GuiIngameForgeMixin",
"GuiMixin",
"GuiNewChatAccessor",
"GuiNewChatMixin",
"GuiNewChatMixin_ChatHeight",
Expand Down

0 comments on commit 32a58c3

Please sign in to comment.