LibraryWindow.cs 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  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. string path = source;
  586. if (!Path.IsPathRooted(path))
  587. path = Path.Combine(ProjectLibrary.ResourceFolder, path);
  588. if (Directory.Exists(path))
  589. DirectoryEx.Copy(path, LibraryUtility.GetUniquePath(path));
  590. else if (File.Exists(path))
  591. FileEx.Copy(path, LibraryUtility.GetUniquePath(path));
  592. ProjectLibrary.Refresh();
  593. }
  594. }
  595. /// <summary>
  596. /// Performs a cut or copy operations on the elements previously marked by calling <see cref="Cut"/> or
  597. /// <see cref="Copy"/>.
  598. /// </summary>
  599. /// <param name="destinationFolder">Project library folder into which to move/copy the elements.</param>
  600. internal void Paste(string destinationFolder)
  601. {
  602. string rootedDestinationFolder = destinationFolder;
  603. if (!Path.IsPathRooted(rootedDestinationFolder))
  604. rootedDestinationFolder = Path.Combine(ProjectLibrary.ResourceFolder, rootedDestinationFolder);
  605. if (copyPaths.Count > 0)
  606. {
  607. for (int i = 0; i < copyPaths.Count; i++)
  608. {
  609. string path = copyPaths[i];
  610. if (!Path.IsPathRooted(path))
  611. path = Path.Combine(ProjectLibrary.ResourceFolder, path);
  612. string destination = Path.Combine(rootedDestinationFolder, PathEx.GetTail(copyPaths[i]));
  613. if (Directory.Exists(path))
  614. DirectoryEx.Copy(path, LibraryUtility.GetUniquePath(destination));
  615. else if (File.Exists(path))
  616. FileEx.Copy(path, LibraryUtility.GetUniquePath(destination));
  617. }
  618. ProjectLibrary.Refresh();
  619. }
  620. else if (cutPaths.Count > 0)
  621. {
  622. for (int i = 0; i < cutPaths.Count; i++)
  623. {
  624. string path = cutPaths[i];
  625. if (!Path.IsPathRooted(path))
  626. path = Path.Combine(ProjectLibrary.ResourceFolder, path);
  627. string destination = Path.Combine(rootedDestinationFolder, PathEx.GetTail(cutPaths[i]));
  628. if (Directory.Exists(path))
  629. DirectoryEx.Move(path, LibraryUtility.GetUniquePath(destination));
  630. else if (File.Exists(path))
  631. FileEx.Move(path, LibraryUtility.GetUniquePath(destination));
  632. }
  633. cutPaths.Clear();
  634. ProjectLibrary.Refresh();
  635. }
  636. }
  637. /// <summary>
  638. /// Scrolls the contents GUI area so that the element at the specified path becomes visible.
  639. /// </summary>
  640. /// <param name="path">Project library path to the element.</param>
  641. private void ScrollToEntry(string path)
  642. {
  643. LibraryGUIEntry entryGUI;
  644. if (!content.TryGetEntry(path, out entryGUI))
  645. return;
  646. Rect2I entryBounds = entryGUI.Bounds;
  647. Rect2I contentBounds = contentScrollArea.Layout.Bounds;
  648. Rect2I windowEntryBounds = entryBounds;
  649. windowEntryBounds.x += contentBounds.x;
  650. windowEntryBounds.y += contentBounds.y;
  651. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  652. bool requiresScroll = windowEntryBounds.y < scrollAreaBounds.y ||
  653. (windowEntryBounds.y + windowEntryBounds.height) > (scrollAreaBounds.y + scrollAreaBounds.height);
  654. if (!requiresScroll)
  655. return;
  656. int scrollableSize = contentBounds.height - scrollAreaBounds.height;
  657. float percent = (((entryBounds.y + entryBounds.height * 0.5f) - scrollAreaBounds.height * 0.5f) / (float)scrollableSize);
  658. percent = MathEx.Clamp01(percent);
  659. contentScrollArea.VerticalScroll = percent;
  660. }
  661. /// <summary>
  662. /// Rebuilds the library window GUI. Should be called any time the active folder or contents change.
  663. /// </summary>
  664. private void Refresh()
  665. {
  666. requiresRefresh = false;
  667. LibraryEntry[] entriesToDisplay = new LibraryEntry[0];
  668. if (IsSearchActive)
  669. {
  670. entriesToDisplay = ProjectLibrary.Search("*" + searchQuery + "*");
  671. }
  672. else
  673. {
  674. DirectoryEntry entry = ProjectLibrary.GetEntry(CurrentFolder) as DirectoryEntry;
  675. if (entry == null)
  676. {
  677. CurrentFolder = ProjectLibrary.Root.Path;
  678. entry = ProjectLibrary.GetEntry(CurrentFolder) as DirectoryEntry;
  679. }
  680. if(entry != null)
  681. entriesToDisplay = entry.Children;
  682. }
  683. inProgressRenameElement = null;
  684. RefreshDirectoryBar();
  685. SortEntries(entriesToDisplay);
  686. content.Refresh(viewType, entriesToDisplay);
  687. foreach (var path in cutPaths)
  688. content.MarkAsCut(path, true);
  689. foreach (var path in selectionPaths)
  690. content.MarkAsSelected(path, true);
  691. content.MarkAsPinged(pingPath, true);
  692. Rect2I contentBounds = content.Bounds;
  693. Rect2I minimalBounds = GetScrollAreaBounds();
  694. contentBounds.height = Math.Max(contentBounds.height, minimalBounds.height);
  695. GUIButton catchAll = new GUIButton("", EditorStyles.Blank);
  696. catchAll.Bounds = contentBounds;
  697. catchAll.OnClick += OnCatchAllClicked;
  698. catchAll.SetContextMenu(entryContextMenu);
  699. content.Underlay.AddElement(catchAll);
  700. Rect2I focusBounds = contentBounds; // Contents + Folder bar
  701. Rect2I scrollBounds = contentScrollArea.Bounds;
  702. focusBounds.x += scrollBounds.x;
  703. focusBounds.y += scrollBounds.y;
  704. Rect2I folderBarBounds = folderListLayout.Bounds;
  705. focusBounds.y -= folderBarBounds.height;
  706. focusBounds.height += folderBarBounds.height;
  707. GUIButton focusCatcher = new GUIButton("", EditorStyles.Blank);
  708. focusCatcher.Blocking = false;
  709. focusCatcher.OnFocusGained += () => hasContentFocus = true;
  710. focusCatcher.OnFocusLost += () => hasContentFocus = false;
  711. focusCatcher.Bounds = focusBounds;
  712. GUIPanel focusPanel = GUI.AddPanel(-3);
  713. focusPanel.AddElement(focusCatcher);
  714. UpdateDragSelection(dragSelectionEnd);
  715. }
  716. /// <summary>
  717. /// Converts coordinates relative to the window into coordinates relative to the contents scroll area.
  718. /// </summary>
  719. /// <param name="windowPos">Coordinates relative to the window.</param>
  720. /// <returns>Coordinates relative to the contents scroll area.</returns>
  721. private Vector2I WindowToScrollAreaCoords(Vector2I windowPos)
  722. {
  723. Rect2I scrollBounds = contentScrollArea.Layout.Bounds;
  724. Vector2I scrollPos = windowPos;
  725. scrollPos.x -= scrollBounds.x;
  726. scrollPos.y -= scrollBounds.y;
  727. return scrollPos;
  728. }
  729. /// <summary>
  730. /// Starts a drag operation that displays a selection outline allowing the user to select multiple entries at once.
  731. /// </summary>
  732. /// <param name="windowPos">Coordinates relative to the window where the drag originated.</param>
  733. private void StartDragSelection(Vector2I windowPos)
  734. {
  735. isDraggingSelection = true;
  736. dragSelectionStart = WindowToScrollAreaCoords(windowPos);
  737. dragSelectionEnd = dragSelectionStart;
  738. }
  739. /// <summary>
  740. /// Updates a selection outline drag operation by expanding the outline to the new location. Elements in the outline
  741. /// are selected.
  742. /// </summary>
  743. /// <param name="windowPos">Coordinates of the pointer relative to the window.</param>
  744. /// <returns>True if the selection outline drag is valid and was updated, false otherwise.</returns>
  745. private bool UpdateDragSelection(Vector2I windowPos)
  746. {
  747. if (!isDraggingSelection)
  748. return false;
  749. if (dragSelection == null)
  750. {
  751. dragSelection = new GUITexture(null, true, EditorStyles.SelectionArea);
  752. content.Overlay.AddElement(dragSelection);
  753. }
  754. dragSelectionEnd = WindowToScrollAreaCoords(windowPos);
  755. Rect2I selectionArea = CalculateSelectionArea();
  756. SelectInArea(selectionArea);
  757. dragSelection.Bounds = selectionArea;
  758. return true;
  759. }
  760. /// <summary>
  761. /// Ends the selection outline drag operation. Elements in the outline are selected.
  762. /// </summary>
  763. /// <returns>True if the selection outline drag is valid and was ended, false otherwise.</returns>
  764. private bool EndDragSelection()
  765. {
  766. if (!isDraggingSelection)
  767. return false;
  768. if (dragSelection != null)
  769. {
  770. dragSelection.Destroy();
  771. dragSelection = null;
  772. }
  773. Rect2I selectionArea = CalculateSelectionArea();
  774. SelectInArea(selectionArea);
  775. isDraggingSelection = false;
  776. return false;
  777. }
  778. /// <summary>
  779. /// Calculates bounds of the selection area used for selection overlay drag operation, depending on drag starting
  780. /// point coordinates and current drag coordinates.
  781. /// </summary>
  782. /// <returns>Bounds of the selection area, relative to the content scroll area.</returns>
  783. private Rect2I CalculateSelectionArea()
  784. {
  785. Rect2I selectionArea = new Rect2I();
  786. if (dragSelectionStart.x < dragSelectionEnd.x)
  787. {
  788. selectionArea.x = dragSelectionStart.x;
  789. selectionArea.width = dragSelectionEnd.x - dragSelectionStart.x;
  790. }
  791. else
  792. {
  793. selectionArea.x = dragSelectionEnd.x;
  794. selectionArea.width = dragSelectionStart.x - dragSelectionEnd.x;
  795. }
  796. if (dragSelectionStart.y < dragSelectionEnd.y)
  797. {
  798. selectionArea.y = dragSelectionStart.y;
  799. selectionArea.height = dragSelectionEnd.y - dragSelectionStart.y;
  800. }
  801. else
  802. {
  803. selectionArea.y = dragSelectionEnd.y;
  804. selectionArea.height = dragSelectionStart.y - dragSelectionEnd.y;
  805. }
  806. Rect2I maxBounds = contentScrollArea.Layout.Bounds;
  807. maxBounds.x = 0;
  808. maxBounds.y = 0;
  809. selectionArea.Clip(maxBounds);
  810. return selectionArea;
  811. }
  812. /// <summary>
  813. /// Selects all elements overlapping the specified bounds.
  814. /// </summary>
  815. /// <param name="scrollBounds">Bounds relative to the content scroll area.</param>
  816. private void SelectInArea(Rect2I scrollBounds)
  817. {
  818. LibraryGUIEntry[] foundElements = content.FindElementsOverlapping(scrollBounds);
  819. if (foundElements.Length > 0)
  820. {
  821. selectionAnchorStart = foundElements[0].index;
  822. selectionAnchorEnd = foundElements[foundElements.Length - 1].index;
  823. }
  824. else
  825. {
  826. selectionAnchorStart = -1;
  827. selectionAnchorEnd = -1;
  828. }
  829. List<string> elementPaths = new List<string>();
  830. foreach (var elem in foundElements)
  831. elementPaths.Add(elem.path);
  832. SetSelection(elementPaths);
  833. }
  834. /// <summary>
  835. /// Updates GUI for the directory bar. Should be called whenever the active folder changes.
  836. /// </summary>
  837. private void RefreshDirectoryBar()
  838. {
  839. if (folderListLayout != null)
  840. {
  841. folderListLayout.Destroy();
  842. folderListLayout = null;
  843. }
  844. folderListLayout = folderBarLayout.AddLayoutX();
  845. string[] folders = null;
  846. string[] fullPaths = null;
  847. if (IsSearchActive)
  848. {
  849. folders = new[] {searchQuery};
  850. fullPaths = new[] { searchQuery };
  851. }
  852. else
  853. {
  854. string currentDir = Path.Combine("Resources", CurrentFolder);
  855. folders = currentDir.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar },
  856. StringSplitOptions.RemoveEmptyEntries);
  857. fullPaths = new string[folders.Length];
  858. for (int i = 0; i < folders.Length; i++)
  859. {
  860. if (i == 0)
  861. fullPaths[i] = "";
  862. else
  863. fullPaths[i] = Path.Combine(fullPaths[i - 1], folders[i]);
  864. }
  865. }
  866. int availableWidth = folderBarLayout.Bounds.width - FOLDER_BUTTON_WIDTH * 2;
  867. int numFolders = 0;
  868. for (int i = folders.Length - 1; i >= 0; i--)
  869. {
  870. GUIButton folderButton = new GUIButton(folders[i]);
  871. if (!IsSearchActive)
  872. {
  873. string fullPath = fullPaths[i];
  874. folderButton.OnClick += () => OnFolderButtonClicked(fullPath);
  875. }
  876. GUIButton separator = new GUIButton("/", GUIOption.FixedWidth(FOLDER_SEPARATOR_WIDTH));
  877. folderListLayout.InsertElement(0, separator);
  878. folderListLayout.InsertElement(0, folderButton);
  879. numFolders++;
  880. Rect2I folderListBounds = folderListLayout.Bounds;
  881. if (folderListBounds.width > availableWidth)
  882. {
  883. if (numFolders > 2)
  884. {
  885. separator.Destroy();
  886. folderButton.Destroy();
  887. break;
  888. }
  889. }
  890. }
  891. }
  892. /// <summary>
  893. /// Performs <see cref="Cut"/> operation on the currently selected elements.
  894. /// </summary>
  895. internal void CutSelection()
  896. {
  897. if (selectionPaths.Count > 0)
  898. Cut(selectionPaths);
  899. }
  900. /// <summary>
  901. /// Performs <see cref="Copy"/> operation on the currently selected elements.
  902. /// </summary>
  903. internal void CopySelection()
  904. {
  905. if (selectionPaths.Count > 0)
  906. Copy(selectionPaths);
  907. }
  908. /// <summary>
  909. /// Performs <see cref="Duplicate"/> operation on the currently selected elements.
  910. /// </summary>
  911. internal void DuplicateSelection()
  912. {
  913. if (selectionPaths.Count > 0)
  914. Duplicate(selectionPaths);
  915. }
  916. /// <summary>
  917. /// Performs <see cref="Paste"/> operation. Elements will be pasted in the currently selected directory (if any), or
  918. /// the active directory otherwise.
  919. /// </summary>
  920. internal void PasteToSelection()
  921. {
  922. Paste(SelectedFolder);
  923. }
  924. /// <summary>
  925. /// Starts a rename operation on the currently selected elements. If more than one elements are selected only the
  926. /// first one will be affected.
  927. /// </summary>
  928. internal void RenameSelection()
  929. {
  930. string[] filePaths = GetFiles(selectionPaths);
  931. if (filePaths.Length == 0)
  932. return;
  933. if (filePaths.Length > 1)
  934. {
  935. DeselectAll();
  936. Select(filePaths[0]);
  937. }
  938. LibraryGUIEntry entry;
  939. if (content.TryGetEntry(filePaths[0], out entry))
  940. {
  941. entry.StartRename();
  942. inProgressRenameElement = entry;
  943. }
  944. }
  945. /// <summary>
  946. /// Deletes currently selected elements. User will be asked to confirm deletion via a dialog box.
  947. /// </summary>
  948. internal void DeleteSelection()
  949. {
  950. string[] filePaths = GetFiles(selectionPaths);
  951. if (filePaths.Length == 0)
  952. return;
  953. DialogBox.Open(new LocEdString("Confirm deletion"), new LocEdString("Are you sure you want to delete the selected object(s)?"),
  954. DialogBox.Type.YesNo,
  955. type =>
  956. {
  957. if (type == DialogBox.ResultType.Yes)
  958. {
  959. foreach (var path in filePaths)
  960. ProjectLibrary.Delete(path);
  961. DeselectAll();
  962. Refresh();
  963. }
  964. });
  965. }
  966. /// <summary>
  967. /// Stops the rename operation, if one is in progress on any element.
  968. /// </summary>
  969. internal void StopRename()
  970. {
  971. if (inProgressRenameElement != null)
  972. {
  973. inProgressRenameElement.StopRename();
  974. inProgressRenameElement = null;
  975. }
  976. }
  977. /// <summary>
  978. /// Clears the search bar and refreshes the content area to display contents of the current directory.
  979. /// </summary>
  980. private void ClearSearch()
  981. {
  982. searchField.Value = "";
  983. searchQuery = "";
  984. Refresh();
  985. }
  986. /// <summary>
  987. /// Takes a list of resource paths and returns only those referencing files or folder and not sub-resources.
  988. /// </summary>
  989. /// <param name="resourcePaths">List of resource paths to find files for.</param>
  990. /// <returns>File paths for all the provided resources.</returns>
  991. private string[] GetFiles(IEnumerable<string> resourcePaths)
  992. {
  993. HashSet<string> filePaths = new HashSet<string>();
  994. foreach (var resPath in resourcePaths)
  995. {
  996. if (resPath == null)
  997. continue;
  998. LibraryEntry entry = ProjectLibrary.GetEntry(resPath);
  999. if (entry == null)
  1000. continue;
  1001. if (ProjectLibrary.IsSubresource(resPath))
  1002. continue;
  1003. if (!filePaths.Contains(entry.Path))
  1004. filePaths.Add(entry.Path);
  1005. }
  1006. string[] output = new string[filePaths.Count];
  1007. int i = 0;
  1008. foreach(var path in filePaths)
  1009. output[i++] = path;
  1010. return output;
  1011. }
  1012. /// <summary>
  1013. /// Opens the drop down options window that allows you to customize library window look and feel.
  1014. /// </summary>
  1015. private void OpenOptionsWindow()
  1016. {
  1017. Vector2I openPosition;
  1018. Rect2I buttonBounds = GUILayoutUtility.CalculateBounds(optionsButton, GUI);
  1019. openPosition.x = buttonBounds.x + buttonBounds.width / 2;
  1020. openPosition.y = buttonBounds.y + buttonBounds.height / 2;
  1021. LibraryDropDown dropDown = DropDownWindow.Open<LibraryDropDown>(this, openPosition);
  1022. dropDown.Initialize(this);
  1023. }
  1024. /// <summary>
  1025. /// Returns the content scroll area bounds.
  1026. /// </summary>
  1027. /// <returns>Bounds of the content scroll area, relative to the window.</returns>
  1028. private Rect2I GetScrollAreaBounds()
  1029. {
  1030. Rect2I bounds = GUI.Bounds;
  1031. Rect2I folderListBounds = folderListLayout.Bounds;
  1032. Rect2I searchBarBounds = searchBarLayout.Bounds;
  1033. bounds.y = folderListBounds.height + searchBarBounds.height;
  1034. bounds.height -= folderListBounds.height + searchBarBounds.height;
  1035. return bounds;
  1036. }
  1037. /// <summary>
  1038. /// Triggered when a project library entry was changed (added, modified, deleted).
  1039. /// </summary>
  1040. /// <param name="entry">Project library path of the changed entry.</param>
  1041. private void OnEntryChanged(string entry)
  1042. {
  1043. requiresRefresh = true;
  1044. }
  1045. /// <summary>
  1046. /// Triggered when the drag and drop operation is starting while over the content area. If drag operation is over
  1047. /// an element, element will be dragged.
  1048. /// </summary>
  1049. /// <param name="windowPos">Coordinates where the drag operation started, relative to the window.</param>
  1050. private void OnDragStart(Vector2I windowPos)
  1051. {
  1052. bool isRenameInProgress = inProgressRenameElement != null;
  1053. if (isRenameInProgress)
  1054. return;
  1055. LibraryGUIEntry underCursorElem = FindElementAt(windowPos);
  1056. if (underCursorElem == null)
  1057. {
  1058. StartDragSelection(windowPos);
  1059. return;
  1060. }
  1061. string resourceDir = ProjectLibrary.ResourceFolder;
  1062. string[] dragPaths = null;
  1063. if (selectionPaths.Count > 0)
  1064. {
  1065. foreach (var path in selectionPaths)
  1066. {
  1067. if (path == underCursorElem.path)
  1068. {
  1069. dragPaths = new string[selectionPaths.Count];
  1070. for (int i = 0; i < selectionPaths.Count; i++)
  1071. {
  1072. dragPaths[i] = Path.Combine(resourceDir, selectionPaths[i]);
  1073. }
  1074. break;
  1075. }
  1076. }
  1077. }
  1078. if (dragPaths == null)
  1079. dragPaths = new[] { Path.Combine(resourceDir, underCursorElem.path) };
  1080. ResourceDragDropData dragDropData = new ResourceDragDropData(dragPaths);
  1081. DragDrop.StartDrag(dragDropData);
  1082. }
  1083. /// <summary>
  1084. /// Triggered when a pointer is moved while a drag operation is in progress.
  1085. /// </summary>
  1086. /// <param name="windowPos">Coordinates of the pointer relative to the window.</param>
  1087. private void OnDragMove(Vector2I windowPos)
  1088. {
  1089. // Auto-scroll
  1090. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  1091. int scrollAreaTop = scrollAreaBounds.y;
  1092. int scrollAreaBottom = scrollAreaBounds.y + scrollAreaBounds.height;
  1093. if (windowPos.y > scrollAreaTop && windowPos.y <= (scrollAreaTop + DRAG_SCROLL_HEIGHT))
  1094. autoScrollAmount = -DRAG_SCROLL_AMOUNT_PER_SECOND;
  1095. else if (windowPos.y >= (scrollAreaBottom - DRAG_SCROLL_HEIGHT) && windowPos.y < scrollAreaBottom)
  1096. autoScrollAmount = DRAG_SCROLL_AMOUNT_PER_SECOND;
  1097. else
  1098. autoScrollAmount = 0;
  1099. // Selection box
  1100. if (UpdateDragSelection(windowPos))
  1101. return;
  1102. // Drag and drop (hover element under cursor)
  1103. LibraryGUIEntry underCursorElem = FindElementAt(windowPos);
  1104. if (underCursorElem == null)
  1105. {
  1106. ClearHoverHighlight();
  1107. }
  1108. else
  1109. {
  1110. if (underCursorElem.path != hoverHighlightPath)
  1111. {
  1112. ClearHoverHighlight();
  1113. hoverHighlightPath = underCursorElem.path;
  1114. underCursorElem.MarkAsHovered(true);
  1115. }
  1116. }
  1117. }
  1118. /// <summary>
  1119. /// Triggered when a pointer leaves the drop targer while a drag operation is in progress.
  1120. /// </summary>
  1121. private void OnDragLeave()
  1122. {
  1123. ClearHoverHighlight();
  1124. autoScrollAmount = 0;
  1125. }
  1126. /// <summary>
  1127. /// Triggered when a resource drop operation finishes over the content area.
  1128. /// </summary>
  1129. /// <param name="windowPos">Coordinates of the pointer relative to the window where the drop operation finished
  1130. /// .</param>
  1131. /// <param name="paths">Paths of the dropped resources.</param>
  1132. private void OnResourceDragDropped(Vector2I windowPos, string[] paths)
  1133. {
  1134. ClearHoverHighlight();
  1135. autoScrollAmount = 0;
  1136. if (EndDragSelection())
  1137. return;
  1138. string resourceDir = ProjectLibrary.ResourceFolder;
  1139. string destinationFolder = Path.Combine(resourceDir, CurrentFolder);
  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 = Path.Combine(resourceDir, entry.Path);
  1146. }
  1147. if (paths != null)
  1148. {
  1149. string[] filePaths = GetFiles(paths);
  1150. List<string> addedResources = new List<string>();
  1151. foreach (var path in filePaths)
  1152. {
  1153. string absolutePath = path;
  1154. if (!Path.IsPathRooted(absolutePath))
  1155. absolutePath = Path.Combine(resourceDir, path);
  1156. if (string.IsNullOrEmpty(absolutePath))
  1157. continue;
  1158. if (PathEx.IsPartOf(destinationFolder, absolutePath) || PathEx.Compare(absolutePath, destinationFolder))
  1159. continue;
  1160. string pathTail = PathEx.GetTail(absolutePath);
  1161. string destination = Path.Combine(destinationFolder, pathTail);
  1162. if (PathEx.Compare(absolutePath, destination))
  1163. continue;
  1164. bool doCopy = !ProjectLibrary.Exists(absolutePath);
  1165. string uniqueDestination = LibraryUtility.GetUniquePath(destination);
  1166. if (Directory.Exists(path))
  1167. {
  1168. if (doCopy)
  1169. DirectoryEx.Copy(absolutePath, uniqueDestination);
  1170. else
  1171. DirectoryEx.Move(absolutePath, uniqueDestination);
  1172. }
  1173. else if (File.Exists(path))
  1174. {
  1175. if (doCopy)
  1176. FileEx.Copy(absolutePath, uniqueDestination);
  1177. else
  1178. ProjectLibrary.Move(absolutePath, uniqueDestination);
  1179. }
  1180. string relativeDestination = uniqueDestination.Substring(resourceDir.Length, uniqueDestination.Length - resourceDir.Length);
  1181. addedResources.Add(relativeDestination);
  1182. ProjectLibrary.Refresh();
  1183. }
  1184. SetSelection(addedResources);
  1185. }
  1186. }
  1187. /// <summary>
  1188. /// Triggered when a scene object drop operation finishes over the content area.
  1189. /// </summary>
  1190. /// <param name="windowPos">Coordinates of the pointer relative to the window where the drop operation finished
  1191. /// .</param>
  1192. /// <param name="objects">Dropped scene objects.</param>
  1193. private void OnSceneObjectDragDropped(Vector2I windowPos, SceneObject[] objects)
  1194. {
  1195. ClearHoverHighlight();
  1196. autoScrollAmount = 0;
  1197. if (EndDragSelection())
  1198. return;
  1199. string destinationFolder = CurrentFolder;
  1200. LibraryGUIEntry underCursorElement = FindElementAt(windowPos);
  1201. if (underCursorElement != null)
  1202. {
  1203. LibraryEntry entry = ProjectLibrary.GetEntry(underCursorElement.path);
  1204. if (entry != null && entry.Type == LibraryEntryType.Directory)
  1205. destinationFolder = entry.Path;
  1206. }
  1207. if (objects != null)
  1208. {
  1209. List<string> addedResources = new List<string>();
  1210. foreach (var so in objects)
  1211. {
  1212. if (so == null)
  1213. continue;
  1214. Prefab newPrefab = new Prefab(so);
  1215. string destination = LibraryUtility.GetUniquePath(Path.Combine(destinationFolder, so.Name + ".prefab"));
  1216. addedResources.Add(destination);
  1217. ProjectLibrary.Create(newPrefab, destination);
  1218. ProjectLibrary.Refresh();
  1219. }
  1220. SetSelection(addedResources);
  1221. }
  1222. }
  1223. /// <summary>
  1224. /// Triggered when a drag operation that originated from this window ends.
  1225. /// </summary>
  1226. /// <param name="windowPos">Coordinates of the pointer where the drag ended relative to the window </param>
  1227. private void OnDragEnd(Vector2I windowPos)
  1228. {
  1229. EndDragSelection();
  1230. autoScrollAmount = 0;
  1231. }
  1232. /// <summary>
  1233. /// Triggered when the global selection changes.
  1234. /// </summary>
  1235. /// <param name="sceneObjects">A set of newly selected scene objects.</param>
  1236. /// <param name="resourcePaths">A set of paths for newly selected resources.</param>
  1237. private void OnSelectionChanged(SceneObject[] sceneObjects, string[] resourcePaths)
  1238. {
  1239. if (sceneObjects.Length > 0)
  1240. DeselectAll(true);
  1241. else
  1242. SetSelection(new List<string>(resourcePaths), true);
  1243. }
  1244. /// <summary>
  1245. /// Triggered when a ping operation was triggered externally.
  1246. /// </summary>
  1247. /// <param name="path">Path to the resource to highlight.</param>
  1248. private void OnPing(string path)
  1249. {
  1250. Ping(path);
  1251. }
  1252. /// <summary>
  1253. /// Triggered when a folder on the directory bar was selected.
  1254. /// </summary>
  1255. /// <param name="path">Project library path to the folder to enter.</param>
  1256. private void OnFolderButtonClicked(string path)
  1257. {
  1258. EnterDirectory(path);
  1259. }
  1260. /// <summary>
  1261. /// Triggered when the user clicks on empty space between elements.
  1262. /// </summary>
  1263. private void OnCatchAllClicked()
  1264. {
  1265. DeselectAll();
  1266. }
  1267. /// <summary>
  1268. /// Triggered when the user clicks on the home button on the directory bar, changing the active directory to
  1269. /// project library root.
  1270. /// </summary>
  1271. private void OnHomeClicked()
  1272. {
  1273. CurrentFolder = ProjectLibrary.Root.Path;
  1274. Refresh();
  1275. }
  1276. /// <summary>
  1277. /// Triggered when the user clicks on the up button on the directory bar, changing the active directory to the
  1278. /// parent directory, unless already at project library root.
  1279. /// </summary>
  1280. private void OnUpClicked()
  1281. {
  1282. string currentDir = CurrentFolder;
  1283. currentDir = currentDir.Trim(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  1284. if (!string.IsNullOrEmpty(currentDir))
  1285. {
  1286. string parent = Path.GetDirectoryName(currentDir);
  1287. CurrentFolder = parent;
  1288. Refresh();
  1289. }
  1290. }
  1291. /// <summary>
  1292. /// Triggered when the user inputs new values into the search input box. Refreshes the contents so they display
  1293. /// elements matching the search text.
  1294. /// </summary>
  1295. /// <param name="newValue">Search box text.</param>
  1296. private void OnSearchChanged(string newValue)
  1297. {
  1298. searchQuery = newValue;
  1299. Refresh();
  1300. }
  1301. /// <summary>
  1302. /// Sorts the specified set of project library entries by type (folder or resource), followed by name.
  1303. /// </summary>
  1304. /// <param name="input">Set of project library entries to sort.</param>
  1305. private static void SortEntries(LibraryEntry[] input)
  1306. {
  1307. Array.Sort(input, (x, y) =>
  1308. {
  1309. if (x.Type == y.Type)
  1310. return x.Name.CompareTo(y.Name);
  1311. else
  1312. return x.Type == LibraryEntryType.File ? 1 : -1;
  1313. });
  1314. }
  1315. }
  1316. }