BsGUIWidget.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisites.h"
  5. #include "Math/BsRect2I.h"
  6. #include "Math/BsVector3.h"
  7. #include "Math/BsQuaternion.h"
  8. #include "Math/BsMatrix4.h"
  9. #include "Utility/BsEvent.h"
  10. namespace bs
  11. {
  12. /** @addtogroup GUI
  13. * @{
  14. */
  15. /**
  16. * A top level container for all types of GUI elements. Every GUI element, layout or area must be assigned to a widget
  17. * in order to be rendered.
  18. *
  19. * Widgets are the only GUI objects that may be arbitrarily transformed, allowing you to create 3D interfaces.
  20. */
  21. class BS_EXPORT GUIWidget
  22. {
  23. public:
  24. virtual ~GUIWidget();
  25. /** Sets the skin used for all GUI elements in the widget. This will update the look of all current elements. */
  26. void setSkin(const HGUISkin& skin);
  27. /** Returns the currently active GUI skin. */
  28. const GUISkin& getSkin() const;
  29. /** Returns the currently active GUI skin resource. */
  30. const HGUISkin& getSkinResource() const { return mSkin; }
  31. /** Returns the root GUI panel for the widget. */
  32. GUIPanel* getPanel() const { return mPanel; }
  33. /**
  34. * Returns the depth to render the widget at. If two widgets overlap the widget with the lower depth will be
  35. * rendered in front.
  36. */
  37. UINT8 getDepth() const { return mDepth; }
  38. /**
  39. * Changes the depth to render the widget at. If two widgets overlap the widget with the lower depth will be
  40. * rendered in front.
  41. */
  42. void setDepth(UINT8 depth);
  43. /**
  44. * Checks are the specified coordinates within widget bounds. Coordinates should be relative to the parent window.
  45. */
  46. bool inBounds(const Vector2I& position) const;
  47. /** Returns bounds of the widget, relative to the parent window. */
  48. const Rect2I& getBounds() const { return mBounds; }
  49. /**
  50. * Return true if widget or any of its elements are dirty.
  51. *
  52. * @param[in] cleanIfDirty If true, all dirty elements will be updated and widget will be marked as clean.
  53. * @return True if dirty, false if not. If "cleanIfDirty" is true, the returned state is the
  54. * one before cleaning.
  55. */
  56. bool isDirty(bool cleanIfDirty);
  57. /** Returns the viewport that this widget will be rendered on. */
  58. Viewport* getTarget() const;
  59. /** Returns the camera this widget is being rendered to. */
  60. SPtr<Camera> getCamera() const { return mCamera; }
  61. /** Changes to which camera does the widget output its contents. */
  62. void setCamera(const SPtr<Camera>& camera);
  63. /** Returns a list of all elements parented to this widget. */
  64. const Vector<GUIElement*>& getElements() const { return mElements; }
  65. /** Returns the world transform that all GUI elements beloning to this widget will be transformed by. */
  66. const Matrix4 getWorldTfrm() const { return mTransform; }
  67. /** Checks whether the widget should be rendered or not. */
  68. bool getIsActive() const { return mIsActive; }
  69. /** Sets whether the widget should be rendered or not. */
  70. void setIsActive(bool active);
  71. /** Creates a new GUI widget that will be rendered on the provided camera. */
  72. static SPtr<GUIWidget> create(const SPtr<Camera>& camera);
  73. /** Creates a new GUI widget that will be rendered on the provided camera. */
  74. static SPtr<GUIWidget> create(const HCamera& camera);
  75. /** Triggered when the widget's viewport size changes. */
  76. Event<void()> onOwnerTargetResized;
  77. /** Triggered when the parent window gained or lost focus. */
  78. Event<void()> onOwnerWindowFocusChanged;
  79. public: // ***** INTERNAL ******
  80. /** @name Internal
  81. * @{
  82. */
  83. /** Registers a new element as a child of the widget. */
  84. void _registerElement(GUIElementBase* elem);
  85. /**
  86. * Unregisters an element from the widget. Usually called when the element is destroyed, or reparented to another
  87. * widget.
  88. */
  89. void _unregisterElement(GUIElementBase* elem);
  90. /**
  91. * Marks the widget mesh dirty requiring a mesh rebuild. Provided element is the one that requested the mesh update.
  92. */
  93. void _markMeshDirty(GUIElementBase* elem);
  94. /**
  95. * Marks the elements content as dirty, meaning its internal mesh will need to be rebuilt (this implies the entire
  96. * widget mesh will be rebuilt as well).
  97. */
  98. void _markContentDirty(GUIElementBase* elem);
  99. /** Updates the layout of all child elements, repositioning and resizing them as needed. */
  100. void _updateLayout();
  101. /** Updates the layout of the provided element, and queues content updates. */
  102. void _updateLayout(GUIElementBase* elem);
  103. /**
  104. * Updates internal transform values from the specified scene object, in case that scene object's transform changed
  105. * since the last call.
  106. *
  107. * @note Assumes the same scene object will be provided every time.
  108. */
  109. void _updateTransform(const HSceneObject& parent);
  110. /**
  111. * Checks if the render target of the destination camera changed, and updates the widget with new information if
  112. * it has. Should be called every frame.
  113. */
  114. void _updateRT();
  115. /** Destroys the GUI widget and all child GUI elements. This is called automatically when GUIWidget is deleted. */
  116. void _destroy();
  117. /** @} */
  118. protected:
  119. friend class SceneObject;
  120. friend class GUIElementBase;
  121. friend class GUIManager;
  122. friend class CGUIWidget;
  123. /** Constructs a new GUI widget that will be rendered on the provided camera. */
  124. GUIWidget(const SPtr<Camera>& camera);
  125. /** Constructs a new GUI widget that will be rendered on the provided camera. */
  126. GUIWidget(const HCamera& camera);
  127. /** Common code for constructors. */
  128. void construct(const SPtr<Camera>& camera);
  129. /** Called when the viewport size changes and widget elements need to be updated. */
  130. virtual void ownerTargetResized();
  131. /** Called when the parent window gained or lost focus. */
  132. virtual void ownerWindowFocusChanged();
  133. private:
  134. /** Calculates widget bounds using the bounds of all child elements. */
  135. void updateBounds() const;
  136. /** Updates the size of the primary GUI panel based on the viewport. */
  137. void updateRootPanel();
  138. SPtr<Camera> mCamera;
  139. Vector<GUIElement*> mElements;
  140. GUIPanel* mPanel;
  141. UINT8 mDepth;
  142. bool mIsActive;
  143. Vector3 mPosition;
  144. Quaternion mRotation;
  145. Vector3 mScale;
  146. Matrix4 mTransform;
  147. HEvent mOwnerTargetResizedConn;
  148. Set<GUIElement*> mDirtyContents;
  149. mutable UINT64 mCachedRTId;
  150. mutable bool mWidgetIsDirty;
  151. mutable Rect2I mBounds;
  152. HGUISkin mSkin;
  153. };
  154. /** @} */
  155. }