BsEditorWidgetManager.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include "BsEditorWidgetManager.h"
  2. #include "BsEditorWidget.h"
  3. #include "BsEditorWindow.h"
  4. #include "BsEditorWidgetContainer.h"
  5. #include "BsEditorWindowManager.h"
  6. #include "BsMainEditorWindow.h"
  7. #include "BsEditorWidgetLayout.h"
  8. #include "BsDockManager.h"
  9. #include "BsException.h"
  10. #include "BsInput.h"
  11. #include "BsRenderWindow.h"
  12. #include "BsRenderWindowManager.h"
  13. #include "BsVector2I.h"
  14. #include "BsCoreThread.h"
  15. using namespace std::placeholders;
  16. namespace BansheeEngine
  17. {
  18. Stack<std::pair<String, std::function<EditorWidgetBase*(EditorWidgetContainer&)>>> EditorWidgetManager::QueuedCreateCallbacks;
  19. EditorWidgetManager::EditorWidgetManager()
  20. {
  21. while(!QueuedCreateCallbacks.empty())
  22. {
  23. std::pair<String, std::function<EditorWidgetBase*(EditorWidgetContainer&)>> curElement = QueuedCreateCallbacks.top();
  24. QueuedCreateCallbacks.pop();
  25. registerWidget(curElement.first, curElement.second);
  26. }
  27. mOnFocusLostConn = RenderWindowManager::instance().onFocusLost.connect(std::bind(&EditorWidgetManager::onFocusLost, this, _1));
  28. mOnFocusGainedConn = RenderWindowManager::instance().onFocusGained.connect(std::bind(&EditorWidgetManager::onFocusGained, this, _1));
  29. }
  30. EditorWidgetManager::~EditorWidgetManager()
  31. {
  32. mOnFocusLostConn.disconnect();
  33. mOnFocusGainedConn.disconnect();
  34. Map<String, EditorWidgetBase*> widgetsCopy = mActiveWidgets;
  35. for (auto& widget : widgetsCopy)
  36. widget.second->close();
  37. }
  38. void EditorWidgetManager::update()
  39. {
  40. if (gInput().isPointerButtonDown(PointerEventButton::Left) || gInput().isPointerButtonDown(PointerEventButton::Right))
  41. {
  42. for (auto& widgetData : mActiveWidgets)
  43. {
  44. EditorWidgetBase* widget = widgetData.second;
  45. EditorWidgetContainer* parentContainer = widget->_getParent();
  46. if (parentContainer == nullptr)
  47. {
  48. widget->_setHasFocus(false);
  49. continue;
  50. }
  51. EditorWindowBase* parentWindow = parentContainer->getParentWindow();
  52. RenderWindowPtr parentRenderWindow = parentWindow->getRenderWindow();
  53. const RenderWindowProperties& props = parentRenderWindow->getProperties();
  54. if (!props.hasFocus())
  55. {
  56. widget->_setHasFocus(false);
  57. continue;
  58. }
  59. if (parentContainer->getActiveWidget() != widget)
  60. {
  61. widget->_setHasFocus(false);
  62. continue;
  63. }
  64. Vector2I widgetPos = widget->screenToWidgetPos(gInput().getPointerPosition());
  65. if (widgetPos.x >= 0 && widgetPos.y >= 0
  66. && widgetPos.x < (INT32)widget->getWidth()
  67. && widgetPos.y < (INT32)widget->getHeight())
  68. {
  69. widget->_setHasFocus(true);
  70. }
  71. else
  72. widget->_setHasFocus(false);
  73. }
  74. }
  75. }
  76. void EditorWidgetManager::registerWidget(const String& name, std::function<EditorWidgetBase*(EditorWidgetContainer&)> createCallback)
  77. {
  78. auto iterFind = mCreateCallbacks.find(name);
  79. if(iterFind != mCreateCallbacks.end())
  80. BS_EXCEPT(InvalidParametersException, "Widget with the same name is already registered. Name: \"" + name + "\"");
  81. mCreateCallbacks[name] = createCallback;
  82. }
  83. void EditorWidgetManager::unregisterWidget(const String& name)
  84. {
  85. mCreateCallbacks.erase(name);
  86. }
  87. EditorWidgetBase* EditorWidgetManager::open(const String& name)
  88. {
  89. auto iterFind = mActiveWidgets.find(name);
  90. if(iterFind != mActiveWidgets.end())
  91. return iterFind->second;
  92. EditorWindow* window = EditorWindow::create();
  93. EditorWidgetBase* newWidget = create(name, window->widgets());
  94. if(newWidget == nullptr)
  95. {
  96. window->close();
  97. return nullptr;
  98. }
  99. Vector2I widgetSize(newWidget->getDefaultWidth(), newWidget->getDefaultHeight());
  100. Vector2I windowSize = EditorWidgetContainer::widgetToWindowSize(widgetSize);
  101. window->setSize((UINT32)windowSize.x, (UINT32)windowSize.y);
  102. return newWidget;
  103. }
  104. void EditorWidgetManager::close(EditorWidgetBase* widget)
  105. {
  106. auto findIter = std::find_if(mActiveWidgets.begin(), mActiveWidgets.end(),
  107. [&] (const std::pair<String, EditorWidgetBase*>& entry) { return entry.second == widget; });
  108. if(findIter != mActiveWidgets.end())
  109. mActiveWidgets.erase(findIter);
  110. if(widget->mParent != nullptr)
  111. widget->mParent->_notifyWidgetDestroyed(widget);
  112. EditorWidgetBase::destroy(widget);
  113. }
  114. void EditorWidgetManager::closeAll()
  115. {
  116. Vector<EditorWidgetBase*> toClose(mActiveWidgets.size());
  117. UINT32 idx = 0;
  118. for (auto& widget : mActiveWidgets)
  119. toClose[idx++] = widget.second;
  120. for (auto& widget : toClose)
  121. widget->close();
  122. }
  123. EditorWidgetBase* EditorWidgetManager::create(const String& name, EditorWidgetContainer& parentContainer)
  124. {
  125. auto iterFind = mActiveWidgets.find(name);
  126. if(iterFind != mActiveWidgets.end())
  127. {
  128. EditorWidgetBase* existingWidget = iterFind->second;
  129. if(existingWidget->_getParent() != nullptr && existingWidget->_getParent() != &parentContainer)
  130. existingWidget->_getParent()->remove(*existingWidget);
  131. if(existingWidget->_getParent() != &parentContainer)
  132. parentContainer.add(*iterFind->second);
  133. return iterFind->second;
  134. }
  135. auto iterFindCreate = mCreateCallbacks.find(name);
  136. if(iterFindCreate == mCreateCallbacks.end())
  137. return nullptr;
  138. EditorWidgetBase* newWidget = mCreateCallbacks[name](parentContainer);
  139. parentContainer.add(*newWidget);
  140. if(newWidget != nullptr)
  141. mActiveWidgets[name] = newWidget;
  142. return newWidget;
  143. }
  144. bool EditorWidgetManager::isValidWidget(const String& name) const
  145. {
  146. auto iterFindCreate = mCreateCallbacks.find(name);
  147. return iterFindCreate != mCreateCallbacks.end();
  148. }
  149. EditorWidgetLayoutPtr EditorWidgetManager::getLayout() const
  150. {
  151. auto GetWidgetNamesInContainer = [&] (const EditorWidgetContainer* container)
  152. {
  153. Vector<String> widgetNames;
  154. if(container != nullptr)
  155. {
  156. UINT32 numWidgets = container->getNumWidgets();
  157. for(UINT32 i = 0; i < numWidgets; i++)
  158. {
  159. EditorWidgetBase* widget = container->getWidget(i);
  160. widgetNames.push_back(widget->getName());
  161. }
  162. }
  163. return widgetNames;
  164. };
  165. MainEditorWindow* mainWindow = EditorWindowManager::instance().getMainWindow();
  166. DockManager& dockManager = mainWindow->getDockManager();
  167. EditorWidgetLayoutPtr layout = bs_shared_ptr_new<EditorWidgetLayout>(dockManager.getLayout());
  168. Vector<EditorWidgetLayout::Entry>& layoutEntries = layout->getEntries();
  169. UnorderedSet<EditorWidgetContainer*> widgetContainers;
  170. for(auto& widget : mActiveWidgets)
  171. {
  172. widgetContainers.insert(widget.second->_getParent());
  173. }
  174. for(auto& widgetContainer : widgetContainers)
  175. {
  176. if(widgetContainer == nullptr)
  177. continue;
  178. layoutEntries.push_back(EditorWidgetLayout::Entry());
  179. EditorWidgetLayout::Entry& entry = layoutEntries.back();
  180. entry.widgetNames = GetWidgetNamesInContainer(widgetContainer);
  181. EditorWindowBase* parentWindow = widgetContainer->getParentWindow();
  182. entry.isDocked = parentWindow->isMain(); // Assumed widget is docked if part of main window
  183. if(!entry.isDocked)
  184. {
  185. entry.x = parentWindow->getLeft();
  186. entry.y = parentWindow->getTop();
  187. entry.width = parentWindow->getWidth();
  188. entry.height = parentWindow->getHeight();
  189. }
  190. }
  191. layout->setIsMainWindowMaximized(mainWindow->getRenderWindow()->getProperties().isMaximized());
  192. return layout;
  193. }
  194. void EditorWidgetManager::setLayout(const EditorWidgetLayoutPtr& layout)
  195. {
  196. // Unparent all widgets
  197. Vector<EditorWidgetBase*> unparentedWidgets;
  198. for(auto& widget : mActiveWidgets)
  199. {
  200. if(widget.second->_getParent() != nullptr)
  201. widget.second->_getParent()->remove(*(widget.second));
  202. unparentedWidgets.push_back(widget.second);
  203. }
  204. // Restore floating widgets
  205. for(auto& entry : layout->getEntries())
  206. {
  207. if(entry.isDocked)
  208. continue;
  209. EditorWindow* window = EditorWindow::create();
  210. for(auto& widgetName : entry.widgetNames)
  211. {
  212. create(widgetName, window->widgets());
  213. }
  214. window->setPosition(entry.x, entry.y);
  215. window->setSize(entry.width, entry.height);
  216. if(window->widgets().getNumWidgets() == 0)
  217. window->close();
  218. }
  219. // Restore docked widgets
  220. MainEditorWindow* mainWindow = EditorWindowManager::instance().getMainWindow();
  221. DockManager& dockManager = mainWindow->getDockManager();
  222. dockManager.setLayout(layout->getDockLayout());
  223. // Destroy any widgets that are no longer have parents
  224. for(auto& widget : unparentedWidgets)
  225. {
  226. if(widget->_getParent() == nullptr)
  227. widget->close();
  228. }
  229. if (layout->getIsMainWindowMaximized())
  230. mainWindow->getRenderWindow()->maximize(gCoreAccessor());
  231. }
  232. void EditorWidgetManager::onFocusGained(const RenderWindow& window)
  233. {
  234. // Do nothing, possibly regain focus on last focused widget?
  235. }
  236. void EditorWidgetManager::onFocusLost(const RenderWindow& window)
  237. {
  238. for (auto& widgetData : mActiveWidgets)
  239. {
  240. EditorWidgetBase* widget = widgetData.second;
  241. EditorWidgetContainer* parentContainer = widget->_getParent();
  242. if (parentContainer == nullptr)
  243. continue;
  244. EditorWindowBase* parentWindow = parentContainer->getParentWindow();
  245. RenderWindowPtr parentRenderWindow = parentWindow->getRenderWindow();
  246. if (parentRenderWindow.get() != &window)
  247. continue;
  248. widget->_setHasFocus(false);
  249. }
  250. }
  251. void EditorWidgetManager::preRegisterWidget(const String& name, std::function<EditorWidgetBase*(EditorWidgetContainer&)> createCallback)
  252. {
  253. QueuedCreateCallbacks.push(std::pair<String, std::function<EditorWidgetBase*(EditorWidgetContainer&)>>(name, createCallback));
  254. }
  255. }