drawing animated and gradient strings

This commit is contained in:
NK 2023-03-14 14:24:42 +00:00
parent 97df131737
commit 7e5616266a
2 changed files with 57 additions and 0 deletions

View file

@ -202,6 +202,9 @@ public class SniperFlipperEvents {
String lines = "X: " + Math.round(Lilase.mc.thePlayer.posX) + "\n" + "Y: " + Math.round(Lilase.mc.thePlayer.posY) + "\n" + "Z: " + Math.round(Lilase.mc.thePlayer.posZ) + "\n" + time + "\n" + "FPS: " + Minecraft.getDebugFPS() + "\n" + "Day: " + days + "\n" + "Auctions Sniped: " + Lilase.sniper.getAuctionsSniped() + "\n" + "Auctions Posted: " + Lilase.sniper.getAuctionsPosted() + "\n" + "Auctions Flipped: " + Lilase.sniper.getAuctionsFlipped() + "\n"; String lines = "X: " + Math.round(Lilase.mc.thePlayer.posX) + "\n" + "Y: " + Math.round(Lilase.mc.thePlayer.posY) + "\n" + "Z: " + Math.round(Lilase.mc.thePlayer.posZ) + "\n" + time + "\n" + "FPS: " + Minecraft.getDebugFPS() + "\n" + "Day: " + days + "\n" + "Auctions Sniped: " + Lilase.sniper.getAuctionsSniped() + "\n" + "Auctions Posted: " + Lilase.sniper.getAuctionsPosted() + "\n" + "Auctions Flipped: " + Lilase.sniper.getAuctionsFlipped() + "\n";
TextRenderer.drawString(lines, 0, 0, 0.9, GUI_COLOR.getRGB()); TextRenderer.drawString(lines, 0, 0, 0.9, GUI_COLOR.getRGB());
} }
} else if (event.type == RenderGameOverlayEvent.ElementType.CHAT) {
TextRenderer.drawGradientString(Lilase.mc.fontRendererObj, "Lilase", 50, 100, 0x00FBAA, 0xFF3EFC);
TextRenderer.drawAnimatedString(Lilase.mc.fontRendererObj, "Hong Kong No.1", 50, 110, 0x00FBAA, 0xFF3EFC, 0.5f);
} }
} }

View file

@ -2,6 +2,7 @@ package me.night0721.lilase.gui;
import me.night0721.lilase.Lilase; import me.night0721.lilase.Lilase;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.GlStateManager;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
@ -34,4 +35,57 @@ public class TextRenderer {
public void drawCenteredString(String text, int x, int y, double scale) { public void drawCenteredString(String text, int x, int y, double scale) {
drawString(text, x - Minecraft.getMinecraft().fontRendererObj.getStringWidth(text) / 2, y - Minecraft.getMinecraft().fontRendererObj.FONT_HEIGHT / 2, scale); drawString(text, x - Minecraft.getMinecraft().fontRendererObj.getStringWidth(text) / 2, y - Minecraft.getMinecraft().fontRendererObj.FONT_HEIGHT / 2, scale);
} }
public static void drawGradientString(FontRenderer fontRenderer, String text, int x, int y, int colorStart, int colorEnd) {
int textWidth = fontRenderer.getStringWidth(text);
int startX = x - textWidth / 2;
int startY = y - fontRenderer.FONT_HEIGHT / 2;
// calculate color differences
float rDiff = ((colorEnd >> 16) & 255) - ((colorStart >> 16) & 255);
float gDiff = ((colorEnd >> 8) & 255) - ((colorStart >> 8) & 255);
float bDiff = (colorEnd & 255) - (colorStart & 255);
// render text with gradient colors
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
int charWidth = fontRenderer.getCharWidth(c);
float ratio = (float) i / (float) text.length();
int color = ((int) ((float) ((colorStart >> 16) & 255) + rDiff * ratio) << 16) |
((int) ((float) ((colorStart >> 8) & 255) + gDiff * ratio) << 8) |
((int) ((float) (colorStart & 255) + bDiff * ratio));
fontRenderer.drawString(Character.toString(c), startX, startY, color);
startX += charWidth;
}
}
public static void drawAnimatedString(FontRenderer fontRenderer, String text, int x, int y, int startColor, int endColor, float speed) {
float time = (float) (System.currentTimeMillis() % 10000) / 1000.0f;
int textWidth = fontRenderer.getStringWidth(text);
int startX = x - textWidth / 2;
int startY = y - fontRenderer.FONT_HEIGHT / 2;
// calculate color differences
float rDiff = ((endColor >> 16) & 255) - ((startColor >> 16) & 255);
float gDiff = ((endColor >> 8) & 255) - ((startColor >> 8) & 255);
float bDiff = (endColor & 255) - (startColor & 255);
// render text with gradient colors
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
int charWidth = fontRenderer.getCharWidth(c);
float ratio = (float) i / (float) text.length();
// calculate color at current position
float cyclePosition = (time * speed + ratio) % 1.0f;
float r = ((startColor >> 16) & 255) + rDiff * cyclePosition;
float g = ((startColor >> 8) & 255) + gDiff * cyclePosition;
float b = (startColor & 255) + bDiff * cyclePosition;
int color = ((int) r << 16) | ((int) g << 8) | (int) b;
fontRenderer.drawString(Character.toString(c), startX, startY, color);
startX += charWidth;
}
}
} }