2
0

BsEditorWidget.h 7.5 KB

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