BsGUIWidget.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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, CM::Viewport* target, CM::RenderWindow* ownerWindow)
  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. assert(target != nullptr);
  29. assert(ownerWindow != nullptr);
  30. if(mOwnerWindow != nullptr)
  31. CM_EXCEPT(InvalidStateException, "Widget has already been initialized.");
  32. mTarget = target;
  33. mOwnerWindow = ownerWindow;
  34. mOwnerTargetResizedConn = mTarget->onResized.connect(boost::bind(&GUIWidget::ownerTargetResized, this));
  35. GUIManager::instance().registerWidget(this);
  36. }
  37. GUIWidget::~GUIWidget()
  38. {
  39. if(mTarget != nullptr)
  40. {
  41. GUIManager::instance().unregisterWidget(this);
  42. mOwnerTargetResizedConn.disconnect();
  43. }
  44. // Iterate over all elements in this way because each
  45. // GUIElement::destroy call internally unregisters the element
  46. // from the widget, and modifies the mElements array
  47. while(mElements.size() > 0)
  48. {
  49. GUIElement::destroy(mElements[0]);
  50. }
  51. for(auto& area : mAreas)
  52. {
  53. GUIArea::destroyInternal(area);
  54. }
  55. mElements.clear();
  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. // If an element has any parents we send the events to all parents first and only then to the children unless
  104. // the parents process them
  105. Stack<GUIElement*>::type todo;
  106. GUIElementBase* curElement = element;
  107. do
  108. {
  109. if(curElement->_getType() == GUIElementBase::Type::Element)
  110. todo.push(static_cast<GUIElement*>(curElement));
  111. curElement = curElement->_getParent();
  112. } while(curElement != nullptr);
  113. while(true)
  114. {
  115. GUIElement* elem = todo.top();
  116. todo.pop();
  117. if(elem->mouseEvent(ev))
  118. return true;
  119. if(todo.size() == 0)
  120. return false;
  121. }
  122. return false;
  123. }
  124. bool GUIWidget::_textInputEvent(GUIElement* element, const GUITextInputEvent& ev)
  125. {
  126. // If an element has any parents we send the events to all parents first and only then to the children unless
  127. // the parents process them
  128. Stack<GUIElement*>::type todo;
  129. GUIElementBase* curElement = element;
  130. do
  131. {
  132. if(curElement->_getType() == GUIElementBase::Type::Element)
  133. todo.push(static_cast<GUIElement*>(curElement));
  134. curElement = curElement->_getParent();
  135. } while(curElement != nullptr);
  136. while(true)
  137. {
  138. GUIElement* elem = todo.top();
  139. todo.pop();
  140. if(elem->textInputEvent(ev))
  141. return true;
  142. if(todo.size() == 0)
  143. return false;
  144. }
  145. return false;
  146. }
  147. bool GUIWidget::_commandEvent(GUIElement* element, const GUICommandEvent& ev)
  148. {
  149. // If an element has any parents we send the events to all parents first and only then to the children unless
  150. // the parents process them
  151. Stack<GUIElement*>::type todo;
  152. GUIElementBase* curElement = element;
  153. do
  154. {
  155. if(curElement->_getType() == GUIElementBase::Type::Element)
  156. todo.push(static_cast<GUIElement*>(curElement));
  157. curElement = curElement->_getParent();
  158. } while(curElement != nullptr);
  159. while(true)
  160. {
  161. GUIElement* elem = todo.top();
  162. todo.pop();
  163. if(elem->commandEvent(ev))
  164. return true;
  165. if(todo.size() == 0)
  166. return false;
  167. }
  168. return false;
  169. }
  170. void GUIWidget::registerElement(GUIElement* elem)
  171. {
  172. assert(elem != nullptr);
  173. mElements.push_back(elem);
  174. mWidgetIsDirty = true;
  175. }
  176. void GUIWidget::unregisterElement(GUIElement* elem)
  177. {
  178. assert(elem != nullptr);
  179. auto iterFind = std::find(begin(mElements), end(mElements), elem);
  180. if(iterFind == mElements.end())
  181. CM_EXCEPT(InvalidParametersException, "Cannot unregister an element that is not registered on this widget.");
  182. mElements.erase(iterFind);
  183. mWidgetIsDirty = true;
  184. }
  185. void GUIWidget::registerArea(GUIArea* area)
  186. {
  187. assert(area != nullptr);
  188. mAreas.push_back(area);
  189. mWidgetIsDirty = true;
  190. }
  191. void GUIWidget::unregisterArea(GUIArea* area)
  192. {
  193. assert(area != nullptr);
  194. auto iterFind = std::find(begin(mAreas), end(mAreas), area);
  195. if(iterFind == mAreas.end())
  196. CM_EXCEPT(InvalidParametersException, "Cannot unregister an area that is not registered on this widget.");
  197. mAreas.erase(iterFind);
  198. mWidgetIsDirty = true;
  199. }
  200. void GUIWidget::setSkin(const GUISkin& skin)
  201. {
  202. mSkin = &skin;
  203. }
  204. const GUISkin& GUIWidget::getSkin() const
  205. {
  206. if(mSkin != nullptr)
  207. return *mSkin;
  208. else
  209. return DefaultSkin;
  210. }
  211. bool GUIWidget::isDirty(bool cleanIfDirty)
  212. {
  213. if(cleanIfDirty)
  214. {
  215. bool dirty = mWidgetIsDirty;
  216. mWidgetIsDirty = false;
  217. for(auto& elem : mElements)
  218. {
  219. if(elem->_isContentDirty())
  220. {
  221. dirty = true;
  222. elem->updateRenderElements();
  223. }
  224. if(elem->_isMeshDirty())
  225. {
  226. dirty = true;
  227. elem->_markAsClean();
  228. }
  229. }
  230. if(dirty)
  231. updateBounds();
  232. return dirty;
  233. }
  234. else
  235. {
  236. if(mWidgetIsDirty)
  237. return true;
  238. for(auto& elem : mElements)
  239. {
  240. if(elem->_isContentDirty() || elem->_isMeshDirty())
  241. {
  242. return true;
  243. }
  244. }
  245. return false;
  246. }
  247. }
  248. bool GUIWidget::inBounds(const Int2& position) const
  249. {
  250. // Technically GUI widget bounds can be larger than the viewport, so make sure we clip to viewport first
  251. if(!getTarget()->getArea().contains(position))
  252. return false;
  253. const Matrix4& worldTfrm = SO()->getWorldTfrm();
  254. Vector3 vecPos((float)position.x, (float)position.y, 0.0f);
  255. vecPos = worldTfrm.inverse() * vecPos;
  256. Int2 localPos(Math::RoundToInt(vecPos.x), Math::RoundToInt(vecPos.y));
  257. return mBounds.contains(localPos);
  258. }
  259. void GUIWidget::updateBounds() const
  260. {
  261. if(mElements.size() > 0)
  262. mBounds = mElements[0]->_getClippedBounds();
  263. for(auto& elem : mElements)
  264. {
  265. Rect elemBounds = elem->_getClippedBounds();
  266. mBounds.encapsulate(elemBounds);
  267. }
  268. }
  269. void GUIWidget::ownerTargetResized()
  270. {
  271. for(auto& area : mAreas)
  272. {
  273. area->updateSizeBasedOnParent(getTarget()->getWidth(), getTarget()->getHeight());
  274. }
  275. }
  276. void GUIWidget::ownerWindowFocusChanged()
  277. {
  278. }
  279. }