gui.cpp 9.6 KB

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