VerticalLayout.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "Base.h"
  2. #include "VerticalLayout.h"
  3. namespace gameplay
  4. {
  5. static VerticalLayout* __instance;
  6. VerticalLayout::VerticalLayout() : _bottomToTop(false)
  7. {
  8. }
  9. VerticalLayout::VerticalLayout(const VerticalLayout& copy)
  10. {
  11. }
  12. VerticalLayout::~VerticalLayout()
  13. {
  14. __instance = NULL;
  15. }
  16. VerticalLayout* VerticalLayout::create()
  17. {
  18. if (!__instance)
  19. {
  20. __instance = new VerticalLayout();
  21. }
  22. else
  23. {
  24. __instance->addRef();
  25. }
  26. return __instance;
  27. }
  28. void VerticalLayout::setBottomToTop(bool bottomToTop)
  29. {
  30. _bottomToTop = bottomToTop;
  31. }
  32. Layout::Type VerticalLayout::getType()
  33. {
  34. return Layout::LAYOUT_VERTICAL;
  35. }
  36. void VerticalLayout::update(const Container* container, const Vector2& offset)
  37. {
  38. GP_ASSERT(container);
  39. // Need border, padding.
  40. Theme::Border border = container->getBorder(container->getState());
  41. Theme::Padding padding = container->getPadding();
  42. float yPosition = 0;
  43. std::vector<Control*> controls = container->getControls();
  44. unsigned int i, end, iter;
  45. if (_bottomToTop)
  46. {
  47. i = controls.size() - 1;
  48. end = -1;
  49. iter = -1;
  50. }
  51. else
  52. {
  53. i = 0;
  54. end = controls.size();
  55. iter = 1;
  56. }
  57. while (i != end)
  58. {
  59. Control* control = controls.at(i);
  60. GP_ASSERT(control);
  61. align(control, container);
  62. const Rectangle& bounds = control->getBounds();
  63. const Theme::Margin& margin = control->getMargin();
  64. yPosition += margin.top;
  65. control->setPosition(margin.left, yPosition);
  66. control->update(container, offset);
  67. yPosition += bounds.height + margin.bottom;
  68. i += iter;
  69. }
  70. }
  71. }