BsGUIWidget.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "CmCoreThreadAccessor.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. onElementAdded = std::shared_ptr<boost::signal<void(GUIElement*)>>(cm_new<boost::signal<void(GUIElement*)>>());
  29. onElementRemoved = std::shared_ptr<boost::signal<void(GUIElement*)>>(cm_new<boost::signal<void(GUIElement*)>>());
  30. }
  31. GUIWidget::~GUIWidget()
  32. {
  33. if(mTarget != nullptr)
  34. GUIManager::instance().unregisterWidget(this);
  35. // Iterate over all elements in this way because each
  36. // GUIElement::destroy call internally unregisters the element
  37. // from the widget, and modifies the mElements array
  38. while(mElements.size() > 0)
  39. {
  40. GUIElement::destroy(mElements[0]);
  41. }
  42. for(auto& area : mAreas)
  43. {
  44. GUIArea::destroyInternal(area);
  45. }
  46. mElements.clear();
  47. }
  48. void GUIWidget::initialize(Viewport* target, RenderWindow* ownerWindow)
  49. {
  50. assert(target != nullptr);
  51. assert(ownerWindow != nullptr);
  52. if(mOwnerWindow != nullptr)
  53. CM_EXCEPT(InvalidStateException, "Widget has already been initialized.");
  54. mTarget = target;
  55. mOwnerWindow = ownerWindow;
  56. GUIManager::instance().registerWidget(this);
  57. }
  58. void GUIWidget::update()
  59. {
  60. // If the widgets parent scene object moved, we need to mark it as dirty
  61. // as the GUIManager batching relies on object positions, so it needs to be updated.
  62. const float diffEpsilon = 0.0001f;
  63. Vector3 position = SO()->getWorldPosition();
  64. Quaternion rotation = SO()->getWorldRotation();
  65. Vector3 scale = SO()->getWorldScale();
  66. if(!mWidgetIsDirty)
  67. {
  68. Vector3 posDiff = mLastFramePosition - position;
  69. if(Math::Abs(posDiff.x) > diffEpsilon || Math::Abs(posDiff.y) > diffEpsilon || Math::Abs(posDiff.z) > diffEpsilon)
  70. {
  71. mWidgetIsDirty = true;
  72. }
  73. else
  74. {
  75. Quaternion rotDiff = mLastFrameRotation - rotation;
  76. if(Math::Abs(rotDiff.x) > diffEpsilon || Math::Abs(rotDiff.y) > diffEpsilon ||
  77. Math::Abs(rotDiff.z) > diffEpsilon || Math::Abs(rotDiff.w) > diffEpsilon)
  78. {
  79. mWidgetIsDirty = true;
  80. }
  81. else
  82. {
  83. Vector3 scaleDiff = mLastFrameScale - scale;
  84. if(Math::Abs(scaleDiff.x) > diffEpsilon || Math::Abs(scaleDiff.y) > diffEpsilon || Math::Abs(scaleDiff.z) > diffEpsilon)
  85. {
  86. mWidgetIsDirty = true;
  87. }
  88. }
  89. }
  90. }
  91. mLastFramePosition = position;
  92. mLastFrameRotation = rotation;
  93. mLastFrameScale = scale;
  94. }
  95. void GUIWidget::_updateLayout()
  96. {
  97. for(auto& area : mAreas)
  98. {
  99. area->_update();
  100. }
  101. }
  102. bool GUIWidget::_mouseEvent(GUIElement* element, const GUIMouseEvent& ev)
  103. {
  104. // If an element has any parents we send the events to all parents first and only then to the children unless
  105. // the parents process them
  106. Stack<GUIElement*>::type todo;
  107. GUIElementBase* curElement = element;
  108. do
  109. {
  110. if(curElement->_getType() == GUIElementBase::Type::Element)
  111. todo.push(static_cast<GUIElement*>(curElement));
  112. curElement = curElement->_getParent();
  113. } while(curElement != nullptr);
  114. while(true)
  115. {
  116. GUIElement* elem = todo.top();
  117. todo.pop();
  118. if(elem->mouseEvent(ev))
  119. return true;
  120. if(todo.size() == 0)
  121. return false;
  122. }
  123. return false;
  124. }
  125. bool GUIWidget::_keyEvent(GUIElement* element, const GUIKeyEvent& ev)
  126. {
  127. // If an element has any parents we send the events to all parents first and only then to the children unless
  128. // the parents process them
  129. Stack<GUIElement*>::type todo;
  130. GUIElementBase* curElement = element;
  131. do
  132. {
  133. if(curElement->_getType() == GUIElementBase::Type::Element)
  134. todo.push(static_cast<GUIElement*>(curElement));
  135. curElement = curElement->_getParent();
  136. } while(curElement != nullptr);
  137. while(true)
  138. {
  139. GUIElement* elem = todo.top();
  140. todo.pop();
  141. if(elem->keyEvent(ev))
  142. return true;
  143. if(todo.size() == 0)
  144. return false;
  145. }
  146. return false;
  147. }
  148. void GUIWidget::registerElement(GUIElement* elem)
  149. {
  150. assert(elem != nullptr);
  151. mElements.push_back(elem);
  152. mWidgetIsDirty = true;
  153. if(!onElementAdded->empty())
  154. (*onElementAdded)(elem);
  155. }
  156. void GUIWidget::unregisterElement(GUIElement* elem)
  157. {
  158. assert(elem != nullptr);
  159. auto iterFind = std::find(begin(mElements), end(mElements), elem);
  160. if(iterFind == mElements.end())
  161. CM_EXCEPT(InvalidParametersException, "Cannot unregister an element that is not registered on this widget.");
  162. mElements.erase(iterFind);
  163. mWidgetIsDirty = true;
  164. if(!onElementRemoved->empty())
  165. (*onElementRemoved)(elem);
  166. }
  167. void GUIWidget::registerArea(GUIArea* area)
  168. {
  169. assert(area != nullptr);
  170. mAreas.push_back(area);
  171. mWidgetIsDirty = true;
  172. }
  173. void GUIWidget::unregisterArea(GUIArea* area)
  174. {
  175. assert(area != nullptr);
  176. auto iterFind = std::find(begin(mAreas), end(mAreas), area);
  177. if(iterFind == mAreas.end())
  178. CM_EXCEPT(InvalidParametersException, "Cannot unregister an area that is not registered on this widget.");
  179. mAreas.erase(iterFind);
  180. mWidgetIsDirty = true;
  181. }
  182. void GUIWidget::setSkin(const GUISkin* skin)
  183. {
  184. mSkin = skin;
  185. }
  186. const GUISkin* GUIWidget::getSkin() const
  187. {
  188. if(mSkin != nullptr)
  189. return mSkin;
  190. else
  191. return &DefaultSkin;
  192. }
  193. bool GUIWidget::isDirty(bool cleanIfDirty)
  194. {
  195. if(cleanIfDirty)
  196. {
  197. bool dirty = mWidgetIsDirty;
  198. mWidgetIsDirty = false;
  199. for(auto& elem : mElements)
  200. {
  201. if(elem->_isContentDirty())
  202. {
  203. dirty = true;
  204. elem->updateRenderElements();
  205. }
  206. if(elem->_isMeshDirty())
  207. {
  208. dirty = true;
  209. elem->_markAsClean();
  210. }
  211. }
  212. if(dirty)
  213. updateBounds();
  214. return dirty;
  215. }
  216. else
  217. {
  218. if(mWidgetIsDirty)
  219. return true;
  220. for(auto& elem : mElements)
  221. {
  222. if(elem->_isContentDirty() || elem->_isMeshDirty())
  223. {
  224. return true;
  225. }
  226. }
  227. return false;
  228. }
  229. }
  230. bool GUIWidget::inBounds(const Int2& position) const
  231. {
  232. // Technically GUI widget bounds can be larger than the viewport, so make sure we clip to viewport first
  233. if(!getTarget()->getDimensions().contains(position))
  234. return false;
  235. const Matrix4& worldTfrm = SO()->getWorldTfrm();
  236. Vector3 vecPos((float)position.x, (float)position.y, 0.0f);
  237. vecPos = worldTfrm.inverse() * vecPos;
  238. Int2 localPos(Math::RoundToInt(vecPos.x), Math::RoundToInt(vecPos.y));
  239. return mBounds.contains(localPos);
  240. }
  241. void GUIWidget::updateBounds() const
  242. {
  243. if(mElements.size() > 0)
  244. mBounds = mElements[0]->_getBounds();
  245. for(auto& elem : mElements)
  246. {
  247. Rect elemBounds = elem->_getBounds();
  248. mBounds.encapsulate(elemBounds);
  249. }
  250. }
  251. void GUIWidget::ownerWindowResized()
  252. {
  253. for(auto& area : mAreas)
  254. {
  255. area->notifyWindowResized(getOwnerWindow()->getWidth(), getOwnerWindow()->getHeight());
  256. }
  257. }
  258. void GUIWidget::ownerWindowFocusChanged()
  259. {
  260. }
  261. }