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