LibraryWindow.cs 58 KB

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