Compare commits

..

No commits in common. "aa68958a772bebffe426255d3118d22565c9369c" and "6d6f4950ef52bc776185b49135787ee280949090" have entirely different histories.

6 changed files with 6 additions and 70 deletions

View File

@ -10,7 +10,6 @@ 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() {
@ -18,17 +17,12 @@ public class TestNode extends BaseGame {
kick = loadSample("demoassets/kick.wav");
var font = window.loadFontTileset("fonts/ega-8x14.png");
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));
text = new NodeText(font, "Hello World!");
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();
}
@ -44,7 +38,6 @@ 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

@ -1,17 +0,0 @@
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 extends BaseFont {
public class FontTTF implements IFont {
private final HashMap<Integer, Char> chars;
private final STBTTFontinfo info;
private final int lineHeight;
@ -33,8 +33,8 @@ public class FontTTF extends BaseFont {
}
//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 extends BaseFont {
public class FontTileset implements IFont {
private final Vec2i charSize;
private final List<ITexture> textures;

View File

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

View File

@ -1,65 +1,27 @@
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;
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);
this.text = text;
}
@Override
public void render(IRenderContext rc) {
Vec2i pos = getAbsolutePos().toVec2i();
for(var line : lines) {
rc.drawString(pos, line, font);
pos = Vec2i.add(pos, new Vec2i(0, font.getLineHeight()));
}
rc.drawString(getAbsolutePos().toVec2i(), text, font);
}
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();
}
}