BsGUISceneTreeView.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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->mIsSelected)
  163. unselectElement(current);
  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(current->mIsVisible && 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. if(element != nullptr && element->isTreeElement())
  266. {
  267. if(event.isCtrlDown())
  268. {
  269. selectElement(interactableToRealElement(*element));
  270. }
  271. else if(event.isShiftDown())
  272. {
  273. if(isSelectionActive())
  274. {
  275. TreeElement* selectionRoot = mSelectedElements[0].element;
  276. unselectAll();
  277. auto iterStartFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  278. [&] (const InteractableElement& x) { return x.parent == selectionRoot->mParent; } );
  279. bool foundStart = false;
  280. bool foundEnd = false;
  281. for(; iterStartFind != mVisibleElements.end(); ++iterStartFind)
  282. {
  283. if(!iterStartFind->isTreeElement())
  284. continue;
  285. TreeElement* curElem = interactableToRealElement(*iterStartFind);
  286. if(curElem == selectionRoot)
  287. {
  288. foundStart = true;
  289. break;
  290. }
  291. }
  292. auto iterEndFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  293. [&] (const InteractableElement& x) { return &x == element; } );
  294. if(iterEndFind != mVisibleElements.end())
  295. foundEnd = true;
  296. if(foundStart && foundEnd)
  297. {
  298. if(iterStartFind < iterEndFind)
  299. {
  300. for(;iterStartFind != (iterEndFind + 1); ++iterStartFind)
  301. {
  302. if(iterStartFind->isTreeElement())
  303. selectElement(interactableToRealElement(*element));
  304. }
  305. }
  306. else if(iterEndFind < iterStartFind)
  307. {
  308. for(;iterEndFind != (iterStartFind + 1); ++iterEndFind)
  309. {
  310. if(iterEndFind->isTreeElement())
  311. selectElement(interactableToRealElement(*element));
  312. }
  313. }
  314. else
  315. selectElement(interactableToRealElement(*element));
  316. }
  317. if(!foundStart || !foundEnd)
  318. selectElement(interactableToRealElement(*element));
  319. }
  320. else
  321. {
  322. selectElement(interactableToRealElement(*element));
  323. }
  324. }
  325. else
  326. {
  327. unselectAll();
  328. selectElement(interactableToRealElement(*element));
  329. }
  330. }
  331. markContentAsDirty();
  332. return true;
  333. }
  334. return false;
  335. }
  336. bool GUISceneTreeView::commandEvent(const GUICommandEvent& ev)
  337. {
  338. if(ev.getType() == GUICommandEventType::Rename)
  339. {
  340. if(isSelectionActive() && mEditElement == nullptr)
  341. {
  342. unselectAll();
  343. enableEdit(mSelectedElements[0].element);
  344. }
  345. return true;
  346. }
  347. return false;
  348. }
  349. bool GUISceneTreeView::isSelectionActive() const
  350. {
  351. return mIsElementSelected && mSelectedElements.size() > 0;
  352. }
  353. void GUISceneTreeView::selectElement(TreeElement* element)
  354. {
  355. auto iterFind = std::find_if(mSelectedElements.begin(), mSelectedElements.end(),
  356. [&] (const SelectedElement& x) { return x.element == element; });
  357. if(iterFind == mSelectedElements.end())
  358. {
  359. GUITexture* background = GUITexture::create(_getParentWidget(), mSelectionBackgroundStyle);
  360. element->mIsSelected = true;
  361. mSelectedElements.push_back(SelectedElement(element, background));
  362. mIsElementSelected = true;
  363. }
  364. }
  365. void GUISceneTreeView::unselectElement(TreeElement* element)
  366. {
  367. auto iterFind = std::find_if(mSelectedElements.begin(), mSelectedElements.end(),
  368. [&] (const SelectedElement& x) { return x.element == element; });
  369. if(iterFind != mSelectedElements.end())
  370. {
  371. iterFind->element->mIsSelected = false;
  372. GUIElement::destroy(iterFind->background);
  373. mSelectedElements.erase(iterFind);
  374. markContentAsDirty();
  375. }
  376. mIsElementSelected = mSelectedElements.size() > 0;
  377. }
  378. void GUISceneTreeView::unselectAll()
  379. {
  380. for(auto& selectedElem : mSelectedElements)
  381. {
  382. selectedElem.element->mIsSelected = false;
  383. GUIElement::destroy(selectedElem.background);
  384. }
  385. mSelectedElements.clear();
  386. mIsElementSelected = false;
  387. markContentAsDirty();
  388. }
  389. void GUISceneTreeView::elementToggled(TreeElement* element, bool toggled)
  390. {
  391. element->mIsExpanded = toggled;
  392. }
  393. void GUISceneTreeView::onEditAccepted()
  394. {
  395. disableEdit(true);
  396. }
  397. void GUISceneTreeView::onEditCanceled()
  398. {
  399. if(mEditElement != nullptr)
  400. disableEdit(false);
  401. }
  402. void GUISceneTreeView::enableEdit(TreeElement* element)
  403. {
  404. assert(mEditElement == nullptr);
  405. mEditElement = element;
  406. mNameEditBox->enableRecursively();
  407. mNameEditBox->setFocus(true);
  408. if(element->mElement != nullptr)
  409. element->mElement->disableRecursively();
  410. }
  411. void GUISceneTreeView::disableEdit(bool applyChanges)
  412. {
  413. assert(mEditElement != nullptr);
  414. if(mEditElement->mElement != nullptr)
  415. mEditElement->mElement->enableRecursively();
  416. if(applyChanges)
  417. {
  418. String newName = toString(mNameEditBox->getText());
  419. CmdEditPlainFieldGO<String>::execute(mEditElement->mSceneObject, "mName", newName);
  420. }
  421. mNameEditBox->disableRecursively();
  422. mEditElement = nullptr;
  423. }
  424. Vector2I GUISceneTreeView::_getOptimalSize() const
  425. {
  426. struct UpdateTreeElement
  427. {
  428. UpdateTreeElement(const TreeElement* element, UINT32 indent)
  429. :element(element), indent(indent)
  430. { }
  431. const TreeElement* element;
  432. UINT32 indent;
  433. };
  434. Vector2I optimalSize;
  435. if(_getLayoutOptions().fixedWidth && _getLayoutOptions().fixedHeight)
  436. {
  437. optimalSize.x = _getLayoutOptions().width;
  438. optimalSize.y = _getLayoutOptions().height;
  439. }
  440. else
  441. {
  442. Stack<UpdateTreeElement>::type todo;
  443. todo.push(UpdateTreeElement(&mRootElement, 0));
  444. optimalSize.y += ELEMENT_EXTRA_SPACING;
  445. while(!todo.empty())
  446. {
  447. UpdateTreeElement currentUpdateElement = todo.top();
  448. const TreeElement* current = currentUpdateElement.element;
  449. todo.pop();
  450. INT32 yOffset = 0;
  451. if(current->mElement != nullptr)
  452. {
  453. Vector2I curOptimalSize = current->mElement->_getOptimalSize();
  454. optimalSize.x = std::max(optimalSize.x,
  455. (INT32)(INITIAL_INDENT_OFFSET + curOptimalSize.x + currentUpdateElement.indent * INDENT_SIZE));
  456. yOffset = curOptimalSize.y + ELEMENT_EXTRA_SPACING;
  457. }
  458. optimalSize.y += yOffset;
  459. for(auto& child : current->mChildren)
  460. {
  461. if(!child->mIsVisible)
  462. continue;
  463. todo.push(UpdateTreeElement(child, currentUpdateElement.indent + 1));
  464. }
  465. }
  466. if(_getLayoutOptions().fixedWidth)
  467. optimalSize.x = _getLayoutOptions().width;
  468. else
  469. {
  470. if(_getLayoutOptions().minWidth > 0)
  471. optimalSize.x = std::max((INT32)_getLayoutOptions().minWidth, optimalSize.x);
  472. if(_getLayoutOptions().maxWidth > 0)
  473. optimalSize.x = std::min((INT32)_getLayoutOptions().maxWidth, optimalSize.x);
  474. }
  475. if(_getLayoutOptions().fixedHeight)
  476. optimalSize.y = _getLayoutOptions().height;
  477. else
  478. {
  479. if(_getLayoutOptions().minHeight > 0)
  480. optimalSize.y = std::max((INT32)_getLayoutOptions().minHeight, optimalSize.y);
  481. if(_getLayoutOptions().maxHeight > 0)
  482. optimalSize.y = std::min((INT32)_getLayoutOptions().maxHeight, optimalSize.y);
  483. }
  484. }
  485. return optimalSize;
  486. }
  487. void GUISceneTreeView::updateClippedBounds()
  488. {
  489. Vector2I offset = _getOffset();
  490. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  491. }
  492. void GUISceneTreeView::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  493. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  494. {
  495. struct UpdateTreeElement
  496. {
  497. UpdateTreeElement(TreeElement* element, UINT32 indent)
  498. :element(element), indent(indent)
  499. { }
  500. TreeElement* element;
  501. UINT32 indent;
  502. };
  503. mVisibleElements.clear();
  504. Stack<UpdateTreeElement>::type todo;
  505. todo.push(UpdateTreeElement(&mRootElement, 0));
  506. // NOTE - Instead of iterating through all elements, try to find those within the clip rect
  507. // and only iterate through those. Others should somehow be marked in-active (similar to GUIElement::isDisabled()?)
  508. Vector<TreeElement*>::type tempOrderedElements;
  509. Vector2I offset(x, y);
  510. while(!todo.empty())
  511. {
  512. UpdateTreeElement currentUpdateElement = todo.top();
  513. TreeElement* current = currentUpdateElement.element;
  514. UINT32 indent = currentUpdateElement.indent;
  515. todo.pop();
  516. if(current->mParent != nullptr && current->mSortedIdx == 0)
  517. {
  518. mVisibleElements.push_back(InteractableElement(current->mParent, 0, RectI(x, offset.y, width, ELEMENT_EXTRA_SPACING)));
  519. }
  520. INT32 btnHeight = 0;
  521. INT32 yOffset = 0;
  522. if(current->mElement != nullptr)
  523. {
  524. Vector2I elementSize = current->mElement->_getOptimalSize();
  525. btnHeight = elementSize.y;
  526. offset.x = INITIAL_INDENT_OFFSET + indent * INDENT_SIZE;
  527. current->mElement->_setOffset(offset);
  528. current->mElement->_setWidth(elementSize.x);
  529. current->mElement->_setHeight(elementSize.y);
  530. current->mElement->_setAreaDepth(areaDepth);
  531. current->mElement->_setWidgetDepth(widgetDepth);
  532. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  533. current->mElement->_setClipRect(elemClipRect);
  534. mVisibleElements.push_back(InteractableElement(current->mParent, current->mSortedIdx * 2 + 1, RectI(x, offset.y, width, btnHeight)));
  535. mVisibleElements.push_back(InteractableElement(current->mParent, current->mSortedIdx * 2 + 2, RectI(x, offset.y + btnHeight, width, ELEMENT_EXTRA_SPACING)));
  536. yOffset = btnHeight + ELEMENT_EXTRA_SPACING;
  537. }
  538. if(current->mFoldoutBtn != nullptr)
  539. {
  540. Vector2I elementSize = current->mFoldoutBtn->_getOptimalSize();
  541. offset.x -= std::min((INT32)INITIAL_INDENT_OFFSET, elementSize.y);
  542. Vector2I myOffset = offset;
  543. if(elementSize.y > btnHeight)
  544. {
  545. UINT32 diff = elementSize.y - btnHeight;
  546. float half = diff * 0.5f;
  547. myOffset.y -= Math::floorToInt(half);
  548. }
  549. current->mFoldoutBtn->_setOffset(myOffset);
  550. current->mFoldoutBtn->_setWidth(elementSize.x);
  551. current->mFoldoutBtn->_setHeight(elementSize.y);
  552. current->mFoldoutBtn->_setAreaDepth(areaDepth);
  553. current->mFoldoutBtn->_setWidgetDepth(widgetDepth);
  554. RectI elemClipRect(clipRect.x - myOffset.x, clipRect.y - myOffset.y, clipRect.width, clipRect.height);
  555. current->mFoldoutBtn->_setClipRect(elemClipRect);
  556. }
  557. offset.y += yOffset;
  558. tempOrderedElements.resize(current->mChildren.size(), nullptr);
  559. for(auto& child : current->mChildren)
  560. {
  561. tempOrderedElements[child->mSortedIdx] = child;
  562. }
  563. for(auto iter = tempOrderedElements.rbegin(); iter != tempOrderedElements.rend(); ++iter)
  564. {
  565. TreeElement* child = *iter;
  566. if(!child->mIsVisible)
  567. continue;
  568. todo.push(UpdateTreeElement(child, indent + 1));
  569. }
  570. }
  571. for(auto selectedElem : mSelectedElements)
  572. {
  573. GUILabel* targetElement = selectedElem.element->mElement;
  574. Vector2I offset = targetElement->_getOffset();
  575. offset.x = x;
  576. selectedElem.background->_setOffset(offset);
  577. selectedElem.background->_setWidth(width);
  578. selectedElem.background->_setHeight(targetElement->_getHeight());
  579. selectedElem.background->_setAreaDepth(areaDepth + 1);
  580. selectedElem.background->_setWidgetDepth(widgetDepth);
  581. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  582. selectedElem.background->_setClipRect(elemClipRect);
  583. }
  584. if(mEditElement != nullptr)
  585. {
  586. GUILabel* targetElement = mEditElement->mElement;
  587. Vector2I offset = targetElement->_getOffset();
  588. UINT32 remainingWidth = (UINT32)std::max(0, (((INT32)width) - (offset.x - x)));
  589. mNameEditBox->_setOffset(offset);
  590. mNameEditBox->_setWidth(remainingWidth);
  591. mNameEditBox->_setHeight(targetElement->_getHeight());
  592. mNameEditBox->_setAreaDepth(areaDepth);
  593. mNameEditBox->_setWidgetDepth(widgetDepth);
  594. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  595. mNameEditBox->_setClipRect(elemClipRect);
  596. }
  597. }
  598. const GUISceneTreeView::InteractableElement* GUISceneTreeView::findElementUnderCoord(const CM::Vector2I& coord) const
  599. {
  600. for(auto& element : mVisibleElements)
  601. {
  602. if(element.bounds.contains(coord))
  603. {
  604. return &element;
  605. }
  606. }
  607. return nullptr;
  608. }
  609. GUISceneTreeView::TreeElement* GUISceneTreeView::interactableToRealElement(const GUISceneTreeView::InteractableElement& element)
  610. {
  611. if(!element.isTreeElement())
  612. return nullptr;
  613. UINT32 sortedIdx = (element.index - 1) / 2;
  614. auto findIter = std::find_if(element.parent->mChildren.begin(), element.parent->mChildren.end(),
  615. [&](const TreeElement* x) { return x->mSortedIdx == sortedIdx; });
  616. if(findIter != element.parent->mChildren.end())
  617. return *findIter;
  618. return nullptr;
  619. }
  620. const String& GUISceneTreeView::getGUITypeName()
  621. {
  622. static String typeName = "SceneTreeView";
  623. return typeName;
  624. }
  625. }