BsGUIWidget.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 GUISkin& skin);
  26. /**
  27. * @brief Returns the currently active GUI skin.
  28. */
  29. const GUISkin& getSkin() const;
  30. /**
  31. * @brief Returns the depth to render the widget at. If two widgets overlap the
  32. * widget with the lower depth will be rendered in front.
  33. */
  34. UINT8 getDepth() const { return mDepth; }
  35. /**
  36. * @brief Changes the depth to render the widget at. If two widgets overlap the
  37. * widget with the lower depth will be rendered in front.
  38. */
  39. void setDepth(UINT8 depth) { mDepth = depth; mWidgetIsDirty = true; }
  40. /**
  41. * @brief Checks are the specified coordinates within widget bounds. Coordinates should
  42. * be relative to the parent window.
  43. */
  44. bool inBounds(const Vector2I& position) const;
  45. /**
  46. * @brief Returns bounds of the widget, relative to the parent window.
  47. */
  48. const Rect2I& getBounds() const { return mBounds; }
  49. /**
  50. * @brief Return true if widget or any of its elements are dirty.
  51. *
  52. * @param cleanIfDirty If true, all dirty elements will be updated and widget will be marked as clean.
  53. *
  54. * @return True if dirty, false if not. If "cleanIfDirty" is true, the returned state is the one before cleaning.
  55. */
  56. bool isDirty(bool cleanIfDirty);
  57. /**
  58. * @brief Returns the viewport that this widget will be rendered on.
  59. */
  60. Viewport* getTarget() const { return mTarget; }
  61. /**
  62. * @brief Returns a list of all elements parented to this widget.
  63. */
  64. const Vector<GUIElement*>& getElements() const { return mElements; }
  65. /**
  66. * @brief Updates the layout of all child elements, repositioning and resizing them as needed.
  67. */
  68. void _updateLayout();
  69. /**
  70. * @brief Forwards the specified mouse event to the specified element. The element
  71. * must be a child of this widget.
  72. */
  73. virtual bool _mouseEvent(GUIElement* element, const GUIMouseEvent& ev);
  74. /**
  75. * @brief Forwards the specified key event to the specified element. The element
  76. * must be a child of this widget.
  77. */
  78. virtual bool _textInputEvent(GUIElement* element, const GUITextInputEvent& ev);
  79. /**
  80. * @brief Forwards the specified key event to the specified element. The element
  81. * must be a child of this widget.
  82. */
  83. virtual bool _commandEvent(GUIElement* element, const GUICommandEvent& ev);
  84. /**
  85. * @brief Forwards the specified virtual button event to the specified element. The element
  86. * must be a child of this widget.
  87. */
  88. virtual bool _virtualButtonEvent(GUIElement* element, const GUIVirtualButtonEvent& ev);
  89. /**
  90. * @brief Default skin that is used when no other is assigned.
  91. */
  92. static GUISkin DefaultSkin;
  93. protected:
  94. friend class SceneObject;
  95. friend class GUIElement;
  96. friend class GUIArea;
  97. friend class GUIManager;
  98. /**
  99. * @brief Constructs a new GUI widget attached to the specified parent scene object.
  100. * Widget elements will be rendered on the provided viewport.
  101. */
  102. GUIWidget(const HSceneObject& parent, Viewport* target);
  103. /**
  104. * @brief Registers a new element as a child of the widget.
  105. */
  106. void registerElement(GUIElement* elem);
  107. /**
  108. * @brief Unregisters an element from the widget. Usually called when the element
  109. * is destroyed, or reparented to another widget.
  110. */
  111. void unregisterElement(GUIElement* elem);
  112. /**
  113. * @brief Registers a new areaas a child of the widget.
  114. */
  115. void registerArea(GUIArea* area);
  116. /**
  117. * @brief Unregisters an area from the widget. Usually called when the area is destroyed.
  118. */
  119. void unregisterArea(GUIArea* area);
  120. /**
  121. * @brief Called when the viewport size changes and widget elements need to be updated.
  122. */
  123. virtual void ownerTargetResized();
  124. /**
  125. * @brief Called when the parent window gained or lost focus.
  126. */
  127. virtual void ownerWindowFocusChanged();
  128. /**
  129. * @copydoc Component::update
  130. */
  131. virtual void update();
  132. /**
  133. * @copydoc Component::onDestroyed
  134. */
  135. virtual void onDestroyed();
  136. private:
  137. GUIWidget(const GUIWidget& other) { }
  138. /**
  139. * @brief Calculates widget bounds using the bounds of all child elements.
  140. */
  141. void updateBounds() const;
  142. Viewport* mTarget;
  143. Vector<GUIElement*> mElements;
  144. Vector<GUIArea*> mAreas;
  145. UINT8 mDepth;
  146. Vector3 mLastFramePosition;
  147. Quaternion mLastFrameRotation;
  148. Vector3 mLastFrameScale;
  149. HEvent mOwnerTargetResizedConn;
  150. mutable bool mWidgetIsDirty;
  151. mutable Rect2I mBounds;
  152. mutable Vector<HMesh> mCachedMeshes;
  153. mutable Vector<HMaterial> mCachedMaterials;
  154. const GUISkin* mSkin;
  155. };
  156. }