LibraryWindow.cs 58 KB

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