BsEditorWidgetContainer.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. using namespace CamelotFramework;
  9. using namespace BansheeEngine;
  10. namespace BansheeEditor
  11. {
  12. const CM::UINT32 EditorWidgetContainer::TitleBarHeight = 13;
  13. EditorWidgetContainer::EditorWidgetContainer(BS::GUIWidget* parent)
  14. :mParent(parent), mX(0), mY(0), mWidth(0), mHeight(0), mTitleBar(nullptr), mActiveWidget(-1)
  15. {
  16. mTitleBar = cm_new<GUITabbedTitleBar>(parent);
  17. mTitleBar->onTabActivated.connect(boost::bind(&EditorWidgetContainer::tabActivated, this, _1));
  18. mTitleBar->onTabClosed.connect(boost::bind(&EditorWidgetContainer::tabClosed, this, _1));
  19. mTitleBar->onTabDraggedOff.connect(boost::bind(&EditorWidgetContainer::tabDraggedOff, this, _1));
  20. mTitleBar->onTabDraggedOn.connect(boost::bind(&EditorWidgetContainer::tabDraggedOn, this, _1));
  21. }
  22. EditorWidgetContainer::~EditorWidgetContainer()
  23. {
  24. cm_delete(mTitleBar);
  25. for(auto& widget : mWidgets)
  26. {
  27. EditorWidget::destroy(widget);
  28. }
  29. }
  30. void EditorWidgetContainer::add(EditorWidget& widget)
  31. {
  32. insert((UINT32)mWidgets.size(), widget);
  33. }
  34. void EditorWidgetContainer::remove(EditorWidget& widget)
  35. {
  36. INT32 widgetIdx = -1;
  37. UINT32 curIdx = 0;
  38. for(auto& curWidget : mWidgets)
  39. {
  40. if(curWidget == &widget)
  41. {
  42. widgetIdx = curIdx;
  43. break;
  44. }
  45. curIdx++;
  46. }
  47. if(widgetIdx == -1)
  48. return;
  49. mWidgets.erase(mWidgets.begin() + widgetIdx);
  50. mTitleBar->removeTab((UINT32)widgetIdx);
  51. widget._changeParent(nullptr);
  52. if(widgetIdx == mActiveWidget)
  53. {
  54. if(mWidgets.size() > 0)
  55. {
  56. setActiveWidget(0);
  57. }
  58. }
  59. }
  60. void EditorWidgetContainer::insert(CM::UINT32 idx, EditorWidget& widget)
  61. {
  62. for(auto& curWidget : mWidgets)
  63. {
  64. if(curWidget == &widget)
  65. return;
  66. }
  67. idx = Math::Clamp(idx, 0U, (UINT32)mWidgets.size());
  68. mTitleBar->insertTab(idx, widget.getName());
  69. mWidgets.insert(mWidgets.begin() + idx, &widget);
  70. widget._changeParent(this);
  71. if(mActiveWidget == -1)
  72. setActiveWidget((UINT32)mWidgets.size() - 1);
  73. else
  74. widget._disable();
  75. }
  76. // Note: Hiding is used for a special purpose right now and there is no way to unhide a widget
  77. // (But such functionally may be easily added)
  78. void EditorWidgetContainer::hide(EditorWidget& widget)
  79. {
  80. INT32 widgetIdx = -1;
  81. UINT32 curIdx = 0;
  82. for(auto& curWidget : mWidgets)
  83. {
  84. if(curWidget == &widget)
  85. {
  86. widgetIdx = curIdx;
  87. break;
  88. }
  89. curIdx++;
  90. }
  91. if(widgetIdx == -1)
  92. return;
  93. mTitleBar->removeTab(widgetIdx);
  94. if(widgetIdx == mActiveWidget)
  95. {
  96. if(mWidgets.size() > 0)
  97. {
  98. setActiveWidget(0);
  99. }
  100. }
  101. }
  102. void EditorWidgetContainer::setSize(UINT32 width, UINT32 height)
  103. {
  104. // TODO - Title bar is always TitleBarHeight size, so what happens when the container area is smaller than that?
  105. mTitleBar->setSize(width, TitleBarHeight);
  106. if(mActiveWidget >= 0)
  107. {
  108. EditorWidget* activeWidgetPtr = mWidgets[mActiveWidget];
  109. UINT32 contentHeight = (UINT32)std::max(0, (INT32)height - (INT32)TitleBarHeight);
  110. activeWidgetPtr->_setSize(width, contentHeight);
  111. }
  112. mWidth = width;
  113. mHeight = height;
  114. }
  115. void EditorWidgetContainer::setPosition(INT32 x, INT32 y)
  116. {
  117. mTitleBar->setPosition(x, y);
  118. if(mActiveWidget >= 0)
  119. {
  120. EditorWidget* activeWidgetPtr = mWidgets[mActiveWidget];
  121. activeWidgetPtr->_setPosition(x, y + TitleBarHeight);
  122. }
  123. mX = x;
  124. mY = y;
  125. }
  126. void EditorWidgetContainer::setActiveWidget(UINT32 idx)
  127. {
  128. if(mActiveWidget == idx)
  129. return;
  130. mActiveWidget = idx;
  131. UINT32 curIdx = 0;
  132. for(auto& curWidget : mWidgets)
  133. {
  134. if(curIdx != (UINT32)mActiveWidget)
  135. curWidget->_disable();
  136. else
  137. curWidget->_enable();
  138. curIdx++;
  139. }
  140. setPosition(mX, mY);
  141. setSize(mWidth, mHeight);
  142. }
  143. void EditorWidgetContainer::tabActivated(UINT32 idx)
  144. {
  145. setActiveWidget(idx);
  146. }
  147. void EditorWidgetContainer::tabClosed(UINT32 idx)
  148. {
  149. EditorWidget* widget = mWidgets[idx];
  150. remove(*widget);
  151. EditorWidget::destroy(widget);
  152. if(!onWidgetClosed.empty())
  153. onWidgetClosed();
  154. }
  155. void EditorWidgetContainer::tabDraggedOff(CM::UINT32 idx)
  156. {
  157. EditorWidget* widget = mWidgets[idx];
  158. hide(*widget);
  159. // TODO - Hook up drag and drop texture
  160. DragAndDropManager::instance().startDrag(HTexture(), (UINT32)DragAndDropType::EditorWidget, (void*)widget, boost::bind(&EditorWidgetContainer::tabDroppedCallback, this, _1));
  161. // We don't want to remove the widget just yet. For now mark it as hidden in case
  162. // user just drops it somewhere else.
  163. // Note: This is primarily implemented because Windows doesn't like me destroying a window
  164. // while I'm capturing mouse input (required for drag and drop outside of window borders).
  165. // So instead I just hide it and do the destroying after drag and drop is done.
  166. if(!onWidgetHidden.empty())
  167. onWidgetHidden();
  168. }
  169. void EditorWidgetContainer::tabDraggedOn(CM::UINT32 idx)
  170. {
  171. #if CM_DEBUG_MODE
  172. if(!DragAndDropManager::instance().isDragInProgress())
  173. CM_EXCEPT(InternalErrorException, "Tab drag and drop reported but no drag in progress.");
  174. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  175. CM_EXCEPT(InternalErrorException, "Tab drag and drop reported but drag type is invalid.");
  176. #endif
  177. EditorWidget* draggedWidget = static_cast<EditorWidget*>(DragAndDropManager::instance().getDragData());
  178. insert(idx, *draggedWidget);
  179. }
  180. void EditorWidgetContainer::tabDroppedCallback(bool wasDragProcessed)
  181. {
  182. if(DragAndDropManager::instance().getDragTypeId() != (UINT32)DragAndDropType::EditorWidget)
  183. return;
  184. EditorWidget* draggedWidget = static_cast<EditorWidget*>(DragAndDropManager::instance().getDragData());
  185. draggedWidget->_changeParent(nullptr); // To ensure it doesn't get destroyed with the parent window
  186. remove(*draggedWidget);
  187. if(!onWidgetClosed.empty())
  188. onWidgetClosed();
  189. if(!wasDragProcessed)
  190. {
  191. EditorWindow* newWindow = EditorWindow::create();
  192. newWindow->widgets().add(*draggedWidget);
  193. Int2 mousePos = Input::instance().getMousePosition();
  194. newWindow->setPosition(mousePos.x, mousePos.y);
  195. }
  196. }
  197. void EditorWidgetContainer::_notifyWidgetDestroyed(EditorWidget* widget)
  198. {
  199. for(auto& curWidget : mWidgets)
  200. {
  201. if(curWidget == widget)
  202. {
  203. remove(*widget);
  204. if(!onWidgetClosed.empty())
  205. onWidgetClosed();
  206. return;
  207. }
  208. }
  209. }
  210. }