Layout.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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->_isAlignmentSet || control->_autoHeight)
  44. {
  45. if ((control->_alignment & Control::ALIGN_BOTTOM) == Control::ALIGN_BOTTOM)
  46. {
  47. controlBounds.y = clipHeight - controlBounds.height - controlMargin.bottom;
  48. }
  49. else if ((control->_alignment & Control::ALIGN_VCENTER) == Control::ALIGN_VCENTER)
  50. {
  51. controlBounds.y = clipHeight * 0.5f - controlBounds.height * 0.5f;
  52. }
  53. else if ((control->_alignment & Control::ALIGN_TOP) == Control::ALIGN_TOP)
  54. {
  55. controlBounds.y = controlMargin.top;
  56. }
  57. }
  58. // Horizontal alignment
  59. if(control->_isAlignmentSet || control->_autoWidth)
  60. {
  61. if ((control->_alignment & Control::ALIGN_RIGHT) == Control::ALIGN_RIGHT)
  62. {
  63. controlBounds.x = clipWidth - controlBounds.width - controlMargin.right;
  64. }
  65. else if ((control->_alignment & Control::ALIGN_HCENTER) == Control::ALIGN_HCENTER)
  66. {
  67. controlBounds.x = clipWidth * 0.5f - controlBounds.width * 0.5f;
  68. }
  69. else if ((control->_alignment & Control::ALIGN_LEFT) == Control::ALIGN_LEFT)
  70. {
  71. controlBounds.x = controlMargin.left;
  72. }
  73. }
  74. control->setBounds(controlBounds);
  75. }
  76. }
  77. bool Layout::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  78. {
  79. return false;
  80. }
  81. }