gui.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "color4.h"
  6. #include "font_resource.h"
  7. #include "gui.h"
  8. #include "material_resource.h"
  9. #include "material_manager.h"
  10. #include "vector2.h"
  11. #include "vector3.h"
  12. #include "matrix4x4.h"
  13. #include <bgfx/bgfx.h>
  14. namespace crown
  15. {
  16. struct VertexData
  17. {
  18. float x;
  19. float y;
  20. float u;
  21. float v;
  22. };
  23. struct IndexData
  24. {
  25. uint16_t a;
  26. uint16_t b;
  27. };
  28. #define UTF8_ACCEPT 0
  29. static const uint8_t s_utf8d[364] =
  30. {
  31. // The first part of the table maps bytes to character classes that
  32. // to reduce the size of the transition table and create bitmasks.
  33. 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,
  34. 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,
  35. 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,
  36. 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,
  37. 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,
  38. 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,
  39. 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,
  40. 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,
  41. // The second part is a transition table that maps a combination
  42. // of a state of the automaton and a character class to a state.
  43. 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
  44. 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
  45. 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
  46. 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
  47. 12,36,12,12,12,12,12,12,12,12,12,12
  48. };
  49. static uint32_t utf8_decode(uint32_t* state, uint32_t* code_point, uint8_t character)
  50. {
  51. uint32_t byte = character;
  52. uint32_t type = s_utf8d[byte];
  53. *code_point = (*state != UTF8_ACCEPT) ? (byte & 0x3fu) | (*code_point << 6) : (0xff >> type) & (byte);
  54. *state = s_utf8d[256 + *state + type];
  55. return *state;
  56. }
  57. bgfx::VertexDecl Gui::s_pos_col;
  58. bgfx::VertexDecl Gui::s_pos_col_tex;
  59. void Gui::init()
  60. {
  61. Gui::s_pos_col
  62. .begin()
  63. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  64. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  65. .end();
  66. Gui::s_pos_col_tex
  67. .begin()
  68. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  69. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float, true)
  70. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  71. .end();
  72. }
  73. Gui::Gui(uint16_t width, uint16_t height, const char* material)
  74. : m_width(width)
  75. , m_height(height)
  76. , m_pose(MATRIX4X4_IDENTITY)
  77. {
  78. orthographic(m_projection, 0, width, 0, height, -0.01f, 100.0f);
  79. m_material = material_manager::get()->create_material(ResourceId(material));
  80. }
  81. Vector2 Gui::resolution() const
  82. {
  83. return vector2(m_width, m_height);
  84. }
  85. void Gui::move(const Vector2& pos)
  86. {
  87. set_identity(m_pose);
  88. set_translation(m_pose, vector3(pos.x, pos.y, 0));
  89. }
  90. Vector2 Gui::screen_to_gui(const Vector2& pos)
  91. {
  92. return vector2(pos.x, m_height - pos.y);
  93. }
  94. void Gui::draw_rectangle(const Vector3& pos, const Vector2& size, const Color4& color)
  95. {
  96. bgfx::TransientVertexBuffer tvb;
  97. bgfx::TransientIndexBuffer tib;
  98. bgfx::allocTransientVertexBuffer(&tvb, 4, Gui::s_pos_col);
  99. bgfx::allocTransientIndexBuffer(&tib, 6);
  100. float* verts = (float*) tvb.data;
  101. verts[0] = pos.x;
  102. verts[1] = pos.y;
  103. verts[2] = pos.x + size.x;
  104. verts[3] = pos.y;
  105. verts[4] = pos.x + size.x;
  106. verts[5] = pos.y + size.y;
  107. verts[6] = pos.x;
  108. verts[7] = pos.y + size.y;
  109. uint16_t* inds = (uint16_t*) tib.data;
  110. inds[0] = 0;
  111. inds[1] = 1;
  112. inds[2] = 2;
  113. inds[3] = 0;
  114. inds[4] = 2;
  115. inds[5] = 3;
  116. bgfx::setViewTransform(1, to_float_ptr(MATRIX4X4_IDENTITY), to_float_ptr(m_projection));
  117. bgfx::setViewRect(1, 0, 0, m_width, m_height);
  118. bgfx::setState(BGFX_STATE_DEFAULT);
  119. bgfx::setVertexBuffer(&tvb);
  120. bgfx::setIndexBuffer(&tib);
  121. material_manager::get()->lookup_material(m_material)->bind();
  122. }
  123. void Gui::draw_image(const char* material, const Vector3& pos, const Vector2& size, const Color4& color)
  124. {
  125. draw_image_uv(material, pos, size, vector2(0, 0), vector2(1, 1), color);
  126. }
  127. void Gui::draw_image_uv(const char* material, const Vector3& pos, const Vector2& size, const Vector2& uv0, const Vector2& uv1, const Color4& color)
  128. {
  129. bgfx::TransientVertexBuffer tvb;
  130. bgfx::TransientIndexBuffer tib;
  131. bgfx::allocTransientVertexBuffer(&tvb, 4, Gui::s_pos_col_tex);
  132. bgfx::allocTransientIndexBuffer(&tib, 6);
  133. float* verts = (float*) tvb.data;
  134. verts[0] = pos.x;
  135. verts[1] = pos.y;
  136. verts[2] = uv0.x;
  137. verts[3] = uv0.y;
  138. verts[4] = pos.x + size.x;
  139. verts[5] = pos.y;
  140. verts[6] = uv1.x;
  141. verts[7] = uv0.y;
  142. verts[8] = pos.x + size.x;
  143. verts[9] = pos.y + size.y;
  144. verts[10] = uv1.x;
  145. verts[11] = uv1.y;
  146. verts[12] = pos.x;
  147. verts[13] = pos.y + size.y;
  148. verts[14] = uv0.x;
  149. verts[15] = uv1.y;
  150. uint16_t* inds = (uint16_t*) tib.data;
  151. inds[0] = 0;
  152. inds[1] = 1;
  153. inds[2] = 2;
  154. inds[3] = 0;
  155. inds[4] = 2;
  156. inds[5] = 3;
  157. bgfx::setViewTransform(1, to_float_ptr(MATRIX4X4_IDENTITY), to_float_ptr(m_projection));
  158. bgfx::setViewRect(1, 0, 0, m_width, m_height);
  159. bgfx::setState(BGFX_STATE_DEFAULT);
  160. bgfx::setVertexBuffer(&tvb);
  161. bgfx::setIndexBuffer(&tib);
  162. material_manager::get()->lookup_material(m_material)->bind();
  163. }
  164. void Gui::draw_text(const char* str, const char* font, uint32_t font_size, const Vector3& pos, const Color4& color)
  165. {
  166. // Renderer* r = device()->renderer();
  167. // const FontResource* resource = (FontResource*) device()->resource_manager()->get("font", font);
  168. // Vector2 m_pen;
  169. // const float scale = ((float)font_size / (float)resource->font_size());
  170. // const uint32_t str_len = strlen32(str);
  171. // TransientVertexBuffer vb;
  172. // TransientIndexBuffer ib;
  173. // r->reserve_transient_vertex_buffer(&vb, 4 * str_len, VertexFormat::P2_T2);
  174. // r->reserve_transient_index_buffer(&ib, 6 * str_len);
  175. // uint16_t index = 0;
  176. // float x_pen_advance = 0.0f;
  177. // float y_pen_advance = 0.0f;
  178. // uint32_t state = 0;
  179. // uint32_t code_point = 0;
  180. // for (uint32_t i = 0; i < str_len; i++)
  181. // {
  182. // switch (str[i])
  183. // {
  184. // case '\n':
  185. // {
  186. // x_pen_advance = 0.0f;
  187. // y_pen_advance -= resource->font_size();
  188. // continue;
  189. // }
  190. // case '\t':
  191. // {
  192. // x_pen_advance += font_size * 4;
  193. // continue;
  194. // }
  195. // }
  196. // if (utf8_decode(&state, &code_point, str[i]) == UTF8_ACCEPT)
  197. // {
  198. // FontGlyphData g = resource->get_glyph(code_point);
  199. // const float baseline = g.height - g.y_offset;
  200. // // Set pen position
  201. // m_pen.x = pos.x + g.x_offset;
  202. // m_pen.y = pos.y - baseline;
  203. // // Position coords
  204. // const float x0 = (m_pen.x + x_pen_advance) * scale;
  205. // const float y0 = (m_pen.y + y_pen_advance) * scale;
  206. // const float x1 = (m_pen.x + g.width + x_pen_advance) * scale;
  207. // const float y1 = (m_pen.y + g.height + y_pen_advance) * scale;
  208. // // Texture coords
  209. // const float u0 = (float) g.x / 512;
  210. // const float v0 = (float) g.y / 512;
  211. // const float u1 = u0 + ((float) g.width) / 512;
  212. // const float v1 = v0 - ((float) g.height) / 512;
  213. // // Fill vertex buffer
  214. // (*(VertexData*)(vb.data)).x = x0;
  215. // (*(VertexData*)(vb.data)).y = y0;
  216. // (*(VertexData*)(vb.data)).u = u0;
  217. // (*(VertexData*)(vb.data)).v = v1;
  218. // vb.data += sizeof(VertexData);
  219. // (*(VertexData*)(vb.data)).x = x1;
  220. // (*(VertexData*)(vb.data)).y = y0;
  221. // (*(VertexData*)(vb.data)).u = u1;
  222. // (*(VertexData*)(vb.data)).v = v1;
  223. // vb.data += sizeof(VertexData);
  224. // (*(VertexData*)(vb.data)).x = x1;
  225. // (*(VertexData*)(vb.data)).y = y1;
  226. // (*(VertexData*)(vb.data)).u = u1;
  227. // (*(VertexData*)(vb.data)).v = v0;
  228. // vb.data += sizeof(VertexData);
  229. // (*(VertexData*)(vb.data)).x = x0;
  230. // (*(VertexData*)(vb.data)).y = y1;
  231. // (*(VertexData*)(vb.data)).u = u0;
  232. // (*(VertexData*)(vb.data)).v = v0;
  233. // vb.data += sizeof(VertexData);
  234. // // Fill index buffer
  235. // (*(IndexData*)(ib.data)).a = index;
  236. // (*(IndexData*)(ib.data)).b = index + 1;
  237. // ib.data += sizeof(IndexData);
  238. // (*(IndexData*)(ib.data)).a = index + 2;
  239. // (*(IndexData*)(ib.data)).b = index;
  240. // ib.data += sizeof(IndexData);
  241. // (*(IndexData*)(ib.data)).a = index + 2;
  242. // (*(IndexData*)(ib.data)).b = index + 3;
  243. // ib.data += sizeof(IndexData);
  244. // // Advance pen position
  245. // x_pen_advance += g.x_advance;
  246. // index += 4;
  247. // }
  248. // }
  249. // const MaterialResource* mr = (MaterialResource*) device()->resource_manager()->get(resource->material());
  250. // const TextureResource* tr = (TextureResource*) device()->resource_manager()->get(mr->get_texture_layer(0));
  251. // r->set_layer_view(1, matrix4x4::IDENTITY);
  252. // r->set_layer_projection(1, m_projection);
  253. // r->set_layer_viewport(1, 0, 0, 1000, 625);
  254. // r->set_state(STATE_COLOR_WRITE
  255. // | STATE_CULL_CW
  256. // | STATE_BLEND_EQUATION_ADD
  257. // | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  258. // r->set_pose(m_pose);
  259. // r->set_program(render_world_globals::default_font_program());
  260. // r->set_texture(0, render_world_globals::default_font_uniform(), tr->texture(),
  261. // TEXTURE_FILTER_LINEAR | TEXTURE_WRAP_U_CLAMP_REPEAT | TEXTURE_WRAP_V_CLAMP_REPEAT);
  262. // r->set_uniform(render_world_globals::default_color_uniform(), UniformType::FLOAT_4, color4::to_float_ptr(color), 1);
  263. // r->set_vertex_buffer(vb);
  264. // r->set_index_buffer(ib);
  265. // r->commit(1, (int32_t) pos.z);
  266. }
  267. } // namespace crown