BsGUILayout.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. GUIElementBase* parentElement = element->_getParent();
  20. if(parentElement != nullptr && parentElement->_getType() == GUIElementBase::Type::Layout)
  21. {
  22. GUILayout* layoutParent = static_cast<GUILayout*>(parentElement);
  23. layoutParent->removeElement(element);
  24. }
  25. element->_setParent(this);
  26. mChildren.push_back(element);
  27. markContentAsDirty();
  28. }
  29. void GUILayout::removeElement(GUIElement* element)
  30. {
  31. bool foundElem = false;
  32. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  33. {
  34. GUIElementBase* child = *iter;
  35. if(child->_getType() == GUIElementBase::Type::Element && child == element)
  36. {
  37. mChildren.erase(iter);
  38. element->_setParent(nullptr);
  39. foundElem = true;
  40. markContentAsDirty();
  41. break;
  42. }
  43. }
  44. if(!foundElem)
  45. CM_EXCEPT(InvalidParametersException, "Provided element is not a part of this layout.");
  46. }
  47. void GUILayout::insertElement(UINT32 idx, GUIElement* element)
  48. {
  49. if(idx < 0 || idx > (UINT32)mChildren.size())
  50. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((UINT32)mChildren.size()));
  51. GUIElementBase* parentElement = element->_getParent();
  52. if(parentElement != nullptr && parentElement->_getType() == GUIElementBase::Type::Layout)
  53. {
  54. GUILayout* layoutParent = static_cast<GUILayout*>(parentElement);
  55. layoutParent->removeElement(element);
  56. }
  57. element->_setParent(this);
  58. mChildren.insert(mChildren.begin() + idx, element);
  59. markContentAsDirty();
  60. }
  61. void GUILayout::removeChildAt(CM::UINT32 idx)
  62. {
  63. if(idx < 0 || idx >= (UINT32)mChildren.size())
  64. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((UINT32)mChildren.size()));
  65. GUIElementBase* child = mChildren[idx];
  66. mChildren.erase(mChildren.begin() + idx);
  67. if(child->_getType() == GUIElementBase::Type::Element)
  68. child->_setParent(nullptr);
  69. else
  70. cm_delete<PoolAlloc>(child);
  71. markContentAsDirty();
  72. }
  73. GUIFixedSpace& GUILayout::addSpace(UINT32 size)
  74. {
  75. GUIFixedSpace* entry = cm_new<GUIFixedSpace, PoolAlloc>(size);
  76. mChildren.push_back(entry);
  77. markContentAsDirty();
  78. return *entry;
  79. }
  80. void GUILayout::removeSpace(GUIFixedSpace& space)
  81. {
  82. bool foundElem = false;
  83. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  84. {
  85. GUIElementBase* child = *iter;
  86. if(child->_getType() == GUIElementBase::Type::FixedSpace && child == &space)
  87. {
  88. cm_delete<PoolAlloc>(child);
  89. mChildren.erase(iter);
  90. foundElem = true;
  91. markContentAsDirty();
  92. break;
  93. }
  94. }
  95. if(!foundElem)
  96. CM_EXCEPT(InvalidParametersException, "Provided element is not a part of this layout.");
  97. }
  98. GUIFixedSpace& GUILayout::insertSpace(UINT32 idx, UINT32 size)
  99. {
  100. if(idx < 0 || idx > (UINT32)mChildren.size())
  101. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((UINT32)mChildren.size()));
  102. GUIFixedSpace* entry = cm_new<GUIFixedSpace, PoolAlloc>(size);
  103. mChildren.insert(mChildren.begin() + idx, entry);
  104. markContentAsDirty();
  105. return *entry;
  106. }
  107. GUIFlexibleSpace& GUILayout::addFlexibleSpace()
  108. {
  109. GUIFlexibleSpace* entry = cm_new<GUIFlexibleSpace, PoolAlloc>();
  110. mChildren.push_back(entry);
  111. markContentAsDirty();
  112. return *entry;
  113. }
  114. void GUILayout::removeFlexibleSpace(GUIFlexibleSpace& space)
  115. {
  116. bool foundElem = false;
  117. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  118. {
  119. GUIElementBase* child = *iter;
  120. if(child->_getType() == GUIElementBase::Type::FlexibleSpace && child == &space)
  121. {
  122. cm_delete<PoolAlloc>(child);
  123. mChildren.erase(iter);
  124. foundElem = true;
  125. markContentAsDirty();
  126. break;
  127. }
  128. }
  129. if(!foundElem)
  130. CM_EXCEPT(InvalidParametersException, "Provided element is not a part of this layout.");
  131. }
  132. GUIFlexibleSpace& GUILayout::insertFlexibleSpace(UINT32 idx)
  133. {
  134. if(idx < 0 || idx > (UINT32)mChildren.size())
  135. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((UINT32)mChildren.size()));
  136. GUIFlexibleSpace* entry = cm_new<GUIFlexibleSpace, PoolAlloc>();
  137. mChildren.insert(mChildren.begin() + idx, entry);
  138. markContentAsDirty();
  139. return *entry;
  140. }
  141. }