ElementBorder.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #include "ElementBorder.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include "../../Include/RmlUi/Core/Property.h"
  31. #include "../../Include/RmlUi/Core/Profiling.h"
  32. namespace Rml {
  33. ElementBorder::ElementBorder(Element* _element) : geometry(_element)
  34. {
  35. element = _element;
  36. border_dirty = true;
  37. }
  38. ElementBorder::~ElementBorder()
  39. {
  40. }
  41. // Renders the element's border, if it has one.
  42. void ElementBorder::RenderBorder()
  43. {
  44. RMLUI_ZoneScoped;
  45. if (border_dirty)
  46. {
  47. border_dirty = false;
  48. GenerateBorder();
  49. }
  50. geometry.Render(element->GetAbsoluteOffset(Box::BORDER));
  51. }
  52. // Marks the border geometry as dirty.
  53. void ElementBorder::DirtyBorder()
  54. {
  55. border_dirty = true;
  56. }
  57. // Generates the border geometry for the element.
  58. void ElementBorder::GenerateBorder()
  59. {
  60. int num_edges = 0;
  61. for (int i = 0; i < element->GetNumBoxes(); ++i)
  62. {
  63. const Box& box = element->GetBox(i);
  64. for (int j = 0; j < 4; j++)
  65. {
  66. if (box.GetEdge(Box::BORDER, (Box::Edge) j) > 0)
  67. num_edges++;
  68. }
  69. }
  70. Vector< Vertex >& vertices = geometry.GetVertices();
  71. Vector< int >& indices = geometry.GetIndices();
  72. int index_offset = 0;
  73. vertices.resize(4 * num_edges);
  74. indices.resize(6 * num_edges);
  75. if (num_edges > 0)
  76. {
  77. Vertex* raw_vertices = &vertices[0];
  78. int* raw_indices = &indices[0];
  79. const ComputedValues& computed = element->GetComputedValues();
  80. Colourb border_colours[4];
  81. border_colours[0] = computed.border_top_color;
  82. border_colours[1] = computed.border_right_color;
  83. border_colours[2] = computed.border_bottom_color;
  84. border_colours[3] = computed.border_left_color;
  85. // Apply opacity to the border
  86. float opacity = computed.opacity;
  87. for(int i = 0; i < 4; ++i) {
  88. border_colours[i].alpha = (byte)(opacity * (float)border_colours[i].alpha);
  89. }
  90. for (int i = 0; i < element->GetNumBoxes(); ++i)
  91. GenerateBorder(raw_vertices, raw_indices, index_offset, element->GetBox(i), border_colours);
  92. }
  93. geometry.Release();
  94. }
  95. // Generates the border geometry for a single box.
  96. void ElementBorder::GenerateBorder(Vertex*& vertices, int*& indices, int& index_offset, const Box& box, const Colourb* colours)
  97. {
  98. // The axis of extrusion for each of the edges.
  99. Vector2f box_extrusions[4] =
  100. {
  101. Vector2f(0, -1 * box.GetEdge(Box::BORDER, Box::TOP)),
  102. Vector2f(box.GetEdge(Box::BORDER, Box::RIGHT), 0),
  103. Vector2f(0, box.GetEdge(Box::BORDER, Box::BOTTOM)),
  104. Vector2f(-1 * box.GetEdge(Box::BORDER, Box::LEFT), 0)
  105. };
  106. // The position of each of the corners of the inner border.
  107. Vector2f box_corners[4];
  108. box_corners[0] = box.GetPosition(Box::PADDING);
  109. box_corners[2] = box_corners[0] + box.GetSize(Box::PADDING);
  110. box_corners[1] = Vector2f(box_corners[2].x, box_corners[0].y);
  111. box_corners[3] = Vector2f(box_corners[0].x, box_corners[2].y);
  112. for (int i = 0; i < 4; i++)
  113. {
  114. float border_width = box.GetEdge(Box::BORDER, (Box::Edge) i);
  115. if (border_width <= 0)
  116. continue;
  117. vertices[0].position = box_corners[i];
  118. vertices[1].position = box_corners[i] + box_extrusions[i] + box_extrusions[i == 0 ? 3 : i - 1];
  119. vertices[2].position = box_corners[i == 3 ? 0 : i + 1];
  120. vertices[3].position = vertices[2].position + box_extrusions[i] + box_extrusions[i == 3 ? 0 : i + 1];
  121. vertices[0].colour = colours[i];
  122. vertices[1].colour = colours[i];
  123. vertices[2].colour = colours[i];
  124. vertices[3].colour = colours[i];
  125. indices[0] = index_offset;
  126. indices[1] = index_offset + 3;
  127. indices[2] = index_offset + 1;
  128. indices[3] = index_offset;
  129. indices[4] = index_offset + 2;
  130. indices[5] = index_offset + 3;
  131. vertices += 4;
  132. indices += 6;
  133. index_offset += 4;
  134. }
  135. }
  136. } // namespace Rml