2
0

LibraryWindow.cs 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// Types of resource tile display in the library window.
  9. /// </summary>
  10. internal enum ProjectViewType
  11. {
  12. Grid64, Grid48, Grid32, List16
  13. }
  14. /// <summary>
  15. /// Editor window that displays all resources in the project. Resources can be displayed as a grid or list of icons,
  16. /// with the ability to move, cut, copy, paste resources and folders, as well as supporting drag and drop and search
  17. /// operations.
  18. /// </summary>
  19. internal sealed class LibraryWindow : EditorWindow
  20. {
  21. /// <summary>
  22. /// Directions the selection cursor in library window can be moved in.
  23. /// </summary>
  24. internal enum MoveDirection
  25. {
  26. Up, Down, Left, Right
  27. }
  28. private const int DRAG_SCROLL_HEIGHT = 20;
  29. private const int DRAG_SCROLL_AMOUNT_PER_SECOND = 100;
  30. private const int FOLDER_BUTTON_WIDTH = 30;
  31. private const int FOLDER_SEPARATOR_WIDTH = 10;
  32. private bool hasContentFocus = false;
  33. private bool HasContentFocus { get { return HasFocus && hasContentFocus; } }
  34. private string searchQuery;
  35. private bool IsSearchActive { get { return !string.IsNullOrEmpty(searchQuery); } }
  36. private ProjectViewType viewType = ProjectViewType.Grid32;
  37. private bool requiresRefresh;
  38. private string currentDirectory = "";
  39. private List<string> selectionPaths = new List<string>();
  40. private int selectionAnchorStart = -1;
  41. private int selectionAnchorEnd = -1;
  42. private string pingPath = "";
  43. private string hoverHighlightPath = "";
  44. private LibraryGUIContent content;
  45. private GUIScrollArea contentScrollArea;
  46. private GUILayoutX searchBarLayout;
  47. private GUIButton optionsButton;
  48. private GUILayout folderBarLayout;
  49. private GUILayout folderListLayout;
  50. private GUITextField searchField;
  51. private GUITexture dragSelection;
  52. private ContextMenu entryContextMenu;
  53. private LibraryDropTarget dropTarget;
  54. private int autoScrollAmount;
  55. private bool isDraggingSelection;
  56. private Vector2I dragSelectionStart;
  57. private Vector2I dragSelectionEnd;
  58. private LibraryGUIEntry inProgressRenameElement;
  59. // Cut/Copy/Paste
  60. private List<string> copyPaths = new List<string>();
  61. private List<string> cutPaths = new List<string>();
  62. /// <summary>
  63. /// Determines how to display resource tiles in the library window.
  64. /// </summary>
  65. internal ProjectViewType ViewType
  66. {
  67. get { return viewType; }
  68. set { viewType = value; Refresh(); }
  69. }
  70. /// <summary>
  71. /// Returns a file or folder currently selected in the library window. If nothing is selected, returns the active
  72. /// folder. Returned path is relative to project library resources folder.
  73. /// </summary>
  74. public string SelectedEntry
  75. {
  76. get
  77. {
  78. if (selectionPaths.Count == 1)
  79. {
  80. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  81. if (entry != null)
  82. return entry.Path;
  83. }
  84. return currentDirectory;
  85. }
  86. }
  87. /// <summary>
  88. /// Returns a folder currently selected in the library window. If no folder is selected, returns the active
  89. /// folder. Returned path is relative to project library resources folder.
  90. /// </summary>
  91. public string SelectedFolder
  92. {
  93. get
  94. {
  95. DirectoryEntry selectedDirectory = null;
  96. if (selectionPaths.Count == 1)
  97. {
  98. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  99. if (entry != null && entry.Type == LibraryEntryType.Directory)
  100. selectedDirectory = (DirectoryEntry) entry;
  101. }
  102. if (selectedDirectory != null)
  103. return selectedDirectory.Path;
  104. return currentDirectory;
  105. }
  106. }
  107. /// <summary>
  108. /// Returns the path to the folder currently displayed in the library window. Returned path is relative to project
  109. /// library resources folder.
  110. /// </summary>
  111. public string CurrentFolder
  112. {
  113. get { return currentDirectory; }
  114. }
  115. /// <summary>
  116. /// Context menu that should open when user right clicks on the content area.
  117. /// </summary>
  118. internal ContextMenu ContextMenu
  119. {
  120. get { return entryContextMenu; }
  121. }
  122. /// <summary>
  123. /// Opens the library window if not already open.
  124. /// </summary>
  125. [MenuItem("Windows/Library", ButtonModifier.CtrlAlt, ButtonCode.L, 6000)]
  126. private static void OpenLibraryWindow()
  127. {
  128. OpenWindow<LibraryWindow>();
  129. }
  130. private void OnInitialize()
  131. {
  132. ProjectLibrary.OnEntryAdded += OnEntryChanged;
  133. ProjectLibrary.OnEntryRemoved += OnEntryChanged;
  134. GUILayoutY contentLayout = GUI.AddLayoutY();
  135. searchBarLayout = contentLayout.AddLayoutX();
  136. searchField = new GUITextField();
  137. searchField.OnChanged += OnSearchChanged;
  138. GUIContent clearIcon = new GUIContent(EditorBuiltin.GetLibraryWindowIcon(LibraryWindowIcon.Clear));
  139. GUIButton clearSearchBtn = new GUIButton(clearIcon);
  140. clearSearchBtn.OnClick += ClearSearch;
  141. clearSearchBtn.SetWidth(40);
  142. GUIContent optionsIcon = new GUIContent(EditorBuiltin.GetLibraryWindowIcon(LibraryWindowIcon.Options));
  143. optionsButton = new GUIButton(optionsIcon);
  144. optionsButton.OnClick += OpenOptionsWindow;
  145. optionsButton.SetWidth(40);
  146. searchBarLayout.AddElement(searchField);
  147. searchBarLayout.AddElement(clearSearchBtn);
  148. searchBarLayout.AddElement(optionsButton);
  149. folderBarLayout = contentLayout.AddLayoutX();
  150. GUIContent homeIcon = new GUIContent(EditorBuiltin.GetLibraryWindowIcon(LibraryWindowIcon.Home));
  151. GUIButton homeButton = new GUIButton(homeIcon, GUIOption.FixedWidth(FOLDER_BUTTON_WIDTH));
  152. homeButton.OnClick += OnHomeClicked;
  153. GUIContent upIcon = new GUIContent(EditorBuiltin.GetLibraryWindowIcon(LibraryWindowIcon.Up));
  154. GUIButton upButton = new GUIButton(upIcon, GUIOption.FixedWidth(FOLDER_BUTTON_WIDTH));
  155. upButton.OnClick += OnUpClicked;
  156. folderBarLayout.AddElement(homeButton);
  157. folderBarLayout.AddElement(upButton);
  158. folderBarLayout.AddSpace(10);
  159. contentScrollArea = new GUIScrollArea(GUIOption.FlexibleWidth(), GUIOption.FlexibleHeight());
  160. contentLayout.AddElement(contentScrollArea);
  161. contentLayout.AddFlexibleSpace();
  162. entryContextMenu = LibraryMenu.CreateContextMenu(this);
  163. content = new LibraryGUIContent(this, contentScrollArea);
  164. Reset();
  165. dropTarget = new LibraryDropTarget(this);
  166. dropTarget.Bounds = GetScrollAreaBounds();
  167. dropTarget.OnStart += OnDragStart;
  168. dropTarget.OnDrag += OnDragMove;
  169. dropTarget.OnLeave += OnDragLeave;
  170. dropTarget.OnDropResource += OnResourceDragDropped;
  171. dropTarget.OnDropSceneObject += OnSceneObjectDragDropped;
  172. dropTarget.OnEnd += OnDragEnd;
  173. Selection.OnSelectionChanged += OnSelectionChanged;
  174. Selection.OnResourcePing += OnPing;
  175. }
  176. private void OnDestroy()
  177. {
  178. Selection.OnSelectionChanged -= OnSelectionChanged;
  179. Selection.OnResourcePing -= OnPing;
  180. }
  181. private void OnEditorUpdate()
  182. {
  183. bool isRenameInProgress = inProgressRenameElement != null;
  184. if (HasContentFocus)
  185. {
  186. if (!isRenameInProgress)
  187. {
  188. if (Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl))
  189. {
  190. if (Input.IsButtonUp(ButtonCode.C))
  191. {
  192. CopySelection();
  193. }
  194. else if (Input.IsButtonUp(ButtonCode.X))
  195. {
  196. CutSelection();
  197. }
  198. else if (Input.IsButtonUp(ButtonCode.D))
  199. {
  200. DuplicateSelection();
  201. }
  202. else if (Input.IsButtonUp(ButtonCode.V))
  203. {
  204. PasteToSelection();
  205. }
  206. }
  207. if (Input.IsButtonDown(ButtonCode.Return))
  208. {
  209. if (selectionPaths.Count == 1)
  210. {
  211. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  212. if (entry != null && entry.Type == LibraryEntryType.Directory)
  213. {
  214. EnterDirectory(entry.Path);
  215. }
  216. }
  217. }
  218. else if (Input.IsButtonDown(ButtonCode.Back))
  219. {
  220. LibraryEntry entry = ProjectLibrary.GetEntry(currentDirectory);
  221. if (entry != null && entry.Parent != null)
  222. {
  223. EnterDirectory(entry.Parent.Path);
  224. }
  225. }
  226. else if (Input.IsButtonDown(ButtonCode.Up))
  227. {
  228. MoveSelection(MoveDirection.Up);
  229. }
  230. else if (Input.IsButtonDown(ButtonCode.Down))
  231. {
  232. MoveSelection(MoveDirection.Down);
  233. }
  234. else if (Input.IsButtonDown(ButtonCode.Left))
  235. {
  236. MoveSelection(MoveDirection.Left);
  237. }
  238. else if (Input.IsButtonDown(ButtonCode.Right))
  239. {
  240. MoveSelection(MoveDirection.Right);
  241. }
  242. else if (Input.IsButtonDown(ButtonCode.F2))
  243. {
  244. RenameSelection();
  245. }
  246. else if (Input.IsButtonDown(ButtonCode.Delete))
  247. {
  248. DeleteSelection();
  249. }
  250. }
  251. else
  252. {
  253. if (Input.IsButtonDown(ButtonCode.Return))
  254. {
  255. string newName = inProgressRenameElement.GetRenamedName();
  256. string originalPath = inProgressRenameElement.path;
  257. originalPath = originalPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  258. string newPath = Path.GetDirectoryName(originalPath);
  259. newPath = Path.Combine(newPath, newName + Path.GetExtension(originalPath));
  260. bool renameOK = true;
  261. if (!PathEx.IsValidFileName(newName))
  262. {
  263. DialogBox.Open(new LocEdString("Error"), new LocEdString("The name you specified is not a valid file name. Try another."), DialogBox.Type.OK);
  264. renameOK = false;
  265. }
  266. if (renameOK)
  267. {
  268. // Windows sees paths with dot at the end as if they didn't have it
  269. // so remove the dot to ensure the project library does the same
  270. string trimmedNewPath = newPath.TrimEnd('.');
  271. if (originalPath != trimmedNewPath && ProjectLibrary.Exists(trimmedNewPath))
  272. {
  273. DialogBox.Open(new LocEdString("Error"), new LocEdString("File/folder with that name already exists in this folder."), DialogBox.Type.OK);
  274. renameOK = false;
  275. }
  276. }
  277. if (renameOK)
  278. {
  279. ProjectLibrary.Rename(originalPath, newPath);
  280. StopRename();
  281. }
  282. }
  283. else if (Input.IsButtonDown(ButtonCode.Escape))
  284. {
  285. StopRename();
  286. }
  287. }
  288. }
  289. else
  290. {
  291. if (isRenameInProgress)
  292. StopRename();
  293. }
  294. if (autoScrollAmount != 0)
  295. {
  296. Rect2I contentBounds = contentScrollArea.ContentBounds;
  297. float scrollPct = autoScrollAmount / (float)contentBounds.height;
  298. contentScrollArea.VerticalScroll += scrollPct * Time.FrameDelta;
  299. }
  300. if (requiresRefresh)
  301. Refresh();
  302. dropTarget.Update();
  303. }
  304. /// <inheritdoc/>
  305. protected override LocString GetDisplayName()
  306. {
  307. return new LocEdString("Library");
  308. }
  309. /// <inheritdoc/>
  310. protected override void WindowResized(int width, int height)
  311. {
  312. base.WindowResized(width, height);
  313. Refresh();
  314. dropTarget.Bounds = GetScrollAreaBounds();
  315. }
  316. /// <summary>
  317. /// Attempts to find a resource tile element at the specified coordinates.
  318. /// </summary>
  319. /// <param name="windowPos">Coordinates relative to the window.</param>
  320. /// <returns>True if found an entry, false otherwise.</returns>
  321. private LibraryGUIEntry FindElementAt(Vector2I windowPos)
  322. {
  323. Vector2I scrollPos = WindowToScrollAreaCoords(windowPos);
  324. return content.FindElementAt(scrollPos);
  325. }
  326. /// <summary>
  327. /// Clears hover highlight from the currently hovered over element.
  328. /// </summary>
  329. private void ClearHoverHighlight()
  330. {
  331. content.MarkAsHovered(hoverHighlightPath, false);
  332. hoverHighlightPath = "";
  333. }
  334. /// <summary>
  335. /// Pings an element at the specified path, displaying and highlighting it in the window.
  336. /// </summary>
  337. /// <param name="path">Project library path to the element.</param>
  338. public void Ping(string path)
  339. {
  340. content.MarkAsPinged(pingPath, false);
  341. pingPath = path;
  342. content.MarkAsPinged(pingPath, true);
  343. }
  344. /// <summary>
  345. /// Resets the library window to initial state.
  346. /// </summary>
  347. public void Reset()
  348. {
  349. currentDirectory = ProjectLibrary.Root.Path;
  350. selectionAnchorStart = -1;
  351. selectionAnchorEnd = -1;
  352. selectionPaths.Clear();
  353. pingPath = "";
  354. hoverHighlightPath = "";
  355. Refresh();
  356. }
  357. /// <summary>
  358. /// Deselects all selected elements.
  359. /// </summary>
  360. /// <param name="onlyInternal">If true, do not update the global <see cref="Selection"/>, instead the operation
  361. /// will be contained to the library window internally.</param>
  362. internal void DeselectAll(bool onlyInternal = false)
  363. {
  364. SetSelection(new List<string>(), onlyInternal);
  365. selectionAnchorStart = -1;
  366. selectionAnchorEnd = -1;
  367. }
  368. /// <summary>
  369. /// Select an element at the specified path. If control or shift keys are pressed during this operations multiple
  370. /// elements can be selected.
  371. /// </summary>
  372. /// <param name="path">Project library path to the element.</param>
  373. internal void Select(string path)
  374. {
  375. LibraryGUIEntry entry;
  376. if (!content.TryGetEntry(path, out entry))
  377. return;
  378. bool ctrlDown = Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl);
  379. bool shiftDown = Input.IsButtonHeld(ButtonCode.LeftShift) || Input.IsButtonHeld(ButtonCode.RightShift);
  380. if (shiftDown)
  381. {
  382. if (selectionAnchorStart != -1 && selectionAnchorStart < content.Entries.Length)
  383. {
  384. int start = Math.Min(entry.index, selectionAnchorStart);
  385. int end = Math.Max(entry.index, selectionAnchorStart);
  386. List<string> newSelection = new List<string>();
  387. for(int i = start; i <= end; i++)
  388. newSelection.Add(content.Entries[i].path);
  389. SetSelection(newSelection);
  390. selectionAnchorEnd = entry.index;
  391. }
  392. else
  393. {
  394. SetSelection(new List<string>() {path});
  395. selectionAnchorStart = entry.index;
  396. selectionAnchorEnd = entry.index;
  397. }
  398. }
  399. else if (ctrlDown)
  400. {
  401. List<string> newSelection = new List<string>(selectionPaths);
  402. if (selectionPaths.Contains(path))
  403. {
  404. newSelection.Remove(path);
  405. if (newSelection.Count == 0)
  406. DeselectAll();
  407. else
  408. {
  409. if (selectionAnchorStart == entry.index)
  410. {
  411. LibraryGUIEntry newAnchorEntry;
  412. if (!content.TryGetEntry(newSelection[0], out newAnchorEntry))
  413. selectionAnchorStart = -1;
  414. else
  415. selectionAnchorStart = newAnchorEntry.index;
  416. }
  417. if (selectionAnchorEnd == entry.index)
  418. {
  419. LibraryGUIEntry newAnchorEntry;
  420. if (!content.TryGetEntry(newSelection[newSelection.Count - 1], out newAnchorEntry))
  421. selectionAnchorEnd = -1;
  422. else
  423. selectionAnchorEnd = newAnchorEntry.index;
  424. }
  425. SetSelection(newSelection);
  426. }
  427. }
  428. else
  429. {
  430. newSelection.Add(path);
  431. SetSelection(newSelection);
  432. selectionAnchorEnd = entry.index;
  433. }
  434. }
  435. else
  436. {
  437. SetSelection(new List<string>() {path});
  438. selectionAnchorStart = entry.index;
  439. selectionAnchorEnd = entry.index;
  440. }
  441. }
  442. /// <summary>
  443. /// Selects a new element in the specified direction from the currently selected element. If shift or control are
  444. /// held during this operation, the selected object will be added to existing selection. If no element is selected
  445. /// the first or last element will be selected depending on direction.
  446. /// </summary>
  447. /// <param name="dir">Direction to move from the currently selected element.</param>
  448. internal void MoveSelection(MoveDirection dir)
  449. {
  450. string newPath = "";
  451. if (selectionPaths.Count == 0 || selectionAnchorEnd == -1)
  452. {
  453. // Nothing is selected so we arbitrarily select first or last element
  454. if (content.Entries.Length > 0)
  455. {
  456. switch (dir)
  457. {
  458. case MoveDirection.Left:
  459. case MoveDirection.Up:
  460. newPath = content.Entries[content.Entries.Length - 1].path;
  461. break;
  462. case MoveDirection.Right:
  463. case MoveDirection.Down:
  464. newPath = content.Entries[0].path;
  465. break;
  466. }
  467. }
  468. }
  469. else
  470. {
  471. switch (dir)
  472. {
  473. case MoveDirection.Left:
  474. if (selectionAnchorEnd - 1 >= 0)
  475. newPath = content.Entries[selectionAnchorEnd - 1].path;
  476. break;
  477. case MoveDirection.Up:
  478. if (selectionAnchorEnd - content.ElementsPerRow >= 0)
  479. newPath = content.Entries[selectionAnchorEnd - content.ElementsPerRow].path;
  480. break;
  481. case MoveDirection.Right:
  482. if (selectionAnchorEnd + 1 < content.Entries.Length)
  483. newPath = content.Entries[selectionAnchorEnd + 1].path;
  484. break;
  485. case MoveDirection.Down:
  486. if (selectionAnchorEnd + content.ElementsPerRow < content.Entries.Length)
  487. newPath = content.Entries[selectionAnchorEnd + content.ElementsPerRow].path;
  488. break;
  489. }
  490. }
  491. if (!string.IsNullOrEmpty(newPath))
  492. {
  493. Select(newPath);
  494. ScrollToEntry(newPath);
  495. }
  496. }
  497. /// <summary>
  498. /// Selects a set of elements based on the provided paths.
  499. /// </summary>
  500. /// <param name="paths">Project library paths of the elements to select.</param>
  501. /// <param name="onlyInternal">If true, do not update the global <see cref="Selection"/>, instead the operation
  502. /// will be contained to the library window internally.</param>
  503. internal void SetSelection(List<string> paths, bool onlyInternal = false)
  504. {
  505. if (selectionPaths != null)
  506. {
  507. foreach (var path in selectionPaths)
  508. content.MarkAsSelected(path, false);
  509. }
  510. selectionPaths = paths;
  511. if (selectionPaths != null)
  512. {
  513. foreach (var path in selectionPaths)
  514. content.MarkAsSelected(path, true);
  515. }
  516. Ping("");
  517. StopRename();
  518. if (!onlyInternal)
  519. {
  520. if (selectionPaths != null)
  521. Selection.ResourcePaths = selectionPaths.ToArray();
  522. else
  523. Selection.ResourcePaths = new string[0];
  524. }
  525. }
  526. /// <summary>
  527. /// Changes the active directory to the provided directory. Current contents of the window will be cleared and
  528. /// instead contents of the new directory will be displayed.
  529. /// </summary>
  530. /// <param name="directory">Project library path to the directory.</param>
  531. internal void EnterDirectory(string directory)
  532. {
  533. currentDirectory = directory;
  534. DeselectAll();
  535. Refresh();
  536. }
  537. /// <summary>
  538. /// Marks the provided set of elements for a cut operation. Cut elements can be moved to a new location by calling
  539. /// <see cref="Paste"/>.
  540. /// </summary>
  541. /// <param name="sourcePaths">Project library paths of the elements to cut.</param>
  542. internal void Cut(IEnumerable<string> sourcePaths)
  543. {
  544. foreach (var path in cutPaths)
  545. content.MarkAsCut(path, false);
  546. cutPaths.Clear();
  547. cutPaths.AddRange(sourcePaths);
  548. foreach (var path in cutPaths)
  549. content.MarkAsCut(path, true);
  550. copyPaths.Clear();
  551. }
  552. /// <summary>
  553. /// Marks the provided set of elements for a copy operation. You can copy the elements by calling <see cref="Paste"/>.
  554. /// </summary>
  555. /// <param name="sourcePaths">Project library paths of the elements to copy.</param>
  556. internal void Copy(IEnumerable<string> sourcePaths)
  557. {
  558. copyPaths.Clear();
  559. copyPaths.AddRange(sourcePaths);
  560. foreach (var path in cutPaths)
  561. content.MarkAsCut(path, false);
  562. cutPaths.Clear();
  563. }
  564. /// <summary>
  565. /// Duplicates the provided set of elements.
  566. /// </summary>
  567. /// <param name="sourcePaths">Project library paths of the elements to duplicate.</param>
  568. internal void Duplicate(IEnumerable<string> sourcePaths)
  569. {
  570. foreach (var source in sourcePaths)
  571. {
  572. if (Directory.Exists(source))
  573. DirectoryEx.Copy(source, LibraryUtility.GetUniquePath(source));
  574. else if (File.Exists(source))
  575. FileEx.Copy(source, LibraryUtility.GetUniquePath(source));
  576. ProjectLibrary.Refresh();
  577. }
  578. }
  579. /// <summary>
  580. /// Performs a cut or copy operations on the elements previously marked by calling <see cref="Cut"/> or
  581. /// <see cref="Copy"/>.
  582. /// </summary>
  583. /// <param name="destinationFolder">Project library folder into which to move/copy the elements.</param>
  584. internal void Paste(string destinationFolder)
  585. {
  586. if (copyPaths.Count > 0)
  587. {
  588. for (int i = 0; i < copyPaths.Count; i++)
  589. {
  590. string destination = Path.Combine(destinationFolder, PathEx.GetTail(copyPaths[i]));
  591. if (Directory.Exists(copyPaths[i]))
  592. DirectoryEx.Copy(copyPaths[i], LibraryUtility.GetUniquePath(destination));
  593. else if (File.Exists(copyPaths[i]))
  594. FileEx.Copy(copyPaths[i], LibraryUtility.GetUniquePath(destination));
  595. }
  596. ProjectLibrary.Refresh();
  597. }
  598. else if (cutPaths.Count > 0)
  599. {
  600. for (int i = 0; i < cutPaths.Count; i++)
  601. {
  602. string destination = Path.Combine(destinationFolder, PathEx.GetTail(cutPaths[i]));
  603. if (Directory.Exists(cutPaths[i]))
  604. DirectoryEx.Move(cutPaths[i], LibraryUtility.GetUniquePath(destination));
  605. else if (File.Exists(cutPaths[i]))
  606. FileEx.Move(cutPaths[i], LibraryUtility.GetUniquePath(destination));
  607. }
  608. cutPaths.Clear();
  609. ProjectLibrary.Refresh();
  610. }
  611. }
  612. /// <summary>
  613. /// Scrolls the contents GUI area so that the element at the specified path becomes visible.
  614. /// </summary>
  615. /// <param name="path">Project library path to the element.</param>
  616. private void ScrollToEntry(string path)
  617. {
  618. LibraryGUIEntry entryGUI;
  619. if (!content.TryGetEntry(path, out entryGUI))
  620. return;
  621. Rect2I entryBounds = entryGUI.Bounds;
  622. Rect2I contentBounds = contentScrollArea.Layout.Bounds;
  623. Rect2I windowEntryBounds = entryBounds;
  624. windowEntryBounds.x += contentBounds.x;
  625. windowEntryBounds.y += contentBounds.y;
  626. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  627. bool requiresScroll = windowEntryBounds.y < scrollAreaBounds.y ||
  628. (windowEntryBounds.y + windowEntryBounds.height) > (scrollAreaBounds.y + scrollAreaBounds.height);
  629. if (!requiresScroll)
  630. return;
  631. int scrollableSize = contentBounds.height - scrollAreaBounds.height;
  632. float percent = (((entryBounds.y + entryBounds.height * 0.5f) - scrollAreaBounds.height * 0.5f) / (float)scrollableSize);
  633. percent = MathEx.Clamp01(percent);
  634. contentScrollArea.VerticalScroll = percent;
  635. }
  636. /// <summary>
  637. /// Rebuilds the library window GUI. Should be called any time the active folder or contents change.
  638. /// </summary>
  639. private void Refresh()
  640. {
  641. requiresRefresh = false;
  642. LibraryEntry[] entriesToDisplay = new LibraryEntry[0];
  643. if (IsSearchActive)
  644. {
  645. entriesToDisplay = ProjectLibrary.Search("*" + searchQuery + "*");
  646. }
  647. else
  648. {
  649. DirectoryEntry entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  650. if (entry == null)
  651. {
  652. currentDirectory = ProjectLibrary.Root.Path;
  653. entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  654. }
  655. if(entry != null)
  656. entriesToDisplay = entry.Children;
  657. }
  658. inProgressRenameElement = null;
  659. RefreshDirectoryBar();
  660. SortEntries(entriesToDisplay);
  661. content.Refresh(viewType, entriesToDisplay);
  662. if (entriesToDisplay.Length == 0)
  663. return;
  664. foreach (var path in cutPaths)
  665. content.MarkAsCut(path, true);
  666. foreach (var path in selectionPaths)
  667. content.MarkAsSelected(path, true);
  668. content.MarkAsPinged(pingPath, true);
  669. Rect2I contentBounds = content.Bounds;
  670. Rect2I minimalBounds = GetScrollAreaBounds();
  671. contentBounds.height = Math.Max(contentBounds.height, minimalBounds.height);
  672. GUIButton catchAll = new GUIButton("", EditorStyles.Blank);
  673. catchAll.Bounds = contentBounds;
  674. catchAll.OnClick += OnCatchAllClicked;
  675. catchAll.SetContextMenu(entryContextMenu);
  676. content.Underlay.AddElement(catchAll);
  677. Rect2I focusBounds = contentBounds; // Contents + Folder bar
  678. Rect2I scrollBounds = contentScrollArea.Bounds;
  679. focusBounds.x += scrollBounds.x;
  680. focusBounds.y += scrollBounds.y;
  681. Rect2I folderBarBounds = folderListLayout.Bounds;
  682. focusBounds.y -= folderBarBounds.height;
  683. focusBounds.height += folderBarBounds.height;
  684. GUIButton focusCatcher = new GUIButton("", EditorStyles.Blank);
  685. focusCatcher.OnFocusGained += () => hasContentFocus = true;
  686. focusCatcher.OnFocusLost += () => hasContentFocus = false;
  687. focusCatcher.Bounds = focusBounds;
  688. GUIPanel focusPanel = GUI.AddPanel(3);
  689. focusPanel.AddElement(focusCatcher);
  690. UpdateDragSelection(dragSelectionEnd);
  691. }
  692. /// <summary>
  693. /// Converts coordinates relative to the window into coordinates relative to the contents scroll area.
  694. /// </summary>
  695. /// <param name="windowPos">Coordinates relative to the window.</param>
  696. /// <returns>Coordinates relative to the contents scroll area.</returns>
  697. private Vector2I WindowToScrollAreaCoords(Vector2I windowPos)
  698. {
  699. Rect2I scrollBounds = contentScrollArea.Layout.Bounds;
  700. Vector2I scrollPos = windowPos;
  701. scrollPos.x -= scrollBounds.x;
  702. scrollPos.y -= scrollBounds.y;
  703. return scrollPos;
  704. }
  705. /// <summary>
  706. /// Starts a drag operation that displays a selection outline allowing the user to select multiple entries at once.
  707. /// </summary>
  708. /// <param name="windowPos">Coordinates relative to the window where the drag originated.</param>
  709. private void StartDragSelection(Vector2I windowPos)
  710. {
  711. isDraggingSelection = true;
  712. dragSelectionStart = WindowToScrollAreaCoords(windowPos);
  713. dragSelectionEnd = dragSelectionStart;
  714. }
  715. /// <summary>
  716. /// Updates a selection outline drag operation by expanding the outline to the new location. Elements in the outline
  717. /// are selected.
  718. /// </summary>
  719. /// <param name="windowPos">Coordinates of the pointer relative to the window.</param>
  720. /// <returns>True if the selection outline drag is valid and was updated, false otherwise.</returns>
  721. private bool UpdateDragSelection(Vector2I windowPos)
  722. {
  723. if (!isDraggingSelection)
  724. return false;
  725. if (dragSelection == null)
  726. {
  727. dragSelection = new GUITexture(null, true, EditorStyles.SelectionArea);
  728. content.Overlay.AddElement(dragSelection);
  729. }
  730. dragSelectionEnd = WindowToScrollAreaCoords(windowPos);
  731. Rect2I selectionArea = CalculateSelectionArea();
  732. SelectInArea(selectionArea);
  733. dragSelection.Bounds = selectionArea;
  734. return true;
  735. }
  736. /// <summary>
  737. /// Ends the selection outline drag operation. Elements in the outline are selected.
  738. /// </summary>
  739. /// <returns>True if the selection outline drag is valid and was ended, false otherwise.</returns>
  740. private bool EndDragSelection()
  741. {
  742. if (!isDraggingSelection)
  743. return false;
  744. if (dragSelection != null)
  745. {
  746. dragSelection.Destroy();
  747. dragSelection = null;
  748. }
  749. Rect2I selectionArea = CalculateSelectionArea();
  750. SelectInArea(selectionArea);
  751. isDraggingSelection = false;
  752. return false;
  753. }
  754. /// <summary>
  755. /// Calculates bounds of the selection area used for selection overlay drag operation, depending on drag starting
  756. /// point coordinates and current drag coordinates.
  757. /// </summary>
  758. /// <returns>Bounds of the selection area, relative to the content scroll area.</returns>
  759. private Rect2I CalculateSelectionArea()
  760. {
  761. Rect2I selectionArea = new Rect2I();
  762. if (dragSelectionStart.x < dragSelectionEnd.x)
  763. {
  764. selectionArea.x = dragSelectionStart.x;
  765. selectionArea.width = dragSelectionEnd.x - dragSelectionStart.x;
  766. }
  767. else
  768. {
  769. selectionArea.x = dragSelectionEnd.x;
  770. selectionArea.width = dragSelectionStart.x - dragSelectionEnd.x;
  771. }
  772. if (dragSelectionStart.y < dragSelectionEnd.y)
  773. {
  774. selectionArea.y = dragSelectionStart.y;
  775. selectionArea.height = dragSelectionEnd.y - dragSelectionStart.y;
  776. }
  777. else
  778. {
  779. selectionArea.y = dragSelectionEnd.y;
  780. selectionArea.height = dragSelectionStart.y - dragSelectionEnd.y;
  781. }
  782. Rect2I maxBounds = contentScrollArea.Layout.Bounds;
  783. maxBounds.x = 0;
  784. maxBounds.y = 0;
  785. selectionArea.Clip(maxBounds);
  786. return selectionArea;
  787. }
  788. /// <summary>
  789. /// Selects all elements overlapping the specified bounds.
  790. /// </summary>
  791. /// <param name="scrollBounds">Bounds relative to the content scroll area.</param>
  792. private void SelectInArea(Rect2I scrollBounds)
  793. {
  794. LibraryGUIEntry[] foundElements = content.FindElementsOverlapping(scrollBounds);
  795. if (foundElements.Length > 0)
  796. {
  797. selectionAnchorStart = foundElements[0].index;
  798. selectionAnchorEnd = foundElements[foundElements.Length - 1].index;
  799. }
  800. else
  801. {
  802. selectionAnchorStart = -1;
  803. selectionAnchorEnd = -1;
  804. }
  805. List<string> elementPaths = new List<string>();
  806. foreach (var elem in foundElements)
  807. elementPaths.Add(elem.path);
  808. SetSelection(elementPaths);
  809. }
  810. /// <summary>
  811. /// Updates GUI for the directory bar. Should be called whenever the active folder changes.
  812. /// </summary>
  813. private void RefreshDirectoryBar()
  814. {
  815. if (folderListLayout != null)
  816. {
  817. folderListLayout.Destroy();
  818. folderListLayout = null;
  819. }
  820. folderListLayout = folderBarLayout.AddLayoutX();
  821. string[] folders = null;
  822. string[] fullPaths = null;
  823. if (IsSearchActive)
  824. {
  825. folders = new[] {searchQuery};
  826. fullPaths = new[] { searchQuery };
  827. }
  828. else
  829. {
  830. string currentDir = Path.Combine("Resources", currentDirectory);
  831. folders = currentDir.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar },
  832. StringSplitOptions.RemoveEmptyEntries);
  833. fullPaths = new string[folders.Length];
  834. for (int i = 0; i < folders.Length; i++)
  835. {
  836. if (i == 0)
  837. fullPaths[i] = "";
  838. else
  839. fullPaths[i] = Path.Combine(fullPaths[i - 1], folders[i]);
  840. }
  841. }
  842. int availableWidth = folderBarLayout.Bounds.width - FOLDER_BUTTON_WIDTH * 2;
  843. int numFolders = 0;
  844. for (int i = folders.Length - 1; i >= 0; i--)
  845. {
  846. GUIButton folderButton = new GUIButton(folders[i]);
  847. if (!IsSearchActive)
  848. {
  849. string fullPath = fullPaths[i];
  850. folderButton.OnClick += () => OnFolderButtonClicked(fullPath);
  851. }
  852. GUIButton separator = new GUIButton("/", GUIOption.FixedWidth(FOLDER_SEPARATOR_WIDTH));
  853. folderListLayout.InsertElement(0, separator);
  854. folderListLayout.InsertElement(0, folderButton);
  855. numFolders++;
  856. Rect2I folderListBounds = folderListLayout.Bounds;
  857. if (folderListBounds.width > availableWidth)
  858. {
  859. if (numFolders > 2)
  860. {
  861. separator.Destroy();
  862. folderButton.Destroy();
  863. break;
  864. }
  865. }
  866. }
  867. }
  868. /// <summary>
  869. /// Performs <see cref="Cut"/> operation on the currently selected elements.
  870. /// </summary>
  871. internal void CutSelection()
  872. {
  873. if (selectionPaths.Count > 0)
  874. Cut(selectionPaths);
  875. }
  876. /// <summary>
  877. /// Performs <see cref="Copy"/> operation on the currently selected elements.
  878. /// </summary>
  879. internal void CopySelection()
  880. {
  881. if (selectionPaths.Count > 0)
  882. Copy(selectionPaths);
  883. }
  884. /// <summary>
  885. /// Performs <see cref="Duplicate"/> operation on the currently selected elements.
  886. /// </summary>
  887. internal void DuplicateSelection()
  888. {
  889. if (selectionPaths.Count > 0)
  890. Duplicate(selectionPaths);
  891. }
  892. /// <summary>
  893. /// Performs <see cref="Paste"/> operation. Elements will be pasted in the currently selected directory (if any), or
  894. /// the active directory otherwise.
  895. /// </summary>
  896. internal void PasteToSelection()
  897. {
  898. Paste(SelectedFolder);
  899. }
  900. /// <summary>
  901. /// Starts a rename operation on the currently selected elements. If more than one elements are selected only the
  902. /// first one will be affected.
  903. /// </summary>
  904. internal void RenameSelection()
  905. {
  906. if (selectionPaths.Count == 0)
  907. return;
  908. if (selectionPaths.Count > 1)
  909. {
  910. DeselectAll();
  911. Select(selectionPaths[0]);
  912. }
  913. LibraryGUIEntry entry;
  914. if (content.TryGetEntry(selectionPaths[0], out entry))
  915. {
  916. entry.StartRename();
  917. inProgressRenameElement = entry;
  918. }
  919. }
  920. /// <summary>
  921. /// Deletes currently selected elements. User will be asked to confirm deletion via a dialog box.
  922. /// </summary>
  923. internal void DeleteSelection()
  924. {
  925. if (selectionPaths.Count == 0)
  926. return;
  927. DialogBox.Open(new LocEdString("Confirm deletion"), new LocEdString("Are you sure you want to delete the selected object(s)?"),
  928. DialogBox.Type.YesNo,
  929. type =>
  930. {
  931. if (type == DialogBox.ResultType.Yes)
  932. {
  933. foreach (var path in selectionPaths)
  934. {
  935. ProjectLibrary.Delete(path);
  936. }
  937. DeselectAll();
  938. Refresh();
  939. }
  940. });
  941. }
  942. /// <summary>
  943. /// Stops the rename operation, if one is in progress on any element.
  944. /// </summary>
  945. internal void StopRename()
  946. {
  947. if (inProgressRenameElement != null)
  948. {
  949. inProgressRenameElement.StopRename();
  950. inProgressRenameElement = null;
  951. }
  952. }
  953. /// <summary>
  954. /// Clears the search bar and refreshes the content area to display contents of the current directory.
  955. /// </summary>
  956. private void ClearSearch()
  957. {
  958. searchField.Value = "";
  959. searchQuery = "";
  960. Refresh();
  961. }
  962. /// <summary>
  963. /// Opens the drop down options window that allows you to customize library window look and feel.
  964. /// </summary>
  965. private void OpenOptionsWindow()
  966. {
  967. Vector2I openPosition;
  968. Rect2I buttonBounds = GUILayoutUtility.CalculateBounds(optionsButton, GUI);
  969. openPosition.x = buttonBounds.x + buttonBounds.width / 2;
  970. openPosition.y = buttonBounds.y + buttonBounds.height / 2;
  971. LibraryDropDown dropDown = DropDownWindow.Open<LibraryDropDown>(this, openPosition);
  972. dropDown.Initialize(this);
  973. }
  974. /// <summary>
  975. /// Returns the content scroll area bounds.
  976. /// </summary>
  977. /// <returns>Bounds of the content scroll area, relative to the window.</returns>
  978. private Rect2I GetScrollAreaBounds()
  979. {
  980. Rect2I bounds = GUI.Bounds;
  981. Rect2I folderListBounds = folderListLayout.Bounds;
  982. Rect2I searchBarBounds = searchBarLayout.Bounds;
  983. bounds.y = folderListBounds.height + searchBarBounds.height;
  984. bounds.height -= folderListBounds.height + searchBarBounds.height;
  985. return bounds;
  986. }
  987. /// <summary>
  988. /// Triggered when a project library entry was changed (added, modified, deleted).
  989. /// </summary>
  990. /// <param name="entry">Project library path of the changed entry.</param>
  991. private void OnEntryChanged(string entry)
  992. {
  993. requiresRefresh = true;
  994. }
  995. /// <summary>
  996. /// Triggered when the drag and drop operation is starting while over the content area. If drag operation is over
  997. /// an element, element will be dragged.
  998. /// </summary>
  999. /// <param name="windowPos">Coordinates where the drag operation started, relative to the window.</param>
  1000. private void OnDragStart(Vector2I windowPos)
  1001. {
  1002. bool isRenameInProgress = inProgressRenameElement != null;
  1003. if (isRenameInProgress)
  1004. return;
  1005. LibraryGUIEntry underCursorElem = FindElementAt(windowPos);
  1006. if (underCursorElem == null)
  1007. {
  1008. StartDragSelection(windowPos);
  1009. return;
  1010. }
  1011. string resourceDir = ProjectLibrary.ResourceFolder;
  1012. string[] dragPaths = null;
  1013. if (selectionPaths.Count > 0)
  1014. {
  1015. foreach (var path in selectionPaths)
  1016. {
  1017. if (path == underCursorElem.path)
  1018. {
  1019. dragPaths = new string[selectionPaths.Count];
  1020. for (int i = 0; i < selectionPaths.Count; i++)
  1021. {
  1022. dragPaths[i] = Path.Combine(resourceDir, selectionPaths[i]);
  1023. }
  1024. break;
  1025. }
  1026. }
  1027. }
  1028. if (dragPaths == null)
  1029. dragPaths = new[] { Path.Combine(resourceDir, underCursorElem.path) };
  1030. ResourceDragDropData dragDropData = new ResourceDragDropData(dragPaths);
  1031. DragDrop.StartDrag(dragDropData);
  1032. }
  1033. /// <summary>
  1034. /// Triggered when a pointer is moved while a drag operation is in progress.
  1035. /// </summary>
  1036. /// <param name="windowPos">Coordinates of the pointer relative to the window.</param>
  1037. private void OnDragMove(Vector2I windowPos)
  1038. {
  1039. // Auto-scroll
  1040. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  1041. int scrollAreaTop = scrollAreaBounds.y;
  1042. int scrollAreaBottom = scrollAreaBounds.y + scrollAreaBounds.height;
  1043. if (windowPos.y > scrollAreaTop && windowPos.y <= (scrollAreaTop + DRAG_SCROLL_HEIGHT))
  1044. autoScrollAmount = -DRAG_SCROLL_AMOUNT_PER_SECOND;
  1045. else if (windowPos.y >= (scrollAreaBottom - DRAG_SCROLL_HEIGHT) && windowPos.y < scrollAreaBottom)
  1046. autoScrollAmount = DRAG_SCROLL_AMOUNT_PER_SECOND;
  1047. else
  1048. autoScrollAmount = 0;
  1049. // Selection box
  1050. if (UpdateDragSelection(windowPos))
  1051. return;
  1052. // Drag and drop (hover element under cursor)
  1053. LibraryGUIEntry underCursorElem = FindElementAt(windowPos);
  1054. if (underCursorElem == null)
  1055. {
  1056. ClearHoverHighlight();
  1057. }
  1058. else
  1059. {
  1060. if (underCursorElem.path != hoverHighlightPath)
  1061. {
  1062. ClearHoverHighlight();
  1063. hoverHighlightPath = underCursorElem.path;
  1064. underCursorElem.MarkAsHovered(true);
  1065. }
  1066. }
  1067. }
  1068. /// <summary>
  1069. /// Triggered when a pointer leaves the drop targer while a drag operation is in progress.
  1070. /// </summary>
  1071. private void OnDragLeave()
  1072. {
  1073. ClearHoverHighlight();
  1074. autoScrollAmount = 0;
  1075. }
  1076. /// <summary>
  1077. /// Triggered when a resource drop operation finishes over the content area.
  1078. /// </summary>
  1079. /// <param name="windowPos">Coordinates of the pointer relative to the window where the drop operation finished
  1080. /// .</param>
  1081. /// <param name="paths">Paths of the dropped resources.</param>
  1082. private void OnResourceDragDropped(Vector2I windowPos, string[] paths)
  1083. {
  1084. ClearHoverHighlight();
  1085. autoScrollAmount = 0;
  1086. if (EndDragSelection())
  1087. return;
  1088. string resourceDir = ProjectLibrary.ResourceFolder;
  1089. string destinationFolder = Path.Combine(resourceDir, currentDirectory);
  1090. LibraryGUIEntry underCursorElement = FindElementAt(windowPos);
  1091. if (underCursorElement != null)
  1092. {
  1093. LibraryEntry entry = ProjectLibrary.GetEntry(underCursorElement.path);
  1094. if (entry != null && entry.Type == LibraryEntryType.Directory)
  1095. destinationFolder = Path.Combine(resourceDir, entry.Path);
  1096. }
  1097. if (paths != null)
  1098. {
  1099. foreach (var path in paths)
  1100. {
  1101. if (path == null)
  1102. continue;
  1103. string absolutePath = path;
  1104. if (!Path.IsPathRooted(absolutePath))
  1105. absolutePath = Path.Combine(resourceDir, path);
  1106. if (string.IsNullOrEmpty(absolutePath))
  1107. continue;
  1108. if (PathEx.IsPartOf(destinationFolder, absolutePath) || PathEx.Compare(absolutePath, destinationFolder))
  1109. continue;
  1110. string pathTail = PathEx.GetTail(absolutePath);
  1111. string destination = Path.Combine(destinationFolder, pathTail);
  1112. if (PathEx.Compare(absolutePath, destination))
  1113. continue;
  1114. bool doCopy = !ProjectLibrary.Exists(absolutePath);
  1115. if (Directory.Exists(path))
  1116. {
  1117. if (doCopy)
  1118. DirectoryEx.Copy(absolutePath, LibraryUtility.GetUniquePath(destination));
  1119. else
  1120. DirectoryEx.Move(absolutePath, LibraryUtility.GetUniquePath(destination));
  1121. }
  1122. else if (File.Exists(path))
  1123. {
  1124. if (doCopy)
  1125. FileEx.Copy(absolutePath, LibraryUtility.GetUniquePath(destination));
  1126. else
  1127. ProjectLibrary.Move(absolutePath, LibraryUtility.GetUniquePath(destination));
  1128. }
  1129. ProjectLibrary.Refresh();
  1130. }
  1131. }
  1132. }
  1133. /// <summary>
  1134. /// Triggered when a scene object drop operation finishes over the content area.
  1135. /// </summary>
  1136. /// <param name="windowPos">Coordinates of the pointer relative to the window where the drop operation finished
  1137. /// .</param>
  1138. /// <param name="objects">Dropped scene objects.</param>
  1139. private void OnSceneObjectDragDropped(Vector2I windowPos, SceneObject[] objects)
  1140. {
  1141. ClearHoverHighlight();
  1142. autoScrollAmount = 0;
  1143. if (EndDragSelection())
  1144. return;
  1145. string destinationFolder = currentDirectory;
  1146. LibraryGUIEntry underCursorElement = FindElementAt(windowPos);
  1147. if (underCursorElement != null)
  1148. {
  1149. LibraryEntry entry = ProjectLibrary.GetEntry(underCursorElement.path);
  1150. if (entry != null && entry.Type == LibraryEntryType.Directory)
  1151. destinationFolder = entry.Path;
  1152. }
  1153. if (objects != null)
  1154. {
  1155. foreach (var so in objects)
  1156. {
  1157. if (so == null)
  1158. continue;
  1159. Prefab newPrefab = new Prefab(so);
  1160. string destination = LibraryUtility.GetUniquePath(Path.Combine(destinationFolder, so.Name + ".prefab"));
  1161. ProjectLibrary.Create(newPrefab, destination);
  1162. ProjectLibrary.Refresh();
  1163. }
  1164. }
  1165. }
  1166. /// <summary>
  1167. /// Triggered when a drag operation that originated from this window ends.
  1168. /// </summary>
  1169. /// <param name="windowPos">Coordinates of the pointer where the drag ended relative to the window </param>
  1170. private void OnDragEnd(Vector2I windowPos)
  1171. {
  1172. EndDragSelection();
  1173. autoScrollAmount = 0;
  1174. }
  1175. /// <summary>
  1176. /// Triggered when the global selection changes.
  1177. /// </summary>
  1178. /// <param name="sceneObjects">A set of newly selected scene objects.</param>
  1179. /// <param name="resourcePaths">A set of paths for newly selected resources.</param>
  1180. private void OnSelectionChanged(SceneObject[] sceneObjects, string[] resourcePaths)
  1181. {
  1182. if(sceneObjects.Length > 0)
  1183. DeselectAll(true);
  1184. }
  1185. /// <summary>
  1186. /// Triggered when a ping operation was triggered externally.
  1187. /// </summary>
  1188. /// <param name="path">Path to the resource to highlight.</param>
  1189. private void OnPing(string path)
  1190. {
  1191. Ping(path);
  1192. }
  1193. /// <summary>
  1194. /// Triggered when a folder on the directory bar was selected.
  1195. /// </summary>
  1196. /// <param name="path">Project library path to the folder to enter.</param>
  1197. private void OnFolderButtonClicked(string path)
  1198. {
  1199. EnterDirectory(path);
  1200. }
  1201. /// <summary>
  1202. /// Triggered when the user clicks on empty space between elements.
  1203. /// </summary>
  1204. private void OnCatchAllClicked()
  1205. {
  1206. DeselectAll();
  1207. }
  1208. /// <summary>
  1209. /// Triggered when the user clicks on the home button on the directory bar, changing the active directory to
  1210. /// project library root.
  1211. /// </summary>
  1212. private void OnHomeClicked()
  1213. {
  1214. currentDirectory = ProjectLibrary.Root.Path;
  1215. Refresh();
  1216. }
  1217. /// <summary>
  1218. /// Triggered when the user clicks on the up button on the directory bar, changing the active directory to the
  1219. /// parent directory, unless already at project library root.
  1220. /// </summary>
  1221. private void OnUpClicked()
  1222. {
  1223. currentDirectory = currentDirectory.Trim(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  1224. if (!string.IsNullOrEmpty(currentDirectory))
  1225. {
  1226. string parent = Path.GetDirectoryName(currentDirectory);
  1227. currentDirectory = parent;
  1228. Refresh();
  1229. }
  1230. }
  1231. /// <summary>
  1232. /// Triggered when the user inputs new values into the search input box. Refreshes the contents so they display
  1233. /// elements matching the search text.
  1234. /// </summary>
  1235. /// <param name="newValue">Search box text.</param>
  1236. private void OnSearchChanged(string newValue)
  1237. {
  1238. searchQuery = newValue;
  1239. Refresh();
  1240. }
  1241. /// <summary>
  1242. /// Sorts the specified set of project library entries by type (folder or resource), followed by name.
  1243. /// </summary>
  1244. /// <param name="input">Set of project library entries to sort.</param>
  1245. private static void SortEntries(LibraryEntry[] input)
  1246. {
  1247. Array.Sort(input, (x, y) =>
  1248. {
  1249. if (x.Type == y.Type)
  1250. return x.Name.CompareTo(y.Name);
  1251. else
  1252. return x.Type == LibraryEntryType.File ? 1 : -1;
  1253. });
  1254. }
  1255. }
  1256. }