BsGUIResourceTreeView.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. using namespace CamelotFramework;
  14. using namespace BansheeEngine;
  15. namespace BansheeEditor
  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(GUIWidget& parent, GUIElementStyle* backgroundStyle, GUIElementStyle* elementBtnStyle,
  28. GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle, GUIElementStyle* editBoxStyle,
  29. BS::GUIElementStyle* dragHighlightStyle, BS::GUIElementStyle* dragSepHighlightStyle, const GUILayoutOptions& layoutOptions)
  30. :GUITreeView(parent, backgroundStyle, elementBtnStyle, foldoutBtnStyle, selectionBackgroundStyle, editBoxStyle, dragHighlightStyle,
  31. dragSepHighlightStyle, layoutOptions), mDraggedResources(nullptr), mCurrentWindow(nullptr), mDropTarget(nullptr), mDropTargetDragActive(false)
  32. {
  33. struct StackElem
  34. {
  35. StackElem(const ProjectLibrary::LibraryEntry* entry, ResourceTreeElement* treeElem)
  36. :entry(entry), treeElem(treeElem)
  37. { }
  38. const ProjectLibrary::LibraryEntry* entry;
  39. ResourceTreeElement* treeElem;
  40. };
  41. ProjectLibrary::instance().onEntryAdded.connect(boost::bind(&GUIResourceTreeView::entryAdded, this, _1));
  42. ProjectLibrary::instance().onEntryRemoved.connect(boost::bind(&GUIResourceTreeView::entryRemoved, this, _1));
  43. const ProjectLibrary::LibraryEntry* rootEntry = ProjectLibrary::instance().getRootEntry();
  44. mRootElement.mFullPath = toWString(rootEntry->path);
  45. mRootElement.mElementName = PathUtil::getFilename(mRootElement.mFullPath);
  46. expandElement(&mRootElement);
  47. Stack<StackElem>::type todo;
  48. todo.push(StackElem(rootEntry, &mRootElement));
  49. while(!todo.empty())
  50. {
  51. StackElem curElem = todo.top();
  52. todo.pop();
  53. const ProjectLibrary::DirectoryEntry* dirEntry = static_cast<const ProjectLibrary::DirectoryEntry*>(curElem.entry);
  54. for(auto& child : dirEntry->mChildren)
  55. {
  56. ResourceTreeElement* newChild = addTreeElement(curElem.treeElem, toWString(child->path));
  57. if(child->type == ProjectLibrary::LibraryEntryType::Directory)
  58. todo.push(StackElem(child, newChild));
  59. }
  60. sortTreeElement(curElem.treeElem);
  61. }
  62. if(parent.getTarget()->getTarget()->isWindow())
  63. {
  64. RenderWindow* parentWindow = static_cast<RenderWindow*>(parent.getTarget()->getTarget().get());
  65. setDropTarget(parentWindow, _getOffset().x, _getOffset().y, _getWidth(), _getHeight());
  66. }
  67. }
  68. GUIResourceTreeView::~GUIResourceTreeView()
  69. {
  70. clearDropTarget();
  71. }
  72. GUIResourceTreeView* GUIResourceTreeView::create(GUIWidget& parent, GUIElementStyle* backgroundStyle, GUIElementStyle* elementBtnStyle,
  73. GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle, GUIElementStyle* editBoxStyle, GUIElementStyle* dragHighlightStyle,
  74. GUIElementStyle* dragSepHighlightStyle)
  75. {
  76. return new (cm_alloc<GUIResourceTreeView, PoolAlloc>()) GUIResourceTreeView(parent, backgroundStyle, elementBtnStyle, foldoutBtnStyle,
  77. selectionBackgroundStyle, editBoxStyle, dragHighlightStyle, dragSepHighlightStyle, GUILayoutOptions::create(&GUISkin::DefaultStyle));
  78. }
  79. GUIResourceTreeView* GUIResourceTreeView::create(GUIWidget& parent, const GUIOptions& options, GUIElementStyle* backgroundStyle,
  80. GUIElementStyle* elementBtnStyle, GUIElementStyle* foldoutBtnStyle, GUIElementStyle* selectionBackgroundStyle,
  81. GUIElementStyle* editBoxStyle, GUIElementStyle* dragHighlightStyle, GUIElementStyle* dragSepHighlightStyle)
  82. {
  83. return new (cm_alloc<GUIResourceTreeView, PoolAlloc>()) GUIResourceTreeView(parent, backgroundStyle, elementBtnStyle,
  84. foldoutBtnStyle, selectionBackgroundStyle, editBoxStyle, dragHighlightStyle, dragSepHighlightStyle, GUILayoutOptions::create(options, &GUISkin::DefaultStyle));
  85. }
  86. void GUIResourceTreeView::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height, RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  87. {
  88. GUITreeView::_updateLayoutInternal(x, y, width, height, clipRect, widgetDepth, areaDepth);
  89. if(mDropTarget != nullptr)
  90. {
  91. mDropTarget->setArea(x, y, width, height);
  92. }
  93. }
  94. void GUIResourceTreeView::updateTreeElementHierarchy()
  95. {
  96. // Do nothing, updates are handled via callbacks
  97. }
  98. void GUIResourceTreeView::renameTreeElement(GUITreeView::TreeElement* element, const CM::WString& name)
  99. {
  100. ResourceTreeElement* resourceTreeElement = static_cast<ResourceTreeElement*>(element);
  101. WString oldPath = resourceTreeElement->mFullPath;
  102. WString newPath = PathUtil::combine(PathUtil::parentPath(oldPath), name);
  103. ProjectLibrary::instance().moveEntry(toPath(oldPath), toPath(findUniquePath(newPath)));
  104. }
  105. GUIResourceTreeView::ResourceTreeElement* GUIResourceTreeView::addTreeElement(ResourceTreeElement* parent, const CM::WString& fullPath)
  106. {
  107. ResourceTreeElement* newChild = cm_new<ResourceTreeElement>();
  108. newChild->mParent = parent;
  109. newChild->mName = toString(PathUtil::getFilename(fullPath));
  110. newChild->mFullPath = fullPath;
  111. newChild->mSortedIdx = (UINT32)parent->mChildren.size();
  112. newChild->mIsVisible = parent->mIsVisible && parent->mIsExpanded;
  113. newChild->mElementName = PathUtil::getFilename(fullPath);
  114. parent->mChildren.push_back(newChild);
  115. updateElementGUI(parent);
  116. updateElementGUI(newChild);
  117. return newChild;
  118. }
  119. void GUIResourceTreeView::deleteTreeElement(ResourceTreeElement* element)
  120. {
  121. closeTemporarilyExpandedElements(); // In case this element is one of them
  122. if(element->mIsSelected)
  123. unselectElement(element);
  124. Stack<ResourceTreeElement*>::type todo;
  125. todo.push(element);
  126. while(!todo.empty())
  127. {
  128. ResourceTreeElement* cur = todo.top();
  129. todo.pop();
  130. for(auto& child : cur->mChildren)
  131. todo.push(static_cast<ResourceTreeElement*>(child));
  132. }
  133. if(element->mParent != nullptr)
  134. {
  135. auto iterFind = std::find(element->mParent->mChildren.begin(), element->mParent->mChildren.end(), element);
  136. if(iterFind != element->mParent->mChildren.end())
  137. element->mParent->mChildren.erase(iterFind);
  138. sortTreeElement(static_cast<ResourceTreeElement*>(element->mParent));
  139. updateElementGUI(element->mParent);
  140. }
  141. cm_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 CM::WString& fullPath)
  158. {
  159. Vector<WString>::type pathElems = PathUtil::split(fullPath);
  160. Vector<WString>::type rootElems = PathUtil::split(mRootElement.mFullPath);
  161. auto pathIter = pathElems.begin();
  162. auto rootIter = rootElems.begin();
  163. while(pathIter != pathElems.end() && rootIter != rootElems.end() && PathUtil::comparePathElements(*pathIter, *rootIter))
  164. {
  165. ++pathIter;
  166. ++rootIter;
  167. }
  168. if(pathIter == pathElems.begin()) // Supplied path not part of the root path
  169. return nullptr;
  170. --pathIter;
  171. Stack<ResourceTreeElement*>::type todo;
  172. todo.push(&mRootElement);
  173. while(!todo.empty())
  174. {
  175. ResourceTreeElement* current = todo.top();
  176. todo.pop();
  177. if(PathUtil::comparePathElements(*pathIter, current->mElementName))
  178. {
  179. ++pathIter;
  180. if(pathIter == pathElems.end())
  181. return current;
  182. while(!todo.empty())
  183. todo.pop();
  184. for(auto& child : current->mChildren)
  185. todo.push(static_cast<ResourceTreeElement*>(child));
  186. }
  187. }
  188. return nullptr;
  189. }
  190. void GUIResourceTreeView::entryAdded(const WPath& path)
  191. {
  192. WPath parentPath = PathUtil::parentPath(path);
  193. ResourceTreeElement* parentElement = findTreeElement(toWString(parentPath));
  194. assert(parentElement != nullptr);
  195. addTreeElement(parentElement, toWString(path));
  196. sortTreeElement(parentElement);
  197. markContentAsDirty();
  198. }
  199. void GUIResourceTreeView::entryRemoved(const WPath& path)
  200. {
  201. ResourceTreeElement* treeElement = findTreeElement(toWString(path));
  202. if(treeElement != nullptr)
  203. deleteTreeElement(treeElement);
  204. }
  205. void GUIResourceTreeView::setDropTarget(CM::RenderWindow* parentWindow, CM::INT32 x, CM::INT32 y, CM::UINT32 width, CM::UINT32 height)
  206. {
  207. if(mDropTarget != nullptr)
  208. {
  209. Platform::destroyDropTarget(*mDropTarget);
  210. mDropTargetEnterConn.disconnect();
  211. mDropTargetLeaveConn.disconnect();
  212. mDropTargetMoveConn.disconnect();
  213. mDropTargetDroppedConn.disconnect();
  214. }
  215. if(parentWindow != nullptr)
  216. {
  217. mCurrentWindow = parentWindow;
  218. mDropTarget = &Platform::createDropTarget(mCurrentWindow, _getOffset().x, _getOffset().y, _getWidth(), _getHeight());
  219. mDropTargetEnterConn = mDropTarget->onEnter.connect(boost::bind(&GUIResourceTreeView::dropTargetDragMove, this, _1, _2));
  220. mDropTargetMoveConn = mDropTarget->onDragOver.connect(boost::bind(&GUIResourceTreeView::dropTargetDragMove, this, _1, _2));
  221. mDropTargetLeaveConn = mDropTarget->onLeave.connect(boost::bind(&GUIResourceTreeView::dropTargetDragLeave, this));
  222. mDropTargetDroppedConn = mDropTarget->onDrop.connect(boost::bind(&GUIResourceTreeView::dropTargetDragDropped, this, _1, _2));
  223. }
  224. else
  225. mDropTarget = nullptr;
  226. }
  227. void GUIResourceTreeView::clearDropTarget()
  228. {
  229. setDropTarget(nullptr, 0, 0, 0, 0);
  230. }
  231. void GUIResourceTreeView::dropTargetDragMove(CM::INT32 x, CM::INT32 y)
  232. {
  233. mDragPosition = Vector2I(x, y);
  234. mDragInProgress = true;
  235. mDropTargetDragActive = true;
  236. markContentAsDirty();
  237. if(mBottomScrollBounds.contains(mDragPosition))
  238. {
  239. if(mScrollState != ScrollState::Down)
  240. mScrollState = ScrollState::TransitioningDown;
  241. }
  242. else if(mTopScrollBounds.contains(mDragPosition))
  243. {
  244. if(mScrollState != ScrollState::Up)
  245. mScrollState = ScrollState::TransitioningUp;
  246. }
  247. else
  248. mScrollState = ScrollState::None;
  249. }
  250. void GUIResourceTreeView::dropTargetDragLeave()
  251. {
  252. mDragInProgress = false;
  253. mDropTargetDragActive = false;
  254. markContentAsDirty();
  255. }
  256. void GUIResourceTreeView::dropTargetDragDropped(CM::INT32 x, CM::INT32 y)
  257. {
  258. const GUITreeView::InteractableElement* element = findElementUnderCoord(Vector2I(x, y));
  259. TreeElement* treeElement = nullptr;
  260. if(element != nullptr)
  261. {
  262. if(element->isTreeElement())
  263. treeElement = element->getTreeElement();
  264. else
  265. treeElement = element->parent;
  266. }
  267. if(mDropTarget->getDropType() == OSDropType::FileList)
  268. {
  269. Vector<WString>::type fileList = mDropTarget->getFileList();
  270. mDraggedResources = cm_new<InternalDraggedResources>((UINT32)fileList.size());
  271. for(UINT32 i = 0; i < (UINT32)fileList.size(); i++)
  272. mDraggedResources->resourcePaths[i] = fileList[i];
  273. dragAndDropEnded(treeElement);
  274. cm_delete(mDraggedResources);
  275. mDraggedResources = nullptr;
  276. unselectAll();
  277. }
  278. mDragInProgress = false;
  279. mDropTargetDragActive = false;
  280. markContentAsDirty();
  281. }
  282. CM::WString GUIResourceTreeView::findUniquePath(const CM::WString& path)
  283. {
  284. if(FileSystem::exists(path))
  285. {
  286. WString noExtensionPath = path;
  287. WString extension = PathUtil::getExtension(path);
  288. PathUtil::replaceExtension(noExtensionPath, L"");
  289. WString newPath;
  290. UINT32 cnt = 1;
  291. do
  292. {
  293. newPath = PathUtil::combine(PathUtil::combine(noExtensionPath, L" " + toWString(cnt)), extension);
  294. cnt++;
  295. } while (FileSystem::exists(newPath));
  296. return newPath;
  297. }
  298. else
  299. return path;
  300. }
  301. bool GUIResourceTreeView::acceptDragAndDrop() const
  302. {
  303. return mDropTargetDragActive || DragAndDropManager::instance().isDragInProgress() && DragAndDropManager::instance().getDragTypeId() == (UINT32)DragAndDropType::Resources;
  304. }
  305. void GUIResourceTreeView::dragAndDropStart()
  306. {
  307. assert(mDraggedResources == nullptr);
  308. DraggedResources* draggedResources = cm_new<DraggedResources>();
  309. InternalDraggedResources* internalDraggedResources = cm_new<InternalDraggedResources>((UINT32)mSelectedElements.size());
  310. ResourceManifestPtr resourceManifest = gResources().getResourceManifest();
  311. UINT32 cnt = 0;
  312. for(auto& selectedElement : mSelectedElements)
  313. {
  314. ResourceTreeElement* resourceTreeElement = static_cast<ResourceTreeElement*>(selectedElement.element);
  315. internalDraggedResources->resourcePaths[cnt] = resourceTreeElement->mFullPath;
  316. cnt++;
  317. if(resourceManifest->filePathExists(internalDraggedResources->resourcePaths[cnt]))
  318. draggedResources->resourceUUIDs.push_back(resourceManifest->filePathToUUID(internalDraggedResources->resourcePaths[cnt]));
  319. }
  320. mDraggedResources = internalDraggedResources;
  321. DragAndDropManager::instance().startDrag(HTexture(), (UINT32)DragAndDropType::Resources, (void*)draggedResources,
  322. boost::bind(&GUIResourceTreeView::dragAndDropFinalize, this));
  323. }
  324. void GUIResourceTreeView::dragAndDropEnded(TreeElement* overTreeElement)
  325. {
  326. if(overTreeElement != nullptr && mDraggedResources != nullptr)
  327. {
  328. ResourceTreeElement* resourceTreeElement = static_cast<ResourceTreeElement*>(overTreeElement);
  329. WString destDir = resourceTreeElement->mFullPath;
  330. if(FileSystem::isFile(destDir))
  331. destDir = PathUtil::parentPath(destDir);
  332. for(UINT32 i = 0; i < mDraggedResources->numObjects; i++)
  333. {
  334. WString filename = PathUtil::getFilename(mDraggedResources->resourcePaths[i]);
  335. WString newPath = PathUtil::combine(destDir, filename);
  336. ProjectLibrary::instance().moveEntry(toPath(mDraggedResources->resourcePaths[i]), toPath(findUniquePath(newPath)));
  337. }
  338. }
  339. }
  340. void GUIResourceTreeView::dragAndDropFinalize()
  341. {
  342. mDragInProgress = false;
  343. markContentAsDirty();
  344. DraggedResources* draggedResources = reinterpret_cast<DraggedResources*>(DragAndDropManager::instance().getDragData());
  345. cm_delete(draggedResources);
  346. if(mDraggedResources != nullptr)
  347. {
  348. cm_delete(mDraggedResources);
  349. mDraggedResources = nullptr;
  350. }
  351. }
  352. void GUIResourceTreeView::_changeParentWidget(GUIWidget* widget)
  353. {
  354. GUITreeView::_changeParentWidget(widget);
  355. if(widget->getTarget()->getTarget()->isWindow())
  356. {
  357. RenderWindow* parentWindow = static_cast<RenderWindow*>(widget->getTarget()->getTarget().get());
  358. setDropTarget(parentWindow, _getOffset().x, _getOffset().y, _getWidth(), _getHeight());
  359. }
  360. else
  361. clearDropTarget();
  362. }
  363. const String& GUIResourceTreeView::getGUITypeName()
  364. {
  365. static String typeName = "ResourceTreeView";
  366. return typeName;
  367. }
  368. }