All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.io.github.seba244c.icespire.graphics.gui.TextObject Maven / Gradle / Ivy

package io.github.seba244c.icespire.graphics.gui;

import io.github.seba244c.icespire.ecs.Entity;
import io.github.seba244c.icespire.ecs.components.MeshRenderer;
import io.github.seba244c.icespire.graphics.Material;
import io.github.seba244c.icespire.graphics.Mesh;
import io.github.seba244c.icespire.graphics.Texture;
import io.github.seba244c.icespire.utils.ListingUtils;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * A object with a text as a mesh W.I.P
 * @author Sebsa
 * @since 1.0.4
 */
public class TextObject extends Entity {
	private static final float ZPOS = 0.0f;

    private static final int VERTICES_PER_QUAD = 4;

    private final FontTexture fontTexture;
    
    private String text;

    public TextObject(String text, FontTexture fontTexture) throws Exception {
    	super();
        this.text = text;
        this.fontTexture = fontTexture;
        this.addComponent(new MeshRenderer(buildMesh()));
    }
    
    private Mesh buildMesh() {
        List positions = new ArrayList<>();
        List textCoords = new ArrayList<>();
        float[] normals   = new float[0];
        List indices   = new ArrayList<>();
        char[] characters = text.toCharArray();
        int numChars = characters.length;

        float startx = 0;
        for(int i=0; ii).toArray();
        Mesh mesh = new Mesh(posArr, textCoordsArr, normals, indicesArr);
        mesh.setMaterial(new Material(fontTexture.getTexture()));
        return mesh;
    }

    public String getText() {
        return text;
    }
    
    public void setText(String text) {
        this.text = text;
        this.getComponent(MeshRenderer.class).getMesh().deleteBuffers();
        this.getComponent(MeshRenderer.class).setMesh(buildMesh());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy