LibraryWindow.cs 54 KB

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