Skip to content

Commit

Permalink
movable chat
Browse files Browse the repository at this point in the history
  • Loading branch information
ImToggle committed Jan 21, 2024
1 parent 756cd83 commit 54bfa80
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cc.polyfrost.oneconfig.internal.hud;

public class HudCore {

public static boolean editing;

}
32 changes: 15 additions & 17 deletions src/main/java/org/polyfrost/chatting/mixin/GuiChatMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@
import cc.polyfrost.oneconfig.libs.universal.UKeyboard;
import com.google.common.collect.Lists;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ChatLine;
import net.minecraft.client.gui.GuiChat;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.gui.*;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.MathHelper;
import net.minecraftforge.fml.client.config.GuiUtils;
import org.apache.commons.lang3.StringUtils;
import org.lwjgl.input.Mouse;
import org.polyfrost.chatting.chat.ChatHooks;
import org.polyfrost.chatting.chat.ChatScrollingHook;
import org.polyfrost.chatting.chat.ChatSearchingManager;
import org.polyfrost.chatting.chat.ChatShortcuts;
import org.polyfrost.chatting.chat.ChatTab;
import org.polyfrost.chatting.chat.ChatTabs;
import org.polyfrost.chatting.chat.*;
import org.polyfrost.chatting.config.ChattingConfig;
import org.polyfrost.chatting.gui.components.CleanButton;
import org.polyfrost.chatting.gui.components.ClearButton;
Expand All @@ -45,6 +37,8 @@ public abstract class GuiChatMixin extends GuiScreen implements GuiChatHook {
@Shadow
protected GuiTextField inputField;

@Shadow public abstract void drawScreen(int mouseX, int mouseY, float partialTicks);

/**
* Gets the modifier key name depending on the operating system
*
Expand Down Expand Up @@ -104,9 +98,11 @@ private void keyTyped(char typedChar, int keyCode, CallbackInfo ci) {
private void onDrawScreen(int mouseX, int mouseY, float partialTicks, CallbackInfo ci) {
if (ChattingConfig.INSTANCE.getChatCopy()) {
GuiNewChatHook hook = ((GuiNewChatHook) Minecraft.getMinecraft().ingameGUI.getChatGUI());
float f = mc.ingameGUI.getChatGUI().getChatScale();
int x = MathHelper.floor_float((float) mouseX / f);
if (hook.chatting$isHovering() && (hook.chatting$getRight() + ModCompatHooks.getXOffset() + 3) <= x && (hook.chatting$getRight() + ModCompatHooks.getXOffset()) + 13 > x) {
ChatWindow hud = ChattingConfig.INSTANCE.getChatWindow();
int scale = new ScaledResolution(mc).getScaleFactor();
int x = Mouse.getX();
int right = (int) ((hook.chatting$getRight() + ModCompatHooks.getXOffset() + 1) * hud.getScale() + (int) hud.position.getX());
if (hook.chatting$isHovering() && x >= right * scale && x < (right + 9 * hud.getScale()) * scale) {
GuiUtils.drawHoveringText(COPY_TOOLTIP, mouseX, mouseY, width, height, -1, fontRendererObj);
GlStateManager.disableLighting();
}
Expand All @@ -126,18 +122,20 @@ private int modifyInputBoxColor(int color) {
@Inject(method = "mouseClicked", at = @At("HEAD"))
private void mouseClicked(int mouseX, int mouseY, int mouseButton, CallbackInfo ci) {
GuiNewChatHook hook = ((GuiNewChatHook) Minecraft.getMinecraft().ingameGUI.getChatGUI());
float f = mc.ingameGUI.getChatGUI().getChatScale();
int x = MathHelper.floor_float((float) mouseX / f);
ChatWindow hud = ChattingConfig.INSTANCE.getChatWindow();
int scale = new ScaledResolution(mc).getScaleFactor();
int x = Mouse.getX();
if (hook.chatting$isHovering()) {
if (ChattingConfig.INSTANCE.getChatCopy() && (((hook.chatting$getRight() + ModCompatHooks.getXOffset() + 3) <= x && (hook.chatting$getRight() + ModCompatHooks.getXOffset()) + 13 > x) || (mouseButton == 1 && ChattingConfig.INSTANCE.getRightClickCopy()))) {
int right = (int) ((hook.chatting$getRight() + ModCompatHooks.getXOffset() + 1) * hud.getScale() + (int) hud.position.getX()) * scale;
if (ChattingConfig.INSTANCE.getChatCopy() && x >= right && x < right + 9 * hud.getScale() * scale || (mouseButton == 1 && ChattingConfig.INSTANCE.getRightClickCopy())) {
Transferable message = hook.chatting$getChattingChatComponent(Mouse.getY());
if (message == null) return;
try {
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(message, null);
} catch (Exception e) {
e.printStackTrace();
}
} else if (ChattingConfig.INSTANCE.getChatDelete() && ((hook.chatting$getRight() + ModCompatHooks.getXOffset() + 13) <= x && (hook.chatting$getRight() + ModCompatHooks.getXOffset()) + 23 > x)) {
} else if (ChattingConfig.INSTANCE.getChatDelete() && x >= right + 10 * hud.getScale() * scale && x < right + 19 * hud.getScale() * scale) {
ChatLine chatLine = hook.chatting$getHoveredLine(Mouse.getY());
if (chatLine == null) return;
ModCompatHooks.getDrawnChatLines().removeIf(line -> ((ChatLineHook) line).chatting$getUniqueId() == ((ChatLineHook) chatLine).chatting$getUniqueId());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.polyfrost.chatting.mixin;

import net.minecraftforge.client.GuiIngameForge;
import org.polyfrost.chatting.config.ChattingConfig;
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.ModifyArgs;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.invoke.arg.Args;

@Mixin(GuiIngameForge.class)
public class GuiIngameForgeMixin {

@ModifyArgs(method = "renderChat", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/GlStateManager;translate(FFF)V"))
private void cancelTranslate(Args args) {
args.set(1, 0f);
}

@Inject(method = "renderChat", at = @At(value = "HEAD"), cancellable = true, remap = false)
private void cancelChat(int width, int height, CallbackInfo ci) {
if (!ChattingConfig.INSTANCE.getChatWindow().isEnabled()) ci.cancel();
}
}
41 changes: 22 additions & 19 deletions src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import cc.polyfrost.oneconfig.libs.universal.UMouse;
import cc.polyfrost.oneconfig.utils.Notifications;
import cc.polyfrost.oneconfig.utils.color.ColorUtils;
import org.lwjgl.input.Mouse;
import org.polyfrost.chatting.Chatting;
import org.polyfrost.chatting.chat.ChatSearchingManager;
import org.polyfrost.chatting.chat.ChatWindow;
import org.polyfrost.chatting.config.ChattingConfig;
import org.polyfrost.chatting.hook.GuiNewChatHook;
import org.polyfrost.chatting.utils.ModCompatHooks;
Expand Down Expand Up @@ -46,10 +48,6 @@ public abstract class GuiNewChatMixin extends Gui implements GuiNewChatHook {
private List<ChatLine> drawnChatLines;
@Shadow
public abstract boolean getChatOpen();

@Shadow
public abstract float getChatScale();

@Shadow
public abstract int getLineCount();

Expand Down Expand Up @@ -120,8 +118,7 @@ private void captureDrawRect(Args args, int updateCounter) {
int left = args.get(0);
int top = args.get(1);
int right = args.get(2);
int bottom = args.get(3);
if (isInBounds(left, top, right, bottom, getChatScale())) {
if (isHovered(left, top, right - left, 9)) {
chatting$isHovering = true;
chatting$lineInBounds = true;
args.set(4, ChattingConfig.INSTANCE.getHoveredChatBackgroundColor().getRGB());
Expand Down Expand Up @@ -160,25 +157,28 @@ private void captureDrawRect(Args args, int updateCounter) {
@ModifyArgs(method = "drawChat", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/FontRenderer;drawStringWithShadow(Ljava/lang/String;FFI)I"))
private void drawChatBox(Args args) {
if (mc.currentScreen instanceof GuiChat) {
float f = this.getChatScale();
float f = ChattingConfig.INSTANCE.getChatWindow().getScale();
int left = 0;
int top = (int) ((float) args.get(2) - 1);
int right = MathHelper.ceiling_float_int((float)getChatWidth() / f) + 4;
int right = MathHelper.ceiling_float_int((float)getChatWidth()) + 4;
int bottom = (int) ((float) args.get(2) + 8);
if ((chatting$isHovering && chatting$lineInBounds) || isInBounds(left, top, right, bottom, f)) {
if ((chatting$isHovering && chatting$lineInBounds) || isHovered(left, top, right + 20, 9)) {
chatting$isHovering = true;
drawCopyChatBox(right, top);
}
}
chatting$lineInBounds = false;
}

private boolean isInBounds(int left, int top, int right, int bottom, float chatScale) {
int mouseX = MathHelper.floor_double(UMouse.getScaledX()) - 3;
int mouseY = MathHelper.floor_double(UMouse.getScaledY()) - 27 + ModCompatHooks.getYOffset() - ModCompatHooks.getChatPosition();
mouseX = MathHelper.floor_float((float) mouseX / chatScale);
mouseY = -(MathHelper.floor_float((float) mouseY / chatScale)); //WHY DO I NEED TO DO THIS
return mouseX >= (left + ModCompatHooks.getXOffset()) && mouseY < bottom && mouseX < (right + 23 + ModCompatHooks.getXOffset()) && mouseY >= top;
private boolean isHovered(int x, int y, int width, int height) {
ChatWindow hud = ChattingConfig.INSTANCE.getChatWindow();
ScaledResolution scaleResolution = new ScaledResolution(mc);
int scale = scaleResolution.getScaleFactor();
int mouseX = Mouse.getX();
int mouseY = mc.displayHeight - Mouse.getY();
int actualX = (int) (((int) hud.position.getX() + x * hud.getScale()) * scale);
int actualY = (int) (((int) hud.position.getBottomY() + y * hud.getScale()) * scale);
return mouseX >= actualX && mouseX < actualX + width * hud.getScale() * scale && mouseY >= actualY && mouseY < actualY + height * hud.getScale() * scale;
}

@ModifyVariable(method = "drawChat", at = @At("STORE"), ordinal = 0)
Expand Down Expand Up @@ -238,7 +238,8 @@ private void drawCopyChatBox(int right, int top) {
GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);
chatting$right = right;
drawModalRectWithCustomSizedTexture(posLeft, top, 0f, 0f, 9, 9, 9, 9);
drawRect(posLeft, top, posRight, top + 9, (((right + ModCompatHooks.getXOffset() + 3) <= (UMouse.getScaledX() / mc.ingameGUI.getChatGUI().getChatScale()) && (right + ModCompatHooks.getXOffset()) + 13 > (UMouse.getScaledX() / mc.ingameGUI.getChatGUI().getChatScale())) ? ChattingConfig.INSTANCE.getChatButtonHoveredBackgroundColor().getRGB() : ChattingConfig.INSTANCE.getChatButtonBackgroundColor().getRGB()));
int color = isHovered(posLeft, top, posRight - posLeft, 9) ? ChattingConfig.INSTANCE.getChatButtonHoveredBackgroundColor().getRGB() : ChattingConfig.INSTANCE.getChatButtonBackgroundColor().getRGB();
drawRect(posLeft, top, posRight, top + 9, color);
posLeft += 10;
posRight += 10;
GlStateManager.disableAlpha();
Expand All @@ -253,7 +254,8 @@ private void drawCopyChatBox(int right, int top) {
GlStateManager.blendFunc(770, 771);
GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);
drawModalRectWithCustomSizedTexture(posLeft, top, 0f, 0f, 9, 9, 9, 9);
drawRect(posLeft, top, posRight, top + 9, (((right + ModCompatHooks.getXOffset() + 13) <= (UMouse.getScaledX() / mc.ingameGUI.getChatGUI().getChatScale()) && (right + ModCompatHooks.getXOffset()) + 23 > (UMouse.getScaledX() / mc.ingameGUI.getChatGUI().getChatScale())) ? ChattingConfig.INSTANCE.getChatButtonHoveredBackgroundColor().getRGB() : ChattingConfig.INSTANCE.getChatButtonBackgroundColor().getRGB()));
int color = isHovered(posLeft, top, posRight - posLeft, 9) ? ChattingConfig.INSTANCE.getChatButtonHoveredBackgroundColor().getRGB() : ChattingConfig.INSTANCE.getChatButtonBackgroundColor().getRGB();
drawRect(posLeft, top, posRight, top + 9, color);
GlStateManager.disableAlpha();
GlStateManager.disableRescaleNormal();
}
Expand All @@ -266,8 +268,9 @@ private void drawCopyChatBox(int right, int top) {
if (this.getChatOpen()) {
ScaledResolution scaledresolution = new ScaledResolution(this.mc);
int i = scaledresolution.getScaleFactor();
float f = this.getChatScale();
int k = mouseY / i - 27 + ModCompatHooks.getYOffset() - ModCompatHooks.getChatPosition();
ChatWindow hud = ChattingConfig.INSTANCE.getChatWindow();
float f = hud.getScale();
int k = (int) (mouseY / i - (scaledresolution.getScaledHeight() - hud.position.getBottomY()) + ModCompatHooks.getYOffset() - ModCompatHooks.getChatPosition());
k = MathHelper.floor_float((float) k / f);

if (k >= 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.polyfrost.chatting.mixin;

import cc.polyfrost.oneconfig.hud.Position;
import cc.polyfrost.oneconfig.internal.hud.HudCore;
import net.minecraft.client.gui.GuiNewChat;
import net.minecraft.util.MathHelper;
import org.polyfrost.chatting.chat.ChatWindow;
import org.polyfrost.chatting.config.ChattingConfig;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.invoke.arg.Args;

@Mixin(GuiNewChat.class)
public abstract class GuiNewChatMixin_Movable {

@Shadow public abstract int getChatWidth();

@ModifyArgs(method = "drawChat", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/GlStateManager;translate(FFF)V", ordinal = 0))
private void translate(Args args) {
ChatWindow hud = ChattingConfig.INSTANCE.getChatWindow();
Position position = hud.position;
args.set(0, position.getX());
args.set(1, position.getBottomY());
}

@ModifyVariable(method = "drawChat", at = @At(value = "STORE", ordinal = 0), ordinal = 4)
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();
}

}
49 changes: 49 additions & 0 deletions src/main/kotlin/org/polyfrost/chatting/chat/ChatWindow.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.polyfrost.chatting.chat

import cc.polyfrost.oneconfig.config.annotations.DualOption
import cc.polyfrost.oneconfig.config.annotations.Exclude
import cc.polyfrost.oneconfig.hud.Hud
import cc.polyfrost.oneconfig.libs.universal.UMatrixStack
import cc.polyfrost.oneconfig.renderer.TextRenderer
import cc.polyfrost.oneconfig.utils.dsl.*
import org.polyfrost.chatting.config.ChattingConfig

class ChatWindow : Hud(true) {

@Exclude
private val exampleChat = listOf(
"Chatting by PolyFrost Team",
"----------------------------------------",
"This is a movable chat",
"Drag me around!",
"Click to drag"
)

@DualOption(
name = "Type",
left = "Left",
right = "Right"
)
var chatType = false

override fun draw(matrices: UMatrixStack?, x: Float, y: Float, scale: Float, example: Boolean) {
if (!example) return
nanoVG(true) {
drawRect(x, y, position.width, position.height, ChattingConfig.chatBackgroundColor.rgb, false)
}
var textY = y
for (text in exampleChat) {
TextRenderer.drawScaledString(text, x, textY, 16777215, TextRenderer.TextType.toType(ChattingConfig.textRenderType), scale)
textY += 9 * scale
}
}

override fun getWidth(scale: Float, example: Boolean): Float {
return 240f * scale
}

override fun getHeight(scale: Float, example: Boolean): Float {
return 9f * 5 * scale
}

}
10 changes: 6 additions & 4 deletions src/main/kotlin/org/polyfrost/chatting/config/ChattingConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import cc.polyfrost.oneconfig.libs.universal.UMinecraft
import cc.polyfrost.oneconfig.utils.hypixel.HypixelUtils
import club.sk1er.patcher.config.PatcherConfig
import org.polyfrost.chatting.Chatting
import org.polyfrost.chatting.chat.ChatHooks
import org.polyfrost.chatting.chat.ChatShortcuts
import org.polyfrost.chatting.chat.ChatTab
import org.polyfrost.chatting.chat.ChatTabs
import org.polyfrost.chatting.chat.*
import org.polyfrost.chatting.gui.components.TabButton
import org.polyfrost.chatting.hook.ChatLineHook
import org.polyfrost.chatting.hook.GuiChatHook
Expand Down Expand Up @@ -236,6 +233,11 @@ object ChattingConfig : Config(
)
var unfocusedHeight = 180

@HUD(
name = "Chat Window", category = "Chat Window"
)
var chatWindow = ChatWindow()

@Dropdown(
name = "Screenshot Mode", category = "Screenshotting", options = ["Save To System", "Add To Clipboard", "Both"],
description = "What to do when taking a screenshot."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ object ModCompatHooks {

@JvmStatic
val chatPosition
get() = if (isPatcher && PatcherConfig.chatPosition) 12 else 0
get() = 0
// get() = if (isPatcher && PatcherConfig.chatPosition) 12 else 0

@JvmStatic
val betterChatSmoothMessages
Expand Down
44 changes: 23 additions & 21 deletions src/main/resources/mixins.chatting.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
{
"compatibilityLevel": "JAVA_8",
"minVersion": "0.7",
"package": "org.polyfrost.chatting.mixin",
"refmap": "mixins.${id}.refmap.json",
"verbose": true,
"client": [
"ChatLineMixin",
"ClientCommandHandlerMixin",
"EntityPlayerSPMixin",
"GuiChatMixin",
"GuiNewChatAccessor",
"GuiNewChatMapMixin",
"GuiNewChatMixin",
"GuiNewChatMixin_ChatHeight",
"GuiNewChatMixin_ChatSearching",
"GuiNewChatMixin_ChatTabs",
"GuiNewChatMixin_Scrolling",
"GuiNewChatMixin_SmoothMessages",
"GuiNewChatMixin_TextRendering",
"GuiUtilsMixin"
]
"compatibilityLevel": "JAVA_8",
"minVersion": "0.7",
"package": "org.polyfrost.chatting.mixin",
"refmap": "mixins.${id}.refmap.json",
"verbose": true,
"client": [
"ChatLineMixin",
"ClientCommandHandlerMixin",
"EntityPlayerSPMixin",
"GuiChatMixin",
"GuiIngameForgeMixin",
"GuiNewChatAccessor",
"GuiNewChatMapMixin",
"GuiNewChatMixin",
"GuiNewChatMixin_ChatHeight",
"GuiNewChatMixin_ChatSearching",
"GuiNewChatMixin_ChatTabs",
"GuiNewChatMixin_Movable",
"GuiNewChatMixin_Scrolling",
"GuiNewChatMixin_SmoothMessages",
"GuiNewChatMixin_TextRendering",
"GuiUtilsMixin"
]
}

0 comments on commit 54bfa80

Please sign in to comment.