LibraryWindow.cs 58 KB

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