199 lines
5.9 KiB
Java
199 lines
5.9 KiB
Java
package com.github.hydos.ginger.engine.opengl.font;
|
|
|
|
import org.joml.*;
|
|
|
|
/** Represents a piece of text in the game. */
|
|
public class GUIText
|
|
{
|
|
private String textString;
|
|
private float fontSize;
|
|
private int textMeshVao;
|
|
private int vertexCount;
|
|
private Vector3f colour = new Vector3f(0f, 0f, 0f), outlineColour;
|
|
private float borderEdge = 0.1f, borderWidth = 0;
|
|
private Vector2f offset = new Vector2f(0, 0);
|
|
private Vector2f position;
|
|
private float lineMaxSize;
|
|
private int numberOfLines;
|
|
private FontType font;
|
|
private boolean centerText = false;
|
|
public String textID;
|
|
|
|
/** Creates a new text, loads the text's quads into a VAO, and adds the text
|
|
* to the screen.
|
|
*
|
|
* @param text
|
|
* - the text.
|
|
* @param fontSize
|
|
* - the font size of the text, where a font size of 1 is the
|
|
* default size.
|
|
* @param font
|
|
* - the font that this text should use.
|
|
* @param position
|
|
* - the position on the screen where the top left corner of the
|
|
* text should be rendered. The top left corner of the screen is
|
|
* (0, 0) and the bottom right is (1, 1).
|
|
* @param maxLineLength
|
|
* - basically the width of the virtual page in terms of screen
|
|
* width (1 is full screen width, 0.5 is half the width of the
|
|
* screen, etc.) Text cannot go off the edge of the page, so if
|
|
* the text is longer than this length it will go onto the next
|
|
* line. When text is centered it is centered into the middle of
|
|
* the line, based on this line length value.
|
|
* @param centered
|
|
* - whether the text should be centered or not. */
|
|
public GUIText(String text, float fontSize, FontType font, Vector2f position, float maxLineLength,
|
|
boolean centered)
|
|
{
|
|
this.textString = text;
|
|
this.fontSize = fontSize;
|
|
this.font = font;
|
|
this.position = position;
|
|
this.outlineColour = colour;
|
|
this.lineMaxSize = maxLineLength;
|
|
this.centerText = centered;
|
|
TextMaster.loadText(this);
|
|
}
|
|
|
|
public float getBorderEdge()
|
|
{ return borderEdge; }
|
|
|
|
public float getBorderWidth()
|
|
{ return borderWidth; }
|
|
|
|
/** @return the colour of the text. */
|
|
public Vector3f getColour()
|
|
{ return colour; }
|
|
|
|
/** @return The font used by this text. */
|
|
public FontType getFont()
|
|
{ return font; }
|
|
|
|
/** @return the font size of the text (a font size of 1 is normal). */
|
|
protected float getFontSize()
|
|
{ return fontSize; }
|
|
|
|
/** @return The maximum length of a line of this text. */
|
|
protected float getMaxLineSize()
|
|
{ return lineMaxSize; }
|
|
|
|
/** @return the ID of the text's VAO, which contains all the vertex data for
|
|
* the quads on which the text will be rendered. */
|
|
public int getMesh()
|
|
{ return textMeshVao; }
|
|
|
|
/** @return The number of lines of text. This is determined when the text is
|
|
* loaded, based on the length of the text and the max line length
|
|
* that is set. */
|
|
public int getNumberOfLines()
|
|
{ return numberOfLines; }
|
|
|
|
public Vector2f getOffset()
|
|
{ return offset; }
|
|
|
|
public Vector3f getOutlineColour()
|
|
{ return outlineColour; }
|
|
|
|
/** @return The position of the top-left corner of the text in screen-space.
|
|
* (0, 0) is the top left corner of the screen, (1, 1) is the bottom
|
|
* right. */
|
|
public Vector2f getPosition()
|
|
{ return position; }
|
|
|
|
/** @return The string of text. */
|
|
protected String getTextString()
|
|
{ return textString; }
|
|
|
|
/** @return The total number of vertices of all the text's quads. */
|
|
public int getVertexCount()
|
|
{ return this.vertexCount; }
|
|
|
|
/** @return {@code true} if the text should be centered. */
|
|
protected boolean isCentered()
|
|
{ return centerText; }
|
|
|
|
/** Remove the text from the screen. */
|
|
public void remove()
|
|
{ TextMaster.removeText(this); }
|
|
|
|
public void setBorderEdge(float borderEdge)
|
|
{ this.borderEdge = borderEdge; }
|
|
|
|
public void setBorderWidth(float borderWidth)
|
|
{ this.borderWidth = borderWidth; }
|
|
|
|
public void setCenterText(boolean centerText)
|
|
{ this.centerText = centerText; }
|
|
|
|
/** Set the colour of the text.
|
|
*
|
|
* @param r
|
|
* - red value, between 0 and 1.
|
|
* @param g
|
|
* - green value, between 0 and 1.
|
|
* @param b
|
|
* - blue value, between 0 and 1. */
|
|
public void setColour(float r, float g, float b)
|
|
{ colour.set(r, g, b); }
|
|
|
|
public void setColour(Vector3f colour)
|
|
{ this.colour = colour; }
|
|
|
|
public void setFont(FontType font)
|
|
{ this.font = font; }
|
|
|
|
public void setFontSize(float fontSize)
|
|
{ this.fontSize = fontSize; }
|
|
|
|
public void setLineMaxSize(float lineMaxSize)
|
|
{ this.lineMaxSize = lineMaxSize; }
|
|
|
|
/** Set the VAO and vertex count for this text.
|
|
*
|
|
* @param vao
|
|
* - the VAO containing all the vertex data for the quads on
|
|
* which the text will be rendered.
|
|
* @param verticesCount
|
|
* - the total number of vertices in all of the quads. */
|
|
public void setMeshInfo(int vao, int verticesCount)
|
|
{
|
|
this.textMeshVao = vao;
|
|
this.vertexCount = verticesCount;
|
|
}
|
|
|
|
/** Sets the number of lines that this text covers (method used only in
|
|
* loading).
|
|
*
|
|
* @param number */
|
|
protected void setNumberOfLines(int number)
|
|
{ this.numberOfLines = number; }
|
|
|
|
public void setOffset(Vector2f offset)
|
|
{ this.offset = offset; }
|
|
|
|
public void setOutlineColour(Vector3f outlineColour)
|
|
{ this.outlineColour = outlineColour; }
|
|
|
|
public void setPosition(Vector2f position)
|
|
{ this.position = position; }
|
|
|
|
public void setText(String string)
|
|
{
|
|
if (string != this.textString)
|
|
{
|
|
this.remove();
|
|
this.textString = string;
|
|
TextMaster.loadText(this);
|
|
}
|
|
}
|
|
|
|
public void setTextMeshVao(int textMeshVao)
|
|
{ this.textMeshVao = textMeshVao; }
|
|
|
|
public void setTextString(String textString)
|
|
{ this.textString = textString; }
|
|
|
|
public void setVertexCount(int vertexCount)
|
|
{ this.vertexCount = vertexCount; }
|
|
}
|