BsGUISceneTreeView.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. #include "BsGUISceneTreeView.h"
  2. #include "BsSceneObject.h"
  3. #include "BsSceneManager.h"
  4. #include "BsGUISkin.h"
  5. #include "BsCmdRecordSO.h"
  6. #include "BsCmdReparentSO.h"
  7. #include "BsCmdDeleteSO.h"
  8. #include "BsDragAndDropManager.h"
  9. #include "BsSelection.h"
  10. #include "BsGUIResourceTreeView.h"
  11. #include "BsProjectLibrary.h"
  12. #include "BsProjectResourceMeta.h"
  13. #include "BsPrefab.h"
  14. #include "BsResources.h"
  15. namespace BansheeEngine
  16. {
  17. const MessageId GUISceneTreeView::SELECTION_CHANGED_MSG = MessageId("SceneTreeView_SelectionChanged");
  18. DraggedSceneObjects::DraggedSceneObjects(UINT32 numObjects)
  19. :numObjects(numObjects)
  20. {
  21. objects = bs_newN<HSceneObject>(numObjects);
  22. }
  23. DraggedSceneObjects::~DraggedSceneObjects()
  24. {
  25. bs_deleteN(objects, numObjects);
  26. objects = nullptr;
  27. }
  28. GUISceneTreeView::GUISceneTreeView(const String& backgroundStyle, const String& elementBtnStyle,
  29. const String& foldoutBtnStyle, const String& highlightBackgroundStyle, const String& selectionBackgroundStyle,
  30. const String& editBoxStyle, const String& dragHighlightStyle, const String& dragSepHighlightStyle, const GUIDimensions& dimensions)
  31. :GUITreeView(backgroundStyle, elementBtnStyle, foldoutBtnStyle, highlightBackgroundStyle, selectionBackgroundStyle, editBoxStyle, dragHighlightStyle,
  32. dragSepHighlightStyle, dimensions)
  33. {
  34. SceneTreeViewLocator::_provide(this);
  35. }
  36. GUISceneTreeView::~GUISceneTreeView()
  37. {
  38. SceneTreeViewLocator::_provide(nullptr);
  39. }
  40. GUISceneTreeView* GUISceneTreeView::create(const String& backgroundStyle, const String& elementBtnStyle, const String& foldoutBtnStyle,
  41. const String& highlightBackgroundStyle, const String& selectionBackgroundStyle, const String& editBoxStyle, const String& dragHighlightStyle,
  42. const String& dragSepHighlightStyle)
  43. {
  44. return new (bs_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(backgroundStyle, elementBtnStyle, foldoutBtnStyle,
  45. highlightBackgroundStyle, selectionBackgroundStyle, editBoxStyle, dragHighlightStyle, dragSepHighlightStyle, GUIDimensions::create());
  46. }
  47. GUISceneTreeView* GUISceneTreeView::create(const GUIOptions& options, const String& backgroundStyle, const String& elementBtnStyle,
  48. const String& foldoutBtnStyle, const String& highlightBackgroundStyle, const String& selectionBackgroundStyle,
  49. const String& editBoxStyle, const String& dragHighlightStyle, const String& dragSepHighlightStyle)
  50. {
  51. return new (bs_alloc<GUISceneTreeView, PoolAlloc>()) GUISceneTreeView(backgroundStyle, elementBtnStyle,
  52. foldoutBtnStyle, highlightBackgroundStyle, selectionBackgroundStyle, editBoxStyle,
  53. dragHighlightStyle, dragSepHighlightStyle, GUIDimensions::create(options));
  54. }
  55. void GUISceneTreeView::updateTreeElement(SceneTreeElement* element)
  56. {
  57. HSceneObject currentSO = element->mSceneObject;
  58. // Check if SceneObject has changed in any way and update the tree element
  59. // Early exit case - Most commonly there will be no changes between active and cached data so
  60. // we first do a quick check in order to avoid expensive comparison later
  61. bool completeMatch = true;
  62. UINT32 visibleChildCount = 0;
  63. for (UINT32 i = 0; i < currentSO->getNumChildren(); i++)
  64. {
  65. if (i >= element->mChildren.size())
  66. {
  67. completeMatch = false;
  68. break;
  69. }
  70. HSceneObject currentSOChild = currentSO->getChild(i);
  71. #if BS_DEBUG_MODE == 0
  72. if (currentSOChild->hasFlag(SOF_Internal))
  73. continue;
  74. #endif
  75. SceneTreeElement* currentChild = static_cast<SceneTreeElement*>(element->mChildren[visibleChildCount]);
  76. visibleChildCount++;
  77. UINT64 curId = currentSOChild->getInstanceId();
  78. if (curId != currentChild->mId)
  79. {
  80. completeMatch = false;
  81. break;
  82. }
  83. }
  84. completeMatch &= visibleChildCount == element->mChildren.size();
  85. // Not a complete match, compare everything and insert/delete elements as needed
  86. bool needsUpdate = false;
  87. if(!completeMatch)
  88. {
  89. Vector<TreeElement*> newChildren;
  90. bool* tempToDelete = (bool*)bs_stack_alloc(sizeof(bool) * (UINT32)element->mChildren.size());
  91. for(UINT32 i = 0; i < (UINT32)element->mChildren.size(); i++)
  92. tempToDelete[i] = true;
  93. for(UINT32 i = 0; i < currentSO->getNumChildren(); i++)
  94. {
  95. HSceneObject currentSOChild = currentSO->getChild(i);
  96. #if BS_DEBUG_MODE == 0
  97. if (currentSOChild->hasFlag(SOF_Internal))
  98. continue;
  99. #endif
  100. UINT64 curId = currentSOChild->getInstanceId();
  101. bool found = false;
  102. for(UINT32 j = 0; j < element->mChildren.size(); j++)
  103. {
  104. SceneTreeElement* currentChild = static_cast<SceneTreeElement*>(element->mChildren[j]);
  105. if(curId == currentChild->mId)
  106. {
  107. tempToDelete[j] = false;
  108. currentChild->mSortedIdx = (UINT32)newChildren.size();
  109. newChildren.push_back(currentChild);
  110. found = true;
  111. break;
  112. }
  113. }
  114. if(!found)
  115. {
  116. SceneTreeElement* newChild = bs_new<SceneTreeElement>();
  117. newChild->mParent = element;
  118. newChild->mSceneObject = currentSOChild;
  119. newChild->mId = currentSOChild->getInstanceId();
  120. newChild->mName = currentSOChild->getName();
  121. newChild->mSortedIdx = (UINT32)newChildren.size();
  122. newChild->mIsVisible = element->mIsVisible && element->mIsExpanded;
  123. newChildren.push_back(newChild);
  124. updateElementGUI(newChild);
  125. }
  126. }
  127. for(UINT32 i = 0; i < element->mChildren.size(); i++)
  128. {
  129. if(!tempToDelete[i])
  130. continue;
  131. deleteTreeElementInternal(element->mChildren[i]);
  132. }
  133. bs_stack_free(tempToDelete);
  134. element->mChildren = newChildren;
  135. needsUpdate = true;
  136. }
  137. // Check if name needs updating
  138. const String& name = element->mSceneObject->getName();
  139. if(element->mName != name)
  140. {
  141. element->mName = name;
  142. needsUpdate = true;
  143. }
  144. if(needsUpdate)
  145. updateElementGUI(element);
  146. for(UINT32 i = 0; i < (UINT32)element->mChildren.size(); i++)
  147. {
  148. SceneTreeElement* sceneElement = static_cast<SceneTreeElement*>(element->mChildren[i]);
  149. updateTreeElement(sceneElement);
  150. }
  151. // Calculate the sorted index of the elements based on their name
  152. bs_frame_mark();
  153. FrameVector<SceneTreeElement*> sortVector;
  154. for (auto& child : element->mChildren)
  155. sortVector.push_back(static_cast<SceneTreeElement*>(child));
  156. std::sort(sortVector.begin(), sortVector.end(),
  157. [&](const SceneTreeElement* lhs, const SceneTreeElement* rhs)
  158. {
  159. return StringUtil::compare(lhs->mName, rhs->mName, false) < 0;
  160. });
  161. UINT32 idx = 0;
  162. for (auto& child : sortVector)
  163. {
  164. child->mSortedIdx = idx;
  165. idx++;
  166. }
  167. bs_frame_clear();
  168. }
  169. void GUISceneTreeView::updateTreeElementHierarchy()
  170. {
  171. HSceneObject root = gCoreSceneManager().getRootNode();
  172. mRootElement.mSceneObject = root;
  173. mRootElement.mId = root->getInstanceId();
  174. mRootElement.mSortedIdx = 0;
  175. mRootElement.mIsExpanded = true;
  176. updateTreeElement(&mRootElement);
  177. }
  178. void GUISceneTreeView::renameTreeElement(GUITreeView::TreeElement* element, const WString& name)
  179. {
  180. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(element);
  181. HSceneObject so = sceneTreeElement->mSceneObject;
  182. CmdRecordSO::execute(so, L"Renamed \"" + toWString(so->getName()) + L"\"");
  183. so->setName("mName");
  184. }
  185. void GUISceneTreeView::deleteTreeElement(TreeElement* element)
  186. {
  187. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(element);
  188. HSceneObject so = sceneTreeElement->mSceneObject;
  189. CmdDeleteSO::execute(so, L"Deleted \"" + toWString(so->getName()) + L"\"");
  190. }
  191. void GUISceneTreeView::deleteTreeElementInternal(GUITreeView::TreeElement* element)
  192. {
  193. closeTemporarilyExpandedElements(); // In case this element is one of them
  194. if (element->mIsHighlighted)
  195. clearPing();
  196. if(element->mIsSelected)
  197. unselectElement(element);
  198. bs_delete(element);
  199. }
  200. bool GUISceneTreeView::acceptDragAndDrop() const
  201. {
  202. return DragAndDropManager::instance().isDragInProgress() &&
  203. (DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::SceneObject ||
  204. DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::Resources);
  205. }
  206. void GUISceneTreeView::dragAndDropStart()
  207. {
  208. DraggedSceneObjects* draggedSceneObjects = bs_new<DraggedSceneObjects>((UINT32)mSelectedElements.size());
  209. UINT32 cnt = 0;
  210. for(auto& selectedElement : mSelectedElements)
  211. {
  212. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(selectedElement.element);
  213. draggedSceneObjects->objects[cnt] = sceneTreeElement->mSceneObject;
  214. cnt++;
  215. }
  216. DragAndDropManager::instance().startDrag((UINT32)DragAndDropType::SceneObject, (void*)draggedSceneObjects,
  217. std::bind(&GUISceneTreeView::dragAndDropFinalize, this), false);
  218. }
  219. void GUISceneTreeView::dragAndDropEnded(TreeElement* overTreeElement)
  220. {
  221. UINT32 dragTypeId = DragAndDropManager::instance().getDragTypeId();
  222. if (dragTypeId == (UINT32)DragAndDropType::SceneObject)
  223. {
  224. if (overTreeElement != nullptr)
  225. {
  226. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
  227. Vector<HSceneObject> sceneObjects;
  228. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(overTreeElement);
  229. HSceneObject newParent = sceneTreeElement->mSceneObject;
  230. for (UINT32 i = 0; i < draggedSceneObjects->numObjects; i++)
  231. {
  232. if (draggedSceneObjects->objects[i] != newParent)
  233. sceneObjects.push_back(draggedSceneObjects->objects[i]);
  234. }
  235. CmdReparentSO::execute(sceneObjects, newParent);
  236. }
  237. }
  238. else if (dragTypeId == (UINT32)DragAndDropType::Resources)
  239. {
  240. DraggedResources* draggedResources = reinterpret_cast<DraggedResources*>(DragAndDropManager::instance().getDragData());
  241. HSceneObject newParent;
  242. if (overTreeElement != nullptr)
  243. {
  244. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(overTreeElement);
  245. newParent = sceneTreeElement->mSceneObject;
  246. }
  247. for (auto& path : draggedResources->resourcePaths)
  248. {
  249. ProjectLibrary::LibraryEntry* entry = ProjectLibrary::instance().findEntry(path);
  250. if (entry != nullptr && entry->type == ProjectLibrary::LibraryEntryType::File)
  251. {
  252. ProjectLibrary::ResourceEntry* resEntry = static_cast<ProjectLibrary::ResourceEntry*>(entry);
  253. if (resEntry->meta->getTypeID() == TID_Prefab)
  254. {
  255. HPrefab prefab = static_resource_cast<Prefab>(gResources().loadFromUUID(resEntry->meta->getUUID()));
  256. if (prefab != nullptr)
  257. {
  258. HSceneObject instance = prefab->instantiate();
  259. if (newParent != nullptr)
  260. instance->setParent(newParent);
  261. }
  262. }
  263. }
  264. }
  265. }
  266. }
  267. void GUISceneTreeView::dragAndDropFinalize()
  268. {
  269. mDragInProgress = false;
  270. _markLayoutAsDirty();
  271. if (DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::SceneObject)
  272. {
  273. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
  274. bs_delete(draggedSceneObjects);
  275. }
  276. }
  277. bool GUISceneTreeView::_acceptDragAndDrop(const Vector2I position, UINT32 typeId) const
  278. {
  279. return typeId == (UINT32)DragAndDropType::SceneObject || typeId == (UINT32)DragAndDropType::Resources;
  280. }
  281. void GUISceneTreeView::selectionChanged()
  282. {
  283. onSelectionChanged();
  284. sendMessage(SELECTION_CHANGED_MSG);
  285. }
  286. Vector<HSceneObject> GUISceneTreeView::getSelection() const
  287. {
  288. Vector<HSceneObject> selectedSOs;
  289. for (auto& selectedElem : mSelectedElements)
  290. {
  291. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(selectedElem.element);
  292. selectedSOs.push_back(sceneTreeElement->mSceneObject);
  293. }
  294. return selectedSOs;
  295. }
  296. void GUISceneTreeView::setSelection(const Vector<HSceneObject>& objects)
  297. {
  298. unselectAll();
  299. SceneTreeElement& root = mRootElement;
  300. Stack<SceneTreeElement*> todo;
  301. todo.push(&mRootElement);
  302. while (!todo.empty())
  303. {
  304. SceneTreeElement* currentElem = todo.top();
  305. todo.pop();
  306. auto iterFind = std::find(objects.begin(), objects.end(), currentElem->mSceneObject);
  307. if (iterFind != objects.end())
  308. {
  309. expandToElement(currentElem);
  310. selectElement(currentElem);
  311. }
  312. for (auto& child : currentElem->mChildren)
  313. {
  314. SceneTreeElement* sceneChild = static_cast<SceneTreeElement*>(child);
  315. todo.push(sceneChild);
  316. }
  317. }
  318. }
  319. void GUISceneTreeView::ping(const HSceneObject& object)
  320. {
  321. SceneTreeElement& root = mRootElement;
  322. Stack<SceneTreeElement*> todo;
  323. todo.push(&mRootElement);
  324. while (!todo.empty())
  325. {
  326. SceneTreeElement* currentElem = todo.top();
  327. todo.pop();
  328. if (currentElem->mSceneObject == object)
  329. {
  330. GUITreeView::ping(currentElem);
  331. break;
  332. }
  333. for (auto& child : currentElem->mChildren)
  334. {
  335. SceneTreeElement* sceneChild = static_cast<SceneTreeElement*>(child);
  336. todo.push(sceneChild);
  337. }
  338. }
  339. }
  340. const String& GUISceneTreeView::getGUITypeName()
  341. {
  342. static String typeName = "SceneTreeView";
  343. return typeName;
  344. }
  345. }