FlowLayout.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "Base.h"
  2. #include "Control.h"
  3. #include "FlowLayout.h"
  4. #include "Container.h"
  5. namespace gameplay
  6. {
  7. static FlowLayout* __instance;
  8. FlowLayout::FlowLayout()
  9. {
  10. }
  11. FlowLayout::~FlowLayout()
  12. {
  13. __instance = NULL;
  14. }
  15. FlowLayout* FlowLayout::create()
  16. {
  17. if (!__instance)
  18. {
  19. __instance = new FlowLayout();
  20. }
  21. else
  22. {
  23. __instance->addRef();
  24. }
  25. return __instance;
  26. }
  27. Layout::Type FlowLayout::getType()
  28. {
  29. return Layout::LAYOUT_FLOW;
  30. }
  31. void FlowLayout::update(const Container* container, const Vector2& offset)
  32. {
  33. GP_ASSERT(container);
  34. const Rectangle& containerBounds = container->getBounds();
  35. const Theme::Border& containerBorder = container->getBorder(container->getState());
  36. const Theme::Padding& containerPadding = container->getPadding();
  37. float clipWidth = containerBounds.width - containerBorder.left - containerBorder.right - containerPadding.left - containerPadding.right;
  38. float clipHeight = containerBounds.height - containerBorder.top - containerBorder.bottom - containerPadding.top - containerPadding.bottom;
  39. float xPosition = 0;
  40. float yPosition = 0;
  41. float rowY = 0;
  42. float tallestHeight = 0;
  43. std::vector<Control*> controls = container->getControls();
  44. for (size_t i = 0, controlsCount = controls.size(); i < controlsCount; i++)
  45. {
  46. Control* control = controls.at(i);
  47. GP_ASSERT(control);
  48. //align(control, container);
  49. const Rectangle& bounds = control->getBounds();
  50. const Theme::Margin& margin = control->getMargin();
  51. xPosition += margin.left;
  52. // Wrap to next row if we've gone past the edge of the container.
  53. if (xPosition + bounds.width >= clipWidth)
  54. {
  55. xPosition = margin.left;
  56. rowY += tallestHeight;
  57. }
  58. yPosition = rowY + margin.top;
  59. control->setPosition(xPosition, yPosition);
  60. control->update(container, offset);
  61. xPosition += bounds.width + margin.right;
  62. float height = bounds.height + margin.top + margin.bottom;
  63. if (height > tallestHeight)
  64. {
  65. tallestHeight = height;
  66. }
  67. }
  68. }
  69. }