LibraryWindow.cs 58 KB

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