Gui.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #include "Gui.h"
  24. #include "Assert.h"
  25. #include "Renderer.h"
  26. #include "Vector3.h"
  27. #include "Vector2.h"
  28. #include "GuiResource.h"
  29. #include "RenderWorld.h"
  30. #include "RendererTypes.h"
  31. #include "Vector3.h"
  32. #include "Vector2.h"
  33. #include "Color4.h"
  34. #include "FontResource.h"
  35. #include "Device.h"
  36. #include "OsWindow.h"
  37. #include "ResourceManager.h"
  38. #include "TextureResource.h"
  39. #include "MaterialResource.h"
  40. namespace crown
  41. {
  42. using namespace matrix4x4;
  43. //-------------------------------------------------------------------------
  44. struct VertexData
  45. {
  46. float x;
  47. float y;
  48. float u;
  49. float v;
  50. };
  51. //-------------------------------------------------------------------------
  52. struct IndexData
  53. {
  54. uint16_t a;
  55. uint16_t b;
  56. };
  57. #define UTF8_ACCEPT 0
  58. //-------------------------------------------------------------------------
  59. static const uint8_t s_utf8d[364] =
  60. {
  61. // The first part of the table maps bytes to character classes that
  62. // to reduce the size of the transition table and create bitmasks.
  63. 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,
  64. 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,
  65. 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,
  66. 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,
  67. 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,
  68. 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,
  69. 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,
  70. 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,
  71. // The second part is a transition table that maps a combination
  72. // of a state of the automaton and a character class to a state.
  73. 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
  74. 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
  75. 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
  76. 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
  77. 12,36,12,12,12,12,12,12,12,12,12,12
  78. };
  79. //-------------------------------------------------------------------------
  80. static uint32_t utf8_decode(uint32_t* state, uint32_t* code_point, uint8_t character)
  81. {
  82. uint32_t byte = character;
  83. uint32_t type = s_utf8d[byte];
  84. *code_point = (*state != UTF8_ACCEPT) ? (byte & 0x3fu) | (*code_point << 6) : (0xff >> type) & (byte);
  85. *state = s_utf8d[256 + *state + type];
  86. return *state;
  87. }
  88. //-----------------------------------------------------------------------------
  89. Gui::Gui(RenderWorld& render_world, uint16_t width, uint16_t height)
  90. : m_render_world(render_world)
  91. , m_width(width)
  92. , m_height(height)
  93. , m_pose(matrix4x4::IDENTITY)
  94. {
  95. set_orthographic_rh(m_projection, 0, width, 0, height, -0.01f, 100.0f);
  96. }
  97. //-----------------------------------------------------------------------------
  98. const GuiId Gui::id() const
  99. {
  100. return m_id;
  101. }
  102. //-----------------------------------------------------------------------------
  103. void Gui::set_id(const GuiId id)
  104. {
  105. m_id = id;
  106. }
  107. //-----------------------------------------------------------------------------
  108. Vector2 Gui::resolution() const
  109. {
  110. return Vector2(m_width, m_height);
  111. }
  112. //-----------------------------------------------------------------------------
  113. void Gui::move(const Vector2& pos)
  114. {
  115. set_identity(m_pose);
  116. set_translation(m_pose, Vector3(pos.x, pos.y, 0));
  117. }
  118. //-----------------------------------------------------------------------------
  119. Vector2 Gui::screen_to_gui(const Vector2& pos)
  120. {
  121. return Vector2(pos.x, m_height - pos.y);
  122. }
  123. //-----------------------------------------------------------------------------
  124. void Gui::draw_rectangle(const Vector3& pos, const Vector2& size, const Color4& color)
  125. {
  126. Renderer* r = device()->renderer();
  127. TransientVertexBuffer tvb;
  128. TransientIndexBuffer tib;
  129. r->reserve_transient_vertex_buffer(&tvb, 4, VertexFormat::P2);
  130. r->reserve_transient_index_buffer(&tib, 6);
  131. float* verts = (float*) tvb.data;
  132. verts[0] = pos.x;
  133. verts[1] = pos.y;
  134. verts[2] = pos.x + size.x;
  135. verts[3] = pos.y;
  136. verts[4] = pos.x + size.x;
  137. verts[5] = pos.y + size.y;
  138. verts[6] = pos.x;
  139. verts[7] = pos.y + size.y;
  140. uint16_t* inds = (uint16_t*) tib.data;
  141. inds[0] = 0;
  142. inds[1] = 1;
  143. inds[2] = 2;
  144. inds[3] = 0;
  145. inds[4] = 2;
  146. inds[5] = 3;
  147. r->set_layer_view(1, matrix4x4::IDENTITY);
  148. r->set_layer_projection(1, m_projection);
  149. r->set_layer_viewport(1, 0, 0, m_width, m_height);
  150. r->set_state(STATE_COLOR_WRITE
  151. | STATE_CULL_CW
  152. | STATE_BLEND_EQUATION_ADD
  153. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  154. r->set_pose(m_pose);
  155. r->set_program(render_world_globals::default_color_program());
  156. r->set_uniform(render_world_globals::default_color_uniform(), UniformType::FLOAT_4, color4::to_float_ptr(color), 1);
  157. r->set_vertex_buffer(tvb);
  158. r->set_index_buffer(tib);
  159. r->commit(1, (int32_t) pos.z);
  160. }
  161. //-----------------------------------------------------------------------------
  162. void Gui::draw_image(const char* material, const Vector3& pos, const Vector2& size, const Color4& color)
  163. {
  164. Renderer* r = device()->renderer();
  165. TransientVertexBuffer tvb;
  166. TransientIndexBuffer tib;
  167. r->reserve_transient_vertex_buffer(&tvb, 4, VertexFormat::P2_T2);
  168. r->reserve_transient_index_buffer(&tib, 6);
  169. float* verts = (float*) tvb.data;
  170. verts[0] = pos.x;
  171. verts[1] = pos.y;
  172. verts[2] = 0;
  173. verts[3] = 0;
  174. verts[4] = pos.x + size.x;
  175. verts[5] = pos.y;
  176. verts[6] = 1;
  177. verts[7] = 0;
  178. verts[8] = pos.x + size.x;
  179. verts[9] = pos.y + size.y;
  180. verts[10] = 1;
  181. verts[11] = 1;
  182. verts[12] = pos.x;
  183. verts[13] = pos.y + size.y;
  184. verts[14] = 0;
  185. verts[15] = 1;
  186. uint16_t* inds = (uint16_t*) tib.data;
  187. inds[0] = 0;
  188. inds[1] = 1;
  189. inds[2] = 2;
  190. inds[3] = 0;
  191. inds[4] = 2;
  192. inds[5] = 3;
  193. const MaterialResource* mr = (MaterialResource*) device()->resource_manager()->get("material", material);
  194. const TextureResource* tr = (TextureResource*) device()->resource_manager()->get(mr->get_texture_layer(0));
  195. r->set_layer_view(1, matrix4x4::IDENTITY);
  196. r->set_layer_projection(1, m_projection);
  197. r->set_layer_viewport(1, 0, 0, m_width, m_height);
  198. r->set_state(STATE_COLOR_WRITE
  199. | STATE_CULL_CW
  200. | STATE_BLEND_EQUATION_ADD
  201. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  202. r->set_pose(m_pose);
  203. r->set_program(render_world_globals::default_texture_program());
  204. r->set_texture(0, render_world_globals::default_albedo_uniform(), tr->texture(),
  205. TEXTURE_FILTER_LINEAR | TEXTURE_WRAP_U_CLAMP_REPEAT | TEXTURE_WRAP_V_CLAMP_REPEAT);
  206. r->set_uniform(render_world_globals::default_color_uniform(), UniformType::FLOAT_4, color4::to_float_ptr(color), 1);
  207. r->set_vertex_buffer(tvb);
  208. r->set_index_buffer(tib);
  209. r->commit(1, (int32_t) pos.z);
  210. }
  211. //-----------------------------------------------------------------------------
  212. void Gui::draw_text(const char* str, const char* font, uint32_t font_size, const Vector3& pos, const Color4& color)
  213. {
  214. Renderer* r = device()->renderer();
  215. const FontResource* resource = (FontResource*) device()->resource_manager()->get("font", font);
  216. Vector2 m_pen;
  217. const float scale = ((float)font_size / (float)resource->font_size());
  218. const uint32_t str_len = string::strlen(str);
  219. TransientVertexBuffer vb;
  220. TransientIndexBuffer ib;
  221. r->reserve_transient_vertex_buffer(&vb, 4 * str_len, VertexFormat::P2_T2);
  222. r->reserve_transient_index_buffer(&ib, 6 * str_len);
  223. uint16_t index = 0;
  224. float x_pen_advance = 0.0f;
  225. float y_pen_advance = 0.0f;
  226. uint32_t state = 0;
  227. uint32_t code_point = 0;
  228. for (uint32_t i = 0; i < str_len; i++)
  229. {
  230. switch (str[i])
  231. {
  232. case '\n':
  233. {
  234. x_pen_advance = 0.0f;
  235. y_pen_advance -= resource->font_size();
  236. continue;
  237. }
  238. case '\t':
  239. {
  240. x_pen_advance += font_size * 4;
  241. continue;
  242. }
  243. }
  244. if (utf8_decode(&state, &code_point, str[i]) == UTF8_ACCEPT)
  245. {
  246. FontGlyphData g = resource->get_glyph(code_point);
  247. const float baseline = g.height - g.y_offset;
  248. // Set pen position
  249. m_pen.x = pos.x + g.x_offset;
  250. m_pen.y = pos.y - baseline;
  251. // Position coords
  252. const float x0 = (m_pen.x + x_pen_advance) * scale;
  253. const float y0 = (m_pen.y + y_pen_advance) * scale;
  254. const float x1 = (m_pen.x + g.width + x_pen_advance) * scale;
  255. const float y1 = (m_pen.y + g.height + y_pen_advance) * scale;
  256. // Texture coords
  257. const float u0 = (float) g.x / 512;
  258. const float v0 = (float) g.y / 512;
  259. const float u1 = u0 + ((float) g.width) / 512;
  260. const float v1 = v0 - ((float) g.height) / 512;
  261. // Fill vertex buffer
  262. (*(VertexData*)(vb.data)).x = x0;
  263. (*(VertexData*)(vb.data)).y = y0;
  264. (*(VertexData*)(vb.data)).u = u0;
  265. (*(VertexData*)(vb.data)).v = v1;
  266. vb.data += sizeof(VertexData);
  267. (*(VertexData*)(vb.data)).x = x1;
  268. (*(VertexData*)(vb.data)).y = y0;
  269. (*(VertexData*)(vb.data)).u = u1;
  270. (*(VertexData*)(vb.data)).v = v1;
  271. vb.data += sizeof(VertexData);
  272. (*(VertexData*)(vb.data)).x = x1;
  273. (*(VertexData*)(vb.data)).y = y1;
  274. (*(VertexData*)(vb.data)).u = u1;
  275. (*(VertexData*)(vb.data)).v = v0;
  276. vb.data += sizeof(VertexData);
  277. (*(VertexData*)(vb.data)).x = x0;
  278. (*(VertexData*)(vb.data)).y = y1;
  279. (*(VertexData*)(vb.data)).u = u0;
  280. (*(VertexData*)(vb.data)).v = v0;
  281. vb.data += sizeof(VertexData);
  282. // Fill index buffer
  283. (*(IndexData*)(ib.data)).a = index;
  284. (*(IndexData*)(ib.data)).b = index + 1;
  285. ib.data += sizeof(IndexData);
  286. (*(IndexData*)(ib.data)).a = index + 2;
  287. (*(IndexData*)(ib.data)).b = index;
  288. ib.data += sizeof(IndexData);
  289. (*(IndexData*)(ib.data)).a = index + 2;
  290. (*(IndexData*)(ib.data)).b = index + 3;
  291. ib.data += sizeof(IndexData);
  292. // Advance pen position
  293. x_pen_advance += g.x_advance;
  294. index += 4;
  295. }
  296. }
  297. const MaterialResource* mr = (MaterialResource*) device()->resource_manager()->get(resource->material());
  298. const TextureResource* tr = (TextureResource*) device()->resource_manager()->get(mr->get_texture_layer(0));
  299. r->set_layer_view(1, matrix4x4::IDENTITY);
  300. r->set_layer_projection(1, m_projection);
  301. r->set_layer_viewport(1, 0, 0, 1000, 625);
  302. r->set_state(STATE_COLOR_WRITE
  303. | STATE_CULL_CW
  304. | STATE_BLEND_EQUATION_ADD
  305. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  306. r->set_pose(m_pose);
  307. r->set_program(render_world_globals::default_font_program());
  308. r->set_texture(0, render_world_globals::default_font_uniform(), tr->texture(),
  309. TEXTURE_FILTER_LINEAR | TEXTURE_WRAP_U_CLAMP_REPEAT | TEXTURE_WRAP_V_CLAMP_REPEAT);
  310. r->set_uniform(render_world_globals::default_color_uniform(), UniformType::FLOAT_4, color4::to_float_ptr(color), 1);
  311. r->set_vertex_buffer(vb);
  312. r->set_index_buffer(ib);
  313. r->commit(1, (int32_t) pos.z);
  314. }
  315. } // namespace crown