BsGUITreeView.cpp 33 KB

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