BsGUISceneTreeView.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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 "BsGUITreeViewEditBox.h"
  11. #include "BsGUIMouseEvent.h"
  12. #include "BsGUISkin.h"
  13. #include "BsGUICommandEvent.h"
  14. #include "CmSceneObject.h"
  15. #include "CmSceneManager.h"
  16. #include "BsCmdEditPlainFieldGO.h"
  17. using namespace CamelotFramework;
  18. using namespace BansheeEngine;
  19. namespace BansheeEditor
  20. {
  21. const UINT32 GUISceneTreeView::ELEMENT_EXTRA_SPACING = 3;
  22. const UINT32 GUISceneTreeView::INDENT_SIZE = 10;
  23. const UINT32 GUISceneTreeView::INITIAL_INDENT_OFFSET = 16;
  24. GUISceneTreeView::TreeElement::TreeElement()
  25. :mParent(nullptr), mFoldoutBtn(nullptr), mElement(nullptr), mIsSelected(false),
  26. mId(0), mIsExpanded(false), mSortedIdx(0), mIsDirty(false), mIsVisible(true)
  27. { }
  28. GUISceneTreeView::TreeElement::~TreeElement()
  29. {
  30. for(auto& child : mChildren)
  31. cm_delete(child);
  32. if(mFoldoutBtn != nullptr)
  33. GUIElement::destroy(mFoldoutBtn);
  34. if(mElement != nullptr)
  35. GUIElement::destroy(mElement);
  36. mChildren.clear();
  37. }
  38. GUISceneTreeView::GUISceneTreeView(GUIWidget& parent, GUIElementStyle* backgroundStyle, GUIElementStyle* elementBtnStyle,
  39. GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle, GUIElementStyle* editBoxStyle,
  40. const GUILayoutOptions& layoutOptions)
  41. :GUIElementContainer(parent, layoutOptions), mBackgroundStyle(backgroundStyle),
  42. mElementBtnStyle(elementBtnStyle), mFoldoutBtnStyle(foldoutBtnStyle), mEditBoxStyle(editBoxStyle), mEditElement(nullptr), mIsElementSelected(false),
  43. mNameEditBox(nullptr), mSelectionBackgroundStyle(selectionBackgroundStyle)
  44. {
  45. if(mBackgroundStyle == nullptr)
  46. mBackgroundStyle = parent.getSkin().getStyle("TreeViewBackground");
  47. if(mElementBtnStyle == nullptr)
  48. mElementBtnStyle = parent.getSkin().getStyle("TreeViewElementBtn");
  49. if(mFoldoutBtnStyle == nullptr)
  50. mFoldoutBtnStyle = parent.getSkin().getStyle("TreeViewFoldoutBtn");
  51. if(mSelectionBackgroundStyle == nullptr)
  52. mSelectionBackgroundStyle = parent.getSkin().getStyle("TreeViewSelectionBackground");
  53. if(mEditBoxStyle == nullptr)
  54. mEditBoxStyle = parent.getSkin().getStyle("TreeViewEditBox");
  55. mBackgroundImage = GUITexture::create(parent, mBackgroundStyle);
  56. mNameEditBox = GUITreeViewEditBox::create(parent, mEditBoxStyle);
  57. mNameEditBox->disableRecursively();
  58. mNameEditBox->onInputConfirmed.connect(boost::bind(&GUISceneTreeView::onEditAccepted, this));
  59. mNameEditBox->onInputCanceled.connect(boost::bind(&GUISceneTreeView::onEditCanceled, this));
  60. _registerChildElement(mBackgroundImage);
  61. _registerChildElement(mNameEditBox);
  62. }
  63. GUISceneTreeView::~GUISceneTreeView()
  64. {
  65. }
  66. GUISceneTreeView* GUISceneTreeView::create(GUIWidget& parent, GUIElementStyle* backgroundStyle, GUIElementStyle* elementBtnStyle,
  67. GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle, GUIElementStyle* editBoxStyle)
  68. {
  69. return new (cm_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(parent, backgroundStyle, elementBtnStyle, foldoutBtnStyle,
  70. selectionBackgroundStyle, editBoxStyle, GUILayoutOptions::create(&GUISkin::DefaultStyle));
  71. }
  72. GUISceneTreeView* GUISceneTreeView::create(GUIWidget& parent, const GUIOptions& options, GUIElementStyle* backgroundStyle,
  73. GUIElementStyle* elementBtnStyle, GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle, GUIElementStyle* editBoxStyle)
  74. {
  75. return new (cm_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(parent, backgroundStyle, elementBtnStyle,
  76. foldoutBtnStyle, selectionBackgroundStyle, editBoxStyle, GUILayoutOptions::create(options, &GUISkin::DefaultStyle));
  77. }
  78. void GUISceneTreeView::update()
  79. {
  80. // NOTE - Instead of iterating through every visible element and comparing it with internal values,
  81. // I might just want to add callbacks to SceneManager that notify me of any changes and then only perform
  82. // update if anything is actually dirty
  83. struct UpdateTreeElement
  84. {
  85. UpdateTreeElement(TreeElement* element, UINT32 seqIdx, bool visible)
  86. :element(element), seqIdx(seqIdx), visible(visible)
  87. { }
  88. TreeElement* element;
  89. UINT32 seqIdx;
  90. bool visible;
  91. };
  92. HSceneObject root = CM::gSceneManager().getRootNode();
  93. mRootElement.mSceneObject = root;
  94. mRootElement.mId = root->getId();
  95. mRootElement.mSortedIdx = 0;
  96. mRootElement.mIsExpanded = true;
  97. Stack<UpdateTreeElement>::type todo;
  98. todo.push(UpdateTreeElement(&mRootElement, 0, true));
  99. while(!todo.empty())
  100. {
  101. UpdateTreeElement updateElement = todo.top();
  102. TreeElement* current = updateElement.element;
  103. HSceneObject currentSO = current->mSceneObject;
  104. todo.pop();
  105. // Check if SceneObject has changed in any way and update the tree element
  106. if(updateElement.visible)
  107. {
  108. bool completeMatch = (UINT32)current->mChildren.size() == currentSO->getNumChildren();
  109. // Early exit case - Most commonly there will be no changes between active and cached data so
  110. // we first do a quick check in order to avoid expensive comparison later
  111. if(completeMatch)
  112. {
  113. for(UINT32 i = 0; i < currentSO->getNumChildren(); i++)
  114. {
  115. UINT32 curId = currentSO->getChild(i)->getId();
  116. if(curId != current->mChildren[i]->mId)
  117. {
  118. completeMatch = false;
  119. break;
  120. }
  121. }
  122. }
  123. // Not a complete match, compare everything and insert/delete elements as needed
  124. if(!completeMatch)
  125. {
  126. Vector<TreeElement*>::type newChildren;
  127. mTempToDelete.resize(current->mChildren.size(), true);
  128. for(UINT32 i = 0; i < currentSO->getNumChildren(); i++)
  129. {
  130. HSceneObject currentSOChild = currentSO->getChild(i);
  131. UINT32 curId = currentSOChild->getId();
  132. bool found = false;
  133. for(UINT32 j = 0; j < current->mChildren.size(); j++)
  134. {
  135. TreeElement* currentChild = current->mChildren[j];
  136. if(curId == currentChild->mId)
  137. {
  138. mTempToDelete[j] = false;
  139. currentChild->mIsDirty = true;
  140. currentChild->mSortedIdx = (UINT32)newChildren.size();
  141. newChildren.push_back(currentChild);
  142. found = true;
  143. break;
  144. }
  145. }
  146. if(!found)
  147. {
  148. TreeElement* newChild = cm_new<TreeElement>();
  149. newChild->mParent = current;
  150. newChild->mSceneObject = currentSOChild;
  151. newChild->mId = currentSOChild->getId();
  152. newChild->mName = currentSOChild->getName();
  153. newChild->mSortedIdx = (UINT32)newChildren.size();
  154. newChild->mIsDirty = true;
  155. newChildren.push_back(newChild);
  156. }
  157. }
  158. for(UINT32 i = 0; i < current->mChildren.size(); i++)
  159. {
  160. if(!mTempToDelete[i])
  161. continue;
  162. if(current->mChildren[i]->mIsSelected)
  163. unselectElement(current->mChildren[i]);
  164. cm_delete(current->mChildren[i]);
  165. }
  166. current->mChildren = newChildren;
  167. current->mIsDirty = true;
  168. }
  169. // Check if name needs updating
  170. const String& name = current->mSceneObject->getName();
  171. if(current->mName != name)
  172. {
  173. current->mName = name;
  174. current->mIsDirty = true;
  175. }
  176. // Calculate the sorted index of the element based on its name
  177. TreeElement* parent = current->mParent;
  178. if(current->mIsDirty && parent != nullptr)
  179. {
  180. for(UINT32 i = 0; i < (UINT32)parent->mChildren.size(); i++)
  181. {
  182. INT32 stringCompare = current->mName.compare(parent->mChildren[i]->mName);
  183. if(stringCompare > 0)
  184. {
  185. if(current->mSortedIdx < parent->mChildren[i]->mSortedIdx)
  186. std::swap(current->mSortedIdx, parent->mChildren[i]->mSortedIdx);
  187. }
  188. else if(stringCompare < 0)
  189. {
  190. if(current->mSortedIdx > parent->mChildren[i]->mSortedIdx)
  191. std::swap(current->mSortedIdx, parent->mChildren[i]->mSortedIdx);
  192. }
  193. }
  194. }
  195. }
  196. bool visibilityChanged = false;
  197. if(current->mIsVisible != updateElement.visible)
  198. {
  199. visibilityChanged = true;
  200. current->mIsVisible = updateElement.visible;
  201. current->mIsDirty = true;
  202. }
  203. if(current->mIsDirty && current != &mRootElement)
  204. {
  205. if(updateElement.visible)
  206. {
  207. HString name(toWString(current->mName));
  208. if(current->mElement == nullptr)
  209. {
  210. current->mElement = GUILabel::create(_getParentWidget(), name, mElementBtnStyle);
  211. _registerChildElement(current->mElement);
  212. }
  213. if(current->mChildren.size() > 0)
  214. {
  215. if(current->mFoldoutBtn == nullptr)
  216. {
  217. current->mFoldoutBtn = GUIToggle::create(_getParentWidget(), GUIContent(HString(L"")), mFoldoutBtnStyle);
  218. _registerChildElement(current->mFoldoutBtn);
  219. current->mFoldoutBtn->onToggled.connect(boost::bind(&GUISceneTreeView::elementToggled, this, current, _1));
  220. }
  221. }
  222. else
  223. {
  224. if(current->mFoldoutBtn != nullptr)
  225. {
  226. GUIElement::destroy(current->mFoldoutBtn);
  227. current->mFoldoutBtn = nullptr;
  228. }
  229. }
  230. current->mElement->setContent(GUIContent(name));
  231. }
  232. else
  233. {
  234. if(current->mElement != nullptr)
  235. {
  236. GUIElement::destroy(current->mElement);
  237. current->mElement = nullptr;
  238. }
  239. if(current->mFoldoutBtn != nullptr)
  240. {
  241. GUIElement::destroy(current->mFoldoutBtn);
  242. current->mFoldoutBtn = nullptr;
  243. }
  244. if(visibilityChanged && current->mIsSelected)
  245. unselectElement(current);
  246. }
  247. markContentAsDirty();
  248. current->mIsDirty = false;
  249. }
  250. // Queue children for next iteration
  251. if(visibilityChanged || current->mIsVisible)
  252. {
  253. for(UINT32 i = 0; i < (UINT32)current->mChildren.size(); i++)
  254. {
  255. todo.push(UpdateTreeElement(current->mChildren[i], i, current->mIsVisible && current->mIsExpanded));
  256. }
  257. }
  258. }
  259. }
  260. bool GUISceneTreeView::mouseEvent(const GUIMouseEvent& event)
  261. {
  262. if(event.getType() == GUIMouseEventType::MouseUp)
  263. {
  264. const GUISceneTreeView::InteractableElement* element = findElementUnderCoord(event.getPosition());
  265. TreeElement* treeElement = nullptr;
  266. if(element != nullptr && element->isTreeElement())
  267. {
  268. treeElement = interactableToRealElement(*element);
  269. }
  270. if(treeElement != nullptr && event.getPosition().x >= treeElement->mElement->getBounds().x)
  271. {
  272. if(event.isCtrlDown())
  273. {
  274. selectElement(treeElement);
  275. }
  276. else if(event.isShiftDown())
  277. {
  278. if(isSelectionActive())
  279. {
  280. TreeElement* selectionRoot = mSelectedElements[0].element;
  281. unselectAll();
  282. auto iterStartFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  283. [&] (const InteractableElement& x) { return x.parent == selectionRoot->mParent; } );
  284. bool foundStart = false;
  285. bool foundEnd = false;
  286. for(; iterStartFind != mVisibleElements.end(); ++iterStartFind)
  287. {
  288. if(!iterStartFind->isTreeElement())
  289. continue;
  290. TreeElement* curElem = interactableToRealElement(*iterStartFind);
  291. if(curElem == selectionRoot)
  292. {
  293. foundStart = true;
  294. break;
  295. }
  296. }
  297. auto iterEndFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  298. [&] (const InteractableElement& x) { return &x == element; } );
  299. if(iterEndFind != mVisibleElements.end())
  300. foundEnd = true;
  301. if(foundStart && foundEnd)
  302. {
  303. if(iterStartFind < iterEndFind)
  304. {
  305. for(;iterStartFind != (iterEndFind + 1); ++iterStartFind)
  306. {
  307. if(iterStartFind->isTreeElement())
  308. selectElement(interactableToRealElement(*iterStartFind));
  309. }
  310. }
  311. else if(iterEndFind < iterStartFind)
  312. {
  313. for(;iterEndFind != (iterStartFind + 1); ++iterEndFind)
  314. {
  315. if(iterEndFind->isTreeElement())
  316. selectElement(interactableToRealElement(*iterEndFind));
  317. }
  318. }
  319. else
  320. selectElement(treeElement);
  321. }
  322. if(!foundStart || !foundEnd)
  323. selectElement(treeElement);
  324. }
  325. else
  326. {
  327. selectElement(treeElement);
  328. }
  329. }
  330. else
  331. {
  332. unselectAll();
  333. selectElement(treeElement);
  334. }
  335. markContentAsDirty();
  336. return true;
  337. }
  338. }
  339. return false;
  340. }
  341. bool GUISceneTreeView::commandEvent(const GUICommandEvent& ev)
  342. {
  343. if(ev.getType() == GUICommandEventType::Rename)
  344. {
  345. if(isSelectionActive() && mEditElement == nullptr)
  346. {
  347. unselectAll();
  348. enableEdit(mSelectedElements[0].element);
  349. }
  350. return true;
  351. }
  352. return false;
  353. }
  354. bool GUISceneTreeView::isSelectionActive() const
  355. {
  356. return mIsElementSelected && mSelectedElements.size() > 0;
  357. }
  358. void GUISceneTreeView::selectElement(TreeElement* element)
  359. {
  360. auto iterFind = std::find_if(mSelectedElements.begin(), mSelectedElements.end(),
  361. [&] (const SelectedElement& x) { return x.element == element; });
  362. if(iterFind == mSelectedElements.end())
  363. {
  364. GUITexture* background = GUITexture::create(_getParentWidget(), mSelectionBackgroundStyle);
  365. _registerChildElement(background);
  366. element->mIsSelected = true;
  367. mSelectedElements.push_back(SelectedElement(element, background));
  368. mIsElementSelected = true;
  369. }
  370. }
  371. void GUISceneTreeView::unselectElement(TreeElement* element)
  372. {
  373. auto iterFind = std::find_if(mSelectedElements.begin(), mSelectedElements.end(),
  374. [&] (const SelectedElement& x) { return x.element == element; });
  375. if(iterFind != mSelectedElements.end())
  376. {
  377. iterFind->element->mIsSelected = false;
  378. GUIElement::destroy(iterFind->background);
  379. mSelectedElements.erase(iterFind);
  380. markContentAsDirty();
  381. }
  382. mIsElementSelected = mSelectedElements.size() > 0;
  383. }
  384. void GUISceneTreeView::unselectAll()
  385. {
  386. for(auto& selectedElem : mSelectedElements)
  387. {
  388. selectedElem.element->mIsSelected = false;
  389. GUIElement::destroy(selectedElem.background);
  390. }
  391. mSelectedElements.clear();
  392. mIsElementSelected = false;
  393. markContentAsDirty();
  394. }
  395. void GUISceneTreeView::elementToggled(TreeElement* element, bool toggled)
  396. {
  397. element->mIsExpanded = toggled;
  398. }
  399. void GUISceneTreeView::onEditAccepted()
  400. {
  401. disableEdit(true);
  402. }
  403. void GUISceneTreeView::onEditCanceled()
  404. {
  405. if(mEditElement != nullptr)
  406. disableEdit(false);
  407. }
  408. void GUISceneTreeView::enableEdit(TreeElement* element)
  409. {
  410. assert(mEditElement == nullptr);
  411. mEditElement = element;
  412. mNameEditBox->enableRecursively();
  413. mNameEditBox->setFocus(true);
  414. if(element->mElement != nullptr)
  415. element->mElement->disableRecursively();
  416. }
  417. void GUISceneTreeView::disableEdit(bool applyChanges)
  418. {
  419. assert(mEditElement != nullptr);
  420. if(mEditElement->mElement != nullptr)
  421. mEditElement->mElement->enableRecursively();
  422. if(applyChanges)
  423. {
  424. String newName = toString(mNameEditBox->getText());
  425. CmdEditPlainFieldGO<String>::execute(mEditElement->mSceneObject, "mName", newName);
  426. }
  427. mNameEditBox->disableRecursively();
  428. mEditElement = nullptr;
  429. }
  430. Vector2I GUISceneTreeView::_getOptimalSize() const
  431. {
  432. struct UpdateTreeElement
  433. {
  434. UpdateTreeElement(const TreeElement* element, UINT32 indent)
  435. :element(element), indent(indent)
  436. { }
  437. const TreeElement* element;
  438. UINT32 indent;
  439. };
  440. Vector2I optimalSize;
  441. if(_getLayoutOptions().fixedWidth && _getLayoutOptions().fixedHeight)
  442. {
  443. optimalSize.x = _getLayoutOptions().width;
  444. optimalSize.y = _getLayoutOptions().height;
  445. }
  446. else
  447. {
  448. Stack<UpdateTreeElement>::type todo;
  449. todo.push(UpdateTreeElement(&mRootElement, 0));
  450. while(!todo.empty())
  451. {
  452. UpdateTreeElement currentUpdateElement = todo.top();
  453. const TreeElement* current = currentUpdateElement.element;
  454. todo.pop();
  455. INT32 yOffset = 0;
  456. if(current->mElement != nullptr)
  457. {
  458. Vector2I curOptimalSize = current->mElement->_getOptimalSize();
  459. optimalSize.x = std::max(optimalSize.x,
  460. (INT32)(INITIAL_INDENT_OFFSET + curOptimalSize.x + currentUpdateElement.indent * INDENT_SIZE));
  461. yOffset = curOptimalSize.y + ELEMENT_EXTRA_SPACING;
  462. }
  463. optimalSize.y += yOffset;
  464. for(auto& child : current->mChildren)
  465. {
  466. if(!child->mIsVisible)
  467. continue;
  468. todo.push(UpdateTreeElement(child, currentUpdateElement.indent + 1));
  469. }
  470. }
  471. if(_getLayoutOptions().fixedWidth)
  472. optimalSize.x = _getLayoutOptions().width;
  473. else
  474. {
  475. if(_getLayoutOptions().minWidth > 0)
  476. optimalSize.x = std::max((INT32)_getLayoutOptions().minWidth, optimalSize.x);
  477. if(_getLayoutOptions().maxWidth > 0)
  478. optimalSize.x = std::min((INT32)_getLayoutOptions().maxWidth, optimalSize.x);
  479. }
  480. if(_getLayoutOptions().fixedHeight)
  481. optimalSize.y = _getLayoutOptions().height;
  482. else
  483. {
  484. if(_getLayoutOptions().minHeight > 0)
  485. optimalSize.y = std::max((INT32)_getLayoutOptions().minHeight, optimalSize.y);
  486. if(_getLayoutOptions().maxHeight > 0)
  487. optimalSize.y = std::min((INT32)_getLayoutOptions().maxHeight, optimalSize.y);
  488. }
  489. }
  490. return optimalSize;
  491. }
  492. void GUISceneTreeView::updateClippedBounds()
  493. {
  494. Vector2I offset = _getOffset();
  495. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  496. }
  497. void GUISceneTreeView::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  498. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  499. {
  500. struct UpdateTreeElement
  501. {
  502. UpdateTreeElement(TreeElement* element, UINT32 indent)
  503. :element(element), indent(indent)
  504. { }
  505. TreeElement* element;
  506. UINT32 indent;
  507. };
  508. mVisibleElements.clear();
  509. Stack<UpdateTreeElement>::type todo;
  510. todo.push(UpdateTreeElement(&mRootElement, 0));
  511. // NOTE - Instead of iterating through all elements, try to find those within the clip rect
  512. // and only iterate through those. Others should somehow be marked in-active (similar to GUIElement::isDisabled()?)
  513. Vector<TreeElement*>::type tempOrderedElements;
  514. Vector2I offset(x, y);
  515. while(!todo.empty())
  516. {
  517. UpdateTreeElement currentUpdateElement = todo.top();
  518. TreeElement* current = currentUpdateElement.element;
  519. UINT32 indent = currentUpdateElement.indent;
  520. todo.pop();
  521. INT32 btnHeight = 0;
  522. INT32 yOffset = 0;
  523. if(current->mElement != nullptr)
  524. {
  525. Vector2I elementSize = current->mElement->_getOptimalSize();
  526. btnHeight = elementSize.y;
  527. mVisibleElements.push_back(InteractableElement(current->mParent, current->mSortedIdx * 2 + 0, RectI(x, offset.y, width, ELEMENT_EXTRA_SPACING)));
  528. mVisibleElements.push_back(InteractableElement(current->mParent, current->mSortedIdx * 2 + 1, RectI(x, offset.y + ELEMENT_EXTRA_SPACING, width, btnHeight)));
  529. offset.x = INITIAL_INDENT_OFFSET + indent * INDENT_SIZE;
  530. offset.y += ELEMENT_EXTRA_SPACING;
  531. current->mElement->_setOffset(offset);
  532. current->mElement->_setWidth(elementSize.x);
  533. current->mElement->_setHeight(elementSize.y);
  534. current->mElement->_setAreaDepth(areaDepth);
  535. current->mElement->_setWidgetDepth(widgetDepth);
  536. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  537. current->mElement->_setClipRect(elemClipRect);
  538. yOffset = btnHeight;
  539. }
  540. if(current->mFoldoutBtn != nullptr)
  541. {
  542. Vector2I elementSize = current->mFoldoutBtn->_getOptimalSize();
  543. offset.x -= std::min((INT32)INITIAL_INDENT_OFFSET, elementSize.y);
  544. Vector2I myOffset = offset;
  545. myOffset.y -= 2; // TODO: Arbitrary offset, I should adjust it based on font baseline so that the button is nicely centered on text
  546. if(elementSize.y > btnHeight)
  547. {
  548. UINT32 diff = elementSize.y - btnHeight;
  549. float half = diff * 0.5f;
  550. myOffset.y -= Math::floorToInt(half);
  551. }
  552. current->mFoldoutBtn->_setOffset(myOffset);
  553. current->mFoldoutBtn->_setWidth(elementSize.x);
  554. current->mFoldoutBtn->_setHeight(elementSize.y);
  555. current->mFoldoutBtn->_setAreaDepth(areaDepth);
  556. current->mFoldoutBtn->_setWidgetDepth(widgetDepth);
  557. RectI elemClipRect(clipRect.x - myOffset.x, clipRect.y - myOffset.y, clipRect.width, clipRect.height);
  558. current->mFoldoutBtn->_setClipRect(elemClipRect);
  559. }
  560. offset.y += yOffset;
  561. tempOrderedElements.resize(current->mChildren.size(), nullptr);
  562. for(auto& child : current->mChildren)
  563. {
  564. tempOrderedElements[child->mSortedIdx] = child;
  565. }
  566. for(auto iter = tempOrderedElements.rbegin(); iter != tempOrderedElements.rend(); ++iter)
  567. {
  568. TreeElement* child = *iter;
  569. if(!child->mIsVisible)
  570. continue;
  571. todo.push(UpdateTreeElement(child, indent + 1));
  572. }
  573. }
  574. UINT32 remainingHeight = (UINT32)std::max(0, (INT32)height - (offset.y - y));
  575. if(remainingHeight > 0)
  576. mVisibleElements.push_back(InteractableElement(&mRootElement, (UINT32)mRootElement.mChildren.size() * 2, RectI(x, offset.y, width, remainingHeight)));
  577. for(auto selectedElem : mSelectedElements)
  578. {
  579. GUILabel* targetElement = selectedElem.element->mElement;
  580. Vector2I offset = targetElement->_getOffset();
  581. offset.x = x;
  582. selectedElem.background->_setOffset(offset);
  583. selectedElem.background->_setWidth(width);
  584. selectedElem.background->_setHeight(targetElement->_getHeight());
  585. selectedElem.background->_setAreaDepth(areaDepth + 1);
  586. selectedElem.background->_setWidgetDepth(widgetDepth);
  587. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  588. selectedElem.background->_setClipRect(elemClipRect);
  589. }
  590. if(mEditElement != nullptr)
  591. {
  592. GUILabel* targetElement = mEditElement->mElement;
  593. Vector2I offset = targetElement->_getOffset();
  594. UINT32 remainingWidth = (UINT32)std::max(0, (((INT32)width) - (offset.x - x)));
  595. mNameEditBox->_setOffset(offset);
  596. mNameEditBox->_setWidth(remainingWidth);
  597. mNameEditBox->_setHeight(targetElement->_getHeight());
  598. mNameEditBox->_setAreaDepth(areaDepth);
  599. mNameEditBox->_setWidgetDepth(widgetDepth);
  600. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  601. mNameEditBox->_setClipRect(elemClipRect);
  602. }
  603. }
  604. const GUISceneTreeView::InteractableElement* GUISceneTreeView::findElementUnderCoord(const CM::Vector2I& coord) const
  605. {
  606. for(auto& element : mVisibleElements)
  607. {
  608. if(element.bounds.contains(coord))
  609. {
  610. return &element;
  611. }
  612. }
  613. return nullptr;
  614. }
  615. GUISceneTreeView::TreeElement* GUISceneTreeView::interactableToRealElement(const GUISceneTreeView::InteractableElement& element)
  616. {
  617. if(!element.isTreeElement())
  618. return nullptr;
  619. UINT32 sortedIdx = (element.index - 1) / 2;
  620. auto findIter = std::find_if(element.parent->mChildren.begin(), element.parent->mChildren.end(),
  621. [&](const TreeElement* x) { return x->mSortedIdx == sortedIdx; });
  622. if(findIter != element.parent->mChildren.end())
  623. return *findIter;
  624. return nullptr;
  625. }
  626. const String& GUISceneTreeView::getGUITypeName()
  627. {
  628. static String typeName = "SceneTreeView";
  629. return typeName;
  630. }
  631. }