LibraryWindow.cs 54 KB

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