BsGUISceneTreeView.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. // Calculate the sorted index of the element based on its name
  147. TreeElement* parent = element->mParent;
  148. if(parent != nullptr)
  149. {
  150. for(UINT32 i = 0; i < (UINT32)parent->mChildren.size(); i++)
  151. {
  152. INT32 stringCompare = element->mName.compare(parent->mChildren[i]->mName);
  153. if(stringCompare > 0)
  154. {
  155. if(element->mSortedIdx < parent->mChildren[i]->mSortedIdx)
  156. std::swap(element->mSortedIdx, parent->mChildren[i]->mSortedIdx);
  157. }
  158. else if(stringCompare < 0)
  159. {
  160. if(element->mSortedIdx > parent->mChildren[i]->mSortedIdx)
  161. std::swap(element->mSortedIdx, parent->mChildren[i]->mSortedIdx);
  162. }
  163. }
  164. }
  165. for(UINT32 i = 0; i < (UINT32)element->mChildren.size(); i++)
  166. {
  167. SceneTreeElement* sceneElement = static_cast<SceneTreeElement*>(element->mChildren[i]);
  168. updateTreeElement(sceneElement);
  169. }
  170. }
  171. void GUISceneTreeView::updateTreeElementHierarchy()
  172. {
  173. HSceneObject root = gCoreSceneManager().getRootNode();
  174. mRootElement.mSceneObject = root;
  175. mRootElement.mId = root->getInstanceId();
  176. mRootElement.mSortedIdx = 0;
  177. mRootElement.mIsExpanded = true;
  178. updateTreeElement(&mRootElement);
  179. }
  180. void GUISceneTreeView::renameTreeElement(GUITreeView::TreeElement* element, const WString& name)
  181. {
  182. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(element);
  183. HSceneObject so = sceneTreeElement->mSceneObject;
  184. CmdRecordSO::execute(so, L"Renamed \"" + toWString(so->getName()) + L"\"");
  185. so->setName("mName");
  186. }
  187. void GUISceneTreeView::deleteTreeElement(TreeElement* element)
  188. {
  189. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(element);
  190. HSceneObject so = sceneTreeElement->mSceneObject;
  191. CmdDeleteSO::execute(so, L"Deleted \"" + toWString(so->getName()) + L"\"");
  192. }
  193. void GUISceneTreeView::deleteTreeElementInternal(GUITreeView::TreeElement* element)
  194. {
  195. closeTemporarilyExpandedElements(); // In case this element is one of them
  196. if (element->mIsHighlighted)
  197. clearPing();
  198. if(element->mIsSelected)
  199. unselectElement(element);
  200. bs_delete(element);
  201. }
  202. bool GUISceneTreeView::acceptDragAndDrop() const
  203. {
  204. return DragAndDropManager::instance().isDragInProgress() &&
  205. (DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::SceneObject ||
  206. DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::Resources);
  207. }
  208. void GUISceneTreeView::dragAndDropStart()
  209. {
  210. DraggedSceneObjects* draggedSceneObjects = bs_new<DraggedSceneObjects>((UINT32)mSelectedElements.size());
  211. UINT32 cnt = 0;
  212. for(auto& selectedElement : mSelectedElements)
  213. {
  214. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(selectedElement.element);
  215. draggedSceneObjects->objects[cnt] = sceneTreeElement->mSceneObject;
  216. cnt++;
  217. }
  218. DragAndDropManager::instance().startDrag((UINT32)DragAndDropType::SceneObject, (void*)draggedSceneObjects,
  219. std::bind(&GUISceneTreeView::dragAndDropFinalize, this), false);
  220. }
  221. void GUISceneTreeView::dragAndDropEnded(TreeElement* overTreeElement)
  222. {
  223. UINT32 dragTypeId = DragAndDropManager::instance().getDragTypeId();
  224. if (dragTypeId == (UINT32)DragAndDropType::SceneObject)
  225. {
  226. if (overTreeElement != nullptr)
  227. {
  228. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
  229. Vector<HSceneObject> sceneObjects;
  230. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(overTreeElement);
  231. HSceneObject newParent = sceneTreeElement->mSceneObject;
  232. for (UINT32 i = 0; i < draggedSceneObjects->numObjects; i++)
  233. {
  234. if (draggedSceneObjects->objects[i] != newParent)
  235. sceneObjects.push_back(draggedSceneObjects->objects[i]);
  236. }
  237. CmdReparentSO::execute(sceneObjects, newParent);
  238. }
  239. }
  240. else if (dragTypeId == (UINT32)DragAndDropType::Resources)
  241. {
  242. DraggedResources* draggedResources = reinterpret_cast<DraggedResources*>(DragAndDropManager::instance().getDragData());
  243. HSceneObject newParent;
  244. if (overTreeElement != nullptr)
  245. {
  246. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(overTreeElement);
  247. newParent = sceneTreeElement->mSceneObject;
  248. }
  249. for (auto& path : draggedResources->resourcePaths)
  250. {
  251. ProjectLibrary::LibraryEntry* entry = ProjectLibrary::instance().findEntry(path);
  252. if (entry != nullptr && entry->type == ProjectLibrary::LibraryEntryType::File)
  253. {
  254. ProjectLibrary::ResourceEntry* resEntry = static_cast<ProjectLibrary::ResourceEntry*>(entry);
  255. if (resEntry->meta->getTypeID() == TID_Prefab)
  256. {
  257. HPrefab prefab = static_resource_cast<Prefab>(gResources().loadFromUUID(resEntry->meta->getUUID()));
  258. if (prefab != nullptr)
  259. {
  260. HSceneObject instance = prefab->instantiate();
  261. if (newParent != nullptr)
  262. instance->setParent(newParent);
  263. }
  264. }
  265. }
  266. }
  267. }
  268. }
  269. void GUISceneTreeView::dragAndDropFinalize()
  270. {
  271. mDragInProgress = false;
  272. _markLayoutAsDirty();
  273. if (DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::SceneObject)
  274. {
  275. DraggedSceneObjects* draggedSceneObjects = reinterpret_cast<DraggedSceneObjects*>(DragAndDropManager::instance().getDragData());
  276. bs_delete(draggedSceneObjects);
  277. }
  278. }
  279. bool GUISceneTreeView::_acceptDragAndDrop(const Vector2I position, UINT32 typeId) const
  280. {
  281. return typeId == (UINT32)DragAndDropType::SceneObject || typeId == (UINT32)DragAndDropType::Resources;
  282. }
  283. void GUISceneTreeView::selectionChanged()
  284. {
  285. onSelectionChanged();
  286. sendMessage(SELECTION_CHANGED_MSG);
  287. }
  288. Vector<HSceneObject> GUISceneTreeView::getSelection() const
  289. {
  290. Vector<HSceneObject> selectedSOs;
  291. for (auto& selectedElem : mSelectedElements)
  292. {
  293. SceneTreeElement* sceneTreeElement = static_cast<SceneTreeElement*>(selectedElem.element);
  294. selectedSOs.push_back(sceneTreeElement->mSceneObject);
  295. }
  296. return selectedSOs;
  297. }
  298. void GUISceneTreeView::setSelection(const Vector<HSceneObject>& objects)
  299. {
  300. unselectAll();
  301. SceneTreeElement& root = mRootElement;
  302. Stack<SceneTreeElement*> todo;
  303. todo.push(&mRootElement);
  304. while (!todo.empty())
  305. {
  306. SceneTreeElement* currentElem = todo.top();
  307. todo.pop();
  308. auto iterFind = std::find(objects.begin(), objects.end(), currentElem->mSceneObject);
  309. if (iterFind != objects.end())
  310. {
  311. expandToElement(currentElem);
  312. selectElement(currentElem);
  313. }
  314. for (auto& child : currentElem->mChildren)
  315. {
  316. SceneTreeElement* sceneChild = static_cast<SceneTreeElement*>(child);
  317. todo.push(sceneChild);
  318. }
  319. }
  320. }
  321. void GUISceneTreeView::ping(const HSceneObject& object)
  322. {
  323. SceneTreeElement& root = mRootElement;
  324. Stack<SceneTreeElement*> todo;
  325. todo.push(&mRootElement);
  326. while (!todo.empty())
  327. {
  328. SceneTreeElement* currentElem = todo.top();
  329. todo.pop();
  330. if (currentElem->mSceneObject == object)
  331. {
  332. GUITreeView::ping(currentElem);
  333. break;
  334. }
  335. for (auto& child : currentElem->mChildren)
  336. {
  337. SceneTreeElement* sceneChild = static_cast<SceneTreeElement*>(child);
  338. todo.push(sceneChild);
  339. }
  340. }
  341. }
  342. const String& GUISceneTreeView::getGUITypeName()
  343. {
  344. static String typeName = "SceneTreeView";
  345. return typeName;
  346. }
  347. }