BsGUITreeView.cpp 34 KB

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