gui.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (c) 2012-2016 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_manager.h"
  9. #include "material_resource.h"
  10. #include "matrix4x4.h"
  11. #include "resource_manager.h"
  12. #include "string_utils.h"
  13. #include "utf8.h"
  14. #include "vector2.h"
  15. #include "vector3.h"
  16. #include <bgfx/bgfx.h>
  17. namespace crown
  18. {
  19. Gui::Gui(ResourceManager& rm, ShaderManager& sm, MaterialManager& mm, u16 width, u16 height)
  20. : _resource_manager(&rm)
  21. , _shader_manager(&sm)
  22. , _material_manager(&mm)
  23. , _width(width)
  24. , _height(height)
  25. , _projection(MATRIX4X4_IDENTITY)
  26. , _world(MATRIX4X4_IDENTITY)
  27. {
  28. orthographic(_projection, 0, width, 0, height, -0.01f, 100.0f);
  29. _pos_tex_col
  30. .begin()
  31. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  32. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float, true)
  33. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  34. .end();
  35. }
  36. Vector2 Gui::resolution() const
  37. {
  38. return vector2(_width, _height);
  39. }
  40. void Gui::move(const Vector2& pos)
  41. {
  42. set_identity(_world);
  43. set_translation(_world, vector3(pos.x, pos.y, 0));
  44. }
  45. Vector2 Gui::screen_to_gui(const Vector2& pos)
  46. {
  47. return vector2(pos.x, _height - pos.y);
  48. }
  49. void Gui::triangle(const Vector3& a, const Vector3& b, const Vector3& c, StringId64 material, const Color4& color)
  50. {
  51. bgfx::TransientVertexBuffer tvb;
  52. bgfx::TransientIndexBuffer tib;
  53. bgfx::allocTransientVertexBuffer(&tvb, 3, _pos_tex_col);
  54. bgfx::allocTransientIndexBuffer(&tib, 3);
  55. VertexData* vd = (VertexData*)tvb.data;
  56. vd[0].pos.x = a.x;
  57. vd[0].pos.y = a.y;
  58. vd[0].pos.z = a.z;
  59. vd[0].uv.x = 0.0f;
  60. vd[0].uv.y = 0.0f;
  61. vd[0].col = to_abgr(color);
  62. vd[1].pos.x = b.x;
  63. vd[1].pos.y = b.y;
  64. vd[1].pos.z = b.z;
  65. vd[1].uv.x = 1.0f;
  66. vd[1].uv.y = 0.0f;
  67. vd[1].col = to_abgr(color);
  68. vd[2].pos.x = c.x;
  69. vd[2].pos.y = c.y;
  70. vd[2].pos.z = c.z;
  71. vd[2].uv.x = 1.0f;
  72. vd[2].uv.y = 1.0f;
  73. vd[2].col = to_abgr(color);
  74. u16* inds = (u16*)tib.data;
  75. inds[0] = 0;
  76. inds[1] = 1;
  77. inds[2] = 2;
  78. }
  79. void Gui::rect3d(const Vector3& pos, const Vector2& size, StringId64 material, const Color4& color)
  80. {
  81. bgfx::TransientVertexBuffer tvb;
  82. bgfx::TransientIndexBuffer tib;
  83. bgfx::allocTransientVertexBuffer(&tvb, 4, _pos_tex_col);
  84. bgfx::allocTransientIndexBuffer(&tib, 6);
  85. VertexData* vd = (VertexData*)tvb.data;
  86. vd[0].pos.x = pos.x;
  87. vd[0].pos.y = pos.y;
  88. vd[0].pos.z = pos.z;
  89. vd[0].uv.x = 0.0f;
  90. vd[0].uv.y = 1.0f;
  91. vd[0].col = to_abgr(color);
  92. vd[1].pos.x = pos.x + size.x;
  93. vd[1].pos.y = pos.y;
  94. vd[1].pos.z = pos.z;
  95. vd[1].uv.x = 1.0f;
  96. vd[1].uv.y = 1.0f;
  97. vd[1].col = to_abgr(color);
  98. vd[2].pos.x = pos.x + size.x;
  99. vd[2].pos.y = pos.y + size.y;
  100. vd[2].pos.z = pos.z;
  101. vd[2].uv.x = 1.0f;
  102. vd[2].uv.y = 0.0f;
  103. vd[2].col = to_abgr(color);
  104. vd[3].pos.x = pos.x;
  105. vd[3].pos.y = pos.y + size.y;
  106. vd[3].pos.z = pos.z;
  107. vd[3].uv.x = 0.0f;
  108. vd[3].uv.y = 0.0f;
  109. vd[3].col = to_abgr(color);
  110. u16* inds = (u16*)tib.data;
  111. inds[0] = 0;
  112. inds[1] = 1;
  113. inds[2] = 2;
  114. inds[3] = 0;
  115. inds[4] = 2;
  116. inds[5] = 3;
  117. bgfx::setVertexBuffer(&tvb);
  118. bgfx::setIndexBuffer(&tib);
  119. bgfx::setTransform(to_float_ptr(_projection));
  120. _material_manager->create_material(material);
  121. _material_manager->get(material)->bind(*_resource_manager, *_shader_manager, 2);
  122. }
  123. void Gui::rect(const Vector2& pos, const Vector2& size, StringId64 material, const Color4& color)
  124. {
  125. rect3d(vector3(pos.x, pos.y, 0.0f), size, material, color);
  126. }
  127. void Gui::image_uv3d(const Vector3& pos, const Vector2& size, const Vector2& uv0, const Vector2& uv1, StringId64 material, const Color4& color)
  128. {
  129. bgfx::TransientVertexBuffer tvb;
  130. bgfx::TransientIndexBuffer tib;
  131. bgfx::allocTransientVertexBuffer(&tvb, 4, _pos_tex_col);
  132. bgfx::allocTransientIndexBuffer(&tib, 6);
  133. VertexData* vd = (VertexData*)tvb.data;
  134. vd[0].pos.x = pos.x;
  135. vd[0].pos.y = pos.y;
  136. vd[0].pos.z = pos.z;
  137. vd[0].uv.x = 0.0f;
  138. vd[0].uv.y = 1.0f;
  139. vd[0].col = to_abgr(color);
  140. vd[1].pos.x = pos.x + size.x;
  141. vd[1].pos.y = pos.y;
  142. vd[1].pos.z = pos.z;
  143. vd[1].uv.x = 1.0f;
  144. vd[1].uv.y = 1.0f;
  145. vd[1].col = to_abgr(color);
  146. vd[2].pos.x = pos.x + size.x;
  147. vd[2].pos.y = pos.y + size.y;
  148. vd[2].pos.z = pos.z;
  149. vd[2].uv.x = 1.0f;
  150. vd[2].uv.y = 0.0f;
  151. vd[2].col = to_abgr(color);
  152. vd[3].pos.x = pos.x;
  153. vd[3].pos.y = pos.y + size.y;
  154. vd[3].pos.z = pos.z;
  155. vd[3].uv.x = 0.0f;
  156. vd[3].uv.y = 0.0f;
  157. vd[3].col = to_abgr(color);
  158. u16* inds = (u16*)tib.data;
  159. inds[0] = 0;
  160. inds[1] = 1;
  161. inds[2] = 2;
  162. inds[3] = 0;
  163. inds[4] = 2;
  164. inds[5] = 3;
  165. bgfx::setVertexBuffer(&tvb);
  166. bgfx::setIndexBuffer(&tib);
  167. }
  168. void Gui::image_uv(const Vector2& pos, const Vector2& size, const Vector2& uv0, const Vector2& uv1, StringId64 material, const Color4& color)
  169. {
  170. image_uv3d(vector3(pos.x, pos.y, 0.0f), size, uv0, uv1, material, color);
  171. }
  172. void Gui::image3d(const Vector3& pos, const Vector2& size, StringId64 material, const Color4& color)
  173. {
  174. image_uv3d(pos, size, VECTOR2_ZERO, VECTOR2_ONE, material, color);
  175. }
  176. void Gui::image(const Vector2& pos, const Vector2& size, StringId64 material, const Color4& color)
  177. {
  178. image3d(vector3(pos.x, pos.y, 0.0f), size, material, color);
  179. }
  180. void Gui::text3d(const Vector3& pos, u32 font_size, const char* str, StringId64 font, StringId64 material, const Color4& color)
  181. {
  182. const FontResource* fr = (FontResource*)_resource_manager->get(RESOURCE_TYPE_FONT, font);
  183. const f32 scale = (f32)font_size / (f32)fr->font_size;
  184. const u32 len = strlen32(str);
  185. bgfx::TransientVertexBuffer tvb;
  186. bgfx::TransientIndexBuffer tib;
  187. bgfx::allocTransientVertexBuffer(&tvb, 4 * len, _pos_tex_col);
  188. bgfx::allocTransientIndexBuffer(&tib, 6 * len);
  189. u16 index = 0;
  190. f32 x_pen_advance = 0.0f;
  191. f32 y_pen_advance = 0.0f;
  192. Vector2 m_pen;
  193. u32 state = 0;
  194. u32 code_point = 0;
  195. for (u32 i = 0; i < len; i++)
  196. {
  197. switch (str[i])
  198. {
  199. case '\n':
  200. {
  201. x_pen_advance = 0.0f;
  202. y_pen_advance -= fr->font_size;
  203. continue;
  204. }
  205. case '\t':
  206. {
  207. x_pen_advance += font_size * 4;
  208. continue;
  209. }
  210. }
  211. if (utf8::decode(&state, &code_point, str[i]) == UTF8_ACCEPT)
  212. {
  213. const GlyphData& g = *font_resource::get_glyph(fr, code_point);
  214. const f32 baseline = g.height - g.y_offset;
  215. // Set pen position
  216. m_pen.x = pos.x + g.x_offset;
  217. m_pen.y = pos.y - baseline;
  218. // Position coords
  219. const f32 x0 = (m_pen.x + x_pen_advance) * scale;
  220. const f32 y0 = (m_pen.y + y_pen_advance) * scale;
  221. const f32 x1 = (m_pen.x + g.width + x_pen_advance) * scale;
  222. const f32 y1 = (m_pen.y + g.height + y_pen_advance) * scale;
  223. // Texture coords
  224. const f32 u0 = g.x / fr->texture_size;
  225. const f32 v1 = g.y / fr->texture_size; // Upper-left char corner
  226. const f32 u1 = u0 + g.width / fr->texture_size;
  227. const f32 v0 = v1 + g.height / fr->texture_size; // Bottom-left char corner
  228. // Fill vertex buffer
  229. VertexData* vd = (VertexData*)&tvb.data[i*4*sizeof(VertexData)];
  230. vd[0].pos.x = x0;
  231. vd[0].pos.y = y0;
  232. vd[0].pos.z = pos.z;
  233. vd[0].uv.x = u0;
  234. vd[0].uv.y = v0;
  235. vd[0].col = to_abgr(color);
  236. vd[1].pos.x = x1;
  237. vd[1].pos.y = y0;
  238. vd[1].pos.z = pos.z;
  239. vd[1].uv.x = u1;
  240. vd[1].uv.y = v0;
  241. vd[1].col = to_abgr(color);
  242. vd[2].pos.x = x1;
  243. vd[2].pos.y = y1;
  244. vd[2].pos.z = pos.z;
  245. vd[2].uv.x = u1;
  246. vd[2].uv.y = v1;
  247. vd[2].col = to_abgr(color);
  248. vd[3].pos.x = x0;
  249. vd[3].pos.y = y1;
  250. vd[3].pos.z = pos.z;
  251. vd[3].uv.x = u0;
  252. vd[3].uv.y = v1;
  253. vd[3].col = to_abgr(color);
  254. // Fill index buffer
  255. u16* inds = (u16*)&tib.data[i*6*sizeof(u16)];
  256. inds[0] = index + 0;
  257. inds[1] = index + 1;
  258. inds[2] = index + 2;
  259. inds[3] = index + 0;
  260. inds[4] = index + 2;
  261. inds[5] = index + 3;
  262. // Advance pen position
  263. x_pen_advance += g.x_advance;
  264. index += 4;
  265. }
  266. }
  267. bgfx::setVertexBuffer(&tvb);
  268. bgfx::setIndexBuffer(&tib);
  269. bgfx::setTransform(to_float_ptr(_projection));
  270. _material_manager->create_material(material);
  271. _material_manager->get(material)->bind(*_resource_manager, *_shader_manager, 2);
  272. }
  273. void Gui::text(const Vector2& pos, u32 font_size, const char* str, StringId64 font, StringId64 material, const Color4& color)
  274. {
  275. text3d(vector3(pos.x, pos.y, 0.0f), font_size, str, font, material, color);
  276. }
  277. } // namespace crown