GeometryBackgroundBorder.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. using Edge = Box::Edge;
  39. EdgeSizes border_widths = {
  40. Math::Round(box.GetEdge(Box::BORDER, Edge::TOP)),
  41. Math::Round(box.GetEdge(Box::BORDER, Edge::RIGHT)),
  42. Math::Round(box.GetEdge(Box::BORDER, Edge::BOTTOM)),
  43. Math::Round(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 = offset.Round();
  59. const Vector2f padding_position = border_position + Vector2f(border_widths[Edge::LEFT], border_widths[Edge::TOP]);
  60. const Vector2f border_size =
  61. padding_size + Vector2f(border_widths[Edge::LEFT] + border_widths[Edge::RIGHT], border_widths[Edge::TOP] + border_widths[Edge::BOTTOM]);
  62. // Border edge positions
  63. CornerPositions positions_outer = {
  64. border_position,
  65. border_position + Vector2f(border_size.x, 0),
  66. border_position + border_size,
  67. border_position + Vector2f(0, border_size.y),
  68. };
  69. // Padding edge positions
  70. CornerPositions positions_inner = {
  71. padding_position,
  72. padding_position + Vector2f(padding_size.x, 0),
  73. padding_position + padding_size,
  74. padding_position + Vector2f(0, padding_size.y),
  75. };
  76. // -- For curved borders, find the positions to draw ellipses around, and the scaled outer and inner radii --
  77. const float sum_radius = (radii[TOP_LEFT] + radii[TOP_RIGHT] + radii[BOTTOM_RIGHT] + radii[BOTTOM_LEFT]);
  78. const bool has_radius = (sum_radius > 1.f);
  79. // Curved borders are drawn as circles (outer border) and ellipses (inner border) around the centers.
  80. CornerPositions positions_circle_center;
  81. // Radii of the padding edges, 2-dimensional as these can be ellipses.
  82. // The inner radii is effectively the (signed) distance from the circle center to the padding edge.
  83. // They can also be zero or negative, in which case a sharp corner should be drawn instead of an arc.
  84. CornerSizes2 inner_radii;
  85. if (has_radius)
  86. {
  87. // Scale the radii such that we have no overlapping curves.
  88. float scale_factor = FLT_MAX;
  89. scale_factor = Math::Min(scale_factor, padding_size.x / (radii[TOP_LEFT] + radii[TOP_RIGHT])); // Top
  90. scale_factor = Math::Min(scale_factor, padding_size.y / (radii[TOP_RIGHT] + radii[BOTTOM_RIGHT])); // Right
  91. scale_factor = Math::Min(scale_factor, padding_size.x / (radii[BOTTOM_RIGHT] + radii[BOTTOM_LEFT])); // Bottom
  92. scale_factor = Math::Min(scale_factor, padding_size.y / (radii[BOTTOM_LEFT] + radii[TOP_LEFT])); // Left
  93. scale_factor = Math::Min(1.0f, scale_factor);
  94. for (float& radius : radii)
  95. radius = Math::Round(radius * scale_factor);
  96. // Place the circle/ellipse centers
  97. positions_circle_center = {
  98. positions_outer[TOP_LEFT] + Vector2f(1, 1) * radii[TOP_LEFT],
  99. positions_outer[TOP_RIGHT] + Vector2f(-1, 1) * radii[TOP_RIGHT],
  100. positions_outer[BOTTOM_RIGHT] + Vector2f(-1, -1) * radii[BOTTOM_RIGHT],
  101. positions_outer[BOTTOM_LEFT] + Vector2f(1, -1) * radii[BOTTOM_LEFT],
  102. };
  103. inner_radii = {
  104. Vector2f(radii[TOP_LEFT]) - Vector2f(border_widths[Edge::LEFT], border_widths[Edge::TOP]),
  105. Vector2f(radii[TOP_RIGHT]) - Vector2f(border_widths[Edge::RIGHT], border_widths[Edge::TOP]),
  106. Vector2f(radii[BOTTOM_RIGHT]) - Vector2f(border_widths[Edge::RIGHT], border_widths[Edge::BOTTOM]),
  107. Vector2f(radii[BOTTOM_LEFT]) - Vector2f(border_widths[Edge::LEFT], border_widths[Edge::BOTTOM]),
  108. };
  109. }
  110. // -- Generate the geometry --
  111. GeometryBackgroundBorder geometry(vertices, indices);
  112. {
  113. // Reserve geometry. A conservative estimate, does not take border-radii into account and assumes same-colored borders.
  114. const int estimated_num_vertices = 4 * int(has_background) + 2 * num_borders;
  115. const int estimated_num_triangles = 2 * int(has_background) + 2 * num_borders;
  116. vertices.reserve((int)vertices.size() + estimated_num_vertices);
  117. indices.reserve((int)indices.size() + 3 * estimated_num_triangles);
  118. }
  119. // Draw the background
  120. if (has_background)
  121. {
  122. const int offset_vertices = (int)vertices.size();
  123. for (int corner = 0; corner < 4; corner++)
  124. geometry.DrawBackgroundCorner(Corner(corner), positions_inner[corner], positions_circle_center[corner], radii[corner],
  125. inner_radii[corner], background_color);
  126. geometry.FillBackground(offset_vertices);
  127. }
  128. // Draw the border
  129. if (has_border)
  130. {
  131. using Edge = Box::Edge;
  132. const int offset_vertices = (int)vertices.size();
  133. const bool draw_edge[4] = {
  134. border_widths[Edge::TOP] > 0 && border_colors[Edge::TOP].alpha > 0,
  135. border_widths[Edge::RIGHT] > 0 && border_colors[Edge::RIGHT].alpha > 0,
  136. border_widths[Edge::BOTTOM] > 0 && border_colors[Edge::BOTTOM].alpha > 0,
  137. border_widths[Edge::LEFT] > 0 && border_colors[Edge::LEFT].alpha > 0,
  138. };
  139. const bool draw_corner[4] = {
  140. draw_edge[Edge::TOP] || draw_edge[Edge::LEFT],
  141. draw_edge[Edge::TOP] || draw_edge[Edge::RIGHT],
  142. draw_edge[Edge::BOTTOM] || draw_edge[Edge::RIGHT],
  143. draw_edge[Edge::BOTTOM] || draw_edge[Edge::LEFT],
  144. };
  145. for (int corner = 0; corner < 4; corner++)
  146. {
  147. const Edge edge0 = Edge((corner + 3) % 4);
  148. const Edge edge1 = Edge(corner);
  149. if (draw_corner[corner])
  150. geometry.DrawBorderCorner(Corner(corner), positions_outer[corner], positions_inner[corner], positions_circle_center[corner],
  151. radii[corner], inner_radii[corner], border_colors[edge0], border_colors[edge1]);
  152. if (draw_edge[edge1])
  153. {
  154. RMLUI_ASSERTMSG(draw_corner[corner] && draw_corner[(corner + 1) % 4],
  155. "Border edges can only be drawn if both of its connected corners are drawn.");
  156. geometry.FillEdge(edge1 == Edge::LEFT ? offset_vertices : (int)vertices.size());
  157. }
  158. }
  159. }
  160. #if 0
  161. // Debug draw vertices
  162. if (has_radius)
  163. {
  164. const int num_vertices = vertices.size();
  165. const int num_indices = indices.size();
  166. vertices.resize(num_vertices + 4 * num_vertices);
  167. indices.resize(num_indices + 6 * num_indices);
  168. for (int i = 0; i < num_vertices; i++)
  169. {
  170. 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);
  171. }
  172. }
  173. #endif
  174. #ifdef RMLUI_DEBUG
  175. const int num_vertices = (int)vertices.size();
  176. for (int index : indices)
  177. {
  178. RMLUI_ASSERT(index < num_vertices);
  179. }
  180. #endif
  181. }
  182. void GeometryBackgroundBorder::DrawBackgroundCorner(Corner corner, Vector2f pos_inner, Vector2f pos_circle_center, float R, Vector2f r, Colourb color)
  183. {
  184. if (R == 0 || r.x <= 0 || r.y <= 0)
  185. {
  186. DrawPoint(pos_inner, color);
  187. }
  188. else if (r.x > 0 && r.y > 0)
  189. {
  190. const float a0 = float((int)corner + 2) * 0.5f * Math::RMLUI_PI;
  191. const float a1 = float((int)corner + 3) * 0.5f * Math::RMLUI_PI;
  192. const int num_points = GetNumPoints(R);
  193. DrawArc(pos_circle_center, r, a0, a1, color, color, num_points);
  194. }
  195. }
  196. void GeometryBackgroundBorder::DrawPoint(Vector2f pos, Colourb color)
  197. {
  198. const int offset_vertices = (int)vertices.size();
  199. vertices.resize(offset_vertices + 1);
  200. vertices[offset_vertices].position = pos;
  201. vertices[offset_vertices].colour = color;
  202. }
  203. void GeometryBackgroundBorder::DrawArc(Vector2f pos_center, Vector2f r, float a0, float a1, Colourb color0, Colourb color1, int num_points)
  204. {
  205. RMLUI_ASSERT(num_points >= 2 && r.x > 0 && r.y > 0);
  206. const int offset_vertices = (int)vertices.size();
  207. vertices.resize(offset_vertices + num_points);
  208. for (int i = 0; i < num_points; i++)
  209. {
  210. const float t = float(i) / float(num_points - 1);
  211. const float a = Math::Lerp(t, a0, a1);
  212. const Vector2f unit_vector(Math::Cos(a), Math::Sin(a));
  213. vertices[offset_vertices + i].position = unit_vector * r + pos_center;
  214. vertices[offset_vertices + i].colour = Math::RoundedLerp(t, color0, color1);
  215. }
  216. }
  217. void GeometryBackgroundBorder::FillBackground(int index_start)
  218. {
  219. const int num_added_vertices = (int)vertices.size() - index_start;
  220. const int offset_indices = (int)indices.size();
  221. const int num_triangles = (num_added_vertices - 2);
  222. indices.resize(offset_indices + 3 * num_triangles);
  223. for (int i = 0; i < num_triangles; i++)
  224. {
  225. indices[offset_indices + 3 * i] = index_start;
  226. indices[offset_indices + 3 * i + 1] = index_start + i + 2;
  227. indices[offset_indices + 3 * i + 2] = index_start + i + 1;
  228. }
  229. }
  230. void GeometryBackgroundBorder::DrawBorderCorner(Corner corner, Vector2f pos_outer, Vector2f pos_inner, Vector2f pos_circle_center, float R,
  231. Vector2f r, Colourb color0, Colourb color1)
  232. {
  233. const float a0 = float((int)corner + 2) * 0.5f * Math::RMLUI_PI;
  234. const float a1 = float((int)corner + 3) * 0.5f * Math::RMLUI_PI;
  235. if (R == 0)
  236. {
  237. DrawPointPoint(pos_outer, pos_inner, color0, color1);
  238. }
  239. else if (r.x > 0 && r.y > 0)
  240. {
  241. DrawArcArc(pos_circle_center, R, r, a0, a1, color0, color1, GetNumPoints(R));
  242. }
  243. else
  244. {
  245. DrawArcPoint(pos_circle_center, pos_inner, R, a0, a1, color0, color1, GetNumPoints(R));
  246. }
  247. }
  248. void GeometryBackgroundBorder::DrawPointPoint(Vector2f pos_outer, Vector2f pos_inner, Colourb color0, Colourb color1)
  249. {
  250. const bool different_color = (color0 != color1);
  251. vertices.reserve((int)vertices.size() + (different_color ? 4 : 2));
  252. DrawPoint(pos_inner, color0);
  253. DrawPoint(pos_outer, color0);
  254. if (different_color)
  255. {
  256. DrawPoint(pos_inner, color1);
  257. DrawPoint(pos_outer, color1);
  258. }
  259. }
  260. void GeometryBackgroundBorder::DrawArcArc(Vector2f pos_center, float R, Vector2f r, float a0, float a1, Colourb color0, Colourb color1,
  261. int num_points)
  262. {
  263. RMLUI_ASSERT(num_points >= 2 && R > 0 && r.x > 0 && r.y > 0);
  264. const int num_triangles = 2 * (num_points - 1);
  265. const int offset_vertices = (int)vertices.size();
  266. const int offset_indices = (int)indices.size();
  267. vertices.resize(offset_vertices + 2 * num_points);
  268. indices.resize(offset_indices + 3 * num_triangles);
  269. for (int i = 0; i < num_points; i++)
  270. {
  271. const float t = float(i) / float(num_points - 1);
  272. const float a = Math::Lerp(t, a0, a1);
  273. const Colourb color = Math::RoundedLerp(t, color0, color1);
  274. const Vector2f unit_vector(Math::Cos(a), Math::Sin(a));
  275. vertices[offset_vertices + 2 * i].position = unit_vector * r + pos_center;
  276. vertices[offset_vertices + 2 * i].colour = color;
  277. vertices[offset_vertices + 2 * i + 1].position = unit_vector * R + pos_center;
  278. vertices[offset_vertices + 2 * i + 1].colour = color;
  279. }
  280. for (int i = 0; i < num_triangles; i += 2)
  281. {
  282. indices[offset_indices + 3 * i + 0] = offset_vertices + i + 0;
  283. indices[offset_indices + 3 * i + 1] = offset_vertices + i + 2;
  284. indices[offset_indices + 3 * i + 2] = offset_vertices + i + 1;
  285. indices[offset_indices + 3 * i + 3] = offset_vertices + i + 1;
  286. indices[offset_indices + 3 * i + 4] = offset_vertices + i + 2;
  287. indices[offset_indices + 3 * i + 5] = offset_vertices + i + 3;
  288. }
  289. }
  290. void GeometryBackgroundBorder::DrawArcPoint(Vector2f pos_center, Vector2f pos_inner, float R, float a0, float a1, Colourb color0, Colourb color1,
  291. int num_points)
  292. {
  293. RMLUI_ASSERT(R > 0 && num_points >= 2);
  294. const int offset_vertices = (int)vertices.size();
  295. vertices.reserve(offset_vertices + num_points + 2);
  296. // Generate the vertices. We could also split the arc mid-way to create a sharp color transition.
  297. DrawPoint(pos_inner, color0);
  298. DrawArc(pos_center, Vector2f(R), a0, a1, color0, color1, num_points);
  299. DrawPoint(pos_inner, color1);
  300. RMLUI_ASSERT((int)vertices.size() - offset_vertices == num_points + 2);
  301. // Swap the last two vertices such that the outer edge vertex is last, see the comment for the border drawing functions. Their colors should
  302. // already be the same.
  303. const int last_vertex = (int)vertices.size() - 1;
  304. std::swap(vertices[last_vertex - 1].position, vertices[last_vertex].position);
  305. // Generate the indices
  306. const int num_triangles = (num_points - 1);
  307. const int i_vertex_inner0 = offset_vertices;
  308. const int i_vertex_inner1 = last_vertex - 1;
  309. const int offset_indices = (int)indices.size();
  310. indices.resize(offset_indices + 3 * num_triangles);
  311. for (int i = 0; i < num_triangles; i++)
  312. {
  313. indices[offset_indices + 3 * i + 0] = (i > num_triangles / 2 ? i_vertex_inner1 : i_vertex_inner0);
  314. indices[offset_indices + 3 * i + 1] = offset_vertices + i + 2;
  315. indices[offset_indices + 3 * i + 2] = offset_vertices + i + 1;
  316. }
  317. // Since we swapped the last two vertices we also need to change the last triangle.
  318. indices[offset_indices + 3 * (num_triangles - 1) + 1] = last_vertex;
  319. }
  320. void GeometryBackgroundBorder::FillEdge(int index_next_corner)
  321. {
  322. const int offset_indices = (int)indices.size();
  323. const int num_vertices = (int)vertices.size();
  324. RMLUI_ASSERT(num_vertices >= 2);
  325. indices.resize(offset_indices + 6);
  326. indices[offset_indices + 0] = num_vertices - 2;
  327. indices[offset_indices + 1] = index_next_corner;
  328. indices[offset_indices + 2] = num_vertices - 1;
  329. indices[offset_indices + 3] = num_vertices - 1;
  330. indices[offset_indices + 4] = index_next_corner;
  331. indices[offset_indices + 5] = index_next_corner + 1;
  332. }
  333. int GeometryBackgroundBorder::GetNumPoints(float R) const
  334. {
  335. return Math::Clamp(3 + Math::RoundToInteger(R / 6.f), 2, 100);
  336. }
  337. } // namespace Rml