BsGUIResourceTreeView.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #include "BsGUIResourceTreeView.h"
  2. #include "BsGUISkin.h"
  3. #include "BsProjectLibrary.h"
  4. #include "BsDragAndDropManager.h"
  5. #include "CmResources.h"
  6. #include "CmResourceManifest.h"
  7. #include "BsProjectLibrary.h"
  8. #include "CmFileSystem.h"
  9. #include "BsGUIWidget.h"
  10. #include "CmViewport.h"
  11. #include "CmRenderWindow.h"
  12. #include "CmPlatform.h"
  13. #include "CmPath.h"
  14. using namespace std::placeholders;
  15. namespace BansheeEngine
  16. {
  17. GUIResourceTreeView::InternalDraggedResources::InternalDraggedResources(UINT32 numObjects)
  18. :numObjects(numObjects)
  19. {
  20. resourcePaths = cm_newN<WString>(numObjects);
  21. }
  22. GUIResourceTreeView::InternalDraggedResources::~InternalDraggedResources()
  23. {
  24. cm_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 = Path::getFilename(mRootElement.mFullPath);
  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 (cm_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 (cm_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, RectI 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. WString oldPath = resourceTreeElement->mFullPath;
  75. WString newPath = Path::combine(Path::parentPath(oldPath), name);
  76. ProjectLibrary::instance().moveEntry(oldPath, findUniquePath(newPath));
  77. }
  78. void GUIResourceTreeView::deleteTreeElement(TreeElement* element)
  79. {
  80. ResourceTreeElement* resourceTreeElement = static_cast<ResourceTreeElement*>(element);
  81. ProjectLibrary::instance().deleteEntry(resourceTreeElement->mFullPath);
  82. }
  83. void GUIResourceTreeView::updateFromProjectLibraryEntry(ResourceTreeElement* treeElement, const ProjectLibrary::LibraryEntry* libraryEntry)
  84. {
  85. struct StackElem
  86. {
  87. StackElem(const ProjectLibrary::LibraryEntry* entry, ResourceTreeElement* treeElem)
  88. :entry(entry), treeElem(treeElem)
  89. { }
  90. const ProjectLibrary::LibraryEntry* entry;
  91. ResourceTreeElement* treeElem;
  92. };
  93. if(libraryEntry->type == ProjectLibrary::LibraryEntryType::Directory)
  94. {
  95. Stack<StackElem>::type todo;
  96. todo.push(StackElem(libraryEntry, treeElement));
  97. while(!todo.empty())
  98. {
  99. StackElem curElem = todo.top();
  100. todo.pop();
  101. const ProjectLibrary::DirectoryEntry* dirEntry = static_cast<const ProjectLibrary::DirectoryEntry*>(curElem.entry);
  102. for(auto& child : dirEntry->mChildren)
  103. {
  104. ResourceTreeElement* newChild = addTreeElement(curElem.treeElem, child->path);
  105. if(child->type == ProjectLibrary::LibraryEntryType::Directory)
  106. todo.push(StackElem(child, newChild));
  107. }
  108. sortTreeElement(curElem.treeElem);
  109. }
  110. }
  111. }
  112. GUIResourceTreeView::ResourceTreeElement* GUIResourceTreeView::addTreeElement(ResourceTreeElement* parent, const WString& fullPath)
  113. {
  114. ResourceTreeElement* newChild = cm_new<ResourceTreeElement>();
  115. newChild->mParent = parent;
  116. newChild->mName = toString(Path::getFilename(fullPath));
  117. newChild->mFullPath = fullPath;
  118. newChild->mSortedIdx = (UINT32)parent->mChildren.size();
  119. newChild->mIsVisible = parent->mIsVisible && parent->mIsExpanded;
  120. newChild->mElementName = Path::getFilename(fullPath);
  121. parent->mChildren.push_back(newChild);
  122. updateElementGUI(parent);
  123. updateElementGUI(newChild);
  124. return newChild;
  125. }
  126. void GUIResourceTreeView::deleteTreeElement(ResourceTreeElement* element)
  127. {
  128. closeTemporarilyExpandedElements(); // In case this element is one of them
  129. if(element->mIsSelected)
  130. unselectElement(element);
  131. if(element->mParent != nullptr)
  132. {
  133. auto iterFind = std::find(element->mParent->mChildren.begin(), element->mParent->mChildren.end(), element);
  134. if(iterFind != element->mParent->mChildren.end())
  135. element->mParent->mChildren.erase(iterFind);
  136. sortTreeElement(static_cast<ResourceTreeElement*>(element->mParent));
  137. updateElementGUI(element->mParent);
  138. }
  139. if(&mRootElement != element)
  140. cm_delete(element);
  141. }
  142. void GUIResourceTreeView::sortTreeElement(ResourceTreeElement* element)
  143. {
  144. auto cmp = [&] (const TreeElement* a, const TreeElement* b)
  145. {
  146. return a->mName.compare(b->mName) < 0;
  147. };
  148. std::sort(element->mChildren.begin(), element->mChildren.end(), cmp);
  149. UINT32 idx = 0;
  150. for(auto& child : element->mChildren)
  151. {
  152. child->mSortedIdx = idx;
  153. idx++;
  154. }
  155. }
  156. GUIResourceTreeView::ResourceTreeElement* GUIResourceTreeView::findTreeElement(const WString& fullPath)
  157. {
  158. Vector<WString>::type pathElems = Path::split(fullPath);
  159. Vector<WString>::type rootElems = Path::split(mRootElement.mFullPath);
  160. auto pathIter = pathElems.begin();
  161. auto rootIter = rootElems.begin();
  162. while(pathIter != pathElems.end() && rootIter != rootElems.end() && Path::comparePathElements(*pathIter, *rootIter))
  163. {
  164. ++pathIter;
  165. ++rootIter;
  166. }
  167. if(pathIter == pathElems.begin()) // Supplied path not part of the root path
  168. return nullptr;
  169. --pathIter;
  170. Stack<ResourceTreeElement*>::type todo;
  171. todo.push(&mRootElement);
  172. while(!todo.empty())
  173. {
  174. ResourceTreeElement* current = todo.top();
  175. todo.pop();
  176. if(Path::comparePathElements(*pathIter, current->mElementName))
  177. {
  178. ++pathIter;
  179. if(pathIter == pathElems.end())
  180. return current;
  181. while(!todo.empty())
  182. todo.pop();
  183. for(auto& child : current->mChildren)
  184. todo.push(static_cast<ResourceTreeElement*>(child));
  185. }
  186. }
  187. return nullptr;
  188. }
  189. void GUIResourceTreeView::entryAdded(const WString& path)
  190. {
  191. WString parentPath = Path::parentPath(path);
  192. ResourceTreeElement* parentElement = findTreeElement(parentPath);
  193. assert(parentElement != nullptr);
  194. ResourceTreeElement* newElement = addTreeElement(parentElement, path);
  195. sortTreeElement(parentElement);
  196. ProjectLibrary::LibraryEntry* libEntry = ProjectLibrary::instance().findEntry(path);
  197. assert(libEntry != nullptr);
  198. updateFromProjectLibraryEntry(newElement, libEntry);
  199. markContentAsDirty();
  200. }
  201. void GUIResourceTreeView::entryRemoved(const WString& path)
  202. {
  203. ResourceTreeElement* treeElement = findTreeElement(path);
  204. if(treeElement != nullptr)
  205. deleteTreeElement(treeElement);
  206. }
  207. void GUIResourceTreeView::setDropTarget(RenderWindow* parentWindow, INT32 x, INT32 y, UINT32 width, UINT32 height)
  208. {
  209. if(mDropTarget != nullptr)
  210. {
  211. Platform::destroyDropTarget(*mDropTarget);
  212. mDropTargetEnterConn.disconnect();
  213. mDropTargetLeaveConn.disconnect();
  214. mDropTargetMoveConn.disconnect();
  215. mDropTargetDroppedConn.disconnect();
  216. }
  217. if(parentWindow != nullptr)
  218. {
  219. mCurrentWindow = parentWindow;
  220. mDropTarget = &Platform::createDropTarget(mCurrentWindow, _getOffset().x, _getOffset().y, _getWidth(), _getHeight());
  221. mDropTargetEnterConn = mDropTarget->onEnter.connect(std::bind(&GUIResourceTreeView::dropTargetDragMove, this, _1, _2));
  222. mDropTargetMoveConn = mDropTarget->onDragOver.connect(std::bind(&GUIResourceTreeView::dropTargetDragMove, this, _1, _2));
  223. mDropTargetLeaveConn = mDropTarget->onLeave.connect(std::bind(&GUIResourceTreeView::dropTargetDragLeave, this));
  224. mDropTargetDroppedConn = mDropTarget->onDrop.connect(std::bind(&GUIResourceTreeView::dropTargetDragDropped, this, _1, _2));
  225. }
  226. else
  227. mDropTarget = nullptr;
  228. }
  229. void GUIResourceTreeView::clearDropTarget()
  230. {
  231. setDropTarget(nullptr, 0, 0, 0, 0);
  232. }
  233. void GUIResourceTreeView::dropTargetDragMove(INT32 x, INT32 y)
  234. {
  235. mDragPosition = Vector2I(x, y);
  236. mDragInProgress = true;
  237. mDropTargetDragActive = true;
  238. markContentAsDirty();
  239. if(mBottomScrollBounds.contains(mDragPosition))
  240. {
  241. if(mScrollState != ScrollState::Down)
  242. mScrollState = ScrollState::TransitioningDown;
  243. }
  244. else if(mTopScrollBounds.contains(mDragPosition))
  245. {
  246. if(mScrollState != ScrollState::Up)
  247. mScrollState = ScrollState::TransitioningUp;
  248. }
  249. else
  250. mScrollState = ScrollState::None;
  251. }
  252. void GUIResourceTreeView::dropTargetDragLeave()
  253. {
  254. mDragInProgress = false;
  255. mDropTargetDragActive = false;
  256. markContentAsDirty();
  257. }
  258. void GUIResourceTreeView::dropTargetDragDropped(INT32 x, INT32 y)
  259. {
  260. const GUITreeView::InteractableElement* element = findElementUnderCoord(Vector2I(x, y));
  261. TreeElement* treeElement = nullptr;
  262. if(element != nullptr)
  263. {
  264. if(element->isTreeElement())
  265. treeElement = element->getTreeElement();
  266. else
  267. treeElement = element->parent;
  268. }
  269. if(mDropTarget->getDropType() == OSDropType::FileList)
  270. {
  271. Vector<WString>::type fileList = mDropTarget->getFileList();
  272. mDraggedResources = cm_new<InternalDraggedResources>((UINT32)fileList.size());
  273. for(UINT32 i = 0; i < (UINT32)fileList.size(); i++)
  274. mDraggedResources->resourcePaths[i] = fileList[i];
  275. dragAndDropEnded(treeElement);
  276. cm_delete(mDraggedResources);
  277. mDraggedResources = nullptr;
  278. unselectAll();
  279. }
  280. mDragInProgress = false;
  281. mDropTargetDragActive = false;
  282. markContentAsDirty();
  283. }
  284. WString GUIResourceTreeView::findUniquePath(const WString& path)
  285. {
  286. if(FileSystem::exists(path))
  287. {
  288. WString noExtensionPath = path;
  289. WString extension = Path::getExtension(path);
  290. Path::replaceExtension(noExtensionPath, L"");
  291. WString newPath;
  292. UINT32 cnt = 1;
  293. do
  294. {
  295. newPath = Path::combine(Path::combine(noExtensionPath, L" " + toWString(cnt)), extension);
  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 = cm_new<DraggedResources>();
  311. InternalDraggedResources* internalDraggedResources = cm_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. WString destDir = resourceTreeElement->mFullPath;
  332. if(FileSystem::isFile(destDir))
  333. destDir = Path::parentPath(destDir);
  334. for(UINT32 i = 0; i < mDraggedResources->numObjects; i++)
  335. {
  336. WString filename = Path::getFilename(mDraggedResources->resourcePaths[i]);
  337. WString currentParent = Path::parentPath(mDraggedResources->resourcePaths[i]);
  338. if(!Path::equals(currentParent, destDir))
  339. {
  340. WString newPath = Path::combine(destDir, filename);
  341. ProjectLibrary::instance().moveEntry(mDraggedResources->resourcePaths[i], findUniquePath(newPath));
  342. }
  343. }
  344. }
  345. }
  346. void GUIResourceTreeView::dragAndDropFinalize()
  347. {
  348. mDragInProgress = false;
  349. markContentAsDirty();
  350. DraggedResources* draggedResources = reinterpret_cast<DraggedResources*>(DragAndDropManager::instance().getDragData());
  351. cm_delete(draggedResources);
  352. if(mDraggedResources != nullptr)
  353. {
  354. cm_delete(mDraggedResources);
  355. mDraggedResources = nullptr;
  356. }
  357. }
  358. void GUIResourceTreeView::_changeParentWidget(GUIWidget* widget)
  359. {
  360. GUITreeView::_changeParentWidget(widget);
  361. if(widget->getTarget()->getTarget()->isWindow())
  362. {
  363. RenderWindow* parentWindow = static_cast<RenderWindow*>(widget->getTarget()->getTarget().get());
  364. setDropTarget(parentWindow, _getOffset().x, _getOffset().y, _getWidth(), _getHeight());
  365. }
  366. else
  367. clearDropTarget();
  368. }
  369. bool GUIResourceTreeView::_acceptDragAndDrop(const Vector2I position, UINT32 typeId) const
  370. {
  371. return typeId == (UINT32)DragAndDropType::Resources;
  372. }
  373. const String& GUIResourceTreeView::getGUITypeName()
  374. {
  375. static String typeName = "ResourceTreeView";
  376. return typeName;
  377. }
  378. }