BsGUISceneTreeView.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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. deleteTreeElement(current->mChildren[i]);
  225. }
  226. current->mChildren = newChildren;
  227. current->mIsDirty = true;
  228. }
  229. // Check if name needs updating
  230. const String& name = current->mSceneObject->getName();
  231. if(current->mName != name)
  232. {
  233. current->mName = name;
  234. current->mIsDirty = true;
  235. }
  236. // Calculate the sorted index of the element based on its name
  237. TreeElement* parent = current->mParent;
  238. if(current->mIsDirty && parent != nullptr)
  239. {
  240. for(UINT32 i = 0; i < (UINT32)parent->mChildren.size(); i++)
  241. {
  242. INT32 stringCompare = current->mName.compare(parent->mChildren[i]->mName);
  243. if(stringCompare > 0)
  244. {
  245. if(current->mSortedIdx < parent->mChildren[i]->mSortedIdx)
  246. std::swap(current->mSortedIdx, parent->mChildren[i]->mSortedIdx);
  247. }
  248. else if(stringCompare < 0)
  249. {
  250. if(current->mSortedIdx > parent->mChildren[i]->mSortedIdx)
  251. std::swap(current->mSortedIdx, parent->mChildren[i]->mSortedIdx);
  252. }
  253. }
  254. }
  255. }
  256. bool visibilityChanged = false;
  257. if(current->mIsVisible != updateElement.visible)
  258. {
  259. visibilityChanged = true;
  260. current->mIsVisible = updateElement.visible;
  261. current->mIsDirty = true;
  262. }
  263. if(current->mIsDirty && current != &mRootElement)
  264. {
  265. if(updateElement.visible)
  266. {
  267. HString name(toWString(current->mName));
  268. if(current->mElement == nullptr)
  269. {
  270. current->mElement = GUILabel::create(_getParentWidget(), name, mElementBtnStyle);
  271. _registerChildElement(current->mElement);
  272. }
  273. if(current->mChildren.size() > 0)
  274. {
  275. if(current->mFoldoutBtn == nullptr)
  276. {
  277. current->mFoldoutBtn = GUIToggle::create(_getParentWidget(), GUIContent(HString(L"")), mFoldoutBtnStyle);
  278. _registerChildElement(current->mFoldoutBtn);
  279. current->mFoldoutBtn->onToggled.connect(boost::bind(&GUISceneTreeView::elementToggled, this, current, _1));
  280. if(current->mIsExpanded)
  281. current->mFoldoutBtn->toggleOn();
  282. }
  283. }
  284. else
  285. {
  286. if(current->mFoldoutBtn != nullptr)
  287. {
  288. GUIElement::destroy(current->mFoldoutBtn);
  289. current->mFoldoutBtn = nullptr;
  290. }
  291. }
  292. current->mElement->setContent(GUIContent(name));
  293. }
  294. else
  295. {
  296. if(current->mElement != nullptr)
  297. {
  298. GUIElement::destroy(current->mElement);
  299. current->mElement = nullptr;
  300. }
  301. if(current->mFoldoutBtn != nullptr)
  302. {
  303. GUIElement::destroy(current->mFoldoutBtn);
  304. current->mFoldoutBtn = nullptr;
  305. }
  306. if(visibilityChanged && current->mIsSelected)
  307. unselectElement(current);
  308. }
  309. markContentAsDirty();
  310. current->mIsDirty = false;
  311. }
  312. // Queue children for next iteration
  313. if(visibilityChanged || current->mIsVisible)
  314. {
  315. for(UINT32 i = 0; i < (UINT32)current->mChildren.size(); i++)
  316. {
  317. todo.push(UpdateTreeElement(current->mChildren[i], i, current->mIsVisible && current->mIsExpanded));
  318. }
  319. }
  320. }
  321. // Attempt to scroll if needed
  322. if(mScrollState != ScrollState::None)
  323. {
  324. GUIScrollArea* scrollArea = findParentScrollArea();
  325. if(scrollArea != nullptr)
  326. {
  327. float curTime = gTime().getTime();
  328. float timeDiff = curTime - mLastScrollTime;
  329. float secondsPerPixel = 1.0f / SCROLL_SPEED_PX_PER_SEC;
  330. switch(mScrollState)
  331. {
  332. case ScrollState::TransitioningUp:
  333. mScrollState = ScrollState::Up;
  334. mLastScrollTime = curTime;
  335. break;
  336. case ScrollState::TransitioningDown:
  337. mScrollState = ScrollState::Down;
  338. mLastScrollTime = curTime;
  339. break;
  340. case ScrollState::Up:
  341. {
  342. UINT32 scrollAmount = (UINT32)Math::floorToInt(timeDiff / secondsPerPixel);
  343. mLastScrollTime += scrollAmount * secondsPerPixel;
  344. scrollArea->scrollUpPx(scrollAmount);
  345. }
  346. break;
  347. case ScrollState::Down:
  348. {
  349. UINT32 scrollAmount = (UINT32)Math::floorToInt(timeDiff / secondsPerPixel);
  350. mLastScrollTime += scrollAmount * secondsPerPixel;
  351. scrollArea->scrollDownPx(scrollAmount);
  352. }
  353. break;
  354. }
  355. }
  356. }
  357. }
  358. bool GUISceneTreeView::mouseEvent(const GUIMouseEvent& event)
  359. {
  360. if(event.getType() == GUIMouseEventType::MouseUp)
  361. {
  362. if(DragAndDropManager::instance().isDragInProgress())
  363. return false;
  364. const GUISceneTreeView::InteractableElement* element = findElementUnderCoord(event.getPosition());
  365. TreeElement* treeElement = nullptr;
  366. if(element != nullptr && element->isTreeElement())
  367. {
  368. treeElement = element->getTreeElement();
  369. }
  370. if(treeElement != nullptr && event.getPosition().x >= treeElement->mElement->getBounds().x)
  371. {
  372. if(event.isCtrlDown())
  373. {
  374. selectElement(treeElement);
  375. }
  376. else if(event.isShiftDown())
  377. {
  378. if(isSelectionActive())
  379. {
  380. TreeElement* selectionRoot = mSelectedElements[0].element;
  381. unselectAll();
  382. auto iterStartFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  383. [&] (const InteractableElement& x) { return x.parent == selectionRoot->mParent; } );
  384. bool foundStart = false;
  385. bool foundEnd = false;
  386. for(; iterStartFind != mVisibleElements.end(); ++iterStartFind)
  387. {
  388. if(!iterStartFind->isTreeElement())
  389. continue;
  390. TreeElement* curElem = iterStartFind->getTreeElement();
  391. if(curElem == selectionRoot)
  392. {
  393. foundStart = true;
  394. break;
  395. }
  396. }
  397. auto iterEndFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  398. [&] (const InteractableElement& x) { return &x == element; } );
  399. if(iterEndFind != mVisibleElements.end())
  400. foundEnd = true;
  401. if(foundStart && foundEnd)
  402. {
  403. if(iterStartFind < iterEndFind)
  404. {
  405. for(;iterStartFind != (iterEndFind + 1); ++iterStartFind)
  406. {
  407. if(iterStartFind->isTreeElement())
  408. selectElement(iterStartFind->getTreeElement());
  409. }
  410. }
  411. else if(iterEndFind < iterStartFind)
  412. {
  413. for(;iterEndFind != (iterStartFind + 1); ++iterEndFind)
  414. {
  415. if(iterEndFind->isTreeElement())
  416. selectElement(iterEndFind->getTreeElement());
  417. }
  418. }
  419. else
  420. selectElement(treeElement);
  421. }
  422. if(!foundStart || !foundEnd)
  423. selectElement(treeElement);
  424. }
  425. else
  426. {
  427. selectElement(treeElement);
  428. }
  429. }
  430. else
  431. {
  432. unselectAll();
  433. selectElement(treeElement);
  434. }
  435. markContentAsDirty();
  436. return true;
  437. }
  438. }
  439. else if(event.getType() == GUIMouseEventType::MouseDragStart)
  440. {
  441. mDragStartPosition = event.getPosition();
  442. }
  443. else if(event.getType() == GUIMouseEventType::MouseDrag)
  444. {
  445. UINT32 dist = mDragStartPosition.manhattanDist(event.getPosition());
  446. if(!DragAndDropManager::instance().isDragInProgress())
  447. {
  448. if(dist > DRAG_MIN_DISTANCE)
  449. {
  450. const GUISceneTreeView::InteractableElement* element = findElementUnderCoord(mDragStartPosition);
  451. TreeElement* treeElement = nullptr;
  452. if(element != nullptr && element->isTreeElement())
  453. {
  454. // If element we are trying to drag isn't selected, select it
  455. TreeElement* treeElement = element->getTreeElement();
  456. auto iterFind = std::find_if(mSelectedElements.begin(), mSelectedElements.end(),
  457. [&] (const SelectedElement& x) { return x.element == treeElement; });
  458. if(iterFind == mSelectedElements.end())
  459. {
  460. unselectAll();
  461. selectElement(element->getTreeElement());
  462. }
  463. }
  464. DraggedSceneObjects* draggedSceneObjects = cm_new<DraggedSceneObjects>((UINT32)mSelectedElements.size());
  465. UINT32 cnt = 0;
  466. for(auto& selectedElement : mSelectedElements)
  467. {
  468. draggedSceneObjects->objects[cnt] = selectedElement.element->mSceneObject;
  469. cnt++;
  470. }
  471. DragAndDropManager::instance().startDrag(HTexture(), (UINT32)DragAndDropType::SceneObject, (void*)draggedSceneObjects,
  472. boost::bind(&GUISceneTreeView::dragAndDropEnded, this));
  473. mDragPosition = event.getPosition();
  474. mDragInProgress = true;
  475. mScrollState = ScrollState::None;
  476. markContentAsDirty();
  477. }
  478. }
  479. }
  480. else if(event.getType() == GUIMouseEventType::MouseDragAndDropDragged)
  481. {
  482. if(DragAndDropManager::instance().isDragInProgress() && DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::SceneObject)
  483. {
  484. mDragPosition = event.getPosition();
  485. mDragInProgress = true;
  486. markContentAsDirty();
  487. if(mBottomScrollBounds.contains(mDragPosition))
  488. {
  489. if(mScrollState != ScrollState::Down)
  490. mScrollState = ScrollState::TransitioningDown;
  491. }
  492. else if(mTopScrollBounds.contains(mDragPosition))
  493. {
  494. if(mScrollState != ScrollState::Up)
  495. mScrollState = ScrollState::TransitioningUp;
  496. }
  497. else
  498. mScrollState = ScrollState::None;
  499. return true;
  500. }
  501. }
  502. else if(event.getType() == GUIMouseEventType::MouseDragAndDropDropped)
  503. {
  504. if(DragAndDropManager::instance().isDragInProgress() && DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::SceneObject)
  505. {
  506. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
  507. const GUISceneTreeView::InteractableElement* element = findElementUnderCoord(event.getPosition());
  508. TreeElement* treeElement = nullptr;
  509. if(element != nullptr)
  510. {
  511. if(element->isTreeElement())
  512. treeElement = element->getTreeElement();
  513. else
  514. treeElement = element->parent;
  515. }
  516. if(treeElement != nullptr)
  517. {
  518. Vector<HSceneObject>::type sceneObjects;
  519. HSceneObject newParent = treeElement->mSceneObject;
  520. for(UINT32 i = 0; i < draggedSceneObjects->numObjects; i++)
  521. {
  522. if(draggedSceneObjects->objects[i] != newParent)
  523. sceneObjects.push_back(draggedSceneObjects->objects[i]);
  524. }
  525. CmdReparentSO::execute(sceneObjects, newParent);
  526. }
  527. unselectAll();
  528. return true;
  529. }
  530. }
  531. else if(event.getType() == GUIMouseEventType::MouseOut)
  532. {
  533. mDragInProgress = false;
  534. markContentAsDirty();
  535. }
  536. return false;
  537. }
  538. bool GUISceneTreeView::commandEvent(const GUICommandEvent& ev)
  539. {
  540. if(ev.getType() == GUICommandEventType::Rename)
  541. {
  542. if(isSelectionActive() && mEditElement == nullptr)
  543. {
  544. unselectAll();
  545. enableEdit(mSelectedElements[0].element);
  546. }
  547. return true;
  548. }
  549. if(ev.getType() == GUICommandEventType::CursorMoveUp || ev.getType() == GUICommandEventType::SelectUp)
  550. {
  551. TreeElement* topMostElement = getTopMostSelectedElement();
  552. auto topMostIter = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  553. [&] (const InteractableElement& x) { return x.getTreeElement() == topMostElement; });
  554. if(topMostIter != mVisibleElements.end() && topMostIter != mVisibleElements.begin())
  555. {
  556. do
  557. {
  558. topMostIter--;
  559. } while (!topMostIter->isTreeElement() && topMostIter != mVisibleElements.begin());
  560. if(topMostIter->isTreeElement())
  561. {
  562. if(ev.getType() == GUICommandEventType::CursorMoveUp)
  563. unselectAll();
  564. TreeElement* treeElement = topMostIter->getTreeElement();
  565. selectElement(treeElement);
  566. scrollToElement(treeElement, false);
  567. }
  568. }
  569. }
  570. else if(ev.getType() == GUICommandEventType::CursorMoveDown || ev.getType() == GUICommandEventType::SelectDown)
  571. {
  572. TreeElement* bottoMostElement = getBottomMostSelectedElement();
  573. auto bottomMostIter = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  574. [&] (const InteractableElement& x) { return x.getTreeElement() == bottoMostElement; });
  575. if(bottomMostIter != mVisibleElements.end())
  576. {
  577. do
  578. {
  579. bottomMostIter++;
  580. } while (bottomMostIter != mVisibleElements.end() && !bottomMostIter->isTreeElement());
  581. if(bottomMostIter != mVisibleElements.end() && bottomMostIter->isTreeElement())
  582. {
  583. if(ev.getType() == GUICommandEventType::CursorMoveDown)
  584. unselectAll();
  585. TreeElement* treeElement = bottomMostIter->getTreeElement();
  586. selectElement(treeElement);
  587. scrollToElement(treeElement, false);
  588. }
  589. }
  590. }
  591. return false;
  592. }
  593. void GUISceneTreeView::dragAndDropEnded()
  594. {
  595. mDragInProgress = false;
  596. markContentAsDirty();
  597. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
  598. cm_delete(draggedSceneObjects);
  599. }
  600. bool GUISceneTreeView::isSelectionActive() const
  601. {
  602. return mIsElementSelected && mSelectedElements.size() > 0;
  603. }
  604. void GUISceneTreeView::selectElement(TreeElement* element)
  605. {
  606. auto iterFind = std::find_if(mSelectedElements.begin(), mSelectedElements.end(),
  607. [&] (const SelectedElement& x) { return x.element == element; });
  608. if(iterFind == mSelectedElements.end())
  609. {
  610. GUITexture* background = GUITexture::create(_getParentWidget(), mSelectionBackgroundStyle);
  611. _registerChildElement(background);
  612. element->mIsSelected = true;
  613. mSelectedElements.push_back(SelectedElement(element, background));
  614. mIsElementSelected = true;
  615. }
  616. }
  617. void GUISceneTreeView::unselectElement(TreeElement* element)
  618. {
  619. auto iterFind = std::find_if(mSelectedElements.begin(), mSelectedElements.end(),
  620. [&] (const SelectedElement& x) { return x.element == element; });
  621. if(iterFind != mSelectedElements.end())
  622. {
  623. iterFind->element->mIsSelected = false;
  624. GUIElement::destroy(iterFind->background);
  625. mSelectedElements.erase(iterFind);
  626. markContentAsDirty();
  627. }
  628. mIsElementSelected = mSelectedElements.size() > 0;
  629. }
  630. void GUISceneTreeView::unselectAll()
  631. {
  632. for(auto& selectedElem : mSelectedElements)
  633. {
  634. selectedElem.element->mIsSelected = false;
  635. GUIElement::destroy(selectedElem.background);
  636. }
  637. mSelectedElements.clear();
  638. mIsElementSelected = false;
  639. markContentAsDirty();
  640. }
  641. void GUISceneTreeView::elementToggled(TreeElement* element, bool toggled)
  642. {
  643. element->mIsExpanded = toggled;
  644. }
  645. void GUISceneTreeView::onEditAccepted()
  646. {
  647. disableEdit(true);
  648. }
  649. void GUISceneTreeView::onEditCanceled()
  650. {
  651. if(mEditElement != nullptr)
  652. disableEdit(false);
  653. }
  654. void GUISceneTreeView::enableEdit(TreeElement* element)
  655. {
  656. assert(mEditElement == nullptr);
  657. mEditElement = element;
  658. mNameEditBox->enableRecursively();
  659. mNameEditBox->setFocus(true);
  660. if(element->mElement != nullptr)
  661. element->mElement->disableRecursively();
  662. }
  663. void GUISceneTreeView::deleteTreeElement(TreeElement* element)
  664. {
  665. closeTemporarilyExpandedElements(); // In case this element is one of them
  666. if(element->mIsSelected)
  667. unselectElement(element);
  668. cm_delete(element);
  669. }
  670. void GUISceneTreeView::disableEdit(bool applyChanges)
  671. {
  672. assert(mEditElement != nullptr);
  673. if(mEditElement->mElement != nullptr)
  674. mEditElement->mElement->enableRecursively();
  675. if(applyChanges)
  676. {
  677. String newName = toString(mNameEditBox->getText());
  678. CmdEditPlainFieldGO<String>::execute(mEditElement->mSceneObject, "mName", newName);
  679. }
  680. mNameEditBox->disableRecursively();
  681. mEditElement = nullptr;
  682. }
  683. Vector2I GUISceneTreeView::_getOptimalSize() const
  684. {
  685. struct UpdateTreeElement
  686. {
  687. UpdateTreeElement(const TreeElement* element, UINT32 indent)
  688. :element(element), indent(indent)
  689. { }
  690. const TreeElement* element;
  691. UINT32 indent;
  692. };
  693. Vector2I optimalSize;
  694. if(_getLayoutOptions().fixedWidth && _getLayoutOptions().fixedHeight)
  695. {
  696. optimalSize.x = _getLayoutOptions().width;
  697. optimalSize.y = _getLayoutOptions().height;
  698. }
  699. else
  700. {
  701. Stack<UpdateTreeElement>::type todo;
  702. todo.push(UpdateTreeElement(&mRootElement, 0));
  703. while(!todo.empty())
  704. {
  705. UpdateTreeElement currentUpdateElement = todo.top();
  706. const TreeElement* current = currentUpdateElement.element;
  707. todo.pop();
  708. INT32 yOffset = 0;
  709. if(current->mElement != nullptr)
  710. {
  711. Vector2I curOptimalSize = current->mElement->_getOptimalSize();
  712. optimalSize.x = std::max(optimalSize.x,
  713. (INT32)(INITIAL_INDENT_OFFSET + curOptimalSize.x + currentUpdateElement.indent * INDENT_SIZE));
  714. yOffset = curOptimalSize.y + ELEMENT_EXTRA_SPACING;
  715. }
  716. optimalSize.y += yOffset;
  717. for(auto& child : current->mChildren)
  718. {
  719. if(!child->mIsVisible)
  720. continue;
  721. todo.push(UpdateTreeElement(child, currentUpdateElement.indent + 1));
  722. }
  723. }
  724. if(_getLayoutOptions().fixedWidth)
  725. optimalSize.x = _getLayoutOptions().width;
  726. else
  727. {
  728. if(_getLayoutOptions().minWidth > 0)
  729. optimalSize.x = std::max((INT32)_getLayoutOptions().minWidth, optimalSize.x);
  730. if(_getLayoutOptions().maxWidth > 0)
  731. optimalSize.x = std::min((INT32)_getLayoutOptions().maxWidth, optimalSize.x);
  732. }
  733. if(_getLayoutOptions().fixedHeight)
  734. optimalSize.y = _getLayoutOptions().height;
  735. else
  736. {
  737. if(_getLayoutOptions().minHeight > 0)
  738. optimalSize.y = std::max((INT32)_getLayoutOptions().minHeight, optimalSize.y);
  739. if(_getLayoutOptions().maxHeight > 0)
  740. optimalSize.y = std::min((INT32)_getLayoutOptions().maxHeight, optimalSize.y);
  741. }
  742. }
  743. return optimalSize;
  744. }
  745. void GUISceneTreeView::updateClippedBounds()
  746. {
  747. Vector2I offset = _getOffset();
  748. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  749. RectI localClipRect(mClipRect.x + mOffset.x, mClipRect.y + mOffset.y, mClipRect.width, mClipRect.height);
  750. mClippedBounds.clip(localClipRect);
  751. }
  752. void GUISceneTreeView::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  753. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  754. {
  755. struct UpdateTreeElement
  756. {
  757. UpdateTreeElement(TreeElement* element, UINT32 indent)
  758. :element(element), indent(indent)
  759. { }
  760. TreeElement* element;
  761. UINT32 indent;
  762. };
  763. mVisibleElements.clear();
  764. Stack<UpdateTreeElement>::type todo;
  765. todo.push(UpdateTreeElement(&mRootElement, 0));
  766. // NOTE - Instead of iterating through all elements, try to find those within the clip rect
  767. // and only iterate through those. Others should somehow be marked in-active (similar to GUIElement::isDisabled()?)
  768. Vector<TreeElement*>::type tempOrderedElements;
  769. Vector2I offset(x, y);
  770. while(!todo.empty())
  771. {
  772. UpdateTreeElement currentUpdateElement = todo.top();
  773. TreeElement* current = currentUpdateElement.element;
  774. UINT32 indent = currentUpdateElement.indent;
  775. todo.pop();
  776. INT32 btnHeight = 0;
  777. INT32 yOffset = 0;
  778. if(current->mElement != nullptr)
  779. {
  780. Vector2I elementSize = current->mElement->_getOptimalSize();
  781. btnHeight = elementSize.y;
  782. mVisibleElements.push_back(InteractableElement(current->mParent, current->mSortedIdx * 2 + 0, RectI(x, offset.y, width, ELEMENT_EXTRA_SPACING)));
  783. mVisibleElements.push_back(InteractableElement(current->mParent, current->mSortedIdx * 2 + 1, RectI(x, offset.y + ELEMENT_EXTRA_SPACING, width, btnHeight)));
  784. offset.x = x + INITIAL_INDENT_OFFSET + indent * INDENT_SIZE;
  785. offset.y += ELEMENT_EXTRA_SPACING;
  786. current->mElement->_setOffset(offset);
  787. current->mElement->_setWidth(elementSize.x);
  788. current->mElement->_setHeight(elementSize.y);
  789. current->mElement->_setAreaDepth(areaDepth);
  790. current->mElement->_setWidgetDepth(widgetDepth);
  791. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  792. current->mElement->_setClipRect(elemClipRect);
  793. yOffset = btnHeight;
  794. }
  795. if(current->mFoldoutBtn != nullptr)
  796. {
  797. Vector2I elementSize = current->mFoldoutBtn->_getOptimalSize();
  798. offset.x -= std::min((INT32)INITIAL_INDENT_OFFSET, elementSize.x);
  799. Vector2I myOffset = offset;
  800. myOffset.y -= 2; // TODO: Arbitrary offset, I should adjust it based on font baseline so that the button is nicely centered on text
  801. if(elementSize.y > btnHeight)
  802. {
  803. UINT32 diff = elementSize.y - btnHeight;
  804. float half = diff * 0.5f;
  805. myOffset.y -= Math::floorToInt(half);
  806. }
  807. current->mFoldoutBtn->_setOffset(myOffset);
  808. current->mFoldoutBtn->_setWidth(elementSize.x);
  809. current->mFoldoutBtn->_setHeight(elementSize.y);
  810. current->mFoldoutBtn->_setAreaDepth(areaDepth);
  811. current->mFoldoutBtn->_setWidgetDepth(widgetDepth);
  812. RectI elemClipRect(clipRect.x - myOffset.x, clipRect.y - myOffset.y, clipRect.width, clipRect.height);
  813. current->mFoldoutBtn->_setClipRect(elemClipRect);
  814. }
  815. offset.y += yOffset;
  816. tempOrderedElements.resize(current->mChildren.size(), nullptr);
  817. for(auto& child : current->mChildren)
  818. {
  819. tempOrderedElements[child->mSortedIdx] = child;
  820. }
  821. for(auto iter = tempOrderedElements.rbegin(); iter != tempOrderedElements.rend(); ++iter)
  822. {
  823. TreeElement* child = *iter;
  824. if(!child->mIsVisible)
  825. continue;
  826. todo.push(UpdateTreeElement(child, indent + 1));
  827. }
  828. }
  829. UINT32 remainingHeight = (UINT32)std::max(0, (INT32)height - (offset.y - y));
  830. if(remainingHeight > 0)
  831. mVisibleElements.push_back(InteractableElement(&mRootElement, (UINT32)mRootElement.mChildren.size() * 2, RectI(x, offset.y, width, remainingHeight)));
  832. for(auto selectedElem : mSelectedElements)
  833. {
  834. GUILabel* targetElement = selectedElem.element->mElement;
  835. Vector2I offset = targetElement->_getOffset();
  836. offset.x = x;
  837. selectedElem.background->_setOffset(offset);
  838. selectedElem.background->_setWidth(width);
  839. selectedElem.background->_setHeight(targetElement->_getHeight());
  840. selectedElem.background->_setAreaDepth(areaDepth + 1);
  841. selectedElem.background->_setWidgetDepth(widgetDepth);
  842. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  843. selectedElem.background->_setClipRect(elemClipRect);
  844. }
  845. if(mEditElement != nullptr)
  846. {
  847. GUILabel* targetElement = mEditElement->mElement;
  848. Vector2I offset = targetElement->_getOffset();
  849. UINT32 remainingWidth = (UINT32)std::max(0, (((INT32)width) - (offset.x - x)));
  850. mNameEditBox->_setOffset(offset);
  851. mNameEditBox->_setWidth(remainingWidth);
  852. mNameEditBox->_setHeight(targetElement->_getHeight());
  853. mNameEditBox->_setAreaDepth(areaDepth);
  854. mNameEditBox->_setWidgetDepth(widgetDepth);
  855. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  856. mNameEditBox->_setClipRect(elemClipRect);
  857. }
  858. if(mDragInProgress)
  859. {
  860. const InteractableElement* interactableElement = findElementUnderCoord(mDragPosition);
  861. if(interactableElement == nullptr)
  862. {
  863. if(!mDragHighlight->_isDisabled())
  864. mDragHighlight->disableRecursively();
  865. if(!mDragSepHighlight->_isDisabled())
  866. mDragSepHighlight->disableRecursively();
  867. }
  868. else
  869. {
  870. if(interactableElement->isTreeElement())
  871. {
  872. if(!mDragSepHighlight->_isDisabled())
  873. mDragSepHighlight->disableRecursively();
  874. if(mDragHighlight->_isDisabled())
  875. mDragHighlight->enableRecursively();
  876. Vector2I offset(interactableElement->bounds.x, interactableElement->bounds.y);
  877. mDragHighlight->_setOffset(offset);
  878. mDragHighlight->_setWidth(interactableElement->bounds.width);
  879. mDragHighlight->_setHeight(interactableElement->bounds.height);
  880. mDragHighlight->_setAreaDepth(areaDepth + 1);
  881. mDragHighlight->_setWidgetDepth(widgetDepth);
  882. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  883. mDragHighlight->_setClipRect(elemClipRect);
  884. }
  885. else
  886. {
  887. if(!mDragHighlight->_isDisabled())
  888. mDragHighlight->disableRecursively();
  889. if(mDragSepHighlight->_isDisabled())
  890. mDragSepHighlight->enableRecursively();
  891. Vector2I offset(interactableElement->bounds.x, interactableElement->bounds.y);
  892. mDragSepHighlight->_setOffset(offset);
  893. mDragSepHighlight->_setWidth(interactableElement->bounds.width);
  894. mDragSepHighlight->_setHeight(interactableElement->bounds.height);
  895. mDragSepHighlight->_setAreaDepth(areaDepth + 1);
  896. mDragSepHighlight->_setWidgetDepth(widgetDepth);
  897. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  898. mDragSepHighlight->_setClipRect(elemClipRect);
  899. }
  900. }
  901. }
  902. else
  903. {
  904. if(!mDragHighlight->_isDisabled())
  905. mDragHighlight->disableRecursively();
  906. if(!mDragSepHighlight->_isDisabled())
  907. mDragSepHighlight->disableRecursively();
  908. }
  909. // Update scroll bounds
  910. UINT32 scrollHeight = (UINT32)Math::roundToInt(clipRect.height * SCROLL_AREA_HEIGHT_PCT);
  911. mTopScrollBounds.x = clipRect.x;
  912. mTopScrollBounds.y = clipRect.y;
  913. mTopScrollBounds.width = clipRect.width;
  914. mTopScrollBounds.height = scrollHeight;
  915. mBottomScrollBounds.x = clipRect.x;
  916. mBottomScrollBounds.y = clipRect.y + clipRect.height - scrollHeight;
  917. mBottomScrollBounds.width = clipRect.width;
  918. mBottomScrollBounds.height = scrollHeight;
  919. }
  920. const GUISceneTreeView::InteractableElement* GUISceneTreeView::findElementUnderCoord(const CM::Vector2I& coord) const
  921. {
  922. for(auto& element : mVisibleElements)
  923. {
  924. if(element.bounds.contains(coord))
  925. {
  926. return &element;
  927. }
  928. }
  929. return nullptr;
  930. }
  931. GUISceneTreeView::TreeElement* GUISceneTreeView::getTopMostSelectedElement() const
  932. {
  933. auto topMostElement = mVisibleElements.end();
  934. for(auto& selectedElement : mSelectedElements)
  935. {
  936. auto iterFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  937. [&] (const InteractableElement& x) { return x.getTreeElement() == selectedElement.element; });
  938. if(iterFind != mVisibleElements.end())
  939. {
  940. if(topMostElement == mVisibleElements.end())
  941. topMostElement = iterFind;
  942. else
  943. {
  944. if(iterFind->bounds.y < topMostElement->bounds.y)
  945. topMostElement = iterFind;
  946. }
  947. }
  948. }
  949. if(topMostElement != mVisibleElements.end())
  950. return topMostElement->getTreeElement();
  951. else
  952. return nullptr;
  953. }
  954. GUISceneTreeView::TreeElement* GUISceneTreeView::getBottomMostSelectedElement() const
  955. {
  956. auto& botMostElement = mVisibleElements.end();
  957. for(auto& selectedElement : mSelectedElements)
  958. {
  959. auto iterFind = std::find_if(mVisibleElements.begin(), mVisibleElements.end(),
  960. [&] (const InteractableElement& x) { return x.getTreeElement() == selectedElement.element; });
  961. if(iterFind != mVisibleElements.end())
  962. {
  963. if(botMostElement == mVisibleElements.end())
  964. botMostElement = iterFind;
  965. else
  966. {
  967. if((iterFind->bounds.y + iterFind->bounds.height) > (botMostElement->bounds.y + botMostElement->bounds.height))
  968. botMostElement = iterFind;
  969. }
  970. }
  971. }
  972. if(botMostElement != mVisibleElements.end())
  973. return botMostElement->getTreeElement();
  974. else
  975. return nullptr;
  976. }
  977. void GUISceneTreeView::closeTemporarilyExpandedElements()
  978. {
  979. temporarilyExpandElement(nullptr);
  980. }
  981. void GUISceneTreeView::temporarilyExpandElement(const GUISceneTreeView::InteractableElement* mouseOverElement)
  982. {
  983. TreeElement* treeElement = nullptr;
  984. if(mouseOverElement != nullptr && mouseOverElement->isTreeElement())
  985. treeElement = mouseOverElement->getTreeElement();
  986. if(treeElement == nullptr || treeElement != mMouseOverDragElement)
  987. {
  988. while(!mAutoExpandedElements.empty())
  989. {
  990. TreeElement* autoExpandedElement = mAutoExpandedElements.top();
  991. bool unexpandElement = false;
  992. if(mouseOverElement != nullptr && mouseOverElement->parent != nullptr)
  993. {
  994. if(mouseOverElement->parent != autoExpandedElement && !mouseOverElement->parent->isParentRec(autoExpandedElement))
  995. unexpandElement = true;
  996. else
  997. break;
  998. }
  999. else
  1000. unexpandElement = true;
  1001. if(unexpandElement)
  1002. {
  1003. autoExpandedElement->mIsExpanded = false;
  1004. if(autoExpandedElement->mFoldoutBtn != nullptr)
  1005. autoExpandedElement->mFoldoutBtn->toggleOff();
  1006. mAutoExpandedElements.pop();
  1007. }
  1008. }
  1009. mMouseOverDragElement = treeElement;
  1010. mMouseOverDragElementTime = gTime().getTime();
  1011. }
  1012. else
  1013. {
  1014. if(mMouseOverDragElement != nullptr && !mMouseOverDragElement->mIsExpanded)
  1015. {
  1016. float timeDiff = gTime().getTime() - mMouseOverDragElementTime;
  1017. if(timeDiff >= AUTO_EXPAND_DELAY_SEC)
  1018. {
  1019. mAutoExpandedElements.push(mMouseOverDragElement);
  1020. mMouseOverDragElement->mIsExpanded = true;
  1021. if(mMouseOverDragElement->mFoldoutBtn != nullptr)
  1022. mMouseOverDragElement->mFoldoutBtn->toggleOn();
  1023. }
  1024. }
  1025. }
  1026. }
  1027. void GUISceneTreeView::scrollToElement(TreeElement* element, bool center)
  1028. {
  1029. if(element->mElement == nullptr)
  1030. return;
  1031. GUIScrollArea* scrollArea = findParentScrollArea();
  1032. if(scrollArea == nullptr)
  1033. return;
  1034. if(center)
  1035. {
  1036. RectI myBounds = _getClippedBounds();
  1037. INT32 clipVertCenter = myBounds.y + (INT32)Math::roundToInt(myBounds.height * 0.5f);
  1038. INT32 elemVertCenter = element->mElement->_getOffset().y + (INT32)Math::roundToInt(element->mElement->_getHeight() * 0.5f);
  1039. if(elemVertCenter > clipVertCenter)
  1040. scrollArea->scrollUpPx(elemVertCenter - clipVertCenter);
  1041. else
  1042. scrollArea->scrollDownPx(clipVertCenter - elemVertCenter);
  1043. }
  1044. else
  1045. {
  1046. RectI myBounds = _getClippedBounds();
  1047. INT32 elemVertTop = element->mElement->_getOffset().y;
  1048. INT32 elemVertBottom = element->mElement->_getOffset().y + element->mElement->_getHeight();
  1049. INT32 top = myBounds.y;
  1050. INT32 bottom = myBounds.y + myBounds.height;
  1051. INT32 offset = 0;
  1052. if(elemVertTop < top)
  1053. scrollArea->scrollUpPx(top - elemVertTop);
  1054. else if(elemVertBottom > bottom)
  1055. scrollArea->scrollDownPx(elemVertBottom - bottom);
  1056. }
  1057. }
  1058. GUIScrollArea* GUISceneTreeView::findParentScrollArea() const
  1059. {
  1060. GUIElementBase* parent = _getParent();
  1061. while(parent != nullptr)
  1062. {
  1063. if(parent->_getType() == GUIElementBase::Type::Element)
  1064. {
  1065. GUIElement* parentElement = static_cast<GUIElement*>(parent);
  1066. if(parentElement->getElementType() == GUIElement::ElementType::ScrollArea)
  1067. {
  1068. GUIScrollArea* scrollArea = static_cast<GUIScrollArea*>(parentElement);
  1069. return scrollArea;
  1070. }
  1071. }
  1072. parent = parent->_getParent();
  1073. }
  1074. return nullptr;
  1075. }
  1076. const String& GUISceneTreeView::getGUITypeName()
  1077. {
  1078. static String typeName = "SceneTreeView";
  1079. return typeName;
  1080. }
  1081. }