ElementBorder.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // Modified by uniquejack
  28. // Implement opacity (Check https://github.com/libRocket/libRocket/pull/262)
  29. #include "precompiled.h"
  30. #include "ElementBorder.h"
  31. #include "../../Include/Rocket/Core/Element.h"
  32. #include "../../Include/Rocket/Core/Property.h"
  33. namespace Rocket {
  34. namespace Core {
  35. ElementBorder::ElementBorder(Element* _element) : geometry(_element)
  36. {
  37. element = _element;
  38. border_dirty = true;
  39. }
  40. ElementBorder::~ElementBorder()
  41. {
  42. }
  43. // Renders the element's border, if it has one.
  44. void ElementBorder::RenderBorder()
  45. {
  46. if (border_dirty)
  47. {
  48. border_dirty = false;
  49. GenerateBorder();
  50. }
  51. geometry.Render(element->GetAbsoluteOffset(Box::BORDER));
  52. }
  53. // Marks the border geometry as dirty.
  54. void ElementBorder::DirtyBorder()
  55. {
  56. border_dirty = true;
  57. }
  58. // Generates the border geometry for the element.
  59. void ElementBorder::GenerateBorder()
  60. {
  61. int num_edges = 0;
  62. for (int i = 0; i < element->GetNumBoxes(); ++i)
  63. {
  64. const Box& box = element->GetBox(i);
  65. for (int j = 0; j < 4; j++)
  66. {
  67. if (box.GetEdge(Box::BORDER, (Box::Edge) j) > 0)
  68. num_edges++;
  69. }
  70. }
  71. std::vector< Vertex >& vertices = geometry.GetVertices();
  72. std::vector< int >& indices = geometry.GetIndices();
  73. int index_offset = 0;
  74. vertices.resize(4 * num_edges);
  75. indices.resize(6 * num_edges);
  76. if (num_edges > 0)
  77. {
  78. Vertex* raw_vertices = &vertices[0];
  79. int* raw_indices = &indices[0];
  80. Colourb border_colours[4];
  81. border_colours[0] = element->GetProperty(BORDER_TOP_COLOR)->value.Get< Colourb >();
  82. border_colours[1] = element->GetProperty(BORDER_RIGHT_COLOR)->value.Get< Colourb >();
  83. border_colours[2] = element->GetProperty(BORDER_BOTTOM_COLOR)->value.Get< Colourb >();
  84. border_colours[3] = element->GetProperty(BORDER_LEFT_COLOR)->value.Get< Colourb >();
  85. // Apply opacity to the border
  86. float opacity = element->GetProperty<float>(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. }
  137. }