BsEditorWidget.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "EditorWindow/BsEditorWidgetManager.h"
  6. #include "Utility/BsEvent.h"
  7. #include "Math/BsRect2I.h"
  8. namespace bs
  9. {
  10. /** @addtogroup Implementation
  11. * @{
  12. */
  13. /**
  14. * Editor widget represents a single "window" in the editor. It may be dragged, docked and can share space with multiple
  15. * other widgets by using tabs.
  16. *
  17. * Each widget has its own position, size, GUI and update method. Widget is always docked inside an
  18. * EditorWidgetContainer unless it's being dragged and is not visible.
  19. */
  20. class BS_ED_EXPORT EditorWidgetBase
  21. {
  22. public:
  23. /** Gets a unique name for this widget. This name will be used for referencing the widget by other systems. */
  24. const String& getName() const { return mName; }
  25. /** Gets the display name for the widget. This is what editor users will see in the widget title bar. */
  26. const HString& getDisplayName() const { return mDisplayName; }
  27. /** Returns the X position of the widget inside of its container. */
  28. INT32 getX() const { return mX; }
  29. /** Returns the Y position of the widget inside of its container. */
  30. INT32 getY() const { return mY; }
  31. /** Returns the width of the widget in pixels. */
  32. UINT32 getWidth() const { return mWidth; }
  33. /** Returns the height of the widget in pixels. */
  34. UINT32 getHeight() const { return mHeight; }
  35. /** Returns the width of the widget when initially created, in pixels. */
  36. UINT32 getDefaultWidth() const { return mDefaultWidth; }
  37. /** Returns the height of the widget when initially created, in pixels. */
  38. UINT32 getDefaultHeight() const { return mDefaultHeight; }
  39. /** Returns the bounds of the widget in pixels, relative to its parent container. */
  40. Rect2I getBounds() const { return Rect2I(mX, mY, mWidth, mHeight); }
  41. /** Makes the widget in or out focus. Widget can only be made in focus if it is active. */
  42. void setHasFocus(bool focus);
  43. /** Checks if the widget has focus (usually means user clicked on it last). */
  44. bool hasFocus() const { return mHasFocus; }
  45. /** Makes the widget active in its container. This means the widgets tab is active and the widget is visible. */
  46. void setActive();
  47. /**
  48. * Checks is the widget the currently active widget in its container. This means the widget's tab is active or
  49. * the widget is the only one in its container.
  50. */
  51. bool isActive() const { return mIsActive; }
  52. /**
  53. * Gets the parent editor window this widget is docked in. Can be null (for example when widget is in the process of
  54. * dragging and not visible).
  55. */
  56. EditorWindowBase* getParentWindow() const;
  57. /**
  58. * Returns the parent widget container. Can be null (for example when widget is in the process of dragging and not
  59. * visible).
  60. */
  61. EditorWidgetContainer* _getParent() const { return mParent; }
  62. /** Converts screen coordinates to coordinates relative to the widget. */
  63. Vector2I screenToWidgetPos(const Vector2I& screenPos) const;
  64. /** Converts widget relative coordinates to screen coordiantes. */
  65. Vector2I widgetToScreenPos(const Vector2I& widgetPos) const;
  66. /** Closes the widget, undocking it from its container and freeing any resources related to it. */
  67. void close();
  68. /** Internal method. Called once per frame. */
  69. virtual void update() { }
  70. Event<void(UINT32, UINT32)> onResized; /**< Triggered whenever widget size changes. */
  71. Event<void(INT32, INT32)> onMoved; /**< Triggered whenever widget position changes. */
  72. Event<void(EditorWidgetContainer*)> onParentChanged; /**< Triggered whenever widget parent container changes. */
  73. Event<void(bool)> onFocusChanged; /**< Triggered whenever widget receives or loses focus. */
  74. /** @name Internal
  75. * @{
  76. */
  77. /** Changes the size of the widget (and its internal GUI panel). */
  78. void _setSize(UINT32 width, UINT32 height);
  79. /** Changes the position of the widget (and its internal GUI panel), relative to its parent container. */
  80. void _setPosition(INT32 x, INT32 y);
  81. /**
  82. * Changes the parent container of the widget (for example when re-docking or moving a widget to another window).
  83. * Parent can be null (for example when widget is in the process of dragging and not visible).
  84. */
  85. void _changeParent(EditorWidgetContainer* parent, UINT32 indx);
  86. /** Sets or removes focus for this widget. */
  87. void _setHasFocus(bool focus);
  88. /** Disables the widget making its GUI contents not visible. The widget remains docked in its container. */
  89. void _disable();
  90. /** Enables the widget making its previously hidden GUI contents visible. */
  91. void _enable();
  92. /** @} */
  93. protected:
  94. friend class EditorWidgetManager;
  95. EditorWidgetBase(const HString& displayName, const String& name, UINT32 defaultWidth,
  96. UINT32 defaultHeight, EditorWidgetContainer& parentContainer);
  97. virtual ~EditorWidgetBase();
  98. /** Triggered whenever widget position changes. */
  99. virtual void doOnMoved(INT32 x, INT32 y);
  100. /** Triggered whenever widget size changes. */
  101. virtual void doOnResized(UINT32 width, UINT32 height);
  102. /** Triggered whenever widget parent container changes. */
  103. virtual void doOnParentChanged();
  104. /**
  105. * Returns the parent GUI widget. Before calling this you must ensure the widget has a container parent otherwise
  106. * this method will fail.
  107. */
  108. GUIWidget& getParentWidget() const;
  109. /** Frees widget resources and deletes the instance. */
  110. static void destroy(EditorWidgetBase* widget);
  111. String mName;
  112. HString mDisplayName;
  113. EditorWidgetContainer* mParent = nullptr;
  114. INT32 mX = 0, mY = 0;
  115. UINT32 mWidth = 0, mHeight = 0;
  116. UINT32 mDefaultWidth, mDefaultHeight;
  117. UINT32 mIndex = 0;
  118. GUIPanel* mContent = nullptr;
  119. bool mHasFocus = false;
  120. bool mIsActive = true;
  121. };
  122. /** @} */
  123. /** @addtogroup EditorWindow-Internal
  124. * @{
  125. */
  126. template <class Type> class EditorWidget;
  127. /**
  128. * Helper class that registers a widget creation callback with the widget manager. The creation callback allows the
  129. * runtime to open widgets just by their name without knowing the actual type.
  130. */
  131. template<typename Type>
  132. struct RegisterWidgetOnStart
  133. {
  134. public:
  135. RegisterWidgetOnStart()
  136. {
  137. EditorWidgetManager::preRegisterWidget(Type::getTypeName(), &create);
  138. }
  139. /** Creates a new widget of a specific type and adds it to the provided container. */
  140. static EditorWidgetBase* create(EditorWidgetContainer& parentContainer)
  141. {
  142. return bs_new<Type>(EditorWidget<Type>::ConstructPrivately(), parentContainer);
  143. }
  144. };
  145. /**
  146. * Editor widget template class that widgets can inherit from. Ensures that all widget implementations are automatically
  147. * registered with the widget manager.
  148. *
  149. * @see EditorWidgetBase
  150. */
  151. template <class Type>
  152. class EditorWidget : public EditorWidgetBase
  153. {
  154. static volatile RegisterWidgetOnStart<Type> RegisterOnStart;
  155. protected:
  156. friend struct RegisterWidgetOnStart<Type>;
  157. struct ConstructPrivately {};
  158. EditorWidget(const HString& displayName, UINT32 defaultWidth, UINT32 defaultHeight,
  159. EditorWidgetContainer& parentContainer)
  160. :EditorWidgetBase(displayName, Type::getTypeName(), defaultWidth, defaultHeight, parentContainer)
  161. { }
  162. public:
  163. virtual ~EditorWidget() { }
  164. };
  165. template <typename Type>
  166. volatile RegisterWidgetOnStart<Type> EditorWidget<Type>::RegisterOnStart;
  167. /** @} */
  168. }