BsGUISceneTreeView.cpp 36 KB

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