BsGUITreeView.cpp 33 KB

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