FlowLayout.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. unsigned int controlsCount = controls.size();
  45. for (unsigned int i = 0; i < controlsCount; i++)
  46. {
  47. Control* control = controls.at(i);
  48. GP_ASSERT(control);
  49. //align(control, container);
  50. const Rectangle& bounds = control->getBounds();
  51. const Theme::Margin& margin = control->getMargin();
  52. xPosition += margin.left;
  53. // Wrap to next row if we've gone past the edge of the container.
  54. if (xPosition + bounds.width >= clipWidth)
  55. {
  56. xPosition = margin.left;
  57. rowY += tallestHeight;
  58. }
  59. yPosition = rowY + margin.top;
  60. control->setPosition(xPosition, yPosition);
  61. control->update(container, offset);
  62. xPosition += bounds.width + margin.right;
  63. float height = bounds.height + margin.top + margin.bottom;
  64. if (height > tallestHeight)
  65. {
  66. tallestHeight = height;
  67. }
  68. }
  69. }
  70. }