BsEditorWidget.h 6.7 KB

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