Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
chatLine copying
Browse files Browse the repository at this point in the history
  • Loading branch information
CubeWhy committed Jul 31, 2023
1 parent a1b9ac5 commit ac41481
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 2 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = 'org.cubewhy.lunarcn'
version = '1.5-build6-pre3-SNAPSHOT'
version = '1.5-build6-pre4-SNAPSHOT'

println('Java: ' + System.getProperty('java.version') + ' JVM: ' + System.getProperty('java.vm.version') + '(' + System.getProperty('java.vendor') + ') Arch: ' + System.getProperty('os.arch'))
println("Give us stars -> https://github.com/CubeWhy/LiquidLunar")
Expand Down Expand Up @@ -55,6 +55,8 @@ dependencies {
}

implementation 'org.jetbrains:annotations:24.0.1'

implementation 'org.fusesource.jansi:jansi:2.4.0'
}

shadowJar {
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/org/cubewhy/lunarcn/gui/elements/CopyButton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.cubewhy.lunarcn.gui.elements;

import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.util.ResourceLocation;
import org.cubewhy.lunarcn.utils.ClientUtils;
import org.cubewhy.lunarcn.utils.MSTimer;
import org.cubewhy.lunarcn.utils.RenderUtils;
import org.lwjgl.input.Mouse;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;

import static org.cubewhy.lunarcn.utils.ClientUtils.*;

public class CopyButton {
public static boolean hasPressed = false;

public static final ResourceLocation imageCopy = new ResourceLocation("lunarcn/icons/copy.png");

public boolean hovered = false;
public int x;
public int y;

public String text;

public CopyButton(int x, int y, String text) {
this.x = x;
this.y = y;
this.text = text;
}

public void drawButton(int mouseX, int mouseY) {
hovered = RenderUtils.isHovering(mouseX, mouseY, this.x, this.y, this.x + 10, this.y + 10);
// draw image
RenderUtils.drawImage(imageCopy, this.x, this.y, 10, 10);

if (Mouse.isButtonDown(0) ^ hasPressed) {
try {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(this.text), null);
logger.info("Copied text: " + this.text);
hasPressed = Mouse.isButtonDown(0);
} catch (Exception e) {
logger.catching(e); // copy failed
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiNewChat;
import net.minecraft.client.gui.GuiUtilRenderComponents;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.IChatComponent;
import net.minecraft.util.MathHelper;
import org.cubewhy.lunarcn.event.events.ChatEvent;
import org.cubewhy.lunarcn.gui.elements.CopyButton;
import org.cubewhy.lunarcn.module.ModuleManager;
import org.cubewhy.lunarcn.module.impl.dev.ChatConfig;
import org.lwjgl.input.Mouse;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
Expand Down Expand Up @@ -56,7 +60,7 @@ private void setChatLine(IChatComponent chatComponent, int chatLineId, int updat
}

int i = MathHelper.floor_float((float) this.getChatWidth() / this.getChatScale());
List<IChatComponent> list = GuiUtilRenderComponents.splitText(chatComponent, i,fontRenderer, false, false);
List<IChatComponent> list = GuiUtilRenderComponents.splitText(chatComponent, i, fontRenderer, false, false);
boolean flag = this.getChatOpen();

for (IChatComponent ichatcomponent : list) {
Expand All @@ -81,6 +85,90 @@ private void setChatLine(IChatComponent chatComponent, int chatLineId, int updat
}
}

/**
* @author CubeWhy
* @reason add copy button
*/
@Overwrite
public void drawChat(int updateCounter) {
if (this.mc.gameSettings.chatVisibility != EntityPlayer.EnumChatVisibility.HIDDEN) {
int i = this.getLineCount();
boolean flag = false;
int j = 0;
int k = this.drawnChatLines.size();
float f = this.mc.gameSettings.chatOpacity * 0.9F + 0.1F;

if (k > 0) {
if (this.getChatOpen()) {
flag = true;
}

float f1 = this.getChatScale();
int l = MathHelper.ceiling_float_int((float) this.getChatWidth() / f1);
GlStateManager.pushMatrix();
GlStateManager.translate(2.0F, 20.0F, 0.0F);
GlStateManager.scale(f1, f1, 1.0F);

for (int i1 = 0; i1 + this.scrollPos < this.drawnChatLines.size() && i1 < i; ++i1) {
ChatLine chatline = this.drawnChatLines.get(i1 + this.scrollPos);

if (chatline != null) {
int j1 = updateCounter - chatline.getUpdatedCounter();

if (j1 < 200 || flag) {
double d0 = (double) j1 / 200.0D;
d0 = 1.0D - d0;
d0 = d0 * 10.0D;
d0 = MathHelper.clamp_double(d0, 0.0D, 1.0D);
d0 = d0 * d0;
int l1 = (int) (255.0D * d0);

if (flag) {
l1 = 255;
}

l1 = (int) ((float) l1 * f);
++j;

if (l1 > 3) {
int x = 0;
int y = -i1 * 9;
drawRect(x, y - 9, x + l + 4, y, l1 / 2 << 24);
String s = chatline.getChatComponent().getFormattedText();
GlStateManager.enableBlend();
this.mc.fontRendererObj.drawStringWithShadow(s, (float) x, (float) (y - 8), 16777215 + (l1 << 24));

CopyButton copyButton = new CopyButton(fontRenderer.getStringWidth(s) + 20, y - 8, chatline.getChatComponent().getUnformattedText());
copyButton.drawButton(Mouse.getX(), Mouse.getY());

GlStateManager.disableAlpha();
GlStateManager.disableBlend();
}
}
}
}

if (flag) {
int k2 = this.mc.fontRendererObj.FONT_HEIGHT;
GlStateManager.translate(-3.0F, 0.0F, 0.0F);
int l2 = k * k2 + k;
int i3 = j * k2 + j;
int j3 = this.scrollPos * i3 / k;
int k1 = i3 * i3 / l2;

if (l2 != i3) {
int k3 = j3 > 0 ? 170 : 96;
int l3 = this.isScrolled ? 13382451 : 3355562;
drawRect(0, -j3, 2, -j3 - k1, l3 + (k3 << 24));
drawRect(2, -j3, 1, -j3 - k1, 13421772 + (k3 << 24));
}
}

GlStateManager.popMatrix();
}
}
}

@Shadow
public abstract void scroll(int i);

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ac41481

Please sign in to comment.