LayoutBlockBoxSpace.cpp 11 KB

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