FlowLayout.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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, const Vector2& offset)
  34. {
  35. GP_ASSERT(container);
  36. const Rectangle& containerBounds = container->getBounds();
  37. const Theme::Border& containerBorder = container->getBorder(container->getState());
  38. const Theme::Padding& containerPadding = container->getPadding();
  39. float clipWidth = containerBounds.width - containerBorder.left - containerBorder.right - containerPadding.left - containerPadding.right;
  40. float clipHeight = containerBounds.height - containerBorder.top - containerBorder.bottom - containerPadding.top - containerPadding.bottom;
  41. float xPosition = 0;
  42. float yPosition = 0;
  43. float rowY = 0;
  44. float tallestHeight = 0;
  45. std::vector<Control*> controls = container->getControls();
  46. unsigned int controlsCount = controls.size();
  47. for (unsigned int i = 0; i < controlsCount; i++)
  48. {
  49. Control* control = controls.at(i);
  50. GP_ASSERT(control);
  51. //align(control, container);
  52. const Rectangle& bounds = control->getBounds();
  53. const Theme::Margin& margin = control->getMargin();
  54. xPosition += margin.left;
  55. // Wrap to next row if we've gone past the edge of the container.
  56. if (xPosition + bounds.width >= clipWidth)
  57. {
  58. xPosition = margin.left;
  59. rowY += tallestHeight;
  60. }
  61. yPosition = rowY + margin.top;
  62. control->setPosition(xPosition, yPosition);
  63. control->update(container, offset);
  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. }