LibraryWindow.cs 59 KB

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