BsGUIWidget.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #pragma once
  2. #include "BsPrerequisites.h"
  3. #include "BsComponent.h"
  4. #include "BsRect2I.h"
  5. #include "BsVector3.h"
  6. #include "BsQuaternion.h"
  7. #include "BsEvent.h"
  8. namespace BansheeEngine
  9. {
  10. /**
  11. * @brief A top level container for all types of GUI elements. Every GUI element, layout or area
  12. * must be assigned to a widget in order to be rendered.
  13. *
  14. * Widgets are the only GUI objects that may be arbitrarily transformed, allowing you to create
  15. * 3D interfaces.
  16. */
  17. class BS_EXPORT GUIWidget : public Component
  18. {
  19. public:
  20. virtual ~GUIWidget();
  21. /**
  22. * @brief Sets the skin used for all GUI elements in the widget. This will update
  23. * the look of all current elements.
  24. */
  25. void setSkin(const HGUISkin& skin);
  26. /**
  27. * @brief Returns the currently active GUI skin.
  28. */
  29. const GUISkin& getSkin() const;
  30. /**
  31. * @brief Returns the currently active GUI skin resource.
  32. */
  33. const HGUISkin& getSkinResource() const { return mSkin; }
  34. /**
  35. * @brief Returns the root GUI panel for the widget.
  36. */
  37. GUIPanel* getPanel() const { return mPanel; }
  38. /**
  39. * @brief Returns the depth to render the widget at. If two widgets overlap the
  40. * widget with the lower depth will be rendered in front.
  41. */
  42. UINT8 getDepth() const { return mDepth; }
  43. /**
  44. * @brief Changes the depth to render the widget at. If two widgets overlap the
  45. * widget with the lower depth will be rendered in front.
  46. */
  47. void setDepth(UINT8 depth);
  48. /**
  49. * @brief Checks are the specified coordinates within widget bounds. Coordinates should
  50. * be relative to the parent window.
  51. */
  52. bool inBounds(const Vector2I& position) const;
  53. /**
  54. * @brief Returns bounds of the widget, relative to the parent window.
  55. */
  56. const Rect2I& getBounds() const { return mBounds; }
  57. /**
  58. * @brief Return true if widget or any of its elements are dirty.
  59. *
  60. * @param cleanIfDirty If true, all dirty elements will be updated and widget will be marked as clean.
  61. *
  62. * @return True if dirty, false if not. If "cleanIfDirty" is true, the returned state is the one before cleaning.
  63. */
  64. bool isDirty(bool cleanIfDirty);
  65. /**
  66. * @brief Returns the viewport that this widget will be rendered on.
  67. */
  68. Viewport* getTarget() const { return mTarget; }
  69. /**
  70. * @brief Returns a list of all elements parented to this widget.
  71. */
  72. const Vector<GUIElement*>& getElements() const { return mElements; }
  73. /**
  74. * @brief Registers a new element as a child of the widget.
  75. *
  76. * @note Internal method.
  77. */
  78. void _registerElement(GUIElementBase* elem);
  79. /**
  80. * @brief Unregisters an element from the widget. Usually called when the element
  81. * is destroyed, or reparented to another widget.
  82. *
  83. * @note Internal method.
  84. */
  85. void _unregisterElement(GUIElementBase* elem);
  86. /**
  87. * @brief Marks the widget mesh dirty requiring a mesh rebuild. Provided element
  88. * is the one that requested the mesh update.
  89. */
  90. void _markMeshDirty(GUIElementBase* elem);
  91. /**
  92. * @brief Updates the layout of all child elements, repositioning and resizing them as needed.
  93. */
  94. void _updateLayout();
  95. /**
  96. * @brief Updates the layout of the provided element, and queues content updates.
  97. */
  98. void _updateLayout(GUIElementBase* elem);
  99. /**
  100. * @brief Forwards the specified mouse event to the specified element. The element
  101. * must be a child of this widget.
  102. */
  103. virtual bool _mouseEvent(GUIElement* element, const GUIMouseEvent& ev);
  104. /**
  105. * @brief Forwards the specified key event to the specified element. The element
  106. * must be a child of this widget.
  107. */
  108. virtual bool _textInputEvent(GUIElement* element, const GUITextInputEvent& ev);
  109. /**
  110. * @brief Forwards the specified key event to the specified element. The element
  111. * must be a child of this widget.
  112. */
  113. virtual bool _commandEvent(GUIElement* element, const GUICommandEvent& ev);
  114. /**
  115. * @brief Forwards the specified virtual button event to the specified element. The element
  116. * must be a child of this widget.
  117. */
  118. virtual bool _virtualButtonEvent(GUIElement* element, const GUIVirtualButtonEvent& ev);
  119. protected:
  120. friend class SceneObject;
  121. friend class GUIElementBase;
  122. friend class GUIManager;
  123. /**
  124. * @brief Constructs a new GUI widget attached to the specified parent scene object.
  125. * Widget elements will be rendered on the provided viewport.
  126. */
  127. GUIWidget(const HSceneObject& parent, Viewport* target);
  128. /**
  129. * @brief Called when the viewport size changes and widget elements need to be updated.
  130. */
  131. virtual void ownerTargetResized();
  132. /**
  133. * @brief Called when the parent window gained or lost focus.
  134. */
  135. virtual void ownerWindowFocusChanged();
  136. /**
  137. * @copydoc Component::update
  138. */
  139. virtual void update() override;
  140. /**
  141. * @copydoc Component::onDestroyed
  142. */
  143. virtual void onDestroyed() override;
  144. private:
  145. GUIWidget(const GUIWidget& other) { }
  146. /**
  147. * @brief Calculates widget bounds using the bounds of all child elements.
  148. */
  149. void updateBounds() const;
  150. /**
  151. * @brief Updates the size of the primary GUI panel based on the viewport.
  152. */
  153. void updateRootPanel();
  154. Viewport* mTarget;
  155. Vector<GUIElement*> mElements;
  156. GUIPanel* mPanel;
  157. UINT8 mDepth;
  158. Vector3 mLastFramePosition;
  159. Quaternion mLastFrameRotation;
  160. Vector3 mLastFrameScale;
  161. HEvent mOwnerTargetResizedConn;
  162. UnorderedSet<GUIElement*> mDirtyContents;
  163. mutable bool mWidgetIsDirty;
  164. mutable Rect2I mBounds;
  165. mutable Vector<HMesh> mCachedMeshes;
  166. mutable Vector<HMaterial> mCachedMaterials;
  167. HGUISkin mSkin;
  168. /************************************************************************/
  169. /* RTTI */
  170. /************************************************************************/
  171. public:
  172. friend class GUIWidgetRTTI;
  173. static RTTITypeBase* getRTTIStatic();
  174. virtual RTTITypeBase* getRTTI() const override;
  175. GUIWidget() { } // Serialization only
  176. };
  177. }