BsGUIResourceTreeView.cpp 15 KB

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