aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kr/syeyoung
diff options
context:
space:
mode:
authorsyeyoung <cyong06@naver.com>2021-08-23 23:50:55 +0900
committersyeyoung <cyong06@naver.com>2021-08-23 23:50:55 +0900
commitab1f4fb39c77711c73251fb90c2f6b31d54e0970 (patch)
tree2114d528ec8fbbea4842eb98474d150663b5a472 /src/main/java/kr/syeyoung
parentb5d4933e2c2c39a55127726a66bf0c46ea558cb1 (diff)
downloadSkyblock-Dungeons-Guide-ab1f4fb39c77711c73251fb90c2f6b31d54e0970.tar.gz
Skyblock-Dungeons-Guide-ab1f4fb39c77711c73251fb90c2f6b31d54e0970.tar.bz2
Skyblock-Dungeons-Guide-ab1f4fb39c77711c73251fb90c2f6b31d54e0970.zip
Optimized StyledTextRenderer
Diffstat (limited to 'src/main/java/kr/syeyoung')
-rw-r--r--src/main/java/kr/syeyoung/dungeonsguide/features/text/StyledTextRenderer.java131
1 files changed, 116 insertions, 15 deletions
diff --git a/src/main/java/kr/syeyoung/dungeonsguide/features/text/StyledTextRenderer.java b/src/main/java/kr/syeyoung/dungeonsguide/features/text/StyledTextRenderer.java
index ae30e113..61ef1907 100644
--- a/src/main/java/kr/syeyoung/dungeonsguide/features/text/StyledTextRenderer.java
+++ b/src/main/java/kr/syeyoung/dungeonsguide/features/text/StyledTextRenderer.java
@@ -25,10 +25,14 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.GlStateManager;
+import net.minecraftforge.fml.relauncher.ReflectionHelper;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL14;
import java.awt.*;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -40,12 +44,20 @@ public class StyledTextRenderer {
LEFT, CENTER, RIGHT
}
+ private static final Method renderChar = ReflectionHelper.findMethod(FontRenderer.class, null, new String[] {"renderChar"}, char.class, boolean.class);
+ private static final Method doDraw = ReflectionHelper.findMethod(FontRenderer.class, null, new String[] {"doDraw"}, float.class);
+ private static final Field posX = ReflectionHelper.findField(FontRenderer.class, "posX");
+ private static final Field posY = ReflectionHelper.findField(FontRenderer.class, "posY");
public static List<StyleTextAssociated> drawTextWithStylesAssociated(List<StyledText> texts, int x, int y,int width, Map<String, TextStyle> styleMap, Alignment alignment) {
String[] totalLines = (texts.stream().map( a-> a.getText()).collect(Collectors.joining())+" ").split("\n");
+ GlStateManager.enableBlend();
+ GlStateManager.enableAlpha();
+ GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
+
int currentLine = 0;
FontRenderer fr = Minecraft.getMinecraft().fontRendererObj;
int currX = alignment == Alignment.LEFT ? x : alignment == Alignment.CENTER ? (x+width-fr.getStringWidth(totalLines[currentLine]))/2 : (x+width-fr.getStringWidth(totalLines[currentLine]));
@@ -57,7 +69,12 @@ public class StyledTextRenderer {
String[] lines = st.getText().split("\n");
for (int i = 0; i < lines.length; i++) {
String str = lines[i];
- Dimension d = drawFragmentText(fr, str, ts, currX, currY, false);
+ Dimension d = null;
+ try {
+ d = drawFragmentText(fr, str, ts, currX, currY, false);
+ } catch (InvocationTargetException | IllegalAccessException e) {
+ e.printStackTrace();
+ }
associateds.add(new StyleTextAssociated(st, new Rectangle(currX, currY, d.width, d.height)));
currX += d.width;
if (maxHeightForLine < d.height)
@@ -91,7 +108,12 @@ public class StyledTextRenderer {
String[] lines = st.getText().split("\n");
for (int i = 0; i < lines.length; i++) {
String str = lines[i];
- Dimension d = drawFragmentText(fr, str, ts, currX, currY, true);
+ Dimension d = null;
+ try {
+ d = drawFragmentText(fr, str, ts, currX, currY, true);
+ } catch (InvocationTargetException | IllegalAccessException e) {
+ e.printStackTrace();
+ }
associateds.add(new StyleTextAssociated(st, new Rectangle(currX, currY, d.width, d.height)));
currX += d.width;
if (maxHeightForLine < d.height)
@@ -119,26 +141,105 @@ public class StyledTextRenderer {
private Rectangle rectangle;
}
- public static Dimension drawFragmentText(FontRenderer fr, String content, TextStyle style, int x, int y, boolean stopDraw) {
+ private static Dimension drawFragmentText(FontRenderer fr, String content, TextStyle style, int x, int y, boolean stopDraw) throws InvocationTargetException, IllegalAccessException {
if (stopDraw)
return new Dimension(fr.getStringWidth(content), fr.FONT_HEIGHT);
- Gui.drawRect(x,y, x+fr.getStringWidth(content), y + fr.FONT_HEIGHT, RenderUtils.getColorAt(x,y, style.getBackground()));
+ int bgColor = RenderUtils.getColorAt(x,y, style.getBackground());
+ if ((bgColor & 0xFF000000) != 0)
+ Gui.drawRect(x,y, x+fr.getStringWidth(content), y + fr.FONT_HEIGHT, bgColor);
- GlStateManager.enableBlend();
- GL14.glBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
- GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
- if (!style.getColor().isChroma()) {
- fr.drawString(content, x, y, style.getColor().getRGB(), style.isShadow());
- return new Dimension(fr.getStringWidth(content), fr.FONT_HEIGHT);
- }else {
+
+ posX.set(fr, x+1);
+ posY.set(fr, y+1);
+
+ if (style.isShadow()) {
char[] charArr = content.toCharArray();
- int width = 0;
+ float width = 0;
for (int i = 0; i < charArr.length; i++) {
- fr.drawString(String.valueOf(charArr[i]), x + width, y, RenderUtils.getColorAt(x + width, y, style.getColor()), style.isShadow());
- width += fr.getCharWidth(charArr[i]);
+ int color = RenderUtils.getColorAt(x + width, y, style.getColor());
+ color = (color & 16579836) >> 2 | color & -16777216;
+
+ width +=renderChar(fr, charArr[i], color,true, false, false, false);
+ }
+ }
+ posX.set(fr, x);
+ posY.set(fr, y);
+ {
+ char[] charArr = content.toCharArray();
+ float width = 0;
+ for (int i = 0; i < charArr.length; i++) {
+ int color = RenderUtils.getColorAt(x + width, y, style.getColor());
+
+ width +=renderChar(fr, charArr[i], color, false, false, false, false);
+ }
+ return new Dimension((int) width, fr.FONT_HEIGHT);
+ }
+ }
+
+ private static float renderChar(FontRenderer fr, char character, int color, boolean shadow, boolean randomStyle, boolean boldStyle, boolean italicStyle) throws InvocationTargetException, IllegalAccessException {
+ RenderUtils.GL_SETCOLOR(color);
+ int j = "\u00c0\u00c1\u00c2\u00c8\u00ca\u00cb\u00cd\u00d3\u00d4\u00d5\u00da\u00df\u00e3\u00f5\u011f\u0130\u0131\u0152\u0153\u015e\u015f\u0174\u0175\u017e\u0207\u0000\u0000\u0000\u0000\u0000\u0000\u0000 !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u0000\u00c7\u00fc\u00e9\u00e2\u00e4\u00e0\u00e5\u00e7\u00ea\u00eb\u00e8\u00ef\u00ee\u00ec\u00c4\u00c5\u00c9\u00e6\u00c6\u00f4\u00f6\u00f2\u00fb\u00f9\u00ff\u00d6\u00dc\u00f8\u00a3\u00d8\u00d7\u0192\u00e1\u00ed\u00f3\u00fa\u00f1\u00d1\u00aa\u00ba\u00bf\u00ae\u00ac\u00bd\u00bc\u00a1\u00ab\u00bb\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255d\u255c\u255b\u2510\u2514\u2534\u252c\u251c\u2500\u253c\u255e\u255f\u255a\u2554\u2569\u2566\u2560\u2550\u256c\u2567\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256b\u256a\u2518\u250c\u2588\u2584\u258c\u2590\u2580\u03b1\u03b2\u0393\u03c0\u03a3\u03c3\u03bc\u03c4\u03a6\u0398\u03a9\u03b4\u221e\u2205\u2208\u2229\u2261\u00b1\u2265\u2264\u2320\u2321\u00f7\u2248\u00b0\u2219\u00b7\u221a\u207f\u00b2\u25a0\u0000".indexOf(character);
+
+ if (randomStyle && j != -1)
+ {
+ int k = fr.getCharWidth(character);
+ char c1;
+
+ while (true)
+ {
+ j = fr.fontRandom.nextInt("\u00c0\u00c1\u00c2\u00c8\u00ca\u00cb\u00cd\u00d3\u00d4\u00d5\u00da\u00df\u00e3\u00f5\u011f\u0130\u0131\u0152\u0153\u015e\u015f\u0174\u0175\u017e\u0207\u0000\u0000\u0000\u0000\u0000\u0000\u0000 !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u0000\u00c7\u00fc\u00e9\u00e2\u00e4\u00e0\u00e5\u00e7\u00ea\u00eb\u00e8\u00ef\u00ee\u00ec\u00c4\u00c5\u00c9\u00e6\u00c6\u00f4\u00f6\u00f2\u00fb\u00f9\u00ff\u00d6\u00dc\u00f8\u00a3\u00d8\u00d7\u0192\u00e1\u00ed\u00f3\u00fa\u00f1\u00d1\u00aa\u00ba\u00bf\u00ae\u00ac\u00bd\u00bc\u00a1\u00ab\u00bb\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255d\u255c\u255b\u2510\u2514\u2534\u252c\u251c\u2500\u253c\u255e\u255f\u255a\u2554\u2569\u2566\u2560\u2550\u256c\u2567\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256b\u256a\u2518\u250c\u2588\u2584\u258c\u2590\u2580\u03b1\u03b2\u0393\u03c0\u03a3\u03c3\u03bc\u03c4\u03a6\u0398\u03a9\u03b4\u221e\u2205\u2208\u2229\u2261\u00b1\u2265\u2264\u2320\u2321\u00f7\u2248\u00b0\u2219\u00b7\u221a\u207f\u00b2\u25a0\u0000".length());
+ c1 = "\u00c0\u00c1\u00c2\u00c8\u00ca\u00cb\u00cd\u00d3\u00d4\u00d5\u00da\u00df\u00e3\u00f5\u011f\u0130\u0131\u0152\u0153\u015e\u015f\u0174\u0175\u017e\u0207\u0000\u0000\u0000\u0000\u0000\u0000\u0000 !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u0000\u00c7\u00fc\u00e9\u00e2\u00e4\u00e0\u00e5\u00e7\u00ea\u00eb\u00e8\u00ef\u00ee\u00ec\u00c4\u00c5\u00c9\u00e6\u00c6\u00f4\u00f6\u00f2\u00fb\u00f9\u00ff\u00d6\u00dc\u00f8\u00a3\u00d8\u00d7\u0192\u00e1\u00ed\u00f3\u00fa\u00f1\u00d1\u00aa\u00ba\u00bf\u00ae\u00ac\u00bd\u00bc\u00a1\u00ab\u00bb\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556\u2555\u2563\u2551\u2557\u255d\u255c\u255b\u2510\u2514\u2534\u252c\u251c\u2500\u253c\u255e\u255f\u255a\u2554\u2569\u2566\u2560\u2550\u256c\u2567\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256b\u256a\u2518\u250c\u2588\u2584\u258c\u2590\u2580\u03b1\u03b2\u0393\u03c0\u03a3\u03c3\u03bc\u03c4\u03a6\u0398\u03a9\u03b4\u221e\u2205\u2208\u2229\u2261\u00b1\u2265\u2264\u2320\u2321\u00f7\u2248\u00b0\u2219\u00b7\u221a\u207f\u00b2\u25a0\u0000".charAt(j);
+
+ if (k == fr.getCharWidth(c1))
+ {
+ break;
+ }
+ }
+
+ character = c1;
+ }
+
+ float f1 = j == -1 || fr.getUnicodeFlag() ? 0.5f : 1f;
+ boolean flag = (character == 0 || j == -1 || fr.getUnicodeFlag()) && shadow;
+
+ if (flag)
+ {
+ posX.set(fr,(float) posX.get(fr) - f1);
+ posY.set(fr,(float) posY.get(fr) - f1);
+ }
+
+ float f = (float) renderChar.invoke(fr, character, italicStyle);
+
+ if (flag)
+ {
+ posX.set(fr,(float) posX.get(fr) + f1);
+ posY.set(fr,(float) posY.get(fr) + f1);
+ }
+
+ if (boldStyle)
+ {
+ posX.set(fr,(float) posX.get(fr) + f1);
+
+ if (flag)
+ {
+ posX.set(fr,(float) posX.get(fr) - f1);
+ posY.set(fr,(float) posY.get(fr) - f1);
}
- return new Dimension(width, fr.FONT_HEIGHT);
+
+ renderChar.invoke(fr, character, italicStyle);
+ posX.set(fr,(float) posX.get(fr) - f1);
+
+ if (flag)
+ {
+ posX.set(fr,(float) posX.get(fr) + f1);
+ posY.set(fr,(float) posY.get(fr) + f1);
+ }
+
+ ++f;
}
+ doDraw.invoke(fr, f);
+
+ return f;
}
}