gui.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 "color4.h"
  24. #include "font_resource.h"
  25. #include "gui.h"
  26. #include "material_resource.h"
  27. #include "material_manager.h"
  28. #include "vector2.h"
  29. #include "vector3.h"
  30. #include "matrix4x4.h"
  31. #include <bgfx.h>
  32. namespace crown
  33. {
  34. using namespace matrix4x4;
  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. static 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. bgfx::VertexDecl Gui::s_pos_col;
  81. bgfx::VertexDecl Gui::s_pos_col_tex;
  82. void Gui::init()
  83. {
  84. Gui::s_pos_col
  85. .begin()
  86. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  87. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  88. .end();
  89. Gui::s_pos_col_tex
  90. .begin()
  91. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  92. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float, true)
  93. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  94. .end();
  95. }
  96. //-----------------------------------------------------------------------------
  97. Gui::Gui(uint16_t width, uint16_t height, const char* material)
  98. : m_width(width)
  99. , m_height(height)
  100. , m_pose(matrix4x4::IDENTITY)
  101. {
  102. set_orthographic_rh(m_projection, 0, width, 0, height, -0.01f, 100.0f);
  103. ResourceId id("material", material);
  104. m_material = material_manager::get()->create_material(id.name);
  105. }
  106. //-----------------------------------------------------------------------------
  107. const GuiId Gui::id() const
  108. {
  109. return m_id;
  110. }
  111. //-----------------------------------------------------------------------------
  112. void Gui::set_id(const GuiId id)
  113. {
  114. m_id = id;
  115. }
  116. //-----------------------------------------------------------------------------
  117. Vector2 Gui::resolution() const
  118. {
  119. return Vector2(m_width, m_height);
  120. }
  121. //-----------------------------------------------------------------------------
  122. void Gui::move(const Vector2& pos)
  123. {
  124. set_identity(m_pose);
  125. set_translation(m_pose, Vector3(pos.x, pos.y, 0));
  126. }
  127. //-----------------------------------------------------------------------------
  128. Vector2 Gui::screen_to_gui(const Vector2& pos)
  129. {
  130. return Vector2(pos.x, m_height - pos.y);
  131. }
  132. //-----------------------------------------------------------------------------
  133. void Gui::draw_rectangle(const Vector3& pos, const Vector2& size, const Color4& color)
  134. {
  135. bgfx::TransientVertexBuffer tvb;
  136. bgfx::TransientIndexBuffer tib;
  137. bgfx::allocTransientVertexBuffer(&tvb, 4, Gui::s_pos_col);
  138. bgfx::allocTransientIndexBuffer(&tib, 6);
  139. float* verts = (float*) tvb.data;
  140. verts[0] = pos.x;
  141. verts[1] = pos.y;
  142. verts[2] = pos.x + size.x;
  143. verts[3] = pos.y;
  144. verts[4] = pos.x + size.x;
  145. verts[5] = pos.y + size.y;
  146. verts[6] = pos.x;
  147. verts[7] = pos.y + size.y;
  148. uint16_t* inds = (uint16_t*) tib.data;
  149. inds[0] = 0;
  150. inds[1] = 1;
  151. inds[2] = 2;
  152. inds[3] = 0;
  153. inds[4] = 2;
  154. inds[5] = 3;
  155. material_manager::get()->lookup_material(m_material)->bind();
  156. bgfx::setViewTransform(1, matrix4x4::to_float_ptr(matrix4x4::IDENTITY), matrix4x4::to_float_ptr(m_projection));
  157. bgfx::setViewRect(1, 0, 0, m_width, m_height);
  158. bgfx::setState(BGFX_STATE_DEFAULT);
  159. bgfx::setVertexBuffer(&tvb);
  160. bgfx::setIndexBuffer(&tib);
  161. bgfx::submit(1, (int32_t) pos.z);
  162. }
  163. //-----------------------------------------------------------------------------
  164. void Gui::draw_image(const char* material, const Vector3& pos, const Vector2& size, const Color4& color)
  165. {
  166. draw_image_uv(material, pos, size, Vector2(0, 0), Vector2(1, 1), color);
  167. }
  168. //-----------------------------------------------------------------------------
  169. void Gui::draw_image_uv(const char* material, const Vector3& pos, const Vector2& size, const Vector2& uv0, const Vector2& uv1, const Color4& color)
  170. {
  171. bgfx::TransientVertexBuffer tvb;
  172. bgfx::TransientIndexBuffer tib;
  173. bgfx::allocTransientVertexBuffer(&tvb, 4, Gui::s_pos_col_tex);
  174. bgfx::allocTransientIndexBuffer(&tib, 6);
  175. float* verts = (float*) tvb.data;
  176. verts[0] = pos.x;
  177. verts[1] = pos.y;
  178. verts[2] = uv0.x;
  179. verts[3] = uv0.y;
  180. verts[4] = pos.x + size.x;
  181. verts[5] = pos.y;
  182. verts[6] = uv1.x;
  183. verts[7] = uv0.y;
  184. verts[8] = pos.x + size.x;
  185. verts[9] = pos.y + size.y;
  186. verts[10] = uv1.x;
  187. verts[11] = uv1.y;
  188. verts[12] = pos.x;
  189. verts[13] = pos.y + size.y;
  190. verts[14] = uv0.x;
  191. verts[15] = uv1.y;
  192. uint16_t* inds = (uint16_t*) tib.data;
  193. inds[0] = 0;
  194. inds[1] = 1;
  195. inds[2] = 2;
  196. inds[3] = 0;
  197. inds[4] = 2;
  198. inds[5] = 3;
  199. /* ResourceId res_id("material", material);
  200. Material* mat = material_manager::get()->lookup_material(res_id.name);
  201. mat->bind();*/
  202. bgfx::setViewTransform(1, matrix4x4::to_float_ptr(matrix4x4::IDENTITY), matrix4x4::to_float_ptr(m_projection));
  203. bgfx::setViewRect(1, 0, 0, m_width, m_height);
  204. bgfx::setState(BGFX_STATE_DEFAULT);
  205. bgfx::setVertexBuffer(&tvb);
  206. bgfx::setIndexBuffer(&tib);
  207. bgfx::submit(1, (int32_t) pos.z);
  208. }
  209. //-----------------------------------------------------------------------------
  210. void Gui::draw_text(const char* str, const char* font, uint32_t font_size, const Vector3& pos, const Color4& color)
  211. {
  212. // Renderer* r = device()->renderer();
  213. // const FontResource* resource = (FontResource*) device()->resource_manager()->get("font", font);
  214. // Vector2 m_pen;
  215. // const float scale = ((float)font_size / (float)resource->font_size());
  216. // const uint32_t str_len = string::strlen(str);
  217. // TransientVertexBuffer vb;
  218. // TransientIndexBuffer ib;
  219. // r->reserve_transient_vertex_buffer(&vb, 4 * str_len, VertexFormat::P2_T2);
  220. // r->reserve_transient_index_buffer(&ib, 6 * str_len);
  221. // uint16_t index = 0;
  222. // float x_pen_advance = 0.0f;
  223. // float y_pen_advance = 0.0f;
  224. // uint32_t state = 0;
  225. // uint32_t code_point = 0;
  226. // for (uint32_t i = 0; i < str_len; i++)
  227. // {
  228. // switch (str[i])
  229. // {
  230. // case '\n':
  231. // {
  232. // x_pen_advance = 0.0f;
  233. // y_pen_advance -= resource->font_size();
  234. // continue;
  235. // }
  236. // case '\t':
  237. // {
  238. // x_pen_advance += font_size * 4;
  239. // continue;
  240. // }
  241. // }
  242. // if (utf8_decode(&state, &code_point, str[i]) == UTF8_ACCEPT)
  243. // {
  244. // FontGlyphData g = resource->get_glyph(code_point);
  245. // const float baseline = g.height - g.y_offset;
  246. // // Set pen position
  247. // m_pen.x = pos.x + g.x_offset;
  248. // m_pen.y = pos.y - baseline;
  249. // // Position coords
  250. // const float x0 = (m_pen.x + x_pen_advance) * scale;
  251. // const float y0 = (m_pen.y + y_pen_advance) * scale;
  252. // const float x1 = (m_pen.x + g.width + x_pen_advance) * scale;
  253. // const float y1 = (m_pen.y + g.height + y_pen_advance) * scale;
  254. // // Texture coords
  255. // const float u0 = (float) g.x / 512;
  256. // const float v0 = (float) g.y / 512;
  257. // const float u1 = u0 + ((float) g.width) / 512;
  258. // const float v1 = v0 - ((float) g.height) / 512;
  259. // // Fill vertex buffer
  260. // (*(VertexData*)(vb.data)).x = x0;
  261. // (*(VertexData*)(vb.data)).y = y0;
  262. // (*(VertexData*)(vb.data)).u = u0;
  263. // (*(VertexData*)(vb.data)).v = v1;
  264. // vb.data += sizeof(VertexData);
  265. // (*(VertexData*)(vb.data)).x = x1;
  266. // (*(VertexData*)(vb.data)).y = y0;
  267. // (*(VertexData*)(vb.data)).u = u1;
  268. // (*(VertexData*)(vb.data)).v = v1;
  269. // vb.data += sizeof(VertexData);
  270. // (*(VertexData*)(vb.data)).x = x1;
  271. // (*(VertexData*)(vb.data)).y = y1;
  272. // (*(VertexData*)(vb.data)).u = u1;
  273. // (*(VertexData*)(vb.data)).v = v0;
  274. // vb.data += sizeof(VertexData);
  275. // (*(VertexData*)(vb.data)).x = x0;
  276. // (*(VertexData*)(vb.data)).y = y1;
  277. // (*(VertexData*)(vb.data)).u = u0;
  278. // (*(VertexData*)(vb.data)).v = v0;
  279. // vb.data += sizeof(VertexData);
  280. // // Fill index buffer
  281. // (*(IndexData*)(ib.data)).a = index;
  282. // (*(IndexData*)(ib.data)).b = index + 1;
  283. // ib.data += sizeof(IndexData);
  284. // (*(IndexData*)(ib.data)).a = index + 2;
  285. // (*(IndexData*)(ib.data)).b = index;
  286. // ib.data += sizeof(IndexData);
  287. // (*(IndexData*)(ib.data)).a = index + 2;
  288. // (*(IndexData*)(ib.data)).b = index + 3;
  289. // ib.data += sizeof(IndexData);
  290. // // Advance pen position
  291. // x_pen_advance += g.x_advance;
  292. // index += 4;
  293. // }
  294. // }
  295. // const MaterialResource* mr = (MaterialResource*) device()->resource_manager()->get(resource->material());
  296. // const TextureResource* tr = (TextureResource*) device()->resource_manager()->get(mr->get_texture_layer(0));
  297. // r->set_layer_view(1, matrix4x4::IDENTITY);
  298. // r->set_layer_projection(1, m_projection);
  299. // r->set_layer_viewport(1, 0, 0, 1000, 625);
  300. // r->set_state(STATE_COLOR_WRITE
  301. // | STATE_CULL_CW
  302. // | STATE_BLEND_EQUATION_ADD
  303. // | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  304. // r->set_pose(m_pose);
  305. // r->set_program(render_world_globals::default_font_program());
  306. // r->set_texture(0, render_world_globals::default_font_uniform(), tr->texture(),
  307. // TEXTURE_FILTER_LINEAR | TEXTURE_WRAP_U_CLAMP_REPEAT | TEXTURE_WRAP_V_CLAMP_REPEAT);
  308. // r->set_uniform(render_world_globals::default_color_uniform(), UniformType::FLOAT_4, color4::to_float_ptr(color), 1);
  309. // r->set_vertex_buffer(vb);
  310. // r->set_index_buffer(ib);
  311. // r->commit(1, (int32_t) pos.z);
  312. }
  313. } // namespace crown