BsGUISceneTreeView.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. #include "BsGUISceneTreeView.h"
  2. #include "BsGUIArea.h"
  3. #include "BsGUILayout.h"
  4. #include "BsGUITexture.h"
  5. #include "BsGUIButton.h"
  6. #include "BsGUILabel.h"
  7. #include "BsGUISpace.h"
  8. #include "BsGUIWidget.h"
  9. #include "BsGUIToggle.h"
  10. #include "BsGUIMouseEvent.h"
  11. #include "BsGUISkin.h"
  12. #include "CmSceneObject.h"
  13. #include "CmSceneManager.h"
  14. using namespace CamelotFramework;
  15. using namespace BansheeEngine;
  16. namespace BansheeEditor
  17. {
  18. const UINT32 GUISceneTreeView::ELEMENT_EXTRA_SPACING = 3;
  19. const UINT32 GUISceneTreeView::INDENT_SIZE = 10;
  20. const UINT32 GUISceneTreeView::INITIAL_INDENT_OFFSET = 16;
  21. GUISceneTreeView::TreeElement::TreeElement()
  22. :mParent(nullptr), mFoldoutBtn(nullptr), mElement(nullptr),
  23. mId(0), mIsExpanded(false), mSortedIdx(0), mIsDirty(false), mIsVisible(true)
  24. { }
  25. GUISceneTreeView::TreeElement::~TreeElement()
  26. {
  27. for(auto& child : mChildren)
  28. cm_delete(child);
  29. if(mFoldoutBtn != nullptr)
  30. GUIElement::destroy(mFoldoutBtn);
  31. if(mElement != nullptr)
  32. GUIElement::destroy(mElement);
  33. mChildren.clear();
  34. }
  35. GUISceneTreeView::GUISceneTreeView(GUIWidget& parent, GUIElementStyle* backgroundStyle, GUIElementStyle* elementBtnStyle,
  36. GUIElementStyle* foldoutBtnStyle, const BS::GUILayoutOptions& layoutOptions)
  37. :GUIElementContainer(parent, layoutOptions), mBackgroundStyle(backgroundStyle),
  38. mElementBtnStyle(elementBtnStyle), mFoldoutBtnStyle(foldoutBtnStyle)
  39. {
  40. if(mBackgroundStyle == nullptr)
  41. mBackgroundStyle = parent.getSkin().getStyle("TreeViewBackground");
  42. if(mElementBtnStyle == nullptr)
  43. mElementBtnStyle = parent.getSkin().getStyle("TreeViewElementBtn");
  44. if(mFoldoutBtnStyle == nullptr)
  45. mFoldoutBtnStyle = parent.getSkin().getStyle("TreeViewFoldoutBtn");
  46. mBackgroundImage = GUITexture::create(parent, mBackgroundStyle);
  47. _registerChildElement(mBackgroundImage);
  48. }
  49. GUISceneTreeView::~GUISceneTreeView()
  50. {
  51. }
  52. GUISceneTreeView* GUISceneTreeView::create(GUIWidget& parent, GUIElementStyle* backgroundStyle, GUIElementStyle* elementBtnStyle,
  53. GUIElementStyle* foldoutBtnStyle)
  54. {
  55. return new (cm_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(parent, backgroundStyle, elementBtnStyle, foldoutBtnStyle,
  56. GUILayoutOptions::create(&GUISkin::DefaultStyle));
  57. }
  58. GUISceneTreeView* GUISceneTreeView::create(GUIWidget& parent, const GUIOptions& options, GUIElementStyle* backgroundStyle,
  59. GUIElementStyle* elementBtnStyle, GUIElementStyle* foldoutBtnStyle)
  60. {
  61. return new (cm_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(parent, backgroundStyle, elementBtnStyle,
  62. foldoutBtnStyle, GUILayoutOptions::create(options, &GUISkin::DefaultStyle));
  63. }
  64. void GUISceneTreeView::update()
  65. {
  66. // NOTE - Instead of iterating through every visible element and comparing it with internal values,
  67. // I might just want to add callbacks to SceneManager that notify me of any changes and then only perform
  68. // update if anything is actually dirty
  69. struct UpdateTreeElement
  70. {
  71. UpdateTreeElement(TreeElement* element, UINT32 seqIdx, bool visible)
  72. :element(element), seqIdx(seqIdx), visible(visible)
  73. { }
  74. TreeElement* element;
  75. UINT32 seqIdx;
  76. bool visible;
  77. };
  78. HSceneObject root = CM::gSceneManager().getRootNode();
  79. mRootElement.mSceneObject = root;
  80. mRootElement.mId = root->getId();
  81. mRootElement.mSortedIdx = 0;
  82. mRootElement.mIsExpanded = true;
  83. Stack<UpdateTreeElement>::type todo;
  84. todo.push(UpdateTreeElement(&mRootElement, 0, true));
  85. while(!todo.empty())
  86. {
  87. UpdateTreeElement updateElement = todo.top();
  88. TreeElement* current = updateElement.element;
  89. HSceneObject currentSO = current->mSceneObject;
  90. todo.pop();
  91. // Check if SceneObject has changed in any way and update the tree element
  92. if(updateElement.visible)
  93. {
  94. bool completeMatch = (UINT32)current->mChildren.size() == currentSO->getNumChildren();
  95. // Early exit case - Most commonly there will be no changes between active and cached data so
  96. // we first do a quick check in order to avoid expensive comparison later
  97. if(completeMatch)
  98. {
  99. for(UINT32 i = 0; i < currentSO->getNumChildren(); i++)
  100. {
  101. UINT32 curId = currentSO->getChild(i)->getId();
  102. if(curId != current->mChildren[i]->mId)
  103. {
  104. completeMatch = false;
  105. break;
  106. }
  107. }
  108. }
  109. // Not a complete match, compare everything and insert/delete elements as needed
  110. if(!completeMatch)
  111. {
  112. Vector<TreeElement*>::type newChildren;
  113. mTempToDelete.resize(current->mChildren.size(), true);
  114. for(UINT32 i = 0; i < currentSO->getNumChildren(); i++)
  115. {
  116. HSceneObject currentSOChild = currentSO->getChild(i);
  117. UINT32 curId = currentSOChild->getId();
  118. bool found = false;
  119. for(UINT32 j = 0; j < current->mChildren.size(); j++)
  120. {
  121. TreeElement* currentChild = current->mChildren[j];
  122. if(curId == currentChild->mId)
  123. {
  124. mTempToDelete[j] = false;
  125. currentChild->mIsDirty = true;
  126. currentChild->mSortedIdx = (UINT32)newChildren.size();
  127. newChildren.push_back(currentChild);
  128. found = true;
  129. break;
  130. }
  131. }
  132. if(!found)
  133. {
  134. TreeElement* newChild = cm_new<TreeElement>();
  135. newChild->mParent = current;
  136. newChild->mSceneObject = currentSOChild;
  137. newChild->mId = currentSOChild->getId();
  138. newChild->mName = currentSOChild->getName();
  139. newChild->mSortedIdx = (UINT32)newChildren.size();
  140. newChild->mIsDirty = true;
  141. newChildren.push_back(newChild);
  142. }
  143. }
  144. for(UINT32 i = 0; i < current->mChildren.size(); i++)
  145. {
  146. if(!mTempToDelete[i])
  147. continue;
  148. cm_delete(current->mChildren[i]);
  149. }
  150. current->mChildren = newChildren;
  151. current->mIsDirty = true;
  152. }
  153. // Check if name needs updating
  154. const String& name = current->mSceneObject->getName();
  155. if(current->mName != name)
  156. {
  157. current->mName = name;
  158. current->mIsDirty = true;
  159. }
  160. // Calculate the sorted index of the element based on its name
  161. TreeElement* parent = current->mParent;
  162. if(current->mIsDirty && parent != nullptr)
  163. {
  164. for(UINT32 i = 0; i < (UINT32)parent->mChildren.size(); i++)
  165. {
  166. INT32 stringCompare = current->mName.compare(parent->mChildren[i]->mName);
  167. if(stringCompare > 0)
  168. {
  169. if(current->mSortedIdx < parent->mChildren[i]->mSortedIdx)
  170. std::swap(current->mSortedIdx, parent->mChildren[i]->mSortedIdx);
  171. }
  172. else if(stringCompare < 0)
  173. {
  174. if(current->mSortedIdx > parent->mChildren[i]->mSortedIdx)
  175. std::swap(current->mSortedIdx, parent->mChildren[i]->mSortedIdx);
  176. }
  177. }
  178. }
  179. }
  180. bool visibilityChanged = false;
  181. if(current->mIsVisible != updateElement.visible)
  182. {
  183. visibilityChanged = true;
  184. current->mIsVisible = updateElement.visible;
  185. current->mIsDirty = true;
  186. }
  187. if(current->mIsDirty && current != &mRootElement)
  188. {
  189. if(updateElement.visible)
  190. {
  191. HString name(toWString(current->mName));
  192. if(current->mElement == nullptr)
  193. {
  194. current->mElement = GUILabel::create(_getParentWidget(), name, mElementBtnStyle);
  195. }
  196. if(current->mChildren.size() > 0)
  197. {
  198. if(current->mFoldoutBtn == nullptr)
  199. {
  200. current->mFoldoutBtn = GUIToggle::create(_getParentWidget(), GUIContent(HString(L"")), mFoldoutBtnStyle);
  201. current->mFoldoutBtn->onToggled.connect(boost::bind(&GUISceneTreeView::elementToggled, this, current, _1));
  202. }
  203. }
  204. else
  205. {
  206. if(current->mFoldoutBtn != nullptr)
  207. {
  208. GUIElement::destroy(current->mFoldoutBtn);
  209. current->mFoldoutBtn = nullptr;
  210. }
  211. }
  212. current->mElement->setContent(GUIContent(name));
  213. }
  214. else
  215. {
  216. if(current->mElement != nullptr)
  217. {
  218. GUIElement::destroy(current->mElement);
  219. current->mElement = nullptr;
  220. }
  221. if(current->mFoldoutBtn != nullptr)
  222. {
  223. GUIElement::destroy(current->mFoldoutBtn);
  224. current->mFoldoutBtn = nullptr;
  225. }
  226. }
  227. markContentAsDirty();
  228. current->mIsDirty = false;
  229. }
  230. // Queue children for next iteration
  231. if(visibilityChanged || current->mIsVisible)
  232. {
  233. for(UINT32 i = 0; i < (UINT32)current->mChildren.size(); i++)
  234. {
  235. todo.push(UpdateTreeElement(current->mChildren[i], i, current->mIsVisible && current->mIsExpanded));
  236. }
  237. }
  238. }
  239. }
  240. bool GUISceneTreeView::mouseEvent(const GUIMouseEvent& event)
  241. {
  242. return false;
  243. }
  244. void GUISceneTreeView::elementToggled(TreeElement* element, bool toggled)
  245. {
  246. element->mIsExpanded = toggled;
  247. }
  248. Vector2I GUISceneTreeView::_getOptimalSize() const
  249. {
  250. struct UpdateTreeElement
  251. {
  252. UpdateTreeElement(const TreeElement* element, UINT32 indent)
  253. :element(element), indent(indent)
  254. { }
  255. const TreeElement* element;
  256. UINT32 indent;
  257. };
  258. Vector2I optimalSize;
  259. if(_getLayoutOptions().fixedWidth && _getLayoutOptions().fixedHeight)
  260. {
  261. optimalSize.x = _getLayoutOptions().width;
  262. optimalSize.y = _getLayoutOptions().height;
  263. }
  264. else
  265. {
  266. Stack<UpdateTreeElement>::type todo;
  267. todo.push(UpdateTreeElement(&mRootElement, 0));
  268. while(!todo.empty())
  269. {
  270. UpdateTreeElement currentUpdateElement = todo.top();
  271. const TreeElement* current = currentUpdateElement.element;
  272. todo.pop();
  273. INT32 yOffset = 0;
  274. if(current->mElement != nullptr)
  275. {
  276. Vector2I curOptimalSize = current->mElement->_getOptimalSize();
  277. optimalSize.x = std::max(optimalSize.x,
  278. (INT32)(INITIAL_INDENT_OFFSET + curOptimalSize.x + currentUpdateElement.indent * INDENT_SIZE));
  279. yOffset = curOptimalSize.y + ELEMENT_EXTRA_SPACING;
  280. }
  281. optimalSize.y += yOffset;
  282. for(auto& child : current->mChildren)
  283. {
  284. if(!child->mIsVisible)
  285. continue;
  286. todo.push(UpdateTreeElement(child, currentUpdateElement.indent + 1));
  287. }
  288. }
  289. if(_getLayoutOptions().fixedWidth)
  290. optimalSize.x = _getLayoutOptions().width;
  291. else
  292. {
  293. if(_getLayoutOptions().minWidth > 0)
  294. optimalSize.x = std::max((INT32)_getLayoutOptions().minWidth, optimalSize.x);
  295. if(_getLayoutOptions().maxWidth > 0)
  296. optimalSize.x = std::min((INT32)_getLayoutOptions().maxWidth, optimalSize.x);
  297. }
  298. if(_getLayoutOptions().fixedHeight)
  299. optimalSize.y = _getLayoutOptions().height;
  300. else
  301. {
  302. if(_getLayoutOptions().minHeight > 0)
  303. optimalSize.y = std::max((INT32)_getLayoutOptions().minHeight, optimalSize.y);
  304. if(_getLayoutOptions().maxHeight > 0)
  305. optimalSize.y = std::min((INT32)_getLayoutOptions().maxHeight, optimalSize.y);
  306. }
  307. }
  308. return optimalSize;
  309. }
  310. void GUISceneTreeView::updateClippedBounds()
  311. {
  312. Vector2I offset = _getOffset();
  313. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  314. }
  315. void GUISceneTreeView::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  316. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  317. {
  318. struct UpdateTreeElement
  319. {
  320. UpdateTreeElement(TreeElement* element, UINT32 indent)
  321. :element(element), indent(indent)
  322. { }
  323. TreeElement* element;
  324. UINT32 indent;
  325. };
  326. Stack<UpdateTreeElement>::type todo;
  327. todo.push(UpdateTreeElement(&mRootElement, 0));
  328. // NOTE - Instead of iterating through all elements, try to find those within the clip rect
  329. // and only iterate through those. Others should somehow be marked in-active (similar to GUIElement::isDisabled()?)
  330. Vector<TreeElement*>::type tempOrderedElements;
  331. Vector2I offset(x, y);
  332. while(!todo.empty())
  333. {
  334. UpdateTreeElement currentUpdateElement = todo.top();
  335. TreeElement* current = currentUpdateElement.element;
  336. UINT32 indent = currentUpdateElement.indent;
  337. todo.pop();
  338. INT32 btnHeight = 0;
  339. INT32 yOffset = 0;
  340. if(current->mElement != nullptr)
  341. {
  342. Vector2I elementSize = current->mElement->_getOptimalSize();
  343. btnHeight = elementSize.y;
  344. offset.x = INITIAL_INDENT_OFFSET + indent * INDENT_SIZE;
  345. current->mElement->_setOffset(offset);
  346. current->mElement->_setWidth(elementSize.x);
  347. current->mElement->_setHeight(elementSize.y);
  348. current->mElement->_setAreaDepth(areaDepth);
  349. current->mElement->_setWidgetDepth(widgetDepth);
  350. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  351. current->mElement->_setClipRect(elemClipRect);
  352. yOffset = btnHeight + ELEMENT_EXTRA_SPACING;
  353. }
  354. if(current->mFoldoutBtn != nullptr)
  355. {
  356. Vector2I elementSize = current->mFoldoutBtn->_getOptimalSize();
  357. offset.x -= std::min((INT32)INITIAL_INDENT_OFFSET, elementSize.y);
  358. Vector2I myOffset = offset;
  359. if(elementSize.y > btnHeight)
  360. {
  361. UINT32 diff = elementSize.y - btnHeight;
  362. float half = diff * 0.5f;
  363. myOffset.y -= Math::floorToInt(half);
  364. }
  365. current->mFoldoutBtn->_setOffset(myOffset);
  366. current->mFoldoutBtn->_setWidth(elementSize.x);
  367. current->mFoldoutBtn->_setHeight(elementSize.y);
  368. current->mFoldoutBtn->_setAreaDepth(areaDepth);
  369. current->mFoldoutBtn->_setWidgetDepth(widgetDepth);
  370. RectI elemClipRect(clipRect.x - myOffset.x, clipRect.y - myOffset.y, clipRect.width, clipRect.height);
  371. current->mFoldoutBtn->_setClipRect(elemClipRect);
  372. }
  373. offset.y += yOffset;
  374. tempOrderedElements.resize(current->mChildren.size(), nullptr);
  375. for(auto& child : current->mChildren)
  376. {
  377. tempOrderedElements[child->mSortedIdx] = child;
  378. }
  379. for(auto iter = tempOrderedElements.rbegin(); iter != tempOrderedElements.rend(); ++iter)
  380. {
  381. TreeElement* child = *iter;
  382. if(!child->mIsVisible)
  383. continue;
  384. todo.push(UpdateTreeElement(child, indent + 1));
  385. }
  386. }
  387. }
  388. const String& GUISceneTreeView::getGUITypeName()
  389. {
  390. static String typeName = "SceneTreeView";
  391. return typeName;
  392. }
  393. }