LibraryWindow.cs 57 KB

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