added max width wrapping in NodeText
This commit is contained in:
parent
f3a535a433
commit
aa68958a77
|
@ -10,6 +10,7 @@ public class TestNode extends BaseGame {
|
||||||
private final ISample kick;
|
private final ISample kick;
|
||||||
private final NodeSprite sprite;
|
private final NodeSprite sprite;
|
||||||
private final NodeText text;
|
private final NodeText text;
|
||||||
|
private final NodeText text2;
|
||||||
float[] axes;
|
float[] axes;
|
||||||
|
|
||||||
public TestNode() {
|
public TestNode() {
|
||||||
|
@ -17,12 +18,17 @@ public class TestNode extends BaseGame {
|
||||||
|
|
||||||
kick = loadSample("demoassets/kick.wav");
|
kick = loadSample("demoassets/kick.wav");
|
||||||
var font = window.loadFontTileset("fonts/ega-8x14.png");
|
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 = new NodeSprite();
|
||||||
sprite.setTexture(loadTexture("demoassets/card.png"));
|
sprite.setTexture(loadTexture("demoassets/card.png"));
|
||||||
sprite.setPos(new Vec2f(100, 100));
|
sprite.setPos(new Vec2f(100, 100));
|
||||||
//rootNode.add(sprite);
|
//rootNode.add(sprite);
|
||||||
rootNode.add(text);
|
rootNode.add(text);
|
||||||
|
rootNode.add(text2);
|
||||||
//doThing();
|
//doThing();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -38,6 +44,7 @@ public class TestNode extends BaseGame {
|
||||||
@Override
|
@Override
|
||||||
protected void update(double delta) {
|
protected void update(double delta) {
|
||||||
text.setPos(new Vec2f(640 + 640 * gamepad.getAxis(0), 360 + 360 * gamepad.getAxis(1)));
|
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)) {
|
if(gamepad.getButtonPressed(0)) {
|
||||||
playSample(kick);
|
playSample(kick);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,7 +5,7 @@ import com.danitheskunk.skunkworks.gfx.ITexture;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class FontTileset implements IFont {
|
public class FontTileset extends BaseFont {
|
||||||
private final Vec2i charSize;
|
private final Vec2i charSize;
|
||||||
private final List<ITexture> textures;
|
private final List<ITexture> textures;
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,8 @@ import com.danitheskunk.skunkworks.gfx.ITexture;
|
||||||
public interface IFont {
|
public interface IFont {
|
||||||
int getLineHeight();
|
int getLineHeight();
|
||||||
|
|
||||||
|
int getStringWidth(String str);
|
||||||
|
|
||||||
Vec2i getMonospaceSize();
|
Vec2i getMonospaceSize();
|
||||||
|
|
||||||
Vec2i getOffset(int ch);
|
Vec2i getOffset(int ch);
|
||||||
|
|
|
@ -1,27 +1,65 @@
|
||||||
package com.danitheskunk.skunkworks.nodes;
|
package com.danitheskunk.skunkworks.nodes;
|
||||||
|
|
||||||
|
import com.danitheskunk.skunkworks.Vec2i;
|
||||||
import com.danitheskunk.skunkworks.gfx.IRenderContext;
|
import com.danitheskunk.skunkworks.gfx.IRenderContext;
|
||||||
import com.danitheskunk.skunkworks.gfx.font.IFont;
|
import com.danitheskunk.skunkworks.gfx.font.IFont;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class NodeText extends Node {
|
public class NodeText extends Node {
|
||||||
private IFont font;
|
private IFont font;
|
||||||
|
private List<String> lines;
|
||||||
|
private Vec2i maxSize;
|
||||||
private String text;
|
private String text;
|
||||||
|
|
||||||
public NodeText(IFont font, String text) {
|
public NodeText(IFont font, String text) {
|
||||||
this.font = font;
|
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
|
@Override
|
||||||
public void render(IRenderContext rc) {
|
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) {
|
public void setFont(IFont font) {
|
||||||
this.font = font;
|
this.font = font;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setMaxSize(Vec2i maxSize) {
|
||||||
|
this.maxSize = maxSize;
|
||||||
|
layoutText();
|
||||||
|
}
|
||||||
|
|
||||||
public void setText(String text) {
|
public void setText(String text) {
|
||||||
this.text = text;
|
this.text = text;
|
||||||
|
layoutText();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue