GeometryBackgroundBorder.cpp 13 KB

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