Skip to content

Commit 2677b65

Browse files
author
Evrim Öztamur
committedSep 28, 2020
Add support for newlines in NeoText
1 parent 97c9d1e commit 2677b65

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed
 

Diff for: ‎Dungeoneer/src/com/interrupt/dungeoneer/gfx/GlRenderer.java

+24-13
Original file line numberDiff line numberDiff line change
@@ -789,33 +789,44 @@ public void renderTextBatches() {
789789
// Global text scale modifier. Make a constant?
790790
float baseTextScale = 0.025f;
791791

792+
Color tempColor = new Color();
793+
792794
for (int i = 0; i < textToRender.size; i++) {
793795
DrawableText dT = textToRender.get(i);
794796

795-
float curXPos = 0f;
796-
float textWidth = 0f;
797+
float curXPos = 0f, curZPos = 0f;
797798

798799
// TODO: Handle newline characters?
799800

800-
// Figure out the width of this text
801-
for(int ii = 0; ii < dT.text.length(); ii++) {
802-
BitmapFont.Glyph glyph = font.getData().getGlyph(dT.text.charAt(ii));
803-
textWidth += glyph.width * dT.scale * baseTextScale;
804-
}
801+
GlyphLayout bounds = FontBounds.GetBounds(font, dT.text);
802+
float textWidth = bounds.width * dT.scale * baseTextScale;
803+
float textHeight = bounds.height * dT.scale * baseTextScale;
804+
805+
BitmapFont.Glyph glyph = font.getData().getGlyph('X');
806+
807+
float glyphWidth, glyphHeight = glyph.height * dT.scale * baseTextScale;
805808

806809
// Draw a decal per-glyph for this text
807810
for(int ii = 0; ii < dT.text.length(); ii++) {
808-
BitmapFont.Glyph glyph = font.getData().getGlyph(dT.text.charAt(ii));
811+
char character = dT.text.charAt(ii);
812+
813+
glyph = font.getData().getGlyph(character);
814+
815+
if (glyph == null && character == '\n') { // Newline support is in DrawableText, replaces "\\n" with "\n" there to avoid issues with font boundary calculations.
816+
curXPos = 0;
817+
curZPos += glyphHeight;
818+
continue;
819+
}
809820

810-
float glyphWidth = glyph.width * dT.scale * baseTextScale;
811-
float glyphHeight = glyph.height * dT.scale * baseTextScale;
821+
glyphWidth = glyph.width * dT.scale * baseTextScale;
812822

813823
float tx = dT.parentPosition.x + curXPos;
814824
float ty = dT.parentPosition.y + 0.001f; // Pull out a bit, to place directly on walls
815-
float tz = dT.parentPosition.z + (glyphHeight * 0.5f); // Place font baseline directly on entity origin
825+
float tz = dT.parentPosition.z - curZPos + (glyphHeight * 0.5f); // Place font baseline directly on entity origin
816826

817827
// Center text on origin
818828
tx -= textWidth * 0.5f;
829+
tz += textHeight * 0.5f;
819830

820831
// Offset a tiny bit, because something was doing that in the glyph rendering code
821832
tx += 0.1f * dT.scale;
@@ -857,8 +868,8 @@ public void renderTextBatches() {
857868
if(dT.fullbrite) {
858869
sd.setColor(dT.color.r, dT.color.g, dT.color.b, 1.0f);
859870
} else {
860-
Color lightmap = GetLightmapAt(tx, tz, ty);
861-
sd.setColor(lightmap.r, lightmap.g, lightmap.b, 1.0f);
871+
tempColor.set(GetLightmapAt(tx, tz, ty)).mul(dT.color);
872+
sd.setColor(tempColor.r, tempColor.g, tempColor.b, 1.0f);
862873
}
863874

864875
if (renderingForPicking)

Diff for: ‎Dungeoneer/src/com/interrupt/dungeoneer/gfx/drawables/DrawableText.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public class DrawableText extends Drawable {
1111
public Vector3 parentPosition = new Vector3();
1212
public Vector3 parentRotation = new Vector3();
1313
public Entity.EditorState editorState = Entity.EditorState.hovered;
14-
public Color pickingColor = new Color(Color.BLACK);
14+
public Color pickingColor = Color.BLACK.cpy();
15+
public Color color = Color.WHITE.cpy();
1516

1617
public DrawableText() {
1718
}
@@ -32,6 +33,7 @@ public void update(Entity e) {
3233

3334
if (e instanceof NeoText) {
3435
text = ((NeoText) e).text;
36+
text = text.replace("\\n", "\n"); // Hack in support for newlines in the editor.
3537
color.set(((NeoText) e).textColor);
3638
}
3739
}

0 commit comments

Comments
 (0)