LibraryWindow.cs 57 KB

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