BsEditorWidget.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. /** 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. UINT32 getIndex();
  53. /**
  54. * Gets the parent editor window this widget is docked in. Can be null (for example when widget is in the process of
  55. * dragging and not visible).
  56. */
  57. EditorWindowBase* getParentWindow() const;
  58. /**
  59. * Returns the parent widget container. Can be null (for example when widget is in the process of dragging and not
  60. * visible).
  61. */
  62. EditorWidgetContainer* _getParent() const { return mParent; }
  63. /** Converts screen coordinates to coordinates relative to the widget. */
  64. Vector2I screenToWidgetPos(const Vector2I& screenPos) const;
  65. /** Converts widget relative coordinates to screen coordiantes. */
  66. Vector2I widgetToScreenPos(const Vector2I& widgetPos) const;
  67. /** Closes the widget, undocking it from its container and freeing any resources related to it. */
  68. void close();
  69. /** Internal method. Called once per frame. */
  70. virtual void update() { }
  71. Event<void(UINT32, UINT32)> onResized; /**< Triggered whenever widget size changes. */
  72. Event<void(INT32, INT32)> onMoved; /**< Triggered whenever widget position changes. */
  73. Event<void(EditorWidgetContainer*)> onParentChanged; /**< Triggered whenever widget parent container changes. */
  74. Event<void(bool)> onFocusChanged; /**< Triggered whenever widget receives or loses focus. */
  75. /** @name Internal
  76. * @{
  77. */
  78. /** Changes the size of the widget (and its internal GUI panel). */
  79. void _setSize(UINT32 width, UINT32 height);
  80. /** Changes the position of the widget (and its internal GUI panel), relative to its parent container. */
  81. void _setPosition(INT32 x, INT32 y);
  82. /**
  83. * Changes the parent container of the widget (for example when re-docking or moving a widget to another window).
  84. * Parent can be null (for example when widget is in the process of dragging and not visible).
  85. */
  86. void _changeParent(EditorWidgetContainer* parent, UINT32 indx);
  87. /** Sets or removes focus for this widget. */
  88. void _setHasFocus(bool focus);
  89. /** Disables the widget making its GUI contents not visible. The widget remains docked in its container. */
  90. void _disable();
  91. /** Enables the widget making its previously hidden GUI contents visible. */
  92. void _enable();
  93. /** @} */
  94. protected:
  95. friend class EditorWidgetManager;
  96. EditorWidgetBase(const HString& displayName, const String& name, UINT32 defaultWidth,
  97. UINT32 defaultHeight, EditorWidgetContainer& parentContainer);
  98. virtual ~EditorWidgetBase();
  99. /** Triggered whenever widget position changes. */
  100. virtual void doOnMoved(INT32 x, INT32 y);
  101. /** Triggered whenever widget size changes. */
  102. virtual void doOnResized(UINT32 width, UINT32 height);
  103. /** Triggered whenever widget parent container changes. */
  104. virtual void doOnParentChanged();
  105. /**
  106. * Returns the parent GUI widget. Before calling this you must ensure the widget has a container parent otherwise
  107. * this method will fail.
  108. */
  109. GUIWidget& getParentWidget() const;
  110. /** Frees widget resources and deletes the instance. */
  111. static void destroy(EditorWidgetBase* widget);
  112. String mName;
  113. HString mDisplayName;
  114. EditorWidgetContainer* mParent;
  115. INT32 mX, mY;
  116. UINT32 mWidth, mHeight;
  117. UINT32 mDefaultWidth, mDefaultHeight;
  118. UINT32 mIndex = 0;
  119. GUIPanel* mContent;
  120. bool mHasFocus;
  121. bool mIsActive;
  122. };
  123. /** @} */
  124. /** @addtogroup EditorWindow-Internal
  125. * @{
  126. */
  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. }