LibraryWindow.cs 60 KB

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