GuiText.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Matrix4x4.h"
  25. #include "Quaternion.h"
  26. #include "RendererTypes.h"
  27. #include "StringUtils.h"
  28. #include "Log.h"
  29. #include "FontResource.h"
  30. namespace crown
  31. {
  32. struct Vector3;
  33. struct Vector2;
  34. struct Color4;
  35. //-------------------------------------------------------------------------
  36. struct VertexData
  37. {
  38. float x;
  39. float y;
  40. float u;
  41. float v;
  42. };
  43. //-------------------------------------------------------------------------
  44. struct IndexData
  45. {
  46. uint16_t a;
  47. uint16_t b;
  48. };
  49. #define UTF8_ACCEPT 0
  50. //-------------------------------------------------------------------------
  51. static const uint8_t s_utf8d[364] =
  52. {
  53. // The first part of the table maps bytes to character classes that
  54. // to reduce the size of the transition table and create bitmasks.
  55. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  56. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  57. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  58. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  59. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  60. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  61. 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  62. 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
  63. // The second part is a transition table that maps a combination
  64. // of a state of the automaton and a character class to a state.
  65. 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
  66. 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
  67. 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
  68. 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
  69. 12,36,12,12,12,12,12,12,12,12,12,12
  70. };
  71. //-------------------------------------------------------------------------
  72. uint32_t utf8_decode(uint32_t* state, uint32_t* code_point, uint8_t character)
  73. {
  74. uint32_t byte = character;
  75. uint32_t type = s_utf8d[byte];
  76. *code_point = (*state != UTF8_ACCEPT) ? (byte & 0x3fu) | (*code_point << 6) : (0xff >> type) & (byte);
  77. *state = s_utf8d[256 + *state + type];
  78. return *state;
  79. }
  80. //-------------------------------------------------------------------------
  81. struct GuiText
  82. {
  83. //-------------------------------------------------------------------------
  84. GuiText(RenderWorld& render_world, Renderer& r, const char* str, const FontResource* fr, uint32_t font_size, const Vector3& pos)
  85. : m_render_world(render_world)
  86. , m_r(r)
  87. , m_resource(fr)
  88. , m_str(str)
  89. , m_font_size(font_size)
  90. , m_pos(pos)
  91. {
  92. MaterialResource* mat = (MaterialResource*) device()->resource_manager()->data(m_resource->material());
  93. m_material = m_render_world.create_material(mat);
  94. }
  95. //-------------------------------------------------------------------------
  96. ~GuiText()
  97. {
  98. m_render_world.destroy_material(m_material);
  99. }
  100. //-------------------------------------------------------------------------
  101. void update(const char* str, uint32_t font_size, const Vector3& pos)
  102. {
  103. m_str = str;
  104. m_font_size = font_size;
  105. m_pos = pos;
  106. }
  107. //-------------------------------------------------------------------------
  108. void render(UniformId uniform)
  109. {
  110. Material* material = m_render_world.lookup_material(m_material);
  111. material->bind(m_r, uniform);
  112. const char* str = m_str.c_str();
  113. const float scale = ((float)m_font_size / (float)m_resource->font_size());
  114. TransientVertexBuffer vb;
  115. TransientIndexBuffer ib;
  116. m_r.reserve_transient_vertex_buffer(&vb, 4 * string::strlen(str), VertexFormat::P2_T2);
  117. m_r.reserve_transient_index_buffer(&ib, 6 * string::strlen(str));
  118. uint16_t index = 0;
  119. float x_pen_advance = 0.0f;
  120. float y_pen_advance = 0.0f;
  121. uint32_t state = 0;
  122. uint32_t code_point = 0;
  123. for (uint32_t i = 0; i < string::strlen(str); i++)
  124. {
  125. switch (str[i])
  126. {
  127. case '\n':
  128. {
  129. x_pen_advance = 0.0f;
  130. y_pen_advance += m_resource->font_size();
  131. continue;
  132. }
  133. case '\t':
  134. {
  135. x_pen_advance += m_font_size * 4;
  136. continue;
  137. }
  138. }
  139. if (utf8_decode(&state, &code_point, str[i]) == UTF8_ACCEPT)
  140. {
  141. FontGlyphData g = m_resource->get_glyph(code_point);
  142. // Set pen position
  143. m_pen.x = m_pos.x + g.x_offset;
  144. m_pen.y = m_pos.y + (g.height - g.y_offset);
  145. // Position coords
  146. const float x0 = (m_pen.x + x_pen_advance) * scale;
  147. const float y0 = (m_pen.y + y_pen_advance) * scale;
  148. const float x1 = (m_pen.x + g.width + x_pen_advance) * scale;
  149. const float y1 = (m_pen.y - g.height + y_pen_advance) * scale;
  150. // Texture coords
  151. const float u0 = (float) g.x / m_resource->texture_size();
  152. const float v0 = (float) g.y / m_resource->texture_size();
  153. const float u1 = u0 + ((float) g.width) / m_resource->texture_size();
  154. const float v1 = v0 - ((float) g.height) / m_resource->texture_size();
  155. // Fill vertex buffer
  156. (*(VertexData*)(vb.data)).x = x0;
  157. (*(VertexData*)(vb.data)).y = y0;
  158. (*(VertexData*)(vb.data)).u = u0;
  159. (*(VertexData*)(vb.data)).v = v1;
  160. vb.data += sizeof(VertexData);
  161. (*(VertexData*)(vb.data)).x = x1;
  162. (*(VertexData*)(vb.data)).y = y0;
  163. (*(VertexData*)(vb.data)).u = u1;
  164. (*(VertexData*)(vb.data)).v = v1;
  165. vb.data += sizeof(VertexData);
  166. (*(VertexData*)(vb.data)).x = x1;
  167. (*(VertexData*)(vb.data)).y = y1;
  168. (*(VertexData*)(vb.data)).u = u1;
  169. (*(VertexData*)(vb.data)).v = v0;
  170. vb.data += sizeof(VertexData);
  171. (*(VertexData*)(vb.data)).x = x0;
  172. (*(VertexData*)(vb.data)).y = y1;
  173. (*(VertexData*)(vb.data)).u = u0;
  174. (*(VertexData*)(vb.data)).v = v0;
  175. vb.data += sizeof(VertexData);
  176. // Fill index buffer
  177. (*(IndexData*)(ib.data)).a = index;
  178. (*(IndexData*)(ib.data)).b = index + 1;
  179. ib.data += sizeof(IndexData);
  180. (*(IndexData*)(ib.data)).a = index + 2;
  181. (*(IndexData*)(ib.data)).b = index;
  182. ib.data += sizeof(IndexData);
  183. (*(IndexData*)(ib.data)).a = index + 2;
  184. (*(IndexData*)(ib.data)).b = index + 3;
  185. ib.data += sizeof(IndexData);
  186. // Advance pen position
  187. x_pen_advance += g.x_advance;
  188. index += 4;
  189. }
  190. }
  191. m_r.set_vertex_buffer(vb);
  192. m_r.set_index_buffer(ib);
  193. m_r.commit(1);
  194. }
  195. public:
  196. RenderWorld& m_render_world;
  197. Renderer& m_r;
  198. const FontResource* m_resource;
  199. DynamicString m_str;
  200. uint32_t m_font_size;
  201. // position states
  202. Vector3 m_pos;
  203. Vector2 m_pen;
  204. MaterialId m_material;
  205. };
  206. } // namespace crown