FloatedBoxSpace.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "FloatedBoxSpace.h"
  2. #include "../../../Include/RmlUi/Core/ComputedValues.h"
  3. #include "../../../Include/RmlUi/Core/Element.h"
  4. #include "../../../Include/RmlUi/Core/ElementScroll.h"
  5. #include "BlockContainer.h"
  6. #include "LayoutPools.h"
  7. #include <float.h>
  8. namespace Rml {
  9. FloatedBoxSpace::FloatedBoxSpace() {}
  10. FloatedBoxSpace::~FloatedBoxSpace() {}
  11. Vector2f FloatedBoxSpace::NextBoxPosition(const BlockContainer* parent, float& box_width, float cursor, const Vector2f dimensions, bool nowrap) const
  12. {
  13. return NextBoxPosition(parent, box_width, cursor, dimensions, nowrap, Style::Float::None);
  14. }
  15. Vector2f FloatedBoxSpace::NextFloatPosition(const BlockContainer* parent, float& out_box_width, float cursor, Vector2f dimensions,
  16. Style::Float float_property, Style::Clear clear_property) const
  17. {
  18. // Shift the cursor down (if necessary) so it isn't placed any higher than a previously-floated box.
  19. for (int i = 0; i < NUM_ANCHOR_EDGES; ++i)
  20. {
  21. if (!boxes[i].empty())
  22. cursor = Math::Max(cursor, boxes[i].back().offset.y);
  23. }
  24. // Shift the cursor down past to clear boxes, if necessary.
  25. cursor = DetermineClearPosition(cursor, clear_property);
  26. // Find a place to put this box.
  27. const bool nowrap = false;
  28. const Vector2f margin_offset = NextBoxPosition(parent, out_box_width, cursor, dimensions, nowrap, float_property);
  29. return margin_offset;
  30. }
  31. void FloatedBoxSpace::PlaceFloat(Style::Float float_property, Vector2f margin_position, Vector2f margin_size, Vector2f overflow_position,
  32. Vector2f overflow_size)
  33. {
  34. boxes[float_property == Style::Float::Left ? LEFT : RIGHT].push_back(FloatedBox{margin_position, margin_size});
  35. // Set our extents so they enclose the new box.
  36. extent_top_left_overflow = Math::Min(extent_top_left_overflow, overflow_position);
  37. extent_bottom_right_overflow = Math::Max(extent_bottom_right_overflow, overflow_position + overflow_size);
  38. extent_bottom_right_margin = Math::Max(extent_bottom_right_margin, margin_position + margin_size);
  39. }
  40. float FloatedBoxSpace::DetermineClearPosition(float cursor, Style::Clear clear_property) const
  41. {
  42. using namespace Style;
  43. // Clear left boxes.
  44. if (clear_property == Clear::Left || clear_property == Clear::Both)
  45. {
  46. for (size_t i = 0; i < boxes[LEFT].size(); ++i)
  47. cursor = Math::Max(cursor, boxes[LEFT][i].offset.y + boxes[LEFT][i].dimensions.y);
  48. }
  49. // Clear right boxes.
  50. if (clear_property == Clear::Right || clear_property == Clear::Both)
  51. {
  52. for (size_t i = 0; i < boxes[RIGHT].size(); ++i)
  53. cursor = Math::Max(cursor, boxes[RIGHT][i].offset.y + boxes[RIGHT][i].dimensions.y);
  54. }
  55. return cursor;
  56. }
  57. Vector2f FloatedBoxSpace::NextBoxPosition(const BlockContainer* parent, float& maximum_box_width, const float cursor, const Vector2f dimensions,
  58. const bool nowrap, const Style::Float float_property) const
  59. {
  60. const float parent_scrollbar_width = parent->GetElement()->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL);
  61. const float parent_edge_left = parent->GetPosition().x + parent->GetBox().GetPosition().x;
  62. const float parent_edge_right = parent_edge_left + parent->GetBox().GetSize().x - parent_scrollbar_width;
  63. const AnchorEdge box_edge = (float_property == Style::Float::Right ? RIGHT : LEFT);
  64. Vector2f box_position = {parent_edge_left, cursor};
  65. if (box_edge == RIGHT)
  66. box_position.x = parent_edge_right - dimensions.x;
  67. float next_cursor = FLT_MAX;
  68. // First up; we iterate through all boxes that share our edge, pushing ourself to the side of them if we intersect
  69. // them. We record the height of the lowest box that gets in our way; in the event we can't be positioned at this
  70. // height, we'll reposition ourselves at that height for the next iteration.
  71. for (const FloatedBox& fixed_box : boxes[box_edge])
  72. {
  73. // If the fixed box's bottom edge is above our top edge, then we can safely skip it.
  74. if (fixed_box.offset.y + fixed_box.dimensions.y <= box_position.y)
  75. continue;
  76. // If the fixed box's top edge is below our bottom edge, then we can safely skip it.
  77. if (fixed_box.offset.y >= box_position.y + dimensions.y)
  78. continue;
  79. // We're intersecting this box vertically, so the box is pushed to the side if necessary.
  80. bool collision = false;
  81. if (box_edge == LEFT)
  82. {
  83. float right_edge = fixed_box.offset.x + fixed_box.dimensions.x;
  84. collision = box_position.x < right_edge;
  85. if (collision)
  86. box_position.x = right_edge;
  87. }
  88. else
  89. {
  90. collision = box_position.x + dimensions.x > fixed_box.offset.x;
  91. if (collision)
  92. box_position.x = fixed_box.offset.x - dimensions.x;
  93. }
  94. // If there was a collision, then we *might* want to remember the height of this box if it is the earliest-
  95. // terminating box we've collided with so far.
  96. if (collision && !nowrap)
  97. {
  98. next_cursor = Math::Min(next_cursor, fixed_box.offset.y + fixed_box.dimensions.y);
  99. // Were we pushed out of our containing box? If so, try again at the next cursor position.
  100. if (box_position.x < parent_edge_left || box_position.x + dimensions.x > parent_edge_right)
  101. return NextBoxPosition(parent, maximum_box_width, next_cursor, dimensions, nowrap, float_property);
  102. }
  103. }
  104. // Second; we go through all of the boxes on the other edge, checking for horizontal collisions and determining the
  105. // maximum width the box can stretch to, if it is placed at this location.
  106. maximum_box_width = (box_edge == LEFT ? parent_edge_right - box_position.x : box_position.x + dimensions.x);
  107. for (const FloatedBox& fixed_box : boxes[1 - box_edge])
  108. {
  109. // If the fixed box's bottom edge is above our top edge, then we can safely skip it.
  110. if (fixed_box.offset.y + fixed_box.dimensions.y <= box_position.y)
  111. continue;
  112. // If the fixed box's top edge is below our bottom edge, then we can safely skip it.
  113. if (fixed_box.offset.y >= box_position.y + dimensions.y)
  114. continue;
  115. // We intersect this box vertically, so check if it intersects horizontally.
  116. bool collision = false;
  117. if (box_edge == LEFT)
  118. {
  119. maximum_box_width = Math::Min(maximum_box_width, fixed_box.offset.x - box_position.x);
  120. collision = box_position.x + dimensions.x > fixed_box.offset.x;
  121. }
  122. else
  123. {
  124. maximum_box_width = Math::Min(maximum_box_width, (box_position.x + dimensions.x) - (fixed_box.offset.x + fixed_box.dimensions.x));
  125. collision = box_position.x < fixed_box.offset.x + fixed_box.dimensions.x;
  126. }
  127. // If we collided with this box ... d'oh! We'll try again lower down the page, at the highest bottom-edge of
  128. // any of the boxes we've been pushed around by so far.
  129. if (collision && !nowrap)
  130. {
  131. next_cursor = Math::Min(next_cursor, fixed_box.offset.y + fixed_box.dimensions.y);
  132. return NextBoxPosition(parent, maximum_box_width, next_cursor, dimensions, nowrap, float_property);
  133. }
  134. }
  135. // If we are restricted from wrapping the position down, then we are already done now that we've shifted horizontally.
  136. if (nowrap)
  137. return box_position;
  138. // Third; we go through all of the boxes (on both sides), checking for vertical collisions.
  139. for (int i = 0; i < 2; ++i)
  140. {
  141. for (const FloatedBox& fixed_box : boxes[i])
  142. {
  143. // If the fixed box's bottom edge is above our top edge, then we can safely skip it.
  144. if (fixed_box.offset.y + fixed_box.dimensions.y <= box_position.y)
  145. continue;
  146. // If the fixed box's top edge is below our bottom edge, then we can safely skip it.
  147. if (fixed_box.offset.y >= box_position.y + dimensions.y)
  148. continue;
  149. // We collide vertically; if we also collide horizontally, then we have to try again further down the
  150. // layout. If the fixed box's left edge is to right of our right edge, then we can safely skip it.
  151. if (fixed_box.offset.x >= box_position.x + dimensions.x)
  152. continue;
  153. // If the fixed box's right edge is to the left of our left edge, then we can safely skip it.
  154. if (fixed_box.offset.x + fixed_box.dimensions.x <= box_position.x)
  155. continue;
  156. // D'oh! We hit this box. Ah well; we'll try again lower down the page, at the highest bottom-edge of any
  157. // of the boxes we've been pushed around by so far.
  158. next_cursor = Math::Min(next_cursor, fixed_box.offset.y + fixed_box.dimensions.y);
  159. return NextBoxPosition(parent, maximum_box_width, next_cursor, dimensions, nowrap, float_property);
  160. }
  161. }
  162. // Looks like we've found a winner!
  163. return box_position;
  164. }
  165. Vector2f FloatedBoxSpace::GetDimensions(FloatedBoxEdge edge) const
  166. {
  167. // For now, we don't really use the top-left extent, because it is not allowed in CSS to scroll to content located
  168. // to the top or left, and thus we have no use for it currently. We could use it later to help detect overflow on
  169. // the top-left sides. For example so we can hide parts of floats pushing outside the top-left sides of its parent
  170. // which is set to 'overflow: auto'.
  171. return edge == FloatedBoxEdge::Margin ? extent_bottom_right_margin : extent_bottom_right_overflow;
  172. }
  173. float FloatedBoxSpace::GetShrinkToFitWidth(float edge_left, float edge_right) const
  174. {
  175. // For the left-anchored boxes: Find the right-most edge of the boxes, relative to our parent's left edge.
  176. float left_shrink_width = 0.f;
  177. for (const FloatedBox& box : boxes[LEFT])
  178. left_shrink_width = Math::Max(left_shrink_width, box.offset.x - edge_left + box.dimensions.x);
  179. // Conversely, for the right-anchored boxes: Find the left-most edge, relative to our parent's right edge.
  180. float right_shrink_width = 0.f;
  181. for (const FloatedBox& box : boxes[RIGHT])
  182. right_shrink_width = Math::Max(right_shrink_width, edge_right - box.offset.x);
  183. return left_shrink_width + right_shrink_width;
  184. }
  185. void* FloatedBoxSpace::operator new(size_t size)
  186. {
  187. return LayoutPools::AllocateLayoutChunk(size);
  188. }
  189. void FloatedBoxSpace::operator delete(void* chunk, size_t size)
  190. {
  191. LayoutPools::DeallocateLayoutChunk(chunk, size);
  192. }
  193. } // namespace Rml