Better line breaking

This commit is contained in:
Snownee 2021-03-15 13:00:55 +08:00
parent 2bf9672d92
commit 0ca388141c
5 changed files with 94 additions and 27 deletions

View File

@ -21,6 +21,7 @@ import com.simibubi.create.content.contraptions.components.flywheel.engine.Engin
import com.simibubi.create.content.contraptions.goggles.IHaveGoggleInformation;
import com.simibubi.create.content.curiosities.tools.AllToolTiers;
import com.simibubi.create.foundation.item.ItemDescription.Palette;
import com.simibubi.create.foundation.utility.FontHelper;
import com.simibubi.create.foundation.utility.Lang;
import net.minecraft.client.Minecraft;
@ -88,30 +89,8 @@ public class TooltipHelper {
words.add(word);
}
// Apply hard wrap
FontRenderer font = Minecraft.getInstance().fontRenderer;
List<String> lines = new LinkedList<>();
StringBuilder currentLine = new StringBuilder();
int width = 0;
for (String word : words) {
int newWidth = font.getStringWidth(word);
if (width + newWidth > maxWidthPerLine) {
if (width > 0) {
String line = currentLine.toString();
lines.add(line);
currentLine = new StringBuilder();
width = 0;
} else {
lines.add(word);
continue;
}
}
currentLine.append(word);
width += newWidth;
}
if (width > 0) {
lines.add(currentLine.toString());
}
List<String> lines = FontHelper.cutString(font, markedUp, maxWidthPerLine);
// Format
String lineStart = Strings.repeat(" ", indent);

View File

@ -25,6 +25,7 @@ import com.simibubi.create.foundation.ponder.content.PonderTagScreen;
import com.simibubi.create.foundation.ponder.ui.PonderButton;
import com.simibubi.create.foundation.renderState.SuperRenderTypeBuffer;
import com.simibubi.create.foundation.utility.ColorHelper;
import com.simibubi.create.foundation.utility.FontHelper;
import com.simibubi.create.foundation.utility.Iterate;
import com.simibubi.create.foundation.utility.Lang;
import com.simibubi.create.foundation.utility.animation.LerpedFloat;
@ -486,7 +487,7 @@ public class PonderUI extends AbstractSimiScreen {
RenderSystem.translated(x, y, 0);
RenderSystem.rotatef(indexDiff * -75, 1, 0, 0);
RenderSystem.translated(0, 0, 5);
font.drawSplitString(title, 0, 0, left.x - 51, ColorHelper.applyAlpha(textColor, 1 - indexDiff));
FontHelper.drawSplitString(font, title, 0, 0, left.x - 51, ColorHelper.applyAlpha(textColor, 1 - indexDiff));
RenderSystem.popMatrix();
if (chapter != null) {

View File

@ -16,6 +16,7 @@ import com.simibubi.create.foundation.ponder.PonderUI;
import com.simibubi.create.foundation.ponder.ui.ChapterLabel;
import com.simibubi.create.foundation.ponder.ui.LayoutHelper;
import com.simibubi.create.foundation.ponder.ui.PonderButton;
import com.simibubi.create.foundation.utility.FontHelper;
import com.simibubi.create.foundation.utility.Lang;
import net.minecraft.block.Block;
@ -197,7 +198,7 @@ public class PonderTagScreen extends AbstractSimiScreen {
PonderUI.renderBox(x - 3, y - 3, w + 6, h + 6, false);
RenderSystem.translated(0, 0, 100);
font.drawSplitString(desc, x, y, w, 0xeeeeee);
FontHelper.drawSplitString(font, desc, x, y, w, 0xeeeeee);
RenderSystem.popMatrix();
}

View File

@ -10,6 +10,7 @@ import com.simibubi.create.foundation.ponder.PonderScene;
import com.simibubi.create.foundation.ponder.PonderUI;
import com.simibubi.create.foundation.ponder.content.PonderPalette;
import com.simibubi.create.foundation.utility.ColorHelper;
import com.simibubi.create.foundation.utility.FontHelper;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec2f;
@ -120,8 +121,8 @@ public class TextWindowElement extends AnimatedOverlayElement {
RenderSystem.popMatrix();
}
screen.getFontRenderer()
.drawSplitString(bakedText, targetX - 10, 3, textWidth, ColorHelper.applyAlpha(brighterColor, fade));
FontHelper.drawSplitString(screen.getFontRenderer(), bakedText, targetX - 10, 3, textWidth,
ColorHelper.applyAlpha(brighterColor, fade));
RenderSystem.popMatrix();
}

View File

@ -0,0 +1,85 @@
package com.simibubi.create.foundation.utility;
import java.text.BreakIterator;
import java.util.LinkedList;
import java.util.List;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.Matrix4f;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.TransformationMatrix;
import net.minecraftforge.client.MinecraftForgeClient;
public final class FontHelper {
private FontHelper() {
}
public static List<String> cutString(FontRenderer font, String text, int maxWidthPerLine) {
// Split words
List<String> words = new LinkedList<>();
BreakIterator iterator = BreakIterator.getLineInstance(MinecraftForgeClient.getLocale());
iterator.setText(text);
int start = iterator.first();
for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next()) {
String word = text.substring(start, end);
words.add(word);
}
// Apply hard wrap
List<String> lines = new LinkedList<>();
StringBuilder currentLine = new StringBuilder();
int width = 0;
for (String word : words) {
int newWidth = font.getStringWidth(word);
if (width + newWidth > maxWidthPerLine) {
if (width > 0) {
String line = currentLine.toString();
lines.add(line);
currentLine = new StringBuilder();
width = 0;
} else {
lines.add(word);
continue;
}
}
currentLine.append(word);
width += newWidth;
}
if (width > 0) {
lines.add(currentLine.toString());
}
return lines;
}
public static void drawSplitString(FontRenderer font, String text, int x, int y, int width, int color) {
List<String> list = cutString(font, text, width);
Matrix4f matrix4f = TransformationMatrix.identity().getMatrix();
for (String s : list) {
float f = (float) x;
if (font.getBidiFlag()) {
int i = font.getStringWidth(font.bidiReorder(s));
f += (float) (width - i);
}
draw(font, s, f, (float) y, color, matrix4f, false);
y += 9;
}
}
private static int draw(FontRenderer font, String p_228078_1_, float p_228078_2_, float p_228078_3_,
int p_228078_4_, Matrix4f p_228078_5_, boolean p_228078_6_) {
if (p_228078_1_ == null) {
return 0;
} else {
IRenderTypeBuffer.Impl irendertypebuffer$impl = IRenderTypeBuffer
.immediate(Tessellator.getInstance().getBuffer());
int i = font.draw(p_228078_1_, p_228078_2_, p_228078_3_, p_228078_4_, p_228078_6_, p_228078_5_,
irendertypebuffer$impl, false, 0, 15728880);
irendertypebuffer$impl.draw();
return i;
}
}
}