Layout.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. GP_ASSERT(control);
  10. GP_ASSERT(container);
  11. if (control->_alignment != Control::ALIGN_TOP_LEFT ||
  12. control->_isAlignmentSet ||
  13. control->_autoWidth || control->_autoHeight)
  14. {
  15. Rectangle controlBounds = control->getBounds();
  16. const Theme::Margin& controlMargin = control->getMargin();
  17. const Rectangle& containerBounds = container->getBounds();
  18. const Theme::Border& containerBorder = container->getBorder(container->getState());
  19. const Theme::Padding& containerPadding = container->getPadding();
  20. float clipWidth;
  21. float clipHeight;
  22. if (container->getScroll() != Container::SCROLL_NONE)
  23. {
  24. const Rectangle& verticalScrollBarBounds = container->getImageRegion("verticalScrollBar", container->getState());
  25. const Rectangle& horizontalScrollBarBounds = container->getImageRegion("horizontalScrollBar", container->getState());
  26. clipWidth = containerBounds.width - containerBorder.left - containerBorder.right - containerPadding.left - containerPadding.right - verticalScrollBarBounds.width;
  27. clipHeight = containerBounds.height - containerBorder.top - containerBorder.bottom - containerPadding.top - containerPadding.bottom - horizontalScrollBarBounds.height;
  28. }
  29. else
  30. {
  31. clipWidth = containerBounds.width - containerBorder.left - containerBorder.right - containerPadding.left - containerPadding.right;
  32. clipHeight = containerBounds.height - containerBorder.top - containerBorder.bottom - containerPadding.top - containerPadding.bottom;
  33. }
  34. if (control->_autoWidth)
  35. {
  36. controlBounds.width = clipWidth - controlMargin.left - controlMargin.right;
  37. }
  38. if (control->_autoHeight)
  39. {
  40. controlBounds.height = clipHeight - controlMargin.top - controlMargin.bottom;
  41. }
  42. // Vertical alignment
  43. if ((control->_alignment & Control::ALIGN_BOTTOM) == Control::ALIGN_BOTTOM)
  44. {
  45. controlBounds.y = clipHeight - controlBounds.height - controlMargin.bottom;
  46. }
  47. else if ((control->_alignment & Control::ALIGN_VCENTER) == Control::ALIGN_VCENTER)
  48. {
  49. controlBounds.y = clipHeight * 0.5f - controlBounds.height * 0.5f;
  50. }
  51. else if ((control->_alignment & Control::ALIGN_TOP) == Control::ALIGN_TOP)
  52. {
  53. controlBounds.y = controlMargin.top;
  54. }
  55. // Horizontal alignment
  56. if ((control->_alignment & Control::ALIGN_RIGHT) == Control::ALIGN_RIGHT)
  57. {
  58. controlBounds.x = clipWidth - controlBounds.width - controlMargin.right;
  59. }
  60. else if ((control->_alignment & Control::ALIGN_HCENTER) == Control::ALIGN_HCENTER)
  61. {
  62. controlBounds.x = clipWidth * 0.5f - controlBounds.width * 0.5f;
  63. }
  64. else if ((control->_alignment & Control::ALIGN_LEFT) == Control::ALIGN_LEFT)
  65. {
  66. controlBounds.x = controlMargin.left;
  67. }
  68. control->setBounds(controlBounds);
  69. }
  70. }
  71. bool Layout::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  72. {
  73. return false;
  74. }
  75. }