BsGUITreeView.cpp 35 KB

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