BsGUITreeView.cpp 35 KB

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