GUIText.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package eu.tankernn.gameEngine.loader.font;
  2. import org.lwjgl.util.vector.Vector2f;
  3. import org.lwjgl.util.vector.Vector3f;
  4. import eu.tankernn.gameEngine.loader.Loader;
  5. import eu.tankernn.gameEngine.renderEngine.Vao;
  6. /**
  7. * Represents a piece of text in the game.
  8. *
  9. * @author Karl
  10. *
  11. */
  12. public class GUIText {
  13. private String textString;
  14. private boolean dirty;
  15. private float fontSize;
  16. private Vao textMeshVao;
  17. private int vertexCount;
  18. private Vector3f colour = new Vector3f(0f, 0f, 0f);
  19. private Vector2f position;
  20. private float lineMaxSize;
  21. private int numberOfLines;
  22. private FontType font;
  23. private boolean centerText = false;
  24. /**
  25. * Creates a new text, loads the text's quads into a VAO, and adds the text
  26. * to the screen.
  27. *
  28. * @param text
  29. * - the text.
  30. * @param fontSize
  31. * - the font size of the text, where a font size of 1 is the
  32. * default size.
  33. * @param font
  34. * - the font that this text should use.
  35. * @param position
  36. * - the position on the screen where the top left corner of the
  37. * text should be rendered. The top left corner of the screen is
  38. * (0, 0) and the bottom right is (1, 1).
  39. * @param maxLineLength
  40. * - basically the width of the virtual page in terms of screen
  41. * width (1 is full screen width, 0.5 is half the width of the
  42. * screen, etc.) Text cannot go off the edge of the page, so if
  43. * the text is longer than this length it will go onto the next
  44. * line. When text is centered it is centered into the middle of
  45. * the line, based on this line length value.
  46. * @param centered
  47. * - whether the text should be centered or not.
  48. */
  49. public GUIText(String text, float fontSize, FontType font, Vector2f position, float maxLineLength,
  50. boolean centered) {
  51. this.textString = text;
  52. this.fontSize = fontSize;
  53. this.font = font;
  54. this.position = position;
  55. this.lineMaxSize = maxLineLength;
  56. this.centerText = centered;
  57. }
  58. /**
  59. * @return The font used by this text.
  60. */
  61. public FontType getFont() {
  62. return font;
  63. }
  64. /**
  65. * Set the colour of the text.
  66. *
  67. * @param r
  68. * - red value, between 0 and 1.
  69. * @param g
  70. * - green value, between 0 and 1.
  71. * @param b
  72. * - blue value, between 0 and 1.
  73. */
  74. public GUIText setColor(float r, float g, float b) {
  75. colour.set(r, g, b);
  76. return this;
  77. }
  78. /**
  79. * @return the colour of the text.
  80. */
  81. public Vector3f getColor() {
  82. return colour;
  83. }
  84. /**
  85. * @return The number of lines of text. This is determined when the text is
  86. * loaded, based on the length of the text and the max line length
  87. * that is set.
  88. */
  89. public int getNumberOfLines() {
  90. return numberOfLines;
  91. }
  92. /**
  93. * @return The position of the top-left corner of the text in screen-space.
  94. * (0, 0) is the top left corner of the screen, (1, 1) is the bottom
  95. * right.
  96. */
  97. public Vector2f getPosition() {
  98. return position;
  99. }
  100. /**
  101. * @return the ID of the text's VAO, which contains all the vertex data for
  102. * the quads on which the text will be rendered.
  103. */
  104. public Vao getMesh() {
  105. return textMeshVao;
  106. }
  107. /**
  108. * Set the VAO and vertex count for this text.
  109. *
  110. * @param vao
  111. * - the VAO containing all the vertex data for the quads on
  112. * which the text will be rendered.
  113. * @param verticesCount
  114. * - the total number of vertices in all of the quads.
  115. */
  116. public void setMeshInfo(Vao vao, int verticesCount) {
  117. this.textMeshVao = vao;
  118. this.vertexCount = verticesCount;
  119. }
  120. /**
  121. * @return The total number of vertices of all the text's quads.
  122. */
  123. public int getVertexCount() {
  124. return this.vertexCount;
  125. }
  126. /**
  127. * @return the font size of the text (a font size of 1 is normal).
  128. */
  129. protected float getFontSize() {
  130. return fontSize;
  131. }
  132. /**
  133. * Sets the number of lines that this text covers (method used only in
  134. * loading).
  135. *
  136. * @param number
  137. */
  138. protected void setNumberOfLines(int number) {
  139. this.numberOfLines = number;
  140. }
  141. /**
  142. * @return {@code true} if the text should be centered.
  143. */
  144. protected boolean isCentered() {
  145. return centerText;
  146. }
  147. /**
  148. * @return The maximum length of a line of this text.
  149. */
  150. protected float getMaxLineSize() {
  151. return lineMaxSize;
  152. }
  153. /**
  154. * @return The string of text.
  155. */
  156. protected String getTextString() {
  157. return textString;
  158. }
  159. public void setText(String text) {
  160. this.textString = text;
  161. this.dirty = true;
  162. }
  163. public boolean isDirty() {
  164. return this.dirty;
  165. }
  166. public void update(Loader loader) {
  167. TextMeshData data = font.loadText(this);
  168. Vao vao = loader.loadToVAO(data.getVertexPositions(), data.getTextureCoords());
  169. this.setMeshInfo(vao, data.getVertexCount());
  170. }
  171. }