BsEditorWidgetManager.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. #include "BsEditorPrerequisites.h"
  3. #include "CmModule.h"
  4. namespace BansheeEditor
  5. {
  6. /**
  7. * @brief Handles opening and closing of EditorWidgets. Its primary purpose
  8. * is to keep track of all types of widgets so they can be saved and restored
  9. * upon program shutdown/startup, as well as being able to change widget layout on
  10. * the fly.
  11. */
  12. class EditorWidgetManager : public CM::Module<EditorWidgetManager>
  13. {
  14. public:
  15. EditorWidgetManager();
  16. /**
  17. * @brief Registers a widget that can then be opened by calling "open". When loading
  18. * a widget layout this name and callback will be used to attempt creating the widget.
  19. *
  20. * @param name Unique name for the widget.
  21. * @param createCallback Callback that returns a new instance of the widget.
  22. */
  23. void registerWidget(const CM::String& name, std::function<EditorWidgetBase*()> createCallback);
  24. /**
  25. * @brief Creates a widget with the given name. If widget is already created it returns the existing instance.
  26. * Widget is opened in a new window.
  27. *
  28. * @param name The name of the widget.
  29. *
  30. * @return Always returns the created widget, and throws an exception if it fails.
  31. */
  32. EditorWidgetBase* open(const CM::String& name);
  33. /**
  34. * @brief Closes the given widget.
  35. */
  36. void close(EditorWidgetBase* widget);
  37. /**
  38. * @brief Retrieves the layout of all currently active widgets. You may later
  39. * use this layout to restore exact position of the widgets.
  40. */
  41. EditorWidgetLayoutPtr getLayout() const;
  42. /**
  43. * @brief Positions all widgets according to the provided layout. It will open
  44. * new widgets or close current ones if needed.
  45. */
  46. void setLayout(const EditorWidgetLayoutPtr& layout);
  47. /**
  48. * @brief Allows you to queue up widgets that will be registered as soon as an instance of EditorWidgetManager is
  49. * created.
  50. *
  51. * @note Useful primarily when widgets are being registered from static methods, because then there is no
  52. * EditorWidgetManager instance yet.
  53. */
  54. static void preRegisterWidget(const CM::String& name, std::function<EditorWidgetBase*()> createCallback);
  55. private:
  56. CM::Map<CM::String, EditorWidgetBase*>::type mActiveWidgets;
  57. CM::Map<CM::String, std::function<EditorWidgetBase*()>>::type mCreateCallbacks;
  58. static CM::Stack<std::pair<CM::String, std::function<EditorWidgetBase*()>>>::type QueuedCreateCallbacks;
  59. bool isOpen(const CM::String& name) const;
  60. EditorWidgetBase* create(const CM::String& name);
  61. };
  62. }