GeometryBackgroundBorder.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. void GeometryBackgroundBorder::Draw(Vector<Vertex>& vertices, Vector<int>& indices, CornerSizes radii, const Box& box, const Vector2f offset,
  36. const Colourb background_color, const Colourb* border_colors)
  37. {
  38. EdgeSizes border_widths = {
  39. Math::Round(box.GetEdge(BoxArea::Border, BoxEdge::Top)),
  40. Math::Round(box.GetEdge(BoxArea::Border, BoxEdge::Right)),
  41. Math::Round(box.GetEdge(BoxArea::Border, BoxEdge::Bottom)),
  42. Math::Round(box.GetEdge(BoxArea::Border, BoxEdge::Left)),
  43. };
  44. int num_borders = 0;
  45. if (border_colors)
  46. {
  47. for (int i = 0; i < 4; i++)
  48. if (border_colors[i].alpha > 0 && border_widths[i] > 0)
  49. num_borders += 1;
  50. }
  51. const Vector2f padding_size = box.GetSize(BoxArea::Padding).Round();
  52. const bool has_background = (background_color.alpha > 0 && padding_size.x > 0 && padding_size.y > 0);
  53. const bool has_border = (num_borders > 0);
  54. if (!has_background && !has_border)
  55. return;
  56. // -- Find the corner positions --
  57. const Vector2f border_position = offset.Round();
  58. const Vector2f padding_position = border_position + Vector2f(border_widths[Edge::LEFT], border_widths[Edge::TOP]);
  59. const Vector2f border_size =
  60. 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::Round(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],
  124. inner_radii[corner], background_color);
  125. geometry.FillBackground(offset_vertices);
  126. }
  127. // Draw the border
  128. if (has_border)
  129. {
  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],
  153. "Border edges can only be drawn if both of its connected corners are drawn.");
  154. geometry.FillEdge(edge1 == Edge::LEFT ? offset_vertices : (int)vertices.size());
  155. }
  156. }
  157. }
  158. #if 0
  159. // Debug draw vertices
  160. if (has_radius)
  161. {
  162. const int num_vertices = vertices.size();
  163. const int num_indices = indices.size();
  164. vertices.resize(num_vertices + 4 * num_vertices);
  165. indices.resize(num_indices + 6 * num_indices);
  166. for (int i = 0; i < num_vertices; i++)
  167. {
  168. 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);
  169. }
  170. }
  171. #endif
  172. #ifdef RMLUI_DEBUG
  173. const int num_vertices = (int)vertices.size();
  174. for (int index : indices)
  175. {
  176. RMLUI_ASSERT(index < num_vertices);
  177. }
  178. #endif
  179. }
  180. void GeometryBackgroundBorder::DrawBackgroundCorner(Corner corner, Vector2f pos_inner, Vector2f pos_circle_center, float R, Vector2f r, Colourb color)
  181. {
  182. if (R == 0 || r.x <= 0 || r.y <= 0)
  183. {
  184. DrawPoint(pos_inner, color);
  185. }
  186. else if (r.x > 0 && r.y > 0)
  187. {
  188. const float a0 = float((int)corner + 2) * 0.5f * Math::RMLUI_PI;
  189. const float a1 = float((int)corner + 3) * 0.5f * Math::RMLUI_PI;
  190. const int num_points = GetNumPoints(R);
  191. DrawArc(pos_circle_center, r, a0, a1, color, color, num_points);
  192. }
  193. }
  194. void GeometryBackgroundBorder::DrawPoint(Vector2f pos, Colourb color)
  195. {
  196. const int offset_vertices = (int)vertices.size();
  197. vertices.resize(offset_vertices + 1);
  198. vertices[offset_vertices].position = pos;
  199. vertices[offset_vertices].colour = color;
  200. }
  201. void GeometryBackgroundBorder::DrawArc(Vector2f pos_center, Vector2f r, float a0, float a1, Colourb color0, Colourb color1, int num_points)
  202. {
  203. RMLUI_ASSERT(num_points >= 2 && r.x > 0 && r.y > 0);
  204. const int offset_vertices = (int)vertices.size();
  205. vertices.resize(offset_vertices + num_points);
  206. for (int i = 0; i < num_points; i++)
  207. {
  208. const float t = float(i) / float(num_points - 1);
  209. const float a = Math::Lerp(t, a0, a1);
  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 = Math::RoundedLerp(t, color0, color1);
  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,
  229. Vector2f r, Colourb color0, Colourb color1)
  230. {
  231. const float a0 = float((int)corner + 2) * 0.5f * Math::RMLUI_PI;
  232. const float a1 = float((int)corner + 3) * 0.5f * Math::RMLUI_PI;
  233. if (R == 0)
  234. {
  235. DrawPointPoint(pos_outer, pos_inner, color0, color1);
  236. }
  237. else if (r.x > 0 && r.y > 0)
  238. {
  239. DrawArcArc(pos_circle_center, R, r, a0, a1, color0, color1, GetNumPoints(R));
  240. }
  241. else
  242. {
  243. DrawArcPoint(pos_circle_center, pos_inner, R, a0, a1, color0, color1, GetNumPoints(R));
  244. }
  245. }
  246. void GeometryBackgroundBorder::DrawPointPoint(Vector2f pos_outer, Vector2f pos_inner, Colourb color0, Colourb color1)
  247. {
  248. const bool different_color = (color0 != color1);
  249. vertices.reserve((int)vertices.size() + (different_color ? 4 : 2));
  250. DrawPoint(pos_inner, color0);
  251. DrawPoint(pos_outer, color0);
  252. if (different_color)
  253. {
  254. DrawPoint(pos_inner, color1);
  255. DrawPoint(pos_outer, color1);
  256. }
  257. }
  258. void GeometryBackgroundBorder::DrawArcArc(Vector2f pos_center, float R, Vector2f r, float a0, float a1, Colourb color0, Colourb color1,
  259. int num_points)
  260. {
  261. RMLUI_ASSERT(num_points >= 2 && R > 0 && r.x > 0 && r.y > 0);
  262. const int num_triangles = 2 * (num_points - 1);
  263. const int offset_vertices = (int)vertices.size();
  264. const int offset_indices = (int)indices.size();
  265. vertices.resize(offset_vertices + 2 * num_points);
  266. indices.resize(offset_indices + 3 * num_triangles);
  267. for (int i = 0; i < num_points; i++)
  268. {
  269. const float t = float(i) / float(num_points - 1);
  270. const float a = Math::Lerp(t, a0, a1);
  271. const Colourb color = Math::RoundedLerp(t, color0, color1);
  272. const Vector2f unit_vector(Math::Cos(a), Math::Sin(a));
  273. vertices[offset_vertices + 2 * i].position = unit_vector * r + pos_center;
  274. vertices[offset_vertices + 2 * i].colour = color;
  275. vertices[offset_vertices + 2 * i + 1].position = unit_vector * R + pos_center;
  276. vertices[offset_vertices + 2 * i + 1].colour = color;
  277. }
  278. for (int i = 0; i < num_triangles; i += 2)
  279. {
  280. indices[offset_indices + 3 * i + 0] = offset_vertices + i + 0;
  281. indices[offset_indices + 3 * i + 1] = offset_vertices + i + 2;
  282. indices[offset_indices + 3 * i + 2] = offset_vertices + i + 1;
  283. indices[offset_indices + 3 * i + 3] = offset_vertices + i + 1;
  284. indices[offset_indices + 3 * i + 4] = offset_vertices + i + 2;
  285. indices[offset_indices + 3 * i + 5] = offset_vertices + i + 3;
  286. }
  287. }
  288. void GeometryBackgroundBorder::DrawArcPoint(Vector2f pos_center, Vector2f pos_inner, float R, float a0, float a1, Colourb color0, Colourb color1,
  289. int num_points)
  290. {
  291. RMLUI_ASSERT(R > 0 && num_points >= 2);
  292. const int offset_vertices = (int)vertices.size();
  293. vertices.reserve(offset_vertices + num_points + 2);
  294. // Generate the vertices. We could also split the arc mid-way to create a sharp color transition.
  295. DrawPoint(pos_inner, color0);
  296. DrawArc(pos_center, Vector2f(R), a0, a1, color0, color1, num_points);
  297. DrawPoint(pos_inner, color1);
  298. RMLUI_ASSERT((int)vertices.size() - offset_vertices == num_points + 2);
  299. // Swap the last two vertices such that the outer edge vertex is last, see the comment for the border drawing functions. Their colors should
  300. // already be the same.
  301. const int last_vertex = (int)vertices.size() - 1;
  302. std::swap(vertices[last_vertex - 1].position, vertices[last_vertex].position);
  303. // Generate the indices
  304. const int num_triangles = (num_points - 1);
  305. const int i_vertex_inner0 = offset_vertices;
  306. const int i_vertex_inner1 = last_vertex - 1;
  307. const int offset_indices = (int)indices.size();
  308. indices.resize(offset_indices + 3 * num_triangles);
  309. for (int i = 0; i < num_triangles; i++)
  310. {
  311. indices[offset_indices + 3 * i + 0] = (i > num_triangles / 2 ? i_vertex_inner1 : i_vertex_inner0);
  312. indices[offset_indices + 3 * i + 1] = offset_vertices + i + 2;
  313. indices[offset_indices + 3 * i + 2] = offset_vertices + i + 1;
  314. }
  315. // Since we swapped the last two vertices we also need to change the last triangle.
  316. indices[offset_indices + 3 * (num_triangles - 1) + 1] = last_vertex;
  317. }
  318. void GeometryBackgroundBorder::FillEdge(int index_next_corner)
  319. {
  320. const int offset_indices = (int)indices.size();
  321. const int num_vertices = (int)vertices.size();
  322. RMLUI_ASSERT(num_vertices >= 2);
  323. indices.resize(offset_indices + 6);
  324. indices[offset_indices + 0] = num_vertices - 2;
  325. indices[offset_indices + 1] = index_next_corner;
  326. indices[offset_indices + 2] = num_vertices - 1;
  327. indices[offset_indices + 3] = num_vertices - 1;
  328. indices[offset_indices + 4] = index_next_corner;
  329. indices[offset_indices + 5] = index_next_corner + 1;
  330. }
  331. int GeometryBackgroundBorder::GetNumPoints(float R) const
  332. {
  333. return Math::Clamp(3 + Math::RoundToInteger(R / 6.f), 2, 100);
  334. }
  335. } // namespace Rml