BsEditorWidgetManager.h 3.6 KB

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