BsDockManagerLayout.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.size() == 0)
  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. }
  105. otherChild->parent = newParent;
  106. bs_delete(parent);
  107. bs_delete(curEntry);
  108. }
  109. }
  110. }
  111. }
  112. for (INT32 i = 0; i < (INT32)mMaximizedWidgetNames.size(); i++)
  113. {
  114. if (!EditorWidgetManager::instance().isValidWidget(mMaximizedWidgetNames[i]))
  115. {
  116. mMaximizedWidgetNames.erase(mMaximizedWidgetNames.begin() + i);
  117. i--;
  118. }
  119. }
  120. }
  121. SPtr<DockManagerLayout> DockManagerLayout::clone()
  122. {
  123. std::function<void(Entry*, Entry*, Entry*)> cloneEntry = [&](Entry* toClone, Entry* parent, Entry* clone)
  124. {
  125. *clone = *toClone;
  126. clone->parent = parent;
  127. if (!toClone->isLeaf)
  128. {
  129. clone->children[0] = bs_new<Entry>();
  130. clone->children[1] = bs_new<Entry>();
  131. cloneEntry(toClone->children[0], clone, clone->children[0]);
  132. cloneEntry(toClone->children[1], clone, clone->children[1]);
  133. }
  134. };
  135. SPtr<DockManagerLayout> copy = bs_shared_ptr_new<DockManagerLayout>();
  136. cloneEntry(&mRootEntry, nullptr, &copy->mRootEntry);
  137. return copy;
  138. }
  139. /************************************************************************/
  140. /* RTTI */
  141. /************************************************************************/
  142. RTTITypeBase* DockManagerLayout::getRTTIStatic()
  143. {
  144. return DockManagerLayoutRTTI::instance();
  145. }
  146. RTTITypeBase* DockManagerLayout::getRTTI() const
  147. {
  148. return DockManagerLayout::getRTTIStatic();
  149. }
  150. }