BsGUIResourceTreeView.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. #include "BsGUIResourceTreeView.h"
  2. #include "BsGUISkin.h"
  3. #include "BsProjectLibrary.h"
  4. #include "BsDragAndDropManager.h"
  5. #include "BsResources.h"
  6. #include "BsResourceManifest.h"
  7. #include "BsProjectLibrary.h"
  8. #include "BsFileSystem.h"
  9. #include "BsGUIWidget.h"
  10. #include "BsViewport.h"
  11. #include "BsRenderWindow.h"
  12. #include "BsPlatform.h"
  13. #include "BsPath.h"
  14. #include "BsSelection.h"
  15. using namespace std::placeholders;
  16. namespace BansheeEngine
  17. {
  18. const MessageId GUIResourceTreeView::SELECTION_CHANGED_MSG = MessageId("ResourceTreeView_SelectionChanged");
  19. GUIResourceTreeView::InternalDraggedResources::InternalDraggedResources(UINT32 numObjects)
  20. :numObjects(numObjects)
  21. {
  22. resourcePaths = bs_newN<Path>(numObjects);
  23. }
  24. GUIResourceTreeView::InternalDraggedResources::~InternalDraggedResources()
  25. {
  26. bs_deleteN(resourcePaths, numObjects);
  27. resourcePaths = nullptr;
  28. }
  29. GUIResourceTreeView::GUIResourceTreeView(const String& backgroundStyle, const String& elementBtnStyle,
  30. const String& foldoutBtnStyle, const String& selectionBackgroundStyle, const String& editBoxStyle,
  31. const String& dragHighlightStyle, const String& dragSepHighlightStyle, const GUILayoutOptions& layoutOptions)
  32. :GUITreeView(backgroundStyle, elementBtnStyle, foldoutBtnStyle, selectionBackgroundStyle, editBoxStyle, dragHighlightStyle,
  33. dragSepHighlightStyle, layoutOptions), mDraggedResources(nullptr), mCurrentWindow(nullptr), mDropTarget(nullptr), mDropTargetDragActive(false)
  34. {
  35. ResourceTreeViewLocator::_provide(this);
  36. ProjectLibrary::instance().onEntryAdded.connect(std::bind(&GUIResourceTreeView::entryAdded, this, _1));
  37. ProjectLibrary::instance().onEntryRemoved.connect(std::bind(&GUIResourceTreeView::entryRemoved, this, _1));
  38. const ProjectLibrary::LibraryEntry* rootEntry = ProjectLibrary::instance().getRootEntry();
  39. mRootElement.mFullPath = rootEntry->path;
  40. mRootElement.mElementName = mRootElement.mFullPath.getWTail();
  41. expandElement(&mRootElement);
  42. updateFromProjectLibraryEntry(&mRootElement, rootEntry);
  43. }
  44. GUIResourceTreeView::~GUIResourceTreeView()
  45. {
  46. clearDropTarget();
  47. ResourceTreeViewLocator::_provide(nullptr);
  48. }
  49. GUIResourceTreeView* GUIResourceTreeView::create(const String& backgroundStyle, const String& elementBtnStyle,
  50. const String& foldoutBtnStyle, const String& selectionBackgroundStyle, const String& editBoxStyle, const String& dragHighlightStyle,
  51. const String& dragSepHighlightStyle)
  52. {
  53. return new (bs_alloc<GUIResourceTreeView, PoolAlloc>()) GUIResourceTreeView(backgroundStyle, elementBtnStyle, foldoutBtnStyle,
  54. selectionBackgroundStyle, editBoxStyle, dragHighlightStyle, dragSepHighlightStyle, GUILayoutOptions::create());
  55. }
  56. GUIResourceTreeView* GUIResourceTreeView::create(const GUIOptions& options, const String& backgroundStyle,
  57. const String& elementBtnStyle, const String& foldoutBtnStyle, const String& selectionBackgroundStyle,
  58. const String& editBoxStyle, const String& dragHighlightStyle, const String& dragSepHighlightStyle)
  59. {
  60. return new (bs_alloc<GUIResourceTreeView, PoolAlloc>()) GUIResourceTreeView(backgroundStyle, elementBtnStyle,
  61. foldoutBtnStyle, selectionBackgroundStyle, editBoxStyle, dragHighlightStyle, dragSepHighlightStyle, GUILayoutOptions::create(options));
  62. }
  63. void GUIResourceTreeView::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height, Rect2I clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  64. {
  65. GUITreeView::_updateLayoutInternal(x, y, width, height, clipRect, widgetDepth, areaDepth);
  66. if(mDropTarget != nullptr)
  67. {
  68. mDropTarget->setArea(x, y, width, height);
  69. }
  70. }
  71. void GUIResourceTreeView::updateTreeElementHierarchy()
  72. {
  73. // Do nothing, updates are handled via callbacks
  74. }
  75. void GUIResourceTreeView::renameTreeElement(GUITreeView::TreeElement* element, const WString& name)
  76. {
  77. ResourceTreeElement* resourceTreeElement = static_cast<ResourceTreeElement*>(element);
  78. Path oldPath = resourceTreeElement->mFullPath;
  79. Path newPath = oldPath.getParent();
  80. newPath.append(name);
  81. ProjectLibrary::instance().moveEntry(oldPath, findUniquePath(newPath));
  82. }
  83. void GUIResourceTreeView::deleteTreeElement(TreeElement* element)
  84. {
  85. ResourceTreeElement* resourceTreeElement = static_cast<ResourceTreeElement*>(element);
  86. ProjectLibrary::instance().deleteEntry(resourceTreeElement->mFullPath);
  87. }
  88. void GUIResourceTreeView::updateFromProjectLibraryEntry(ResourceTreeElement* treeElement, const ProjectLibrary::LibraryEntry* libraryEntry)
  89. {
  90. struct StackElem
  91. {
  92. StackElem(const ProjectLibrary::LibraryEntry* entry, ResourceTreeElement* treeElem)
  93. :entry(entry), treeElem(treeElem)
  94. { }
  95. const ProjectLibrary::LibraryEntry* entry;
  96. ResourceTreeElement* treeElem;
  97. };
  98. if(libraryEntry->type == ProjectLibrary::LibraryEntryType::Directory)
  99. {
  100. Stack<StackElem> todo;
  101. todo.push(StackElem(libraryEntry, treeElement));
  102. while(!todo.empty())
  103. {
  104. StackElem curElem = todo.top();
  105. todo.pop();
  106. const ProjectLibrary::DirectoryEntry* dirEntry = static_cast<const ProjectLibrary::DirectoryEntry*>(curElem.entry);
  107. for(auto& child : dirEntry->mChildren)
  108. {
  109. ResourceTreeElement* newChild = addTreeElement(curElem.treeElem, child->path);
  110. if(child->type == ProjectLibrary::LibraryEntryType::Directory)
  111. todo.push(StackElem(child, newChild));
  112. }
  113. sortTreeElement(curElem.treeElem);
  114. }
  115. }
  116. }
  117. GUIResourceTreeView::ResourceTreeElement* GUIResourceTreeView::addTreeElement(ResourceTreeElement* parent, const Path& fullPath)
  118. {
  119. ResourceTreeElement* newChild = bs_new<ResourceTreeElement>();
  120. newChild->mParent = parent;
  121. newChild->mName = fullPath.getTail();
  122. newChild->mFullPath = fullPath;
  123. newChild->mSortedIdx = (UINT32)parent->mChildren.size();
  124. newChild->mIsVisible = parent->mIsVisible && parent->mIsExpanded;
  125. newChild->mElementName = fullPath.getWTail();
  126. parent->mChildren.push_back(newChild);
  127. updateElementGUI(parent);
  128. updateElementGUI(newChild);
  129. return newChild;
  130. }
  131. void GUIResourceTreeView::deleteTreeElement(ResourceTreeElement* element)
  132. {
  133. closeTemporarilyExpandedElements(); // In case this element is one of them
  134. if(element->mIsSelected)
  135. unselectElement(element);
  136. if(element->mParent != nullptr)
  137. {
  138. auto iterFind = std::find(element->mParent->mChildren.begin(), element->mParent->mChildren.end(), element);
  139. if(iterFind != element->mParent->mChildren.end())
  140. element->mParent->mChildren.erase(iterFind);
  141. sortTreeElement(static_cast<ResourceTreeElement*>(element->mParent));
  142. updateElementGUI(element->mParent);
  143. }
  144. if(&mRootElement != element)
  145. bs_delete(element);
  146. }
  147. void GUIResourceTreeView::sortTreeElement(ResourceTreeElement* element)
  148. {
  149. auto cmp = [&] (const TreeElement* a, const TreeElement* b)
  150. {
  151. return a->mName.compare(b->mName) < 0;
  152. };
  153. std::sort(element->mChildren.begin(), element->mChildren.end(), cmp);
  154. UINT32 idx = 0;
  155. for(auto& child : element->mChildren)
  156. {
  157. child->mSortedIdx = idx;
  158. idx++;
  159. }
  160. }
  161. GUIResourceTreeView::ResourceTreeElement* GUIResourceTreeView::findTreeElement(const Path& fullPath)
  162. {
  163. if (!mRootElement.mFullPath.includes(fullPath))
  164. return nullptr;
  165. Path relPath = fullPath.getRelative(mRootElement.mFullPath);
  166. UINT32 numElems = relPath.getNumDirectories() + (relPath.isFile() ? 1 : 0);
  167. UINT32 idx = 0;
  168. ResourceTreeElement* current = &mRootElement;
  169. while (current != nullptr)
  170. {
  171. if (idx == numElems)
  172. return current;
  173. WString curElem;
  174. if (relPath.isFile() && idx == (numElems - 1))
  175. curElem = relPath.getWFilename();
  176. else
  177. curElem = relPath[idx];
  178. bool foundChild = false;
  179. for (auto& child : current->mChildren)
  180. {
  181. ResourceTreeElement* resourceChild = static_cast<ResourceTreeElement*>(child);
  182. if (Path::comparePathElem(curElem, resourceChild->mElementName))
  183. {
  184. idx++;
  185. current = resourceChild;
  186. foundChild = true;
  187. break;
  188. }
  189. }
  190. if (!foundChild)
  191. current = nullptr;
  192. }
  193. return nullptr;
  194. }
  195. void GUIResourceTreeView::entryAdded(const Path& path)
  196. {
  197. Path parentPath = path.getParent();
  198. ResourceTreeElement* parentElement = findTreeElement(parentPath);
  199. assert(parentElement != nullptr);
  200. ResourceTreeElement* newElement = addTreeElement(parentElement, path);
  201. sortTreeElement(parentElement);
  202. ProjectLibrary::LibraryEntry* libEntry = ProjectLibrary::instance().findEntry(path);
  203. assert(libEntry != nullptr);
  204. updateFromProjectLibraryEntry(newElement, libEntry);
  205. markContentAsDirty();
  206. }
  207. void GUIResourceTreeView::entryRemoved(const Path& path)
  208. {
  209. ResourceTreeElement* treeElement = findTreeElement(path);
  210. if(treeElement != nullptr)
  211. deleteTreeElement(treeElement);
  212. }
  213. void GUIResourceTreeView::setDropTarget(RenderWindow* parentWindow, INT32 x, INT32 y, UINT32 width, UINT32 height)
  214. {
  215. if(mDropTarget != nullptr)
  216. {
  217. Platform::destroyDropTarget(*mDropTarget);
  218. mDropTargetEnterConn.disconnect();
  219. mDropTargetLeaveConn.disconnect();
  220. mDropTargetMoveConn.disconnect();
  221. mDropTargetDroppedConn.disconnect();
  222. }
  223. if(parentWindow != nullptr)
  224. {
  225. mCurrentWindow = parentWindow;
  226. mDropTarget = &Platform::createDropTarget(mCurrentWindow, _getOffset().x, _getOffset().y, _getWidth(), _getHeight());
  227. mDropTargetEnterConn = mDropTarget->onEnter.connect(std::bind(&GUIResourceTreeView::dropTargetDragMove, this, _1, _2));
  228. mDropTargetMoveConn = mDropTarget->onDragOver.connect(std::bind(&GUIResourceTreeView::dropTargetDragMove, this, _1, _2));
  229. mDropTargetLeaveConn = mDropTarget->onLeave.connect(std::bind(&GUIResourceTreeView::dropTargetDragLeave, this));
  230. mDropTargetDroppedConn = mDropTarget->onDrop.connect(std::bind(&GUIResourceTreeView::dropTargetDragDropped, this, _1, _2));
  231. }
  232. else
  233. mDropTarget = nullptr;
  234. }
  235. void GUIResourceTreeView::clearDropTarget()
  236. {
  237. setDropTarget(nullptr, 0, 0, 0, 0);
  238. }
  239. void GUIResourceTreeView::dropTargetDragMove(INT32 x, INT32 y)
  240. {
  241. mDragPosition = Vector2I(x, y);
  242. mDragInProgress = true;
  243. mDropTargetDragActive = true;
  244. markContentAsDirty();
  245. if(mBottomScrollBounds.contains(mDragPosition))
  246. {
  247. if(mScrollState != ScrollState::Down)
  248. mScrollState = ScrollState::TransitioningDown;
  249. }
  250. else if(mTopScrollBounds.contains(mDragPosition))
  251. {
  252. if(mScrollState != ScrollState::Up)
  253. mScrollState = ScrollState::TransitioningUp;
  254. }
  255. else
  256. mScrollState = ScrollState::None;
  257. }
  258. void GUIResourceTreeView::dropTargetDragLeave()
  259. {
  260. mDragInProgress = false;
  261. mDropTargetDragActive = false;
  262. markContentAsDirty();
  263. }
  264. void GUIResourceTreeView::dropTargetDragDropped(INT32 x, INT32 y)
  265. {
  266. const GUITreeView::InteractableElement* element = findElementUnderCoord(Vector2I(x, y));
  267. TreeElement* treeElement = nullptr;
  268. if(element != nullptr)
  269. {
  270. if(element->isTreeElement())
  271. treeElement = element->getTreeElement();
  272. else
  273. treeElement = element->parent;
  274. }
  275. if(mDropTarget->getDropType() == OSDropType::FileList)
  276. {
  277. Vector<WString> fileList = mDropTarget->getFileList();
  278. mDraggedResources = bs_new<InternalDraggedResources>((UINT32)fileList.size());
  279. for(UINT32 i = 0; i < (UINT32)fileList.size(); i++)
  280. mDraggedResources->resourcePaths[i] = fileList[i];
  281. dragAndDropEnded(treeElement);
  282. bs_delete(mDraggedResources);
  283. mDraggedResources = nullptr;
  284. unselectAll();
  285. }
  286. mDragInProgress = false;
  287. mDropTargetDragActive = false;
  288. markContentAsDirty();
  289. }
  290. Path GUIResourceTreeView::findUniquePath(const Path& path)
  291. {
  292. if(FileSystem::exists(path))
  293. {
  294. Path newPath = path;
  295. WString filename = path.getWFilename(false);
  296. UINT32 cnt = 1;
  297. do
  298. {
  299. newPath.setBasename(filename + toWString(cnt));
  300. cnt++;
  301. } while (FileSystem::exists(newPath));
  302. return newPath;
  303. }
  304. else
  305. return path;
  306. }
  307. bool GUIResourceTreeView::acceptDragAndDrop() const
  308. {
  309. return mDropTargetDragActive || DragAndDropManager::instance().isDragInProgress() && DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::Resources;
  310. }
  311. void GUIResourceTreeView::dragAndDropStart()
  312. {
  313. assert(mDraggedResources == nullptr);
  314. DraggedResources* draggedResources = bs_new<DraggedResources>();
  315. InternalDraggedResources* internalDraggedResources = bs_new<InternalDraggedResources>((UINT32)mSelectedElements.size());
  316. UINT32 cnt = 0;
  317. for(auto& selectedElement : mSelectedElements)
  318. {
  319. ResourceTreeElement* resourceTreeElement = static_cast<ResourceTreeElement*>(selectedElement.element);
  320. internalDraggedResources->resourcePaths[cnt] = resourceTreeElement->mFullPath;
  321. String uuid;
  322. if(gResources().getUUIDFromFilePath(internalDraggedResources->resourcePaths[cnt], uuid))
  323. draggedResources->resourceUUIDs.push_back(uuid);
  324. cnt++;
  325. }
  326. mDraggedResources = internalDraggedResources;
  327. DragAndDropManager::instance().startDrag((UINT32)DragAndDropType::Resources, (void*)draggedResources,
  328. std::bind(&GUIResourceTreeView::dragAndDropFinalize, this), true);
  329. }
  330. void GUIResourceTreeView::dragAndDropEnded(TreeElement* overTreeElement)
  331. {
  332. if(overTreeElement != nullptr && mDraggedResources != nullptr)
  333. {
  334. ResourceTreeElement* resourceTreeElement = static_cast<ResourceTreeElement*>(overTreeElement);
  335. Path destDir = resourceTreeElement->mFullPath;
  336. if(FileSystem::isFile(destDir))
  337. destDir = destDir.getParent();
  338. for(UINT32 i = 0; i < mDraggedResources->numObjects; i++)
  339. {
  340. WString filename = mDraggedResources->resourcePaths[i].getWFilename();
  341. Path currentParent = mDraggedResources->resourcePaths[i].getParent();
  342. if(currentParent != destDir)
  343. {
  344. Path newPath = destDir;
  345. newPath.append(filename);
  346. ProjectLibrary::instance().moveEntry(mDraggedResources->resourcePaths[i], findUniquePath(newPath));
  347. }
  348. }
  349. }
  350. }
  351. void GUIResourceTreeView::dragAndDropFinalize()
  352. {
  353. mDragInProgress = false;
  354. markContentAsDirty();
  355. DraggedResources* draggedResources = reinterpret_cast<DraggedResources*>(DragAndDropManager::instance().getDragData());
  356. bs_delete(draggedResources);
  357. if(mDraggedResources != nullptr)
  358. {
  359. bs_delete(mDraggedResources);
  360. mDraggedResources = nullptr;
  361. }
  362. }
  363. void GUIResourceTreeView::_changeParentWidget(GUIWidget* widget)
  364. {
  365. GUITreeView::_changeParentWidget(widget);
  366. if (widget != nullptr && widget->getTarget()->getTarget()->getProperties().isWindow())
  367. {
  368. RenderWindow* parentWindow = static_cast<RenderWindow*>(widget->getTarget()->getTarget().get());
  369. setDropTarget(parentWindow, _getOffset().x, _getOffset().y, _getWidth(), _getHeight());
  370. }
  371. else
  372. clearDropTarget();
  373. }
  374. bool GUIResourceTreeView::_acceptDragAndDrop(const Vector2I position, UINT32 typeId) const
  375. {
  376. return typeId == (UINT32)DragAndDropType::Resources;
  377. }
  378. void GUIResourceTreeView::selectionChanged()
  379. {
  380. onSelectionChanged();
  381. sendMessage(SELECTION_CHANGED_MSG);
  382. }
  383. Vector<Path> GUIResourceTreeView::getSelection() const
  384. {
  385. Vector<Path> selectedPaths;
  386. for (auto& selectedElem : mSelectedElements)
  387. {
  388. ResourceTreeElement* resTreeElement = static_cast<ResourceTreeElement*>(selectedElem.element);
  389. selectedPaths.push_back(resTreeElement->mFullPath);
  390. }
  391. return selectedPaths;
  392. }
  393. void GUIResourceTreeView::setSelection(const Vector<Path>& paths)
  394. {
  395. unselectAll();
  396. ResourceTreeElement& root = mRootElement;
  397. Stack<ResourceTreeElement*> todo;
  398. todo.push(&mRootElement);
  399. while (!todo.empty())
  400. {
  401. ResourceTreeElement* currentElem = todo.top();
  402. todo.pop();
  403. auto iterFind = std::find(paths.begin(), paths.end(), currentElem->mFullPath);
  404. if (iterFind != paths.end())
  405. {
  406. expandToElement(currentElem);
  407. selectElement(currentElem);
  408. }
  409. for (auto& child : currentElem->mChildren)
  410. {
  411. ResourceTreeElement* sceneChild = static_cast<ResourceTreeElement*>(child);
  412. todo.push(sceneChild);
  413. }
  414. }
  415. }
  416. const String& GUIResourceTreeView::getGUITypeName()
  417. {
  418. static String typeName = "ResourceTreeView";
  419. return typeName;
  420. }
  421. }