LibraryWindow.cs 58 KB

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