BsGUISceneTreeView.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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 "CmSceneObject.h"
  15. #include "CmSceneManager.h"
  16. #include "BsCmdEditPlainFieldGO.h"
  17. #include "BsDragAndDropManager.h"
  18. #include "BsCmdReparentSO.h"
  19. #include "CmTime.h"
  20. #include "BsGUIScrollArea.h"
  21. using namespace CamelotFramework;
  22. using namespace BansheeEngine;
  23. namespace BansheeEditor
  24. {
  25. const UINT32 GUISceneTreeView::ELEMENT_EXTRA_SPACING = 3;
  26. const UINT32 GUISceneTreeView::INDENT_SIZE = 10;
  27. const UINT32 GUISceneTreeView::INITIAL_INDENT_OFFSET = 16;
  28. const UINT32 GUISceneTreeView::DRAG_MIN_DISTANCE = 3;
  29. const float GUISceneTreeView::AUTO_EXPAND_DELAY_SEC = 0.5f;
  30. const float GUISceneTreeView::SCROLL_AREA_HEIGHT_PCT = 0.1f;
  31. const UINT32 GUISceneTreeView::SCROLL_SPEED_PX_PER_SEC = 25;
  32. GUISceneTreeView::DraggedSceneObjects::DraggedSceneObjects(UINT32 numObjects)
  33. :numObjects(numObjects)
  34. {
  35. objects = cm_newN<HSceneObject>(numObjects);
  36. }
  37. GUISceneTreeView::DraggedSceneObjects::~DraggedSceneObjects()
  38. {
  39. cm_deleteN(objects, numObjects);
  40. objects = nullptr;
  41. }
  42. GUISceneTreeView::TreeElement::TreeElement()
  43. :mParent(nullptr), mFoldoutBtn(nullptr), mElement(nullptr), mIsSelected(false),
  44. mId(0), mIsExpanded(false), mSortedIdx(0), mIsDirty(false), mIsVisible(true)
  45. { }
  46. GUISceneTreeView::TreeElement::~TreeElement()
  47. {
  48. for(auto& child : mChildren)
  49. cm_delete(child);
  50. if(mFoldoutBtn != nullptr)
  51. GUIElement::destroy(mFoldoutBtn);
  52. if(mElement != nullptr)
  53. GUIElement::destroy(mElement);
  54. mChildren.clear();
  55. }
  56. bool GUISceneTreeView::TreeElement::isParentRec(TreeElement* element) const
  57. {
  58. TreeElement* curParent = mParent;
  59. while(curParent != nullptr)
  60. {
  61. if(curParent == element)
  62. return true;
  63. curParent = curParent->mParent;
  64. }
  65. return false;
  66. }
  67. GUISceneTreeView::TreeElement* GUISceneTreeView::InteractableElement::getTreeElement() const
  68. {
  69. if(!isTreeElement())
  70. return nullptr;
  71. UINT32 sortedIdx = (index - 1) / 2;
  72. auto findIter = std::find_if(parent->mChildren.begin(), parent->mChildren.end(),
  73. [&](const TreeElement* x) { return x->mSortedIdx == sortedIdx; });
  74. if(findIter != parent->mChildren.end())
  75. return *findIter;
  76. return nullptr;
  77. }
  78. GUISceneTreeView::GUISceneTreeView(GUIWidget& parent, GUIElementStyle* backgroundStyle, GUIElementStyle* elementBtnStyle,
  79. GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle, GUIElementStyle* editBoxStyle,
  80. BS::GUIElementStyle* dragHighlightStyle, BS::GUIElementStyle* dragSepHighlightStyle, const GUILayoutOptions& layoutOptions)
  81. :GUIElementContainer(parent, layoutOptions), mBackgroundStyle(backgroundStyle),
  82. mElementBtnStyle(elementBtnStyle), mFoldoutBtnStyle(foldoutBtnStyle), mEditBoxStyle(editBoxStyle), mEditElement(nullptr), mIsElementSelected(false),
  83. mNameEditBox(nullptr), mSelectionBackgroundStyle(selectionBackgroundStyle), mDragInProgress(nullptr), mDragHighlightStyle(dragHighlightStyle),
  84. mDragSepHighlightStyle(dragSepHighlightStyle), mDragHighlight(nullptr), mDragSepHighlight(nullptr), mMouseOverDragElement(nullptr), mMouseOverDragElementTime(0.0f),
  85. mScrollState(ScrollState::None), mLastScrollTime(0.0f)
  86. {
  87. if(mBackgroundStyle == nullptr)
  88. mBackgroundStyle = parent.getSkin().getStyle("TreeViewBackground");
  89. if(mElementBtnStyle == nullptr)
  90. mElementBtnStyle = parent.getSkin().getStyle("TreeViewElementBtn");
  91. if(mFoldoutBtnStyle == nullptr)
  92. mFoldoutBtnStyle = parent.getSkin().getStyle("TreeViewFoldoutBtn");
  93. if(mSelectionBackgroundStyle == nullptr)
  94. mSelectionBackgroundStyle = parent.getSkin().getStyle("TreeViewSelectionBackground");
  95. if(mEditBoxStyle == nullptr)
  96. mEditBoxStyle = parent.getSkin().getStyle("TreeViewEditBox");
  97. if(mDragHighlightStyle == nullptr)
  98. mDragHighlightStyle = parent.getSkin().getStyle("TreeViewElementHighlight");
  99. if(mDragSepHighlightStyle == nullptr)
  100. mDragSepHighlightStyle = parent.getSkin().getStyle("TreeViewElementSepHighlight");
  101. mBackgroundImage = GUITexture::create(parent, mBackgroundStyle);
  102. mNameEditBox = GUITreeViewEditBox::create(parent, mEditBoxStyle);
  103. mNameEditBox->disableRecursively();
  104. mNameEditBox->onInputConfirmed.connect(boost::bind(&GUISceneTreeView::onEditAccepted, this));
  105. mNameEditBox->onInputCanceled.connect(boost::bind(&GUISceneTreeView::onEditCanceled, this));
  106. mDragHighlight = GUITexture::create(parent, mDragHighlightStyle);
  107. mDragSepHighlight = GUITexture::create(parent, mDragSepHighlightStyle);
  108. mDragHighlight->disableRecursively();
  109. mDragSepHighlight->disableRecursively();
  110. _registerChildElement(mBackgroundImage);
  111. _registerChildElement(mNameEditBox);
  112. _registerChildElement(mDragHighlight);
  113. _registerChildElement(mDragSepHighlight);
  114. }
  115. GUISceneTreeView::~GUISceneTreeView()
  116. {
  117. }
  118. GUISceneTreeView* GUISceneTreeView::create(GUIWidget& parent, GUIElementStyle* backgroundStyle, GUIElementStyle* elementBtnStyle,
  119. GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle, GUIElementStyle* editBoxStyle, GUIElementStyle* dragHighlightStyle,
  120. GUIElementStyle* dragSepHighlightStyle)
  121. {
  122. return new (cm_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(parent, backgroundStyle, elementBtnStyle, foldoutBtnStyle,
  123. selectionBackgroundStyle, editBoxStyle, dragHighlightStyle, dragSepHighlightStyle, GUILayoutOptions::create(&GUISkin::DefaultStyle));
  124. }
  125. GUISceneTreeView* GUISceneTreeView::create(GUIWidget& parent, const GUIOptions& options, GUIElementStyle* backgroundStyle,
  126. GUIElementStyle* elementBtnStyle, GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle,
  127. GUIElementStyle* editBoxStyle, GUIElementStyle* dragHighlightStyle, GUIElementStyle* dragSepHighlightStyle)
  128. {
  129. return new (cm_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(parent, backgroundStyle, elementBtnStyle,
  130. foldoutBtnStyle, selectionBackgroundStyle, editBoxStyle, dragHighlightStyle, dragSepHighlightStyle, GUILayoutOptions::create(options, &GUISkin::DefaultStyle));
  131. }
  132. void GUISceneTreeView::update()
  133. {
  134. // Attempt to auto-expand elements we are dragging over
  135. if(DragAndDropManager::instance().isDragInProgress() && DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::SceneObject)
  136. {
  137. const GUISceneTreeView::InteractableElement* element = findElementUnderCoord(mDragPosition);
  138. temporarilyExpandElement(element);
  139. }
  140. // NOTE - Instead of iterating through every visible element and comparing it with internal values,
  141. // I might just want to add callbacks to SceneManager that notify me of any changes and then only perform
  142. // update if anything is actually dirty
  143. struct UpdateTreeElement
  144. {
  145. UpdateTreeElement(TreeElement* element, UINT32 seqIdx, bool visible)
  146. :element(element), seqIdx(seqIdx), visible(visible)
  147. { }
  148. TreeElement* element;
  149. UINT32 seqIdx;
  150. bool visible;
  151. };
  152. HSceneObject root = CM::gSceneManager().getRootNode();
  153. mRootElement.mSceneObject = root;
  154. mRootElement.mId = root->getId();
  155. mRootElement.mSortedIdx = 0;
  156. mRootElement.mIsExpanded = true;
  157. Stack<UpdateTreeElement>::type todo;
  158. todo.push(UpdateTreeElement(&mRootElement, 0, true));
  159. while(!todo.empty())
  160. {
  161. UpdateTreeElement updateElement = todo.top();
  162. TreeElement* current = updateElement.element;
  163. HSceneObject currentSO = current->mSceneObject;
  164. todo.pop();
  165. // Check if SceneObject has changed in any way and update the tree element
  166. if(updateElement.visible)
  167. {
  168. bool completeMatch = (UINT32)current->mChildren.size() == currentSO->getNumChildren();
  169. // Early exit case - Most commonly there will be no changes between active and cached data so
  170. // we first do a quick check in order to avoid expensive comparison later
  171. if(completeMatch)
  172. {
  173. for(UINT32 i = 0; i < currentSO->getNumChildren(); i++)
  174. {
  175. UINT32 curId = currentSO->getChild(i)->getId();
  176. if(curId != current->mChildren[i]->mId)
  177. {
  178. completeMatch = false;
  179. break;
  180. }
  181. }
  182. }
  183. // Not a complete match, compare everything and insert/delete elements as needed
  184. if(!completeMatch)
  185. {
  186. Vector<TreeElement*>::type newChildren;
  187. mTempToDelete.resize(current->mChildren.size());
  188. for(UINT32 i = 0; i < (UINT32)current->mChildren.size(); i++)
  189. mTempToDelete[i] = true;
  190. for(UINT32 i = 0; i < currentSO->getNumChildren(); i++)
  191. {
  192. HSceneObject currentSOChild = currentSO->getChild(i);
  193. UINT32 curId = currentSOChild->getId();
  194. bool found = false;
  195. for(UINT32 j = 0; j < current->mChildren.size(); j++)
  196. {
  197. TreeElement* currentChild = current->mChildren[j];
  198. if(curId == currentChild->mId)
  199. {
  200. mTempToDelete[j] = false;
  201. currentChild->mIsDirty = true;
  202. currentChild->mSortedIdx = (UINT32)newChildren.size();
  203. newChildren.push_back(currentChild);
  204. found = true;
  205. break;
  206. }
  207. }
  208. if(!found)
  209. {
  210. TreeElement* newChild = cm_new<TreeElement>();
  211. newChild->mParent = current;
  212. newChild->mSceneObject = currentSOChild;
  213. newChild->mId = currentSOChild->getId();
  214. newChild->mName = currentSOChild->getName();
  215. newChild->mSortedIdx = (UINT32)newChildren.size();
  216. newChild->mIsDirty = true;
  217. newChildren.push_back(newChild);
  218. }
  219. }
  220. for(UINT32 i = 0; i < current->mChildren.size(); i++)
  221. {
  222. if(!mTempToDelete[i])
  223. continue;
  224. if(current->mChildren[i]->mIsSelected)
  225. unselectElement(current->mChildren[i]);
  226. cm_delete(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::Rename)
  543. {
  544. if(isSelectionActive() && mEditElement == nullptr)
  545. {
  546. unselectAll();
  547. enableEdit(mSelectedElements[0].element);
  548. }
  549. return true;
  550. }
  551. if(ev.getType() == GUICommandEventType::CursorMoveUp || ev.getType() == GUICommandEventType::SelectUp)
  552. {
  553. TreeElement* topMostElement = getTopMostSelectedElement();
  554. auto topMostIter = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  555. [&] (const InteractableElement& x) { return x.getTreeElement() == topMostElement; });
  556. if(topMostIter != mVisibleElements.end() && topMostIter != mVisibleElements.begin())
  557. {
  558. do
  559. {
  560. topMostIter--;
  561. } while (!topMostIter->isTreeElement() && topMostIter != mVisibleElements.begin());
  562. if(topMostIter->isTreeElement())
  563. {
  564. if(ev.getType() == GUICommandEventType::CursorMoveUp)
  565. unselectAll();
  566. TreeElement* treeElement = topMostIter->getTreeElement();
  567. selectElement(treeElement);
  568. scrollToElement(treeElement, false);
  569. }
  570. }
  571. }
  572. else if(ev.getType() == GUICommandEventType::CursorMoveDown || ev.getType() == GUICommandEventType::SelectDown)
  573. {
  574. TreeElement* bottoMostElement = getBottomMostSelectedElement();
  575. auto bottomMostIter = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  576. [&] (const InteractableElement& x) { return x.getTreeElement() == bottoMostElement; });
  577. if(bottomMostIter != mVisibleElements.end())
  578. {
  579. do
  580. {
  581. bottomMostIter++;
  582. } while (bottomMostIter != mVisibleElements.end() && !bottomMostIter->isTreeElement());
  583. if(bottomMostIter != mVisibleElements.end() && bottomMostIter->isTreeElement())
  584. {
  585. if(ev.getType() == GUICommandEventType::CursorMoveDown)
  586. unselectAll();
  587. TreeElement* treeElement = bottomMostIter->getTreeElement();
  588. selectElement(treeElement);
  589. scrollToElement(treeElement, false);
  590. }
  591. }
  592. }
  593. return false;
  594. }
  595. void GUISceneTreeView::dragAndDropEnded()
  596. {
  597. mDragInProgress = false;
  598. markContentAsDirty();
  599. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
  600. cm_delete(draggedSceneObjects);
  601. }
  602. bool GUISceneTreeView::isSelectionActive() const
  603. {
  604. return mIsElementSelected && mSelectedElements.size() > 0;
  605. }
  606. void GUISceneTreeView::selectElement(TreeElement* element)
  607. {
  608. auto iterFind = std::find_if(mSelectedElements.begin(), mSelectedElements.end(),
  609. [&] (const SelectedElement& x) { return x.element == element; });
  610. if(iterFind == mSelectedElements.end())
  611. {
  612. GUITexture* background = GUITexture::create(_getParentWidget(), mSelectionBackgroundStyle);
  613. _registerChildElement(background);
  614. element->mIsSelected = true;
  615. mSelectedElements.push_back(SelectedElement(element, background));
  616. mIsElementSelected = true;
  617. }
  618. }
  619. void GUISceneTreeView::unselectElement(TreeElement* element)
  620. {
  621. auto iterFind = std::find_if(mSelectedElements.begin(), mSelectedElements.end(),
  622. [&] (const SelectedElement& x) { return x.element == element; });
  623. if(iterFind != mSelectedElements.end())
  624. {
  625. iterFind->element->mIsSelected = false;
  626. GUIElement::destroy(iterFind->background);
  627. mSelectedElements.erase(iterFind);
  628. markContentAsDirty();
  629. }
  630. mIsElementSelected = mSelectedElements.size() > 0;
  631. }
  632. void GUISceneTreeView::unselectAll()
  633. {
  634. for(auto& selectedElem : mSelectedElements)
  635. {
  636. selectedElem.element->mIsSelected = false;
  637. GUIElement::destroy(selectedElem.background);
  638. }
  639. mSelectedElements.clear();
  640. mIsElementSelected = false;
  641. markContentAsDirty();
  642. }
  643. void GUISceneTreeView::elementToggled(TreeElement* element, bool toggled)
  644. {
  645. element->mIsExpanded = toggled;
  646. }
  647. void GUISceneTreeView::onEditAccepted()
  648. {
  649. disableEdit(true);
  650. }
  651. void GUISceneTreeView::onEditCanceled()
  652. {
  653. if(mEditElement != nullptr)
  654. disableEdit(false);
  655. }
  656. void GUISceneTreeView::enableEdit(TreeElement* element)
  657. {
  658. assert(mEditElement == nullptr);
  659. mEditElement = element;
  660. mNameEditBox->enableRecursively();
  661. mNameEditBox->setFocus(true);
  662. if(element->mElement != nullptr)
  663. element->mElement->disableRecursively();
  664. }
  665. void GUISceneTreeView::disableEdit(bool applyChanges)
  666. {
  667. assert(mEditElement != nullptr);
  668. if(mEditElement->mElement != nullptr)
  669. mEditElement->mElement->enableRecursively();
  670. if(applyChanges)
  671. {
  672. String newName = toString(mNameEditBox->getText());
  673. CmdEditPlainFieldGO<String>::execute(mEditElement->mSceneObject, "mName", newName);
  674. }
  675. mNameEditBox->disableRecursively();
  676. mEditElement = nullptr;
  677. }
  678. Vector2I GUISceneTreeView::_getOptimalSize() const
  679. {
  680. struct UpdateTreeElement
  681. {
  682. UpdateTreeElement(const TreeElement* element, UINT32 indent)
  683. :element(element), indent(indent)
  684. { }
  685. const TreeElement* element;
  686. UINT32 indent;
  687. };
  688. Vector2I optimalSize;
  689. if(_getLayoutOptions().fixedWidth && _getLayoutOptions().fixedHeight)
  690. {
  691. optimalSize.x = _getLayoutOptions().width;
  692. optimalSize.y = _getLayoutOptions().height;
  693. }
  694. else
  695. {
  696. Stack<UpdateTreeElement>::type todo;
  697. todo.push(UpdateTreeElement(&mRootElement, 0));
  698. while(!todo.empty())
  699. {
  700. UpdateTreeElement currentUpdateElement = todo.top();
  701. const TreeElement* current = currentUpdateElement.element;
  702. todo.pop();
  703. INT32 yOffset = 0;
  704. if(current->mElement != nullptr)
  705. {
  706. Vector2I curOptimalSize = current->mElement->_getOptimalSize();
  707. optimalSize.x = std::max(optimalSize.x,
  708. (INT32)(INITIAL_INDENT_OFFSET + curOptimalSize.x + currentUpdateElement.indent * INDENT_SIZE));
  709. yOffset = curOptimalSize.y + ELEMENT_EXTRA_SPACING;
  710. }
  711. optimalSize.y += yOffset;
  712. for(auto& child : current->mChildren)
  713. {
  714. if(!child->mIsVisible)
  715. continue;
  716. todo.push(UpdateTreeElement(child, currentUpdateElement.indent + 1));
  717. }
  718. }
  719. if(_getLayoutOptions().fixedWidth)
  720. optimalSize.x = _getLayoutOptions().width;
  721. else
  722. {
  723. if(_getLayoutOptions().minWidth > 0)
  724. optimalSize.x = std::max((INT32)_getLayoutOptions().minWidth, optimalSize.x);
  725. if(_getLayoutOptions().maxWidth > 0)
  726. optimalSize.x = std::min((INT32)_getLayoutOptions().maxWidth, optimalSize.x);
  727. }
  728. if(_getLayoutOptions().fixedHeight)
  729. optimalSize.y = _getLayoutOptions().height;
  730. else
  731. {
  732. if(_getLayoutOptions().minHeight > 0)
  733. optimalSize.y = std::max((INT32)_getLayoutOptions().minHeight, optimalSize.y);
  734. if(_getLayoutOptions().maxHeight > 0)
  735. optimalSize.y = std::min((INT32)_getLayoutOptions().maxHeight, optimalSize.y);
  736. }
  737. }
  738. return optimalSize;
  739. }
  740. void GUISceneTreeView::updateClippedBounds()
  741. {
  742. Vector2I offset = _getOffset();
  743. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  744. RectI localClipRect(mClipRect.x + mOffset.x, mClipRect.y + mOffset.y, mClipRect.width, mClipRect.height);
  745. mClippedBounds.clip(localClipRect);
  746. }
  747. void GUISceneTreeView::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  748. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  749. {
  750. struct UpdateTreeElement
  751. {
  752. UpdateTreeElement(TreeElement* element, UINT32 indent)
  753. :element(element), indent(indent)
  754. { }
  755. TreeElement* element;
  756. UINT32 indent;
  757. };
  758. mVisibleElements.clear();
  759. Stack<UpdateTreeElement>::type todo;
  760. todo.push(UpdateTreeElement(&mRootElement, 0));
  761. // NOTE - Instead of iterating through all elements, try to find those within the clip rect
  762. // and only iterate through those. Others should somehow be marked in-active (similar to GUIElement::isDisabled()?)
  763. Vector<TreeElement*>::type tempOrderedElements;
  764. Vector2I offset(x, y);
  765. while(!todo.empty())
  766. {
  767. UpdateTreeElement currentUpdateElement = todo.top();
  768. TreeElement* current = currentUpdateElement.element;
  769. UINT32 indent = currentUpdateElement.indent;
  770. todo.pop();
  771. INT32 btnHeight = 0;
  772. INT32 yOffset = 0;
  773. if(current->mElement != nullptr)
  774. {
  775. Vector2I elementSize = current->mElement->_getOptimalSize();
  776. btnHeight = elementSize.y;
  777. mVisibleElements.push_back(InteractableElement(current->mParent, current->mSortedIdx * 2 + 0, RectI(x, offset.y, width, ELEMENT_EXTRA_SPACING)));
  778. mVisibleElements.push_back(InteractableElement(current->mParent, current->mSortedIdx * 2 + 1, RectI(x, offset.y + ELEMENT_EXTRA_SPACING, width, btnHeight)));
  779. offset.x = x + INITIAL_INDENT_OFFSET + indent * INDENT_SIZE;
  780. offset.y += ELEMENT_EXTRA_SPACING;
  781. current->mElement->_setOffset(offset);
  782. current->mElement->_setWidth(elementSize.x);
  783. current->mElement->_setHeight(elementSize.y);
  784. current->mElement->_setAreaDepth(areaDepth);
  785. current->mElement->_setWidgetDepth(widgetDepth);
  786. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  787. current->mElement->_setClipRect(elemClipRect);
  788. yOffset = btnHeight;
  789. }
  790. if(current->mFoldoutBtn != nullptr)
  791. {
  792. Vector2I elementSize = current->mFoldoutBtn->_getOptimalSize();
  793. offset.x -= std::min((INT32)INITIAL_INDENT_OFFSET, elementSize.x);
  794. Vector2I myOffset = offset;
  795. myOffset.y -= 2; // TODO: Arbitrary offset, I should adjust it based on font baseline so that the button is nicely centered on text
  796. if(elementSize.y > btnHeight)
  797. {
  798. UINT32 diff = elementSize.y - btnHeight;
  799. float half = diff * 0.5f;
  800. myOffset.y -= Math::floorToInt(half);
  801. }
  802. current->mFoldoutBtn->_setOffset(myOffset);
  803. current->mFoldoutBtn->_setWidth(elementSize.x);
  804. current->mFoldoutBtn->_setHeight(elementSize.y);
  805. current->mFoldoutBtn->_setAreaDepth(areaDepth);
  806. current->mFoldoutBtn->_setWidgetDepth(widgetDepth);
  807. RectI elemClipRect(clipRect.x - myOffset.x, clipRect.y - myOffset.y, clipRect.width, clipRect.height);
  808. current->mFoldoutBtn->_setClipRect(elemClipRect);
  809. }
  810. offset.y += yOffset;
  811. tempOrderedElements.resize(current->mChildren.size(), nullptr);
  812. for(auto& child : current->mChildren)
  813. {
  814. tempOrderedElements[child->mSortedIdx] = child;
  815. }
  816. for(auto iter = tempOrderedElements.rbegin(); iter != tempOrderedElements.rend(); ++iter)
  817. {
  818. TreeElement* child = *iter;
  819. if(!child->mIsVisible)
  820. continue;
  821. todo.push(UpdateTreeElement(child, indent + 1));
  822. }
  823. }
  824. UINT32 remainingHeight = (UINT32)std::max(0, (INT32)height - (offset.y - y));
  825. if(remainingHeight > 0)
  826. mVisibleElements.push_back(InteractableElement(&mRootElement, (UINT32)mRootElement.mChildren.size() * 2, RectI(x, offset.y, width, remainingHeight)));
  827. for(auto selectedElem : mSelectedElements)
  828. {
  829. GUILabel* targetElement = selectedElem.element->mElement;
  830. Vector2I offset = targetElement->_getOffset();
  831. offset.x = x;
  832. selectedElem.background->_setOffset(offset);
  833. selectedElem.background->_setWidth(width);
  834. selectedElem.background->_setHeight(targetElement->_getHeight());
  835. selectedElem.background->_setAreaDepth(areaDepth + 1);
  836. selectedElem.background->_setWidgetDepth(widgetDepth);
  837. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  838. selectedElem.background->_setClipRect(elemClipRect);
  839. }
  840. if(mEditElement != nullptr)
  841. {
  842. GUILabel* targetElement = mEditElement->mElement;
  843. Vector2I offset = targetElement->_getOffset();
  844. UINT32 remainingWidth = (UINT32)std::max(0, (((INT32)width) - (offset.x - x)));
  845. mNameEditBox->_setOffset(offset);
  846. mNameEditBox->_setWidth(remainingWidth);
  847. mNameEditBox->_setHeight(targetElement->_getHeight());
  848. mNameEditBox->_setAreaDepth(areaDepth);
  849. mNameEditBox->_setWidgetDepth(widgetDepth);
  850. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  851. mNameEditBox->_setClipRect(elemClipRect);
  852. }
  853. if(mDragInProgress)
  854. {
  855. const InteractableElement* interactableElement = findElementUnderCoord(mDragPosition);
  856. if(interactableElement == nullptr)
  857. {
  858. if(!mDragHighlight->_isDisabled())
  859. mDragHighlight->disableRecursively();
  860. if(!mDragSepHighlight->_isDisabled())
  861. mDragSepHighlight->disableRecursively();
  862. }
  863. else
  864. {
  865. if(interactableElement->isTreeElement())
  866. {
  867. if(!mDragSepHighlight->_isDisabled())
  868. mDragSepHighlight->disableRecursively();
  869. if(mDragHighlight->_isDisabled())
  870. mDragHighlight->enableRecursively();
  871. Vector2I offset(interactableElement->bounds.x, interactableElement->bounds.y);
  872. mDragHighlight->_setOffset(offset);
  873. mDragHighlight->_setWidth(interactableElement->bounds.width);
  874. mDragHighlight->_setHeight(interactableElement->bounds.height);
  875. mDragHighlight->_setAreaDepth(areaDepth + 1);
  876. mDragHighlight->_setWidgetDepth(widgetDepth);
  877. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  878. mDragHighlight->_setClipRect(elemClipRect);
  879. }
  880. else
  881. {
  882. if(!mDragHighlight->_isDisabled())
  883. mDragHighlight->disableRecursively();
  884. if(mDragSepHighlight->_isDisabled())
  885. mDragSepHighlight->enableRecursively();
  886. Vector2I offset(interactableElement->bounds.x, interactableElement->bounds.y);
  887. mDragSepHighlight->_setOffset(offset);
  888. mDragSepHighlight->_setWidth(interactableElement->bounds.width);
  889. mDragSepHighlight->_setHeight(interactableElement->bounds.height);
  890. mDragSepHighlight->_setAreaDepth(areaDepth + 1);
  891. mDragSepHighlight->_setWidgetDepth(widgetDepth);
  892. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  893. mDragSepHighlight->_setClipRect(elemClipRect);
  894. }
  895. }
  896. }
  897. else
  898. {
  899. if(!mDragHighlight->_isDisabled())
  900. mDragHighlight->disableRecursively();
  901. if(!mDragSepHighlight->_isDisabled())
  902. mDragSepHighlight->disableRecursively();
  903. }
  904. // Update scroll bounds
  905. UINT32 scrollHeight = (UINT32)Math::roundToInt(clipRect.height * SCROLL_AREA_HEIGHT_PCT);
  906. mTopScrollBounds.x = clipRect.x;
  907. mTopScrollBounds.y = clipRect.y;
  908. mTopScrollBounds.width = clipRect.width;
  909. mTopScrollBounds.height = scrollHeight;
  910. mBottomScrollBounds.x = clipRect.x;
  911. mBottomScrollBounds.y = clipRect.y + clipRect.height - scrollHeight;
  912. mBottomScrollBounds.width = clipRect.width;
  913. mBottomScrollBounds.height = scrollHeight;
  914. }
  915. const GUISceneTreeView::InteractableElement* GUISceneTreeView::findElementUnderCoord(const CM::Vector2I& coord) const
  916. {
  917. for(auto& element : mVisibleElements)
  918. {
  919. if(element.bounds.contains(coord))
  920. {
  921. return &element;
  922. }
  923. }
  924. return nullptr;
  925. }
  926. GUISceneTreeView::TreeElement* GUISceneTreeView::getTopMostSelectedElement() const
  927. {
  928. auto topMostElement = mVisibleElements.end();
  929. for(auto& selectedElement : mSelectedElements)
  930. {
  931. auto iterFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  932. [&] (const InteractableElement& x) { return x.getTreeElement() == selectedElement.element; });
  933. if(iterFind != mVisibleElements.end())
  934. {
  935. if(topMostElement == mVisibleElements.end())
  936. topMostElement = iterFind;
  937. else
  938. {
  939. if(iterFind->bounds.y < topMostElement->bounds.y)
  940. topMostElement = iterFind;
  941. }
  942. }
  943. }
  944. if(topMostElement != mVisibleElements.end())
  945. return topMostElement->getTreeElement();
  946. else
  947. return nullptr;
  948. }
  949. GUISceneTreeView::TreeElement* GUISceneTreeView::getBottomMostSelectedElement() const
  950. {
  951. auto& botMostElement = mVisibleElements.end();
  952. for(auto& selectedElement : mSelectedElements)
  953. {
  954. auto iterFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  955. [&] (const InteractableElement& x) { return x.getTreeElement() == selectedElement.element; });
  956. if(iterFind != mVisibleElements.end())
  957. {
  958. if(botMostElement == mVisibleElements.end())
  959. botMostElement = iterFind;
  960. else
  961. {
  962. if((iterFind->bounds.y + iterFind->bounds.height) > (botMostElement->bounds.y + botMostElement->bounds.height))
  963. botMostElement = iterFind;
  964. }
  965. }
  966. }
  967. if(botMostElement != mVisibleElements.end())
  968. return botMostElement->getTreeElement();
  969. else
  970. return nullptr;
  971. }
  972. void GUISceneTreeView::temporarilyExpandElement(const GUISceneTreeView::InteractableElement* mouseOverElement)
  973. {
  974. TreeElement* treeElement = nullptr;
  975. if(mouseOverElement != nullptr && mouseOverElement->isTreeElement())
  976. treeElement = mouseOverElement->getTreeElement();
  977. if(treeElement == nullptr || treeElement != mMouseOverDragElement)
  978. {
  979. while(!mAutoExpandedElements.empty())
  980. {
  981. TreeElement* autoExpandedElement = mAutoExpandedElements.top();
  982. bool unexpandElement = false;
  983. if(mouseOverElement != nullptr && mouseOverElement->parent != nullptr)
  984. {
  985. if(mouseOverElement->parent != autoExpandedElement && !mouseOverElement->parent->isParentRec(autoExpandedElement))
  986. unexpandElement = true;
  987. else
  988. break;
  989. }
  990. else
  991. unexpandElement = true;
  992. if(unexpandElement)
  993. {
  994. autoExpandedElement->mIsExpanded = false;
  995. if(autoExpandedElement->mFoldoutBtn != nullptr)
  996. autoExpandedElement->mFoldoutBtn->toggleOff();
  997. mAutoExpandedElements.pop();
  998. }
  999. }
  1000. mMouseOverDragElement = treeElement;
  1001. mMouseOverDragElementTime = gTime().getTime();
  1002. }
  1003. else
  1004. {
  1005. if(mMouseOverDragElement != nullptr && !mMouseOverDragElement->mIsExpanded)
  1006. {
  1007. float timeDiff = gTime().getTime() - mMouseOverDragElementTime;
  1008. if(timeDiff >= AUTO_EXPAND_DELAY_SEC)
  1009. {
  1010. mAutoExpandedElements.push(mMouseOverDragElement);
  1011. mMouseOverDragElement->mIsExpanded = true;
  1012. if(mMouseOverDragElement->mFoldoutBtn != nullptr)
  1013. mMouseOverDragElement->mFoldoutBtn->toggleOn();
  1014. }
  1015. }
  1016. }
  1017. }
  1018. void GUISceneTreeView::scrollToElement(TreeElement* element, bool center)
  1019. {
  1020. if(element->mElement == nullptr)
  1021. return;
  1022. GUIScrollArea* scrollArea = findParentScrollArea();
  1023. if(scrollArea == nullptr)
  1024. return;
  1025. if(center)
  1026. {
  1027. RectI myBounds = _getClippedBounds();
  1028. INT32 clipVertCenter = myBounds.y + (INT32)Math::roundToInt(myBounds.height * 0.5f);
  1029. INT32 elemVertCenter = element->mElement->_getOffset().y + (INT32)Math::roundToInt(element->mElement->_getHeight() * 0.5f);
  1030. if(elemVertCenter > clipVertCenter)
  1031. scrollArea->scrollUpPx(elemVertCenter - clipVertCenter);
  1032. else
  1033. scrollArea->scrollDownPx(clipVertCenter - elemVertCenter);
  1034. }
  1035. else
  1036. {
  1037. RectI myBounds = _getClippedBounds();
  1038. INT32 elemVertTop = element->mElement->_getOffset().y;
  1039. INT32 elemVertBottom = element->mElement->_getOffset().y + element->mElement->_getHeight();
  1040. INT32 top = myBounds.y;
  1041. INT32 bottom = myBounds.y + myBounds.height;
  1042. INT32 offset = 0;
  1043. if(elemVertTop < top)
  1044. scrollArea->scrollUpPx(top - elemVertTop);
  1045. else if(elemVertBottom > bottom)
  1046. scrollArea->scrollDownPx(elemVertBottom - bottom);
  1047. }
  1048. }
  1049. GUIScrollArea* GUISceneTreeView::findParentScrollArea() const
  1050. {
  1051. GUIElementBase* parent = _getParent();
  1052. while(parent != nullptr)
  1053. {
  1054. if(parent->_getType() == GUIElementBase::Type::Element)
  1055. {
  1056. GUIElement* parentElement = static_cast<GUIElement*>(parent);
  1057. if(parentElement->getElementType() == GUIElement::ElementType::ScrollArea)
  1058. {
  1059. GUIScrollArea* scrollArea = static_cast<GUIScrollArea*>(parentElement);
  1060. return scrollArea;
  1061. }
  1062. }
  1063. parent = parent->_getParent();
  1064. }
  1065. return nullptr;
  1066. }
  1067. const String& GUISceneTreeView::getGUITypeName()
  1068. {
  1069. static String typeName = "SceneTreeView";
  1070. return typeName;
  1071. }
  1072. }