BsEditorWidgetManager.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "BsEditorWidgetManager.h"
  2. #include "BsEditorWidget.h"
  3. #include "BsEditorWindow.h"
  4. #include "BsEditorWidgetContainer.h"
  5. #include "CmException.h"
  6. using namespace CamelotFramework;
  7. using namespace BansheeEngine;
  8. namespace BansheeEditor
  9. {
  10. Stack<std::pair<String, std::function<EditorWidgetBase*()>>>::type EditorWidgetManager::QueuedCreateCallbacks;
  11. EditorWidgetManager::EditorWidgetManager()
  12. {
  13. while(!QueuedCreateCallbacks.empty())
  14. {
  15. std::pair<String, std::function<EditorWidgetBase*()>> curElement = QueuedCreateCallbacks.top();
  16. QueuedCreateCallbacks.pop();
  17. registerWidget(curElement.first, curElement.second);
  18. }
  19. }
  20. void EditorWidgetManager::registerWidget(const String& name, std::function<EditorWidgetBase*()> createCallback)
  21. {
  22. auto iterFind = mCreateCallbacks.find(name);
  23. if(iterFind != mCreateCallbacks.end())
  24. CM_EXCEPT(InvalidParametersException, "Widget with the same name is already registered. Name: \"" + name + "\"");
  25. mCreateCallbacks[name] = createCallback;
  26. }
  27. EditorWidgetBase* EditorWidgetManager::open(const String& name)
  28. {
  29. auto iterFind = mActiveWidgets.find(name);
  30. if(iterFind != mActiveWidgets.end())
  31. return iterFind->second;
  32. auto iterFindCreate = mCreateCallbacks.find(name);
  33. if(iterFindCreate == mCreateCallbacks.end())
  34. CM_EXCEPT(InvalidParametersException, "Trying to open a widget that is not registered with the widget manager. Name: \"" + name + "\"");
  35. EditorWidgetBase* newWidget = mCreateCallbacks[name]();
  36. EditorWindow* window = EditorWindow::create();
  37. window->widgets().add(*newWidget);
  38. newWidget->initialize();
  39. mActiveWidgets[name] = newWidget;
  40. return newWidget;
  41. }
  42. void EditorWidgetManager::close(EditorWidgetBase* widget)
  43. {
  44. auto findIter = std::find_if(mActiveWidgets.begin(), mActiveWidgets.end(),
  45. [&] (const std::pair<String, EditorWidgetBase*>& entry) { return entry.second == widget; });
  46. if(findIter != mActiveWidgets.end())
  47. mActiveWidgets.erase(findIter);
  48. widget->mParent->_notifyWidgetDestroyed(widget);
  49. EditorWidgetBase::destroy(widget);
  50. }
  51. void EditorWidgetManager::preRegisterWidget(const String& name, std::function<EditorWidgetBase*()> createCallback)
  52. {
  53. QueuedCreateCallbacks.push(std::pair<String, std::function<EditorWidgetBase*()>>(name, createCallback));
  54. }
  55. }