BsGUIResourceTreeView.cpp 16 KB

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