Layout.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "Base.h"
  2. #include "Layout.h"
  3. #include "Control.h"
  4. #include "Container.h"
  5. namespace gameplay
  6. {
  7. void Layout::align(Control* control, const Container* container)
  8. {
  9. if (control->_alignment != Control::ALIGN_TOP_LEFT ||
  10. control->_autoWidth || control->_autoHeight)
  11. {
  12. Rectangle controlBounds = control->getBounds();
  13. const Rectangle& containerBounds = container->getClipBounds();
  14. const Theme::Border& containerBorder = container->getBorder(container->getState());
  15. const Theme::Padding& containerPadding = container->getPadding();
  16. float clipWidth = containerBounds.width - containerBorder.left - containerBorder.right - containerPadding.left - containerPadding.right;
  17. float clipHeight = containerBounds.height - containerBorder.top - containerBorder.bottom - containerPadding.top - containerPadding.bottom;
  18. if (control->_autoWidth)
  19. {
  20. controlBounds.width = clipWidth;
  21. }
  22. if (control->_autoHeight)
  23. {
  24. controlBounds.height = clipHeight;
  25. }
  26. // Vertical alignment
  27. if ((control->_alignment & Control::ALIGN_BOTTOM) == Control::ALIGN_BOTTOM)
  28. {
  29. controlBounds.y = clipHeight - controlBounds.height;
  30. }
  31. else if ((control->_alignment & Control::ALIGN_VCENTER) == Control::ALIGN_VCENTER)
  32. {
  33. controlBounds.y = clipHeight * 0.5f - controlBounds.height * 0.5f;
  34. }
  35. // Horizontal alignment
  36. if ((control->_alignment & Control::ALIGN_RIGHT) == Control::ALIGN_RIGHT)
  37. {
  38. controlBounds.x = clipWidth - controlBounds.width;
  39. }
  40. else if ((control->_alignment & Control::ALIGN_HCENTER) == Control::ALIGN_HCENTER)
  41. {
  42. controlBounds.x = clipWidth * 0.5f - controlBounds.width * 0.5f;
  43. }
  44. control->setBounds(controlBounds);
  45. }
  46. }
  47. }