BsGUITreeView.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. #include "BsGUITreeView.h"
  2. #include "BsGUIArea.h"
  3. #include "BsGUILayout.h"
  4. #include "BsGUITexture.h"
  5. #include "BsGUIButton.h"
  6. #include "BsGUILabel.h"
  7. #include "BsGUISpace.h"
  8. #include "BsGUIWidget.h"
  9. #include "BsGUIToggle.h"
  10. #include "BsGUITreeViewEditBox.h"
  11. #include "BsGUIMouseEvent.h"
  12. #include "BsGUISkin.h"
  13. #include "BsGUICommandEvent.h"
  14. #include "BsGUIVirtualButtonEvent.h"
  15. #include "BsGUIScrollArea.h"
  16. #include "BsDragAndDropManager.h"
  17. #include "CmTime.h"
  18. using namespace CamelotFramework;
  19. using namespace BansheeEngine;
  20. using namespace std::placeholders;
  21. namespace BansheeEditor
  22. {
  23. const UINT32 GUITreeView::ELEMENT_EXTRA_SPACING = 3;
  24. const UINT32 GUITreeView::INDENT_SIZE = 10;
  25. const UINT32 GUITreeView::INITIAL_INDENT_OFFSET = 16;
  26. const UINT32 GUITreeView::DRAG_MIN_DISTANCE = 3;
  27. const float GUITreeView::AUTO_EXPAND_DELAY_SEC = 0.5f;
  28. const float GUITreeView::SCROLL_AREA_HEIGHT_PCT = 0.1f;
  29. const UINT32 GUITreeView::SCROLL_SPEED_PX_PER_SEC = 25;
  30. VirtualButton GUITreeView::mRenameVB = VirtualButton("Rename");
  31. VirtualButton GUITreeView::mDeleteVB = VirtualButton("Delete");
  32. GUITreeView::TreeElement::TreeElement()
  33. :mParent(nullptr), mFoldoutBtn(nullptr), mElement(nullptr), mIsSelected(false),
  34. mIsExpanded(false), mSortedIdx(0), mIsVisible(true)
  35. { }
  36. GUITreeView::TreeElement::~TreeElement()
  37. {
  38. for(auto& child : mChildren)
  39. cm_delete(child);
  40. if(mFoldoutBtn != nullptr)
  41. GUIElement::destroy(mFoldoutBtn);
  42. if(mElement != nullptr)
  43. GUIElement::destroy(mElement);
  44. mChildren.clear();
  45. }
  46. bool GUITreeView::TreeElement::isParentRec(TreeElement* element) const
  47. {
  48. TreeElement* curParent = mParent;
  49. while(curParent != nullptr)
  50. {
  51. if(curParent == element)
  52. return true;
  53. curParent = curParent->mParent;
  54. }
  55. return false;
  56. }
  57. GUITreeView::TreeElement* GUITreeView::InteractableElement::getTreeElement() const
  58. {
  59. if(!isTreeElement())
  60. return nullptr;
  61. UINT32 sortedIdx = (index - 1) / 2;
  62. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  63. [&](const TreeElement* x) { return x->mSortedIdx == sortedIdx; });
  64. if(findIter != parent->mChildren.end())
  65. return *findIter;
  66. return nullptr;
  67. }
  68. GUITreeView::GUITreeView(const CM::String& backgroundStyle, const CM::String& elementBtnStyle,
  69. const CM::String& foldoutBtnStyle, const CM::String& selectionBackgroundStyle, const CM::String& editBoxStyle,
  70. const CM::String& dragHighlightStyle, const CM::String& dragSepHighlightStyle, const GUILayoutOptions& layoutOptions)
  71. :GUIElementContainer(layoutOptions), mBackgroundStyle(backgroundStyle),
  72. mElementBtnStyle(elementBtnStyle), mFoldoutBtnStyle(foldoutBtnStyle), mEditBoxStyle(editBoxStyle), mEditElement(nullptr), mIsElementSelected(false),
  73. mNameEditBox(nullptr), mSelectionBackgroundStyle(selectionBackgroundStyle), mDragInProgress(nullptr), mDragHighlightStyle(dragHighlightStyle),
  74. mDragSepHighlightStyle(dragSepHighlightStyle), mDragHighlight(nullptr), mDragSepHighlight(nullptr), mMouseOverDragElement(nullptr), mMouseOverDragElementTime(0.0f),
  75. mScrollState(ScrollState::None), mLastScrollTime(0.0f)
  76. {
  77. if(mBackgroundStyle == CM::StringUtil::BLANK)
  78. mBackgroundStyle = "TreeViewBackground";
  79. if(mElementBtnStyle == CM::StringUtil::BLANK)
  80. mElementBtnStyle = "TreeViewElementBtn";
  81. if(mFoldoutBtnStyle == CM::StringUtil::BLANK)
  82. mFoldoutBtnStyle = "TreeViewFoldoutBtn";
  83. if(mSelectionBackgroundStyle == CM::StringUtil::BLANK)
  84. mSelectionBackgroundStyle = "TreeViewSelectionBackground";
  85. if(mEditBoxStyle == CM::StringUtil::BLANK)
  86. mEditBoxStyle = "TreeViewEditBox";
  87. if(mDragHighlightStyle == CM::StringUtil::BLANK)
  88. mDragHighlightStyle = "TreeViewElementHighlight";
  89. if(mDragSepHighlightStyle == CM::StringUtil::BLANK)
  90. mDragSepHighlightStyle = "TreeViewElementSepHighlight";
  91. mBackgroundImage = GUITexture::create(mBackgroundStyle);
  92. mNameEditBox = GUITreeViewEditBox::create(mEditBoxStyle);
  93. mNameEditBox->disableRecursively();
  94. mNameEditBox->onInputConfirmed.connect(std::bind(&GUITreeView::onEditAccepted, this));
  95. mNameEditBox->onInputCanceled.connect(std::bind(&GUITreeView::onEditCanceled, this));
  96. mDragHighlight = GUITexture::create(mDragHighlightStyle);
  97. mDragSepHighlight = GUITexture::create(mDragSepHighlightStyle);
  98. mDragHighlight->disableRecursively();
  99. mDragSepHighlight->disableRecursively();
  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->getBounds().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 BS::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*>::type 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. _registerChildElement(background);
  422. element->mIsSelected = true;
  423. mSelectedElements.push_back(SelectedElement(element, background));
  424. mIsElementSelected = true;
  425. }
  426. }
  427. void GUITreeView::unselectElement(TreeElement* element)
  428. {
  429. auto iterFind = std::find_if(mSelectedElements.begin(), mSelectedElements.end(),
  430. [&] (const SelectedElement& x) { return x.element == element; });
  431. if(iterFind != mSelectedElements.end())
  432. {
  433. iterFind->element->mIsSelected = false;
  434. GUIElement::destroy(iterFind->background);
  435. mSelectedElements.erase(iterFind);
  436. markContentAsDirty();
  437. }
  438. mIsElementSelected = mSelectedElements.size() > 0;
  439. }
  440. void GUITreeView::unselectAll()
  441. {
  442. for(auto& selectedElem : mSelectedElements)
  443. {
  444. selectedElem.element->mIsSelected = false;
  445. GUIElement::destroy(selectedElem.background);
  446. }
  447. mSelectedElements.clear();
  448. mIsElementSelected = false;
  449. markContentAsDirty();
  450. }
  451. void GUITreeView::expandElement(TreeElement* element)
  452. {
  453. if(element->mIsExpanded)
  454. return;
  455. element->mIsExpanded = true;
  456. if(element->mParent == nullptr || (element->mParent->mIsVisible && element->mParent->mIsExpanded))
  457. {
  458. Stack<TreeElement*>::type todo;
  459. todo.push(element);
  460. while(!todo.empty())
  461. {
  462. TreeElement* curElem = todo.top();
  463. todo.pop();
  464. curElem->mIsVisible = true;
  465. updateElementGUI(curElem);
  466. if(curElem->mIsExpanded)
  467. {
  468. for(auto& child : curElem->mChildren)
  469. todo.push(child);
  470. }
  471. }
  472. }
  473. }
  474. void GUITreeView::collapseElement(TreeElement* element)
  475. {
  476. if(!element->mIsExpanded)
  477. return;
  478. element->mIsExpanded = false;
  479. updateElementGUI(element);
  480. if(element->mParent == nullptr || (element->mParent->mIsVisible && element->mParent->mIsExpanded))
  481. {
  482. Stack<TreeElement*>::type todo;
  483. for(auto& child : element->mChildren)
  484. todo.push(child);
  485. while(!todo.empty())
  486. {
  487. TreeElement* curElem = todo.top();
  488. todo.pop();
  489. curElem->mIsVisible = false;
  490. if(curElem->mIsSelected)
  491. unselectElement(curElem);
  492. updateElementGUI(curElem);
  493. if(curElem->mIsExpanded)
  494. {
  495. for(auto& child : curElem->mChildren)
  496. todo.push(child);
  497. }
  498. }
  499. }
  500. }
  501. void GUITreeView::updateElementGUI(TreeElement* element)
  502. {
  503. if(element == &getRootElement())
  504. return;
  505. if(element->mIsVisible)
  506. {
  507. HString name(toWString(element->mName));
  508. if(element->mElement == nullptr)
  509. {
  510. element->mElement = GUILabel::create(name, mElementBtnStyle);
  511. _registerChildElement(element->mElement);
  512. }
  513. if(element->mChildren.size() > 0)
  514. {
  515. if(element->mFoldoutBtn == nullptr)
  516. {
  517. element->mFoldoutBtn = GUIToggle::create(GUIContent(HString(L"")), mFoldoutBtnStyle);
  518. _registerChildElement(element->mFoldoutBtn);
  519. element->mFoldoutBtn->onToggled.connect(std::bind(&GUITreeView::elementToggled, this, element, _1));
  520. if(element->mIsExpanded)
  521. element->mFoldoutBtn->toggleOn();
  522. }
  523. }
  524. else
  525. {
  526. if(element->mFoldoutBtn != nullptr)
  527. {
  528. GUIElement::destroy(element->mFoldoutBtn);
  529. element->mFoldoutBtn = nullptr;
  530. }
  531. }
  532. element->mElement->setContent(GUIContent(name));
  533. }
  534. else
  535. {
  536. if(element->mElement != nullptr)
  537. {
  538. GUIElement::destroy(element->mElement);
  539. element->mElement = nullptr;
  540. }
  541. if(element->mFoldoutBtn != nullptr)
  542. {
  543. GUIElement::destroy(element->mFoldoutBtn);
  544. element->mFoldoutBtn = nullptr;
  545. }
  546. if(element->mIsSelected && element->mIsExpanded)
  547. unselectElement(element);
  548. }
  549. markContentAsDirty();
  550. }
  551. void GUITreeView::elementToggled(TreeElement* element, bool toggled)
  552. {
  553. if(toggled)
  554. expandElement(element);
  555. else
  556. collapseElement(element);
  557. }
  558. void GUITreeView::onEditAccepted()
  559. {
  560. disableEdit(true);
  561. }
  562. void GUITreeView::onEditCanceled()
  563. {
  564. if(mEditElement != nullptr)
  565. disableEdit(false);
  566. }
  567. void GUITreeView::enableEdit(TreeElement* element)
  568. {
  569. assert(mEditElement == nullptr);
  570. mEditElement = element;
  571. mNameEditBox->enableRecursively();
  572. mNameEditBox->setFocus(true);
  573. if(element->mElement != nullptr)
  574. element->mElement->disableRecursively();
  575. }
  576. void GUITreeView::disableEdit(bool applyChanges)
  577. {
  578. assert(mEditElement != nullptr);
  579. if(mEditElement->mElement != nullptr)
  580. mEditElement->mElement->enableRecursively();
  581. if(applyChanges)
  582. {
  583. WString newName = mNameEditBox->getText();
  584. renameTreeElement(mEditElement, newName);
  585. }
  586. mNameEditBox->disableRecursively();
  587. mEditElement = nullptr;
  588. }
  589. Vector2I GUITreeView::_getOptimalSize() const
  590. {
  591. struct UpdateTreeElement
  592. {
  593. UpdateTreeElement(const TreeElement* element, UINT32 indent)
  594. :element(element), indent(indent)
  595. { }
  596. const TreeElement* element;
  597. UINT32 indent;
  598. };
  599. Vector2I optimalSize;
  600. if(_getLayoutOptions().fixedWidth && _getLayoutOptions().fixedHeight)
  601. {
  602. optimalSize.x = _getLayoutOptions().width;
  603. optimalSize.y = _getLayoutOptions().height;
  604. }
  605. else
  606. {
  607. Stack<UpdateTreeElement>::type todo;
  608. todo.push(UpdateTreeElement(&getRootElementConst(), 0));
  609. while(!todo.empty())
  610. {
  611. UpdateTreeElement currentUpdateElement = todo.top();
  612. const TreeElement* current = currentUpdateElement.element;
  613. todo.pop();
  614. INT32 yOffset = 0;
  615. if(current->mElement != nullptr)
  616. {
  617. Vector2I curOptimalSize = current->mElement->_getOptimalSize();
  618. optimalSize.x = std::max(optimalSize.x,
  619. (INT32)(INITIAL_INDENT_OFFSET + curOptimalSize.x + currentUpdateElement.indent * INDENT_SIZE));
  620. yOffset = curOptimalSize.y + ELEMENT_EXTRA_SPACING;
  621. }
  622. optimalSize.y += yOffset;
  623. for(auto& child : current->mChildren)
  624. {
  625. if(!child->mIsVisible)
  626. continue;
  627. todo.push(UpdateTreeElement(child, currentUpdateElement.indent + 1));
  628. }
  629. }
  630. if(_getLayoutOptions().fixedWidth)
  631. optimalSize.x = _getLayoutOptions().width;
  632. else
  633. {
  634. if(_getLayoutOptions().minWidth > 0)
  635. optimalSize.x = std::max((INT32)_getLayoutOptions().minWidth, optimalSize.x);
  636. if(_getLayoutOptions().maxWidth > 0)
  637. optimalSize.x = std::min((INT32)_getLayoutOptions().maxWidth, optimalSize.x);
  638. }
  639. if(_getLayoutOptions().fixedHeight)
  640. optimalSize.y = _getLayoutOptions().height;
  641. else
  642. {
  643. if(_getLayoutOptions().minHeight > 0)
  644. optimalSize.y = std::max((INT32)_getLayoutOptions().minHeight, optimalSize.y);
  645. if(_getLayoutOptions().maxHeight > 0)
  646. optimalSize.y = std::min((INT32)_getLayoutOptions().maxHeight, optimalSize.y);
  647. }
  648. }
  649. return optimalSize;
  650. }
  651. void GUITreeView::updateClippedBounds()
  652. {
  653. Vector2I offset = _getOffset();
  654. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  655. RectI localClipRect(mClipRect.x + mOffset.x, mClipRect.y + mOffset.y, mClipRect.width, mClipRect.height);
  656. mClippedBounds.clip(localClipRect);
  657. }
  658. void GUITreeView::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  659. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  660. {
  661. struct UpdateTreeElement
  662. {
  663. UpdateTreeElement(TreeElement* element, UINT32 indent)
  664. :element(element), indent(indent)
  665. { }
  666. TreeElement* element;
  667. UINT32 indent;
  668. };
  669. mVisibleElements.clear();
  670. Stack<UpdateTreeElement>::type todo;
  671. todo.push(UpdateTreeElement(&getRootElement(), 0));
  672. // NOTE - Instead of iterating through all elements, try to find those within the clip rect
  673. // and only iterate through those. Others should somehow be marked in-active (similar to GUIElement::isDisabled()?)
  674. Vector<TreeElement*>::type tempOrderedElements;
  675. Vector2I offset(x, y);
  676. while(!todo.empty())
  677. {
  678. UpdateTreeElement currentUpdateElement = todo.top();
  679. TreeElement* current = currentUpdateElement.element;
  680. UINT32 indent = currentUpdateElement.indent;
  681. todo.pop();
  682. INT32 btnHeight = 0;
  683. INT32 yOffset = 0;
  684. if(current->mElement != nullptr)
  685. {
  686. Vector2I elementSize = current->mElement->_getOptimalSize();
  687. btnHeight = elementSize.y;
  688. mVisibleElements.push_back(InteractableElement(current->mParent, current->mSortedIdx * 2 + 0, RectI(x, offset.y, width, ELEMENT_EXTRA_SPACING)));
  689. mVisibleElements.push_back(InteractableElement(current->mParent, current->mSortedIdx * 2 + 1, RectI(x, offset.y + ELEMENT_EXTRA_SPACING, width, btnHeight)));
  690. offset.x = x + INITIAL_INDENT_OFFSET + indent * INDENT_SIZE;
  691. offset.y += ELEMENT_EXTRA_SPACING;
  692. current->mElement->_setOffset(offset);
  693. current->mElement->_setWidth(elementSize.x);
  694. current->mElement->_setHeight(elementSize.y);
  695. current->mElement->_setAreaDepth(areaDepth);
  696. current->mElement->_setWidgetDepth(widgetDepth);
  697. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  698. current->mElement->_setClipRect(elemClipRect);
  699. yOffset = btnHeight;
  700. }
  701. if(current->mFoldoutBtn != nullptr)
  702. {
  703. Vector2I elementSize = current->mFoldoutBtn->_getOptimalSize();
  704. offset.x -= std::min((INT32)INITIAL_INDENT_OFFSET, elementSize.x);
  705. Vector2I myOffset = offset;
  706. myOffset.y -= 2; // TODO: Arbitrary offset, I should adjust it based on font baseline so that the button is nicely centered on text
  707. if(elementSize.y > btnHeight)
  708. {
  709. UINT32 diff = elementSize.y - btnHeight;
  710. float half = diff * 0.5f;
  711. myOffset.y -= Math::floorToInt(half);
  712. }
  713. current->mFoldoutBtn->_setOffset(myOffset);
  714. current->mFoldoutBtn->_setWidth(elementSize.x);
  715. current->mFoldoutBtn->_setHeight(elementSize.y);
  716. current->mFoldoutBtn->_setAreaDepth(areaDepth);
  717. current->mFoldoutBtn->_setWidgetDepth(widgetDepth);
  718. RectI elemClipRect(clipRect.x - myOffset.x, clipRect.y - myOffset.y, clipRect.width, clipRect.height);
  719. current->mFoldoutBtn->_setClipRect(elemClipRect);
  720. }
  721. offset.y += yOffset;
  722. tempOrderedElements.resize(current->mChildren.size(), nullptr);
  723. for(auto& child : current->mChildren)
  724. {
  725. tempOrderedElements[child->mSortedIdx] = child;
  726. }
  727. for(auto iter = tempOrderedElements.rbegin(); iter != tempOrderedElements.rend(); ++iter)
  728. {
  729. TreeElement* child = *iter;
  730. if(!child->mIsVisible)
  731. continue;
  732. todo.push(UpdateTreeElement(child, indent + 1));
  733. }
  734. }
  735. UINT32 remainingHeight = (UINT32)std::max(0, (INT32)height - (offset.y - y));
  736. if(remainingHeight > 0)
  737. mVisibleElements.push_back(InteractableElement(&getRootElement(), (UINT32)getRootElement().mChildren.size() * 2, RectI(x, offset.y, width, remainingHeight)));
  738. for(auto selectedElem : mSelectedElements)
  739. {
  740. GUILabel* targetElement = selectedElem.element->mElement;
  741. Vector2I offset = targetElement->_getOffset();
  742. offset.x = x;
  743. selectedElem.background->_setOffset(offset);
  744. selectedElem.background->_setWidth(width);
  745. selectedElem.background->_setHeight(targetElement->_getHeight());
  746. selectedElem.background->_setAreaDepth(areaDepth + 1);
  747. selectedElem.background->_setWidgetDepth(widgetDepth);
  748. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  749. selectedElem.background->_setClipRect(elemClipRect);
  750. }
  751. if(mEditElement != nullptr)
  752. {
  753. GUILabel* targetElement = mEditElement->mElement;
  754. Vector2I offset = targetElement->_getOffset();
  755. UINT32 remainingWidth = (UINT32)std::max(0, (((INT32)width) - (offset.x - x)));
  756. mNameEditBox->_setOffset(offset);
  757. mNameEditBox->_setWidth(remainingWidth);
  758. mNameEditBox->_setHeight(targetElement->_getHeight());
  759. mNameEditBox->_setAreaDepth(areaDepth);
  760. mNameEditBox->_setWidgetDepth(widgetDepth);
  761. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  762. mNameEditBox->_setClipRect(elemClipRect);
  763. }
  764. if(mDragInProgress)
  765. {
  766. const InteractableElement* interactableElement = findElementUnderCoord(mDragPosition);
  767. if(interactableElement == nullptr)
  768. {
  769. if(!mDragHighlight->_isDisabled())
  770. mDragHighlight->disableRecursively();
  771. if(!mDragSepHighlight->_isDisabled())
  772. mDragSepHighlight->disableRecursively();
  773. }
  774. else
  775. {
  776. if(interactableElement->isTreeElement())
  777. {
  778. if(!mDragSepHighlight->_isDisabled())
  779. mDragSepHighlight->disableRecursively();
  780. if(mDragHighlight->_isDisabled())
  781. mDragHighlight->enableRecursively();
  782. Vector2I offset(interactableElement->bounds.x, interactableElement->bounds.y);
  783. mDragHighlight->_setOffset(offset);
  784. mDragHighlight->_setWidth(interactableElement->bounds.width);
  785. mDragHighlight->_setHeight(interactableElement->bounds.height);
  786. mDragHighlight->_setAreaDepth(areaDepth + 1);
  787. mDragHighlight->_setWidgetDepth(widgetDepth);
  788. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  789. mDragHighlight->_setClipRect(elemClipRect);
  790. }
  791. else
  792. {
  793. if(!mDragHighlight->_isDisabled())
  794. mDragHighlight->disableRecursively();
  795. if(mDragSepHighlight->_isDisabled())
  796. mDragSepHighlight->enableRecursively();
  797. Vector2I offset(interactableElement->bounds.x, interactableElement->bounds.y);
  798. mDragSepHighlight->_setOffset(offset);
  799. mDragSepHighlight->_setWidth(interactableElement->bounds.width);
  800. mDragSepHighlight->_setHeight(interactableElement->bounds.height);
  801. mDragSepHighlight->_setAreaDepth(areaDepth + 1);
  802. mDragSepHighlight->_setWidgetDepth(widgetDepth);
  803. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  804. mDragSepHighlight->_setClipRect(elemClipRect);
  805. }
  806. }
  807. }
  808. else
  809. {
  810. if(!mDragHighlight->_isDisabled())
  811. mDragHighlight->disableRecursively();
  812. if(!mDragSepHighlight->_isDisabled())
  813. mDragSepHighlight->disableRecursively();
  814. }
  815. // Update scroll bounds
  816. UINT32 scrollHeight = (UINT32)Math::roundToInt(clipRect.height * SCROLL_AREA_HEIGHT_PCT);
  817. mTopScrollBounds.x = clipRect.x;
  818. mTopScrollBounds.y = clipRect.y;
  819. mTopScrollBounds.width = clipRect.width;
  820. mTopScrollBounds.height = scrollHeight;
  821. mBottomScrollBounds.x = clipRect.x;
  822. mBottomScrollBounds.y = clipRect.y + clipRect.height - scrollHeight;
  823. mBottomScrollBounds.width = clipRect.width;
  824. mBottomScrollBounds.height = scrollHeight;
  825. }
  826. const GUITreeView::InteractableElement* GUITreeView::findElementUnderCoord(const CM::Vector2I& coord) const
  827. {
  828. for(auto& element : mVisibleElements)
  829. {
  830. if(element.bounds.contains(coord))
  831. {
  832. return &element;
  833. }
  834. }
  835. return nullptr;
  836. }
  837. GUITreeView::TreeElement* GUITreeView::getTopMostSelectedElement() const
  838. {
  839. auto topMostElement = mVisibleElements.end();
  840. for(auto& selectedElement : mSelectedElements)
  841. {
  842. auto iterFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  843. [&] (const InteractableElement& x) { return x.getTreeElement() == selectedElement.element; });
  844. if(iterFind != mVisibleElements.end())
  845. {
  846. if(topMostElement == mVisibleElements.end())
  847. topMostElement = iterFind;
  848. else
  849. {
  850. if(iterFind->bounds.y < topMostElement->bounds.y)
  851. topMostElement = iterFind;
  852. }
  853. }
  854. }
  855. if(topMostElement != mVisibleElements.end())
  856. return topMostElement->getTreeElement();
  857. else
  858. return nullptr;
  859. }
  860. GUITreeView::TreeElement* GUITreeView::getBottomMostSelectedElement() const
  861. {
  862. auto& botMostElement = 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(botMostElement == mVisibleElements.end())
  870. botMostElement = iterFind;
  871. else
  872. {
  873. if((iterFind->bounds.y + iterFind->bounds.height) > (botMostElement->bounds.y + botMostElement->bounds.height))
  874. botMostElement = iterFind;
  875. }
  876. }
  877. }
  878. if(botMostElement != mVisibleElements.end())
  879. return botMostElement->getTreeElement();
  880. else
  881. return nullptr;
  882. }
  883. void GUITreeView::closeTemporarilyExpandedElements()
  884. {
  885. temporarilyExpandElement(nullptr);
  886. }
  887. void GUITreeView::temporarilyExpandElement(const GUITreeView::InteractableElement* mouseOverElement)
  888. {
  889. TreeElement* treeElement = nullptr;
  890. if(mouseOverElement != nullptr && mouseOverElement->isTreeElement())
  891. treeElement = mouseOverElement->getTreeElement();
  892. if(treeElement == nullptr || treeElement != mMouseOverDragElement)
  893. {
  894. while(!mAutoExpandedElements.empty())
  895. {
  896. TreeElement* autoExpandedElement = mAutoExpandedElements.top();
  897. bool unexpandElement = false;
  898. if(mouseOverElement != nullptr && mouseOverElement->parent != nullptr)
  899. {
  900. if(mouseOverElement->parent != autoExpandedElement && !mouseOverElement->parent->isParentRec(autoExpandedElement))
  901. unexpandElement = true;
  902. else
  903. break;
  904. }
  905. else
  906. unexpandElement = true;
  907. if(unexpandElement)
  908. {
  909. collapseElement(autoExpandedElement);
  910. if(autoExpandedElement->mFoldoutBtn != nullptr)
  911. autoExpandedElement->mFoldoutBtn->toggleOff();
  912. mAutoExpandedElements.pop();
  913. }
  914. }
  915. mMouseOverDragElement = treeElement;
  916. mMouseOverDragElementTime = gTime().getTime();
  917. }
  918. else
  919. {
  920. if(mMouseOverDragElement != nullptr && !mMouseOverDragElement->mIsExpanded)
  921. {
  922. float timeDiff = gTime().getTime() - mMouseOverDragElementTime;
  923. if(timeDiff >= AUTO_EXPAND_DELAY_SEC)
  924. {
  925. mAutoExpandedElements.push(mMouseOverDragElement);
  926. expandElement(mMouseOverDragElement);
  927. if(mMouseOverDragElement->mFoldoutBtn != nullptr)
  928. mMouseOverDragElement->mFoldoutBtn->toggleOn();
  929. }
  930. }
  931. }
  932. }
  933. void GUITreeView::scrollToElement(TreeElement* element, bool center)
  934. {
  935. if(element->mElement == nullptr)
  936. return;
  937. GUIScrollArea* scrollArea = findParentScrollArea();
  938. if(scrollArea == nullptr)
  939. return;
  940. if(center)
  941. {
  942. RectI myBounds = _getClippedBounds();
  943. INT32 clipVertCenter = myBounds.y + (INT32)Math::roundToInt(myBounds.height * 0.5f);
  944. INT32 elemVertCenter = element->mElement->_getOffset().y + (INT32)Math::roundToInt(element->mElement->_getHeight() * 0.5f);
  945. if(elemVertCenter > clipVertCenter)
  946. scrollArea->scrollUpPx(elemVertCenter - clipVertCenter);
  947. else
  948. scrollArea->scrollDownPx(clipVertCenter - elemVertCenter);
  949. }
  950. else
  951. {
  952. RectI myBounds = _getClippedBounds();
  953. INT32 elemVertTop = element->mElement->_getOffset().y;
  954. INT32 elemVertBottom = element->mElement->_getOffset().y + element->mElement->_getHeight();
  955. INT32 top = myBounds.y;
  956. INT32 bottom = myBounds.y + myBounds.height;
  957. INT32 offset = 0;
  958. if(elemVertTop < top)
  959. scrollArea->scrollUpPx(top - elemVertTop);
  960. else if(elemVertBottom > bottom)
  961. scrollArea->scrollDownPx(elemVertBottom - bottom);
  962. }
  963. }
  964. GUIScrollArea* GUITreeView::findParentScrollArea() const
  965. {
  966. GUIElementBase* parent = _getParent();
  967. while(parent != nullptr)
  968. {
  969. if(parent->_getType() == GUIElementBase::Type::Element)
  970. {
  971. GUIElement* parentElement = static_cast<GUIElement*>(parent);
  972. if(parentElement->getElementType() == GUIElement::ElementType::ScrollArea)
  973. {
  974. GUIScrollArea* scrollArea = static_cast<GUIScrollArea*>(parentElement);
  975. return scrollArea;
  976. }
  977. }
  978. parent = parent->_getParent();
  979. }
  980. return nullptr;
  981. }
  982. const String& GUITreeView::getGUITypeName()
  983. {
  984. static String typeName = "SceneTreeView";
  985. return typeName;
  986. }
  987. }