LibraryWindow.cs 59 KB

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