LibraryWindow.cs 60 KB

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