BsEditorWidgetManager.h 3.5 KB

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