LibraryWindow.cs 58 KB

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