BsEditorWidget.h 7.2 KB

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