ElementBorder.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "ElementBorder.h"
  29. #include "../../Include/Rocket/Core/Element.h"
  30. #include "../../Include/Rocket/Core/Property.h"
  31. namespace Rocket {
  32. namespace Core {
  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. if (border_dirty)
  45. {
  46. border_dirty = false;
  47. GenerateBorder();
  48. }
  49. geometry.Render(element->GetAbsoluteOffset(Box::BORDER));
  50. }
  51. // Marks the border geometry as dirty.
  52. void ElementBorder::DirtyBorder()
  53. {
  54. border_dirty = true;
  55. }
  56. // Generates the border geometry for the element.
  57. void ElementBorder::GenerateBorder()
  58. {
  59. int num_edges = 0;
  60. for (int i = 0; i < element->GetNumBoxes(); ++i)
  61. {
  62. const Box& box = element->GetBox(i);
  63. for (int j = 0; j < 4; j++)
  64. {
  65. if (box.GetEdge(Box::BORDER, (Box::Edge) j) > 0)
  66. num_edges++;
  67. }
  68. }
  69. std::vector< Vertex >& vertices = geometry.GetVertices();
  70. std::vector< int >& indices = geometry.GetIndices();
  71. int index_offset = 0;
  72. vertices.resize(4 * num_edges);
  73. indices.resize(6 * num_edges);
  74. if (num_edges > 0)
  75. {
  76. Vertex* raw_vertices = &vertices[0];
  77. int* raw_indices = &indices[0];
  78. Colourb border_colours[4];
  79. border_colours[0] = element->GetProperty(PropertyId::BorderTopColor)->value.Get< Colourb >();
  80. border_colours[1] = element->GetProperty(PropertyId::BorderRightColor)->value.Get< Colourb >();
  81. border_colours[2] = element->GetProperty(PropertyId::BorderBottomColor)->value.Get< Colourb >();
  82. border_colours[3] = element->GetProperty(PropertyId::BorderLeftColor)->value.Get< Colourb >();
  83. // Apply opacity to the border
  84. float opacity = element->GetProperty<float>(PropertyId::Opacity);
  85. for(int i = 0; i < 4; ++i) {
  86. border_colours[i].alpha = (byte)(opacity * (float)border_colours[i].alpha);
  87. }
  88. for (int i = 0; i < element->GetNumBoxes(); ++i)
  89. GenerateBorder(raw_vertices, raw_indices, index_offset, element->GetBox(i), border_colours);
  90. }
  91. geometry.Release();
  92. }
  93. // Generates the border geometry for a single box.
  94. void ElementBorder::GenerateBorder(Vertex*& vertices, int*& indices, int& index_offset, const Box& box, const Colourb* colours)
  95. {
  96. // The axis of extrusion for each of the edges.
  97. Vector2f box_extrusions[4] =
  98. {
  99. Vector2f(0, -1 * box.GetEdge(Box::BORDER, Box::TOP)),
  100. Vector2f(box.GetEdge(Box::BORDER, Box::RIGHT), 0),
  101. Vector2f(0, box.GetEdge(Box::BORDER, Box::BOTTOM)),
  102. Vector2f(-1 * box.GetEdge(Box::BORDER, Box::LEFT), 0)
  103. };
  104. // The position of each of the corners of the inner border.
  105. Vector2f box_corners[4];
  106. box_corners[0] = box.GetPosition(Box::PADDING);
  107. box_corners[2] = box_corners[0] + box.GetSize(Box::PADDING);
  108. box_corners[1] = Vector2f(box_corners[2].x, box_corners[0].y);
  109. box_corners[3] = Vector2f(box_corners[0].x, box_corners[2].y);
  110. for (int i = 0; i < 4; i++)
  111. {
  112. float border_width = box.GetEdge(Box::BORDER, (Box::Edge) i);
  113. if (border_width <= 0)
  114. continue;
  115. vertices[0].position = box_corners[i];
  116. vertices[1].position = box_corners[i] + box_extrusions[i] + box_extrusions[i == 0 ? 3 : i - 1];
  117. vertices[2].position = box_corners[i == 3 ? 0 : i + 1];
  118. vertices[3].position = vertices[2].position + box_extrusions[i] + box_extrusions[i == 3 ? 0 : i + 1];
  119. vertices[0].colour = colours[i];
  120. vertices[1].colour = colours[i];
  121. vertices[2].colour = colours[i];
  122. vertices[3].colour = colours[i];
  123. indices[0] = index_offset;
  124. indices[1] = index_offset + 3;
  125. indices[2] = index_offset + 1;
  126. indices[3] = index_offset;
  127. indices[4] = index_offset + 2;
  128. indices[5] = index_offset + 3;
  129. vertices += 4;
  130. indices += 6;
  131. index_offset += 4;
  132. }
  133. }
  134. }
  135. }