BsGUIResourceTreeView.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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, UINT16 panelDepth, UINT16 panelDepthRange)
  64. {
  65. GUITreeView::_updateLayoutInternal(x, y, width, height, clipRect, widgetDepth, panelDepth, panelDepthRange);
  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. draggedResources->resourcePaths.push_back(internalDraggedResources->resourcePaths[cnt]);
  322. cnt++;
  323. }
  324. mDraggedResources = internalDraggedResources;
  325. DragAndDropManager::instance().startDrag((UINT32)DragAndDropType::Resources, (void*)draggedResources,
  326. std::bind(&GUIResourceTreeView::dragAndDropFinalize, this), true);
  327. }
  328. void GUIResourceTreeView::dragAndDropEnded(TreeElement* overTreeElement)
  329. {
  330. if(overTreeElement != nullptr && mDraggedResources != nullptr)
  331. {
  332. ResourceTreeElement* resourceTreeElement = static_cast<ResourceTreeElement*>(overTreeElement);
  333. Path destDir = resourceTreeElement->mFullPath;
  334. if(FileSystem::isFile(destDir))
  335. destDir = destDir.getParent();
  336. for(UINT32 i = 0; i < mDraggedResources->numObjects; i++)
  337. {
  338. WString filename = mDraggedResources->resourcePaths[i].getWFilename();
  339. Path currentParent = mDraggedResources->resourcePaths[i].getParent();
  340. if(currentParent != destDir)
  341. {
  342. Path newPath = destDir;
  343. newPath.append(filename);
  344. ProjectLibrary::instance().moveEntry(mDraggedResources->resourcePaths[i], findUniquePath(newPath));
  345. }
  346. }
  347. }
  348. }
  349. void GUIResourceTreeView::dragAndDropFinalize()
  350. {
  351. mDragInProgress = false;
  352. markContentAsDirty();
  353. DraggedResources* draggedResources = reinterpret_cast<DraggedResources*>(DragAndDropManager::instance().getDragData());
  354. bs_delete(draggedResources);
  355. if(mDraggedResources != nullptr)
  356. {
  357. bs_delete(mDraggedResources);
  358. mDraggedResources = nullptr;
  359. }
  360. }
  361. void GUIResourceTreeView::_changeParentWidget(GUIWidget* widget)
  362. {
  363. GUITreeView::_changeParentWidget(widget);
  364. if (widget != nullptr && widget->getTarget()->getTarget()->getProperties().isWindow())
  365. {
  366. RenderWindow* parentWindow = static_cast<RenderWindow*>(widget->getTarget()->getTarget().get());
  367. setDropTarget(parentWindow, _getOffset().x, _getOffset().y, _getWidth(), _getHeight());
  368. }
  369. else
  370. clearDropTarget();
  371. }
  372. bool GUIResourceTreeView::_acceptDragAndDrop(const Vector2I position, UINT32 typeId) const
  373. {
  374. return typeId == (UINT32)DragAndDropType::Resources;
  375. }
  376. void GUIResourceTreeView::selectionChanged()
  377. {
  378. onSelectionChanged();
  379. sendMessage(SELECTION_CHANGED_MSG);
  380. }
  381. Vector<Path> GUIResourceTreeView::getSelection() const
  382. {
  383. Vector<Path> selectedPaths;
  384. for (auto& selectedElem : mSelectedElements)
  385. {
  386. ResourceTreeElement* resTreeElement = static_cast<ResourceTreeElement*>(selectedElem.element);
  387. selectedPaths.push_back(resTreeElement->mFullPath);
  388. }
  389. return selectedPaths;
  390. }
  391. void GUIResourceTreeView::setSelection(const Vector<Path>& paths)
  392. {
  393. unselectAll();
  394. ResourceTreeElement& root = mRootElement;
  395. Stack<ResourceTreeElement*> todo;
  396. todo.push(&mRootElement);
  397. while (!todo.empty())
  398. {
  399. ResourceTreeElement* currentElem = todo.top();
  400. todo.pop();
  401. auto iterFind = std::find(paths.begin(), paths.end(), currentElem->mFullPath);
  402. if (iterFind != paths.end())
  403. {
  404. expandToElement(currentElem);
  405. selectElement(currentElem);
  406. }
  407. for (auto& child : currentElem->mChildren)
  408. {
  409. ResourceTreeElement* sceneChild = static_cast<ResourceTreeElement*>(child);
  410. todo.push(sceneChild);
  411. }
  412. }
  413. }
  414. const String& GUIResourceTreeView::getGUITypeName()
  415. {
  416. static String typeName = "ResourceTreeView";
  417. return typeName;
  418. }
  419. }