2
0

BsGUIResourceTreeView.cpp 16 KB

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