GeometryBackgroundBorder.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 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. class GeometryBackgroundBorder {
  41. public:
  42. /// Generate geometry for background and borders.
  43. /// @param[out] vertices Destination vector for generated vertices.
  44. /// @param[out] indices Destination vector for generated indices.
  45. /// @param[in] radii The radius of each corner.
  46. /// @param[in] box The box used for positioning and sizing of the background and borders.
  47. /// @param[in] offset Offset the position of the generated vertices.
  48. /// @param[in] background_color Color of the background, set alpha to zero to not generate a background.
  49. /// @param[in] border_colors Pointer to a four-element array of border colors in top-right-bottom-left order, or nullptr to not generate borders.
  50. static void Draw(Vector<Vertex>& vertices, Vector<int>& indices, CornerSizes radii, const Box& box, Vector2f offset, Colourb background_color, const Colourb* border_colors);
  51. private:
  52. enum Corner { TOP_LEFT, TOP_RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT };
  53. GeometryBackgroundBorder(Vector<Vertex>& vertices, Vector<int>& indices);
  54. // -- Background --
  55. // All draw operations place vertices in clockwise order.
  56. // Draw the corner, delegate to the specific corner shape drawing function.
  57. void DrawBackgroundCorner(Corner corner, Vector2f pos_inner, Vector2f pos_circle_center, float R, Vector2f r, Colourb color);
  58. // Add a single point.
  59. void DrawPoint(Vector2f pos, Colourb color);
  60. // 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 interpolated.
  61. void DrawArc(Vector2f pos_center, Vector2f r, float a0, float a1, Colourb color0, Colourb color1, int num_points);
  62. // Generates triangles by connecting the added vertices.
  63. void FillBackground(int index_start);
  64. // -- Border --
  65. // All draw operations place the first and last vertices in the following manner.
  66. // Let N be the number of vertices placed, and the numbers be indices into the newly placed vertices:
  67. // 0: Inner edge, aligned with the previous corner
  68. // 1: Outer edge, aligned with the previous corner
  69. // N-2: Inner edge, aligned with the next corner
  70. // N-1: Outer edge, aligned with the next corner
  71. // Where 'next' corner means along the clockwise direction. This way we can easily fill the triangles of the edges in FillEdge().
  72. // Draw the corner, delegate to the specific corner shape drawing function.
  73. void DrawBorderCorner(Corner corner, Vector2f pos_outer, Vector2f pos_inner, Vector2f pos_circle_center, float R, Vector2f r, Colourb color0, Colourb color1);
  74. // Draw a sharp border corner, ie. no border-radius. Does not produce any triangles.
  75. void DrawPointPoint(Vector2f pos_outer, Vector2f pos_inner, Colourb color0, Colourb color1);
  76. // Draw an arc along the outer edge (radius R), and an arc along the inner edge (two-axis radius r),
  77. // spaced evenly between angles a0,a1 (inclusive). Connect them by triangles. Colors are interpolated.
  78. void DrawArcArc(Vector2f pos_center, float R, Vector2f r, float a0, float a1, Colourb color0, Colourb color1, int num_points);
  79. // Draw an arc along the outer edge, and connect them by triangles to a point on the inner edge.
  80. void DrawArcPoint(Vector2f pos_center, Vector2f pos_inner, float R, float a0, float a1, Colourb color0, Colourb color1, int num_points);
  81. // Add triangles between the previous corner to another one specified by the index (possibly yet-to-be-drawn).
  82. void FillEdge(int index_next_corner);
  83. // -- Tools --
  84. int GetNumPoints(float R) const;
  85. Vector<Vertex>& vertices;
  86. Vector<int>& indices;
  87. };
  88. } // namespace Rml
  89. #endif