BsEditorWidgetManager.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsEditorPrerequisites.h"
  5. #include "BsModule.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief Handles opening and closing of EditorWidgets. Its primary purpose
  10. * is to keep track of all types of widgets so they can be saved and restored
  11. * upon program shutdown/startup, as well as being able to change widget layout on
  12. * the fly.
  13. */
  14. class BS_ED_EXPORT EditorWidgetManager : public Module<EditorWidgetManager>
  15. {
  16. public:
  17. EditorWidgetManager();
  18. ~EditorWidgetManager();
  19. /**
  20. * @brief Called every frame.
  21. */
  22. void update();
  23. /**
  24. * @brief Registers a widget that can then be opened by calling "open". When loading
  25. * a widget layout this name and callback will be used to attempt creating the widget.
  26. *
  27. * @param name Unique name for the widget.
  28. * @param createCallback Callback that returns a new instance of the widget.
  29. */
  30. void registerWidget(const String& name, std::function<EditorWidgetBase*(EditorWidgetContainer&)> createCallback);
  31. /**
  32. * @brief Unregisters a widget so it may no longer be opened using this manager.
  33. */
  34. void unregisterWidget(const String& name);
  35. /**
  36. * @brief Creates a widget with the given name. If widget is already created it returns the existing instance.
  37. * Widget is opened in a new window.
  38. *
  39. * @param name The name of the widget.
  40. *
  41. * @return Always returns the created widget, and throws an exception if it fails.
  42. */
  43. EditorWidgetBase* open(const String& name);
  44. /**
  45. * @brief Creates a new widget an inserts it into the specified container.
  46. */
  47. EditorWidgetBase* create(const String& name, EditorWidgetContainer& parentContainer);
  48. /**
  49. * @brief Checks if the provided name represents a widget that can be created.
  50. */
  51. bool isValidWidget(const String& name) const;
  52. /**
  53. * @brief Closes the given widget.
  54. */
  55. void close(EditorWidgetBase* widget);
  56. /**
  57. * @brief Closes all open editor widgets.
  58. */
  59. void closeAll();
  60. /**
  61. * @brief Retrieves the layout of all currently active widgets. You may later
  62. * use this layout to restore exact position of the widgets.
  63. */
  64. EditorWidgetLayoutPtr getLayout() const;
  65. /**
  66. * @brief Positions all widgets according to the provided layout. It will open
  67. * new widgets or close current ones if needed.
  68. */
  69. void setLayout(const EditorWidgetLayoutPtr& layout);
  70. /**
  71. * @brief Allows you to queue up widgets that will be registered as soon as an instance of EditorWidgetManager is
  72. * created.
  73. *
  74. * @note Useful primarily when widgets are being registered from static methods, because then there is no
  75. * EditorWidgetManager instance yet.
  76. */
  77. static void preRegisterWidget(const String& name, std::function<EditorWidgetBase*(EditorWidgetContainer&)> createCallback);
  78. private:
  79. /**
  80. * @brief Triggered whenever a window gains focus.
  81. */
  82. void onFocusGained(const RenderWindow& window);
  83. /**
  84. * @brief Triggered whenever a window loses focus.
  85. */
  86. void onFocusLost(const RenderWindow& window);
  87. Map<String, EditorWidgetBase*> mActiveWidgets;
  88. Map<String, std::function<EditorWidgetBase*(EditorWidgetContainer&)>> mCreateCallbacks;
  89. HEvent mOnFocusLostConn;
  90. HEvent mOnFocusGainedConn;
  91. static Stack<std::pair<String, std::function<EditorWidgetBase*(EditorWidgetContainer&)>>> QueuedCreateCallbacks;
  92. };
  93. }