BsEditorWidget.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 "BsEditorWidgetManager.h"
  6. #include "BsEvent.h"
  7. #include "BsRect2I.h"
  8. namespace BansheeEngine
  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. /**
  46. * Checks is the widget the currently active widget in its container. This means the widget's tab is active or
  47. * the widget is the only one in its container.
  48. */
  49. bool isActive() const { return mIsActive; }
  50. /**
  51. * Gets the parent editor window this widget is docked in. Can be null (for example when widget is in the process of
  52. * dragging and not visible).
  53. */
  54. EditorWindowBase* getParentWindow() const;
  55. /**
  56. * Returns the parent widget container. Can be null (for example when widget is in the process of dragging and not
  57. * visible).
  58. */
  59. EditorWidgetContainer* _getParent() const { return mParent; }
  60. /** Converts screen coordinates to coordinates relative to the widget. */
  61. Vector2I screenToWidgetPos(const Vector2I& screenPos) const;
  62. /** Converts widget relative coordinates to screen coordiantes. */
  63. Vector2I widgetToScreenPos(const Vector2I& widgetPos) const;
  64. /** Closes the widget, undocking it from its container and freeing any resources related to it. */
  65. void close();
  66. /** Internal method. Called once per frame. */
  67. virtual void update() { }
  68. Event<void(UINT32, UINT32)> onResized; /**< Triggered whenever widget size changes. */
  69. Event<void(INT32, INT32)> onMoved; /**< Triggered whenever widget position changes. */
  70. Event<void(EditorWidgetContainer*)> onParentChanged; /**< Triggered whenever widget parent container changes. */
  71. Event<void(bool)> onFocusChanged; /**< Triggered whenever widget receives or loses focus. */
  72. /** @name Internal
  73. * @{
  74. */
  75. /** Changes the size of the widget (and its internal GUI panel). */
  76. void _setSize(UINT32 width, UINT32 height);
  77. /** Changes the position of the widget (and its internal GUI panel), relative to its parent container. */
  78. void _setPosition(INT32 x, INT32 y);
  79. /**
  80. * Changes the parent container of the widget (for example when re-docking or moving a widget to another window).
  81. * Parent can be null (for example when widget is in the process of dragging and not visible).
  82. */
  83. void _changeParent(EditorWidgetContainer* parent);
  84. /** Sets or removes focus for this widget. */
  85. void _setHasFocus(bool focus);
  86. /** Disables the widget making its GUI contents not visible. The widget remains docked in its container. */
  87. void _disable();
  88. /** Enables the widget making its previously hidden GUI contents visible. */
  89. void _enable();
  90. /** @} */
  91. protected:
  92. friend class EditorWidgetManager;
  93. EditorWidgetBase(const HString& displayName, const String& name, UINT32 defaultWidth,
  94. UINT32 defaultHeight, EditorWidgetContainer& parentContainer);
  95. virtual ~EditorWidgetBase();
  96. /** Triggered whenever widget position changes. */
  97. virtual void doOnMoved(INT32 x, INT32 y);
  98. /** Triggered whenever widget size changes. */
  99. virtual void doOnResized(UINT32 width, UINT32 height);
  100. /** Triggered whenever widget parent container changes. */
  101. virtual void doOnParentChanged();
  102. /**
  103. * Returns the parent GUI widget. Before calling this you must ensure the widget has a container parent otherwise
  104. * this method will fail.
  105. */
  106. GUIWidget& getParentWidget() const;
  107. /** Frees widget resources and deletes the instance. */
  108. static void destroy(EditorWidgetBase* widget);
  109. String mName;
  110. HString mDisplayName;
  111. EditorWidgetContainer* mParent;
  112. INT32 mX, mY;
  113. UINT32 mWidth, mHeight;
  114. UINT32 mDefaultWidth, mDefaultHeight;
  115. GUIPanel* mContent;
  116. bool mHasFocus;
  117. bool mIsActive;
  118. };
  119. /** @} */
  120. /** @cond INTERNAL */
  121. /** @addtogroup EditorWindow
  122. * @{
  123. */
  124. /**
  125. * Helper class that registers a widget creation callback with the widget manager. The creation callback allows the
  126. * runtime to open widgets just by their name without knowing the actual type.
  127. */
  128. template<typename Type>
  129. struct RegisterWidgetOnStart
  130. {
  131. public:
  132. RegisterWidgetOnStart()
  133. {
  134. EditorWidgetManager::preRegisterWidget(Type::getTypeName(), &create);
  135. }
  136. /** Creates a new widget of a specific type and adds it to the provided container. */
  137. static EditorWidgetBase* create(EditorWidgetContainer& parentContainer)
  138. {
  139. return bs_new<Type>(EditorWidget<Type>::ConstructPrivately(), parentContainer);
  140. }
  141. };
  142. /**
  143. * Editor widget template class that widgets can inherit from. Ensures that all widget implementations are automatically
  144. * registered with the widget manager.
  145. *
  146. * @see EditorWidgetBase
  147. */
  148. template <class Type>
  149. class EditorWidget : public EditorWidgetBase
  150. {
  151. static volatile RegisterWidgetOnStart<Type> RegisterOnStart;
  152. protected:
  153. friend struct RegisterWidgetOnStart<Type>;
  154. struct ConstructPrivately {};
  155. EditorWidget(const HString& displayName, UINT32 defaultWidth, UINT32 defaultHeight,
  156. EditorWidgetContainer& parentContainer)
  157. :EditorWidgetBase(displayName, Type::getTypeName(), defaultWidth, defaultHeight, parentContainer)
  158. { }
  159. public:
  160. virtual ~EditorWidget() { }
  161. };
  162. template <typename Type>
  163. volatile RegisterWidgetOnStart<Type> EditorWidget<Type>::RegisterOnStart;
  164. /** @} */
  165. /** @endcond */
  166. }