BsGUITreeView.cpp 33 KB

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