LibraryWindow.cs 60 KB

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