BsGUIResourceTreeView.cpp 15 KB

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