BsGUISceneTreeView.cpp 40 KB

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