LibraryWindow.cs 54 KB

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