BsGUILayout.cpp 4.0 KB

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