BsGUISceneTreeView.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. if(current->mElement == nullptr)
  192. {
  193. HString name(toWString(current->mName));
  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. }
  213. else
  214. {
  215. if(current->mElement != nullptr)
  216. {
  217. GUIElement::destroy(current->mElement);
  218. current->mElement = nullptr;
  219. }
  220. if(current->mFoldoutBtn != nullptr)
  221. {
  222. GUIElement::destroy(current->mFoldoutBtn);
  223. current->mFoldoutBtn = nullptr;
  224. }
  225. }
  226. markContentAsDirty();
  227. current->mIsDirty = false;
  228. }
  229. // Queue children for next iteration
  230. if(visibilityChanged || current->mIsVisible)
  231. {
  232. for(UINT32 i = 0; i < (UINT32)current->mChildren.size(); i++)
  233. {
  234. todo.push(UpdateTreeElement(current->mChildren[i], i, current->mIsVisible && current->mIsExpanded));
  235. }
  236. }
  237. }
  238. }
  239. bool GUISceneTreeView::mouseEvent(const GUIMouseEvent& event)
  240. {
  241. return false;
  242. }
  243. void GUISceneTreeView::elementToggled(TreeElement* element, bool toggled)
  244. {
  245. element->mIsExpanded = toggled;
  246. }
  247. Vector2I GUISceneTreeView::_getOptimalSize() const
  248. {
  249. struct UpdateTreeElement
  250. {
  251. UpdateTreeElement(const TreeElement* element, UINT32 indent)
  252. :element(element), indent(indent)
  253. { }
  254. const TreeElement* element;
  255. UINT32 indent;
  256. };
  257. Vector2I optimalSize;
  258. if(_getLayoutOptions().fixedWidth && _getLayoutOptions().fixedHeight)
  259. {
  260. optimalSize.x = _getLayoutOptions().width;
  261. optimalSize.y = _getLayoutOptions().height;
  262. }
  263. else
  264. {
  265. Stack<UpdateTreeElement>::type todo;
  266. todo.push(UpdateTreeElement(&mRootElement, 0));
  267. while(!todo.empty())
  268. {
  269. UpdateTreeElement currentUpdateElement = todo.top();
  270. const TreeElement* current = currentUpdateElement.element;
  271. todo.pop();
  272. if(current->mElement != nullptr)
  273. {
  274. Vector2I curOptimalSize = current->mElement->_getOptimalSize();
  275. optimalSize.x = std::max(optimalSize.x,
  276. (INT32)(INITIAL_INDENT_OFFSET + curOptimalSize.x + currentUpdateElement.indent * INDENT_SIZE));
  277. optimalSize.y += curOptimalSize.y + ELEMENT_EXTRA_SPACING;
  278. }
  279. for(auto& child : current->mChildren)
  280. {
  281. todo.push(UpdateTreeElement(child, currentUpdateElement.indent + 1));
  282. }
  283. }
  284. if(_getLayoutOptions().fixedWidth)
  285. optimalSize.x = _getLayoutOptions().width;
  286. else
  287. {
  288. if(_getLayoutOptions().minWidth > 0)
  289. optimalSize.x = std::max((INT32)_getLayoutOptions().minWidth, optimalSize.x);
  290. if(_getLayoutOptions().maxWidth > 0)
  291. optimalSize.x = std::min((INT32)_getLayoutOptions().maxWidth, optimalSize.x);
  292. }
  293. if(_getLayoutOptions().fixedHeight)
  294. optimalSize.y = _getLayoutOptions().height;
  295. else
  296. {
  297. if(_getLayoutOptions().minHeight > 0)
  298. optimalSize.y = std::max((INT32)_getLayoutOptions().minHeight, optimalSize.y);
  299. if(_getLayoutOptions().maxHeight > 0)
  300. optimalSize.y = std::min((INT32)_getLayoutOptions().maxHeight, optimalSize.y);
  301. }
  302. }
  303. return optimalSize;
  304. }
  305. void GUISceneTreeView::updateClippedBounds()
  306. {
  307. Vector2I offset = _getOffset();
  308. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  309. }
  310. void GUISceneTreeView::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  311. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  312. {
  313. struct UpdateTreeElement
  314. {
  315. UpdateTreeElement(TreeElement* element, UINT32 indent)
  316. :element(element), indent(indent)
  317. { }
  318. TreeElement* element;
  319. UINT32 indent;
  320. };
  321. Stack<UpdateTreeElement>::type todo;
  322. todo.push(UpdateTreeElement(&mRootElement, 0));
  323. // NOTE - Instead of iterating through all elements, try to find those within the clip rect
  324. // and only iterate through those. Others should somehow be marked in-active (similar to GUIElement::isDisabled()?)
  325. Vector<TreeElement*>::type tempOrderedElements;
  326. Vector2I offset(x, y);
  327. while(!todo.empty())
  328. {
  329. UpdateTreeElement currentUpdateElement = todo.top();
  330. TreeElement* current = currentUpdateElement.element;
  331. UINT32 indent = currentUpdateElement.indent;
  332. todo.pop();
  333. tempOrderedElements.resize(current->mChildren.size(), nullptr);
  334. for(auto& child : current->mChildren)
  335. {
  336. tempOrderedElements[child->mSortedIdx] = child;
  337. }
  338. for(auto& child : tempOrderedElements)
  339. {
  340. if(!child->mIsVisible)
  341. continue;
  342. INT32 yOffset = 0;
  343. if(child->mElement != nullptr)
  344. {
  345. Vector2I elementSize = child->mElement->_getOptimalSize();
  346. offset.x = INITIAL_INDENT_OFFSET + indent * INDENT_SIZE;
  347. child->mElement->_setOffset(offset);
  348. child->mElement->_setWidth(elementSize.x);
  349. child->mElement->_setHeight(elementSize.y);
  350. child->mElement->_setAreaDepth(areaDepth);
  351. child->mElement->_setWidgetDepth(widgetDepth);
  352. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  353. child->mElement->_setClipRect(elemClipRect);
  354. yOffset = elementSize.y + ELEMENT_EXTRA_SPACING;
  355. }
  356. if(child->mFoldoutBtn != nullptr)
  357. {
  358. Vector2I elementSize = child->mFoldoutBtn->_getOptimalSize();
  359. offset.x -= std::min((INT32)INITIAL_INDENT_OFFSET, elementSize.y);
  360. child->mFoldoutBtn->_setOffset(offset);
  361. child->mFoldoutBtn->_setWidth(elementSize.x);
  362. child->mFoldoutBtn->_setHeight(elementSize.y);
  363. child->mFoldoutBtn->_setAreaDepth(areaDepth);
  364. child->mFoldoutBtn->_setWidgetDepth(widgetDepth);
  365. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  366. child->mFoldoutBtn->_setClipRect(elemClipRect);
  367. }
  368. offset.y += yOffset;
  369. todo.push(UpdateTreeElement(child, indent + 1));
  370. }
  371. }
  372. }
  373. const String& GUISceneTreeView::getGUITypeName()
  374. {
  375. static String typeName = "SceneTreeView";
  376. return typeName;
  377. }
  378. }