BsGUITreeView.cpp 34 KB

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