BsGUITreeView.cpp 34 KB

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