BsGUITreeView.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. #include "BsGUITreeView.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 "BsGUIVirtualButtonEvent.h"
  15. #include "BsGUIScrollArea.h"
  16. #include "BsDragAndDropManager.h"
  17. #include "CmTime.h"
  18. using namespace CamelotFramework;
  19. using namespace BansheeEngine;
  20. namespace BansheeEditor
  21. {
  22. const UINT32 GUITreeView::ELEMENT_EXTRA_SPACING = 3;
  23. const UINT32 GUITreeView::INDENT_SIZE = 10;
  24. const UINT32 GUITreeView::INITIAL_INDENT_OFFSET = 16;
  25. const UINT32 GUITreeView::DRAG_MIN_DISTANCE = 3;
  26. const float GUITreeView::AUTO_EXPAND_DELAY_SEC = 0.5f;
  27. const float GUITreeView::SCROLL_AREA_HEIGHT_PCT = 0.1f;
  28. const UINT32 GUITreeView::SCROLL_SPEED_PX_PER_SEC = 25;
  29. VirtualButton GUITreeView::mRenameVB = VirtualButton("Rename");
  30. GUITreeView::TreeElement::TreeElement()
  31. :mParent(nullptr), mFoldoutBtn(nullptr), mElement(nullptr), mIsSelected(false),
  32. mIsExpanded(false), mSortedIdx(0), mIsDirty(false), mIsVisible(true)
  33. { }
  34. GUITreeView::TreeElement::~TreeElement()
  35. {
  36. for(auto& child : mChildren)
  37. cm_delete(child);
  38. if(mFoldoutBtn != nullptr)
  39. GUIElement::destroy(mFoldoutBtn);
  40. if(mElement != nullptr)
  41. GUIElement::destroy(mElement);
  42. mChildren.clear();
  43. }
  44. bool GUITreeView::TreeElement::isParentRec(TreeElement* element) const
  45. {
  46. TreeElement* curParent = mParent;
  47. while(curParent != nullptr)
  48. {
  49. if(curParent == element)
  50. return true;
  51. curParent = curParent->mParent;
  52. }
  53. return false;
  54. }
  55. GUITreeView::TreeElement* GUITreeView::InteractableElement::getTreeElement() const
  56. {
  57. if(!isTreeElement())
  58. return nullptr;
  59. UINT32 sortedIdx = (index - 1) / 2;
  60. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  61. [&](const TreeElement* x) { return x->mSortedIdx == sortedIdx; });
  62. if(findIter != parent->mChildren.end())
  63. return *findIter;
  64. return nullptr;
  65. }
  66. GUITreeView::GUITreeView(GUIWidget& parent, GUIElementStyle* backgroundStyle, GUIElementStyle* elementBtnStyle,
  67. GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle, GUIElementStyle* editBoxStyle,
  68. BS::GUIElementStyle* dragHighlightStyle, BS::GUIElementStyle* dragSepHighlightStyle, const GUILayoutOptions& layoutOptions)
  69. :GUIElementContainer(parent, layoutOptions), mBackgroundStyle(backgroundStyle),
  70. mElementBtnStyle(elementBtnStyle), mFoldoutBtnStyle(foldoutBtnStyle), mEditBoxStyle(editBoxStyle), mEditElement(nullptr), mIsElementSelected(false),
  71. mNameEditBox(nullptr), mSelectionBackgroundStyle(selectionBackgroundStyle), mDragInProgress(nullptr), mDragHighlightStyle(dragHighlightStyle),
  72. mDragSepHighlightStyle(dragSepHighlightStyle), mDragHighlight(nullptr), mDragSepHighlight(nullptr), mMouseOverDragElement(nullptr), mMouseOverDragElementTime(0.0f),
  73. mScrollState(ScrollState::None), mLastScrollTime(0.0f)
  74. {
  75. if(mBackgroundStyle == nullptr)
  76. mBackgroundStyle = parent.getSkin().getStyle("TreeViewBackground");
  77. if(mElementBtnStyle == nullptr)
  78. mElementBtnStyle = parent.getSkin().getStyle("TreeViewElementBtn");
  79. if(mFoldoutBtnStyle == nullptr)
  80. mFoldoutBtnStyle = parent.getSkin().getStyle("TreeViewFoldoutBtn");
  81. if(mSelectionBackgroundStyle == nullptr)
  82. mSelectionBackgroundStyle = parent.getSkin().getStyle("TreeViewSelectionBackground");
  83. if(mEditBoxStyle == nullptr)
  84. mEditBoxStyle = parent.getSkin().getStyle("TreeViewEditBox");
  85. if(mDragHighlightStyle == nullptr)
  86. mDragHighlightStyle = parent.getSkin().getStyle("TreeViewElementHighlight");
  87. if(mDragSepHighlightStyle == nullptr)
  88. mDragSepHighlightStyle = parent.getSkin().getStyle("TreeViewElementSepHighlight");
  89. mBackgroundImage = GUITexture::create(parent, mBackgroundStyle);
  90. mNameEditBox = GUITreeViewEditBox::create(parent, mEditBoxStyle);
  91. mNameEditBox->disableRecursively();
  92. mNameEditBox->onInputConfirmed.connect(boost::bind(&GUITreeView::onEditAccepted, this));
  93. mNameEditBox->onInputCanceled.connect(boost::bind(&GUITreeView::onEditCanceled, this));
  94. mDragHighlight = GUITexture::create(parent, mDragHighlightStyle);
  95. mDragSepHighlight = GUITexture::create(parent, mDragSepHighlightStyle);
  96. mDragHighlight->disableRecursively();
  97. mDragSepHighlight->disableRecursively();
  98. _registerChildElement(mBackgroundImage);
  99. _registerChildElement(mNameEditBox);
  100. _registerChildElement(mDragHighlight);
  101. _registerChildElement(mDragSepHighlight);
  102. }
  103. GUITreeView::~GUITreeView()
  104. {
  105. }
  106. void GUITreeView::update()
  107. {
  108. // Attempt to auto-expand elements we are dragging over
  109. if(acceptDragAndDrop())
  110. {
  111. const GUITreeView::InteractableElement* element = findElementUnderCoord(mDragPosition);
  112. temporarilyExpandElement(element);
  113. }
  114. // NOTE - Instead of iterating through every visible element and comparing it with internal values,
  115. // I might just want to add callbacks to SceneManager that notify me of any changes and then only perform
  116. // update if anything is actually dirty
  117. updateTreeElementHierarchy();
  118. // Create/Destroy GUI elements
  119. Stack<TreeElement*>::type todo;
  120. todo.push(&getRootElement());
  121. while(!todo.empty())
  122. {
  123. TreeElement* current = todo.top();
  124. todo.pop();
  125. if(current->mIsDirty && current != &getRootElement())
  126. {
  127. if(current->mIsVisible)
  128. {
  129. HString name(toWString(current->mName));
  130. if(current->mElement == nullptr)
  131. {
  132. current->mElement = GUILabel::create(_getParentWidget(), name, mElementBtnStyle);
  133. _registerChildElement(current->mElement);
  134. }
  135. if(current->mChildren.size() > 0)
  136. {
  137. if(current->mFoldoutBtn == nullptr)
  138. {
  139. current->mFoldoutBtn = GUIToggle::create(_getParentWidget(), GUIContent(HString(L"")), mFoldoutBtnStyle);
  140. _registerChildElement(current->mFoldoutBtn);
  141. current->mFoldoutBtn->onToggled.connect(boost::bind(&GUITreeView::elementToggled, this, current, _1));
  142. if(current->mIsExpanded)
  143. current->mFoldoutBtn->toggleOn();
  144. }
  145. }
  146. else
  147. {
  148. if(current->mFoldoutBtn != nullptr)
  149. {
  150. GUIElement::destroy(current->mFoldoutBtn);
  151. current->mFoldoutBtn = nullptr;
  152. }
  153. }
  154. current->mElement->setContent(GUIContent(name));
  155. }
  156. else
  157. {
  158. if(current->mElement != nullptr)
  159. {
  160. GUIElement::destroy(current->mElement);
  161. current->mElement = nullptr;
  162. }
  163. if(current->mFoldoutBtn != nullptr)
  164. {
  165. GUIElement::destroy(current->mFoldoutBtn);
  166. current->mFoldoutBtn = nullptr;
  167. }
  168. if(current->mIsSelected && current->mIsExpanded)
  169. unselectElement(current);
  170. }
  171. markContentAsDirty();
  172. current->mIsDirty = false;
  173. }
  174. // Queue children for next iteration
  175. if(current->mIsDirty || current->mIsVisible)
  176. {
  177. for(UINT32 i = 0; i < (UINT32)current->mChildren.size(); i++)
  178. {
  179. todo.push(current->mChildren[i]);
  180. }
  181. }
  182. }
  183. // Attempt to scroll if needed
  184. if(mScrollState != ScrollState::None)
  185. {
  186. GUIScrollArea* scrollArea = findParentScrollArea();
  187. if(scrollArea != nullptr)
  188. {
  189. float curTime = gTime().getTime();
  190. float timeDiff = curTime - mLastScrollTime;
  191. float secondsPerPixel = 1.0f / SCROLL_SPEED_PX_PER_SEC;
  192. switch(mScrollState)
  193. {
  194. case ScrollState::TransitioningUp:
  195. mScrollState = ScrollState::Up;
  196. mLastScrollTime = curTime;
  197. break;
  198. case ScrollState::TransitioningDown:
  199. mScrollState = ScrollState::Down;
  200. mLastScrollTime = curTime;
  201. break;
  202. case ScrollState::Up:
  203. {
  204. UINT32 scrollAmount = (UINT32)Math::floorToInt(timeDiff / secondsPerPixel);
  205. mLastScrollTime += scrollAmount * secondsPerPixel;
  206. scrollArea->scrollUpPx(scrollAmount);
  207. }
  208. break;
  209. case ScrollState::Down:
  210. {
  211. UINT32 scrollAmount = (UINT32)Math::floorToInt(timeDiff / secondsPerPixel);
  212. mLastScrollTime += scrollAmount * secondsPerPixel;
  213. scrollArea->scrollDownPx(scrollAmount);
  214. }
  215. break;
  216. }
  217. }
  218. }
  219. }
  220. bool GUITreeView::mouseEvent(const GUIMouseEvent& event)
  221. {
  222. if(event.getType() == GUIMouseEventType::MouseUp)
  223. {
  224. if(DragAndDropManager::instance().isDragInProgress())
  225. return false;
  226. const GUITreeView::InteractableElement* element = findElementUnderCoord(event.getPosition());
  227. TreeElement* treeElement = nullptr;
  228. if(element != nullptr && element->isTreeElement())
  229. {
  230. treeElement = element->getTreeElement();
  231. }
  232. if(treeElement != nullptr && event.getPosition().x >= treeElement->mElement->getBounds().x)
  233. {
  234. if(event.isCtrlDown())
  235. {
  236. selectElement(treeElement);
  237. }
  238. else if(event.isShiftDown())
  239. {
  240. if(isSelectionActive())
  241. {
  242. TreeElement* selectionRoot = mSelectedElements[0].element;
  243. unselectAll();
  244. auto iterStartFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  245. [&] (const InteractableElement& x) { return x.parent == selectionRoot->mParent; } );
  246. bool foundStart = false;
  247. bool foundEnd = false;
  248. for(; iterStartFind != mVisibleElements.end(); ++iterStartFind)
  249. {
  250. if(!iterStartFind->isTreeElement())
  251. continue;
  252. TreeElement* curElem = iterStartFind->getTreeElement();
  253. if(curElem == selectionRoot)
  254. {
  255. foundStart = true;
  256. break;
  257. }
  258. }
  259. auto iterEndFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  260. [&] (const InteractableElement& x) { return &x == element; } );
  261. if(iterEndFind != mVisibleElements.end())
  262. foundEnd = true;
  263. if(foundStart && foundEnd)
  264. {
  265. if(iterStartFind < iterEndFind)
  266. {
  267. for(;iterStartFind != (iterEndFind + 1); ++iterStartFind)
  268. {
  269. if(iterStartFind->isTreeElement())
  270. selectElement(iterStartFind->getTreeElement());
  271. }
  272. }
  273. else if(iterEndFind < iterStartFind)
  274. {
  275. for(;iterEndFind != (iterStartFind + 1); ++iterEndFind)
  276. {
  277. if(iterEndFind->isTreeElement())
  278. selectElement(iterEndFind->getTreeElement());
  279. }
  280. }
  281. else
  282. selectElement(treeElement);
  283. }
  284. if(!foundStart || !foundEnd)
  285. selectElement(treeElement);
  286. }
  287. else
  288. {
  289. selectElement(treeElement);
  290. }
  291. }
  292. else
  293. {
  294. unselectAll();
  295. selectElement(treeElement);
  296. }
  297. markContentAsDirty();
  298. return true;
  299. }
  300. }
  301. else if(event.getType() == GUIMouseEventType::MouseDragStart)
  302. {
  303. mDragStartPosition = event.getPosition();
  304. }
  305. else if(event.getType() == GUIMouseEventType::MouseDrag)
  306. {
  307. UINT32 dist = mDragStartPosition.manhattanDist(event.getPosition());
  308. if(!DragAndDropManager::instance().isDragInProgress())
  309. {
  310. if(dist > DRAG_MIN_DISTANCE)
  311. {
  312. const GUITreeView::InteractableElement* element = findElementUnderCoord(mDragStartPosition);
  313. TreeElement* treeElement = nullptr;
  314. if(element != nullptr && element->isTreeElement())
  315. {
  316. // If element we are trying to drag isn't selected, select it
  317. TreeElement* treeElement = element->getTreeElement();
  318. auto iterFind = std::find_if(mSelectedElements.begin(), mSelectedElements.end(),
  319. [&] (const SelectedElement& x) { return x.element == treeElement; });
  320. if(iterFind == mSelectedElements.end())
  321. {
  322. unselectAll();
  323. selectElement(element->getTreeElement());
  324. }
  325. }
  326. dragAndDropStart();
  327. mDragPosition = event.getPosition();
  328. mDragInProgress = true;
  329. mScrollState = ScrollState::None;
  330. markContentAsDirty();
  331. }
  332. }
  333. }
  334. else if(event.getType() == GUIMouseEventType::MouseDragAndDropDragged)
  335. {
  336. if(acceptDragAndDrop())
  337. {
  338. mDragPosition = event.getPosition();
  339. mDragInProgress = true;
  340. markContentAsDirty();
  341. if(mBottomScrollBounds.contains(mDragPosition))
  342. {
  343. if(mScrollState != ScrollState::Down)
  344. mScrollState = ScrollState::TransitioningDown;
  345. }
  346. else if(mTopScrollBounds.contains(mDragPosition))
  347. {
  348. if(mScrollState != ScrollState::Up)
  349. mScrollState = ScrollState::TransitioningUp;
  350. }
  351. else
  352. mScrollState = ScrollState::None;
  353. return true;
  354. }
  355. }
  356. else if(event.getType() == GUIMouseEventType::MouseDragAndDropDropped)
  357. {
  358. if(acceptDragAndDrop())
  359. {
  360. const GUITreeView::InteractableElement* element = findElementUnderCoord(event.getPosition());
  361. TreeElement* treeElement = nullptr;
  362. if(element != nullptr)
  363. {
  364. if(element->isTreeElement())
  365. treeElement = element->getTreeElement();
  366. else
  367. treeElement = element->parent;
  368. }
  369. dragAndDropEnded(treeElement);
  370. unselectAll();
  371. return true;
  372. }
  373. }
  374. else if(event.getType() == GUIMouseEventType::MouseOut)
  375. {
  376. mDragInProgress = false;
  377. markContentAsDirty();
  378. }
  379. return false;
  380. }
  381. bool GUITreeView::commandEvent(const GUICommandEvent& ev)
  382. {
  383. if(ev.getType() == GUICommandEventType::CursorMoveUp || ev.getType() == GUICommandEventType::SelectUp)
  384. {
  385. TreeElement* topMostElement = getTopMostSelectedElement();
  386. auto topMostIter = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  387. [&] (const InteractableElement& x) { return x.getTreeElement() == topMostElement; });
  388. if(topMostIter != mVisibleElements.end() && topMostIter != mVisibleElements.begin())
  389. {
  390. do
  391. {
  392. topMostIter--;
  393. } while (!topMostIter->isTreeElement() && topMostIter != mVisibleElements.begin());
  394. if(topMostIter->isTreeElement())
  395. {
  396. if(ev.getType() == GUICommandEventType::CursorMoveUp)
  397. unselectAll();
  398. TreeElement* treeElement = topMostIter->getTreeElement();
  399. selectElement(treeElement);
  400. scrollToElement(treeElement, false);
  401. }
  402. }
  403. }
  404. else if(ev.getType() == GUICommandEventType::CursorMoveDown || ev.getType() == GUICommandEventType::SelectDown)
  405. {
  406. TreeElement* bottoMostElement = getBottomMostSelectedElement();
  407. auto bottomMostIter = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  408. [&] (const InteractableElement& x) { return x.getTreeElement() == bottoMostElement; });
  409. if(bottomMostIter != mVisibleElements.end())
  410. {
  411. do
  412. {
  413. bottomMostIter++;
  414. } while (bottomMostIter != mVisibleElements.end() && !bottomMostIter->isTreeElement());
  415. if(bottomMostIter != mVisibleElements.end() && bottomMostIter->isTreeElement())
  416. {
  417. if(ev.getType() == GUICommandEventType::CursorMoveDown)
  418. unselectAll();
  419. TreeElement* treeElement = bottomMostIter->getTreeElement();
  420. selectElement(treeElement);
  421. scrollToElement(treeElement, false);
  422. }
  423. }
  424. }
  425. return false;
  426. }
  427. bool GUITreeView::virtualButtonEvent(const BS::GUIVirtualButtonEvent& ev)
  428. {
  429. if(ev.getButton() == mRenameVB)
  430. {
  431. if(isSelectionActive() && mEditElement == nullptr)
  432. {
  433. enableEdit(mSelectedElements[0].element);
  434. unselectAll();
  435. }
  436. return true;
  437. }
  438. return false;
  439. }
  440. bool GUITreeView::isSelectionActive() const
  441. {
  442. return mIsElementSelected && mSelectedElements.size() > 0;
  443. }
  444. void GUITreeView::selectElement(TreeElement* element)
  445. {
  446. auto iterFind = std::find_if(mSelectedElements.begin(), mSelectedElements.end(),
  447. [&] (const SelectedElement& x) { return x.element == element; });
  448. if(iterFind == mSelectedElements.end())
  449. {
  450. GUITexture* background = GUITexture::create(_getParentWidget(), mSelectionBackgroundStyle);
  451. _registerChildElement(background);
  452. element->mIsSelected = true;
  453. mSelectedElements.push_back(SelectedElement(element, background));
  454. mIsElementSelected = true;
  455. }
  456. }
  457. void GUITreeView::unselectElement(TreeElement* element)
  458. {
  459. auto iterFind = std::find_if(mSelectedElements.begin(), mSelectedElements.end(),
  460. [&] (const SelectedElement& x) { return x.element == element; });
  461. if(iterFind != mSelectedElements.end())
  462. {
  463. iterFind->element->mIsSelected = false;
  464. GUIElement::destroy(iterFind->background);
  465. mSelectedElements.erase(iterFind);
  466. markContentAsDirty();
  467. }
  468. mIsElementSelected = mSelectedElements.size() > 0;
  469. }
  470. void GUITreeView::unselectAll()
  471. {
  472. for(auto& selectedElem : mSelectedElements)
  473. {
  474. selectedElem.element->mIsSelected = false;
  475. GUIElement::destroy(selectedElem.background);
  476. }
  477. mSelectedElements.clear();
  478. mIsElementSelected = false;
  479. markContentAsDirty();
  480. }
  481. void GUITreeView::elementToggled(TreeElement* element, bool toggled)
  482. {
  483. element->mIsExpanded = toggled;
  484. }
  485. void GUITreeView::onEditAccepted()
  486. {
  487. disableEdit(true);
  488. }
  489. void GUITreeView::onEditCanceled()
  490. {
  491. if(mEditElement != nullptr)
  492. disableEdit(false);
  493. }
  494. void GUITreeView::enableEdit(TreeElement* element)
  495. {
  496. assert(mEditElement == nullptr);
  497. mEditElement = element;
  498. mNameEditBox->enableRecursively();
  499. mNameEditBox->setFocus(true);
  500. if(element->mElement != nullptr)
  501. element->mElement->disableRecursively();
  502. }
  503. void GUITreeView::disableEdit(bool applyChanges)
  504. {
  505. assert(mEditElement != nullptr);
  506. if(mEditElement->mElement != nullptr)
  507. mEditElement->mElement->enableRecursively();
  508. if(applyChanges)
  509. {
  510. WString newName = mNameEditBox->getText();
  511. renameTreeElement(mEditElement, newName);
  512. }
  513. mNameEditBox->disableRecursively();
  514. mEditElement = nullptr;
  515. }
  516. Vector2I GUITreeView::_getOptimalSize() const
  517. {
  518. struct UpdateTreeElement
  519. {
  520. UpdateTreeElement(const TreeElement* element, UINT32 indent)
  521. :element(element), indent(indent)
  522. { }
  523. const TreeElement* element;
  524. UINT32 indent;
  525. };
  526. Vector2I optimalSize;
  527. if(_getLayoutOptions().fixedWidth && _getLayoutOptions().fixedHeight)
  528. {
  529. optimalSize.x = _getLayoutOptions().width;
  530. optimalSize.y = _getLayoutOptions().height;
  531. }
  532. else
  533. {
  534. Stack<UpdateTreeElement>::type todo;
  535. todo.push(UpdateTreeElement(&getRootElementConst(), 0));
  536. while(!todo.empty())
  537. {
  538. UpdateTreeElement currentUpdateElement = todo.top();
  539. const TreeElement* current = currentUpdateElement.element;
  540. todo.pop();
  541. INT32 yOffset = 0;
  542. if(current->mElement != nullptr)
  543. {
  544. Vector2I curOptimalSize = current->mElement->_getOptimalSize();
  545. optimalSize.x = std::max(optimalSize.x,
  546. (INT32)(INITIAL_INDENT_OFFSET + curOptimalSize.x + currentUpdateElement.indent * INDENT_SIZE));
  547. yOffset = curOptimalSize.y + ELEMENT_EXTRA_SPACING;
  548. }
  549. optimalSize.y += yOffset;
  550. for(auto& child : current->mChildren)
  551. {
  552. if(!child->mIsVisible)
  553. continue;
  554. todo.push(UpdateTreeElement(child, currentUpdateElement.indent + 1));
  555. }
  556. }
  557. if(_getLayoutOptions().fixedWidth)
  558. optimalSize.x = _getLayoutOptions().width;
  559. else
  560. {
  561. if(_getLayoutOptions().minWidth > 0)
  562. optimalSize.x = std::max((INT32)_getLayoutOptions().minWidth, optimalSize.x);
  563. if(_getLayoutOptions().maxWidth > 0)
  564. optimalSize.x = std::min((INT32)_getLayoutOptions().maxWidth, optimalSize.x);
  565. }
  566. if(_getLayoutOptions().fixedHeight)
  567. optimalSize.y = _getLayoutOptions().height;
  568. else
  569. {
  570. if(_getLayoutOptions().minHeight > 0)
  571. optimalSize.y = std::max((INT32)_getLayoutOptions().minHeight, optimalSize.y);
  572. if(_getLayoutOptions().maxHeight > 0)
  573. optimalSize.y = std::min((INT32)_getLayoutOptions().maxHeight, optimalSize.y);
  574. }
  575. }
  576. return optimalSize;
  577. }
  578. void GUITreeView::updateClippedBounds()
  579. {
  580. Vector2I offset = _getOffset();
  581. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  582. RectI localClipRect(mClipRect.x + mOffset.x, mClipRect.y + mOffset.y, mClipRect.width, mClipRect.height);
  583. mClippedBounds.clip(localClipRect);
  584. }
  585. void GUITreeView::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  586. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  587. {
  588. struct UpdateTreeElement
  589. {
  590. UpdateTreeElement(TreeElement* element, UINT32 indent)
  591. :element(element), indent(indent)
  592. { }
  593. TreeElement* element;
  594. UINT32 indent;
  595. };
  596. mVisibleElements.clear();
  597. Stack<UpdateTreeElement>::type todo;
  598. todo.push(UpdateTreeElement(&getRootElement(), 0));
  599. // NOTE - Instead of iterating through all elements, try to find those within the clip rect
  600. // and only iterate through those. Others should somehow be marked in-active (similar to GUIElement::isDisabled()?)
  601. Vector<TreeElement*>::type tempOrderedElements;
  602. Vector2I offset(x, y);
  603. while(!todo.empty())
  604. {
  605. UpdateTreeElement currentUpdateElement = todo.top();
  606. TreeElement* current = currentUpdateElement.element;
  607. UINT32 indent = currentUpdateElement.indent;
  608. todo.pop();
  609. INT32 btnHeight = 0;
  610. INT32 yOffset = 0;
  611. if(current->mElement != nullptr)
  612. {
  613. Vector2I elementSize = current->mElement->_getOptimalSize();
  614. btnHeight = elementSize.y;
  615. mVisibleElements.push_back(InteractableElement(current->mParent, current->mSortedIdx * 2 + 0, RectI(x, offset.y, width, ELEMENT_EXTRA_SPACING)));
  616. mVisibleElements.push_back(InteractableElement(current->mParent, current->mSortedIdx * 2 + 1, RectI(x, offset.y + ELEMENT_EXTRA_SPACING, width, btnHeight)));
  617. offset.x = x + INITIAL_INDENT_OFFSET + indent * INDENT_SIZE;
  618. offset.y += ELEMENT_EXTRA_SPACING;
  619. current->mElement->_setOffset(offset);
  620. current->mElement->_setWidth(elementSize.x);
  621. current->mElement->_setHeight(elementSize.y);
  622. current->mElement->_setAreaDepth(areaDepth);
  623. current->mElement->_setWidgetDepth(widgetDepth);
  624. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  625. current->mElement->_setClipRect(elemClipRect);
  626. yOffset = btnHeight;
  627. }
  628. if(current->mFoldoutBtn != nullptr)
  629. {
  630. Vector2I elementSize = current->mFoldoutBtn->_getOptimalSize();
  631. offset.x -= std::min((INT32)INITIAL_INDENT_OFFSET, elementSize.x);
  632. Vector2I myOffset = offset;
  633. myOffset.y -= 2; // TODO: Arbitrary offset, I should adjust it based on font baseline so that the button is nicely centered on text
  634. if(elementSize.y > btnHeight)
  635. {
  636. UINT32 diff = elementSize.y - btnHeight;
  637. float half = diff * 0.5f;
  638. myOffset.y -= Math::floorToInt(half);
  639. }
  640. current->mFoldoutBtn->_setOffset(myOffset);
  641. current->mFoldoutBtn->_setWidth(elementSize.x);
  642. current->mFoldoutBtn->_setHeight(elementSize.y);
  643. current->mFoldoutBtn->_setAreaDepth(areaDepth);
  644. current->mFoldoutBtn->_setWidgetDepth(widgetDepth);
  645. RectI elemClipRect(clipRect.x - myOffset.x, clipRect.y - myOffset.y, clipRect.width, clipRect.height);
  646. current->mFoldoutBtn->_setClipRect(elemClipRect);
  647. }
  648. offset.y += yOffset;
  649. tempOrderedElements.resize(current->mChildren.size(), nullptr);
  650. for(auto& child : current->mChildren)
  651. {
  652. tempOrderedElements[child->mSortedIdx] = child;
  653. }
  654. for(auto iter = tempOrderedElements.rbegin(); iter != tempOrderedElements.rend(); ++iter)
  655. {
  656. TreeElement* child = *iter;
  657. if(!child->mIsVisible)
  658. continue;
  659. todo.push(UpdateTreeElement(child, indent + 1));
  660. }
  661. }
  662. UINT32 remainingHeight = (UINT32)std::max(0, (INT32)height - (offset.y - y));
  663. if(remainingHeight > 0)
  664. mVisibleElements.push_back(InteractableElement(&getRootElement(), (UINT32)getRootElement().mChildren.size() * 2, RectI(x, offset.y, width, remainingHeight)));
  665. for(auto selectedElem : mSelectedElements)
  666. {
  667. GUILabel* targetElement = selectedElem.element->mElement;
  668. Vector2I offset = targetElement->_getOffset();
  669. offset.x = x;
  670. selectedElem.background->_setOffset(offset);
  671. selectedElem.background->_setWidth(width);
  672. selectedElem.background->_setHeight(targetElement->_getHeight());
  673. selectedElem.background->_setAreaDepth(areaDepth + 1);
  674. selectedElem.background->_setWidgetDepth(widgetDepth);
  675. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  676. selectedElem.background->_setClipRect(elemClipRect);
  677. }
  678. if(mEditElement != nullptr)
  679. {
  680. GUILabel* targetElement = mEditElement->mElement;
  681. Vector2I offset = targetElement->_getOffset();
  682. UINT32 remainingWidth = (UINT32)std::max(0, (((INT32)width) - (offset.x - x)));
  683. mNameEditBox->_setOffset(offset);
  684. mNameEditBox->_setWidth(remainingWidth);
  685. mNameEditBox->_setHeight(targetElement->_getHeight());
  686. mNameEditBox->_setAreaDepth(areaDepth);
  687. mNameEditBox->_setWidgetDepth(widgetDepth);
  688. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  689. mNameEditBox->_setClipRect(elemClipRect);
  690. }
  691. if(mDragInProgress)
  692. {
  693. const InteractableElement* interactableElement = findElementUnderCoord(mDragPosition);
  694. if(interactableElement == nullptr)
  695. {
  696. if(!mDragHighlight->_isDisabled())
  697. mDragHighlight->disableRecursively();
  698. if(!mDragSepHighlight->_isDisabled())
  699. mDragSepHighlight->disableRecursively();
  700. }
  701. else
  702. {
  703. if(interactableElement->isTreeElement())
  704. {
  705. if(!mDragSepHighlight->_isDisabled())
  706. mDragSepHighlight->disableRecursively();
  707. if(mDragHighlight->_isDisabled())
  708. mDragHighlight->enableRecursively();
  709. Vector2I offset(interactableElement->bounds.x, interactableElement->bounds.y);
  710. mDragHighlight->_setOffset(offset);
  711. mDragHighlight->_setWidth(interactableElement->bounds.width);
  712. mDragHighlight->_setHeight(interactableElement->bounds.height);
  713. mDragHighlight->_setAreaDepth(areaDepth + 1);
  714. mDragHighlight->_setWidgetDepth(widgetDepth);
  715. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  716. mDragHighlight->_setClipRect(elemClipRect);
  717. }
  718. else
  719. {
  720. if(!mDragHighlight->_isDisabled())
  721. mDragHighlight->disableRecursively();
  722. if(mDragSepHighlight->_isDisabled())
  723. mDragSepHighlight->enableRecursively();
  724. Vector2I offset(interactableElement->bounds.x, interactableElement->bounds.y);
  725. mDragSepHighlight->_setOffset(offset);
  726. mDragSepHighlight->_setWidth(interactableElement->bounds.width);
  727. mDragSepHighlight->_setHeight(interactableElement->bounds.height);
  728. mDragSepHighlight->_setAreaDepth(areaDepth + 1);
  729. mDragSepHighlight->_setWidgetDepth(widgetDepth);
  730. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  731. mDragSepHighlight->_setClipRect(elemClipRect);
  732. }
  733. }
  734. }
  735. else
  736. {
  737. if(!mDragHighlight->_isDisabled())
  738. mDragHighlight->disableRecursively();
  739. if(!mDragSepHighlight->_isDisabled())
  740. mDragSepHighlight->disableRecursively();
  741. }
  742. // Update scroll bounds
  743. UINT32 scrollHeight = (UINT32)Math::roundToInt(clipRect.height * SCROLL_AREA_HEIGHT_PCT);
  744. mTopScrollBounds.x = clipRect.x;
  745. mTopScrollBounds.y = clipRect.y;
  746. mTopScrollBounds.width = clipRect.width;
  747. mTopScrollBounds.height = scrollHeight;
  748. mBottomScrollBounds.x = clipRect.x;
  749. mBottomScrollBounds.y = clipRect.y + clipRect.height - scrollHeight;
  750. mBottomScrollBounds.width = clipRect.width;
  751. mBottomScrollBounds.height = scrollHeight;
  752. }
  753. const GUITreeView::InteractableElement* GUITreeView::findElementUnderCoord(const CM::Vector2I& coord) const
  754. {
  755. for(auto& element : mVisibleElements)
  756. {
  757. if(element.bounds.contains(coord))
  758. {
  759. return &element;
  760. }
  761. }
  762. return nullptr;
  763. }
  764. GUITreeView::TreeElement* GUITreeView::getTopMostSelectedElement() const
  765. {
  766. auto topMostElement = mVisibleElements.end();
  767. for(auto& selectedElement : mSelectedElements)
  768. {
  769. auto iterFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  770. [&] (const InteractableElement& x) { return x.getTreeElement() == selectedElement.element; });
  771. if(iterFind != mVisibleElements.end())
  772. {
  773. if(topMostElement == mVisibleElements.end())
  774. topMostElement = iterFind;
  775. else
  776. {
  777. if(iterFind->bounds.y < topMostElement->bounds.y)
  778. topMostElement = iterFind;
  779. }
  780. }
  781. }
  782. if(topMostElement != mVisibleElements.end())
  783. return topMostElement->getTreeElement();
  784. else
  785. return nullptr;
  786. }
  787. GUITreeView::TreeElement* GUITreeView::getBottomMostSelectedElement() const
  788. {
  789. auto& botMostElement = mVisibleElements.end();
  790. for(auto& selectedElement : mSelectedElements)
  791. {
  792. auto iterFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  793. [&] (const InteractableElement& x) { return x.getTreeElement() == selectedElement.element; });
  794. if(iterFind != mVisibleElements.end())
  795. {
  796. if(botMostElement == mVisibleElements.end())
  797. botMostElement = iterFind;
  798. else
  799. {
  800. if((iterFind->bounds.y + iterFind->bounds.height) > (botMostElement->bounds.y + botMostElement->bounds.height))
  801. botMostElement = iterFind;
  802. }
  803. }
  804. }
  805. if(botMostElement != mVisibleElements.end())
  806. return botMostElement->getTreeElement();
  807. else
  808. return nullptr;
  809. }
  810. void GUITreeView::closeTemporarilyExpandedElements()
  811. {
  812. temporarilyExpandElement(nullptr);
  813. }
  814. void GUITreeView::temporarilyExpandElement(const GUITreeView::InteractableElement* mouseOverElement)
  815. {
  816. TreeElement* treeElement = nullptr;
  817. if(mouseOverElement != nullptr && mouseOverElement->isTreeElement())
  818. treeElement = mouseOverElement->getTreeElement();
  819. if(treeElement == nullptr || treeElement != mMouseOverDragElement)
  820. {
  821. while(!mAutoExpandedElements.empty())
  822. {
  823. TreeElement* autoExpandedElement = mAutoExpandedElements.top();
  824. bool unexpandElement = false;
  825. if(mouseOverElement != nullptr && mouseOverElement->parent != nullptr)
  826. {
  827. if(mouseOverElement->parent != autoExpandedElement && !mouseOverElement->parent->isParentRec(autoExpandedElement))
  828. unexpandElement = true;
  829. else
  830. break;
  831. }
  832. else
  833. unexpandElement = true;
  834. if(unexpandElement)
  835. {
  836. autoExpandedElement->mIsExpanded = false;
  837. if(autoExpandedElement->mFoldoutBtn != nullptr)
  838. autoExpandedElement->mFoldoutBtn->toggleOff();
  839. mAutoExpandedElements.pop();
  840. }
  841. }
  842. mMouseOverDragElement = treeElement;
  843. mMouseOverDragElementTime = gTime().getTime();
  844. }
  845. else
  846. {
  847. if(mMouseOverDragElement != nullptr && !mMouseOverDragElement->mIsExpanded)
  848. {
  849. float timeDiff = gTime().getTime() - mMouseOverDragElementTime;
  850. if(timeDiff >= AUTO_EXPAND_DELAY_SEC)
  851. {
  852. mAutoExpandedElements.push(mMouseOverDragElement);
  853. mMouseOverDragElement->mIsExpanded = true;
  854. if(mMouseOverDragElement->mFoldoutBtn != nullptr)
  855. mMouseOverDragElement->mFoldoutBtn->toggleOn();
  856. }
  857. }
  858. }
  859. }
  860. void GUITreeView::scrollToElement(TreeElement* element, bool center)
  861. {
  862. if(element->mElement == nullptr)
  863. return;
  864. GUIScrollArea* scrollArea = findParentScrollArea();
  865. if(scrollArea == nullptr)
  866. return;
  867. if(center)
  868. {
  869. RectI myBounds = _getClippedBounds();
  870. INT32 clipVertCenter = myBounds.y + (INT32)Math::roundToInt(myBounds.height * 0.5f);
  871. INT32 elemVertCenter = element->mElement->_getOffset().y + (INT32)Math::roundToInt(element->mElement->_getHeight() * 0.5f);
  872. if(elemVertCenter > clipVertCenter)
  873. scrollArea->scrollUpPx(elemVertCenter - clipVertCenter);
  874. else
  875. scrollArea->scrollDownPx(clipVertCenter - elemVertCenter);
  876. }
  877. else
  878. {
  879. RectI myBounds = _getClippedBounds();
  880. INT32 elemVertTop = element->mElement->_getOffset().y;
  881. INT32 elemVertBottom = element->mElement->_getOffset().y + element->mElement->_getHeight();
  882. INT32 top = myBounds.y;
  883. INT32 bottom = myBounds.y + myBounds.height;
  884. INT32 offset = 0;
  885. if(elemVertTop < top)
  886. scrollArea->scrollUpPx(top - elemVertTop);
  887. else if(elemVertBottom > bottom)
  888. scrollArea->scrollDownPx(elemVertBottom - bottom);
  889. }
  890. }
  891. GUIScrollArea* GUITreeView::findParentScrollArea() const
  892. {
  893. GUIElementBase* parent = _getParent();
  894. while(parent != nullptr)
  895. {
  896. if(parent->_getType() == GUIElementBase::Type::Element)
  897. {
  898. GUIElement* parentElement = static_cast<GUIElement*>(parent);
  899. if(parentElement->getElementType() == GUIElement::ElementType::ScrollArea)
  900. {
  901. GUIScrollArea* scrollArea = static_cast<GUIScrollArea*>(parentElement);
  902. return scrollArea;
  903. }
  904. }
  905. parent = parent->_getParent();
  906. }
  907. return nullptr;
  908. }
  909. const String& GUITreeView::getGUITypeName()
  910. {
  911. static String typeName = "SceneTreeView";
  912. return typeName;
  913. }
  914. }