BsEditorWidget.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. * @brief Gets the parent editor window this widget is docked in.
  64. */
  65. EditorWindowBase* getParentWindow() const;
  66. /**
  67. * @brief Changes the size of the widget (and its internal GUI panel).
  68. *
  69. * @note Internal method.
  70. */
  71. void _setSize(UINT32 width, UINT32 height);
  72. /**
  73. * @brief Changes the position of the widget (and its internal GUI panel),
  74. * relative to its parent container.
  75. *
  76. * @note Internal method.
  77. */
  78. void _setPosition(INT32 x, INT32 y);
  79. /**
  80. * @brief Changes the parent container of the widget (e.g. when re-docking or moving
  81. * a widget to another window). Parent can be null (e.g. when widget is in the
  82. * process of dragging and not visible).
  83. *
  84. * @note Internal method.
  85. */
  86. void _changeParent(EditorWidgetContainer* parent);
  87. /**
  88. * @brief Sets or removes focus for this widget.
  89. *
  90. * @note Internal method.
  91. */
  92. void _setHasFocus(bool focus);
  93. /**
  94. * @brief Returns the parent widget container. Can be null (e.g. when widget is in the
  95. * process of dragging and not visible).
  96. *
  97. * @note Internal method.
  98. */
  99. EditorWidgetContainer* _getParent() const { return mParent; }
  100. /**
  101. * @brief Converts screen coordinates to coordinates relative to the
  102. * widget.
  103. */
  104. Vector2I screenToWidgetPos(const Vector2I& screenPos) const;
  105. /**
  106. * @brief Converts widget relative coordinates to screen coordiantes.
  107. */
  108. Vector2I widgetToScreenPos(const Vector2I& widgetPos) const;
  109. /**
  110. * @brief Disables the widget making its GUI contents not visible. The widget
  111. * remains docked in its container.
  112. *
  113. * @note Internal method.
  114. */
  115. void _disable();
  116. /**
  117. * @brief Enables the widget making its previously hidden GUI contents visible.
  118. *
  119. * @note Internal method.
  120. */
  121. void _enable();
  122. /**
  123. * @brief Closes the widget, undocking it from its container and freeing any resources
  124. * related to it.
  125. */
  126. void close();
  127. /**
  128. * @brief Called once per frame.
  129. *
  130. * @note Internal method.
  131. */
  132. virtual void update() { }
  133. Event<void(UINT32, UINT32)> onResized; /**< Triggered whenever widget size changes. */
  134. Event<void(INT32, INT32)> onMoved; /**< Triggered whenever widget position changes. */
  135. Event<void(EditorWidgetContainer*)> onParentChanged; /**< Triggered whenever widget parent container changes. */
  136. Event<void(bool)> onFocusChanged; /**< Triggered whenever widget receives or loses focus. */
  137. protected:
  138. friend class EditorWidgetManager;
  139. EditorWidgetBase(const HString& displayName, const String& name, UINT32 defaultWidth,
  140. UINT32 defaultHeight, EditorWidgetContainer& parentContainer);
  141. virtual ~EditorWidgetBase();
  142. /**
  143. * @brief Triggered whenever widget position changes.
  144. */
  145. virtual void doOnMoved(INT32 x, INT32 y);
  146. /**
  147. * @brief Triggered whenever widget size changes.
  148. */
  149. virtual void doOnResized(UINT32 width, UINT32 height);
  150. /**
  151. * @brief Triggered whenever widget parent container changes.
  152. */
  153. virtual void doOnParentChanged();
  154. /**
  155. * @brief Returns the parent GUI widget. Before calling this you must ensure
  156. * the widget has a container parent otherwise this method will fail.
  157. */
  158. CGUIWidget& getParentWidget() const;
  159. /**
  160. * @brief Frees widget resources and deletes the instance.
  161. */
  162. static void destroy(EditorWidgetBase* widget);
  163. String mName;
  164. HString mDisplayName;
  165. EditorWidgetContainer* mParent;
  166. INT32 mX, mY;
  167. UINT32 mWidth, mHeight;
  168. UINT32 mDefaultWidth, mDefaultHeight;
  169. GUIPanel* mContent;
  170. bool mHasFocus;
  171. };
  172. /**
  173. * @brief Helper class that registers a widget creation callback with the widget manager.
  174. * The creation callback allows the runtime to open widgets just by their name
  175. * without knowing the actual type.
  176. */
  177. template<typename Type>
  178. struct RegisterWidgetOnStart
  179. {
  180. public:
  181. RegisterWidgetOnStart()
  182. {
  183. EditorWidgetManager::preRegisterWidget(Type::getTypeName(), &create);
  184. }
  185. /**
  186. * @brief Creates a new widget of a specific type and adds it to the provided container.
  187. */
  188. static EditorWidgetBase* create(EditorWidgetContainer& parentContainer)
  189. {
  190. return bs_new<Type>(EditorWidget<Type>::ConstructPrivately(), parentContainer);
  191. }
  192. };
  193. /**
  194. * @brief Editor widget template class that widgets can inherit from. Ensures that
  195. * all widget implementations are automatically registered with the widget manager.
  196. *
  197. * @see EditorWidgetBase
  198. */
  199. template <class Type>
  200. class EditorWidget : public EditorWidgetBase
  201. {
  202. static volatile RegisterWidgetOnStart<Type> RegisterOnStart;
  203. protected:
  204. friend struct RegisterWidgetOnStart<Type>;
  205. struct ConstructPrivately {};
  206. EditorWidget(const HString& displayName, UINT32 defaultWidth, UINT32 defaultHeight,
  207. EditorWidgetContainer& parentContainer)
  208. :EditorWidgetBase(displayName, Type::getTypeName(), defaultWidth, defaultHeight, parentContainer)
  209. { }
  210. public:
  211. virtual ~EditorWidget() { }
  212. };
  213. template <typename Type>
  214. volatile RegisterWidgetOnStart<Type> EditorWidget<Type>::RegisterOnStart;
  215. }