GeometryBackgroundBorder.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #ifndef RMLUI_CORE_GEOMETRYBACKGROUNDBORDER_H
  29. #define RMLUI_CORE_GEOMETRYBACKGROUNDBORDER_H
  30. #include "../../Include/RmlUi/Core/Types.h"
  31. #include "../../Include/RmlUi/Core/Vertex.h"
  32. namespace Rml {
  33. class Box;
  34. // Ordered by top, right, bottom, left.
  35. using EdgeSizes = Array<float, 4>;
  36. // Ordered by top-left, top-right, bottom-right, bottom-left.
  37. using CornerSizes = Array<float, 4>;
  38. using CornerSizes2 = Array<Vector2f, 4>;
  39. using CornerPositions = Array<Vector2f, 4>;
  40. // The background-border metrics specify an inner and an outer rectangular area, whose corners can be rounded.
  41. struct BorderMetrics {
  42. // Outer corner positions (e.g. at border edge)
  43. CornerPositions positions_outer;
  44. // Inner corner positions (e.g. at padding edge)
  45. CornerPositions positions_inner;
  46. // Curved borders are drawn as circles (outer border) and ellipses (inner border) around the centers.
  47. CornerPositions positions_circle_center;
  48. // Radii of the outer edges, always circles.
  49. CornerSizes outer_radii;
  50. // Radii of the inner edges, 2-dimensional as these can be elliptic.
  51. // The inner radii is effectively the (signed) distance from the circle center to the inner edge.
  52. // They can also be zero or negative, in which case a sharp corner should be drawn instead of an arc.
  53. CornerSizes2 inner_radii;
  54. };
  55. class GeometryBackgroundBorder {
  56. public:
  57. // Construct the background-border geometry drawer, passing in the target vertex and index lists to be filled by later draw operations.
  58. GeometryBackgroundBorder(Vector<Vertex>& vertices, Vector<int>& indices);
  59. /// Compute background-border metrics used by later draw operations.
  60. /// @param outer_position Top-left position of the outer edge.
  61. /// @param edge_sizes Widths of the border.
  62. /// @param inner_size Size of the inner area.
  63. /// @param outer_radii The radius of the outer edge at each corner.
  64. /// @return The computed metrics.
  65. static BorderMetrics ComputeBorderMetrics(Vector2f outer_position, EdgeSizes edge_sizes, Vector2f inner_size, CornerSizes outer_radii);
  66. // Generate geometry for the background, defined by the inner area of the border metrics.
  67. void DrawBackground(const BorderMetrics& metrics, ColourbPremultiplied color);
  68. /// Generate geometry for the border, defined by the intersection of the outer and inner areas of the border metrics.
  69. void DrawBorder(const BorderMetrics& metrics, EdgeSizes edge_sizes, const ColourbPremultiplied border_colors[4]);
  70. private:
  71. enum Edge { TOP, RIGHT, BOTTOM, LEFT };
  72. enum Corner { TOP_LEFT, TOP_RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT };
  73. // -- Background --
  74. // All draw operations place vertices in clockwise order.
  75. // Draw the corner, delegate to the specific corner shape drawing function.
  76. void DrawBackgroundCorner(Corner corner, Vector2f pos_inner, Vector2f pos_circle_center, float R, Vector2f r, ColourbPremultiplied color);
  77. // Add a single point.
  78. void DrawPoint(Vector2f pos, ColourbPremultiplied color);
  79. // Draw an arc by placing vertices along the ellipse formed by the two-axis radius r, spaced evenly between angles a0,a1 (inclusive). Colors are
  80. // interpolated.
  81. void DrawArc(Vector2f pos_center, Vector2f r, float a0, float a1, ColourbPremultiplied color0, ColourbPremultiplied color1, int num_points);
  82. // Generates triangles by connecting the added vertices.
  83. void FillBackground(int index_start);
  84. // -- Border --
  85. // All draw operations place the first and last vertices in the following manner.
  86. // Let N be the number of vertices placed, and the numbers be indices into the newly placed vertices:
  87. // 0: Inner edge, aligned with the previous corner
  88. // 1: Outer edge, aligned with the previous corner
  89. // N-2: Inner edge, aligned with the next corner
  90. // N-1: Outer edge, aligned with the next corner
  91. // Where 'next' corner means along the clockwise direction. This way we can easily fill the triangles of the edges in FillEdge().
  92. // Draw the corner, delegate to the specific corner shape drawing function.
  93. void DrawBorderCorner(Corner corner, Vector2f pos_outer, Vector2f pos_inner, Vector2f pos_circle_center, float R, Vector2f r,
  94. ColourbPremultiplied color0, ColourbPremultiplied color1);
  95. // Draw a sharp border corner, ie. no border-radius. Does not produce any triangles.
  96. void DrawPointPoint(Vector2f pos_outer, Vector2f pos_inner, ColourbPremultiplied color0, ColourbPremultiplied color1);
  97. // Draw an arc along the outer edge (radius R), and an arc along the inner edge (two-axis radius r),
  98. // spaced evenly between angles a0,a1 (inclusive). Connect them by triangles. Colors are interpolated.
  99. void DrawArcArc(Vector2f pos_center, float R, Vector2f r, float a0, float a1, ColourbPremultiplied color0, ColourbPremultiplied color1,
  100. int num_points);
  101. // Draw an arc along the outer edge, and connect them by triangles to a point on the inner edge.
  102. void DrawArcPoint(Vector2f pos_center, Vector2f pos_inner, float R, float a0, float a1, ColourbPremultiplied color0, ColourbPremultiplied color1,
  103. int num_points);
  104. // Add triangles between the previous corner to another one specified by the index (possibly yet-to-be-drawn).
  105. void FillEdge(int index_next_corner);
  106. // -- Tools --
  107. int GetNumPoints(float R) const;
  108. Vector<Vertex>& vertices;
  109. Vector<int>& indices;
  110. };
  111. } // namespace Rml
  112. #endif