BsDockManager.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "BsDockManager.h"
  2. #include "BsEditorWidgetContainer.h"
  3. #include "CmMath.h"
  4. #include "CmException.h"
  5. using namespace CamelotFramework;
  6. using namespace BansheeEngine;
  7. namespace BansheeEditor
  8. {
  9. const CM::UINT32 DockManager::DockContainer::SliderSize = 4;
  10. DockManager::DockContainer::DockContainer()
  11. :mIsLeaf(false), mWidgets(nullptr), mX(0), mY(0), mWidth(0), mHeight(0), mSplitPosition(0.5f),
  12. mIsHorizontal(false)
  13. {
  14. mChildren[0] = nullptr;
  15. mChildren[1] = nullptr;
  16. }
  17. DockManager::DockContainer::~DockContainer()
  18. {
  19. if(mIsLeaf && mWidgets != nullptr)
  20. cm_delete(mWidgets);
  21. if(!mIsLeaf)
  22. {
  23. if(mChildren[0] != nullptr)
  24. cm_delete(mChildren[0]);
  25. if(mChildren[1] != nullptr)
  26. cm_delete(mChildren[1]);
  27. }
  28. // TODO - Clean up slider
  29. }
  30. void DockManager::DockContainer::setArea(CM::INT32 x, CM::INT32 y, UINT32 width, UINT32 height)
  31. {
  32. mX = x;
  33. mY = y;
  34. mWidth = width;
  35. mHeight = height;
  36. if(mIsLeaf)
  37. {
  38. if(mWidgets != nullptr)
  39. {
  40. mWidgets->setPosition(x, y);
  41. mWidgets->setSize(width, height);
  42. }
  43. }
  44. else if(mChildren[0] != nullptr && mChildren[1] != nullptr)
  45. {
  46. if(mIsHorizontal)
  47. {
  48. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mHeight - (INT32)SliderSize);
  49. UINT32 sizeTop = Math::FloorToInt(remainingSize * mSplitPosition);
  50. UINT32 sizeBottom = remainingSize - sizeTop;
  51. mChildren[0]->setArea(mX, mY, mWidth, sizeTop);
  52. mChildren[1]->setArea(mX, mY + sizeTop + SliderSize, mWidth, sizeBottom);
  53. // TODO - Set slider position
  54. }
  55. else
  56. {
  57. UINT32 remainingSize = (UINT32)std::max(0, (INT32)mWidth - (INT32)SliderSize);
  58. UINT32 sizeLeft = Math::FloorToInt(remainingSize * mSplitPosition);
  59. UINT32 sizeRight = remainingSize - sizeLeft;
  60. mChildren[0]->setArea(mX, mY, sizeLeft, mHeight);
  61. mChildren[1]->setArea(mX + sizeLeft + SliderSize, mY, sizeRight, mHeight);
  62. // TODO - Set slider position
  63. }
  64. }
  65. }
  66. void DockManager::DockContainer::makeLeaf(GUIWidget* widgetParent, EditorWidget* widget)
  67. {
  68. mIsLeaf = true;
  69. mWidgets = cm_new<EditorWidgetContainer>(widgetParent);
  70. mWidgets->add(*widget);
  71. mWidgets->setPosition(mX, mY);
  72. mWidgets->setSize(mWidth, mHeight);
  73. }
  74. void DockManager::DockContainer::addLeft(BS::GUIWidget* widgetParent, EditorWidget* widget)
  75. {
  76. splitContainer(widgetParent, widget, false, true);
  77. }
  78. void DockManager::DockContainer::addRight(BS::GUIWidget* widgetParent, EditorWidget* widget)
  79. {
  80. splitContainer(widgetParent, widget, false, false);
  81. }
  82. void DockManager::DockContainer::addTop(BS::GUIWidget* widgetParent, EditorWidget* widget)
  83. {
  84. splitContainer(widgetParent, widget, true, true);
  85. }
  86. void DockManager::DockContainer::addBottom(BS::GUIWidget* widgetParent, EditorWidget* widget)
  87. {
  88. splitContainer(widgetParent, widget, true, false);
  89. }
  90. void DockManager::DockContainer::splitContainer(BS::GUIWidget* widgetParent, EditorWidget* widget, bool horizontal, bool newChildIsFirst)
  91. {
  92. UINT32 idxA = newChildIsFirst ? 0 : 1;
  93. UINT32 idxB = (idxA + 1) % 2;
  94. mChildren[idxA] = cm_new<DockContainer>();
  95. mChildren[idxB] = cm_new<DockContainer>(*this);
  96. mChildren[idxA]->makeLeaf(widgetParent, widget);
  97. mIsLeaf = false;
  98. mIsHorizontal = horizontal;
  99. mWidgets = nullptr;
  100. mSplitPosition = 0.5f;
  101. // TODO - Add slider
  102. setArea(mX, mY, mWidth, mHeight);
  103. }
  104. DockManager::DockContainer* DockManager::DockContainer::find(EditorWidgetContainer* widgetContainer)
  105. {
  106. if(mIsLeaf)
  107. {
  108. if(mWidgets == widgetContainer)
  109. return this;
  110. else
  111. return nullptr;
  112. }
  113. else
  114. {
  115. if(mChildren[0] != nullptr && mChildren[0]->find(widgetContainer) != nullptr)
  116. return mChildren[0];
  117. if(mChildren[1] != nullptr && mChildren[1]->find(widgetContainer) != nullptr)
  118. return mChildren[1];
  119. }
  120. return nullptr;
  121. }
  122. DockManager::DockManager(BS::GUIWidget* parent)
  123. :mParent(parent)
  124. {
  125. }
  126. DockManager::~DockManager()
  127. {
  128. }
  129. void DockManager::insert(EditorWidgetContainer* relativeTo, EditorWidget* widgetToInsert, DockLocation location)
  130. {
  131. if(relativeTo != nullptr)
  132. {
  133. DockContainer* container = mRootContainer.find(relativeTo);
  134. if(container == nullptr)
  135. CM_EXCEPT(InternalErrorException, "Cannot find the wanted widget container relative to which the widget should be inserted.");
  136. switch(location)
  137. {
  138. case DockLocation::Left:
  139. container->addLeft(mParent, widgetToInsert);
  140. break;
  141. case DockLocation::Right:
  142. container->addRight(mParent, widgetToInsert);
  143. break;
  144. case DockLocation::Top:
  145. container->addTop(mParent, widgetToInsert);
  146. break;
  147. case DockLocation::Bottom:
  148. container->addBottom(mParent, widgetToInsert);
  149. break;
  150. }
  151. }
  152. else
  153. {
  154. if(mRootContainer.mWidgets != nullptr)
  155. CM_EXCEPT(InternalErrorException, "Trying to insert a widget into dock manager root container but one already exists.");
  156. mRootContainer.makeLeaf(mParent, widgetToInsert);
  157. }
  158. }
  159. void DockManager::setSize(CM::UINT32 width, CM::UINT32 height)
  160. {
  161. mRootContainer.setArea(0, 0, width, height);
  162. }
  163. }