BsGUILayout.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. :mIsDirty(false)
  12. {
  13. }
  14. GUILayout::~GUILayout()
  15. {
  16. for(auto& child : mChildren)
  17. {
  18. if(child.isLayout())
  19. {
  20. // Child layouts are directly owned by us
  21. CM_DELETE(child.layout, GUILayout, PoolAlloc);
  22. }
  23. else if(child.isFixedSpace())
  24. {
  25. CM_DELETE(child.space, GUIFixedSpace, PoolAlloc);
  26. }
  27. else if(child.isFlexibleSpace())
  28. {
  29. CM_DELETE(child.flexibleSpace, GUIFlexibleSpace, PoolAlloc);
  30. }
  31. else
  32. {
  33. child.element->_setParentLayout(nullptr);
  34. }
  35. }
  36. }
  37. void GUILayout::addElement(GUIElement* element)
  38. {
  39. if(element->_getParentLayout() != nullptr)
  40. element->_getParentLayout()->removeElement(element);
  41. GUILayoutEntry entry;
  42. entry.setElement(element);
  43. element->_setParentLayout(this);
  44. mChildren.push_back(entry);
  45. mIsDirty = true;
  46. }
  47. void GUILayout::removeElement(GUIElement* element)
  48. {
  49. bool foundElem = false;
  50. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  51. {
  52. GUILayoutEntry& child = *iter;
  53. if(child.isElement() && child.element == element)
  54. {
  55. mChildren.erase(iter);
  56. foundElem = true;
  57. mIsDirty = true;
  58. break;
  59. }
  60. }
  61. if(!foundElem)
  62. CM_EXCEPT(InvalidParametersException, "Provided element is not a part of this layout.");
  63. }
  64. void GUILayout::insertElement(UINT32 idx, GUIElement* element)
  65. {
  66. if(idx < 0 || idx >= (UINT32)mChildren.size())
  67. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((UINT32)mChildren.size()));
  68. if(element->_getParentLayout() != nullptr)
  69. element->_getParentLayout()->removeElement(element);
  70. GUILayoutEntry entry;
  71. entry.setElement(element);
  72. element->_setParentLayout(this);
  73. mChildren.insert(mChildren.begin() + idx, entry);
  74. mIsDirty = true;
  75. }
  76. GUILayout& GUILayout::addLayoutX()
  77. {
  78. GUILayoutEntry entry;
  79. entry.setLayout(CM_NEW(GUILayoutX, PoolAlloc) GUILayoutX());
  80. mChildren.push_back(entry);
  81. mIsDirty = true;
  82. return *entry.layout;
  83. }
  84. GUILayout& GUILayout::addLayoutY()
  85. {
  86. GUILayoutEntry entry;
  87. entry.setLayout(CM_NEW(GUILayoutY, PoolAlloc) GUILayoutY());
  88. mChildren.push_back(entry);
  89. mIsDirty = true;
  90. return *entry.layout;
  91. }
  92. void GUILayout::removeLayout(GUILayout& layout)
  93. {
  94. bool foundElem = false;
  95. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  96. {
  97. GUILayoutEntry& child = *iter;
  98. if(child.isLayout() && child.layout == &layout)
  99. {
  100. CM_DELETE(child.layout, GUILayout, PoolAlloc);
  101. mChildren.erase(iter);
  102. foundElem = true;
  103. mIsDirty = true;
  104. break;
  105. }
  106. }
  107. if(!foundElem)
  108. CM_EXCEPT(InvalidParametersException, "Provided element is not a part of this layout.");
  109. }
  110. GUILayout& GUILayout::insertLayoutX(UINT32 idx)
  111. {
  112. if(idx < 0 || idx >= (UINT32)mChildren.size())
  113. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((UINT32)mChildren.size()));
  114. GUILayoutEntry entry;
  115. entry.setLayout(CM_NEW(GUILayoutX, PoolAlloc) GUILayoutX());
  116. mChildren.insert(mChildren.begin() + idx, entry);
  117. mIsDirty = true;
  118. return *entry.layout;
  119. }
  120. GUILayout& GUILayout::insertLayoutY(UINT32 idx)
  121. {
  122. if(idx < 0 || idx >= (UINT32)mChildren.size())
  123. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((UINT32)mChildren.size()));
  124. GUILayoutEntry entry;
  125. entry.setLayout(CM_NEW(GUILayoutY, PoolAlloc) GUILayoutY());;
  126. mChildren.insert(mChildren.begin() + idx, entry);
  127. mIsDirty = true;
  128. return *entry.layout;
  129. }
  130. GUIFixedSpace& GUILayout::addSpace(UINT32 size)
  131. {
  132. GUILayoutEntry entry;
  133. entry.setSpace(CM_NEW(GUIFixedSpace, PoolAlloc) GUIFixedSpace(size));
  134. mChildren.push_back(entry);
  135. mIsDirty = true;
  136. return *entry.space;
  137. }
  138. void GUILayout::removeSpace(GUIFixedSpace& space)
  139. {
  140. bool foundElem = false;
  141. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  142. {
  143. GUILayoutEntry& child = *iter;
  144. if(child.isFixedSpace() && child.space == &space)
  145. {
  146. CM_DELETE(child.space, GUIFixedSpace, PoolAlloc);
  147. mChildren.erase(iter);
  148. foundElem = true;
  149. mIsDirty = true;
  150. break;
  151. }
  152. }
  153. if(!foundElem)
  154. CM_EXCEPT(InvalidParametersException, "Provided element is not a part of this layout.");
  155. }
  156. GUIFixedSpace& GUILayout::insertSpace(UINT32 idx, UINT32 size)
  157. {
  158. if(idx < 0 || idx >= (UINT32)mChildren.size())
  159. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((UINT32)mChildren.size()));
  160. GUILayoutEntry entry;
  161. entry.setSpace(CM_NEW(GUIFixedSpace, PoolAlloc) GUIFixedSpace(size));
  162. mChildren.insert(mChildren.begin() + idx, entry);
  163. mIsDirty = true;
  164. return *entry.space;
  165. }
  166. GUIFlexibleSpace& GUILayout::addFlexibleSpace()
  167. {
  168. GUILayoutEntry entry;
  169. entry.setFlexibleSpace(CM_NEW(GUIFlexibleSpace, PoolAlloc) GUIFlexibleSpace());
  170. mChildren.push_back(entry);
  171. mIsDirty = true;
  172. return *entry.flexibleSpace;
  173. }
  174. void GUILayout::removeFlexibleSpace(GUIFlexibleSpace& space)
  175. {
  176. bool foundElem = false;
  177. for(auto iter = mChildren.begin(); iter != mChildren.end(); ++iter)
  178. {
  179. GUILayoutEntry& child = *iter;
  180. if(child.isFlexibleSpace() && child.flexibleSpace == &space)
  181. {
  182. CM_DELETE(child.flexibleSpace, GUIFlexibleSpace, PoolAlloc);
  183. mChildren.erase(iter);
  184. foundElem = true;
  185. mIsDirty = true;
  186. break;
  187. }
  188. }
  189. if(!foundElem)
  190. CM_EXCEPT(InvalidParametersException, "Provided element is not a part of this layout.");
  191. }
  192. GUIFlexibleSpace& GUILayout::insertFlexibleSpace(UINT32 idx)
  193. {
  194. if(idx < 0 || idx >= (UINT32)mChildren.size())
  195. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx) + ". Valid range: 0 .. " + toString((UINT32)mChildren.size()));
  196. GUILayoutEntry entry;
  197. entry.setFlexibleSpace(CM_NEW(GUIFlexibleSpace, PoolAlloc) GUIFlexibleSpace());
  198. mChildren.insert(mChildren.begin() + idx, entry);
  199. mIsDirty = true;
  200. return *entry.flexibleSpace;
  201. }
  202. UINT32 GUILayout::getNumChildren() const
  203. {
  204. return (UINT32)mChildren.size();
  205. }
  206. void GUILayout::_update(UINT32 x, UINT32 y, UINT32 width, UINT32 height, UINT32 depth)
  207. {
  208. updateInternal(x, y, width, height, depth);
  209. mIsDirty = false;
  210. }
  211. bool GUILayout::_isDirty()
  212. {
  213. if(mIsDirty)
  214. return true;
  215. for(auto& child : mChildren)
  216. {
  217. if(child.isLayout())
  218. {
  219. if(child.layout->_isDirty())
  220. return true;
  221. }
  222. }
  223. return false;
  224. }
  225. }