LibraryWindow.cs 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// Types of resource tile display in the library window.
  9. /// </summary>
  10. internal enum ProjectViewType
  11. {
  12. Grid64, Grid48, Grid32, List16
  13. }
  14. /// <summary>
  15. /// Editor window that displays all resources in the project. Resources can be displayed as a grid or list of icons,
  16. /// with the ability to move, cut, copy, paste resources and folders, as well as supporting drag and drop and search
  17. /// operations.
  18. /// </summary>
  19. internal sealed class LibraryWindow : EditorWindow
  20. {
  21. /// <summary>
  22. /// Directions the selection cursor in library window can be moved in.
  23. /// </summary>
  24. internal enum MoveDirection
  25. {
  26. Up, Down, Left, Right
  27. }
  28. private const int DRAG_SCROLL_HEIGHT = 20;
  29. private const int DRAG_SCROLL_AMOUNT_PER_SECOND = 100;
  30. private const int FOLDER_BUTTON_WIDTH = 30;
  31. private const int FOLDER_SEPARATOR_WIDTH = 10;
  32. private bool hasContentFocus = false;
  33. private bool HasContentFocus { get { return HasFocus && hasContentFocus; } }
  34. private string searchQuery;
  35. private bool IsSearchActive { get { return !string.IsNullOrEmpty(searchQuery); } }
  36. private ProjectViewType viewType = ProjectViewType.Grid32;
  37. private bool requiresRefresh;
  38. private string currentDirectory = "";
  39. private List<string> selectionPaths = new List<string>();
  40. private int selectionAnchorStart = -1;
  41. private int selectionAnchorEnd = -1;
  42. private string pingPath = "";
  43. private string hoverHighlightPath = "";
  44. private LibraryGUIContent content;
  45. private GUIScrollArea contentScrollArea;
  46. private GUILayoutX searchBarLayout;
  47. private GUIButton optionsButton;
  48. private GUILayout folderBarLayout;
  49. private GUILayout folderListLayout;
  50. private GUITextField searchField;
  51. private GUITexture dragSelection;
  52. private ContextMenu entryContextMenu;
  53. private LibraryDropTarget dropTarget;
  54. private int autoScrollAmount;
  55. private bool isDraggingSelection;
  56. private Vector2I dragSelectionStart;
  57. private Vector2I dragSelectionEnd;
  58. private LibraryGUIEntry inProgressRenameElement;
  59. // Cut/Copy/Paste
  60. private List<string> copyPaths = new List<string>();
  61. private List<string> cutPaths = new List<string>();
  62. /// <summary>
  63. /// Determines how to display resource tiles in the library window.
  64. /// </summary>
  65. internal ProjectViewType ViewType
  66. {
  67. get { return viewType; }
  68. set { viewType = value; Refresh(); }
  69. }
  70. /// <summary>
  71. /// Returns a file or folder currently selected in the library window. If nothing is selected, returns the active
  72. /// folder. Returned path is relative to project library resources folder.
  73. /// </summary>
  74. public string SelectedEntry
  75. {
  76. get
  77. {
  78. if (selectionPaths.Count == 1)
  79. {
  80. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  81. if (entry != null)
  82. return entry.Path;
  83. }
  84. return currentDirectory;
  85. }
  86. }
  87. /// <summary>
  88. /// Returns a folder currently selected in the library window. If no folder is selected, returns the active
  89. /// folder. Returned path is relative to project library resources folder.
  90. /// </summary>
  91. public string SelectedFolder
  92. {
  93. get
  94. {
  95. DirectoryEntry selectedDirectory = null;
  96. if (selectionPaths.Count == 1)
  97. {
  98. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  99. if (entry != null && entry.Type == LibraryEntryType.Directory)
  100. selectedDirectory = (DirectoryEntry) entry;
  101. }
  102. if (selectedDirectory != null)
  103. return selectedDirectory.Path;
  104. return currentDirectory;
  105. }
  106. }
  107. /// <summary>
  108. /// Returns the path to the folder currently displayed in the library window. Returned path is relative to project
  109. /// library resources folder.
  110. /// </summary>
  111. public string CurrentFolder
  112. {
  113. get { return currentDirectory; }
  114. }
  115. /// <summary>
  116. /// Context menu that should open when user right clicks on the content area.
  117. /// </summary>
  118. internal ContextMenu ContextMenu
  119. {
  120. get { return entryContextMenu; }
  121. }
  122. /// <summary>
  123. /// Opens the library window if not already open.
  124. /// </summary>
  125. [MenuItem("Windows/Library", ButtonModifier.CtrlAlt, ButtonCode.L, 6000)]
  126. private static void OpenLibraryWindow()
  127. {
  128. OpenWindow<LibraryWindow>();
  129. }
  130. private void OnInitialize()
  131. {
  132. ProjectLibrary.OnEntryAdded += OnEntryChanged;
  133. ProjectLibrary.OnEntryRemoved += OnEntryChanged;
  134. GUILayoutY contentLayout = GUI.AddLayoutY();
  135. searchBarLayout = contentLayout.AddLayoutX();
  136. searchField = new GUITextField();
  137. searchField.OnChanged += OnSearchChanged;
  138. GUIContent clearIcon = new GUIContent(EditorBuiltin.GetLibraryWindowIcon(LibraryWindowIcon.Clear));
  139. GUIButton clearSearchBtn = new GUIButton(clearIcon);
  140. clearSearchBtn.OnClick += ClearSearch;
  141. clearSearchBtn.SetWidth(40);
  142. GUIContent optionsIcon = new GUIContent(EditorBuiltin.GetLibraryWindowIcon(LibraryWindowIcon.Options));
  143. optionsButton = new GUIButton(optionsIcon);
  144. optionsButton.OnClick += OpenOptionsWindow;
  145. optionsButton.SetWidth(40);
  146. searchBarLayout.AddElement(searchField);
  147. searchBarLayout.AddElement(clearSearchBtn);
  148. searchBarLayout.AddElement(optionsButton);
  149. folderBarLayout = contentLayout.AddLayoutX();
  150. GUIContent homeIcon = new GUIContent(EditorBuiltin.GetLibraryWindowIcon(LibraryWindowIcon.Home));
  151. GUIButton homeButton = new GUIButton(homeIcon, GUIOption.FixedWidth(FOLDER_BUTTON_WIDTH));
  152. homeButton.OnClick += OnHomeClicked;
  153. GUIContent upIcon = new GUIContent(EditorBuiltin.GetLibraryWindowIcon(LibraryWindowIcon.Up));
  154. GUIButton upButton = new GUIButton(upIcon, GUIOption.FixedWidth(FOLDER_BUTTON_WIDTH));
  155. upButton.OnClick += OnUpClicked;
  156. folderBarLayout.AddElement(homeButton);
  157. folderBarLayout.AddElement(upButton);
  158. folderBarLayout.AddSpace(10);
  159. contentScrollArea = new GUIScrollArea(GUIOption.FlexibleWidth(), GUIOption.FlexibleHeight());
  160. contentLayout.AddElement(contentScrollArea);
  161. contentLayout.AddFlexibleSpace();
  162. entryContextMenu = LibraryMenu.CreateContextMenu(this);
  163. content = new LibraryGUIContent(this, contentScrollArea);
  164. Reset();
  165. dropTarget = new LibraryDropTarget(this);
  166. dropTarget.Bounds = contentScrollArea.Bounds;
  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. newPath = Path.Combine(newPath, newName + Path.GetExtension(originalPath));
  260. bool renameOK = true;
  261. if (!PathEx.IsValidFileName(newName))
  262. {
  263. DialogBox.Open(new LocEdString("Error"), new LocEdString("The name you specified is not a valid file name. Try another."), DialogBox.Type.OK);
  264. renameOK = false;
  265. }
  266. if (renameOK)
  267. {
  268. // Windows sees paths with dot at the end as if they didn't have it
  269. // so remove the dot to ensure the project library does the same
  270. string trimmedNewPath = newPath.TrimEnd('.');
  271. if (originalPath != trimmedNewPath && ProjectLibrary.Exists(trimmedNewPath))
  272. {
  273. DialogBox.Open(new LocEdString("Error"), new LocEdString("File/folder with that name already exists in this folder."), DialogBox.Type.OK);
  274. renameOK = false;
  275. }
  276. }
  277. if (renameOK)
  278. {
  279. ProjectLibrary.Rename(originalPath, newPath);
  280. StopRename();
  281. }
  282. }
  283. else if (Input.IsButtonDown(ButtonCode.Escape))
  284. {
  285. StopRename();
  286. }
  287. }
  288. }
  289. else
  290. {
  291. if (isRenameInProgress)
  292. StopRename();
  293. }
  294. if (autoScrollAmount != 0)
  295. {
  296. Rect2I contentBounds = contentScrollArea.ContentBounds;
  297. float scrollPct = autoScrollAmount / (float)contentBounds.height;
  298. contentScrollArea.VerticalScroll += scrollPct * Time.FrameDelta;
  299. }
  300. if (requiresRefresh)
  301. Refresh();
  302. dropTarget.Update();
  303. }
  304. /// <inheritdoc/>
  305. protected override LocString GetDisplayName()
  306. {
  307. return new LocEdString("Library");
  308. }
  309. /// <inheritdoc/>
  310. protected override void WindowResized(int width, int height)
  311. {
  312. base.WindowResized(width, height);
  313. Refresh();
  314. dropTarget.Bounds = contentScrollArea.Bounds;
  315. }
  316. /// <summary>
  317. /// Attempts to find a resource tile element at the specified coordinates.
  318. /// </summary>
  319. /// <param name="windowPos">Coordinates relative to the window.</param>
  320. /// <returns>True if found an entry, false otherwise.</returns>
  321. private LibraryGUIEntry FindElementAt(Vector2I windowPos)
  322. {
  323. Vector2I scrollPos = WindowToScrollAreaCoords(windowPos);
  324. return content.FindElementAt(scrollPos);
  325. }
  326. /// <summary>
  327. /// Clears hover highlight from the currently hovered over element.
  328. /// </summary>
  329. private void ClearHoverHighlight()
  330. {
  331. content.MarkAsHovered(hoverHighlightPath, false);
  332. hoverHighlightPath = "";
  333. }
  334. /// <summary>
  335. /// Pings an element at the specified path, displaying and highlighting it in the window.
  336. /// </summary>
  337. /// <param name="path">Project library path to the element.</param>
  338. public void Ping(string path)
  339. {
  340. content.MarkAsPinged(pingPath, false);
  341. pingPath = path;
  342. content.MarkAsPinged(pingPath, true);
  343. }
  344. /// <summary>
  345. /// Resets the library window to initial state.
  346. /// </summary>
  347. public void Reset()
  348. {
  349. currentDirectory = ProjectLibrary.Root.Path;
  350. selectionAnchorStart = -1;
  351. selectionAnchorEnd = -1;
  352. selectionPaths.Clear();
  353. pingPath = "";
  354. hoverHighlightPath = "";
  355. Refresh();
  356. }
  357. /// <summary>
  358. /// Deselects all selected elements.
  359. /// </summary>
  360. /// <param name="onlyInternal">If true, do not update the global <see cref="Selection"/>, instead the operation
  361. /// will be contained to the library window internally.</param>
  362. internal void DeselectAll(bool onlyInternal = false)
  363. {
  364. SetSelection(new List<string>(), onlyInternal);
  365. selectionAnchorStart = -1;
  366. selectionAnchorEnd = -1;
  367. }
  368. /// <summary>
  369. /// Select an element at the specified path. If control or shift keys are pressed during this operations multiple
  370. /// elements can be selected.
  371. /// </summary>
  372. /// <param name="path">Project library path to the element.</param>
  373. internal void Select(string path)
  374. {
  375. LibraryGUIEntry entry;
  376. if (!content.TryGetEntry(path, out entry))
  377. return;
  378. bool ctrlDown = Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl);
  379. bool shiftDown = Input.IsButtonHeld(ButtonCode.LeftShift) || Input.IsButtonHeld(ButtonCode.RightShift);
  380. if (shiftDown)
  381. {
  382. if (selectionAnchorStart != -1 && selectionAnchorStart < content.Entries.Length)
  383. {
  384. int start = Math.Min(entry.index, selectionAnchorStart);
  385. int end = Math.Max(entry.index, selectionAnchorStart);
  386. List<string> newSelection = new List<string>();
  387. for(int i = start; i <= end; i++)
  388. newSelection.Add(content.Entries[i].path);
  389. SetSelection(newSelection);
  390. selectionAnchorEnd = entry.index;
  391. }
  392. else
  393. {
  394. SetSelection(new List<string>() {path});
  395. selectionAnchorStart = entry.index;
  396. selectionAnchorEnd = entry.index;
  397. }
  398. }
  399. else if (ctrlDown)
  400. {
  401. List<string> newSelection = new List<string>(selectionPaths);
  402. if (selectionPaths.Contains(path))
  403. {
  404. newSelection.Remove(path);
  405. if (newSelection.Count == 0)
  406. DeselectAll();
  407. else
  408. {
  409. if (selectionAnchorStart == entry.index)
  410. {
  411. LibraryGUIEntry newAnchorEntry;
  412. if (!content.TryGetEntry(newSelection[0], out newAnchorEntry))
  413. selectionAnchorStart = -1;
  414. else
  415. selectionAnchorStart = newAnchorEntry.index;
  416. }
  417. if (selectionAnchorEnd == entry.index)
  418. {
  419. LibraryGUIEntry newAnchorEntry;
  420. if (!content.TryGetEntry(newSelection[newSelection.Count - 1], out newAnchorEntry))
  421. selectionAnchorEnd = -1;
  422. else
  423. selectionAnchorEnd = newAnchorEntry.index;
  424. }
  425. SetSelection(newSelection);
  426. }
  427. }
  428. else
  429. {
  430. newSelection.Add(path);
  431. SetSelection(newSelection);
  432. selectionAnchorEnd = entry.index;
  433. }
  434. }
  435. else
  436. {
  437. SetSelection(new List<string>() {path});
  438. selectionAnchorStart = entry.index;
  439. selectionAnchorEnd = entry.index;
  440. }
  441. }
  442. /// <summary>
  443. /// Selects a new element in the specified direction from the currently selected element. If shift or control are
  444. /// held during this operation, the selected object will be added to existing selection. If no element is selected
  445. /// the first or last element will be selected depending on direction.
  446. /// </summary>
  447. /// <param name="dir">Direction to move from the currently selected element.</param>
  448. internal void MoveSelection(MoveDirection dir)
  449. {
  450. string newPath = "";
  451. if (selectionPaths.Count == 0 || selectionAnchorEnd == -1)
  452. {
  453. // Nothing is selected so we arbitrarily select first or last element
  454. if (content.Entries.Length > 0)
  455. {
  456. switch (dir)
  457. {
  458. case MoveDirection.Left:
  459. case MoveDirection.Up:
  460. newPath = content.Entries[content.Entries.Length - 1].path;
  461. break;
  462. case MoveDirection.Right:
  463. case MoveDirection.Down:
  464. newPath = content.Entries[0].path;
  465. break;
  466. }
  467. }
  468. }
  469. else
  470. {
  471. switch (dir)
  472. {
  473. case MoveDirection.Left:
  474. if (selectionAnchorEnd - 1 >= 0)
  475. newPath = content.Entries[selectionAnchorEnd - 1].path;
  476. break;
  477. case MoveDirection.Up:
  478. if (selectionAnchorEnd - content.ElementsPerRow >= 0)
  479. newPath = content.Entries[selectionAnchorEnd - content.ElementsPerRow].path;
  480. break;
  481. case MoveDirection.Right:
  482. if (selectionAnchorEnd + 1 < content.Entries.Length)
  483. newPath = content.Entries[selectionAnchorEnd + 1].path;
  484. break;
  485. case MoveDirection.Down:
  486. if (selectionAnchorEnd + content.ElementsPerRow < content.Entries.Length)
  487. newPath = content.Entries[selectionAnchorEnd + content.ElementsPerRow].path;
  488. break;
  489. }
  490. }
  491. if (!string.IsNullOrEmpty(newPath))
  492. {
  493. Select(newPath);
  494. ScrollToEntry(newPath);
  495. }
  496. }
  497. /// <summary>
  498. /// Selects a set of elements based on the provided paths.
  499. /// </summary>
  500. /// <param name="paths">Project library paths of the elements to select.</param>
  501. /// <param name="onlyInternal">If true, do not update the global <see cref="Selection"/>, instead the operation
  502. /// will be contained to the library window internally.</param>
  503. internal void SetSelection(List<string> paths, bool onlyInternal = false)
  504. {
  505. if (selectionPaths != null)
  506. {
  507. foreach (var path in selectionPaths)
  508. content.MarkAsSelected(path, false);
  509. }
  510. selectionPaths = paths;
  511. if (selectionPaths != null)
  512. {
  513. foreach (var path in selectionPaths)
  514. content.MarkAsSelected(path, true);
  515. }
  516. Ping("");
  517. StopRename();
  518. if (!onlyInternal)
  519. {
  520. if (selectionPaths != null)
  521. Selection.ResourcePaths = selectionPaths.ToArray();
  522. else
  523. Selection.ResourcePaths = new string[0];
  524. }
  525. }
  526. /// <summary>
  527. /// Changes the active directory to the provided directory. Current contents of the window will be cleared and
  528. /// instead contents of the new directory will be displayed.
  529. /// </summary>
  530. /// <param name="directory">Project library path to the directory.</param>
  531. internal void EnterDirectory(string directory)
  532. {
  533. currentDirectory = directory;
  534. DeselectAll();
  535. Refresh();
  536. }
  537. /// <summary>
  538. /// Marks the provided set of elements for a cut operation. Cut elements can be moved to a new location by calling
  539. /// <see cref="Paste"/>.
  540. /// </summary>
  541. /// <param name="sourcePaths">Project library paths of the elements to cut.</param>
  542. internal void Cut(IEnumerable<string> sourcePaths)
  543. {
  544. foreach (var path in cutPaths)
  545. content.MarkAsCut(path, false);
  546. cutPaths.Clear();
  547. cutPaths.AddRange(sourcePaths);
  548. foreach (var path in cutPaths)
  549. content.MarkAsCut(path, true);
  550. copyPaths.Clear();
  551. }
  552. /// <summary>
  553. /// Marks the provided set of elements for a copy operation. You can copy the elements by calling <see cref="Paste"/>.
  554. /// </summary>
  555. /// <param name="sourcePaths">Project library paths of the elements to copy.</param>
  556. internal void Copy(IEnumerable<string> sourcePaths)
  557. {
  558. copyPaths.Clear();
  559. copyPaths.AddRange(sourcePaths);
  560. foreach (var path in cutPaths)
  561. content.MarkAsCut(path, false);
  562. cutPaths.Clear();
  563. }
  564. /// <summary>
  565. /// Duplicates the provided set of elements.
  566. /// </summary>
  567. /// <param name="sourcePaths">Project library paths of the elements to duplicate.</param>
  568. internal void Duplicate(IEnumerable<string> sourcePaths)
  569. {
  570. foreach (var source in sourcePaths)
  571. {
  572. if (Directory.Exists(source))
  573. DirectoryEx.Copy(source, LibraryUtility.GetUniquePath(source));
  574. else if (File.Exists(source))
  575. FileEx.Copy(source, LibraryUtility.GetUniquePath(source));
  576. ProjectLibrary.Refresh();
  577. }
  578. }
  579. /// <summary>
  580. /// Performs a cut or copy operations on the elements previously marked by calling <see cref="Cut"/> or
  581. /// <see cref="Copy"/>.
  582. /// </summary>
  583. /// <param name="destinationFolder">Project library folder into which to move/copy the elements.</param>
  584. internal void Paste(string destinationFolder)
  585. {
  586. if (copyPaths.Count > 0)
  587. {
  588. for (int i = 0; i < copyPaths.Count; i++)
  589. {
  590. string destination = Path.Combine(destinationFolder, PathEx.GetTail(copyPaths[i]));
  591. if (Directory.Exists(copyPaths[i]))
  592. DirectoryEx.Copy(copyPaths[i], LibraryUtility.GetUniquePath(destination));
  593. else if (File.Exists(copyPaths[i]))
  594. FileEx.Copy(copyPaths[i], LibraryUtility.GetUniquePath(destination));
  595. }
  596. ProjectLibrary.Refresh();
  597. }
  598. else if (cutPaths.Count > 0)
  599. {
  600. for (int i = 0; i < cutPaths.Count; i++)
  601. {
  602. string destination = Path.Combine(destinationFolder, PathEx.GetTail(cutPaths[i]));
  603. if (Directory.Exists(cutPaths[i]))
  604. DirectoryEx.Move(cutPaths[i], LibraryUtility.GetUniquePath(destination));
  605. else if (File.Exists(cutPaths[i]))
  606. FileEx.Move(cutPaths[i], LibraryUtility.GetUniquePath(destination));
  607. }
  608. cutPaths.Clear();
  609. ProjectLibrary.Refresh();
  610. }
  611. }
  612. /// <summary>
  613. /// Scrolls the contents GUI area so that the element at the specified path becomes visible.
  614. /// </summary>
  615. /// <param name="path">Project library path to the element.</param>
  616. private void ScrollToEntry(string path)
  617. {
  618. LibraryGUIEntry entryGUI;
  619. if (!content.TryGetEntry(path, out entryGUI))
  620. return;
  621. Rect2I entryBounds = entryGUI.Bounds;
  622. Rect2I contentBounds = contentScrollArea.Layout.Bounds;
  623. Rect2I windowEntryBounds = entryBounds;
  624. windowEntryBounds.x += contentBounds.x;
  625. windowEntryBounds.y += contentBounds.y;
  626. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  627. bool requiresScroll = windowEntryBounds.y < scrollAreaBounds.y ||
  628. (windowEntryBounds.y + windowEntryBounds.height) > (scrollAreaBounds.y + scrollAreaBounds.height);
  629. if (!requiresScroll)
  630. return;
  631. int scrollableSize = contentBounds.height - scrollAreaBounds.height;
  632. float percent = (((entryBounds.y + entryBounds.height * 0.5f) - scrollAreaBounds.height * 0.5f) / (float)scrollableSize);
  633. percent = MathEx.Clamp01(percent);
  634. contentScrollArea.VerticalScroll = percent;
  635. }
  636. /// <summary>
  637. /// Rebuilds the library window GUI. Should be called any time the active folder or contents change.
  638. /// </summary>
  639. private void Refresh()
  640. {
  641. requiresRefresh = false;
  642. LibraryEntry[] entriesToDisplay = new LibraryEntry[0];
  643. if (IsSearchActive)
  644. {
  645. entriesToDisplay = ProjectLibrary.Search("*" + searchQuery + "*");
  646. }
  647. else
  648. {
  649. DirectoryEntry entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  650. if (entry == null)
  651. {
  652. currentDirectory = ProjectLibrary.Root.Path;
  653. entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  654. }
  655. if(entry != null)
  656. entriesToDisplay = entry.Children;
  657. }
  658. inProgressRenameElement = null;
  659. RefreshDirectoryBar();
  660. SortEntries(entriesToDisplay);
  661. content.Refresh(viewType, entriesToDisplay);
  662. if (entriesToDisplay.Length == 0)
  663. return;
  664. foreach (var path in cutPaths)
  665. content.MarkAsCut(path, true);
  666. foreach (var path in selectionPaths)
  667. content.MarkAsSelected(path, true);
  668. content.MarkAsPinged(pingPath, true);
  669. Rect2I contentBounds = content.Bounds;
  670. Rect2I minimalBounds = GetScrollAreaBounds();
  671. contentBounds.height = Math.Max(contentBounds.height, minimalBounds.height);
  672. GUIButton catchAll = new GUIButton("", EditorStyles.Blank);
  673. catchAll.Bounds = contentBounds;
  674. catchAll.OnClick += OnCatchAllClicked;
  675. catchAll.SetContextMenu(entryContextMenu);
  676. content.Underlay.AddElement(catchAll);
  677. Rect2I focusBounds = contentBounds; // Contents + Folder bar
  678. Rect2I scrollBounds = contentScrollArea.Bounds;
  679. focusBounds.x += scrollBounds.x;
  680. focusBounds.y += scrollBounds.y;
  681. Rect2I folderBarBounds = folderListLayout.Bounds;
  682. focusBounds.y -= folderBarBounds.height;
  683. focusBounds.height += folderBarBounds.height;
  684. GUIButton focusCatcher = new GUIButton("", EditorStyles.Blank);
  685. focusCatcher.OnFocusChanged += OnContentsFocusChanged;
  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. LibraryGUIEntry underCursorElem = FindElementAt(windowPos);
  1002. if (underCursorElem == null)
  1003. {
  1004. StartDragSelection(windowPos);
  1005. return;
  1006. }
  1007. string resourceDir = ProjectLibrary.ResourceFolder;
  1008. string[] dragPaths = null;
  1009. if (selectionPaths.Count > 0)
  1010. {
  1011. foreach (var path in selectionPaths)
  1012. {
  1013. if (path == underCursorElem.path)
  1014. {
  1015. dragPaths = new string[selectionPaths.Count];
  1016. for (int i = 0; i < selectionPaths.Count; i++)
  1017. {
  1018. dragPaths[i] = Path.Combine(resourceDir, selectionPaths[i]);
  1019. }
  1020. break;
  1021. }
  1022. }
  1023. }
  1024. if (dragPaths == null)
  1025. dragPaths = new[] { Path.Combine(resourceDir, underCursorElem.path) };
  1026. ResourceDragDropData dragDropData = new ResourceDragDropData(dragPaths);
  1027. DragDrop.StartDrag(dragDropData);
  1028. }
  1029. /// <summary>
  1030. /// Triggered when a pointer is moved while a drag operation is in progress.
  1031. /// </summary>
  1032. /// <param name="windowPos">Coordinates of the pointer relative to the window.</param>
  1033. private void OnDragMove(Vector2I windowPos)
  1034. {
  1035. // Auto-scroll
  1036. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  1037. int scrollAreaTop = scrollAreaBounds.y;
  1038. int scrollAreaBottom = scrollAreaBounds.y + scrollAreaBounds.height;
  1039. if (windowPos.y > scrollAreaTop && windowPos.y <= (scrollAreaTop + DRAG_SCROLL_HEIGHT))
  1040. autoScrollAmount = -DRAG_SCROLL_AMOUNT_PER_SECOND;
  1041. else if (windowPos.y >= (scrollAreaBottom - DRAG_SCROLL_HEIGHT) && windowPos.y < scrollAreaBottom)
  1042. autoScrollAmount = DRAG_SCROLL_AMOUNT_PER_SECOND;
  1043. else
  1044. autoScrollAmount = 0;
  1045. // Selection box
  1046. if (UpdateDragSelection(windowPos))
  1047. return;
  1048. // Drag and drop (hover element under cursor)
  1049. LibraryGUIEntry underCursorElem = FindElementAt(windowPos);
  1050. if (underCursorElem == null)
  1051. {
  1052. ClearHoverHighlight();
  1053. }
  1054. else
  1055. {
  1056. if (underCursorElem.path != hoverHighlightPath)
  1057. {
  1058. ClearHoverHighlight();
  1059. hoverHighlightPath = underCursorElem.path;
  1060. underCursorElem.MarkAsHovered(true);
  1061. }
  1062. }
  1063. }
  1064. /// <summary>
  1065. /// Triggered when a pointer leaves the drop targer while a drag operation is in progress.
  1066. /// </summary>
  1067. private void OnDragLeave()
  1068. {
  1069. ClearHoverHighlight();
  1070. autoScrollAmount = 0;
  1071. }
  1072. /// <summary>
  1073. /// Triggered when a resource drop operation finishes over the content area.
  1074. /// </summary>
  1075. /// <param name="windowPos">Coordinates of the pointer relative to the window where the drop operation finished
  1076. /// .</param>
  1077. /// <param name="paths">Paths of the dropped resources.</param>
  1078. private void OnResourceDragDropped(Vector2I windowPos, string[] paths)
  1079. {
  1080. ClearHoverHighlight();
  1081. autoScrollAmount = 0;
  1082. if (EndDragSelection())
  1083. return;
  1084. string resourceDir = ProjectLibrary.ResourceFolder;
  1085. string destinationFolder = Path.Combine(resourceDir, currentDirectory);
  1086. LibraryGUIEntry underCursorElement = FindElementAt(windowPos);
  1087. if (underCursorElement != null)
  1088. {
  1089. LibraryEntry entry = ProjectLibrary.GetEntry(underCursorElement.path);
  1090. if (entry != null && entry.Type == LibraryEntryType.Directory)
  1091. destinationFolder = Path.Combine(resourceDir, entry.Path);
  1092. }
  1093. if (paths != null)
  1094. {
  1095. foreach (var path in paths)
  1096. {
  1097. if (path == null)
  1098. continue;
  1099. string absolutePath = path;
  1100. if (!Path.IsPathRooted(absolutePath))
  1101. absolutePath = Path.Combine(resourceDir, path);
  1102. if (string.IsNullOrEmpty(absolutePath))
  1103. continue;
  1104. if (PathEx.IsPartOf(destinationFolder, absolutePath) || PathEx.Compare(absolutePath, destinationFolder))
  1105. continue;
  1106. string pathTail = PathEx.GetTail(absolutePath);
  1107. string destination = Path.Combine(destinationFolder, pathTail);
  1108. bool doCopy = !ProjectLibrary.Exists(path);
  1109. if (Directory.Exists(path))
  1110. {
  1111. if (doCopy)
  1112. DirectoryEx.Copy(path, LibraryUtility.GetUniquePath(destination));
  1113. else
  1114. DirectoryEx.Move(path, LibraryUtility.GetUniquePath(destination));
  1115. }
  1116. else if (File.Exists(path))
  1117. {
  1118. if (doCopy)
  1119. FileEx.Copy(path, LibraryUtility.GetUniquePath(destination));
  1120. else
  1121. FileEx.Move(path, LibraryUtility.GetUniquePath(destination));
  1122. }
  1123. ProjectLibrary.Refresh();
  1124. }
  1125. }
  1126. }
  1127. /// <summary>
  1128. /// Triggered when a scene object drop operation finishes over the content area.
  1129. /// </summary>
  1130. /// <param name="windowPos">Coordinates of the pointer relative to the window where the drop operation finished
  1131. /// .</param>
  1132. /// <param name="objects">Dropped scene objects.</param>
  1133. private void OnSceneObjectDragDropped(Vector2I windowPos, SceneObject[] objects)
  1134. {
  1135. ClearHoverHighlight();
  1136. autoScrollAmount = 0;
  1137. if (EndDragSelection())
  1138. return;
  1139. string destinationFolder = currentDirectory;
  1140. LibraryGUIEntry underCursorElement = FindElementAt(windowPos);
  1141. if (underCursorElement != null)
  1142. {
  1143. LibraryEntry entry = ProjectLibrary.GetEntry(underCursorElement.path);
  1144. if (entry != null && entry.Type == LibraryEntryType.Directory)
  1145. destinationFolder = entry.Path;
  1146. }
  1147. if (objects != null)
  1148. {
  1149. foreach (var so in objects)
  1150. {
  1151. if (so == null)
  1152. continue;
  1153. Prefab newPrefab = new Prefab(so);
  1154. string destination = LibraryUtility.GetUniquePath(Path.Combine(destinationFolder, so.Name + ".prefab"));
  1155. ProjectLibrary.Create(newPrefab, destination);
  1156. ProjectLibrary.Refresh();
  1157. }
  1158. }
  1159. }
  1160. /// <summary>
  1161. /// Triggered when a drag operation that originated from this window ends.
  1162. /// </summary>
  1163. /// <param name="windowPos">Coordinates of the pointer where the drag ended relative to the window </param>
  1164. private void OnDragEnd(Vector2I windowPos)
  1165. {
  1166. EndDragSelection();
  1167. autoScrollAmount = 0;
  1168. }
  1169. /// <summary>
  1170. /// Triggered when the global selection changes.
  1171. /// </summary>
  1172. /// <param name="sceneObjects">A set of newly selected scene objects.</param>
  1173. /// <param name="resourcePaths">A set of paths for newly selected resources.</param>
  1174. private void OnSelectionChanged(SceneObject[] sceneObjects, string[] resourcePaths)
  1175. {
  1176. if(sceneObjects.Length > 0)
  1177. DeselectAll(true);
  1178. }
  1179. /// <summary>
  1180. /// Triggered when a ping operation was triggered externally.
  1181. /// </summary>
  1182. /// <param name="path">Path to the resource to highlight.</param>
  1183. private void OnPing(string path)
  1184. {
  1185. Ping(path);
  1186. }
  1187. /// <summary>
  1188. /// Triggered when a folder on the directory bar was selected.
  1189. /// </summary>
  1190. /// <param name="path">Project library path to the folder to enter.</param>
  1191. private void OnFolderButtonClicked(string path)
  1192. {
  1193. EnterDirectory(path);
  1194. }
  1195. /// <summary>
  1196. /// Triggered when the content area receives or loses keyboard focus.
  1197. /// </summary>
  1198. /// <param name="focus">True if focus was received, false otherwise.</param>
  1199. private void OnContentsFocusChanged(bool focus)
  1200. {
  1201. hasContentFocus = focus;
  1202. }
  1203. /// <summary>
  1204. /// Triggered when the user clicks on empty space between elements.
  1205. /// </summary>
  1206. private void OnCatchAllClicked()
  1207. {
  1208. DeselectAll();
  1209. }
  1210. /// <summary>
  1211. /// Triggered when the user clicks on the home button on the directory bar, changing the active directory to
  1212. /// project library root.
  1213. /// </summary>
  1214. private void OnHomeClicked()
  1215. {
  1216. currentDirectory = ProjectLibrary.Root.Path;
  1217. Refresh();
  1218. }
  1219. /// <summary>
  1220. /// Triggered when the user clicks on the up button on the directory bar, changing the active directory to the
  1221. /// parent directory, unless already at project library root.
  1222. /// </summary>
  1223. private void OnUpClicked()
  1224. {
  1225. currentDirectory = currentDirectory.Trim(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  1226. if (!string.IsNullOrEmpty(currentDirectory))
  1227. {
  1228. string parent = Path.GetDirectoryName(currentDirectory);
  1229. currentDirectory = parent;
  1230. Refresh();
  1231. }
  1232. }
  1233. /// <summary>
  1234. /// Triggered when the user inputs new values into the search input box. Refreshes the contents so they display
  1235. /// elements matching the search text.
  1236. /// </summary>
  1237. /// <param name="newValue">Search box text.</param>
  1238. private void OnSearchChanged(string newValue)
  1239. {
  1240. searchQuery = newValue;
  1241. Refresh();
  1242. }
  1243. /// <summary>
  1244. /// Sorts the specified set of project library entries by type (folder or resource), followed by name.
  1245. /// </summary>
  1246. /// <param name="input">Set of project library entries to sort.</param>
  1247. private static void SortEntries(LibraryEntry[] input)
  1248. {
  1249. Array.Sort(input, (x, y) =>
  1250. {
  1251. if (x.Type == y.Type)
  1252. return x.Name.CompareTo(y.Name);
  1253. else
  1254. return x.Type == LibraryEntryType.File ? 1 : -1;
  1255. });
  1256. }
  1257. }
  1258. }