2
0

LibraryWindow.cs 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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.Count)
  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.Count > 0)
  465. {
  466. switch (dir)
  467. {
  468. case MoveDirection.Left:
  469. case MoveDirection.Up:
  470. newPath = content.Entries[content.Entries.Count - 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.Count)
  493. newPath = content.Entries[selectionAnchorEnd + 1].path;
  494. break;
  495. case MoveDirection.Down:
  496. if (selectionAnchorEnd + content.ElementsPerRow < content.Entries.Count)
  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. string[] filePaths = GetFiles(sourcePaths);
  557. cutPaths.Clear();
  558. cutPaths.AddRange(filePaths);
  559. foreach (var path in cutPaths)
  560. content.MarkAsCut(path, true);
  561. copyPaths.Clear();
  562. }
  563. /// <summary>
  564. /// Marks the provided set of elements for a copy operation. You can copy the elements by calling <see cref="Paste"/>.
  565. /// </summary>
  566. /// <param name="sourcePaths">Project library paths of the elements to copy.</param>
  567. internal void Copy(IEnumerable<string> sourcePaths)
  568. {
  569. copyPaths.Clear();
  570. string[] filePaths = GetFiles(sourcePaths);
  571. copyPaths.AddRange(filePaths);
  572. foreach (var path in cutPaths)
  573. content.MarkAsCut(path, false);
  574. cutPaths.Clear();
  575. }
  576. /// <summary>
  577. /// Duplicates the provided set of elements.
  578. /// </summary>
  579. /// <param name="sourcePaths">Project library paths of the elements to duplicate.</param>
  580. internal void Duplicate(IEnumerable<string> sourcePaths)
  581. {
  582. string[] filePaths = GetFiles(sourcePaths);
  583. foreach (var source in filePaths)
  584. {
  585. string path = source;
  586. if (!Path.IsPathRooted(path))
  587. path = Path.Combine(ProjectLibrary.ResourceFolder, path);
  588. if (Directory.Exists(path))
  589. DirectoryEx.Copy(path, LibraryUtility.GetUniquePath(path));
  590. else if (File.Exists(path))
  591. FileEx.Copy(path, LibraryUtility.GetUniquePath(path));
  592. ProjectLibrary.Refresh();
  593. }
  594. }
  595. /// <summary>
  596. /// Performs a cut or copy operations on the elements previously marked by calling <see cref="Cut"/> or
  597. /// <see cref="Copy"/>.
  598. /// </summary>
  599. /// <param name="destinationFolder">Project library folder into which to move/copy the elements.</param>
  600. internal void Paste(string destinationFolder)
  601. {
  602. string rootedDestinationFolder = destinationFolder;
  603. if (!Path.IsPathRooted(rootedDestinationFolder))
  604. rootedDestinationFolder = Path.Combine(ProjectLibrary.ResourceFolder, rootedDestinationFolder);
  605. if (copyPaths.Count > 0)
  606. {
  607. for (int i = 0; i < copyPaths.Count; i++)
  608. {
  609. string path = copyPaths[i];
  610. if (!Path.IsPathRooted(path))
  611. path = Path.Combine(ProjectLibrary.ResourceFolder, path);
  612. string destination = Path.Combine(rootedDestinationFolder, PathEx.GetTail(copyPaths[i]));
  613. if (Directory.Exists(path))
  614. DirectoryEx.Copy(path, LibraryUtility.GetUniquePath(destination));
  615. else if (File.Exists(path))
  616. FileEx.Copy(path, LibraryUtility.GetUniquePath(destination));
  617. }
  618. ProjectLibrary.Refresh();
  619. }
  620. else if (cutPaths.Count > 0)
  621. {
  622. for (int i = 0; i < cutPaths.Count; i++)
  623. {
  624. string path = cutPaths[i];
  625. if (!Path.IsPathRooted(path))
  626. path = Path.Combine(ProjectLibrary.ResourceFolder, path);
  627. string destination = Path.Combine(rootedDestinationFolder, PathEx.GetTail(cutPaths[i]));
  628. if (Directory.Exists(path))
  629. DirectoryEx.Move(path, LibraryUtility.GetUniquePath(destination));
  630. else if (File.Exists(path))
  631. FileEx.Move(path, LibraryUtility.GetUniquePath(destination));
  632. }
  633. cutPaths.Clear();
  634. ProjectLibrary.Refresh();
  635. }
  636. }
  637. /// <summary>
  638. /// Scrolls the contents GUI area so that the element at the specified path becomes visible.
  639. /// </summary>
  640. /// <param name="path">Project library path to the element.</param>
  641. private void ScrollToEntry(string path)
  642. {
  643. LibraryGUIEntry entryGUI;
  644. if (!content.TryGetEntry(path, out entryGUI))
  645. return;
  646. Rect2I entryBounds = entryGUI.Bounds;
  647. Rect2I contentBounds = contentScrollArea.Layout.Bounds;
  648. Rect2I windowEntryBounds = entryBounds;
  649. windowEntryBounds.x += contentBounds.x;
  650. windowEntryBounds.y += contentBounds.y;
  651. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  652. bool requiresScroll = windowEntryBounds.y < scrollAreaBounds.y ||
  653. (windowEntryBounds.y + windowEntryBounds.height) > (scrollAreaBounds.y + scrollAreaBounds.height);
  654. if (!requiresScroll)
  655. return;
  656. int scrollableSize = contentBounds.height - scrollAreaBounds.height;
  657. float percent = (((entryBounds.y + entryBounds.height * 0.5f) - scrollAreaBounds.height * 0.5f) / (float)scrollableSize);
  658. percent = MathEx.Clamp01(percent);
  659. contentScrollArea.VerticalScroll = percent;
  660. }
  661. /// <summary>
  662. /// Rebuilds the library window GUI. Should be called any time the active folder or contents change.
  663. /// </summary>
  664. private void Refresh()
  665. {
  666. requiresRefresh = false;
  667. LibraryEntry[] entriesToDisplay = new LibraryEntry[0];
  668. if (IsSearchActive)
  669. {
  670. entriesToDisplay = ProjectLibrary.Search("*" + searchQuery + "*");
  671. }
  672. else
  673. {
  674. DirectoryEntry entry = ProjectLibrary.GetEntry(CurrentFolder) as DirectoryEntry;
  675. if (entry == null)
  676. {
  677. CurrentFolder = ProjectLibrary.Root.Path;
  678. entry = ProjectLibrary.GetEntry(CurrentFolder) as DirectoryEntry;
  679. }
  680. if(entry != null)
  681. entriesToDisplay = entry.Children;
  682. }
  683. inProgressRenameElement = null;
  684. RefreshDirectoryBar();
  685. SortEntries(entriesToDisplay);
  686. content.Refresh(viewType, entriesToDisplay);
  687. foreach (var path in cutPaths)
  688. content.MarkAsCut(path, true);
  689. foreach (var path in selectionPaths)
  690. content.MarkAsSelected(path, true);
  691. content.MarkAsPinged(pingPath, true);
  692. Rect2I contentBounds = content.Bounds;
  693. Rect2I minimalBounds = GetScrollAreaBounds();
  694. contentBounds.height = Math.Max(contentBounds.height, minimalBounds.height);
  695. GUIButton catchAll = new GUIButton("", EditorStyles.Blank);
  696. catchAll.Bounds = contentBounds;
  697. catchAll.OnClick += OnCatchAllClicked;
  698. catchAll.SetContextMenu(entryContextMenu);
  699. content.Underlay.AddElement(catchAll);
  700. Rect2I focusBounds = contentBounds; // Contents + Folder bar
  701. Rect2I scrollBounds = contentScrollArea.Bounds;
  702. focusBounds.x += scrollBounds.x;
  703. focusBounds.y += scrollBounds.y;
  704. Rect2I folderBarBounds = folderListLayout.Bounds;
  705. focusBounds.y -= folderBarBounds.height;
  706. focusBounds.height += folderBarBounds.height;
  707. GUIButton focusCatcher = new GUIButton("", EditorStyles.Blank);
  708. focusCatcher.OnFocusGained += () => hasContentFocus = true;
  709. focusCatcher.OnFocusLost += () => hasContentFocus = false;
  710. focusCatcher.Bounds = focusBounds;
  711. GUIPanel focusPanel = GUI.AddPanel(3);
  712. focusPanel.AddElement(focusCatcher);
  713. UpdateDragSelection(dragSelectionEnd);
  714. }
  715. /// <summary>
  716. /// Converts coordinates relative to the window into coordinates relative to the contents scroll area.
  717. /// </summary>
  718. /// <param name="windowPos">Coordinates relative to the window.</param>
  719. /// <returns>Coordinates relative to the contents scroll area.</returns>
  720. private Vector2I WindowToScrollAreaCoords(Vector2I windowPos)
  721. {
  722. Rect2I scrollBounds = contentScrollArea.Layout.Bounds;
  723. Vector2I scrollPos = windowPos;
  724. scrollPos.x -= scrollBounds.x;
  725. scrollPos.y -= scrollBounds.y;
  726. return scrollPos;
  727. }
  728. /// <summary>
  729. /// Starts a drag operation that displays a selection outline allowing the user to select multiple entries at once.
  730. /// </summary>
  731. /// <param name="windowPos">Coordinates relative to the window where the drag originated.</param>
  732. private void StartDragSelection(Vector2I windowPos)
  733. {
  734. isDraggingSelection = true;
  735. dragSelectionStart = WindowToScrollAreaCoords(windowPos);
  736. dragSelectionEnd = dragSelectionStart;
  737. }
  738. /// <summary>
  739. /// Updates a selection outline drag operation by expanding the outline to the new location. Elements in the outline
  740. /// are selected.
  741. /// </summary>
  742. /// <param name="windowPos">Coordinates of the pointer relative to the window.</param>
  743. /// <returns>True if the selection outline drag is valid and was updated, false otherwise.</returns>
  744. private bool UpdateDragSelection(Vector2I windowPos)
  745. {
  746. if (!isDraggingSelection)
  747. return false;
  748. if (dragSelection == null)
  749. {
  750. dragSelection = new GUITexture(null, true, EditorStyles.SelectionArea);
  751. content.Overlay.AddElement(dragSelection);
  752. }
  753. dragSelectionEnd = WindowToScrollAreaCoords(windowPos);
  754. Rect2I selectionArea = CalculateSelectionArea();
  755. SelectInArea(selectionArea);
  756. dragSelection.Bounds = selectionArea;
  757. return true;
  758. }
  759. /// <summary>
  760. /// Ends the selection outline drag operation. Elements in the outline are selected.
  761. /// </summary>
  762. /// <returns>True if the selection outline drag is valid and was ended, false otherwise.</returns>
  763. private bool EndDragSelection()
  764. {
  765. if (!isDraggingSelection)
  766. return false;
  767. if (dragSelection != null)
  768. {
  769. dragSelection.Destroy();
  770. dragSelection = null;
  771. }
  772. Rect2I selectionArea = CalculateSelectionArea();
  773. SelectInArea(selectionArea);
  774. isDraggingSelection = false;
  775. return false;
  776. }
  777. /// <summary>
  778. /// Calculates bounds of the selection area used for selection overlay drag operation, depending on drag starting
  779. /// point coordinates and current drag coordinates.
  780. /// </summary>
  781. /// <returns>Bounds of the selection area, relative to the content scroll area.</returns>
  782. private Rect2I CalculateSelectionArea()
  783. {
  784. Rect2I selectionArea = new Rect2I();
  785. if (dragSelectionStart.x < dragSelectionEnd.x)
  786. {
  787. selectionArea.x = dragSelectionStart.x;
  788. selectionArea.width = dragSelectionEnd.x - dragSelectionStart.x;
  789. }
  790. else
  791. {
  792. selectionArea.x = dragSelectionEnd.x;
  793. selectionArea.width = dragSelectionStart.x - dragSelectionEnd.x;
  794. }
  795. if (dragSelectionStart.y < dragSelectionEnd.y)
  796. {
  797. selectionArea.y = dragSelectionStart.y;
  798. selectionArea.height = dragSelectionEnd.y - dragSelectionStart.y;
  799. }
  800. else
  801. {
  802. selectionArea.y = dragSelectionEnd.y;
  803. selectionArea.height = dragSelectionStart.y - dragSelectionEnd.y;
  804. }
  805. Rect2I maxBounds = contentScrollArea.Layout.Bounds;
  806. maxBounds.x = 0;
  807. maxBounds.y = 0;
  808. selectionArea.Clip(maxBounds);
  809. return selectionArea;
  810. }
  811. /// <summary>
  812. /// Selects all elements overlapping the specified bounds.
  813. /// </summary>
  814. /// <param name="scrollBounds">Bounds relative to the content scroll area.</param>
  815. private void SelectInArea(Rect2I scrollBounds)
  816. {
  817. LibraryGUIEntry[] foundElements = content.FindElementsOverlapping(scrollBounds);
  818. if (foundElements.Length > 0)
  819. {
  820. selectionAnchorStart = foundElements[0].index;
  821. selectionAnchorEnd = foundElements[foundElements.Length - 1].index;
  822. }
  823. else
  824. {
  825. selectionAnchorStart = -1;
  826. selectionAnchorEnd = -1;
  827. }
  828. List<string> elementPaths = new List<string>();
  829. foreach (var elem in foundElements)
  830. elementPaths.Add(elem.path);
  831. SetSelection(elementPaths);
  832. }
  833. /// <summary>
  834. /// Updates GUI for the directory bar. Should be called whenever the active folder changes.
  835. /// </summary>
  836. private void RefreshDirectoryBar()
  837. {
  838. if (folderListLayout != null)
  839. {
  840. folderListLayout.Destroy();
  841. folderListLayout = null;
  842. }
  843. folderListLayout = folderBarLayout.AddLayoutX();
  844. string[] folders = null;
  845. string[] fullPaths = null;
  846. if (IsSearchActive)
  847. {
  848. folders = new[] {searchQuery};
  849. fullPaths = new[] { searchQuery };
  850. }
  851. else
  852. {
  853. string currentDir = Path.Combine("Resources", CurrentFolder);
  854. folders = currentDir.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar },
  855. StringSplitOptions.RemoveEmptyEntries);
  856. fullPaths = new string[folders.Length];
  857. for (int i = 0; i < folders.Length; i++)
  858. {
  859. if (i == 0)
  860. fullPaths[i] = "";
  861. else
  862. fullPaths[i] = Path.Combine(fullPaths[i - 1], folders[i]);
  863. }
  864. }
  865. int availableWidth = folderBarLayout.Bounds.width - FOLDER_BUTTON_WIDTH * 2;
  866. int numFolders = 0;
  867. for (int i = folders.Length - 1; i >= 0; i--)
  868. {
  869. GUIButton folderButton = new GUIButton(folders[i]);
  870. if (!IsSearchActive)
  871. {
  872. string fullPath = fullPaths[i];
  873. folderButton.OnClick += () => OnFolderButtonClicked(fullPath);
  874. }
  875. GUIButton separator = new GUIButton("/", GUIOption.FixedWidth(FOLDER_SEPARATOR_WIDTH));
  876. folderListLayout.InsertElement(0, separator);
  877. folderListLayout.InsertElement(0, folderButton);
  878. numFolders++;
  879. Rect2I folderListBounds = folderListLayout.Bounds;
  880. if (folderListBounds.width > availableWidth)
  881. {
  882. if (numFolders > 2)
  883. {
  884. separator.Destroy();
  885. folderButton.Destroy();
  886. break;
  887. }
  888. }
  889. }
  890. }
  891. /// <summary>
  892. /// Performs <see cref="Cut"/> operation on the currently selected elements.
  893. /// </summary>
  894. internal void CutSelection()
  895. {
  896. if (selectionPaths.Count > 0)
  897. Cut(selectionPaths);
  898. }
  899. /// <summary>
  900. /// Performs <see cref="Copy"/> operation on the currently selected elements.
  901. /// </summary>
  902. internal void CopySelection()
  903. {
  904. if (selectionPaths.Count > 0)
  905. Copy(selectionPaths);
  906. }
  907. /// <summary>
  908. /// Performs <see cref="Duplicate"/> operation on the currently selected elements.
  909. /// </summary>
  910. internal void DuplicateSelection()
  911. {
  912. if (selectionPaths.Count > 0)
  913. Duplicate(selectionPaths);
  914. }
  915. /// <summary>
  916. /// Performs <see cref="Paste"/> operation. Elements will be pasted in the currently selected directory (if any), or
  917. /// the active directory otherwise.
  918. /// </summary>
  919. internal void PasteToSelection()
  920. {
  921. Paste(SelectedFolder);
  922. }
  923. /// <summary>
  924. /// Starts a rename operation on the currently selected elements. If more than one elements are selected only the
  925. /// first one will be affected.
  926. /// </summary>
  927. internal void RenameSelection()
  928. {
  929. string[] filePaths = GetFiles(selectionPaths);
  930. if (filePaths.Length == 0)
  931. return;
  932. if (filePaths.Length > 1)
  933. {
  934. DeselectAll();
  935. Select(filePaths[0]);
  936. }
  937. LibraryGUIEntry entry;
  938. if (content.TryGetEntry(filePaths[0], out entry))
  939. {
  940. entry.StartRename();
  941. inProgressRenameElement = entry;
  942. }
  943. }
  944. /// <summary>
  945. /// Deletes currently selected elements. User will be asked to confirm deletion via a dialog box.
  946. /// </summary>
  947. internal void DeleteSelection()
  948. {
  949. string[] filePaths = GetFiles(selectionPaths);
  950. if (filePaths.Length == 0)
  951. return;
  952. DialogBox.Open(new LocEdString("Confirm deletion"), new LocEdString("Are you sure you want to delete the selected object(s)?"),
  953. DialogBox.Type.YesNo,
  954. type =>
  955. {
  956. if (type == DialogBox.ResultType.Yes)
  957. {
  958. foreach (var path in filePaths)
  959. ProjectLibrary.Delete(path);
  960. DeselectAll();
  961. Refresh();
  962. }
  963. });
  964. }
  965. /// <summary>
  966. /// Stops the rename operation, if one is in progress on any element.
  967. /// </summary>
  968. internal void StopRename()
  969. {
  970. if (inProgressRenameElement != null)
  971. {
  972. inProgressRenameElement.StopRename();
  973. inProgressRenameElement = null;
  974. }
  975. }
  976. /// <summary>
  977. /// Clears the search bar and refreshes the content area to display contents of the current directory.
  978. /// </summary>
  979. private void ClearSearch()
  980. {
  981. searchField.Value = "";
  982. searchQuery = "";
  983. Refresh();
  984. }
  985. /// <summary>
  986. /// Takes a list of resource paths and returns only those referencing files or folder and not sub-resources.
  987. /// </summary>
  988. /// <param name="resourcePaths">List of resource paths to find files for.</param>
  989. /// <returns>File paths for all the provided resources.</returns>
  990. private string[] GetFiles(IEnumerable<string> resourcePaths)
  991. {
  992. HashSet<string> filePaths = new HashSet<string>();
  993. foreach (var resPath in resourcePaths)
  994. {
  995. if (resPath == null)
  996. continue;
  997. LibraryEntry entry = ProjectLibrary.GetEntry(resPath);
  998. if (entry == null)
  999. continue;
  1000. if (ProjectLibrary.IsSubresource(resPath))
  1001. continue;
  1002. if (!filePaths.Contains(entry.Path))
  1003. filePaths.Add(entry.Path);
  1004. }
  1005. string[] output = new string[filePaths.Count];
  1006. int i = 0;
  1007. foreach(var path in filePaths)
  1008. output[i++] = path;
  1009. return output;
  1010. }
  1011. /// <summary>
  1012. /// Opens the drop down options window that allows you to customize library window look and feel.
  1013. /// </summary>
  1014. private void OpenOptionsWindow()
  1015. {
  1016. Vector2I openPosition;
  1017. Rect2I buttonBounds = GUILayoutUtility.CalculateBounds(optionsButton, GUI);
  1018. openPosition.x = buttonBounds.x + buttonBounds.width / 2;
  1019. openPosition.y = buttonBounds.y + buttonBounds.height / 2;
  1020. LibraryDropDown dropDown = DropDownWindow.Open<LibraryDropDown>(this, openPosition);
  1021. dropDown.Initialize(this);
  1022. }
  1023. /// <summary>
  1024. /// Returns the content scroll area bounds.
  1025. /// </summary>
  1026. /// <returns>Bounds of the content scroll area, relative to the window.</returns>
  1027. private Rect2I GetScrollAreaBounds()
  1028. {
  1029. Rect2I bounds = GUI.Bounds;
  1030. Rect2I folderListBounds = folderListLayout.Bounds;
  1031. Rect2I searchBarBounds = searchBarLayout.Bounds;
  1032. bounds.y = folderListBounds.height + searchBarBounds.height;
  1033. bounds.height -= folderListBounds.height + searchBarBounds.height;
  1034. return bounds;
  1035. }
  1036. /// <summary>
  1037. /// Triggered when a project library entry was changed (added, modified, deleted).
  1038. /// </summary>
  1039. /// <param name="entry">Project library path of the changed entry.</param>
  1040. private void OnEntryChanged(string entry)
  1041. {
  1042. requiresRefresh = true;
  1043. }
  1044. /// <summary>
  1045. /// Triggered when the drag and drop operation is starting while over the content area. If drag operation is over
  1046. /// an element, element will be dragged.
  1047. /// </summary>
  1048. /// <param name="windowPos">Coordinates where the drag operation started, relative to the window.</param>
  1049. private void OnDragStart(Vector2I windowPos)
  1050. {
  1051. bool isRenameInProgress = inProgressRenameElement != null;
  1052. if (isRenameInProgress)
  1053. return;
  1054. LibraryGUIEntry underCursorElem = FindElementAt(windowPos);
  1055. if (underCursorElem == null)
  1056. {
  1057. StartDragSelection(windowPos);
  1058. return;
  1059. }
  1060. string resourceDir = ProjectLibrary.ResourceFolder;
  1061. string[] dragPaths = null;
  1062. if (selectionPaths.Count > 0)
  1063. {
  1064. foreach (var path in selectionPaths)
  1065. {
  1066. if (path == underCursorElem.path)
  1067. {
  1068. dragPaths = new string[selectionPaths.Count];
  1069. for (int i = 0; i < selectionPaths.Count; i++)
  1070. {
  1071. dragPaths[i] = Path.Combine(resourceDir, selectionPaths[i]);
  1072. }
  1073. break;
  1074. }
  1075. }
  1076. }
  1077. if (dragPaths == null)
  1078. dragPaths = new[] { Path.Combine(resourceDir, underCursorElem.path) };
  1079. ResourceDragDropData dragDropData = new ResourceDragDropData(dragPaths);
  1080. DragDrop.StartDrag(dragDropData);
  1081. }
  1082. /// <summary>
  1083. /// Triggered when a pointer is moved while a drag operation is in progress.
  1084. /// </summary>
  1085. /// <param name="windowPos">Coordinates of the pointer relative to the window.</param>
  1086. private void OnDragMove(Vector2I windowPos)
  1087. {
  1088. // Auto-scroll
  1089. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  1090. int scrollAreaTop = scrollAreaBounds.y;
  1091. int scrollAreaBottom = scrollAreaBounds.y + scrollAreaBounds.height;
  1092. if (windowPos.y > scrollAreaTop && windowPos.y <= (scrollAreaTop + DRAG_SCROLL_HEIGHT))
  1093. autoScrollAmount = -DRAG_SCROLL_AMOUNT_PER_SECOND;
  1094. else if (windowPos.y >= (scrollAreaBottom - DRAG_SCROLL_HEIGHT) && windowPos.y < scrollAreaBottom)
  1095. autoScrollAmount = DRAG_SCROLL_AMOUNT_PER_SECOND;
  1096. else
  1097. autoScrollAmount = 0;
  1098. // Selection box
  1099. if (UpdateDragSelection(windowPos))
  1100. return;
  1101. // Drag and drop (hover element under cursor)
  1102. LibraryGUIEntry underCursorElem = FindElementAt(windowPos);
  1103. if (underCursorElem == null)
  1104. {
  1105. ClearHoverHighlight();
  1106. }
  1107. else
  1108. {
  1109. if (underCursorElem.path != hoverHighlightPath)
  1110. {
  1111. ClearHoverHighlight();
  1112. hoverHighlightPath = underCursorElem.path;
  1113. underCursorElem.MarkAsHovered(true);
  1114. }
  1115. }
  1116. }
  1117. /// <summary>
  1118. /// Triggered when a pointer leaves the drop targer while a drag operation is in progress.
  1119. /// </summary>
  1120. private void OnDragLeave()
  1121. {
  1122. ClearHoverHighlight();
  1123. autoScrollAmount = 0;
  1124. }
  1125. /// <summary>
  1126. /// Triggered when a resource drop operation finishes over the content area.
  1127. /// </summary>
  1128. /// <param name="windowPos">Coordinates of the pointer relative to the window where the drop operation finished
  1129. /// .</param>
  1130. /// <param name="paths">Paths of the dropped resources.</param>
  1131. private void OnResourceDragDropped(Vector2I windowPos, string[] paths)
  1132. {
  1133. ClearHoverHighlight();
  1134. autoScrollAmount = 0;
  1135. if (EndDragSelection())
  1136. return;
  1137. string resourceDir = ProjectLibrary.ResourceFolder;
  1138. string destinationFolder = Path.Combine(resourceDir, CurrentFolder);
  1139. LibraryGUIEntry underCursorElement = FindElementAt(windowPos);
  1140. if (underCursorElement != null)
  1141. {
  1142. LibraryEntry entry = ProjectLibrary.GetEntry(underCursorElement.path);
  1143. if (entry != null && entry.Type == LibraryEntryType.Directory)
  1144. destinationFolder = Path.Combine(resourceDir, entry.Path);
  1145. }
  1146. if (paths != null)
  1147. {
  1148. string[] filePaths = GetFiles(paths);
  1149. List<string> addedResources = new List<string>();
  1150. foreach (var path in filePaths)
  1151. {
  1152. string absolutePath = path;
  1153. if (!Path.IsPathRooted(absolutePath))
  1154. absolutePath = Path.Combine(resourceDir, path);
  1155. if (string.IsNullOrEmpty(absolutePath))
  1156. continue;
  1157. if (PathEx.IsPartOf(destinationFolder, absolutePath) || PathEx.Compare(absolutePath, destinationFolder))
  1158. continue;
  1159. string pathTail = PathEx.GetTail(absolutePath);
  1160. string destination = Path.Combine(destinationFolder, pathTail);
  1161. if (PathEx.Compare(absolutePath, destination))
  1162. continue;
  1163. bool doCopy = !ProjectLibrary.Exists(absolutePath);
  1164. string uniqueDestination = LibraryUtility.GetUniquePath(destination);
  1165. if (Directory.Exists(path))
  1166. {
  1167. if (doCopy)
  1168. DirectoryEx.Copy(absolutePath, uniqueDestination);
  1169. else
  1170. DirectoryEx.Move(absolutePath, uniqueDestination);
  1171. }
  1172. else if (File.Exists(path))
  1173. {
  1174. if (doCopy)
  1175. FileEx.Copy(absolutePath, uniqueDestination);
  1176. else
  1177. ProjectLibrary.Move(absolutePath, uniqueDestination);
  1178. }
  1179. string relativeDestination = uniqueDestination.Substring(resourceDir.Length, uniqueDestination.Length - resourceDir.Length);
  1180. addedResources.Add(relativeDestination);
  1181. ProjectLibrary.Refresh();
  1182. }
  1183. SetSelection(addedResources);
  1184. }
  1185. }
  1186. /// <summary>
  1187. /// Triggered when a scene object drop operation finishes over the content area.
  1188. /// </summary>
  1189. /// <param name="windowPos">Coordinates of the pointer relative to the window where the drop operation finished
  1190. /// .</param>
  1191. /// <param name="objects">Dropped scene objects.</param>
  1192. private void OnSceneObjectDragDropped(Vector2I windowPos, SceneObject[] objects)
  1193. {
  1194. ClearHoverHighlight();
  1195. autoScrollAmount = 0;
  1196. if (EndDragSelection())
  1197. return;
  1198. string destinationFolder = CurrentFolder;
  1199. LibraryGUIEntry underCursorElement = FindElementAt(windowPos);
  1200. if (underCursorElement != null)
  1201. {
  1202. LibraryEntry entry = ProjectLibrary.GetEntry(underCursorElement.path);
  1203. if (entry != null && entry.Type == LibraryEntryType.Directory)
  1204. destinationFolder = entry.Path;
  1205. }
  1206. if (objects != null)
  1207. {
  1208. List<string> addedResources = new List<string>();
  1209. foreach (var so in objects)
  1210. {
  1211. if (so == null)
  1212. continue;
  1213. Prefab newPrefab = new Prefab(so);
  1214. string destination = LibraryUtility.GetUniquePath(Path.Combine(destinationFolder, so.Name + ".prefab"));
  1215. addedResources.Add(destination);
  1216. ProjectLibrary.Create(newPrefab, destination);
  1217. ProjectLibrary.Refresh();
  1218. }
  1219. SetSelection(addedResources);
  1220. }
  1221. }
  1222. /// <summary>
  1223. /// Triggered when a drag operation that originated from this window ends.
  1224. /// </summary>
  1225. /// <param name="windowPos">Coordinates of the pointer where the drag ended relative to the window </param>
  1226. private void OnDragEnd(Vector2I windowPos)
  1227. {
  1228. EndDragSelection();
  1229. autoScrollAmount = 0;
  1230. }
  1231. /// <summary>
  1232. /// Triggered when the global selection changes.
  1233. /// </summary>
  1234. /// <param name="sceneObjects">A set of newly selected scene objects.</param>
  1235. /// <param name="resourcePaths">A set of paths for newly selected resources.</param>
  1236. private void OnSelectionChanged(SceneObject[] sceneObjects, string[] resourcePaths)
  1237. {
  1238. if (sceneObjects.Length > 0)
  1239. DeselectAll(true);
  1240. else
  1241. SetSelection(new List<string>(resourcePaths), true);
  1242. }
  1243. /// <summary>
  1244. /// Triggered when a ping operation was triggered externally.
  1245. /// </summary>
  1246. /// <param name="path">Path to the resource to highlight.</param>
  1247. private void OnPing(string path)
  1248. {
  1249. Ping(path);
  1250. }
  1251. /// <summary>
  1252. /// Triggered when a folder on the directory bar was selected.
  1253. /// </summary>
  1254. /// <param name="path">Project library path to the folder to enter.</param>
  1255. private void OnFolderButtonClicked(string path)
  1256. {
  1257. EnterDirectory(path);
  1258. }
  1259. /// <summary>
  1260. /// Triggered when the user clicks on empty space between elements.
  1261. /// </summary>
  1262. private void OnCatchAllClicked()
  1263. {
  1264. DeselectAll();
  1265. }
  1266. /// <summary>
  1267. /// Triggered when the user clicks on the home button on the directory bar, changing the active directory to
  1268. /// project library root.
  1269. /// </summary>
  1270. private void OnHomeClicked()
  1271. {
  1272. CurrentFolder = ProjectLibrary.Root.Path;
  1273. Refresh();
  1274. }
  1275. /// <summary>
  1276. /// Triggered when the user clicks on the up button on the directory bar, changing the active directory to the
  1277. /// parent directory, unless already at project library root.
  1278. /// </summary>
  1279. private void OnUpClicked()
  1280. {
  1281. string currentDir = CurrentFolder;
  1282. currentDir = currentDir.Trim(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  1283. if (!string.IsNullOrEmpty(currentDir))
  1284. {
  1285. string parent = Path.GetDirectoryName(currentDir);
  1286. CurrentFolder = parent;
  1287. Refresh();
  1288. }
  1289. }
  1290. /// <summary>
  1291. /// Triggered when the user inputs new values into the search input box. Refreshes the contents so they display
  1292. /// elements matching the search text.
  1293. /// </summary>
  1294. /// <param name="newValue">Search box text.</param>
  1295. private void OnSearchChanged(string newValue)
  1296. {
  1297. searchQuery = newValue;
  1298. Refresh();
  1299. }
  1300. /// <summary>
  1301. /// Sorts the specified set of project library entries by type (folder or resource), followed by name.
  1302. /// </summary>
  1303. /// <param name="input">Set of project library entries to sort.</param>
  1304. private static void SortEntries(LibraryEntry[] input)
  1305. {
  1306. Array.Sort(input, (x, y) =>
  1307. {
  1308. if (x.Type == y.Type)
  1309. return x.Name.CompareTo(y.Name);
  1310. else
  1311. return x.Type == LibraryEntryType.File ? 1 : -1;
  1312. });
  1313. }
  1314. }
  1315. }