LibraryWindow.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. internal enum ProjectViewType
  8. {
  9. Grid64, Grid48, Grid32, List16
  10. }
  11. internal sealed class LibraryWindow : EditorWindow
  12. {
  13. internal enum MoveDirection
  14. {
  15. Up, Down, Left, Right
  16. }
  17. private const int DRAG_SCROLL_HEIGHT = 20;
  18. private const int DRAG_SCROLL_AMOUNT_PER_SECOND = 100;
  19. private const int FOLDER_BUTTON_WIDTH = 20;
  20. private const int FOLDER_SEPARATOR_WIDTH = 10;
  21. private bool hasContentFocus = false;
  22. private bool HasContentFocus { get { return HasFocus && hasContentFocus; } }
  23. private string searchQuery;
  24. private bool IsSearchActive { get { return !string.IsNullOrEmpty(searchQuery); } }
  25. private ProjectViewType viewType = ProjectViewType.Grid32;
  26. private bool requiresRefresh;
  27. private string currentDirectory = "";
  28. private List<string> selectionPaths = new List<string>();
  29. private int selectionAnchorStart = -1;
  30. private int selectionAnchorEnd = -1;
  31. private string pingPath = "";
  32. private string hoverHighlightPath = "";
  33. private LibraryGUIContent content;
  34. private GUIScrollArea contentScrollArea;
  35. private GUILayoutX searchBarLayout;
  36. private GUIButton optionsButton;
  37. private GUILayout folderBarLayout;
  38. private GUILayout folderListLayout;
  39. private GUITextField searchField;
  40. private GUITexture dragSelection;
  41. private ContextMenu entryContextMenu;
  42. private LibraryDropTarget dropTarget;
  43. private int autoScrollAmount;
  44. private bool isDraggingSelection;
  45. private Vector2I dragSelectionStart;
  46. private Vector2I dragSelectionEnd;
  47. private LibraryGUIEntry inProgressRenameElement;
  48. // Cut/Copy/Paste
  49. private List<string> copyPaths = new List<string>();
  50. private List<string> cutPaths = new List<string>();
  51. internal ProjectViewType ViewType
  52. {
  53. get { return viewType; }
  54. set { viewType = value; Refresh(); }
  55. }
  56. /// <summary>
  57. /// Returns a file or folder currently selected in the library window. If nothing is selected, returns the active
  58. /// folder. Returned path is relative to project library resources folder.
  59. /// </summary>
  60. public string SelectedEntry
  61. {
  62. get
  63. {
  64. if (selectionPaths.Count == 1)
  65. {
  66. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  67. if (entry != null)
  68. return entry.Path;
  69. }
  70. return currentDirectory;
  71. }
  72. }
  73. /// <summary>
  74. /// Returns a folder currently selected in the library window. If no folder is selected, returns the active
  75. /// folder. Returned path is relative to project library resources folder.
  76. /// </summary>
  77. public string SelectedFolder
  78. {
  79. get
  80. {
  81. DirectoryEntry selectedDirectory = null;
  82. if (selectionPaths.Count == 1)
  83. {
  84. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  85. if (entry != null && entry.Type == LibraryEntryType.Directory)
  86. selectedDirectory = (DirectoryEntry) entry;
  87. }
  88. if (selectedDirectory != null)
  89. return selectedDirectory.Path;
  90. return currentDirectory;
  91. }
  92. }
  93. /// <summary>
  94. /// Returns the path to the folder currently displayed in the library window. Returned path is relative to project
  95. /// library resources folder.
  96. /// </summary>
  97. public string CurrentFolder
  98. {
  99. get { return currentDirectory; }
  100. }
  101. // Note: I don't feel like I should be exposing this
  102. internal ContextMenu ContextMenu
  103. {
  104. get { return entryContextMenu; }
  105. }
  106. [MenuItem("Windows/Library", ButtonModifier.CtrlAlt, ButtonCode.L, 6000)]
  107. private static void OpenLibraryWindow()
  108. {
  109. OpenWindow<LibraryWindow>();
  110. }
  111. private void OnInitialize()
  112. {
  113. ProjectLibrary.OnEntryAdded += OnEntryChanged;
  114. ProjectLibrary.OnEntryRemoved += OnEntryChanged;
  115. GUILayoutY contentLayout = GUI.AddLayoutY();
  116. searchBarLayout = contentLayout.AddLayoutX();
  117. searchField = new GUITextField();
  118. searchField.OnChanged += OnSearchChanged;
  119. GUIButton clearSearchBtn = new GUIButton("C");
  120. clearSearchBtn.OnClick += ClearSearch;
  121. clearSearchBtn.SetWidth(40);
  122. optionsButton = new GUIButton("O");
  123. optionsButton.OnClick += OpenOptionsWindow;
  124. optionsButton.SetWidth(40);
  125. searchBarLayout.AddElement(searchField);
  126. searchBarLayout.AddElement(clearSearchBtn);
  127. searchBarLayout.AddElement(optionsButton);
  128. folderBarLayout = contentLayout.AddLayoutX();
  129. GUIButton homeButton = new GUIButton("H", GUIOption.FixedWidth(FOLDER_BUTTON_WIDTH));
  130. homeButton.OnClick += OnHomeClicked;
  131. GUIButton upButton = new GUIButton("U", GUIOption.FixedWidth(FOLDER_BUTTON_WIDTH));
  132. upButton.OnClick += OnUpClicked;
  133. folderBarLayout.AddElement(homeButton);
  134. folderBarLayout.AddElement(upButton);
  135. folderBarLayout.AddSpace(10);
  136. contentScrollArea = new GUIScrollArea(GUIOption.FlexibleWidth(), GUIOption.FlexibleHeight());
  137. contentLayout.AddElement(contentScrollArea);
  138. contentLayout.AddFlexibleSpace();
  139. entryContextMenu = LibraryMenu.CreateContextMenu(this);
  140. content = new LibraryGUIContent(this, contentScrollArea);
  141. Reset();
  142. dropTarget = new LibraryDropTarget(this);
  143. dropTarget.Bounds = contentScrollArea.Bounds;
  144. dropTarget.OnStart += OnDragStart;
  145. dropTarget.OnDrag += OnDragMove;
  146. dropTarget.OnLeave += OnDragLeave;
  147. dropTarget.OnDropResource += OnResourceDragDropped;
  148. dropTarget.OnDropSceneObject += OnSceneObjectDragDropped;
  149. dropTarget.OnEnd += OnDragEnd;
  150. Selection.OnSelectionChanged += OnSelectionChanged;
  151. Selection.OnResourcePing += OnPing;
  152. }
  153. private void OnDestroy()
  154. {
  155. Selection.OnSelectionChanged -= OnSelectionChanged;
  156. Selection.OnResourcePing -= OnPing;
  157. }
  158. private void OnEditorUpdate()
  159. {
  160. bool isRenameInProgress = inProgressRenameElement != null;
  161. if (HasContentFocus)
  162. {
  163. if (!isRenameInProgress)
  164. {
  165. if (Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl))
  166. {
  167. if (Input.IsButtonUp(ButtonCode.C))
  168. {
  169. CopySelection();
  170. }
  171. else if (Input.IsButtonUp(ButtonCode.X))
  172. {
  173. CutSelection();
  174. }
  175. else if (Input.IsButtonUp(ButtonCode.D))
  176. {
  177. DuplicateSelection();
  178. }
  179. else if (Input.IsButtonUp(ButtonCode.V))
  180. {
  181. PasteToSelection();
  182. }
  183. }
  184. if (Input.IsButtonDown(ButtonCode.Return))
  185. {
  186. if (selectionPaths.Count == 1)
  187. {
  188. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  189. if (entry != null && entry.Type == LibraryEntryType.Directory)
  190. {
  191. EnterDirectory(entry.Path);
  192. }
  193. }
  194. }
  195. else if (Input.IsButtonDown(ButtonCode.Back))
  196. {
  197. LibraryEntry entry = ProjectLibrary.GetEntry(currentDirectory);
  198. if (entry != null && entry.Parent != null)
  199. {
  200. EnterDirectory(entry.Parent.Path);
  201. }
  202. }
  203. else if (Input.IsButtonDown(ButtonCode.Up))
  204. {
  205. MoveSelection(MoveDirection.Up);
  206. }
  207. else if (Input.IsButtonDown(ButtonCode.Down))
  208. {
  209. MoveSelection(MoveDirection.Down);
  210. }
  211. else if (Input.IsButtonDown(ButtonCode.Left))
  212. {
  213. MoveSelection(MoveDirection.Left);
  214. }
  215. else if (Input.IsButtonDown(ButtonCode.Right))
  216. {
  217. MoveSelection(MoveDirection.Right);
  218. }
  219. else if (Input.IsButtonDown(ButtonCode.F2))
  220. {
  221. RenameSelection();
  222. }
  223. else if (Input.IsButtonDown(ButtonCode.Delete))
  224. {
  225. DeleteSelection();
  226. }
  227. }
  228. else
  229. {
  230. if (Input.IsButtonDown(ButtonCode.Return))
  231. {
  232. string newName = inProgressRenameElement.GetRenamedName();
  233. string originalPath = inProgressRenameElement.path;
  234. originalPath = originalPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  235. string newPath = Path.GetDirectoryName(originalPath);
  236. newPath = Path.Combine(newPath, newName + Path.GetExtension(originalPath));
  237. bool renameOK = true;
  238. if (!PathEx.IsValidFileName(newName))
  239. {
  240. DialogBox.Open(new LocEdString("Error"), new LocEdString("The name you specified is not a valid file name. Try another."), DialogBox.Type.OK);
  241. renameOK = false;
  242. }
  243. if (renameOK)
  244. {
  245. // Windows sees paths with dot at the end as if they didn't have it
  246. // so remove the dot to ensure the project library does the same
  247. string trimmedNewPath = newPath.TrimEnd('.');
  248. if (originalPath != trimmedNewPath && ProjectLibrary.Exists(trimmedNewPath))
  249. {
  250. DialogBox.Open(new LocEdString("Error"), new LocEdString("File/folder with that name already exists in this folder."), DialogBox.Type.OK);
  251. renameOK = false;
  252. }
  253. }
  254. if (renameOK)
  255. {
  256. ProjectLibrary.Rename(originalPath, newPath);
  257. StopRename();
  258. }
  259. }
  260. else if (Input.IsButtonDown(ButtonCode.Escape))
  261. {
  262. StopRename();
  263. }
  264. }
  265. }
  266. else
  267. {
  268. if (isRenameInProgress)
  269. StopRename();
  270. }
  271. if (autoScrollAmount != 0)
  272. {
  273. Rect2I contentBounds = contentScrollArea.ContentBounds;
  274. float scrollPct = autoScrollAmount / (float)contentBounds.height;
  275. contentScrollArea.VerticalScroll += scrollPct * Time.FrameDelta;
  276. }
  277. if (requiresRefresh)
  278. Refresh();
  279. dropTarget.Update();
  280. }
  281. protected override LocString GetDisplayName()
  282. {
  283. return new LocEdString("Library");
  284. }
  285. protected override void WindowResized(int width, int height)
  286. {
  287. base.WindowResized(width, height);
  288. Refresh();
  289. dropTarget.Bounds = contentScrollArea.Bounds;
  290. }
  291. private LibraryGUIEntry FindElementAt(Vector2I windowPos)
  292. {
  293. Vector2I scrollPos = WindowToScrollAreaCoords(windowPos);
  294. return content.FindElementAt(scrollPos);
  295. }
  296. private void ClearHoverHighlight()
  297. {
  298. content.MarkAsHovered(hoverHighlightPath, false);
  299. hoverHighlightPath = "";
  300. }
  301. public void Ping(string path)
  302. {
  303. content.MarkAsPinged(pingPath, false);
  304. pingPath = path;
  305. content.MarkAsPinged(pingPath, true);
  306. }
  307. public void Reset()
  308. {
  309. currentDirectory = ProjectLibrary.Root.Path;
  310. selectionAnchorStart = -1;
  311. selectionAnchorEnd = -1;
  312. selectionPaths.Clear();
  313. pingPath = "";
  314. hoverHighlightPath = "";
  315. Refresh();
  316. }
  317. internal void DeselectAll(bool onlyInternal = false)
  318. {
  319. SetSelection(new List<string>(), onlyInternal);
  320. selectionAnchorStart = -1;
  321. selectionAnchorEnd = -1;
  322. }
  323. internal void Select(string path)
  324. {
  325. LibraryGUIEntry entry;
  326. if (!content.TryGetEntry(path, out entry))
  327. return;
  328. bool ctrlDown = Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl);
  329. bool shiftDown = Input.IsButtonHeld(ButtonCode.LeftShift) || Input.IsButtonHeld(ButtonCode.RightShift);
  330. if (shiftDown)
  331. {
  332. if (selectionAnchorStart != -1 && selectionAnchorStart < content.Entries.Length)
  333. {
  334. int start = Math.Min(entry.index, selectionAnchorStart);
  335. int end = Math.Max(entry.index, selectionAnchorStart);
  336. List<string> newSelection = new List<string>();
  337. for(int i = start; i <= end; i++)
  338. newSelection.Add(content.Entries[i].path);
  339. SetSelection(newSelection);
  340. selectionAnchorEnd = entry.index;
  341. }
  342. else
  343. {
  344. SetSelection(new List<string>() {path});
  345. selectionAnchorStart = entry.index;
  346. selectionAnchorEnd = entry.index;
  347. }
  348. }
  349. else if (ctrlDown)
  350. {
  351. List<string> newSelection = new List<string>(selectionPaths);
  352. if (selectionPaths.Contains(path))
  353. {
  354. newSelection.Remove(path);
  355. if (newSelection.Count == 0)
  356. DeselectAll();
  357. else
  358. {
  359. if (selectionAnchorStart == entry.index)
  360. {
  361. LibraryGUIEntry newAnchorEntry;
  362. if (!content.TryGetEntry(newSelection[0], out newAnchorEntry))
  363. selectionAnchorStart = -1;
  364. else
  365. selectionAnchorStart = newAnchorEntry.index;
  366. }
  367. if (selectionAnchorEnd == entry.index)
  368. {
  369. LibraryGUIEntry newAnchorEntry;
  370. if (!content.TryGetEntry(newSelection[newSelection.Count - 1], out newAnchorEntry))
  371. selectionAnchorEnd = -1;
  372. else
  373. selectionAnchorEnd = newAnchorEntry.index;
  374. }
  375. SetSelection(newSelection);
  376. }
  377. }
  378. else
  379. {
  380. newSelection.Add(path);
  381. SetSelection(newSelection);
  382. selectionAnchorEnd = entry.index;
  383. }
  384. }
  385. else
  386. {
  387. SetSelection(new List<string>() {path});
  388. selectionAnchorStart = entry.index;
  389. selectionAnchorEnd = entry.index;
  390. }
  391. }
  392. internal void MoveSelection(MoveDirection dir)
  393. {
  394. string newPath = "";
  395. if (selectionPaths.Count == 0 || selectionAnchorEnd == -1)
  396. {
  397. // Nothing is selected so we arbitrarily select first or last element
  398. if (content.Entries.Length > 0)
  399. {
  400. switch (dir)
  401. {
  402. case MoveDirection.Left:
  403. case MoveDirection.Up:
  404. newPath = content.Entries[content.Entries.Length - 1].path;
  405. break;
  406. case MoveDirection.Right:
  407. case MoveDirection.Down:
  408. newPath = content.Entries[0].path;
  409. break;
  410. }
  411. }
  412. }
  413. else
  414. {
  415. switch (dir)
  416. {
  417. case MoveDirection.Left:
  418. if (selectionAnchorEnd - 1 >= 0)
  419. newPath = content.Entries[selectionAnchorEnd - 1].path;
  420. break;
  421. case MoveDirection.Up:
  422. if (selectionAnchorEnd - content.ElementsPerRow >= 0)
  423. newPath = content.Entries[selectionAnchorEnd - content.ElementsPerRow].path;
  424. break;
  425. case MoveDirection.Right:
  426. if (selectionAnchorEnd + 1 < content.Entries.Length)
  427. newPath = content.Entries[selectionAnchorEnd + 1].path;
  428. break;
  429. case MoveDirection.Down:
  430. if (selectionAnchorEnd + content.ElementsPerRow < content.Entries.Length)
  431. newPath = content.Entries[selectionAnchorEnd + content.ElementsPerRow].path;
  432. break;
  433. }
  434. }
  435. if (!string.IsNullOrEmpty(newPath))
  436. {
  437. Select(newPath);
  438. ScrollToEntry(newPath);
  439. }
  440. }
  441. internal void SetSelection(List<string> paths, bool onlyInternal = false)
  442. {
  443. if (selectionPaths != null)
  444. {
  445. foreach (var path in selectionPaths)
  446. content.MarkAsSelected(path, false);
  447. }
  448. selectionPaths = paths;
  449. if (selectionPaths != null)
  450. {
  451. foreach (var path in selectionPaths)
  452. content.MarkAsSelected(path, true);
  453. }
  454. Ping("");
  455. StopRename();
  456. if (!onlyInternal)
  457. {
  458. if (selectionPaths != null)
  459. Selection.resourcePaths = selectionPaths.ToArray();
  460. else
  461. Selection.resourcePaths = new string[0];
  462. }
  463. }
  464. internal void EnterDirectory(string directory)
  465. {
  466. currentDirectory = directory;
  467. DeselectAll();
  468. Refresh();
  469. }
  470. internal void Cut(IEnumerable<string> sourcePaths)
  471. {
  472. foreach (var path in cutPaths)
  473. content.MarkAsCut(path, false);
  474. cutPaths.Clear();
  475. cutPaths.AddRange(sourcePaths);
  476. foreach (var path in cutPaths)
  477. content.MarkAsCut(path, true);
  478. copyPaths.Clear();
  479. }
  480. internal void Copy(IEnumerable<string> sourcePaths)
  481. {
  482. copyPaths.Clear();
  483. copyPaths.AddRange(sourcePaths);
  484. foreach (var path in cutPaths)
  485. content.MarkAsCut(path, false);
  486. cutPaths.Clear();
  487. }
  488. internal void Duplicate(IEnumerable<string> sourcePaths)
  489. {
  490. foreach (var source in sourcePaths)
  491. {
  492. if (Directory.Exists(source))
  493. DirectoryEx.Copy(source, LibraryUtility.GetUniquePath(source));
  494. else if (File.Exists(source))
  495. FileEx.Copy(source, LibraryUtility.GetUniquePath(source));
  496. ProjectLibrary.Refresh();
  497. }
  498. }
  499. internal void Paste(string destinationFolder)
  500. {
  501. if (copyPaths.Count > 0)
  502. {
  503. for (int i = 0; i < copyPaths.Count; i++)
  504. {
  505. string destination = Path.Combine(destinationFolder, PathEx.GetTail(copyPaths[i]));
  506. if (Directory.Exists(copyPaths[i]))
  507. DirectoryEx.Copy(copyPaths[i], LibraryUtility.GetUniquePath(destination));
  508. else if (File.Exists(copyPaths[i]))
  509. FileEx.Copy(copyPaths[i], LibraryUtility.GetUniquePath(destination));
  510. }
  511. ProjectLibrary.Refresh();
  512. }
  513. else if (cutPaths.Count > 0)
  514. {
  515. for (int i = 0; i < cutPaths.Count; i++)
  516. {
  517. string destination = Path.Combine(destinationFolder, PathEx.GetTail(cutPaths[i]));
  518. if (Directory.Exists(cutPaths[i]))
  519. DirectoryEx.Move(cutPaths[i], LibraryUtility.GetUniquePath(destination));
  520. else if (File.Exists(cutPaths[i]))
  521. FileEx.Move(cutPaths[i], LibraryUtility.GetUniquePath(destination));
  522. }
  523. cutPaths.Clear();
  524. ProjectLibrary.Refresh();
  525. }
  526. }
  527. private void ScrollToEntry(string path)
  528. {
  529. LibraryGUIEntry entryGUI;
  530. if (!content.TryGetEntry(path, out entryGUI))
  531. return;
  532. Rect2I entryBounds = entryGUI.Bounds;
  533. Rect2I contentBounds = contentScrollArea.Layout.Bounds;
  534. Rect2I windowEntryBounds = entryBounds;
  535. windowEntryBounds.x += contentBounds.x;
  536. windowEntryBounds.y += contentBounds.y;
  537. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  538. bool requiresScroll = windowEntryBounds.y < scrollAreaBounds.y ||
  539. (windowEntryBounds.y + windowEntryBounds.height) > (scrollAreaBounds.y + scrollAreaBounds.height);
  540. if (!requiresScroll)
  541. return;
  542. int scrollableSize = contentBounds.height - scrollAreaBounds.height;
  543. float percent = (((entryBounds.y + entryBounds.height * 0.5f) - scrollAreaBounds.height * 0.5f) / (float)scrollableSize);
  544. percent = MathEx.Clamp01(percent);
  545. contentScrollArea.VerticalScroll = percent;
  546. }
  547. private void Refresh()
  548. {
  549. requiresRefresh = false;
  550. LibraryEntry[] entriesToDisplay = new LibraryEntry[0];
  551. if (IsSearchActive)
  552. {
  553. entriesToDisplay = ProjectLibrary.Search("*" + searchQuery + "*");
  554. }
  555. else
  556. {
  557. DirectoryEntry entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  558. if (entry == null)
  559. {
  560. currentDirectory = ProjectLibrary.Root.Path;
  561. entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  562. }
  563. if(entry != null)
  564. entriesToDisplay = entry.Children;
  565. }
  566. inProgressRenameElement = null;
  567. RefreshDirectoryBar();
  568. SortEntries(entriesToDisplay);
  569. content.Refresh(viewType, entriesToDisplay);
  570. if (entriesToDisplay.Length == 0)
  571. return;
  572. foreach (var path in cutPaths)
  573. content.MarkAsCut(path, true);
  574. foreach (var path in selectionPaths)
  575. content.MarkAsSelected(path, true);
  576. content.MarkAsPinged(pingPath, true);
  577. Rect2I contentBounds = content.Bounds;
  578. Rect2I minimalBounds = GetScrollAreaBounds();
  579. contentBounds.height = Math.Max(contentBounds.height, minimalBounds.height);
  580. GUIButton catchAll = new GUIButton("", EditorStyles.Blank);
  581. catchAll.Bounds = contentBounds;
  582. catchAll.OnClick += OnCatchAllClicked;
  583. catchAll.SetContextMenu(entryContextMenu);
  584. content.Underlay.AddElement(catchAll);
  585. Rect2I focusBounds = contentBounds; // Contents + Folder bar
  586. Rect2I scrollBounds = contentScrollArea.Bounds;
  587. focusBounds.x += scrollBounds.x;
  588. focusBounds.y += scrollBounds.y;
  589. Rect2I folderBarBounds = folderListLayout.Bounds;
  590. focusBounds.y -= folderBarBounds.height;
  591. focusBounds.height += folderBarBounds.height;
  592. GUIButton focusCatcher = new GUIButton("", EditorStyles.Blank);
  593. focusCatcher.OnFocusChanged += OnContentsFocusChanged;
  594. focusCatcher.Bounds = focusBounds;
  595. GUIPanel focusPanel = GUI.AddPanel(3);
  596. focusPanel.AddElement(focusCatcher);
  597. UpdateDragSelection(dragSelectionEnd);
  598. }
  599. private Vector2I WindowToScrollAreaCoords(Vector2I windowPos)
  600. {
  601. Rect2I scrollBounds = contentScrollArea.Layout.Bounds;
  602. Vector2I scrollPos = windowPos;
  603. scrollPos.x -= scrollBounds.x;
  604. scrollPos.y -= scrollBounds.y;
  605. return scrollPos;
  606. }
  607. private void StartDragSelection(Vector2I windowPos)
  608. {
  609. isDraggingSelection = true;
  610. dragSelectionStart = WindowToScrollAreaCoords(windowPos);
  611. dragSelectionEnd = dragSelectionStart;
  612. }
  613. private bool UpdateDragSelection(Vector2I windowPos)
  614. {
  615. if (!isDraggingSelection)
  616. return false;
  617. if (dragSelection == null)
  618. {
  619. dragSelection = new GUITexture(null, true, EditorStyles.SelectionArea);
  620. content.Overlay.AddElement(dragSelection);
  621. }
  622. dragSelectionEnd = WindowToScrollAreaCoords(windowPos);
  623. Rect2I selectionArea = CalculateSelectionArea();
  624. SelectInArea(selectionArea);
  625. dragSelection.Bounds = selectionArea;
  626. return true;
  627. }
  628. private bool EndDragSelection()
  629. {
  630. if (!isDraggingSelection)
  631. return false;
  632. if (dragSelection != null)
  633. {
  634. dragSelection.Destroy();
  635. dragSelection = null;
  636. }
  637. Rect2I selectionArea = CalculateSelectionArea();
  638. SelectInArea(selectionArea);
  639. isDraggingSelection = false;
  640. return false;
  641. }
  642. private Rect2I CalculateSelectionArea()
  643. {
  644. Rect2I selectionArea = new Rect2I();
  645. if (dragSelectionStart.x < dragSelectionEnd.x)
  646. {
  647. selectionArea.x = dragSelectionStart.x;
  648. selectionArea.width = dragSelectionEnd.x - dragSelectionStart.x;
  649. }
  650. else
  651. {
  652. selectionArea.x = dragSelectionEnd.x;
  653. selectionArea.width = dragSelectionStart.x - dragSelectionEnd.x;
  654. }
  655. if (dragSelectionStart.y < dragSelectionEnd.y)
  656. {
  657. selectionArea.y = dragSelectionStart.y;
  658. selectionArea.height = dragSelectionEnd.y - dragSelectionStart.y;
  659. }
  660. else
  661. {
  662. selectionArea.y = dragSelectionEnd.y;
  663. selectionArea.height = dragSelectionStart.y - dragSelectionEnd.y;
  664. }
  665. Rect2I maxBounds = contentScrollArea.Layout.Bounds;
  666. maxBounds.x = 0;
  667. maxBounds.y = 0;
  668. selectionArea.Clip(maxBounds);
  669. return selectionArea;
  670. }
  671. private void SelectInArea(Rect2I scrollBounds)
  672. {
  673. LibraryGUIEntry[] foundElements = content.FindElementsOverlapping(scrollBounds);
  674. if (foundElements.Length > 0)
  675. {
  676. selectionAnchorStart = foundElements[0].index;
  677. selectionAnchorEnd = foundElements[foundElements.Length - 1].index;
  678. }
  679. else
  680. {
  681. selectionAnchorStart = -1;
  682. selectionAnchorEnd = -1;
  683. }
  684. List<string> elementPaths = new List<string>();
  685. foreach (var elem in foundElements)
  686. elementPaths.Add(elem.path);
  687. SetSelection(elementPaths);
  688. }
  689. private void RefreshDirectoryBar()
  690. {
  691. if (folderListLayout != null)
  692. {
  693. folderListLayout.Destroy();
  694. folderListLayout = null;
  695. }
  696. folderListLayout = folderBarLayout.AddLayoutX();
  697. string[] folders = null;
  698. string[] fullPaths = null;
  699. if (IsSearchActive)
  700. {
  701. folders = new[] {searchQuery};
  702. fullPaths = new[] { searchQuery };
  703. }
  704. else
  705. {
  706. string currentDir = Path.Combine("Resources", currentDirectory);
  707. folders = currentDir.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar },
  708. StringSplitOptions.RemoveEmptyEntries);
  709. fullPaths = new string[folders.Length];
  710. for (int i = 0; i < folders.Length; i++)
  711. {
  712. if (i == 0)
  713. fullPaths[i] = "";
  714. else
  715. fullPaths[i] = Path.Combine(fullPaths[i - 1], folders[i]);
  716. }
  717. }
  718. int availableWidth = folderBarLayout.Bounds.width - FOLDER_BUTTON_WIDTH * 2;
  719. int numFolders = 0;
  720. for (int i = folders.Length - 1; i >= 0; i--)
  721. {
  722. GUIButton folderButton = new GUIButton(folders[i]);
  723. if (!IsSearchActive)
  724. {
  725. string fullPath = fullPaths[i];
  726. folderButton.OnClick += () => OnFolderButtonClicked(fullPath);
  727. }
  728. GUIButton separator = new GUIButton("/", GUIOption.FixedWidth(FOLDER_SEPARATOR_WIDTH));
  729. folderListLayout.InsertElement(0, separator);
  730. folderListLayout.InsertElement(0, folderButton);
  731. numFolders++;
  732. Rect2I folderListBounds = folderListLayout.Bounds;
  733. if (folderListBounds.width > availableWidth)
  734. {
  735. if (numFolders > 2)
  736. {
  737. separator.Destroy();
  738. folderButton.Destroy();
  739. break;
  740. }
  741. }
  742. }
  743. }
  744. internal void CutSelection()
  745. {
  746. if (selectionPaths.Count > 0)
  747. Cut(selectionPaths);
  748. }
  749. internal void CopySelection()
  750. {
  751. if (selectionPaths.Count > 0)
  752. Copy(selectionPaths);
  753. }
  754. internal void DuplicateSelection()
  755. {
  756. if (selectionPaths.Count > 0)
  757. Duplicate(selectionPaths);
  758. }
  759. internal void PasteToSelection()
  760. {
  761. Paste(SelectedFolder);
  762. }
  763. internal void RenameSelection()
  764. {
  765. if (selectionPaths.Count == 0)
  766. return;
  767. if (selectionPaths.Count > 1)
  768. {
  769. DeselectAll();
  770. Select(selectionPaths[0]);
  771. }
  772. LibraryGUIEntry entry;
  773. if (content.TryGetEntry(selectionPaths[0], out entry))
  774. {
  775. entry.StartRename();
  776. inProgressRenameElement = entry;
  777. }
  778. }
  779. internal void DeleteSelection()
  780. {
  781. if (selectionPaths.Count == 0)
  782. return;
  783. DialogBox.Open(new LocEdString("Confirm deletion"), new LocEdString("Are you sure you want to delete the selected object(s)?"),
  784. DialogBox.Type.YesNo,
  785. type =>
  786. {
  787. if (type == DialogBox.ResultType.Yes)
  788. {
  789. foreach (var path in selectionPaths)
  790. {
  791. ProjectLibrary.Delete(path);
  792. }
  793. DeselectAll();
  794. Refresh();
  795. }
  796. });
  797. }
  798. internal void StopRename()
  799. {
  800. if (inProgressRenameElement != null)
  801. {
  802. inProgressRenameElement.StopRename();
  803. inProgressRenameElement = null;
  804. }
  805. }
  806. private void ClearSearch()
  807. {
  808. searchField.Value = "";
  809. searchQuery = "";
  810. Refresh();
  811. }
  812. private void OpenOptionsWindow()
  813. {
  814. Vector2I openPosition;
  815. Rect2I buttonBounds = GUILayoutUtility.CalculateBounds(optionsButton, GUI);
  816. openPosition.x = buttonBounds.x + buttonBounds.width / 2;
  817. openPosition.y = buttonBounds.y + buttonBounds.height / 2;
  818. LibraryDropDown dropDown = DropDownWindow.Open<LibraryDropDown>(this, openPosition);
  819. dropDown.SetParent(this);
  820. }
  821. private Rect2I GetScrollAreaBounds()
  822. {
  823. Rect2I bounds = GUI.Bounds;
  824. Rect2I folderListBounds = folderListLayout.Bounds;
  825. Rect2I searchBarBounds = searchBarLayout.Bounds;
  826. bounds.y += folderListBounds.height + searchBarBounds.height;
  827. bounds.height -= folderListBounds.height + searchBarBounds.height;
  828. return bounds;
  829. }
  830. private void OnEntryChanged(string entry)
  831. {
  832. requiresRefresh = true;
  833. }
  834. private void OnDragStart(Vector2I windowPos)
  835. {
  836. LibraryGUIEntry underCursorElem = FindElementAt(windowPos);
  837. if (underCursorElem == null)
  838. {
  839. StartDragSelection(windowPos);
  840. return;
  841. }
  842. string resourceDir = ProjectLibrary.ResourceFolder;
  843. string[] dragPaths = null;
  844. if (selectionPaths.Count > 0)
  845. {
  846. foreach (var path in selectionPaths)
  847. {
  848. if (path == underCursorElem.path)
  849. {
  850. dragPaths = new string[selectionPaths.Count];
  851. for (int i = 0; i < selectionPaths.Count; i++)
  852. {
  853. dragPaths[i] = Path.Combine(resourceDir, selectionPaths[i]);
  854. }
  855. break;
  856. }
  857. }
  858. }
  859. if (dragPaths == null)
  860. dragPaths = new[] { Path.Combine(resourceDir, underCursorElem.path) };
  861. ResourceDragDropData dragDropData = new ResourceDragDropData(dragPaths);
  862. DragDrop.StartDrag(dragDropData);
  863. }
  864. private void OnDragMove(Vector2I windowPos)
  865. {
  866. // Auto-scroll
  867. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  868. int scrollAreaTop = scrollAreaBounds.y;
  869. int scrollAreaBottom = scrollAreaBounds.y + scrollAreaBounds.height;
  870. if (windowPos.y > scrollAreaTop && windowPos.y <= (scrollAreaTop + DRAG_SCROLL_HEIGHT))
  871. autoScrollAmount = -DRAG_SCROLL_AMOUNT_PER_SECOND;
  872. else if (windowPos.y >= (scrollAreaBottom - DRAG_SCROLL_HEIGHT) && windowPos.y < scrollAreaBottom)
  873. autoScrollAmount = DRAG_SCROLL_AMOUNT_PER_SECOND;
  874. else
  875. autoScrollAmount = 0;
  876. // Selection box
  877. if (UpdateDragSelection(windowPos))
  878. return;
  879. // Drag and drop (hover element under cursor)
  880. LibraryGUIEntry underCursorElem = FindElementAt(windowPos);
  881. if (underCursorElem == null)
  882. {
  883. ClearHoverHighlight();
  884. }
  885. else
  886. {
  887. if (underCursorElem.path != hoverHighlightPath)
  888. {
  889. ClearHoverHighlight();
  890. hoverHighlightPath = underCursorElem.path;
  891. underCursorElem.MarkAsHovered(true);
  892. }
  893. }
  894. }
  895. private void OnDragLeave()
  896. {
  897. ClearHoverHighlight();
  898. autoScrollAmount = 0;
  899. }
  900. private void OnResourceDragDropped(Vector2I windowPos, string[] paths)
  901. {
  902. ClearHoverHighlight();
  903. autoScrollAmount = 0;
  904. if (EndDragSelection())
  905. return;
  906. string resourceDir = ProjectLibrary.ResourceFolder;
  907. string destinationFolder = Path.Combine(resourceDir, currentDirectory);
  908. LibraryGUIEntry underCursorElement = FindElementAt(windowPos);
  909. if (underCursorElement != null)
  910. {
  911. LibraryEntry entry = ProjectLibrary.GetEntry(underCursorElement.path);
  912. if (entry != null && entry.Type == LibraryEntryType.Directory)
  913. destinationFolder = Path.Combine(resourceDir, entry.Path);
  914. }
  915. if (paths != null)
  916. {
  917. foreach (var path in paths)
  918. {
  919. if (path == null)
  920. continue;
  921. string absolutePath = path;
  922. if (!Path.IsPathRooted(absolutePath))
  923. absolutePath = Path.Combine(resourceDir, path);
  924. if (string.IsNullOrEmpty(absolutePath))
  925. continue;
  926. if (PathEx.IsPartOf(destinationFolder, absolutePath) || PathEx.Compare(absolutePath, destinationFolder))
  927. continue;
  928. string pathTail = PathEx.GetTail(absolutePath);
  929. string destination = Path.Combine(destinationFolder, pathTail);
  930. bool doCopy = !ProjectLibrary.Exists(path);
  931. if (Directory.Exists(path))
  932. {
  933. if (doCopy)
  934. DirectoryEx.Copy(path, LibraryUtility.GetUniquePath(destination));
  935. else
  936. DirectoryEx.Move(path, LibraryUtility.GetUniquePath(destination));
  937. }
  938. else if (File.Exists(path))
  939. {
  940. if (doCopy)
  941. FileEx.Copy(path, LibraryUtility.GetUniquePath(destination));
  942. else
  943. FileEx.Move(path, LibraryUtility.GetUniquePath(destination));
  944. }
  945. ProjectLibrary.Refresh();
  946. }
  947. }
  948. }
  949. private void OnSceneObjectDragDropped(Vector2I windowPos, SceneObject[] objects)
  950. {
  951. ClearHoverHighlight();
  952. autoScrollAmount = 0;
  953. if (EndDragSelection())
  954. return;
  955. string destinationFolder = currentDirectory;
  956. LibraryGUIEntry underCursorElement = FindElementAt(windowPos);
  957. if (underCursorElement != null)
  958. {
  959. LibraryEntry entry = ProjectLibrary.GetEntry(underCursorElement.path);
  960. if (entry != null && entry.Type == LibraryEntryType.Directory)
  961. destinationFolder = entry.Path;
  962. }
  963. if (objects != null)
  964. {
  965. foreach (var so in objects)
  966. {
  967. if (so == null)
  968. continue;
  969. Prefab newPrefab = new Prefab(so);
  970. string destination = LibraryUtility.GetUniquePath(Path.Combine(destinationFolder, so.Name + ".prefab"));
  971. ProjectLibrary.Create(newPrefab, destination);
  972. ProjectLibrary.Refresh();
  973. }
  974. }
  975. }
  976. private void OnDragEnd(Vector2I windowPos)
  977. {
  978. EndDragSelection();
  979. autoScrollAmount = 0;
  980. }
  981. private void OnSelectionChanged(SceneObject[] sceneObjects, string[] resourcePaths)
  982. {
  983. if(sceneObjects.Length > 0)
  984. DeselectAll(true);
  985. }
  986. private void OnPing(string path)
  987. {
  988. Ping(path);
  989. }
  990. private void OnFolderButtonClicked(string path)
  991. {
  992. EnterDirectory(path);
  993. }
  994. private void OnContentsFocusChanged(bool focus)
  995. {
  996. hasContentFocus = focus;
  997. }
  998. private void OnCatchAllClicked()
  999. {
  1000. DeselectAll();
  1001. }
  1002. private void OnHomeClicked()
  1003. {
  1004. currentDirectory = ProjectLibrary.Root.Path;
  1005. Refresh();
  1006. }
  1007. private void OnUpClicked()
  1008. {
  1009. currentDirectory = currentDirectory.Trim(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  1010. if (!string.IsNullOrEmpty(currentDirectory))
  1011. {
  1012. string parent = Path.GetDirectoryName(currentDirectory);
  1013. currentDirectory = parent;
  1014. Refresh();
  1015. }
  1016. }
  1017. private void OnSearchChanged(string newValue)
  1018. {
  1019. searchQuery = newValue;
  1020. Refresh();
  1021. }
  1022. private static void SortEntries(LibraryEntry[] input)
  1023. {
  1024. Array.Sort(input, (x, y) =>
  1025. {
  1026. if (x.Type == y.Type)
  1027. return x.Name.CompareTo(y.Name);
  1028. else
  1029. return x.Type == LibraryEntryType.File ? 1 : -1;
  1030. });
  1031. }
  1032. }
  1033. }