BsEditorWidget.h 7.4 KB

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