BsGUIWidget.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "GUI/BsGUIWidget.h"
  4. #include "GUI/BsGUIManager.h"
  5. #include "GUI/BsGUISkin.h"
  6. #include "GUI/BsGUILabel.h"
  7. #include "GUI/BsGUIPanel.h"
  8. #include "Math/BsVector2I.h"
  9. #include "Components/BsCCamera.h"
  10. #include "RenderAPI/BsViewport.h"
  11. #include "Scene/BsSceneObject.h"
  12. #include "Resources/BsBuiltinResources.h"
  13. namespace bs
  14. {
  15. GUIWidget::GUIWidget(const SPtr<Camera>& camera)
  16. : mCamera(camera), mPanel(nullptr), mDepth(0), mIsActive(true), mPosition(BsZero), mRotation(BsIdentity)
  17. , mScale(Vector3::ONE), mTransform(BsIdentity), mCachedRTId(0), mWidgetIsDirty(false)
  18. {
  19. construct(camera);
  20. }
  21. GUIWidget::GUIWidget(const HCamera& camera)
  22. : mCamera(camera->_getCamera()), mPanel(nullptr), mDepth(0), mIsActive(true), mPosition(BsZero)
  23. , mRotation(BsIdentity), mScale(Vector3::ONE), mTransform(BsIdentity), mCachedRTId(0), mWidgetIsDirty(false)
  24. {
  25. construct(mCamera);
  26. }
  27. void GUIWidget::construct(const SPtr<Camera>& camera)
  28. {
  29. if (mCamera != nullptr)
  30. {
  31. SPtr<RenderTarget> target = mCamera->getViewport()->getTarget();
  32. if (target != nullptr)
  33. {
  34. mOwnerTargetResizedConn = target->onResized.connect(std::bind(&GUIWidget::ownerTargetResized, this));
  35. mCachedRTId = target->getInternalID();
  36. }
  37. }
  38. GUIManager::instance().registerWidget(this);
  39. mPanel = GUIPanel::create();
  40. mPanel->_changeParentWidget(this);
  41. updateRootPanel();
  42. }
  43. GUIWidget::~GUIWidget()
  44. {
  45. _destroy();
  46. }
  47. SPtr<GUIWidget> GUIWidget::create(const SPtr<Camera>& camera)
  48. {
  49. return bs_shared_ptr(new (bs_alloc<GUIWidget>()) GUIWidget(camera));
  50. }
  51. SPtr<GUIWidget> GUIWidget::create(const HCamera& camera)
  52. {
  53. return bs_shared_ptr(new (bs_alloc<GUIWidget>()) GUIWidget(camera));
  54. }
  55. void GUIWidget::_destroy()
  56. {
  57. if (mPanel != nullptr)
  58. {
  59. GUILayout::destroy(mPanel);
  60. mPanel = nullptr;
  61. }
  62. if (mCamera != nullptr)
  63. {
  64. GUIManager::instance().unregisterWidget(this);
  65. mOwnerTargetResizedConn.disconnect();
  66. mCamera = nullptr;
  67. }
  68. mElements.clear();
  69. mDirtyContents.clear();
  70. }
  71. void GUIWidget::setDepth(UINT8 depth)
  72. {
  73. mDepth = depth;
  74. mWidgetIsDirty = true;
  75. updateRootPanel();
  76. }
  77. Viewport* GUIWidget::getTarget() const
  78. {
  79. if(mCamera != nullptr)
  80. return mCamera->getViewport().get();
  81. return nullptr;
  82. }
  83. void GUIWidget::_updateTransform(const HSceneObject& parent)
  84. {
  85. // If the widgets parent scene object moved, we need to mark it as dirty
  86. // as the GUIManager batching relies on object positions, so it needs to be updated.
  87. const float diffEpsilon = 0.0001f;
  88. const Transform& tfrm = parent->getTransform();
  89. Vector3 position = tfrm.getPosition();
  90. Quaternion rotation = tfrm.getRotation();
  91. Vector3 scale = tfrm.getScale();
  92. if(!mWidgetIsDirty)
  93. {
  94. if(!Math::approxEquals(mPosition, position, diffEpsilon))
  95. mWidgetIsDirty = true;
  96. else
  97. {
  98. if(!Math::approxEquals(mRotation, rotation, diffEpsilon))
  99. mWidgetIsDirty = true;
  100. else
  101. {
  102. if(Math::approxEquals(mScale, scale))
  103. mWidgetIsDirty = true;
  104. }
  105. }
  106. }
  107. mPosition = position;
  108. mRotation = rotation;
  109. mScale = scale;
  110. mTransform = parent->getWorldMatrix();
  111. }
  112. void GUIWidget::_updateRT()
  113. {
  114. SPtr<RenderTarget> rt;
  115. UINT64 newRTId = 0;
  116. if(mCamera != nullptr)
  117. {
  118. rt = mCamera->getViewport()->getTarget();
  119. if (rt != nullptr)
  120. newRTId = rt->getInternalID();
  121. }
  122. if(mCachedRTId != newRTId)
  123. {
  124. mCachedRTId = newRTId;
  125. mOwnerTargetResizedConn.disconnect();
  126. if(rt != nullptr)
  127. mOwnerTargetResizedConn = rt->onResized.connect(std::bind(&GUIWidget::ownerTargetResized, this));
  128. updateRootPanel();
  129. }
  130. }
  131. void GUIWidget::_updateLayout()
  132. {
  133. bs_frame_mark();
  134. // Determine dirty contents and layouts
  135. FrameStack<GUIElementBase*> todo;
  136. todo.push(mPanel);
  137. while (!todo.empty())
  138. {
  139. GUIElementBase* currentElem = todo.top();
  140. todo.pop();
  141. if (currentElem->_isDirty())
  142. {
  143. GUIElementBase* updateParent = currentElem->_getUpdateParent();
  144. assert(updateParent != nullptr || currentElem == mPanel);
  145. if (updateParent != nullptr)
  146. _updateLayout(updateParent);
  147. else // Must be root panel
  148. _updateLayout(mPanel);
  149. }
  150. else
  151. {
  152. UINT32 numChildren = currentElem->_getNumChildren();
  153. for (UINT32 i = 0; i < numChildren; i++)
  154. todo.push(currentElem->_getChild(i));
  155. }
  156. }
  157. bs_frame_clear();
  158. }
  159. void GUIWidget::_updateLayout(GUIElementBase* elem)
  160. {
  161. GUIElementBase* parent = elem->_getParent();
  162. bool isPanelOptimized = parent != nullptr && parent->_getType() == GUIElementBase::Type::Panel;
  163. GUIElementBase* updateParent = nullptr;
  164. if (isPanelOptimized)
  165. updateParent = parent;
  166. else
  167. updateParent = elem;
  168. // For GUIPanel we can do a an optimization and update only the element in question instead
  169. // of all the children
  170. if (isPanelOptimized)
  171. {
  172. GUIPanel* panel = static_cast<GUIPanel*>(updateParent);
  173. GUIElementBase* dirtyElement = elem;
  174. dirtyElement->_updateOptimalLayoutSizes();
  175. LayoutSizeRange elementSizeRange = panel->_getElementSizeRange(dirtyElement);
  176. Rect2I elementArea = panel->_getElementArea(panel->_getLayoutData().area, dirtyElement, elementSizeRange);
  177. GUILayoutData childLayoutData = panel->_getLayoutData();
  178. panel->_updateDepthRange(childLayoutData);
  179. childLayoutData.area = elementArea;
  180. panel->_updateChildLayout(dirtyElement, childLayoutData);
  181. }
  182. else
  183. {
  184. GUILayoutData childLayoutData = updateParent->_getLayoutData();
  185. updateParent->_updateLayout(childLayoutData);
  186. }
  187. // Mark dirty contents
  188. bs_frame_mark();
  189. {
  190. FrameStack<GUIElementBase*> todo;
  191. todo.push(elem);
  192. while (!todo.empty())
  193. {
  194. GUIElementBase* currentElem = todo.top();
  195. todo.pop();
  196. if (currentElem->_getType() == GUIElementBase::Type::Element)
  197. mDirtyContents.insert(static_cast<GUIElement*>(currentElem));
  198. currentElem->_markAsClean();
  199. UINT32 numChildren = currentElem->_getNumChildren();
  200. for (UINT32 i = 0; i < numChildren; i++)
  201. todo.push(currentElem->_getChild(i));
  202. }
  203. }
  204. bs_frame_clear();
  205. }
  206. void GUIWidget::_registerElement(GUIElementBase* elem)
  207. {
  208. assert(elem != nullptr && !elem->_isDestroyed());
  209. if (elem->_getType() == GUIElementBase::Type::Element)
  210. {
  211. mElements.push_back(static_cast<GUIElement*>(elem));
  212. mWidgetIsDirty = true;
  213. }
  214. }
  215. void GUIWidget::_unregisterElement(GUIElementBase* elem)
  216. {
  217. assert(elem != nullptr);
  218. auto iterFind = std::find(begin(mElements), end(mElements), elem);
  219. if (iterFind != mElements.end())
  220. {
  221. mElements.erase(iterFind);
  222. mWidgetIsDirty = true;
  223. }
  224. if (elem->_getType() == GUIElementBase::Type::Element)
  225. mDirtyContents.erase(static_cast<GUIElement*>(elem));
  226. }
  227. void GUIWidget::_markMeshDirty(GUIElementBase* elem)
  228. {
  229. mWidgetIsDirty = true;
  230. }
  231. void GUIWidget::_markContentDirty(GUIElementBase* elem)
  232. {
  233. if (elem->_getType() == GUIElementBase::Type::Element)
  234. mDirtyContents.insert(static_cast<GUIElement*>(elem));
  235. }
  236. void GUIWidget::setSkin(const HGUISkin& skin)
  237. {
  238. mSkin = skin;
  239. for(auto& element : mElements)
  240. element->_refreshStyle();
  241. }
  242. const GUISkin& GUIWidget::getSkin() const
  243. {
  244. if(mSkin.isLoaded())
  245. return *mSkin;
  246. else
  247. return *BuiltinResources::instance().getEmptyGUISkin();
  248. }
  249. void GUIWidget::setCamera(const SPtr<Camera>& camera)
  250. {
  251. SPtr<Camera> newCamera = camera;
  252. if(newCamera != nullptr)
  253. {
  254. if (newCamera->getViewport()->getTarget() == nullptr)
  255. newCamera = nullptr;
  256. }
  257. if (mCamera == newCamera)
  258. return;
  259. GUIManager::instance().unregisterWidget(this);
  260. mOwnerTargetResizedConn.disconnect();
  261. mCamera = newCamera;
  262. Viewport* viewport = getTarget();
  263. if (viewport != nullptr && viewport->getTarget() != nullptr)
  264. mOwnerTargetResizedConn = viewport->getTarget()->onResized.connect(std::bind(&GUIWidget::ownerTargetResized, this));
  265. GUIManager::instance().registerWidget(this);
  266. updateRootPanel();
  267. }
  268. void GUIWidget::setIsActive(bool active)
  269. {
  270. mIsActive = active;
  271. }
  272. bool GUIWidget::isDirty(bool cleanIfDirty)
  273. {
  274. if (!mIsActive)
  275. return false;
  276. bool dirty = mWidgetIsDirty || mDirtyContents.size() > 0;
  277. if(cleanIfDirty && dirty)
  278. {
  279. mWidgetIsDirty = false;
  280. for (auto& dirtyElement : mDirtyContents)
  281. dirtyElement->_updateRenderElements();
  282. mDirtyContents.clear();
  283. updateBounds();
  284. }
  285. return dirty;
  286. }
  287. bool GUIWidget::inBounds(const Vector2I& position) const
  288. {
  289. Viewport* target = getTarget();
  290. if (target == nullptr)
  291. return false;
  292. // Technically GUI widget bounds can be larger than the viewport, so make sure we clip to viewport first
  293. if(!target->getPixelArea().contains(position))
  294. return false;
  295. Vector3 vecPos((float)position.x, (float)position.y, 0.0f);
  296. vecPos = mTransform.inverse().multiplyAffine(vecPos);
  297. Vector2I localPos(Math::roundToInt(vecPos.x), Math::roundToInt(vecPos.y));
  298. return mBounds.contains(localPos);
  299. }
  300. void GUIWidget::updateBounds() const
  301. {
  302. if(mElements.size() > 0)
  303. mBounds = mElements[0]->_getClippedBounds();
  304. for(auto& elem : mElements)
  305. {
  306. Rect2I elemBounds = elem->_getClippedBounds();
  307. mBounds.encapsulate(elemBounds);
  308. }
  309. }
  310. void GUIWidget::ownerTargetResized()
  311. {
  312. updateRootPanel();
  313. onOwnerTargetResized();
  314. }
  315. void GUIWidget::ownerWindowFocusChanged()
  316. {
  317. onOwnerWindowFocusChanged();
  318. }
  319. void GUIWidget::updateRootPanel()
  320. {
  321. Viewport* target = getTarget();
  322. if (target == nullptr)
  323. return;
  324. Rect2I area = target->getPixelArea();
  325. UINT32 width = area.width;
  326. UINT32 height = area.height;
  327. GUILayoutData layoutData;
  328. layoutData.area.width = width;
  329. layoutData.area.height = height;
  330. layoutData.clipRect = Rect2I(0, 0, width, height);
  331. layoutData.setWidgetDepth(mDepth);
  332. mPanel->setWidth(width);
  333. mPanel->setHeight(height);
  334. mPanel->_setLayoutData(layoutData);
  335. mPanel->_markLayoutAsDirty();
  336. }
  337. }