LibraryWindow.cs 54 KB

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