Compare commits

..

2 Commits

Author SHA1 Message Date
DaniTheSkunk aa68958a77 added max width wrapping in NodeText 2022-12-13 04:01:33 +00:00
DaniTheSkunk f3a535a433 fixed line height in ttf fonts 2022-12-13 04:01:18 +00:00
6 changed files with 70 additions and 6 deletions

View File

@ -10,6 +10,7 @@ public class TestNode extends BaseGame {
private final ISample kick;
private final NodeSprite sprite;
private final NodeText text;
private final NodeText text2;
float[] axes;
public TestNode() {
@ -17,12 +18,17 @@ public class TestNode extends BaseGame {
kick = loadSample("demoassets/kick.wav");
var font = window.loadFontTileset("fonts/ega-8x14.png");
text = new NodeText(font, "Hello World!");
var font2 = window.loadFontTTF("fonts/LiberationSans-Regular.ttf", 24.0f);
text = new NodeText(font, "Hello World! this is an extra long test meow meow nya nya! nyaaa!");
text2 = new NodeText(font2, "Hello World! this is an extra long test meow meow nya nya! nyaaa!");
text.setMaxSize(new Vec2i(100, 100));
text2.setMaxSize(new Vec2i(100, 100));
sprite = new NodeSprite();
sprite.setTexture(loadTexture("demoassets/card.png"));
sprite.setPos(new Vec2f(100, 100));
//rootNode.add(sprite);
rootNode.add(text);
rootNode.add(text2);
//doThing();
}
@ -38,6 +44,7 @@ public class TestNode extends BaseGame {
@Override
protected void update(double delta) {
text.setPos(new Vec2f(640 + 640 * gamepad.getAxis(0), 360 + 360 * gamepad.getAxis(1)));
text2.setPos(new Vec2f(440 + 640 * gamepad.getAxis(0), 360 + 360 * gamepad.getAxis(1)));
if(gamepad.getButtonPressed(0)) {
playSample(kick);
}

View File

@ -0,0 +1,17 @@
package com.danitheskunk.skunkworks.gfx.font;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public abstract class BaseFont implements IFont {
@Override
public int getStringWidth(String str) {
//todo: figure out later
Charset charset = isCP437() ? StandardCharsets.ISO_8859_1 : StandardCharsets.UTF_8;
int width = 0;
for(var ch : str.getBytes(charset)) {
width += getXAdvance(ch);
}
return width;
}
}

View File

@ -11,7 +11,7 @@ import java.util.HashMap;
import static org.lwjgl.stb.STBTruetype.*;
public class FontTTF implements IFont {
public class FontTTF extends BaseFont {
private final HashMap<Integer, Char> chars;
private final STBTTFontinfo info;
private final int lineHeight;
@ -33,8 +33,8 @@ public class FontTTF implements IFont {
}
//todo: save these
stbtt_GetFontVMetrics(info, ascent, descent, lineGap);
lineHeight = lineGap[0];
this.size = stbtt_ScaleForPixelHeight(info, size);
lineHeight = (int)((ascent[0] - descent[0] + lineGap[0]) * this.size);
//precache ascii characters
for(int i = 32; i < 128; ++i) {

View File

@ -5,7 +5,7 @@ import com.danitheskunk.skunkworks.gfx.ITexture;
import java.util.List;
public class FontTileset implements IFont {
public class FontTileset extends BaseFont {
private final Vec2i charSize;
private final List<ITexture> textures;

View File

@ -6,6 +6,8 @@ import com.danitheskunk.skunkworks.gfx.ITexture;
public interface IFont {
int getLineHeight();
int getStringWidth(String str);
Vec2i getMonospaceSize();
Vec2i getOffset(int ch);

View File

@ -1,27 +1,65 @@
package com.danitheskunk.skunkworks.nodes;
import com.danitheskunk.skunkworks.Vec2i;
import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.font.IFont;
import java.util.ArrayList;
import java.util.List;
public class NodeText extends Node {
private IFont font;
private List<String> lines;
private Vec2i maxSize;
private String text;
public NodeText(IFont font, String text) {
this.font = font;
this.text = text;
maxSize = Vec2i.ZERO;
lines = new ArrayList<>();
setText(text);
}
private void layoutText() {
lines.clear();
if(maxSize == Vec2i.ZERO) {
lines.add(text);
return;
}
var words = text.split(" ");
String currentLine = words[0];
for(int i = 1; i < words.length; ++i) {
String newLine = currentLine + " " + words[i];
if(font.getStringWidth(newLine) > maxSize.getX()) {
lines.add(currentLine);
currentLine = words[i];
} else {
currentLine = newLine;
}
}
lines.add(currentLine);
}
@Override
public void render(IRenderContext rc) {
rc.drawString(getAbsolutePos().toVec2i(), text, font);
Vec2i pos = getAbsolutePos().toVec2i();
for(var line : lines) {
rc.drawString(pos, line, font);
pos = Vec2i.add(pos, new Vec2i(0, font.getLineHeight()));
}
}
public void setFont(IFont font) {
this.font = font;
}
public void setMaxSize(Vec2i maxSize) {
this.maxSize = maxSize;
layoutText();
}
public void setText(String text) {
this.text = text;
layoutText();
}
}