LayoutBlockBoxSpace.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 "LayoutBlockBoxSpace.h"
  29. #include "LayoutBlockBox.h"
  30. #include "LayoutEngine.h"
  31. #include "../../Include/RmlUi/Core/Element.h"
  32. #include "../../Include/RmlUi/Core/ElementScroll.h"
  33. #include <float.h>
  34. namespace Rml {
  35. LayoutBlockBoxSpace::LayoutBlockBoxSpace(LayoutBlockBox* _parent) : offset(0, 0), dimensions(0, 0)
  36. {
  37. parent = _parent;
  38. }
  39. LayoutBlockBoxSpace::~LayoutBlockBoxSpace()
  40. {
  41. }
  42. // Imports boxes from another block into this space.
  43. void LayoutBlockBoxSpace::ImportSpace(const LayoutBlockBoxSpace& space)
  44. {
  45. // Copy all the boxes from the parent into this space. Could do some optimisation here!
  46. for (int i = 0; i < NUM_ANCHOR_EDGES; ++i)
  47. {
  48. for (size_t j = 0; j < space.boxes[i].size(); ++j)
  49. boxes[i].push_back(space.boxes[i][j]);
  50. }
  51. }
  52. // Generates the position for a box of a given size within a containing block box.
  53. void LayoutBlockBoxSpace::PositionBox(Vector2f& box_position, float& box_width, float cursor, const Vector2f dimensions) const
  54. {
  55. box_width = PositionBox(box_position, cursor, dimensions);
  56. }
  57. // Generates and sets the position for a floating box of a given size within our block box.
  58. float LayoutBlockBoxSpace::PositionBox(float cursor, Element* element)
  59. {
  60. Vector2f element_size = element->GetBox().GetSize(Box::MARGIN);
  61. Style::Float float_property = element->GetComputedValues().float_;
  62. // Shift the cursor down (if necessary) so it isn't placed any higher than a previously-floated box.
  63. for (int i = 0; i < NUM_ANCHOR_EDGES; ++i)
  64. {
  65. if (!boxes[i].empty())
  66. cursor = Math::Max(cursor, boxes[i].back().offset.y);
  67. }
  68. // Shift the cursor down past to clear boxes, if necessary.
  69. cursor = ClearBoxes(cursor, element->GetComputedValues().clear);
  70. // Find a place to put this box.
  71. Vector2f element_offset;
  72. PositionBox(element_offset, cursor, element_size, float_property);
  73. // It's been placed, so we can now add it to our list of floating boxes.
  74. boxes[float_property == Style::Float::Left ? LEFT : RIGHT].push_back(SpaceBox(element_offset, element_size));
  75. // Set our offset and dimensions (if necessary) so they enclose the new box.
  76. Vector2f normalised_offset = element_offset - (parent->GetPosition() + parent->GetBox().GetPosition());
  77. offset.x = Math::Min(offset.x, normalised_offset.x);
  78. offset.y = Math::Min(offset.y, normalised_offset.y);
  79. dimensions.x = Math::Max(dimensions.x, normalised_offset.x + element_size.x);
  80. dimensions.y = Math::Max(dimensions.y, normalised_offset.y + element_size.y);
  81. // Shift the offset into the correct space relative to the element's offset parent.
  82. element_offset += Vector2f(element->GetBox().GetEdge(Box::MARGIN, Box::LEFT), element->GetBox().GetEdge(Box::MARGIN, Box::TOP));
  83. element->SetOffset(element_offset - parent->GetOffsetParent()->GetPosition(), parent->GetOffsetParent()->GetElement());
  84. return element_offset.y + element_size.y;
  85. }
  86. // Determines the appropriate vertical position for an object that is choosing to clear floating elements to the left
  87. // or right (or both).
  88. float LayoutBlockBoxSpace::ClearBoxes(float cursor, Style::Clear clear_property) const
  89. {
  90. using namespace Style;
  91. // Clear left boxes.
  92. if (clear_property == Clear::Left ||
  93. clear_property == Clear::Both)
  94. {
  95. for (size_t i = 0; i < boxes[LEFT].size(); ++i)
  96. cursor = Math::Max(cursor, boxes[LEFT][i].offset.y + boxes[LEFT][i].dimensions.y);
  97. }
  98. // Clear right boxes.
  99. if (clear_property == Clear::Right ||
  100. clear_property == Clear::Both)
  101. {
  102. for (size_t i = 0; i < boxes[RIGHT].size(); ++i)
  103. cursor = Math::Max(cursor, boxes[RIGHT][i].offset.y + boxes[RIGHT][i].dimensions.y);
  104. }
  105. return cursor;
  106. }
  107. // Generates the position for an arbitrary box within our space layout, floated against either the left or right edge.
  108. float LayoutBlockBoxSpace::PositionBox(Vector2f& box_position, float cursor, const Vector2f dimensions, Style::Float float_property) const
  109. {
  110. float parent_scrollbar_width = parent->GetElement()->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL);
  111. float parent_origin = parent->GetPosition().x + parent->GetBox().GetPosition(Box::CONTENT).x;
  112. float parent_edge = parent->GetBox().GetSize().x + parent_origin - parent_scrollbar_width;
  113. AnchorEdge box_edge = float_property == Style::Float::Right ? RIGHT : LEFT;
  114. box_position.y = cursor;
  115. box_position.x = parent_origin;
  116. if (box_edge == RIGHT)
  117. box_position.x += parent->GetBox().GetSize().x - dimensions.x - parent_scrollbar_width;
  118. float next_cursor = FLT_MAX;
  119. // First up; we iterate through all boxes that share our edge, pushing ourself to the side of them if we intersect
  120. // them. We record the height of the lowest box that gets in our way; in the event we can't be positioned at this
  121. // height, we'll reposition ourselves at that height for the next iteration.
  122. for (size_t i = 0; i < boxes[box_edge].size(); ++i)
  123. {
  124. const SpaceBox& fixed_box = boxes[box_edge][i];
  125. // If the fixed box's bottom edge is above our top edge, then we can safely skip it.
  126. if (fixed_box.offset.y + fixed_box.dimensions.y <= box_position.y)
  127. continue;
  128. // If the fixed box's top edge is below our bottom edge, then we can safely skip it.
  129. if (fixed_box.offset.y >= box_position.y + dimensions.y)
  130. continue;
  131. // We're intersecting this box vertically, so the box is pushed to the side if necessary.
  132. bool collision = false;
  133. if (box_edge == LEFT)
  134. {
  135. float right_edge = fixed_box.offset.x + fixed_box.dimensions.x;
  136. collision = box_position.x < right_edge;
  137. if (collision)
  138. box_position.x = right_edge;
  139. }
  140. else
  141. {
  142. collision = box_position.x + dimensions.x > fixed_box.offset.x;
  143. if (collision)
  144. box_position.x = fixed_box.offset.x - dimensions.x;
  145. }
  146. // If there was a collision, then we *might* want to remember the height of this box if it is the earliest-
  147. // terminating box we've collided with so far.
  148. if (collision)
  149. {
  150. next_cursor = Math::Min(next_cursor, fixed_box.offset.y + fixed_box.dimensions.y);
  151. // Were we pushed out of our containing box? If so, try again at the next cursor position.
  152. float normalised_position = box_position.x - parent_origin;
  153. if (normalised_position < 0 ||
  154. normalised_position + dimensions.x > parent->GetBox().GetSize().x)
  155. return PositionBox(box_position, next_cursor + 0.01f, dimensions, float_property);
  156. }
  157. }
  158. // Second; we go through all of the boxes on the other edge, checking for horizontal collisions and determining the
  159. // maximum width the box can stretch to, if it is placed at this location.
  160. float maximum_box_width = box_edge == LEFT ? parent_edge - box_position.x : box_position.x + dimensions.x;
  161. for (size_t i = 0; i < boxes[1 - box_edge].size(); ++i)
  162. {
  163. const SpaceBox& fixed_box = boxes[1 - box_edge][i];
  164. // If the fixed box's bottom edge is above our top edge, then we can safely skip it.
  165. if (fixed_box.offset.y + fixed_box.dimensions.y <= box_position.y)
  166. continue;
  167. // If the fixed box's top edge is below our bottom edge, then we can safely skip it.
  168. if (fixed_box.offset.y >= box_position.y + dimensions.y)
  169. continue;
  170. // We intersect this box vertically, so check if it intersects horizontally.
  171. bool collision = false;
  172. if (box_edge == LEFT)
  173. {
  174. maximum_box_width = Math::Min(maximum_box_width, fixed_box.offset.x - box_position.x);
  175. collision = box_position.x + dimensions.x > fixed_box.offset.x;
  176. }
  177. else
  178. {
  179. maximum_box_width = Math::Min(maximum_box_width, (box_position.x + dimensions.x) - (fixed_box.offset.x + fixed_box.dimensions.x));
  180. collision = box_position.x < fixed_box.offset.x + fixed_box.dimensions.x;
  181. }
  182. // If we collided with this box ... d'oh! We'll try again lower down the page, at the highest bottom-edge of
  183. // any of the boxes we've been pushed around by so far.
  184. if (collision)
  185. {
  186. next_cursor = Math::Min(next_cursor, fixed_box.offset.y + fixed_box.dimensions.y);
  187. return PositionBox(box_position, next_cursor + 0.01f, dimensions, float_property);
  188. }
  189. }
  190. // Third; we go through all of the boxes (on both sides), checking for vertical collisions.
  191. for (int i = 0; i < 2; ++i)
  192. {
  193. for (size_t j = 0; j < boxes[i].size(); ++j)
  194. {
  195. const SpaceBox& fixed_box = boxes[i][j];
  196. // If the fixed box's bottom edge is above our top edge, then we can safely skip it.
  197. if (fixed_box.offset.y + fixed_box.dimensions.y <= box_position.y)
  198. continue;
  199. // If the fixed box's top edge is below our bottom edge, then we can safely skip it.
  200. if (fixed_box.offset.y >= box_position.y + dimensions.y)
  201. continue;
  202. // We collide vertically; if we also collide horizontally, then we have to try again further down the
  203. // layout. If the fixed box's left edge is to right of our right edge, then we can safely skip it.
  204. if (fixed_box.offset.x >= box_position.x + dimensions.x)
  205. continue;
  206. // If the fixed box's right edge is to the left of our left edge, then we can safely skip it.
  207. if (fixed_box.offset.x + fixed_box.dimensions.x <= box_position.x)
  208. continue;
  209. // D'oh! We hit this box. Ah well; we'll try again lower down the page, at the highest bottom-edge of any
  210. // of the boxes we've been pushed around by so far.
  211. next_cursor = Math::Min(next_cursor, fixed_box.offset.y + fixed_box.dimensions.y);
  212. return PositionBox(box_position, next_cursor + 0.01f, dimensions, float_property);
  213. }
  214. }
  215. // Looks like we've found a winner!
  216. return maximum_box_width;
  217. }
  218. // Returns the top-left offset of the boxes within the space.
  219. Vector2f LayoutBlockBoxSpace::GetOffset() const
  220. {
  221. return offset;
  222. }
  223. // Returns the dimensions of the boxes within the space.
  224. Vector2f LayoutBlockBoxSpace::GetDimensions() const
  225. {
  226. return dimensions - offset;
  227. }
  228. void* LayoutBlockBoxSpace::operator new(size_t size)
  229. {
  230. return LayoutEngine::AllocateLayoutChunk(size);
  231. }
  232. void LayoutBlockBoxSpace::operator delete(void* chunk, size_t size)
  233. {
  234. LayoutEngine::DeallocateLayoutChunk(chunk, size);
  235. }
  236. LayoutBlockBoxSpace::SpaceBox::SpaceBox() : offset(0, 0), dimensions(0, 0)
  237. {
  238. }
  239. LayoutBlockBoxSpace::SpaceBox::SpaceBox(const Vector2f offset, const Vector2f dimensions) : offset(offset), dimensions(dimensions)
  240. {
  241. }
  242. } // namespace Rml