Gui.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 "GuiRect.h"
  38. #include "GuiTriangle.h"
  39. #include "GuiImage.h"
  40. #include "GuiText.h"
  41. namespace crown
  42. {
  43. using namespace matrix4x4;
  44. //-----------------------------------------------------------------------------
  45. Gui::Gui(RenderWorld& render_world, GuiResource* gr, Renderer& r)
  46. : m_render_world(render_world)
  47. , m_resource(gr)
  48. , m_r(r)
  49. , m_resolution(1000, 625)
  50. , m_visible(true)
  51. , m_rect_pool(default_allocator(), CE_MAX_GUI_RECTS, sizeof(GuiRect), CE_ALIGNOF(GuiRect))
  52. , m_triangle_pool(default_allocator(), CE_MAX_GUI_TRIANGLES, sizeof(GuiTriangle), CE_ALIGNOF(GuiTriangle))
  53. , m_image_pool(default_allocator(), CE_MAX_GUI_IMAGES, sizeof(GuiImage), CE_ALIGNOF(GuiImage))
  54. , m_text_pool(default_allocator(), CE_MAX_GUI_TEXTS, sizeof(GuiText), CE_ALIGNOF(GuiText))
  55. {
  56. // orthographic projection
  57. set_orthographic_rh(m_projection, 0, m_resolution.x, m_resolution.y, 0, -0.01f, 100.0f);
  58. // pose
  59. Vector3 pos = m_resource->gui_position();
  60. set_identity(m_pose);
  61. set_translation(m_pose, pos);
  62. // Gui's rects creation
  63. for (uint32_t i = 0; i < m_resource->num_rects(); i++)
  64. {
  65. GuiRectData data = m_resource->get_rect(i);
  66. Vector3 pos(data.position[0], data.position[1], data.position[2]);
  67. Vector2 size(data.size[0], data.size[1]);
  68. Color4 color(data.color[0], data.color[1], data.color[2], data.color[3]);
  69. create_rect(pos, size, color);
  70. }
  71. // Gui's triangles creation
  72. for (uint32_t i = 0; i < m_resource->num_triangles(); i++)
  73. {
  74. GuiTriangleData data = m_resource->get_triangle(i);
  75. Vector2 p1(data.points[0], data.points[1]);
  76. Vector2 p2(data.points[2], data.points[3]);
  77. Vector2 p3(data.points[4], data.points[5]);
  78. Color4 color(data.color[0], data.color[1], data.color[2], data.color[3]);
  79. create_triangle(p1, p2, p3, color);
  80. }
  81. // Gui's images creation
  82. for (uint32_t i = 0; i < m_resource->num_images(); i++)
  83. {
  84. GuiImageData data = m_resource->get_image(i);
  85. ResourceId mat = data.material;
  86. Vector3 pos(data.position[0], data.position[1], 0);
  87. Vector2 size(data.size[0], data.size[1]);
  88. create_image(mat, pos, size);
  89. }
  90. }
  91. //-----------------------------------------------------------------------------
  92. Gui::~Gui()
  93. {
  94. for (uint32_t i = 0; i < m_rects.size(); i++)
  95. {
  96. CE_DELETE(m_rect_pool, m_rects[i]);
  97. }
  98. for (uint32_t i = 0; i < m_triangles.size(); i++)
  99. {
  100. CE_DELETE(m_triangle_pool, m_triangles[i]);
  101. }
  102. for (uint32_t i = 0; i < m_images.size(); i++)
  103. {
  104. CE_DELETE(m_image_pool, m_images[i]);
  105. }
  106. for (uint32_t i = 0; i < m_texts.size(); i++)
  107. {
  108. CE_DELETE(m_text_pool, m_texts[i]);
  109. }
  110. }
  111. //-----------------------------------------------------------------------------
  112. const GuiId Gui::id() const
  113. {
  114. return m_id;
  115. }
  116. //-----------------------------------------------------------------------------
  117. void Gui::set_id(const GuiId id)
  118. {
  119. m_id = id;
  120. }
  121. //-----------------------------------------------------------------------------
  122. Vector2 Gui::resolution() const
  123. {
  124. return m_resolution;
  125. }
  126. //-----------------------------------------------------------------------------
  127. void Gui::move(const Vector3& pos)
  128. {
  129. set_identity(m_pose);
  130. set_translation(m_pose, pos);
  131. }
  132. //-----------------------------------------------------------------------------
  133. void Gui::show()
  134. {
  135. m_visible = true;
  136. }
  137. //-----------------------------------------------------------------------------
  138. void Gui::hide()
  139. {
  140. m_visible = false;
  141. }
  142. //-----------------------------------------------------------------------------
  143. GuiRectId Gui::create_rect(const Vector3& pos, const Vector2& size, const Color4& color)
  144. {
  145. GuiRect* rect = CE_NEW(m_rect_pool, GuiRect)(m_r, pos, size, color);
  146. return m_rects.create(rect);
  147. }
  148. //-----------------------------------------------------------------------------
  149. void Gui::update_rect(GuiRectId id, const Vector3& pos, const Vector2& size, const Color4& color)
  150. {
  151. CE_ASSERT(m_rects.has(id), "GuiRect does not exists");
  152. GuiRect* rect = m_rects.lookup(id);
  153. rect->update(pos, size, color);
  154. }
  155. //-----------------------------------------------------------------------------
  156. void Gui::destroy_rect(GuiRectId id)
  157. {
  158. CE_ASSERT(m_rects.has(id), "GuiRect does not exist");
  159. GuiRect* rect = m_rects.lookup(id);
  160. CE_DELETE(m_rect_pool, rect);
  161. m_rects.destroy(id);
  162. }
  163. //-----------------------------------------------------------------------------
  164. GuiTriangleId Gui::create_triangle(const Vector2& p1, const Vector2& p2, const Vector2& p3, const Color4& color)
  165. {
  166. GuiTriangle* triangle = CE_NEW(m_triangle_pool, GuiTriangle)(m_r, p1, p2, p3, color);
  167. return m_triangles.create(triangle);
  168. }
  169. //-----------------------------------------------------------------------------
  170. void Gui::update_triangle(GuiTriangleId id, const Vector2& p1, const Vector2& p2, const Vector2& p3, const Color4& color)
  171. {
  172. CE_ASSERT(m_triangles.has(id), "GuiTriangle does not exists");
  173. GuiTriangle* triangle = m_triangles.lookup(id);
  174. triangle->update(p1, p2, p3, color);
  175. }
  176. //-----------------------------------------------------------------------------
  177. void Gui::destroy_triangle(GuiTriangleId id)
  178. {
  179. CE_ASSERT(m_triangles.has(id), "GuiTriangle does not exist");
  180. GuiTriangle* triangle = m_triangles.lookup(id);
  181. CE_DELETE(m_triangle_pool, triangle);
  182. m_triangles.destroy(id);
  183. }
  184. //-----------------------------------------------------------------------------
  185. GuiImageId Gui::create_image(ResourceId material, const Vector3& pos, const Vector2& size)
  186. {
  187. GuiImage* image = CE_NEW(m_image_pool, GuiImage)(m_render_world, m_r, material, pos, size);
  188. return m_images.create(image);
  189. }
  190. //-----------------------------------------------------------------------------
  191. void Gui::update_image(GuiImageId id, const Vector3& pos, const Vector2& size)
  192. {
  193. CE_ASSERT(m_images.has(id), "GuiImage does not exists");
  194. GuiImage* image = m_images.lookup(id);
  195. image->update(pos, size);
  196. }
  197. //-----------------------------------------------------------------------------
  198. void Gui::destroy_image(GuiImageId id)
  199. {
  200. CE_ASSERT(m_images.has(id), "GuiImage does not exists");
  201. GuiImage* image = m_images.lookup(id);
  202. CE_DELETE(m_image_pool, image);
  203. m_images.destroy(id);
  204. }
  205. //-----------------------------------------------------------------------------
  206. GuiTextId Gui::create_text(const char* str, const FontResource* font, uint32_t font_size, const Vector3& pos)
  207. {
  208. GuiText* text = CE_NEW(m_text_pool, GuiText)(m_render_world, m_r, str, font, font_size, pos);
  209. return m_texts.create(text);
  210. }
  211. //-----------------------------------------------------------------------------
  212. void Gui::update_text(GuiTextId id, const char* str, uint32_t font_size, const Vector3& pos)
  213. {
  214. CE_ASSERT(m_texts.has(id), "GuiText does not exists");
  215. GuiText* text = m_texts.lookup(id);
  216. text->update(str, font_size, pos);
  217. }
  218. //-----------------------------------------------------------------------------
  219. void Gui::destroy_text(GuiTextId id)
  220. {
  221. CE_ASSERT(m_texts.has(id), "GuiText does not exists");
  222. GuiText* text = m_texts.lookup(id);
  223. CE_DELETE(m_text_pool, text);
  224. m_texts.destroy(id);
  225. }
  226. //-----------------------------------------------------------------------------
  227. void Gui::render(UniformId font, UniformId albedo)
  228. {
  229. // resolution
  230. uint32_t width = 0;
  231. uint32_t height = 0;
  232. device()->window()->get_size(width, height);
  233. m_resolution.x = width;
  234. m_resolution.y = height;
  235. m_r.set_layer_view(1, matrix4x4::IDENTITY);
  236. m_r.set_layer_projection(1, m_projection);
  237. m_r.set_layer_viewport(1, translation(m_pose).x, translation(m_pose).y, m_resolution.x, m_resolution.y);
  238. if (!m_visible) return;
  239. // Render all Rects
  240. for (uint32_t i = 0; i < m_rects.size(); i++)
  241. {
  242. m_r.set_program(render_world_globals::default_program());
  243. m_r.set_state(STATE_DEPTH_WRITE
  244. | STATE_COLOR_WRITE
  245. | STATE_ALPHA_WRITE
  246. | STATE_CULL_CW
  247. | STATE_PRIMITIVE_LINES
  248. | STATE_BLEND_EQUATION_ADD
  249. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  250. m_r.set_pose(m_pose);
  251. m_rects[i]->render();
  252. }
  253. // Render all Triangles
  254. for (uint32_t i = 0; i < m_triangles.size(); i++)
  255. {
  256. m_r.set_program(render_world_globals::default_program());
  257. m_r.set_state(STATE_DEPTH_WRITE
  258. | STATE_COLOR_WRITE
  259. | STATE_ALPHA_WRITE
  260. | STATE_CULL_CW
  261. | STATE_PRIMITIVE_LINES
  262. | STATE_BLEND_EQUATION_ADD
  263. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  264. m_r.set_pose(m_pose);
  265. m_triangles[i]->render();
  266. }
  267. // Render all Images
  268. for (uint32_t i = 0; i < m_images.size(); i++)
  269. {
  270. m_r.set_program(render_world_globals::default_texture_program());
  271. m_r.set_state(STATE_DEPTH_WRITE
  272. | STATE_COLOR_WRITE
  273. | STATE_ALPHA_WRITE
  274. | STATE_CULL_CW
  275. | STATE_PRIMITIVE_TRIANGLES
  276. | STATE_BLEND_EQUATION_ADD
  277. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  278. m_r.set_pose(m_pose);
  279. m_images[i]->render(albedo);
  280. }
  281. // Render all Texts
  282. for (uint32_t i = 0; i < m_texts.size(); i++)
  283. {
  284. m_r.set_program(render_world_globals::default_font_program());
  285. m_r.set_state(STATE_DEPTH_WRITE
  286. | STATE_COLOR_WRITE
  287. | STATE_ALPHA_WRITE
  288. | STATE_CULL_CW
  289. | STATE_PRIMITIVE_TRIANGLES
  290. | STATE_BLEND_EQUATION_ADD
  291. | STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
  292. m_r.set_pose(m_pose);
  293. m_texts[i]->render(font);
  294. }
  295. }
  296. } // namespace crown