LibraryWindow.cs 56 KB

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