GeometryBackgroundBorder.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "GeometryBackgroundBorder.h"
  29. #include "../../Include/RmlUi/Core/Box.h"
  30. #include "../../Include/RmlUi/Core/Math.h"
  31. #include <algorithm>
  32. #include <float.h>
  33. namespace Rml {
  34. GeometryBackgroundBorder::GeometryBackgroundBorder(Vector<Vertex>& vertices, Vector<int>& indices) : vertices(vertices), indices(indices)
  35. {}
  36. void GeometryBackgroundBorder::Draw(Vector<Vertex>& vertices, Vector<int>& indices, CornerSizes radii, const Box& box, const Colourb background_color, const Colourb* border_colors)
  37. {
  38. using Edge = Box::Edge;
  39. EdgeSizes border_widths = {
  40. Math::RoundFloat(box.GetEdge(Box::BORDER, Edge::TOP)),
  41. Math::RoundFloat(box.GetEdge(Box::BORDER, Edge::RIGHT)),
  42. Math::RoundFloat(box.GetEdge(Box::BORDER, Edge::BOTTOM)),
  43. Math::RoundFloat(box.GetEdge(Box::BORDER, Edge::LEFT)),
  44. };
  45. int num_borders = 0;
  46. if (border_colors)
  47. {
  48. for (int i = 0; i < 4; i++)
  49. if (border_colors[i].alpha > 0 && border_widths[i] > 0)
  50. num_borders += 1;
  51. }
  52. const Vector2f padding_size = box.GetSize(Box::PADDING).Round();
  53. const bool has_background = (background_color.alpha > 0 && padding_size.x > 0 && padding_size.y > 0);
  54. const bool has_border = (num_borders > 0);
  55. if (!has_background && !has_border)
  56. return;
  57. // -- Find the corner positions --
  58. const Vector2f border_position = box.GetPosition(Box::BORDER).Round();
  59. const Vector2f padding_position = border_position + Vector2f(border_widths[Edge::LEFT], border_widths[Edge::TOP]);
  60. const Vector2f border_size = padding_size + Vector2f(border_widths[Edge::LEFT] + border_widths[Edge::RIGHT], border_widths[Edge::TOP] + border_widths[Edge::BOTTOM]);
  61. // Border edge positions
  62. CornerPositions positions_outer = {
  63. border_position,
  64. border_position + Vector2f(border_size.x, 0),
  65. border_position + border_size,
  66. border_position + Vector2f(0, border_size.y)
  67. };
  68. // Padding edge positions
  69. CornerPositions positions_inner = {
  70. padding_position,
  71. padding_position + Vector2f(padding_size.x, 0),
  72. padding_position + padding_size,
  73. padding_position + Vector2f(0, padding_size.y)
  74. };
  75. // -- For curved borders, find the positions to draw ellipses around, and the scaled outer and inner radii --
  76. const float sum_radius = (radii[TOP_LEFT] + radii[TOP_RIGHT] + radii[BOTTOM_RIGHT] + radii[BOTTOM_LEFT]);
  77. const bool has_radius = (sum_radius > 1.f);
  78. // Curved borders are drawn as circles (outer border) and ellipses (inner border) around the centers.
  79. CornerPositions positions_circle_center;
  80. // Radii of the padding edges, 2-dimensional as these can be ellipses.
  81. // The inner radii is effectively the (signed) distance from the circle center to the padding edge.
  82. // They can also be zero or negative, in which case a sharp corner should be drawn instead of an arc.
  83. CornerSizes2 inner_radii;
  84. if (has_radius)
  85. {
  86. // Scale the radii such that we have no overlapping curves.
  87. float scale_factor = FLT_MAX;
  88. scale_factor = Math::Min(scale_factor, padding_size.x / (radii[TOP_LEFT] + radii[TOP_RIGHT])); // Top
  89. scale_factor = Math::Min(scale_factor, padding_size.y / (radii[TOP_RIGHT] + radii[BOTTOM_RIGHT])); // Right
  90. scale_factor = Math::Min(scale_factor, padding_size.x / (radii[BOTTOM_RIGHT] + radii[BOTTOM_LEFT])); // Bottom
  91. scale_factor = Math::Min(scale_factor, padding_size.y / (radii[BOTTOM_LEFT] + radii[TOP_LEFT])); // Left
  92. scale_factor = Math::Min(1.0f, scale_factor);
  93. for (float& radius : radii)
  94. radius = Math::RoundFloat(radius * scale_factor);
  95. // Place the circle/ellipse centers
  96. positions_circle_center = {
  97. positions_outer[TOP_LEFT] + Vector2f(1, 1) * radii[TOP_LEFT],
  98. positions_outer[TOP_RIGHT] + Vector2f(-1, 1) * radii[TOP_RIGHT],
  99. positions_outer[BOTTOM_RIGHT] + Vector2f(-1, -1) * radii[BOTTOM_RIGHT],
  100. positions_outer[BOTTOM_LEFT] + Vector2f(1, -1) * radii[BOTTOM_LEFT]
  101. };
  102. inner_radii = {
  103. Vector2f(radii[TOP_LEFT]) - Vector2f(border_widths[Edge::LEFT], border_widths[Edge::TOP]),
  104. Vector2f(radii[TOP_RIGHT]) - Vector2f(border_widths[Edge::RIGHT], border_widths[Edge::TOP]),
  105. Vector2f(radii[BOTTOM_RIGHT]) - Vector2f(border_widths[Edge::RIGHT], border_widths[Edge::BOTTOM]),
  106. Vector2f(radii[BOTTOM_LEFT]) - Vector2f(border_widths[Edge::LEFT], border_widths[Edge::BOTTOM])
  107. };
  108. }
  109. // -- Generate the geometry --
  110. GeometryBackgroundBorder geometry(vertices, indices);
  111. {
  112. // Reserve geometry. A conservative estimate, does not take border-radii into account and assumes same-colored borders.
  113. const int estimated_num_vertices = 4 * int(has_background) + 2 * num_borders;
  114. const int estimated_num_triangles = 2 * int(has_background) + 2 * num_borders;
  115. vertices.reserve((int)vertices.size() + estimated_num_vertices);
  116. indices.reserve((int)indices.size() + 3 * estimated_num_triangles);
  117. }
  118. // Draw the background
  119. if (has_background)
  120. {
  121. const int offset_vertices = (int)vertices.size();
  122. for (int corner = 0; corner < 4; corner++)
  123. geometry.DrawBackgroundCorner(Corner(corner), positions_inner[corner], positions_circle_center[corner], radii[corner], inner_radii[corner], background_color);
  124. geometry.FillBackground(offset_vertices);
  125. }
  126. // Draw the border
  127. if (has_border)
  128. {
  129. using Edge = Box::Edge;
  130. const int offset_vertices = (int)vertices.size();
  131. const bool draw_edge[4] = {
  132. border_widths[Edge::TOP] > 0 && border_colors[Edge::TOP].alpha > 0,
  133. border_widths[Edge::RIGHT] > 0 && border_colors[Edge::RIGHT].alpha > 0,
  134. border_widths[Edge::BOTTOM] > 0 && border_colors[Edge::BOTTOM].alpha > 0,
  135. border_widths[Edge::LEFT] > 0 && border_colors[Edge::LEFT].alpha > 0
  136. };
  137. const bool draw_corner[4] = {
  138. draw_edge[Edge::TOP] || draw_edge[Edge::LEFT],
  139. draw_edge[Edge::TOP] || draw_edge[Edge::RIGHT],
  140. draw_edge[Edge::BOTTOM] || draw_edge[Edge::RIGHT],
  141. draw_edge[Edge::BOTTOM] || draw_edge[Edge::LEFT]
  142. };
  143. for (int corner = 0; corner < 4; corner++)
  144. {
  145. const Edge edge0 = Edge((corner + 3) % 4);
  146. const Edge edge1 = Edge(corner);
  147. if (draw_corner[corner])
  148. geometry.DrawBorderCorner(Corner(corner), positions_outer[corner], positions_inner[corner], positions_circle_center[corner],
  149. radii[corner], inner_radii[corner], border_colors[edge0], border_colors[edge1]);
  150. if (draw_edge[edge1])
  151. {
  152. RMLUI_ASSERTMSG(draw_corner[corner] && draw_corner[(corner + 1) % 4], "Border edges can only be drawn if both of its connected corners are drawn.");
  153. geometry.FillEdge(edge1 == Edge::LEFT ? offset_vertices : (int)vertices.size());
  154. }
  155. }
  156. }
  157. #if 0
  158. // Debug draw vertices
  159. if (has_radius)
  160. {
  161. const int num_vertices = vertices.size();
  162. const int num_indices = indices.size();
  163. vertices.resize(num_vertices + 4 * num_vertices);
  164. indices.resize(num_indices + 6 * num_indices);
  165. for (int i = 0; i < num_vertices; i++)
  166. {
  167. GeometryUtilities::GenerateQuad(vertices.data() + num_vertices + 4 * i, indices.data() + num_indices + 6 * i, vertices[i].position, Vector2f(3, 3), Colourb(255, 0, (i % 2) == 0 ? 0 : 255), num_vertices + 4 * i);
  168. }
  169. }
  170. #endif
  171. #ifdef RMLUI_DEBUG
  172. const int num_vertices = (int)vertices.size();
  173. for (int index : indices)
  174. {
  175. RMLUI_ASSERT(index < num_vertices);
  176. }
  177. #endif
  178. }
  179. void GeometryBackgroundBorder::DrawBackgroundCorner(Corner corner, Vector2f pos_inner, Vector2f pos_circle_center, float R, Vector2f r, Colourb color)
  180. {
  181. if (R == 0 || r.x <= 0 || r.y <= 0)
  182. {
  183. DrawPoint(pos_inner, color);
  184. }
  185. else if (r.x > 0 && r.y > 0)
  186. {
  187. const float a0 = float((int)corner + 2) * 0.5f * Math::RMLUI_PI;
  188. const float a1 = float((int)corner + 3) * 0.5f * Math::RMLUI_PI;
  189. const int num_points = GetNumPoints(R);
  190. DrawArc(pos_circle_center, r, a0, a1, color, color, num_points);
  191. }
  192. }
  193. void GeometryBackgroundBorder::DrawPoint(Vector2f pos, Colourb color)
  194. {
  195. const int offset_vertices = (int)vertices.size();
  196. vertices.resize(offset_vertices + 1);
  197. vertices[offset_vertices].position = pos;
  198. vertices[offset_vertices].colour = color;
  199. }
  200. void GeometryBackgroundBorder::DrawArc(Vector2f pos_center, Vector2f r, float a0, float a1, Colourb color0, Colourb color1, int num_points)
  201. {
  202. RMLUI_ASSERT(num_points >= 2 && r.x > 0 && r.y > 0);
  203. const int offset_vertices = (int)vertices.size();
  204. vertices.resize(offset_vertices + num_points);
  205. for (int i = 0; i < num_points; i++)
  206. {
  207. const float t = float(i) / float(num_points - 1);
  208. const float a = Math::Lerp(t, a0, a1);
  209. const Colourb color = Math::Lerp(t, color0, color1);
  210. const Vector2f unit_vector(Math::Cos(a), Math::Sin(a));
  211. vertices[offset_vertices + i].position = unit_vector * r + pos_center;
  212. vertices[offset_vertices + i].colour = color;
  213. }
  214. }
  215. void GeometryBackgroundBorder::FillBackground(int index_start)
  216. {
  217. const int num_added_vertices = (int)vertices.size() - index_start;
  218. const int offset_indices = (int)indices.size();
  219. const int num_triangles = (num_added_vertices - 2);
  220. indices.resize(offset_indices + 3 * num_triangles);
  221. for (int i = 0; i < num_triangles; i++)
  222. {
  223. indices[offset_indices + 3 * i] = index_start;
  224. indices[offset_indices + 3 * i + 1] = index_start + i + 2;
  225. indices[offset_indices + 3 * i + 2] = index_start + i + 1;
  226. }
  227. }
  228. void GeometryBackgroundBorder::DrawBorderCorner(Corner corner, Vector2f pos_outer, Vector2f pos_inner, Vector2f pos_circle_center, float R, Vector2f r, Colourb color0, Colourb color1)
  229. {
  230. const float a0 = float((int)corner + 2) * 0.5f * Math::RMLUI_PI;
  231. const float a1 = float((int)corner + 3) * 0.5f * Math::RMLUI_PI;
  232. if (R == 0)
  233. {
  234. DrawPointPoint(pos_outer, pos_inner, color0, color1);
  235. }
  236. else if (r.x > 0 && r.y > 0)
  237. {
  238. DrawArcArc(pos_circle_center, R, r, a0, a1, color0, color1, GetNumPoints(R));
  239. }
  240. else
  241. {
  242. DrawArcPoint(pos_circle_center, pos_inner, R, a0, a1, color0, color1, GetNumPoints(R));
  243. }
  244. }
  245. void GeometryBackgroundBorder::DrawPointPoint(Vector2f pos_outer, Vector2f pos_inner, Colourb color0, Colourb color1)
  246. {
  247. const bool different_color = (color0 != color1);
  248. vertices.reserve((int)vertices.size() + (different_color ? 4 : 2));
  249. DrawPoint(pos_inner, color0);
  250. DrawPoint(pos_outer, color0);
  251. if (different_color)
  252. {
  253. DrawPoint(pos_inner, color1);
  254. DrawPoint(pos_outer, color1);
  255. }
  256. }
  257. void GeometryBackgroundBorder::DrawArcArc(Vector2f pos_center, float R, Vector2f r, float a0, float a1, Colourb color0, Colourb color1, int num_points)
  258. {
  259. RMLUI_ASSERT(num_points >= 2 && R > 0 && r.x > 0 && r.y > 0);
  260. const int num_triangles = 2 * (num_points - 1);
  261. const int offset_vertices = (int)vertices.size();
  262. const int offset_indices = (int)indices.size();
  263. vertices.resize(offset_vertices + 2 * num_points);
  264. indices.resize(offset_indices + 3 * num_triangles);
  265. for (int i = 0; i < num_points; i++)
  266. {
  267. const float t = float(i) / float(num_points - 1);
  268. const float a = Math::Lerp(t, a0, a1);
  269. const Colourb color = Math::Lerp(t, color0, color1);
  270. const Vector2f unit_vector(Math::Cos(a), Math::Sin(a));
  271. vertices[offset_vertices + 2 * i].position = unit_vector * r + pos_center;
  272. vertices[offset_vertices + 2 * i].colour = color;
  273. vertices[offset_vertices + 2 * i + 1].position = unit_vector * R + pos_center;
  274. vertices[offset_vertices + 2 * i + 1].colour = color;
  275. }
  276. for (int i = 0; i < num_triangles; i += 2)
  277. {
  278. indices[offset_indices + 3 * i + 0] = offset_vertices + i + 0;
  279. indices[offset_indices + 3 * i + 1] = offset_vertices + i + 2;
  280. indices[offset_indices + 3 * i + 2] = offset_vertices + i + 1;
  281. indices[offset_indices + 3 * i + 3] = offset_vertices + i + 1;
  282. indices[offset_indices + 3 * i + 4] = offset_vertices + i + 2;
  283. indices[offset_indices + 3 * i + 5] = offset_vertices + i + 3;
  284. }
  285. }
  286. void GeometryBackgroundBorder::DrawArcPoint(Vector2f pos_center, Vector2f pos_inner, float R, float a0, float a1, Colourb color0, Colourb color1, int num_points)
  287. {
  288. RMLUI_ASSERT(R > 0 && num_points >= 2);
  289. const int offset_vertices = (int)vertices.size();
  290. vertices.reserve(offset_vertices + num_points + 2);
  291. // Generate the vertices. We could also split the arc mid-way to create a sharp color transition.
  292. DrawPoint(pos_inner, color0);
  293. DrawArc(pos_center, Vector2f(R), a0, a1, color0, color1, num_points);
  294. DrawPoint(pos_inner, color1);
  295. RMLUI_ASSERT((int)vertices.size() - offset_vertices == num_points + 2);
  296. // Swap the last two vertices such that the outer edge vertex is last, see the comment for the border drawing functions. Their colors should already be the same.
  297. const int last_vertex = (int)vertices.size() - 1;
  298. std::swap(vertices[last_vertex - 1].position, vertices[last_vertex].position);
  299. // Generate the indices
  300. const int num_triangles = (num_points - 1);
  301. const int i_vertex_inner0 = offset_vertices;
  302. const int i_vertex_inner1 = last_vertex - 1;
  303. const int offset_indices = (int)indices.size();
  304. indices.resize(offset_indices + 3 * num_triangles);
  305. for (int i = 0; i < num_triangles; i++)
  306. {
  307. indices[offset_indices + 3 * i + 0] = (i > num_triangles / 2 ? i_vertex_inner1 : i_vertex_inner0);
  308. indices[offset_indices + 3 * i + 1] = offset_vertices + i + 2;
  309. indices[offset_indices + 3 * i + 2] = offset_vertices + i + 1;
  310. }
  311. // Since we swapped the last two vertices we also need to change the last triangle.
  312. indices[offset_indices + 3 * (num_triangles - 1) + 1] = last_vertex;
  313. }
  314. void GeometryBackgroundBorder::FillEdge(int index_next_corner)
  315. {
  316. const int offset_indices = (int)indices.size();
  317. const int num_vertices = (int)vertices.size();
  318. RMLUI_ASSERT(num_vertices >= 2);
  319. indices.resize(offset_indices + 6);
  320. indices[offset_indices + 0] = num_vertices - 2;
  321. indices[offset_indices + 1] = index_next_corner;
  322. indices[offset_indices + 2] = num_vertices - 1;
  323. indices[offset_indices + 3] = num_vertices - 1;
  324. indices[offset_indices + 4] = index_next_corner;
  325. indices[offset_indices + 5] = index_next_corner + 1;
  326. }
  327. int GeometryBackgroundBorder::GetNumPoints(float R) const
  328. {
  329. return Math::Clamp(3 + Math::RoundToInteger(R / 6.f), 2, 100);
  330. }
  331. } // namespace Rml