BsGUITreeView.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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->_getCachedBounds().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 false;
  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. Vector2I offset = _getOffset();
  676. mClippedBounds = Rect2I(offset.x, offset.y, _getWidth(), _getHeight());
  677. Rect2I localClipRect(mClipRect.x + mOffset.x, mClipRect.y + mOffset.y, mClipRect.width, mClipRect.height);
  678. mClippedBounds.clip(localClipRect);
  679. }
  680. void GUITreeView::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  681. Rect2I clipRect, UINT8 widgetDepth, INT16 panelDepth, UINT16 panelDepthRangeMin, UINT16 panelDepthRangeMax)
  682. {
  683. struct UpdateTreeElement
  684. {
  685. UpdateTreeElement(TreeElement* element, UINT32 indent)
  686. :element(element), indent(indent)
  687. { }
  688. TreeElement* element;
  689. UINT32 indent;
  690. };
  691. mVisibleElements.clear();
  692. Stack<UpdateTreeElement> todo;
  693. todo.push(UpdateTreeElement(&getRootElement(), 0));
  694. // NOTE - Instead of iterating through all elements, try to find those within the clip rect
  695. // and only iterate through those. Others should somehow be marked in-active (similar to GUIElement::isDisabled()?)
  696. Vector<TreeElement*> tempOrderedElements;
  697. Vector2I offset(x, y);
  698. while(!todo.empty())
  699. {
  700. UpdateTreeElement currentUpdateElement = todo.top();
  701. TreeElement* current = currentUpdateElement.element;
  702. UINT32 indent = currentUpdateElement.indent;
  703. todo.pop();
  704. INT32 btnHeight = 0;
  705. INT32 yOffset = 0;
  706. if(current->mElement != nullptr)
  707. {
  708. Vector2I elementSize = current->mElement->_getOptimalSize();
  709. btnHeight = elementSize.y;
  710. mVisibleElements.push_back(InteractableElement(current->mParent, current->mSortedIdx * 2 + 0, Rect2I(x, offset.y, width, ELEMENT_EXTRA_SPACING)));
  711. mVisibleElements.push_back(InteractableElement(current->mParent, current->mSortedIdx * 2 + 1, Rect2I(x, offset.y + ELEMENT_EXTRA_SPACING, width, btnHeight)));
  712. offset.x = x + INITIAL_INDENT_OFFSET + indent * INDENT_SIZE;
  713. offset.y += ELEMENT_EXTRA_SPACING;
  714. current->mElement->_setPosition(offset);
  715. current->mElement->_setWidth(elementSize.x);
  716. current->mElement->_setHeight(elementSize.y);
  717. current->mElement->_setAreaDepth(panelDepth);
  718. current->mElement->_setWidgetDepth(widgetDepth);
  719. Rect2I elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  720. current->mElement->_setClipRect(elemClipRect);
  721. yOffset = btnHeight;
  722. }
  723. if(current->mFoldoutBtn != nullptr)
  724. {
  725. Vector2I elementSize = current->mFoldoutBtn->_getOptimalSize();
  726. offset.x -= std::min((INT32)INITIAL_INDENT_OFFSET, elementSize.x);
  727. Vector2I myOffset = offset;
  728. myOffset.y -= 2; // TODO: Arbitrary offset, I should adjust it based on font baseline so that the button is nicely centered on text
  729. if(elementSize.y > btnHeight)
  730. {
  731. UINT32 diff = elementSize.y - btnHeight;
  732. float half = diff * 0.5f;
  733. myOffset.y -= Math::floorToInt(half);
  734. }
  735. current->mFoldoutBtn->_setPosition(myOffset);
  736. current->mFoldoutBtn->_setWidth(elementSize.x);
  737. current->mFoldoutBtn->_setHeight(elementSize.y);
  738. current->mFoldoutBtn->_setAreaDepth(panelDepth);
  739. current->mFoldoutBtn->_setWidgetDepth(widgetDepth);
  740. Rect2I elemClipRect(clipRect.x - myOffset.x, clipRect.y - myOffset.y, clipRect.width, clipRect.height);
  741. current->mFoldoutBtn->_setClipRect(elemClipRect);
  742. }
  743. offset.y += yOffset;
  744. tempOrderedElements.resize(current->mChildren.size(), nullptr);
  745. for(auto& child : current->mChildren)
  746. {
  747. tempOrderedElements[child->mSortedIdx] = child;
  748. }
  749. for(auto iter = tempOrderedElements.rbegin(); iter != tempOrderedElements.rend(); ++iter)
  750. {
  751. TreeElement* child = *iter;
  752. if(!child->mIsVisible)
  753. continue;
  754. todo.push(UpdateTreeElement(child, indent + 1));
  755. }
  756. }
  757. UINT32 remainingHeight = (UINT32)std::max(0, (INT32)height - (offset.y - y));
  758. if(remainingHeight > 0)
  759. mVisibleElements.push_back(InteractableElement(&getRootElement(), (UINT32)getRootElement().mChildren.size() * 2, Rect2I(x, offset.y, width, remainingHeight)));
  760. for(auto selectedElem : mSelectedElements)
  761. {
  762. GUILabel* targetElement = selectedElem.element->mElement;
  763. Vector2I offset = targetElement->_getOffset();
  764. offset.x = x;
  765. selectedElem.background->_setPosition(offset);
  766. selectedElem.background->_setWidth(width);
  767. selectedElem.background->_setHeight(targetElement->_getHeight());
  768. selectedElem.background->_setAreaDepth(panelDepth);
  769. selectedElem.background->_setWidgetDepth(widgetDepth);
  770. Rect2I elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  771. selectedElem.background->_setClipRect(elemClipRect);
  772. }
  773. if(mEditElement != nullptr)
  774. {
  775. GUILabel* targetElement = mEditElement->mElement;
  776. Vector2I offset = targetElement->_getOffset();
  777. UINT32 remainingWidth = (UINT32)std::max(0, (((INT32)width) - (offset.x - x)));
  778. mNameEditBox->_setPosition(offset);
  779. mNameEditBox->_setWidth(remainingWidth);
  780. mNameEditBox->_setHeight(targetElement->_getHeight());
  781. mNameEditBox->_setAreaDepth(panelDepth);
  782. mNameEditBox->_setWidgetDepth(widgetDepth);
  783. Rect2I elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  784. mNameEditBox->_setClipRect(elemClipRect);
  785. }
  786. if(mDragInProgress)
  787. {
  788. const InteractableElement* interactableElement = findElementUnderCoord(mDragPosition);
  789. if(interactableElement == nullptr)
  790. {
  791. if(!mDragHighlight->_isDisabled())
  792. mDragHighlight->disableRecursively();
  793. if(!mDragSepHighlight->_isDisabled())
  794. mDragSepHighlight->disableRecursively();
  795. }
  796. else
  797. {
  798. if(interactableElement->isTreeElement())
  799. {
  800. if(!mDragSepHighlight->_isDisabled())
  801. mDragSepHighlight->disableRecursively();
  802. if(mDragHighlight->_isDisabled())
  803. mDragHighlight->enableRecursively();
  804. Vector2I offset(interactableElement->bounds.x, interactableElement->bounds.y);
  805. mDragHighlight->_setPosition(offset);
  806. mDragHighlight->_setWidth(interactableElement->bounds.width);
  807. mDragHighlight->_setHeight(interactableElement->bounds.height);
  808. mDragHighlight->_setAreaDepth(panelDepth);
  809. mDragHighlight->_setWidgetDepth(widgetDepth);
  810. Rect2I elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  811. mDragHighlight->_setClipRect(elemClipRect);
  812. }
  813. else
  814. {
  815. if(!mDragHighlight->_isDisabled())
  816. mDragHighlight->disableRecursively();
  817. if(mDragSepHighlight->_isDisabled())
  818. mDragSepHighlight->enableRecursively();
  819. Vector2I offset(interactableElement->bounds.x, interactableElement->bounds.y);
  820. mDragSepHighlight->_setPosition(offset);
  821. mDragSepHighlight->_setWidth(interactableElement->bounds.width);
  822. mDragSepHighlight->_setHeight(interactableElement->bounds.height);
  823. mDragSepHighlight->_setAreaDepth(panelDepth);
  824. mDragSepHighlight->_setWidgetDepth(widgetDepth);
  825. Rect2I elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  826. mDragSepHighlight->_setClipRect(elemClipRect);
  827. }
  828. }
  829. }
  830. else
  831. {
  832. if(!mDragHighlight->_isDisabled())
  833. mDragHighlight->disableRecursively();
  834. if(!mDragSepHighlight->_isDisabled())
  835. mDragSepHighlight->disableRecursively();
  836. }
  837. // Update scroll bounds
  838. UINT32 scrollHeight = (UINT32)Math::roundToInt(clipRect.height * SCROLL_AREA_HEIGHT_PCT);
  839. mTopScrollBounds.x = clipRect.x;
  840. mTopScrollBounds.y = clipRect.y;
  841. mTopScrollBounds.width = clipRect.width;
  842. mTopScrollBounds.height = scrollHeight;
  843. mBottomScrollBounds.x = clipRect.x;
  844. mBottomScrollBounds.y = clipRect.y + clipRect.height - scrollHeight;
  845. mBottomScrollBounds.width = clipRect.width;
  846. mBottomScrollBounds.height = scrollHeight;
  847. }
  848. const GUITreeView::InteractableElement* GUITreeView::findElementUnderCoord(const Vector2I& coord) const
  849. {
  850. for(auto& element : mVisibleElements)
  851. {
  852. if(element.bounds.contains(coord))
  853. {
  854. return &element;
  855. }
  856. }
  857. return nullptr;
  858. }
  859. GUITreeView::TreeElement* GUITreeView::getTopMostSelectedElement() const
  860. {
  861. auto topMostElement = mVisibleElements.end();
  862. for(auto& selectedElement : mSelectedElements)
  863. {
  864. auto iterFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  865. [&] (const InteractableElement& x) { return x.getTreeElement() == selectedElement.element; });
  866. if(iterFind != mVisibleElements.end())
  867. {
  868. if(topMostElement == mVisibleElements.end())
  869. topMostElement = iterFind;
  870. else
  871. {
  872. if(iterFind->bounds.y < topMostElement->bounds.y)
  873. topMostElement = iterFind;
  874. }
  875. }
  876. }
  877. if(topMostElement != mVisibleElements.end())
  878. return topMostElement->getTreeElement();
  879. else
  880. return nullptr;
  881. }
  882. GUITreeView::TreeElement* GUITreeView::getBottomMostSelectedElement() const
  883. {
  884. auto& botMostElement = mVisibleElements.end();
  885. for(auto& selectedElement : mSelectedElements)
  886. {
  887. auto iterFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  888. [&] (const InteractableElement& x) { return x.getTreeElement() == selectedElement.element; });
  889. if(iterFind != mVisibleElements.end())
  890. {
  891. if(botMostElement == mVisibleElements.end())
  892. botMostElement = iterFind;
  893. else
  894. {
  895. if((iterFind->bounds.y + iterFind->bounds.height) > (botMostElement->bounds.y + botMostElement->bounds.height))
  896. botMostElement = iterFind;
  897. }
  898. }
  899. }
  900. if(botMostElement != mVisibleElements.end())
  901. return botMostElement->getTreeElement();
  902. else
  903. return nullptr;
  904. }
  905. void GUITreeView::closeTemporarilyExpandedElements()
  906. {
  907. temporarilyExpandElement(nullptr);
  908. }
  909. void GUITreeView::temporarilyExpandElement(const GUITreeView::InteractableElement* mouseOverElement)
  910. {
  911. TreeElement* treeElement = nullptr;
  912. if(mouseOverElement != nullptr && mouseOverElement->isTreeElement())
  913. treeElement = mouseOverElement->getTreeElement();
  914. if(treeElement == nullptr || treeElement != mMouseOverDragElement)
  915. {
  916. while(!mAutoExpandedElements.empty())
  917. {
  918. TreeElement* autoExpandedElement = mAutoExpandedElements.top();
  919. bool unexpandElement = false;
  920. if(mouseOverElement != nullptr && mouseOverElement->parent != nullptr)
  921. {
  922. if(mouseOverElement->parent != autoExpandedElement && !mouseOverElement->parent->isParentRec(autoExpandedElement))
  923. unexpandElement = true;
  924. else
  925. break;
  926. }
  927. else
  928. unexpandElement = true;
  929. if(unexpandElement)
  930. {
  931. collapseElement(autoExpandedElement);
  932. if(autoExpandedElement->mFoldoutBtn != nullptr)
  933. autoExpandedElement->mFoldoutBtn->toggleOff();
  934. mAutoExpandedElements.pop();
  935. }
  936. }
  937. mMouseOverDragElement = treeElement;
  938. mMouseOverDragElementTime = gTime().getTime();
  939. }
  940. else
  941. {
  942. if(mMouseOverDragElement != nullptr && !mMouseOverDragElement->mIsExpanded)
  943. {
  944. float timeDiff = gTime().getTime() - mMouseOverDragElementTime;
  945. if(timeDiff >= AUTO_EXPAND_DELAY_SEC)
  946. {
  947. mAutoExpandedElements.push(mMouseOverDragElement);
  948. expandElement(mMouseOverDragElement);
  949. if(mMouseOverDragElement->mFoldoutBtn != nullptr)
  950. mMouseOverDragElement->mFoldoutBtn->toggleOn();
  951. }
  952. }
  953. }
  954. }
  955. void GUITreeView::scrollToElement(TreeElement* element, bool center)
  956. {
  957. if(element->mElement == nullptr)
  958. return;
  959. GUIScrollArea* scrollArea = findParentScrollArea();
  960. if(scrollArea == nullptr)
  961. return;
  962. if(center)
  963. {
  964. Rect2I myBounds = _getClippedBounds();
  965. INT32 clipVertCenter = myBounds.y + (INT32)Math::roundToInt(myBounds.height * 0.5f);
  966. INT32 elemVertCenter = element->mElement->_getOffset().y + (INT32)Math::roundToInt(element->mElement->_getHeight() * 0.5f);
  967. if(elemVertCenter > clipVertCenter)
  968. scrollArea->scrollUpPx(elemVertCenter - clipVertCenter);
  969. else
  970. scrollArea->scrollDownPx(clipVertCenter - elemVertCenter);
  971. }
  972. else
  973. {
  974. Rect2I myBounds = _getClippedBounds();
  975. INT32 elemVertTop = element->mElement->_getOffset().y;
  976. INT32 elemVertBottom = element->mElement->_getOffset().y + element->mElement->_getHeight();
  977. INT32 top = myBounds.y;
  978. INT32 bottom = myBounds.y + myBounds.height;
  979. INT32 offset = 0;
  980. if(elemVertTop < top)
  981. scrollArea->scrollUpPx(top - elemVertTop);
  982. else if(elemVertBottom > bottom)
  983. scrollArea->scrollDownPx(elemVertBottom - bottom);
  984. }
  985. }
  986. GUIScrollArea* GUITreeView::findParentScrollArea() const
  987. {
  988. GUIElementBase* parent = _getParent();
  989. while(parent != nullptr)
  990. {
  991. if(parent->_getType() == GUIElementBase::Type::Element)
  992. {
  993. GUIElement* parentElement = static_cast<GUIElement*>(parent);
  994. if(parentElement->_getElementType() == GUIElement::ElementType::ScrollArea)
  995. {
  996. GUIScrollArea* scrollArea = static_cast<GUIScrollArea*>(parentElement);
  997. return scrollArea;
  998. }
  999. }
  1000. parent = parent->_getParent();
  1001. }
  1002. return nullptr;
  1003. }
  1004. const String& GUITreeView::getGUITypeName()
  1005. {
  1006. static String typeName = "SceneTreeView";
  1007. return typeName;
  1008. }
  1009. }