BsGUIWidget.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "BsGUIWidget.h"
  2. #include "BsGUIManager.h"
  3. #include "BsGUISkin.h"
  4. #include "BsGUILabel.h"
  5. #include "BsGUIMouseEvent.h"
  6. #include "BsGUIArea.h"
  7. #include "CmApplication.h"
  8. #include "CmDeferredRenderContext.h"
  9. #include "CmMaterial.h"
  10. #include "CmPass.h"
  11. #include "CmMesh.h"
  12. #include "CmInt2.h"
  13. #include "BsOverlayManager.h"
  14. #include "BsCamera.h"
  15. #include "CmViewport.h"
  16. #include "CmSceneObject.h"
  17. #include "CmRenderWindow.h"
  18. using namespace CamelotFramework;
  19. namespace BansheeEngine
  20. {
  21. GUISkin GUIWidget::DefaultSkin;
  22. GUIWidget::GUIWidget(const HSceneObject& parent)
  23. :Component(parent), mSkin(nullptr), mOwnerWindow(nullptr), mWidgetIsDirty(false), mTarget(nullptr), mDepth(0)
  24. {
  25. mLastFramePosition = SO()->getWorldPosition();
  26. mLastFrameRotation = SO()->getWorldRotation();
  27. mLastFrameScale = SO()->getWorldScale();
  28. }
  29. GUIWidget::~GUIWidget()
  30. {
  31. if(mTarget != nullptr)
  32. GUIManager::instance().unregisterWidget(this);
  33. for(auto& elem : mElements)
  34. {
  35. GUIElement::_destroyInternal(elem);
  36. }
  37. for(auto& area : mAreas)
  38. {
  39. GUIArea::destroyInternal(area);
  40. }
  41. mElements.clear();
  42. mWinResizeConn.disconnect();
  43. mWinFocusConn.disconnect();
  44. }
  45. void GUIWidget::initialize(Viewport* target, const RenderWindow* ownerWindow)
  46. {
  47. assert(target != nullptr);
  48. assert(ownerWindow != nullptr);
  49. if(mOwnerWindow != nullptr)
  50. CM_EXCEPT(InvalidStateException, "Widget has already been initialized.");
  51. mTarget = target;
  52. mOwnerWindow = ownerWindow;
  53. GUIManager::instance().registerWidget(this);
  54. mWinResizeConn = mOwnerWindow->onWindowMovedOrResized.connect(boost::bind(&GUIWidget::ownerWindowResized, this, _1));
  55. mWinFocusConn = mOwnerWindow->onWindowFocusChanged.connect(boost::bind(&GUIWidget::ownerWindowFocusChanged, this, _1));
  56. }
  57. void GUIWidget::update()
  58. {
  59. // If the widgets parent scene object moved, we need to mark it as dirty
  60. // as the GUIManager batching relies on object positions, so it needs to be updated.
  61. const float diffEpsilon = 0.0001f;
  62. Vector3 position = SO()->getWorldPosition();
  63. Quaternion rotation = SO()->getWorldRotation();
  64. Vector3 scale = SO()->getWorldScale();
  65. if(!mWidgetIsDirty)
  66. {
  67. Vector3 posDiff = mLastFramePosition - position;
  68. if(Math::Abs(posDiff.x) > diffEpsilon || Math::Abs(posDiff.y) > diffEpsilon || Math::Abs(posDiff.z) > diffEpsilon)
  69. {
  70. mWidgetIsDirty = true;
  71. }
  72. else
  73. {
  74. Quaternion rotDiff = mLastFrameRotation - rotation;
  75. if(Math::Abs(rotDiff.x) > diffEpsilon || Math::Abs(rotDiff.y) > diffEpsilon ||
  76. Math::Abs(rotDiff.z) > diffEpsilon || Math::Abs(rotDiff.w) > diffEpsilon)
  77. {
  78. mWidgetIsDirty = true;
  79. }
  80. else
  81. {
  82. Vector3 scaleDiff = mLastFrameScale - scale;
  83. if(Math::Abs(scaleDiff.x) > diffEpsilon || Math::Abs(scaleDiff.y) > diffEpsilon || Math::Abs(scaleDiff.z) > diffEpsilon)
  84. {
  85. mWidgetIsDirty = true;
  86. }
  87. }
  88. }
  89. }
  90. mLastFramePosition = position;
  91. mLastFrameRotation = rotation;
  92. mLastFrameScale = scale;
  93. }
  94. void GUIWidget::_updateLayout()
  95. {
  96. for(auto& area : mAreas)
  97. {
  98. area->_update();
  99. }
  100. }
  101. bool GUIWidget::_mouseEvent(GUIElement* element, const GUIMouseEvent& ev)
  102. {
  103. return element->mouseEvent(ev);
  104. }
  105. void GUIWidget::registerElement(GUIElement* elem)
  106. {
  107. assert(elem != nullptr);
  108. mElements.push_back(elem);
  109. mWidgetIsDirty = true;
  110. }
  111. void GUIWidget::unregisterElement(GUIElement* elem)
  112. {
  113. assert(elem != nullptr);
  114. auto iterFind = std::find(begin(mElements), end(mElements), elem);
  115. if(iterFind == mElements.end())
  116. CM_EXCEPT(InvalidParametersException, "Cannot unregister an element that is not registered on this widget.");
  117. mElements.erase(iterFind);
  118. mWidgetIsDirty = true;
  119. }
  120. void GUIWidget::registerArea(GUIArea* area)
  121. {
  122. assert(area != nullptr);
  123. mAreas.push_back(area);
  124. mWidgetIsDirty = true;
  125. }
  126. void GUIWidget::unregisterArea(GUIArea* area)
  127. {
  128. assert(area != nullptr);
  129. auto iterFind = std::find(begin(mAreas), end(mAreas), area);
  130. if(iterFind == mAreas.end())
  131. CM_EXCEPT(InvalidParametersException, "Cannot unregister an area that is not registered on this widget.");
  132. mAreas.erase(iterFind);
  133. mWidgetIsDirty = true;
  134. }
  135. void GUIWidget::setSkin(const GUISkin* skin)
  136. {
  137. mSkin = skin;
  138. }
  139. const GUISkin* GUIWidget::getSkin() const
  140. {
  141. if(mSkin != nullptr)
  142. return mSkin;
  143. else
  144. return &DefaultSkin;
  145. }
  146. bool GUIWidget::isDirty(bool cleanIfDirty)
  147. {
  148. if(cleanIfDirty)
  149. {
  150. bool dirty = mWidgetIsDirty;
  151. mWidgetIsDirty = false;
  152. for(auto& elem : mElements)
  153. {
  154. if(elem->_isDirty())
  155. {
  156. dirty = true;
  157. elem->updateRenderElements();
  158. }
  159. }
  160. updateBounds();
  161. return dirty;
  162. }
  163. else
  164. {
  165. if(mWidgetIsDirty)
  166. return true;
  167. for(auto& elem : mElements)
  168. {
  169. if(elem->_isDirty())
  170. {
  171. return true;
  172. }
  173. }
  174. return false;
  175. }
  176. }
  177. bool GUIWidget::inBounds(const Int2& position) const
  178. {
  179. // Technically GUI widget bounds can be larger than the viewport, so make sure we clip to viewport first
  180. if(!getTarget()->getDimensions().contains(position))
  181. return false;
  182. const Matrix4& worldTfrm = SO()->getWorldTfrm();
  183. Vector3 vecPos((float)position.x, (float)position.y, 0.0f);
  184. vecPos = worldTfrm.inverse() * vecPos;
  185. Int2 localPos(Math::RoundToInt(vecPos.x), Math::RoundToInt(vecPos.y));
  186. return mBounds.contains(localPos);
  187. }
  188. void GUIWidget::updateBounds() const
  189. {
  190. if(mElements.size() > 0)
  191. mBounds = mElements[0]->_getBounds();
  192. for(auto& elem : mElements)
  193. {
  194. Rect elemBounds = elem->_getBounds();
  195. mBounds.encapsulate(elemBounds);
  196. }
  197. }
  198. void GUIWidget::ownerWindowResized(RenderWindow* window)
  199. {
  200. for(auto& area : mAreas)
  201. {
  202. area->notifyWindowResized(window->getWidth(), window->getHeight());
  203. }
  204. }
  205. void GUIWidget::ownerWindowFocusChanged(RenderWindow* window)
  206. {
  207. }
  208. }