GeometryBackgroundBorder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include "GeometryBackgroundBorder.h"
  2. #include "../../Include/RmlUi/Core/Box.h"
  3. #include "../../Include/RmlUi/Core/Math.h"
  4. #include <algorithm>
  5. #include <float.h>
  6. namespace Rml {
  7. GeometryBackgroundBorder::GeometryBackgroundBorder(Vector<Vertex>& vertices, Vector<int>& indices) : vertices(vertices), indices(indices) {}
  8. BorderMetrics GeometryBackgroundBorder::ComputeBorderMetrics(Vector2f outer_position, EdgeSizes edge_sizes, Vector2f inner_size,
  9. CornerSizes outer_radii_def)
  10. {
  11. BorderMetrics metrics = {};
  12. // -- Find the corner positions --
  13. const Vector2f inner_position = outer_position + Vector2f(edge_sizes[LEFT], edge_sizes[TOP]);
  14. const Vector2f outer_size = inner_size + Vector2f(edge_sizes[LEFT] + edge_sizes[RIGHT], edge_sizes[TOP] + edge_sizes[BOTTOM]);
  15. metrics.positions_outer = {
  16. outer_position,
  17. outer_position + Vector2f(outer_size.x, 0),
  18. outer_position + outer_size,
  19. outer_position + Vector2f(0, outer_size.y),
  20. };
  21. metrics.positions_inner = {
  22. inner_position,
  23. inner_position + Vector2f(inner_size.x, 0),
  24. inner_position + inner_size,
  25. inner_position + Vector2f(0, inner_size.y),
  26. };
  27. // -- For curved borders, find the positions to draw ellipses around, and the scaled outer and inner radii --
  28. const float sum_radius = (outer_radii_def[TOP_LEFT] + outer_radii_def[TOP_RIGHT] + outer_radii_def[BOTTOM_RIGHT] + outer_radii_def[BOTTOM_LEFT]);
  29. const bool has_radius = (sum_radius > 1.f);
  30. if (has_radius)
  31. {
  32. auto& outer_radii = metrics.outer_radii;
  33. outer_radii = outer_radii_def;
  34. // Scale the radii such that we have no overlapping curves.
  35. float scale_factor = FLT_MAX;
  36. scale_factor = Math::Min(scale_factor, inner_size.x / (outer_radii[TOP_LEFT] + outer_radii[TOP_RIGHT])); // Top
  37. scale_factor = Math::Min(scale_factor, inner_size.y / (outer_radii[TOP_RIGHT] + outer_radii[BOTTOM_RIGHT])); // Right
  38. scale_factor = Math::Min(scale_factor, inner_size.x / (outer_radii[BOTTOM_RIGHT] + outer_radii[BOTTOM_LEFT])); // Bottom
  39. scale_factor = Math::Min(scale_factor, inner_size.y / (outer_radii[BOTTOM_LEFT] + outer_radii[TOP_LEFT])); // Left
  40. scale_factor = Math::Min(1.0f, scale_factor);
  41. for (float& radius : outer_radii)
  42. radius = Math::Round(radius * scale_factor);
  43. // Place the circle/ellipse centers
  44. metrics.positions_circle_center = {
  45. metrics.positions_outer[TOP_LEFT] + Vector2f(1, 1) * outer_radii[TOP_LEFT],
  46. metrics.positions_outer[TOP_RIGHT] + Vector2f(-1, 1) * outer_radii[TOP_RIGHT],
  47. metrics.positions_outer[BOTTOM_RIGHT] + Vector2f(-1, -1) * outer_radii[BOTTOM_RIGHT],
  48. metrics.positions_outer[BOTTOM_LEFT] + Vector2f(1, -1) * outer_radii[BOTTOM_LEFT],
  49. };
  50. metrics.inner_radii = {
  51. Vector2f(outer_radii[TOP_LEFT]) - Vector2f(edge_sizes[LEFT], edge_sizes[TOP]),
  52. Vector2f(outer_radii[TOP_RIGHT]) - Vector2f(edge_sizes[RIGHT], edge_sizes[TOP]),
  53. Vector2f(outer_radii[BOTTOM_RIGHT]) - Vector2f(edge_sizes[RIGHT], edge_sizes[BOTTOM]),
  54. Vector2f(outer_radii[BOTTOM_LEFT]) - Vector2f(edge_sizes[LEFT], edge_sizes[BOTTOM]),
  55. };
  56. }
  57. return metrics;
  58. }
  59. void GeometryBackgroundBorder::DrawBackground(const BorderMetrics& metrics, ColourbPremultiplied color)
  60. {
  61. const int offset_vertices = (int)vertices.size();
  62. for (int corner = 0; corner < 4; corner++)
  63. DrawBackgroundCorner(Corner(corner), metrics.positions_inner[corner], metrics.positions_circle_center[corner], metrics.outer_radii[corner],
  64. metrics.inner_radii[corner], color);
  65. FillBackground(offset_vertices);
  66. }
  67. void GeometryBackgroundBorder::DrawBorder(const BorderMetrics& metrics, EdgeSizes edge_sizes, const ColourbPremultiplied border_colors[4])
  68. {
  69. RMLUI_ASSERT(border_colors);
  70. const int offset_vertices = (int)vertices.size();
  71. const bool draw_edge[4] = {
  72. edge_sizes[TOP] > 0 && border_colors[TOP].alpha > 0,
  73. edge_sizes[RIGHT] > 0 && border_colors[RIGHT].alpha > 0,
  74. edge_sizes[BOTTOM] > 0 && border_colors[BOTTOM].alpha > 0,
  75. edge_sizes[LEFT] > 0 && border_colors[LEFT].alpha > 0,
  76. };
  77. const bool draw_corner[4] = {
  78. draw_edge[TOP] || draw_edge[LEFT],
  79. draw_edge[TOP] || draw_edge[RIGHT],
  80. draw_edge[BOTTOM] || draw_edge[RIGHT],
  81. draw_edge[BOTTOM] || draw_edge[LEFT],
  82. };
  83. for (int corner = 0; corner < 4; corner++)
  84. {
  85. const Edge edge0 = Edge((corner + 3) % 4);
  86. const Edge edge1 = Edge(corner);
  87. if (draw_corner[corner])
  88. {
  89. DrawBorderCorner(Corner(corner), metrics.positions_outer[corner], metrics.positions_inner[corner],
  90. metrics.positions_circle_center[corner], metrics.outer_radii[corner], metrics.inner_radii[corner], border_colors[edge0],
  91. border_colors[edge1]);
  92. }
  93. if (draw_edge[edge1])
  94. {
  95. RMLUI_ASSERTMSG(draw_corner[corner] && draw_corner[(corner + 1) % 4],
  96. "Border edges can only be drawn if both of its connected corners are drawn.");
  97. FillEdge(edge1 == LEFT ? offset_vertices : (int)vertices.size());
  98. }
  99. }
  100. }
  101. void GeometryBackgroundBorder::DrawBackgroundCorner(Corner corner, Vector2f pos_inner, Vector2f pos_circle_center, float R, Vector2f r,
  102. ColourbPremultiplied color)
  103. {
  104. if (R == 0 || r.x <= 0 || r.y <= 0)
  105. {
  106. DrawPoint(pos_inner, color);
  107. }
  108. else if (r.x > 0 && r.y > 0)
  109. {
  110. const float a0 = float((int)corner + 2) * 0.5f * Math::RMLUI_PI;
  111. const float a1 = float((int)corner + 3) * 0.5f * Math::RMLUI_PI;
  112. const int num_points = GetNumPoints(R);
  113. DrawArc(pos_circle_center, r, a0, a1, color, color, num_points);
  114. }
  115. }
  116. void GeometryBackgroundBorder::DrawPoint(Vector2f pos, ColourbPremultiplied color)
  117. {
  118. const int offset_vertices = (int)vertices.size();
  119. vertices.resize(offset_vertices + 1);
  120. vertices[offset_vertices].position = pos;
  121. vertices[offset_vertices].colour = color;
  122. }
  123. void GeometryBackgroundBorder::DrawArc(Vector2f pos_center, Vector2f r, float a0, float a1, ColourbPremultiplied color0, ColourbPremultiplied color1,
  124. int num_points)
  125. {
  126. RMLUI_ASSERT(num_points >= 2 && r.x > 0 && r.y > 0);
  127. const int offset_vertices = (int)vertices.size();
  128. vertices.resize(offset_vertices + num_points);
  129. for (int i = 0; i < num_points; i++)
  130. {
  131. const float t = float(i) / float(num_points - 1);
  132. const float a = Math::Lerp(t, a0, a1);
  133. const Vector2f unit_vector(Math::Cos(a), Math::Sin(a));
  134. vertices[offset_vertices + i].position = unit_vector * r + pos_center;
  135. vertices[offset_vertices + i].colour = Math::RoundedLerp(t, color0, color1);
  136. }
  137. }
  138. void GeometryBackgroundBorder::FillBackground(int index_start)
  139. {
  140. const int num_added_vertices = (int)vertices.size() - index_start;
  141. const int offset_indices = (int)indices.size();
  142. const int num_triangles = (num_added_vertices - 2);
  143. indices.resize(offset_indices + 3 * num_triangles);
  144. for (int i = 0; i < num_triangles; i++)
  145. {
  146. indices[offset_indices + 3 * i] = index_start;
  147. indices[offset_indices + 3 * i + 1] = index_start + i + 2;
  148. indices[offset_indices + 3 * i + 2] = index_start + i + 1;
  149. }
  150. }
  151. void GeometryBackgroundBorder::DrawBorderCorner(Corner corner, Vector2f pos_outer, Vector2f pos_inner, Vector2f pos_circle_center, float R,
  152. Vector2f r, ColourbPremultiplied color0, ColourbPremultiplied color1)
  153. {
  154. const float a0 = float((int)corner + 2) * 0.5f * Math::RMLUI_PI;
  155. const float a1 = float((int)corner + 3) * 0.5f * Math::RMLUI_PI;
  156. if (R == 0)
  157. {
  158. DrawPointPoint(pos_outer, pos_inner, color0, color1);
  159. }
  160. else if (r.x > 0 && r.y > 0)
  161. {
  162. DrawArcArc(pos_circle_center, R, r, a0, a1, color0, color1, GetNumPoints(R));
  163. }
  164. else
  165. {
  166. DrawArcPoint(pos_circle_center, pos_inner, R, a0, a1, color0, color1, GetNumPoints(R));
  167. }
  168. }
  169. void GeometryBackgroundBorder::DrawPointPoint(Vector2f pos_outer, Vector2f pos_inner, ColourbPremultiplied color0, ColourbPremultiplied color1)
  170. {
  171. const bool different_color = (color0 != color1);
  172. vertices.reserve((int)vertices.size() + (different_color ? 4 : 2));
  173. DrawPoint(pos_inner, color0);
  174. DrawPoint(pos_outer, color0);
  175. if (different_color)
  176. {
  177. DrawPoint(pos_inner, color1);
  178. DrawPoint(pos_outer, color1);
  179. }
  180. }
  181. void GeometryBackgroundBorder::DrawArcArc(Vector2f pos_center, float R, Vector2f r, float a0, float a1, ColourbPremultiplied color0,
  182. ColourbPremultiplied color1, int num_points)
  183. {
  184. RMLUI_ASSERT(num_points >= 2 && R > 0 && r.x > 0 && r.y > 0);
  185. const int num_triangles = 2 * (num_points - 1);
  186. const int offset_vertices = (int)vertices.size();
  187. const int offset_indices = (int)indices.size();
  188. vertices.resize(offset_vertices + 2 * num_points);
  189. indices.resize(offset_indices + 3 * num_triangles);
  190. for (int i = 0; i < num_points; i++)
  191. {
  192. const float t = float(i) / float(num_points - 1);
  193. const float a = Math::Lerp(t, a0, a1);
  194. const ColourbPremultiplied color = Math::RoundedLerp(t, color0, color1);
  195. const Vector2f unit_vector(Math::Cos(a), Math::Sin(a));
  196. vertices[offset_vertices + 2 * i].position = unit_vector * r + pos_center;
  197. vertices[offset_vertices + 2 * i].colour = color;
  198. vertices[offset_vertices + 2 * i + 1].position = unit_vector * R + pos_center;
  199. vertices[offset_vertices + 2 * i + 1].colour = color;
  200. }
  201. for (int i = 0; i < num_triangles; i += 2)
  202. {
  203. indices[offset_indices + 3 * i + 0] = offset_vertices + i + 0;
  204. indices[offset_indices + 3 * i + 1] = offset_vertices + i + 2;
  205. indices[offset_indices + 3 * i + 2] = offset_vertices + i + 1;
  206. indices[offset_indices + 3 * i + 3] = offset_vertices + i + 1;
  207. indices[offset_indices + 3 * i + 4] = offset_vertices + i + 2;
  208. indices[offset_indices + 3 * i + 5] = offset_vertices + i + 3;
  209. }
  210. }
  211. void GeometryBackgroundBorder::DrawArcPoint(Vector2f pos_center, Vector2f pos_inner, float R, float a0, float a1, ColourbPremultiplied color0,
  212. ColourbPremultiplied color1, int num_points)
  213. {
  214. RMLUI_ASSERT(R > 0 && num_points >= 2);
  215. const int offset_vertices = (int)vertices.size();
  216. vertices.reserve(offset_vertices + num_points + 2);
  217. // Generate the vertices. We could also split the arc mid-way to create a sharp color transition.
  218. DrawPoint(pos_inner, color0);
  219. DrawArc(pos_center, Vector2f(R), a0, a1, color0, color1, num_points);
  220. DrawPoint(pos_inner, color1);
  221. RMLUI_ASSERT((int)vertices.size() - offset_vertices == num_points + 2);
  222. // Swap the last two vertices such that the outer edge vertex is last, see the comment for the border drawing functions. Their colors should
  223. // already be the same.
  224. const int last_vertex = (int)vertices.size() - 1;
  225. std::swap(vertices[last_vertex - 1].position, vertices[last_vertex].position);
  226. // Generate the indices
  227. const int num_triangles = (num_points - 1);
  228. const int i_vertex_inner0 = offset_vertices;
  229. const int i_vertex_inner1 = last_vertex - 1;
  230. const int offset_indices = (int)indices.size();
  231. indices.resize(offset_indices + 3 * num_triangles);
  232. for (int i = 0; i < num_triangles; i++)
  233. {
  234. indices[offset_indices + 3 * i + 0] = (i > num_triangles / 2 ? i_vertex_inner1 : i_vertex_inner0);
  235. indices[offset_indices + 3 * i + 1] = offset_vertices + i + 2;
  236. indices[offset_indices + 3 * i + 2] = offset_vertices + i + 1;
  237. }
  238. // Since we swapped the last two vertices we also need to change the last triangle.
  239. indices[offset_indices + 3 * (num_triangles - 1) + 1] = last_vertex;
  240. }
  241. void GeometryBackgroundBorder::FillEdge(int index_next_corner)
  242. {
  243. const int offset_indices = (int)indices.size();
  244. const int num_vertices = (int)vertices.size();
  245. RMLUI_ASSERT(num_vertices >= 2);
  246. indices.resize(offset_indices + 6);
  247. indices[offset_indices + 0] = num_vertices - 2;
  248. indices[offset_indices + 1] = index_next_corner;
  249. indices[offset_indices + 2] = num_vertices - 1;
  250. indices[offset_indices + 3] = num_vertices - 1;
  251. indices[offset_indices + 4] = index_next_corner;
  252. indices[offset_indices + 5] = index_next_corner + 1;
  253. }
  254. int GeometryBackgroundBorder::GetNumPoints(float R) const
  255. {
  256. return Math::Clamp(3 + Math::RoundToInteger(R / 6.f), 2, 100);
  257. }
  258. } // namespace Rml