BsGUIArea.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "BsGUIArea.h"
  2. #include "BsGUIWidget.h"
  3. #include "BsGUILayoutX.h"
  4. #include "CmViewport.h"
  5. using namespace CamelotFramework;
  6. namespace BansheeEngine
  7. {
  8. GUIArea::GUIArea(GUIWidget& widget, UINT32 x, UINT32 y, UINT16 depth)
  9. :mWidget(widget), mLeft(x), mTop(y), mDepth(depth), mIsDirty(true), mIsDisabled(false),
  10. mResizeXWithWidget(false), mResizeYWithWidget(false), mWidth(0), mHeight(0), mRight(0), mBottom(0)
  11. {
  12. mLayout = cm_new<GUILayoutX, PoolAlloc>();
  13. mWidget.registerArea(this);
  14. }
  15. GUIArea::~GUIArea()
  16. {
  17. cm_delete<PoolAlloc>(mLayout);
  18. }
  19. GUIArea* GUIArea::create(GUIWidget& widget, UINT32 x, UINT32 y, UINT32 width, UINT32 height, UINT16 depth)
  20. {
  21. GUIArea* area = new (cm_alloc<GUIArea, PoolAlloc>()) GUIArea(widget, x, y, depth);
  22. area->mWidth = width;
  23. area->mHeight = height;
  24. return area;
  25. }
  26. GUIArea* GUIArea::createStretchedXY(GUIWidget& widget, UINT32 offsetLeft,
  27. UINT32 offsetRight, UINT32 offsetTop, UINT32 offsetBottom, UINT16 depth)
  28. {
  29. GUIArea* area = new (cm_alloc<GUIArea, PoolAlloc>()) GUIArea(widget, offsetLeft, offsetTop, depth);
  30. area->mWidth = std::max(0, (INT32)widget.getTarget()->getWidth() - (INT32)offsetLeft - (INT32)offsetRight);
  31. area->mHeight = std::max(0, (INT32)widget.getTarget()->getHeight() - (INT32)offsetTop - (INT32)offsetBottom);
  32. area->mRight = offsetRight;
  33. area->mBottom = offsetBottom;
  34. area->mResizeXWithWidget = true;
  35. area->mResizeYWithWidget = true;
  36. return area;
  37. }
  38. GUIArea* GUIArea::createStretchedX(GUIWidget& widget, CM::UINT32 offsetLeft,
  39. CM::UINT32 offsetRight, CM::UINT32 offsetTop, CM::UINT32 height, CM::UINT16 depth)
  40. {
  41. GUIArea* area = new (cm_alloc<GUIArea, PoolAlloc>()) GUIArea(widget, offsetLeft, offsetTop, depth);
  42. area->mWidth = std::max(0, (INT32)widget.getTarget()->getWidth() - (INT32)offsetLeft - (INT32)offsetRight);
  43. area->mHeight = height;
  44. area->mRight = offsetRight;
  45. area->mResizeXWithWidget = true;
  46. area->mResizeYWithWidget = false;
  47. return area;
  48. }
  49. GUIArea* GUIArea::createStretchedY(GUIWidget& widget, CM::UINT32 offsetTop,
  50. CM::UINT32 offsetBottom, CM::UINT32 offsetLeft, CM::UINT32 width, CM::UINT16 depth)
  51. {
  52. GUIArea* area = new (cm_alloc<GUIArea, PoolAlloc>()) GUIArea(widget, offsetLeft, offsetTop, depth);
  53. area->mWidth = width;
  54. area->mHeight = std::max(0, (INT32)widget.getTarget()->getHeight() - (INT32)offsetTop - (INT32)offsetBottom);
  55. area->mBottom = offsetBottom;
  56. area->mResizeXWithWidget = false;
  57. area->mResizeYWithWidget = true;
  58. return area;
  59. }
  60. void GUIArea::destroy(GUIArea* area)
  61. {
  62. area->mWidget.unregisterArea(area);
  63. cm_delete<PoolAlloc>(area);
  64. }
  65. void GUIArea::destroyInternal(GUIArea* area)
  66. {
  67. cm_delete<PoolAlloc>(area);
  68. }
  69. void GUIArea::disable()
  70. {
  71. mIsDisabled = true;
  72. mLayout->disableRecursively();
  73. }
  74. void GUIArea::enable()
  75. {
  76. mIsDisabled = false;
  77. mLayout->enableRecursively();
  78. }
  79. void GUIArea::changeParentWidget(GUIWidget& widget)
  80. {
  81. if(&mWidget == &widget)
  82. return;
  83. mWidget.unregisterArea(this);
  84. widget.registerArea(this);
  85. mWidget = widget;
  86. mLayout->_changeParentWidget(widget);
  87. }
  88. void GUIArea::_update()
  89. {
  90. if(!mIsDisabled && isDirty())
  91. {
  92. Rect clipRect(mLeft, mTop, mWidth, mHeight);
  93. mLayout->_updateLayout(mLeft, mTop, mWidth, mHeight, clipRect, mWidget.getDepth(), mDepth);
  94. mIsDirty = false;
  95. }
  96. }
  97. bool GUIArea::isDirty() const
  98. {
  99. if(mIsDirty)
  100. return true;
  101. return mLayout->_isContentDirty();
  102. }
  103. void GUIArea::setPosition(INT32 x, INT32 y)
  104. {
  105. mLeft = x;
  106. mTop = y;
  107. mIsDirty = true;
  108. }
  109. void GUIArea::setSize(UINT32 width, UINT32 height)
  110. {
  111. mWidth = width;
  112. mHeight = height;
  113. mIsDirty = true;
  114. }
  115. void GUIArea::notifyWindowResized(UINT32 newWidth, UINT32 newHeight)
  116. {
  117. if(mResizeXWithWidget)
  118. mWidth = (UINT32)std::max(0, (INT32)newWidth - (INT32)mLeft - (INT32)mRight);
  119. if(mResizeYWithWidget)
  120. mHeight = (UINT32)std::max(0, (INT32)newHeight - (INT32)mTop - (INT32)mBottom);
  121. if(mResizeXWithWidget || mResizeYWithWidget)
  122. mIsDirty = true;
  123. }
  124. }