BsGUIWidget.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "BsCoreApplication.h"
  8. #include "BsCoreThreadAccessor.h"
  9. #include "BsMaterial.h"
  10. #include "BsPass.h"
  11. #include "BsMesh.h"
  12. #include "BsVector2I.h"
  13. #include "BsOverlayManager.h"
  14. #include "BsCamera.h"
  15. #include "BsViewport.h"
  16. #include "BsSceneObject.h"
  17. #include "BsRenderWindow.h"
  18. namespace BansheeEngine
  19. {
  20. GUISkin GUIWidget::DefaultSkin;
  21. GUIWidget::GUIWidget(const HSceneObject& parent, Viewport* target)
  22. :Component(parent), mSkin(nullptr), mWidgetIsDirty(false), mTarget(nullptr), mDepth(0)
  23. {
  24. setName("GUIWidget");
  25. mLastFramePosition = SO()->getWorldPosition();
  26. mLastFrameRotation = SO()->getWorldRotation();
  27. mLastFrameScale = SO()->getWorldScale();
  28. assert(target != nullptr);
  29. mTarget = target;
  30. mOwnerTargetResizedConn = mTarget->getTarget()->onResized.connect(std::bind(&GUIWidget::ownerTargetResized, this));
  31. GUIManager::instance().registerWidget(this);
  32. }
  33. GUIWidget::~GUIWidget()
  34. { }
  35. void GUIWidget::onDestroyed()
  36. {
  37. if (mTarget != nullptr)
  38. {
  39. GUIManager::instance().unregisterWidget(this);
  40. mOwnerTargetResizedConn.disconnect();
  41. }
  42. // Iterate over all elements in this way because each
  43. // GUIElement::destroy call internally unregisters the element
  44. // from the widget, and modifies the mElements array
  45. while (mElements.size() > 0)
  46. {
  47. GUIElement::destroy(mElements[0]);
  48. }
  49. for (auto& area : mAreas)
  50. {
  51. GUIArea::destroyInternal(area);
  52. }
  53. mElements.clear();
  54. }
  55. void GUIWidget::update()
  56. {
  57. // If the widgets parent scene object moved, we need to mark it as dirty
  58. // as the GUIManager batching relies on object positions, so it needs to be updated.
  59. const float diffEpsilon = 0.0001f;
  60. Vector3 position = SO()->getWorldPosition();
  61. Quaternion rotation = SO()->getWorldRotation();
  62. Vector3 scale = SO()->getWorldScale();
  63. if(!mWidgetIsDirty)
  64. {
  65. Vector3 posDiff = mLastFramePosition - position;
  66. if(Math::abs(posDiff.x) > diffEpsilon || Math::abs(posDiff.y) > diffEpsilon || Math::abs(posDiff.z) > diffEpsilon)
  67. {
  68. mWidgetIsDirty = true;
  69. }
  70. else
  71. {
  72. Quaternion rotDiff = mLastFrameRotation - rotation;
  73. if(Math::abs(rotDiff.x) > diffEpsilon || Math::abs(rotDiff.y) > diffEpsilon ||
  74. Math::abs(rotDiff.z) > diffEpsilon || Math::abs(rotDiff.w) > diffEpsilon)
  75. {
  76. mWidgetIsDirty = true;
  77. }
  78. else
  79. {
  80. Vector3 scaleDiff = mLastFrameScale - scale;
  81. if(Math::abs(scaleDiff.x) > diffEpsilon || Math::abs(scaleDiff.y) > diffEpsilon || Math::abs(scaleDiff.z) > diffEpsilon)
  82. {
  83. mWidgetIsDirty = true;
  84. }
  85. }
  86. }
  87. }
  88. mLastFramePosition = position;
  89. mLastFrameRotation = rotation;
  90. mLastFrameScale = scale;
  91. }
  92. void GUIWidget::_updateLayout()
  93. {
  94. for(auto& area : mAreas)
  95. {
  96. area->_update();
  97. }
  98. }
  99. bool GUIWidget::_mouseEvent(GUIElement* element, const GUIMouseEvent& ev)
  100. {
  101. return element->mouseEvent(ev);
  102. }
  103. bool GUIWidget::_textInputEvent(GUIElement* element, const GUITextInputEvent& ev)
  104. {
  105. return element->textInputEvent(ev);
  106. }
  107. bool GUIWidget::_commandEvent(GUIElement* element, const GUICommandEvent& ev)
  108. {
  109. return element->commandEvent(ev);
  110. }
  111. bool GUIWidget::_virtualButtonEvent(GUIElement* element, const GUIVirtualButtonEvent& ev)
  112. {
  113. return element->virtualButtonEvent(ev);
  114. }
  115. void GUIWidget::registerElement(GUIElement* elem)
  116. {
  117. assert(elem != nullptr);
  118. mElements.push_back(elem);
  119. mWidgetIsDirty = true;
  120. }
  121. void GUIWidget::unregisterElement(GUIElement* elem)
  122. {
  123. assert(elem != nullptr);
  124. auto iterFind = std::find(begin(mElements), end(mElements), elem);
  125. if (iterFind != mElements.end())
  126. {
  127. mElements.erase(iterFind);
  128. mWidgetIsDirty = true;
  129. }
  130. }
  131. void GUIWidget::registerArea(GUIArea* area)
  132. {
  133. assert(area != nullptr);
  134. mAreas.push_back(area);
  135. mWidgetIsDirty = true;
  136. }
  137. void GUIWidget::unregisterArea(GUIArea* area)
  138. {
  139. assert(area != nullptr);
  140. auto iterFind = std::find(begin(mAreas), end(mAreas), area);
  141. if(iterFind == mAreas.end())
  142. BS_EXCEPT(InvalidParametersException, "Cannot unregister an area that is not registered on this widget.");
  143. mAreas.erase(iterFind);
  144. mWidgetIsDirty = true;
  145. }
  146. void GUIWidget::setSkin(const GUISkin& skin)
  147. {
  148. mSkin = &skin;
  149. for(auto& element : mElements)
  150. element->_refreshStyle();
  151. }
  152. const GUISkin& GUIWidget::getSkin() const
  153. {
  154. if(mSkin != nullptr)
  155. return *mSkin;
  156. else
  157. return DefaultSkin;
  158. }
  159. bool GUIWidget::isDirty(bool cleanIfDirty)
  160. {
  161. if(cleanIfDirty)
  162. {
  163. bool dirty = mWidgetIsDirty;
  164. mWidgetIsDirty = false;
  165. for(auto& elem : mElements)
  166. {
  167. if(elem->_isContentDirty())
  168. {
  169. dirty = true;
  170. elem->updateRenderElements();
  171. }
  172. if(elem->_isMeshDirty())
  173. {
  174. dirty = true;
  175. elem->_markAsClean();
  176. }
  177. }
  178. if(dirty)
  179. updateBounds();
  180. return dirty;
  181. }
  182. else
  183. {
  184. if(mWidgetIsDirty)
  185. return true;
  186. for(auto& elem : mElements)
  187. {
  188. if(elem->_isContentDirty() || elem->_isMeshDirty())
  189. {
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195. }
  196. bool GUIWidget::inBounds(const Vector2I& position) const
  197. {
  198. // Technically GUI widget bounds can be larger than the viewport, so make sure we clip to viewport first
  199. if(!getTarget()->getArea().contains(position))
  200. return false;
  201. const Matrix4& worldTfrm = SO()->getWorldTfrm();
  202. Vector3 vecPos((float)position.x, (float)position.y, 0.0f);
  203. vecPos = worldTfrm.inverse().multiplyAffine(vecPos);
  204. Vector2I localPos(Math::roundToInt(vecPos.x), Math::roundToInt(vecPos.y));
  205. return mBounds.contains(localPos);
  206. }
  207. void GUIWidget::updateBounds() const
  208. {
  209. if(mElements.size() > 0)
  210. mBounds = mElements[0]->_getClippedBounds();
  211. for(auto& elem : mElements)
  212. {
  213. Rect2I elemBounds = elem->_getClippedBounds();
  214. mBounds.encapsulate(elemBounds);
  215. }
  216. }
  217. void GUIWidget::ownerTargetResized()
  218. {
  219. for(auto& area : mAreas)
  220. {
  221. area->updateSizeBasedOnParent(getTarget()->getWidth(), getTarget()->getHeight());
  222. }
  223. }
  224. void GUIWidget::ownerWindowFocusChanged()
  225. {
  226. }
  227. }