BsDockManagerLayout.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "EditorWindow/BsDockManagerLayout.h"
  4. #include "Private/RTTI/BsDockManagerLayoutRTTI.h"
  5. #include "EditorWindow/BsEditorWidgetManager.h"
  6. namespace bs
  7. {
  8. DockManagerLayout::Entry::Entry()
  9. :isLeaf(true), splitPosition(0), horizontalSplit(false),
  10. parent(nullptr)
  11. {
  12. children[0] = nullptr;
  13. children[1] = nullptr;
  14. }
  15. DockManagerLayout::Entry::~Entry()
  16. { }
  17. DockManagerLayout::Entry* DockManagerLayout::Entry::createLeaf(Entry* parent, UINT32 childIdx,
  18. const Vector<String>& widgetNames)
  19. {
  20. Entry* newEntry = bs_new<Entry>();
  21. newEntry->isLeaf = true;
  22. newEntry->parent = parent;
  23. if(parent != nullptr)
  24. parent->children[childIdx] = newEntry;
  25. newEntry->widgetNames = widgetNames;
  26. return newEntry;
  27. }
  28. DockManagerLayout::Entry* DockManagerLayout::Entry::createContainer(Entry* parent, UINT32 childIdx,
  29. float splitPosition, bool horizontalSplit)
  30. {
  31. Entry* newEntry = bs_new<Entry>();
  32. newEntry->isLeaf = false;
  33. newEntry->parent = parent;
  34. if(parent != nullptr)
  35. parent->children[childIdx] = newEntry;
  36. newEntry->horizontalSplit = horizontalSplit;
  37. newEntry->splitPosition = splitPosition;
  38. return newEntry;
  39. }
  40. DockManagerLayout::DockManagerLayout()
  41. :mIsMaximized(false)
  42. { }
  43. DockManagerLayout::~DockManagerLayout()
  44. {
  45. Stack<Entry*> todo;
  46. if(!mRootEntry.isLeaf)
  47. {
  48. todo.push(mRootEntry.children[0]);
  49. todo.push(mRootEntry.children[1]);
  50. }
  51. while(!todo.empty())
  52. {
  53. Entry* current = todo.top();
  54. todo.pop();
  55. if(!current->isLeaf)
  56. {
  57. todo.push(current->children[0]);
  58. todo.push(current->children[1]);
  59. }
  60. bs_delete(current);
  61. }
  62. }
  63. void DockManagerLayout::setIsMaximized(bool maximized, const Vector<String>& widgetNames)
  64. {
  65. mIsMaximized = maximized;
  66. mMaximizedWidgetNames = widgetNames;
  67. }
  68. void DockManagerLayout::pruneInvalidLeaves()
  69. {
  70. Stack<Entry*> layoutTodo;
  71. layoutTodo.push(&mRootEntry);
  72. while (!layoutTodo.empty())
  73. {
  74. Entry* curEntry = layoutTodo.top();
  75. layoutTodo.pop();
  76. if (!curEntry->isLeaf)
  77. {
  78. layoutTodo.push(curEntry->children[0]);
  79. layoutTodo.push(curEntry->children[1]);
  80. }
  81. else
  82. {
  83. for (INT32 i = 0; i < (INT32)curEntry->widgetNames.size(); i++)
  84. {
  85. if (!EditorWidgetManager::instance().isValidWidget(curEntry->widgetNames[i]))
  86. {
  87. curEntry->widgetNames.erase(curEntry->widgetNames.begin() + i);
  88. i--;
  89. }
  90. }
  91. if (curEntry->widgetNames.empty())
  92. {
  93. Entry* parent = curEntry->parent;
  94. if (parent != nullptr)
  95. {
  96. Entry* newParent = parent->parent;
  97. Entry* otherChild = parent->children[0] == curEntry ? parent->children[1] : parent->children[0];
  98. if (newParent != nullptr)
  99. {
  100. if (newParent->children[0] == parent)
  101. newParent->children[0] = otherChild;
  102. else
  103. newParent->children[1] = otherChild;
  104. bs_delete(parent);
  105. }
  106. else // Parent is root
  107. {
  108. if(otherChild)
  109. {
  110. parent->isLeaf = true;
  111. parent->widgetNames = otherChild->widgetNames;
  112. parent->children[0] = nullptr;
  113. parent->children[1] = nullptr;
  114. }
  115. else
  116. {
  117. parent->isLeaf = false;
  118. parent->widgetNames.clear();
  119. }
  120. }
  121. if(otherChild)
  122. otherChild->parent = newParent;
  123. bs_delete(curEntry);
  124. }
  125. }
  126. }
  127. }
  128. for (INT32 i = 0; i < (INT32)mMaximizedWidgetNames.size(); i++)
  129. {
  130. if (!EditorWidgetManager::instance().isValidWidget(mMaximizedWidgetNames[i]))
  131. {
  132. mMaximizedWidgetNames.erase(mMaximizedWidgetNames.begin() + i);
  133. i--;
  134. }
  135. }
  136. }
  137. SPtr<DockManagerLayout> DockManagerLayout::clone()
  138. {
  139. std::function<void(Entry*, Entry*, Entry*)> cloneEntry = [&](Entry* toClone, Entry* parent, Entry* clone)
  140. {
  141. *clone = *toClone;
  142. clone->parent = parent;
  143. if (!toClone->isLeaf)
  144. {
  145. clone->children[0] = bs_new<Entry>();
  146. clone->children[1] = bs_new<Entry>();
  147. cloneEntry(toClone->children[0], clone, clone->children[0]);
  148. cloneEntry(toClone->children[1], clone, clone->children[1]);
  149. }
  150. };
  151. SPtr<DockManagerLayout> copy = bs_shared_ptr_new<DockManagerLayout>();
  152. cloneEntry(&mRootEntry, nullptr, &copy->mRootEntry);
  153. return copy;
  154. }
  155. /************************************************************************/
  156. /* RTTI */
  157. /************************************************************************/
  158. RTTITypeBase* DockManagerLayout::getRTTIStatic()
  159. {
  160. return DockManagerLayoutRTTI::instance();
  161. }
  162. RTTITypeBase* DockManagerLayout::getRTTI() const
  163. {
  164. return DockManagerLayout::getRTTIStatic();
  165. }
  166. }