FlowLayout.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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(const FlowLayout& copy)
  12. {
  13. }
  14. FlowLayout::~FlowLayout()
  15. {
  16. }
  17. FlowLayout* FlowLayout::create()
  18. {
  19. if (!__instance)
  20. {
  21. __instance = new FlowLayout();
  22. }
  23. else
  24. {
  25. __instance->addRef();
  26. }
  27. return __instance;
  28. }
  29. Layout::Type FlowLayout::getType()
  30. {
  31. return Layout::LAYOUT_FLOW;
  32. }
  33. void FlowLayout::update(const Container* container)
  34. {
  35. const Rectangle& containerBounds = container->getClipBounds();
  36. const Theme::Border& containerBorder = container->getBorder(container->getState());
  37. const Theme::Padding& containerPadding = container->getPadding();
  38. float clipWidth = containerBounds.width - containerBorder.left - containerBorder.right - containerPadding.left - containerPadding.right;
  39. float clipHeight = containerBounds.height - containerBorder.top - containerBorder.bottom - containerPadding.top - containerPadding.bottom;
  40. float xPosition = 0;
  41. float yPosition = 0;
  42. float rowY = 0;
  43. float tallestHeight = 0;
  44. std::vector<Control*> controls = container->getControls();
  45. unsigned int controlsCount = controls.size();
  46. for (unsigned int i = 0; i < controlsCount; i++)
  47. {
  48. Control* control = controls.at(i);
  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. if (control->isDirty() || control->isContainer())
  61. {
  62. control->update(container->getClip());
  63. }
  64. xPosition += bounds.width + margin.right;
  65. float height = bounds.height + margin.top + margin.bottom;
  66. if (height > tallestHeight)
  67. {
  68. tallestHeight = height;
  69. }
  70. }
  71. }
  72. }