BsEditorWidgetContainer.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "BsEditorWidgetContainer.h"
  2. #include "BsGUITabbedTitleBar.h"
  3. #include "BsEditorWidget.h"
  4. #include "BsDragAndDropManager.h"
  5. #include "BsEditorWindow.h"
  6. #include "CmMath.h"
  7. #include "CmInput.h"
  8. #include "BsGUIWidget.h"
  9. using namespace CamelotFramework;
  10. using namespace BansheeEngine;
  11. namespace BansheeEditor
  12. {
  13. const CM::UINT32 EditorWidgetContainer::TitleBarHeight = 13;
  14. EditorWidgetContainer::EditorWidgetContainer(BS::GUIWidget* parent, RenderWindow* renderWindow)
  15. :mParent(parent), mX(0), mY(0), mWidth(0), mHeight(0), mTitleBar(nullptr), mActiveWidget(-1),
  16. mIsHandlingWidgetDragAndDrop(false)
  17. {
  18. mTitleBar = GUITabbedTitleBar::create(*parent, renderWindow);
  19. mTitleBar->onTabActivated.connect(boost::bind(&EditorWidgetContainer::tabActivated, this, _1));
  20. mTitleBar->onTabClosed.connect(boost::bind(&EditorWidgetContainer::tabClosed, this, _1));
  21. mTitleBar->onTabDraggedOff.connect(boost::bind(&EditorWidgetContainer::tabDraggedOff, this, _1));
  22. mTitleBar->onTabDraggedOn.connect(boost::bind(&EditorWidgetContainer::tabDraggedOn, this, _1));
  23. }
  24. EditorWidgetContainer::~EditorWidgetContainer()
  25. {
  26. GUIElement::destroy(mTitleBar);
  27. for(auto& widget : mWidgets)
  28. {
  29. EditorWidget::destroy(widget);
  30. }
  31. }
  32. void EditorWidgetContainer::add(EditorWidget& widget)
  33. {
  34. insert((UINT32)mWidgets.size(), widget);
  35. }
  36. void EditorWidgetContainer::remove(EditorWidget& widget)
  37. {
  38. INT32 widgetIdx = -1;
  39. UINT32 curIdx = 0;
  40. for(auto& curWidget : mWidgets)
  41. {
  42. if(curWidget == &widget)
  43. {
  44. widgetIdx = curIdx;
  45. break;
  46. }
  47. curIdx++;
  48. }
  49. if(widgetIdx == -1)
  50. return;
  51. mWidgets.erase(mWidgets.begin() + widgetIdx);
  52. mTitleBar->removeTab((UINT32)widgetIdx);
  53. widget._changeParent(nullptr);
  54. if(widgetIdx == mActiveWidget)
  55. {
  56. if(mWidgets.size() > 0)
  57. {
  58. setActiveWidget(0);
  59. }
  60. }
  61. }
  62. void EditorWidgetContainer::insert(CM::UINT32 idx, EditorWidget& widget)
  63. {
  64. for(auto& curWidget : mWidgets)
  65. {
  66. if(curWidget == &widget)
  67. return;
  68. }
  69. idx = Math::clamp(idx, 0U, (UINT32)mWidgets.size());
  70. mTitleBar->insertTab(idx, widget.getName());
  71. mWidgets.insert(mWidgets.begin() + idx, &widget);
  72. widget._changeParent(this);
  73. if(mActiveWidget == -1)
  74. setActiveWidget((UINT32)mWidgets.size() - 1);
  75. else
  76. widget._disable();
  77. }
  78. void EditorWidgetContainer::setSize(UINT32 width, UINT32 height)
  79. {
  80. // TODO - Title bar is always TitleBarHeight size, so what happens when the container area is smaller than that?
  81. RectI clipRect(mX, mY, width, TitleBarHeight);
  82. // TODO - Consider making EditorWidgetContainer a GUIElement so this gets called automatically
  83. Vector2I offset(mX, mY);
  84. mTitleBar->_setOffset(offset);
  85. mTitleBar->_setWidth(width);
  86. mTitleBar->_setHeight(TitleBarHeight);
  87. mTitleBar->_setAreaDepth(9900);
  88. mTitleBar->_setWidgetDepth(mParent->getDepth());
  89. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  90. mTitleBar->_setClipRect(elemClipRect);
  91. mTitleBar->_updateLayoutInternal(mX, mY, width, TitleBarHeight, clipRect, mParent->getDepth(), 9900);
  92. if(mActiveWidget >= 0)
  93. {
  94. EditorWidget* activeWidgetPtr = mWidgets[mActiveWidget];
  95. UINT32 contentHeight = (UINT32)std::max(0, (INT32)height - (INT32)TitleBarHeight);
  96. activeWidgetPtr->_setSize(width, contentHeight);
  97. }
  98. mWidth = width;
  99. mHeight = height;
  100. }
  101. void EditorWidgetContainer::setPosition(INT32 x, INT32 y)
  102. {
  103. RectI clipRect(x, y, mWidth, mHeight);
  104. // TODO - Consider making EditorWidgetContainer a GUIElement so this gets called automatically
  105. Vector2I offset(x, y);
  106. mTitleBar->_setOffset(offset);
  107. mTitleBar->_setWidth(mWidth);
  108. mTitleBar->_setHeight(mHeight);
  109. mTitleBar->_setAreaDepth(9900);
  110. mTitleBar->_setWidgetDepth(mParent->getDepth());
  111. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  112. mTitleBar->_setClipRect(elemClipRect);
  113. mTitleBar->_updateLayoutInternal(x, y, mWidth, mHeight, clipRect, mParent->getDepth(), 9900);
  114. if(mActiveWidget >= 0)
  115. {
  116. EditorWidget* activeWidgetPtr = mWidgets[mActiveWidget];
  117. activeWidgetPtr->_setPosition(x, y + TitleBarHeight);
  118. }
  119. mX = x;
  120. mY = y;
  121. }
  122. void EditorWidgetContainer::setActiveWidget(UINT32 idx)
  123. {
  124. if(mActiveWidget == idx)
  125. return;
  126. mActiveWidget = idx;
  127. UINT32 curIdx = 0;
  128. for(auto& curWidget : mWidgets)
  129. {
  130. if(curIdx != (UINT32)mActiveWidget)
  131. curWidget->_disable();
  132. else
  133. curWidget->_enable();
  134. curIdx++;
  135. }
  136. setPosition(mX, mY);
  137. setSize(mWidth, mHeight);
  138. }
  139. void EditorWidgetContainer::tabActivated(UINT32 idx)
  140. {
  141. setActiveWidget(idx);
  142. }
  143. void EditorWidgetContainer::tabClosed(UINT32 idx)
  144. {
  145. EditorWidget* widget = mWidgets[idx];
  146. remove(*widget);
  147. EditorWidget::destroy(widget);
  148. if(!onWidgetClosed.empty())
  149. onWidgetClosed();
  150. }
  151. void EditorWidgetContainer::tabDraggedOff(CM::UINT32 idx)
  152. {
  153. EditorWidget* widget = mWidgets[idx];
  154. remove(*widget);
  155. mIsHandlingWidgetDragAndDrop = true;
  156. // TODO - Hook up drag and drop texture
  157. DragAndDropManager::instance().startDrag(HTexture(), (UINT32)DragAndDropType::EditorWidget, (void*)widget, boost::bind(&EditorWidgetContainer::tabDroppedCallback, this, _1));
  158. if(!onWidgetClosed.empty())
  159. onWidgetClosed();
  160. }
  161. void EditorWidgetContainer::tabDraggedOn(CM::UINT32 idx)
  162. {
  163. #if CM_DEBUG_MODE
  164. if(!DragAndDropManager::instance().isDragInProgress())
  165. CM_EXCEPT(InternalErrorException, "Tab drag and drop reported but no drag in progress.");
  166. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  167. CM_EXCEPT(InternalErrorException, "Tab drag and drop reported but drag type is invalid.");
  168. #endif
  169. EditorWidget* draggedWidget = static_cast<EditorWidget*>(DragAndDropManager::instance().getDragData());
  170. insert(idx, *draggedWidget);
  171. }
  172. void EditorWidgetContainer::tabDroppedCallback(bool wasDragProcessed)
  173. {
  174. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  175. return;
  176. if(!wasDragProcessed)
  177. {
  178. EditorWidget* draggedWidget = static_cast<EditorWidget*>(DragAndDropManager::instance().getDragData());
  179. EditorWindow* newWindow = EditorWindow::create();
  180. newWindow->widgets().add(*draggedWidget);
  181. Vector2I mousePos = Input::instance().getCursorPosition();
  182. newWindow->setPosition(mousePos.x, mousePos.y);
  183. }
  184. mIsHandlingWidgetDragAndDrop = false;
  185. if(mWidgetDroppedCallback != nullptr)
  186. {
  187. mWidgetDroppedCallback();
  188. mWidgetDroppedCallback = nullptr;
  189. }
  190. }
  191. void EditorWidgetContainer::_notifyWidgetDestroyed(EditorWidget* widget)
  192. {
  193. for(auto& curWidget : mWidgets)
  194. {
  195. if(curWidget == widget)
  196. {
  197. remove(*widget);
  198. if(!onWidgetClosed.empty())
  199. onWidgetClosed();
  200. return;
  201. }
  202. }
  203. }
  204. void EditorWidgetContainer::_addCallbackOnDraggedWidgetDropped(std::function<void()> callback)
  205. {
  206. if(mIsHandlingWidgetDragAndDrop)
  207. mWidgetDroppedCallback = callback;
  208. }
  209. }