BsGUIResourceTreeView.cpp 16 KB

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