BsGUILayout.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "BsGUILayout.h"
  2. #include "BsGUIElement.h"
  3. #include "BsGUILayoutX.h"
  4. #include "BsGUILayoutY.h"
  5. #include "BsGUISpace.h"
  6. #include "BsException.h"
  7. namespace BansheeEngine
  8. {
  9. GUILayout::GUILayout(GUIArea* parentArea)
  10. :mOptimalWidth(0), mOptimalHeight(0), mActualWidth(0),
  11. mActualHeight(0), mParentGUIArea(parentArea)
  12. {
  13. }
  14. GUILayout::GUILayout()
  15. :mOptimalWidth(0), mOptimalHeight(0), mActualWidth(0),
  16. mActualHeight(0), mParentGUIArea(nullptr)
  17. {
  18. }
  19. GUILayout::~GUILayout()
  20. {
  21. }
  22. void GUILayout::addElement(GUIElement* element)
  23. {
  24. _registerChildElement(element);
  25. }
  26. void GUILayout::removeElement(GUIElement* element)
  27. {
  28. _unregisterChildElement(element);
  29. }
  30. void GUILayout::insertElement(UINT32 idx, GUIElement* element)
  31. {
  32. if(idx < 0 || idx > (UINT32)mChildren.size())
  33. BS_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((UINT32)mChildren.size()));
  34. GUIElementBase* parentElement = element->_getParent();
  35. if(parentElement != nullptr)
  36. {
  37. parentElement->_unregisterChildElement(element);
  38. }
  39. element->_setParent(this);
  40. mChildren.insert(mChildren.begin() + idx, element);
  41. if (mIsDisabled)
  42. element->disableRecursively();
  43. markContentAsDirty();
  44. }
  45. void GUILayout::removeChildAt(UINT32 idx)
  46. {
  47. if(idx < 0 || idx >= (UINT32)mChildren.size())
  48. BS_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((UINT32)mChildren.size()));
  49. GUIElementBase* child = mChildren[idx];
  50. mChildren.erase(mChildren.begin() + idx);
  51. if(child->_getType() == GUIElementBase::Type::Element)
  52. child->_setParent(nullptr);
  53. else
  54. bs_delete<PoolAlloc>(child);
  55. markContentAsDirty();
  56. }
  57. GUIFixedSpace& GUILayout::addSpace(UINT32 size)
  58. {
  59. GUIFixedSpace* entry = bs_new<GUIFixedSpace, PoolAlloc>(size);
  60. mChildren.push_back(entry);
  61. markContentAsDirty();
  62. return *entry;
  63. }
  64. void GUILayout::removeSpace(GUIFixedSpace& space)
  65. {
  66. bool foundElem = false;
  67. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  68. {
  69. GUIElementBase* child = *iter;
  70. if(child->_getType() == GUIElementBase::Type::FixedSpace && child == &space)
  71. {
  72. bs_delete<PoolAlloc>(child);
  73. mChildren.erase(iter);
  74. foundElem = true;
  75. markContentAsDirty();
  76. break;
  77. }
  78. }
  79. if(!foundElem)
  80. BS_EXCEPT(InvalidParametersException, "Provided element is not a part of this layout.");
  81. }
  82. GUIFixedSpace& GUILayout::insertSpace(UINT32 idx, UINT32 size)
  83. {
  84. if(idx < 0 || idx > (UINT32)mChildren.size())
  85. BS_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((UINT32)mChildren.size()));
  86. GUIFixedSpace* entry = bs_new<GUIFixedSpace, PoolAlloc>(size);
  87. mChildren.insert(mChildren.begin() + idx, entry);
  88. markContentAsDirty();
  89. return *entry;
  90. }
  91. GUIFlexibleSpace& GUILayout::addFlexibleSpace()
  92. {
  93. GUIFlexibleSpace* entry = bs_new<GUIFlexibleSpace, PoolAlloc>();
  94. mChildren.push_back(entry);
  95. markContentAsDirty();
  96. return *entry;
  97. }
  98. void GUILayout::removeFlexibleSpace(GUIFlexibleSpace& space)
  99. {
  100. bool foundElem = false;
  101. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  102. {
  103. GUIElementBase* child = *iter;
  104. if(child->_getType() == GUIElementBase::Type::FlexibleSpace && child == &space)
  105. {
  106. bs_delete<PoolAlloc>(child);
  107. mChildren.erase(iter);
  108. foundElem = true;
  109. markContentAsDirty();
  110. break;
  111. }
  112. }
  113. if(!foundElem)
  114. BS_EXCEPT(InvalidParametersException, "Provided element is not a part of this layout.");
  115. }
  116. GUIFlexibleSpace& GUILayout::insertFlexibleSpace(UINT32 idx)
  117. {
  118. if(idx < 0 || idx > (UINT32)mChildren.size())
  119. BS_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((UINT32)mChildren.size()));
  120. GUIFlexibleSpace* entry = bs_new<GUIFlexibleSpace, PoolAlloc>();
  121. mChildren.insert(mChildren.begin() + idx, entry);
  122. markContentAsDirty();
  123. return *entry;
  124. }
  125. const RectOffset& GUILayout::_getPadding() const
  126. {
  127. static RectOffset padding;
  128. return padding;
  129. }
  130. }