BsEditorWidgetManager.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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))
  41. {
  42. for (auto& widgetData : mActiveWidgets)
  43. {
  44. EditorWidgetBase* widget = widgetData.second;
  45. EditorWidgetContainer* parentContainer = widget->_getParent();
  46. EditorWindowBase* parentWindow = parentContainer->getParentWindow();
  47. RenderWindowPtr parentRenderWindow = parentWindow->getRenderWindow();
  48. const RenderWindowProperties& props = parentRenderWindow->getProperties();
  49. if (!props.hasFocus())
  50. {
  51. widget->_setHasFocus(false);
  52. continue;
  53. }
  54. if (parentContainer->getActiveWidget() != widget)
  55. {
  56. widget->_setHasFocus(false);
  57. continue;
  58. }
  59. Vector2I widgetPos = widget->screenToWidgetPos(gInput().getPointerPosition());
  60. if (widgetPos.x >= 0 && widgetPos.y >= 0
  61. && widgetPos.x < (INT32)widget->getWidth()
  62. && widgetPos.y < (INT32)widget->getHeight())
  63. {
  64. widget->_setHasFocus(true);
  65. }
  66. else
  67. widget->_setHasFocus(false);
  68. }
  69. }
  70. }
  71. void EditorWidgetManager::registerWidget(const String& name, std::function<EditorWidgetBase*(EditorWidgetContainer&)> createCallback)
  72. {
  73. auto iterFind = mCreateCallbacks.find(name);
  74. if(iterFind != mCreateCallbacks.end())
  75. BS_EXCEPT(InvalidParametersException, "Widget with the same name is already registered. Name: \"" + name + "\"");
  76. mCreateCallbacks[name] = createCallback;
  77. }
  78. void EditorWidgetManager::unregisterWidget(const String& name)
  79. {
  80. mCreateCallbacks.erase(name);
  81. }
  82. EditorWidgetBase* EditorWidgetManager::open(const String& name)
  83. {
  84. auto iterFind = mActiveWidgets.find(name);
  85. if(iterFind != mActiveWidgets.end())
  86. return iterFind->second;
  87. EditorWindow* window = EditorWindow::create();
  88. EditorWidgetBase* newWidget = create(name, window->widgets());
  89. if(newWidget == nullptr)
  90. {
  91. window->close();
  92. return nullptr;
  93. }
  94. return newWidget;
  95. }
  96. void EditorWidgetManager::close(EditorWidgetBase* widget)
  97. {
  98. auto findIter = std::find_if(mActiveWidgets.begin(), mActiveWidgets.end(),
  99. [&] (const std::pair<String, EditorWidgetBase*>& entry) { return entry.second == widget; });
  100. if(findIter != mActiveWidgets.end())
  101. mActiveWidgets.erase(findIter);
  102. if(widget->mParent != nullptr)
  103. widget->mParent->_notifyWidgetDestroyed(widget);
  104. EditorWidgetBase::destroy(widget);
  105. }
  106. EditorWidgetBase* EditorWidgetManager::create(const String& name, EditorWidgetContainer& parentContainer)
  107. {
  108. auto iterFind = mActiveWidgets.find(name);
  109. if(iterFind != mActiveWidgets.end())
  110. {
  111. EditorWidgetBase* existingWidget = iterFind->second;
  112. if(existingWidget->_getParent() != nullptr && existingWidget->_getParent() != &parentContainer)
  113. existingWidget->_getParent()->remove(*existingWidget);
  114. if(existingWidget->_getParent() != &parentContainer)
  115. parentContainer.add(*iterFind->second);
  116. return iterFind->second;
  117. }
  118. auto iterFindCreate = mCreateCallbacks.find(name);
  119. if(iterFindCreate == mCreateCallbacks.end())
  120. return nullptr;
  121. EditorWidgetBase* newWidget = mCreateCallbacks[name](parentContainer);
  122. parentContainer.add(*newWidget);
  123. if(newWidget != nullptr)
  124. mActiveWidgets[name] = newWidget;
  125. return newWidget;
  126. }
  127. bool EditorWidgetManager::isValidWidget(const String& name) const
  128. {
  129. auto iterFindCreate = mCreateCallbacks.find(name);
  130. return iterFindCreate != mCreateCallbacks.end();
  131. }
  132. EditorWidgetLayoutPtr EditorWidgetManager::getLayout() const
  133. {
  134. auto GetWidgetNamesInContainer = [&] (const EditorWidgetContainer* container)
  135. {
  136. Vector<String> widgetNames;
  137. if(container != nullptr)
  138. {
  139. UINT32 numWidgets = container->getNumWidgets();
  140. for(UINT32 i = 0; i < numWidgets; i++)
  141. {
  142. EditorWidgetBase* widget = container->getWidget(i);
  143. widgetNames.push_back(widget->getName());
  144. }
  145. }
  146. return widgetNames;
  147. };
  148. MainEditorWindow* mainWindow = EditorWindowManager::instance().getMainWindow();
  149. DockManager& dockManager = mainWindow->getDockManager();
  150. EditorWidgetLayoutPtr layout = bs_shared_ptr<EditorWidgetLayout>(dockManager.getLayout());
  151. Vector<EditorWidgetLayout::Entry>& layoutEntries = layout->getEntries();
  152. UnorderedSet<EditorWidgetContainer*> widgetContainers;
  153. for(auto& widget : mActiveWidgets)
  154. {
  155. widgetContainers.insert(widget.second->_getParent());
  156. }
  157. for(auto& widgetContainer : widgetContainers)
  158. {
  159. if(widgetContainer == nullptr)
  160. continue;
  161. layoutEntries.push_back(EditorWidgetLayout::Entry());
  162. EditorWidgetLayout::Entry& entry = layoutEntries.back();
  163. entry.widgetNames = GetWidgetNamesInContainer(widgetContainer);
  164. EditorWindowBase* parentWindow = widgetContainer->getParentWindow();
  165. entry.isDocked = parentWindow->isMain(); // Assumed widget is docked if part of main window
  166. if(!entry.isDocked)
  167. {
  168. entry.x = parentWindow->getLeft();
  169. entry.y = parentWindow->getTop();
  170. entry.width = parentWindow->getWidth();
  171. entry.height = parentWindow->getHeight();
  172. }
  173. }
  174. layout->setIsMainWindowMaximized(mainWindow->getRenderWindow()->getProperties().isMaximized());
  175. return layout;
  176. }
  177. void EditorWidgetManager::setLayout(const EditorWidgetLayoutPtr& layout)
  178. {
  179. // Unparent all widgets
  180. Vector<EditorWidgetBase*> unparentedWidgets;
  181. for(auto& widget : mActiveWidgets)
  182. {
  183. if(widget.second->_getParent() != nullptr)
  184. widget.second->_getParent()->remove(*(widget.second));
  185. unparentedWidgets.push_back(widget.second);
  186. }
  187. // Restore floating widgets
  188. for(auto& entry : layout->getEntries())
  189. {
  190. if(entry.isDocked)
  191. continue;
  192. EditorWindow* window = EditorWindow::create();
  193. for(auto& widgetName : entry.widgetNames)
  194. {
  195. create(widgetName, window->widgets());
  196. }
  197. window->setPosition(entry.x, entry.y);
  198. window->setSize(entry.width, entry.height);
  199. if(window->widgets().getNumWidgets() == 0)
  200. window->close();
  201. }
  202. // Restore docked widgets
  203. MainEditorWindow* mainWindow = EditorWindowManager::instance().getMainWindow();
  204. DockManager& dockManager = mainWindow->getDockManager();
  205. dockManager.setLayout(layout->getDockLayout());
  206. // Destroy any widgets that are no longer have parents
  207. for(auto& widget : unparentedWidgets)
  208. {
  209. if(widget->_getParent() == nullptr)
  210. widget->close();
  211. }
  212. if (layout->getIsMainWindowMaximized())
  213. mainWindow->getRenderWindow()->maximize(gCoreAccessor());
  214. }
  215. void EditorWidgetManager::onFocusGained(const RenderWindow& window)
  216. {
  217. // Do nothing, possibly regain focus on last focused widget?
  218. }
  219. void EditorWidgetManager::onFocusLost(const RenderWindow& window)
  220. {
  221. for (auto& widgetData : mActiveWidgets)
  222. {
  223. EditorWidgetBase* widget = widgetData.second;
  224. EditorWidgetContainer* parentContainer = widget->_getParent();
  225. if (parentContainer == nullptr)
  226. continue;
  227. EditorWindowBase* parentWindow = parentContainer->getParentWindow();
  228. RenderWindowPtr parentRenderWindow = parentWindow->getRenderWindow();
  229. if (parentRenderWindow.get() != &window)
  230. continue;
  231. widget->_setHasFocus(false);
  232. }
  233. }
  234. void EditorWidgetManager::preRegisterWidget(const String& name, std::function<EditorWidgetBase*(EditorWidgetContainer&)> createCallback)
  235. {
  236. QueuedCreateCallbacks.push(std::pair<String, std::function<EditorWidgetBase*(EditorWidgetContainer&)>>(name, createCallback));
  237. }
  238. }