LibraryWindow.cs 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. internal enum ProjectViewType
  8. {
  9. Grid64, Grid48, Grid32, List16
  10. }
  11. internal sealed class LibraryWindow : EditorWindow
  12. {
  13. private const int GRID_ENTRY_SPACING = 15;
  14. private const int LIST_ENTRY_SPACING = 7;
  15. private const int MAX_LABEL_HEIGHT = 50;
  16. private const int MIN_HORZ_SPACING = 8;
  17. private const int DRAG_SCROLL_HEIGHT = 20;
  18. private const int DRAG_SCROLL_AMOUNT_PER_SECOND = 100;
  19. private const int FOLDER_BUTTON_WIDTH = 20;
  20. private const int FOLDER_SEPARATOR_WIDTH = 7;
  21. private const int SELECTION_EXTRA_WIDTH = 3;
  22. private static readonly Color PING_COLOR = Color.BansheeOrange;
  23. private static readonly Color SELECTION_COLOR = Color.DarkCyan;
  24. private static readonly Color HOVER_COLOR = new Color(Color.DarkCyan.r, Color.DarkCyan.g, Color.DarkCyan.b, 0.5f);
  25. private static readonly Color CUT_COLOR = new Color(1.0f, 1.0f, 1.0f, 0.5f);
  26. private bool hasContentFocus = false;
  27. private bool HasContentFocus { get { return HasFocus && hasContentFocus; } }
  28. private string searchQuery;
  29. private bool IsSearchActive { get { return !string.IsNullOrEmpty(searchQuery); } }
  30. private ProjectViewType viewType = ProjectViewType.Grid32;
  31. private bool requiresRefresh;
  32. private string currentDirectory = "";
  33. private List<string> selectionPaths = new List<string>();
  34. private int selectionAnchorStart = -1;
  35. private int selectionAnchorEnd = -1;
  36. private string pingPath = "";
  37. private string hoverHighlightPath = "";
  38. private ContentInfo contentInfo;
  39. private GUIScrollArea contentScrollArea;
  40. private GUIPanel scrollAreaPanel;
  41. private GUILayoutX searchBarLayout;
  42. private GUIButton optionsButton;
  43. private GUILayout folderBarLayout;
  44. private GUILayout folderListLayout;
  45. private GUITextField searchField;
  46. private GUITexture dragSelection;
  47. private ContextMenu entryContextMenu;
  48. private LibraryDropTarget dropTarget;
  49. private List<ElementEntry> entries = new List<ElementEntry>();
  50. private Dictionary<string, ElementEntry> entryLookup = new Dictionary<string, ElementEntry>();
  51. private int autoScrollAmount;
  52. private bool isDraggingSelection;
  53. private Vector2I dragSelectionStart;
  54. private Vector2I dragSelectionEnd;
  55. private ElementEntry inProgressRenameElement;
  56. // Cut/Copy/Paste
  57. private List<string> copyPaths = new List<string>();
  58. private List<string> cutPaths = new List<string>();
  59. internal ProjectViewType ViewType
  60. {
  61. get { return viewType; }
  62. set { viewType = value; Refresh(); }
  63. }
  64. [MenuItem("Windows/Library", ButtonModifier.CtrlAlt, ButtonCode.L)]
  65. private static void OpenLibraryWindow()
  66. {
  67. OpenWindow<LibraryWindow>();
  68. }
  69. protected override LocString GetDisplayName()
  70. {
  71. return new LocEdString("Library");
  72. }
  73. private void OnInitialize()
  74. {
  75. ProjectLibrary.OnEntryAdded += OnEntryChanged;
  76. ProjectLibrary.OnEntryRemoved += OnEntryChanged;
  77. GUILayoutY contentLayout = GUI.AddLayoutY();
  78. searchBarLayout = contentLayout.AddLayoutX();
  79. searchField = new GUITextField();
  80. searchField.OnChanged += OnSearchChanged;
  81. GUIButton clearSearchBtn = new GUIButton("C");
  82. clearSearchBtn.OnClick += ClearSearch;
  83. clearSearchBtn.SetWidth(40);
  84. optionsButton = new GUIButton("O");
  85. optionsButton.OnClick += OpenOptionsWindow;
  86. optionsButton.SetWidth(40);
  87. searchBarLayout.AddElement(searchField);
  88. searchBarLayout.AddElement(clearSearchBtn);
  89. searchBarLayout.AddElement(optionsButton);
  90. folderBarLayout = contentLayout.AddLayoutX();
  91. GUIButton homeButton = new GUIButton("H", GUIOption.FixedWidth(FOLDER_BUTTON_WIDTH));
  92. homeButton.OnClick += OnHomeClicked;
  93. GUIButton upButton = new GUIButton("U", GUIOption.FixedWidth(FOLDER_BUTTON_WIDTH));
  94. upButton.OnClick += OnUpClicked;
  95. folderBarLayout.AddElement(homeButton);
  96. folderBarLayout.AddElement(upButton);
  97. folderBarLayout.AddSpace(10);
  98. contentScrollArea = new GUIScrollArea(GUIOption.FlexibleWidth(), GUIOption.FlexibleHeight());
  99. contentLayout.AddElement(contentScrollArea);
  100. contentLayout.AddFlexibleSpace();
  101. entryContextMenu = new ContextMenu();
  102. entryContextMenu.AddItem("Rename", RenameSelection, new ShortcutKey(ButtonModifier.None, ButtonCode.F2));
  103. entryContextMenu.AddSeparator("");
  104. entryContextMenu.AddItem("Cut", CutSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.X));
  105. entryContextMenu.AddItem("Copy", CopySelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.C));
  106. entryContextMenu.AddItem("Duplicate", DuplicateSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.D));
  107. entryContextMenu.AddItem("Paste", PasteToSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.V));
  108. entryContextMenu.AddSeparator("");
  109. entryContextMenu.AddItem("Delete", DeleteSelection, new ShortcutKey(ButtonModifier.None, ButtonCode.Delete));
  110. entryContextMenu.SetLocalizedName("Rename", new LocEdString("Rename"));
  111. entryContextMenu.SetLocalizedName("Cut", new LocEdString("Cut"));
  112. entryContextMenu.SetLocalizedName("Copy", new LocEdString("Copy"));
  113. entryContextMenu.SetLocalizedName("Duplicate", new LocEdString("Duplicate"));
  114. entryContextMenu.SetLocalizedName("Paste", new LocEdString("Paste"));
  115. entryContextMenu.SetLocalizedName("Delete", new LocEdString("Delete"));
  116. Reset();
  117. dropTarget = new LibraryDropTarget(this);
  118. dropTarget.Bounds = contentScrollArea.Bounds;
  119. dropTarget.OnStart += DoOnDragStart;
  120. dropTarget.OnDrag += DoOnDragMove;
  121. dropTarget.OnLeave += DoOnDragLeave;
  122. dropTarget.OnDropResource += DoOnResourceDragDropped;
  123. dropTarget.OnDropSceneObject += DoOnSceneObjectDragDropped;
  124. dropTarget.OnEnd += DoOnDragEnd;
  125. Selection.OnSelectionChanged += OnSelectionChanged;
  126. Selection.OnResourcePing += OnPing;
  127. }
  128. private void OnDestroy()
  129. {
  130. Selection.OnSelectionChanged -= OnSelectionChanged;
  131. Selection.OnResourcePing -= OnPing;
  132. }
  133. private ElementEntry FindElementAt(Vector2I windowPos)
  134. {
  135. Vector2I scrollPos = WindowToScrollAreaCoords(windowPos);
  136. foreach (var element in entries)
  137. {
  138. if (element.bounds.Contains(scrollPos))
  139. return element;
  140. }
  141. return null;
  142. }
  143. private ElementEntry[] FindElementsOverlapping(Rect2I scrollBounds)
  144. {
  145. List<ElementEntry> elements = new List<ElementEntry>();
  146. foreach (var element in entries)
  147. {
  148. if(element.Bounds.Overlaps(scrollBounds))
  149. elements.Add(element);
  150. }
  151. return elements.ToArray();
  152. }
  153. private void DoOnDragStart(Vector2I windowPos)
  154. {
  155. Debug.Log("DRAG STARTED + " + windowPos + " - " + GUI.Bounds);
  156. ElementEntry underCursorElem = FindElementAt(windowPos);
  157. if (underCursorElem == null)
  158. {
  159. StartDragSelection(windowPos);
  160. return;
  161. }
  162. string resourceDir = ProjectLibrary.ResourceFolder;
  163. string[] dragPaths = null;
  164. if (selectionPaths.Count > 0)
  165. {
  166. foreach(var path in selectionPaths)
  167. {
  168. if (path == underCursorElem.path)
  169. {
  170. dragPaths = new string[selectionPaths.Count];
  171. for (int i = 0; i < selectionPaths.Count; i++)
  172. {
  173. dragPaths[i] = Path.Combine(resourceDir, selectionPaths[i]);
  174. }
  175. break;
  176. }
  177. }
  178. }
  179. if (dragPaths == null)
  180. dragPaths = new[] { Path.Combine(resourceDir, underCursorElem.path) };
  181. ResourceDragDropData dragDropData = new ResourceDragDropData(dragPaths);
  182. DragDrop.StartDrag(dragDropData);
  183. }
  184. private void DoOnDragMove(Vector2I windowPos)
  185. {
  186. // Auto-scroll
  187. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  188. int scrollAreaTop = scrollAreaBounds.y;
  189. int scrollAreaBottom = scrollAreaBounds.y + scrollAreaBounds.height;
  190. if (windowPos.y > scrollAreaTop && windowPos.y <= (scrollAreaTop + DRAG_SCROLL_HEIGHT))
  191. autoScrollAmount = -DRAG_SCROLL_AMOUNT_PER_SECOND;
  192. else if (windowPos.y >= (scrollAreaBottom - DRAG_SCROLL_HEIGHT) && windowPos.y < scrollAreaBottom)
  193. autoScrollAmount = DRAG_SCROLL_AMOUNT_PER_SECOND;
  194. else
  195. autoScrollAmount = 0;
  196. // Selection box
  197. if (UpdateDragSelection(windowPos))
  198. return;
  199. // Drag and drop (hover element under cursor)
  200. ElementEntry underCursorElem = FindElementAt(windowPos);
  201. if (underCursorElem == null)
  202. {
  203. ClearHoverHighlight();
  204. }
  205. else
  206. {
  207. if (underCursorElem.path != hoverHighlightPath)
  208. {
  209. ClearHoverHighlight();
  210. hoverHighlightPath = underCursorElem.path;
  211. underCursorElem.MarkAsHovered(true);
  212. }
  213. }
  214. }
  215. private void DoOnDragLeave()
  216. {
  217. ClearHoverHighlight();
  218. autoScrollAmount = 0;
  219. }
  220. private void DoOnResourceDragDropped(Vector2I windowPos, string[] paths)
  221. {
  222. ClearHoverHighlight();
  223. autoScrollAmount = 0;
  224. if (EndDragSelection())
  225. return;
  226. string resourceDir = ProjectLibrary.ResourceFolder;
  227. string destinationFolder = Path.Combine(resourceDir, currentDirectory);
  228. ElementEntry underCursorElement = FindElementAt(windowPos);
  229. if (underCursorElement != null)
  230. {
  231. LibraryEntry entry = ProjectLibrary.GetEntry(underCursorElement.path);
  232. if (entry != null && entry.Type == LibraryEntryType.Directory)
  233. destinationFolder = Path.Combine(resourceDir, entry.Path);
  234. }
  235. if (paths != null)
  236. {
  237. foreach (var path in paths)
  238. {
  239. if (path == null)
  240. continue;
  241. string absolutePath = path;
  242. if (!Path.IsPathRooted(absolutePath))
  243. absolutePath = Path.Combine(resourceDir, path);
  244. if (string.IsNullOrEmpty(absolutePath))
  245. continue;
  246. if (PathEx.IsPartOf(destinationFolder, absolutePath) || PathEx.Compare(absolutePath, destinationFolder))
  247. continue;
  248. string pathTail = PathEx.GetTail(absolutePath);
  249. string destination = Path.Combine(destinationFolder, pathTail);
  250. bool doCopy = !ProjectLibrary.Exists(path);
  251. if (Directory.Exists(path))
  252. {
  253. if (doCopy)
  254. DirectoryEx.Copy(path, GetUniquePath(destination));
  255. else
  256. DirectoryEx.Move(path, GetUniquePath(destination));
  257. }
  258. else if (File.Exists(path))
  259. {
  260. if (doCopy)
  261. FileEx.Copy(path, GetUniquePath(destination));
  262. else
  263. FileEx.Move(path, GetUniquePath(destination));
  264. }
  265. ProjectLibrary.Refresh();
  266. }
  267. }
  268. }
  269. private void DoOnSceneObjectDragDropped(Vector2I windowPos, SceneObject[] objects)
  270. {
  271. ClearHoverHighlight();
  272. autoScrollAmount = 0;
  273. if (EndDragSelection())
  274. return;
  275. string destinationFolder = currentDirectory;
  276. ElementEntry underCursorElement = FindElementAt(windowPos);
  277. if (underCursorElement != null)
  278. {
  279. LibraryEntry entry = ProjectLibrary.GetEntry(underCursorElement.path);
  280. if (entry != null && entry.Type == LibraryEntryType.Directory)
  281. destinationFolder = entry.Path;
  282. }
  283. if (objects != null)
  284. {
  285. foreach (var so in objects)
  286. {
  287. if (so == null)
  288. continue;
  289. Prefab newPrefab = new Prefab(so);
  290. string destination = GetUniquePath(Path.Combine(destinationFolder, so.Name + ".prefab"));
  291. ProjectLibrary.Create(newPrefab, destination);
  292. ProjectLibrary.Refresh();
  293. }
  294. }
  295. }
  296. private void DoOnDragEnd(Vector2I windowPos)
  297. {
  298. EndDragSelection();
  299. autoScrollAmount = 0;
  300. }
  301. private void ClearHoverHighlight()
  302. {
  303. if (!string.IsNullOrEmpty(hoverHighlightPath))
  304. {
  305. ElementEntry previousUnderCursorElem;
  306. if (entryLookup.TryGetValue(hoverHighlightPath, out previousUnderCursorElem))
  307. previousUnderCursorElem.MarkAsHovered(false);
  308. }
  309. hoverHighlightPath = "";
  310. }
  311. public void Ping(string path)
  312. {
  313. if (!string.IsNullOrEmpty(pingPath))
  314. {
  315. ElementEntry entry;
  316. if (entryLookup.TryGetValue(pingPath, out entry))
  317. entry.MarkAsPinged(false);
  318. }
  319. pingPath = path;
  320. if (!string.IsNullOrEmpty(pingPath))
  321. {
  322. ElementEntry entry;
  323. if (entryLookup.TryGetValue(pingPath, out entry))
  324. entry.MarkAsPinged(true);
  325. ScrollToEntry(pingPath);
  326. }
  327. }
  328. public void Reset()
  329. {
  330. currentDirectory = ProjectLibrary.Root.Path;
  331. selectionAnchorStart = -1;
  332. selectionAnchorEnd = -1;
  333. selectionPaths.Clear();
  334. pingPath = "";
  335. hoverHighlightPath = "";
  336. Refresh();
  337. }
  338. private void DeselectAll(bool onlyInternal = false)
  339. {
  340. SetSelection(new List<string>(), onlyInternal);
  341. selectionAnchorStart = -1;
  342. selectionAnchorEnd = -1;
  343. }
  344. private void Select(string path)
  345. {
  346. ElementEntry entry;
  347. if (!entryLookup.TryGetValue(path, out entry))
  348. return;
  349. bool ctrlDown = Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl);
  350. bool shiftDown = Input.IsButtonHeld(ButtonCode.LeftShift) || Input.IsButtonHeld(ButtonCode.RightShift);
  351. if (shiftDown)
  352. {
  353. if (selectionAnchorStart != -1 && selectionAnchorStart < entries.Count)
  354. {
  355. int start = Math.Min(entry.index, selectionAnchorStart);
  356. int end = Math.Max(entry.index, selectionAnchorStart);
  357. List<string> newSelection = new List<string>();
  358. for(int i = start; i <= end; i++)
  359. newSelection.Add(entries[i].path);
  360. SetSelection(newSelection);
  361. selectionAnchorEnd = entry.index;
  362. }
  363. else
  364. {
  365. SetSelection(new List<string>() {path});
  366. selectionAnchorStart = entry.index;
  367. selectionAnchorEnd = entry.index;
  368. }
  369. }
  370. else if (ctrlDown)
  371. {
  372. List<string> newSelection = new List<string>(selectionPaths);
  373. if (selectionPaths.Contains(path))
  374. {
  375. newSelection.Remove(path);
  376. if (newSelection.Count == 0)
  377. DeselectAll();
  378. else
  379. {
  380. if (selectionAnchorStart == entry.index)
  381. {
  382. ElementEntry newAnchorEntry;
  383. if (!entryLookup.TryGetValue(newSelection[0], out newAnchorEntry))
  384. selectionAnchorStart = -1;
  385. else
  386. selectionAnchorStart = newAnchorEntry.index;
  387. }
  388. if (selectionAnchorEnd == entry.index)
  389. {
  390. ElementEntry newAnchorEntry;
  391. if (!entryLookup.TryGetValue(newSelection[newSelection.Count - 1], out newAnchorEntry))
  392. selectionAnchorEnd = -1;
  393. else
  394. selectionAnchorEnd = newAnchorEntry.index;
  395. }
  396. SetSelection(newSelection);
  397. }
  398. }
  399. else
  400. {
  401. newSelection.Add(path);
  402. SetSelection(newSelection);
  403. selectionAnchorEnd = entry.index;
  404. }
  405. }
  406. else
  407. {
  408. SetSelection(new List<string>() {path});
  409. selectionAnchorStart = entry.index;
  410. selectionAnchorEnd = entry.index;
  411. }
  412. }
  413. private void MoveSelection(MoveDirection dir)
  414. {
  415. string newPath = "";
  416. if (selectionPaths.Count == 0 || selectionAnchorEnd == -1)
  417. {
  418. // Nothing is selected so we arbitrarily select first or last element
  419. if (entries.Count > 0)
  420. {
  421. switch (dir)
  422. {
  423. case MoveDirection.Left:
  424. case MoveDirection.Up:
  425. newPath = entries[entries.Count - 1].path;
  426. break;
  427. case MoveDirection.Right:
  428. case MoveDirection.Down:
  429. newPath = entries[0].path;
  430. break;
  431. }
  432. }
  433. }
  434. else
  435. {
  436. switch (dir)
  437. {
  438. case MoveDirection.Left:
  439. if (selectionAnchorEnd - 1 >= 0)
  440. newPath = entries[selectionAnchorEnd - 1].path;
  441. break;
  442. case MoveDirection.Up:
  443. if (selectionAnchorEnd - contentInfo.elementsPerRow >= 0)
  444. newPath = entries[selectionAnchorEnd - contentInfo.elementsPerRow].path;
  445. break;
  446. case MoveDirection.Right:
  447. if (selectionAnchorEnd + 1 < entries.Count)
  448. newPath = entries[selectionAnchorEnd + 1].path;
  449. break;
  450. case MoveDirection.Down:
  451. if (selectionAnchorEnd + contentInfo.elementsPerRow < entries.Count)
  452. newPath = entries[selectionAnchorEnd + contentInfo.elementsPerRow].path;
  453. break;
  454. }
  455. }
  456. if (!string.IsNullOrEmpty(newPath))
  457. {
  458. Select(newPath);
  459. ScrollToEntry(newPath);
  460. }
  461. }
  462. private void SetSelection(List<string> paths, bool onlyInternal = false)
  463. {
  464. if (selectionPaths != null)
  465. {
  466. foreach (var path in selectionPaths)
  467. {
  468. ElementEntry entry;
  469. if (entryLookup.TryGetValue(path, out entry))
  470. entry.MarkAsSelected(false);
  471. }
  472. }
  473. selectionPaths = paths;
  474. if (selectionPaths != null)
  475. {
  476. foreach (var path in selectionPaths)
  477. {
  478. ElementEntry entry;
  479. if (entryLookup.TryGetValue(path, out entry))
  480. entry.MarkAsSelected(true);
  481. }
  482. }
  483. Ping("");
  484. StopRename();
  485. if (!onlyInternal)
  486. {
  487. if (selectionPaths != null)
  488. Selection.resourcePaths = selectionPaths.ToArray();
  489. else
  490. Selection.resourcePaths = new string[0];
  491. }
  492. }
  493. private void EnterDirectory(string directory)
  494. {
  495. currentDirectory = directory;
  496. DeselectAll();
  497. Refresh();
  498. }
  499. private void Cut(IEnumerable<string> sourcePaths)
  500. {
  501. foreach (var path in cutPaths)
  502. {
  503. ElementEntry entry;
  504. if (entryLookup.TryGetValue(path, out entry))
  505. entry.MarkAsCut(false);
  506. }
  507. cutPaths.Clear();
  508. cutPaths.AddRange(sourcePaths);
  509. foreach (var path in cutPaths)
  510. {
  511. ElementEntry entry;
  512. if (entryLookup.TryGetValue(path, out entry))
  513. entry.MarkAsCut(true);
  514. }
  515. copyPaths.Clear();
  516. }
  517. private void Copy(IEnumerable<string> sourcePaths)
  518. {
  519. copyPaths.Clear();
  520. copyPaths.AddRange(sourcePaths);
  521. foreach (var path in cutPaths)
  522. {
  523. ElementEntry entry;
  524. if (entryLookup.TryGetValue(path, out entry))
  525. entry.MarkAsCut(false);
  526. }
  527. cutPaths.Clear();
  528. }
  529. private void Duplicate(IEnumerable<string> sourcePaths)
  530. {
  531. foreach (var source in sourcePaths)
  532. {
  533. if (Directory.Exists(source))
  534. DirectoryEx.Copy(source, GetUniquePath(source));
  535. else if (File.Exists(source))
  536. FileEx.Copy(source, GetUniquePath(source));
  537. ProjectLibrary.Refresh();
  538. }
  539. }
  540. private void Paste(string destinationFolder)
  541. {
  542. if (copyPaths.Count > 0)
  543. {
  544. for (int i = 0; i < copyPaths.Count; i++)
  545. {
  546. string destination = Path.Combine(destinationFolder, PathEx.GetTail(copyPaths[i]));
  547. if (Directory.Exists(copyPaths[i]))
  548. DirectoryEx.Copy(copyPaths[i], GetUniquePath(destination));
  549. else if (File.Exists(copyPaths[i]))
  550. FileEx.Copy(copyPaths[i], GetUniquePath(destination));
  551. }
  552. ProjectLibrary.Refresh();
  553. }
  554. else if (cutPaths.Count > 0)
  555. {
  556. for (int i = 0; i < cutPaths.Count; i++)
  557. {
  558. string destination = Path.Combine(destinationFolder, PathEx.GetTail(cutPaths[i]));
  559. if (Directory.Exists(cutPaths[i]))
  560. DirectoryEx.Move(cutPaths[i], GetUniquePath(destination));
  561. else if (File.Exists(cutPaths[i]))
  562. FileEx.Move(cutPaths[i], GetUniquePath(destination));
  563. }
  564. cutPaths.Clear();
  565. ProjectLibrary.Refresh();
  566. }
  567. }
  568. private string GetUniquePath(string path)
  569. {
  570. string extension = Path.GetExtension(path);
  571. string pathNoExtension = path;
  572. if (!string.IsNullOrEmpty(extension))
  573. pathNoExtension = path.Remove(path.Length - extension.Length);
  574. int idx = 0;
  575. string destination = path;
  576. while (ProjectLibrary.Exists(destination))
  577. {
  578. destination = pathNoExtension + "_" + idx;
  579. idx++;
  580. }
  581. return destination + extension;
  582. }
  583. private void OnEditorUpdate()
  584. {
  585. bool isRenameInProgress = inProgressRenameElement != null;
  586. if (HasContentFocus)
  587. {
  588. if (!isRenameInProgress)
  589. {
  590. if (Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl))
  591. {
  592. if (Input.IsButtonUp(ButtonCode.C))
  593. {
  594. CopySelection();
  595. }
  596. else if (Input.IsButtonUp(ButtonCode.X))
  597. {
  598. CutSelection();
  599. }
  600. else if (Input.IsButtonUp(ButtonCode.D))
  601. {
  602. DuplicateSelection();
  603. }
  604. else if (Input.IsButtonUp(ButtonCode.V))
  605. {
  606. PasteToSelection();
  607. }
  608. }
  609. if (Input.IsButtonDown(ButtonCode.Return))
  610. {
  611. if (selectionPaths.Count == 1)
  612. {
  613. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  614. if (entry != null && entry.Type == LibraryEntryType.Directory)
  615. {
  616. EnterDirectory(entry.Path);
  617. }
  618. }
  619. }
  620. else if (Input.IsButtonDown(ButtonCode.Back))
  621. {
  622. LibraryEntry entry = ProjectLibrary.GetEntry(currentDirectory);
  623. if (entry != null && entry.Parent != null)
  624. {
  625. EnterDirectory(entry.Parent.Path);
  626. }
  627. }
  628. else if (Input.IsButtonDown(ButtonCode.Up))
  629. {
  630. MoveSelection(MoveDirection.Up);
  631. }
  632. else if (Input.IsButtonDown(ButtonCode.Down))
  633. {
  634. MoveSelection(MoveDirection.Down);
  635. }
  636. else if (Input.IsButtonDown(ButtonCode.Left))
  637. {
  638. MoveSelection(MoveDirection.Left);
  639. }
  640. else if (Input.IsButtonDown(ButtonCode.Right))
  641. {
  642. MoveSelection(MoveDirection.Right);
  643. }
  644. else if (Input.IsButtonDown(ButtonCode.F2))
  645. {
  646. RenameSelection();
  647. }
  648. else if (Input.IsButtonDown(ButtonCode.Delete))
  649. {
  650. DeleteSelection();
  651. }
  652. }
  653. else
  654. {
  655. if (Input.IsButtonDown(ButtonCode.Return))
  656. {
  657. string newName = inProgressRenameElement.GetRenamedName();
  658. string originalPath = inProgressRenameElement.path;
  659. originalPath = originalPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  660. string newPath = Path.GetDirectoryName(originalPath);
  661. newPath = Path.Combine(newPath, newName + Path.GetExtension(originalPath));
  662. bool renameOK = true;
  663. if (!PathEx.IsValidFileName(newName))
  664. {
  665. DialogBox.Open(new LocEdString("Error"), new LocEdString("The name you specified is not a valid file name. Try another."), DialogBox.Type.OK);
  666. renameOK = false;
  667. }
  668. if (renameOK)
  669. {
  670. // Windows sees paths with dot at the end as if they didn't have it
  671. // so remove the dot to ensure the project library does the same
  672. string trimmedNewPath = newPath.TrimEnd('.');
  673. if (originalPath != trimmedNewPath && ProjectLibrary.Exists(trimmedNewPath))
  674. {
  675. DialogBox.Open(new LocEdString("Error"), new LocEdString("File/folder with that name already exists in this folder."), DialogBox.Type.OK);
  676. renameOK = false;
  677. }
  678. }
  679. if (renameOK)
  680. {
  681. ProjectLibrary.Rename(originalPath, newPath);
  682. StopRename();
  683. }
  684. }
  685. else if (Input.IsButtonDown(ButtonCode.Escape))
  686. {
  687. StopRename();
  688. }
  689. }
  690. }
  691. else
  692. {
  693. if(isRenameInProgress)
  694. StopRename();
  695. }
  696. if (autoScrollAmount != 0)
  697. {
  698. Rect2I contentBounds = contentScrollArea.ContentBounds;
  699. float scrollPct = autoScrollAmount / (float)contentBounds.height;
  700. contentScrollArea.VerticalScroll += scrollPct * Time.FrameDelta;
  701. }
  702. if (requiresRefresh)
  703. Refresh();
  704. dropTarget.Update();
  705. }
  706. private void OnEntryChanged(string entry)
  707. {
  708. requiresRefresh = true;
  709. }
  710. private void ScrollToEntry(string path)
  711. {
  712. ElementEntry entryGUI;
  713. if (!entryLookup.TryGetValue(path, out entryGUI))
  714. return;
  715. Rect2I entryBounds = entryGUI.Bounds;
  716. Rect2I contentBounds = contentScrollArea.Layout.Bounds;
  717. Rect2I windowEntryBounds = entryBounds;
  718. windowEntryBounds.x += contentBounds.x;
  719. windowEntryBounds.y += contentBounds.y;
  720. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  721. bool requiresScroll = windowEntryBounds.y < scrollAreaBounds.y ||
  722. (windowEntryBounds.y + windowEntryBounds.height) > (scrollAreaBounds.y + scrollAreaBounds.height);
  723. if (!requiresScroll)
  724. return;
  725. int scrollableSize = contentBounds.height - scrollAreaBounds.height;
  726. float percent = (((entryBounds.y + entryBounds.height * 0.5f) - scrollAreaBounds.height * 0.5f) / (float)scrollableSize);
  727. percent = MathEx.Clamp01(percent);
  728. contentScrollArea.VerticalScroll = percent;
  729. }
  730. private void Refresh()
  731. {
  732. requiresRefresh = false;
  733. LibraryEntry[] entriesToDisplay = new LibraryEntry[0];
  734. if (IsSearchActive)
  735. {
  736. entriesToDisplay = ProjectLibrary.Search("*" + searchQuery + "*");
  737. }
  738. else
  739. {
  740. DirectoryEntry entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  741. if (entry == null)
  742. {
  743. currentDirectory = ProjectLibrary.Root.Path;
  744. entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  745. }
  746. if(entry != null)
  747. entriesToDisplay = entry.Children;
  748. }
  749. if (scrollAreaPanel != null)
  750. scrollAreaPanel.Destroy();
  751. entries.Clear();
  752. entryLookup.Clear();
  753. inProgressRenameElement = null;
  754. scrollAreaPanel = contentScrollArea.Layout.AddPanel();
  755. RefreshDirectoryBar();
  756. SortEntries(entriesToDisplay);
  757. if (entriesToDisplay.Length == 0)
  758. return;
  759. contentInfo = new ContentInfo(this, viewType, entriesToDisplay.Length);
  760. if (viewType == ProjectViewType.List16)
  761. {
  762. for (int i = 0; i < entriesToDisplay.Length; i++)
  763. {
  764. ElementEntry guiEntry = new ElementEntry(contentInfo, contentInfo.main, entriesToDisplay[i], i);
  765. entries.Add(guiEntry);
  766. entryLookup[guiEntry.path] = guiEntry;
  767. if (i != entriesToDisplay.Length - 1)
  768. contentInfo.main.AddSpace(LIST_ENTRY_SPACING);
  769. }
  770. contentInfo.main.AddFlexibleSpace();
  771. }
  772. else
  773. {
  774. contentInfo.main.AddSpace(GRID_ENTRY_SPACING / 2);
  775. GUILayoutX rowLayout = contentInfo.main.AddLayoutX();
  776. contentInfo.main.AddSpace(GRID_ENTRY_SPACING);
  777. rowLayout.AddFlexibleSpace();
  778. int elemsInRow = 0;
  779. for (int i = 0; i < entriesToDisplay.Length; i++)
  780. {
  781. if (elemsInRow == contentInfo.elementsPerRow && elemsInRow > 0)
  782. {
  783. rowLayout = contentInfo.main.AddLayoutX();
  784. contentInfo.main.AddSpace(GRID_ENTRY_SPACING);
  785. rowLayout.AddFlexibleSpace();
  786. elemsInRow = 0;
  787. }
  788. ElementEntry guiEntry = new ElementEntry(contentInfo, rowLayout, entriesToDisplay[i], i);
  789. entries.Add(guiEntry);
  790. entryLookup[guiEntry.path] = guiEntry;
  791. rowLayout.AddFlexibleSpace();
  792. elemsInRow++;
  793. }
  794. int extraElements = contentInfo.elementsPerRow - elemsInRow;
  795. for (int i = 0; i < extraElements; i++)
  796. {
  797. rowLayout.AddSpace(contentInfo.labelWidth);
  798. rowLayout.AddFlexibleSpace();
  799. }
  800. contentInfo.main.AddFlexibleSpace();
  801. }
  802. for (int i = 0; i < entries.Count; i++)
  803. {
  804. ElementEntry guiEntry = entries[i];
  805. guiEntry.Initialize();
  806. if (cutPaths.Contains(guiEntry.path))
  807. guiEntry.MarkAsCut(true);
  808. if (selectionPaths.Contains(guiEntry.path))
  809. guiEntry.MarkAsSelected(true);
  810. else if (pingPath == guiEntry.path)
  811. guiEntry.MarkAsPinged(true);
  812. }
  813. Rect2I contentBounds = contentInfo.main.Bounds;
  814. Rect2I minimalBounds = GetScrollAreaBounds();
  815. contentBounds.height = Math.Max(contentBounds.height, minimalBounds.height);
  816. GUIButton catchAll = new GUIButton("", EditorStyles.Blank);
  817. catchAll.Bounds = contentBounds;
  818. catchAll.OnClick += OnCatchAllClicked;
  819. catchAll.SetContextMenu(entryContextMenu);
  820. contentInfo.underlay.AddElement(catchAll);
  821. Rect2I focusBounds = contentBounds; // Contents + Folder bar
  822. Rect2I scrollBounds = contentScrollArea.Bounds;
  823. focusBounds.x += scrollBounds.x;
  824. focusBounds.y += scrollBounds.y;
  825. Rect2I folderBarBounds = folderListLayout.Bounds;
  826. focusBounds.y -= folderBarBounds.height;
  827. focusBounds.height += folderBarBounds.height;
  828. GUIButton focusCatcher = new GUIButton("", EditorStyles.Blank);
  829. focusCatcher.OnFocusChanged += OnContentsFocusChanged;
  830. focusCatcher.Bounds = focusBounds;
  831. GUIPanel focusPanel = GUI.AddPanel(3);
  832. focusPanel.AddElement(focusCatcher);
  833. UpdateDragSelection(dragSelectionEnd);
  834. }
  835. private Vector2I WindowToScrollAreaCoords(Vector2I windowPos)
  836. {
  837. Rect2I scrollBounds = contentScrollArea.Layout.Bounds;
  838. Vector2I scrollPos = windowPos;
  839. scrollPos.x -= scrollBounds.x;
  840. scrollPos.y -= scrollBounds.y;
  841. return scrollPos;
  842. }
  843. private void StartDragSelection(Vector2I windowPos)
  844. {
  845. isDraggingSelection = true;
  846. dragSelectionStart = WindowToScrollAreaCoords(windowPos);
  847. dragSelectionEnd = dragSelectionStart;
  848. }
  849. private bool UpdateDragSelection(Vector2I windowPos)
  850. {
  851. if (!isDraggingSelection)
  852. return false;
  853. if (dragSelection == null)
  854. {
  855. dragSelection = new GUITexture(null, true, EditorStyles.SelectionArea);
  856. contentInfo.overlay.AddElement(dragSelection);
  857. }
  858. dragSelectionEnd = WindowToScrollAreaCoords(windowPos);
  859. Rect2I selectionArea = CalculateSelectionArea();
  860. SelectInArea(selectionArea);
  861. dragSelection.Bounds = selectionArea;
  862. return true;
  863. }
  864. private bool EndDragSelection()
  865. {
  866. if (!isDraggingSelection)
  867. return false;
  868. if (dragSelection != null)
  869. {
  870. dragSelection.Destroy();
  871. dragSelection = null;
  872. }
  873. Rect2I selectionArea = CalculateSelectionArea();
  874. SelectInArea(selectionArea);
  875. isDraggingSelection = false;
  876. return false;
  877. }
  878. private Rect2I CalculateSelectionArea()
  879. {
  880. Rect2I selectionArea = new Rect2I();
  881. if (dragSelectionStart.x < dragSelectionEnd.x)
  882. {
  883. selectionArea.x = dragSelectionStart.x;
  884. selectionArea.width = dragSelectionEnd.x - dragSelectionStart.x;
  885. }
  886. else
  887. {
  888. selectionArea.x = dragSelectionEnd.x;
  889. selectionArea.width = dragSelectionStart.x - dragSelectionEnd.x;
  890. }
  891. if (dragSelectionStart.y < dragSelectionEnd.y)
  892. {
  893. selectionArea.y = dragSelectionStart.y;
  894. selectionArea.height = dragSelectionEnd.y - dragSelectionStart.y;
  895. }
  896. else
  897. {
  898. selectionArea.y = dragSelectionEnd.y;
  899. selectionArea.height = dragSelectionStart.y - dragSelectionEnd.y;
  900. }
  901. Rect2I maxBounds = contentScrollArea.Layout.Bounds;
  902. maxBounds.x = 0;
  903. maxBounds.y = 0;
  904. selectionArea.Clip(maxBounds);
  905. return selectionArea;
  906. }
  907. private void SelectInArea(Rect2I scrollBounds)
  908. {
  909. ElementEntry[] foundElements = FindElementsOverlapping(scrollBounds);
  910. if (foundElements.Length > 0)
  911. {
  912. selectionAnchorStart = foundElements[0].index;
  913. selectionAnchorEnd = foundElements[foundElements.Length - 1].index;
  914. }
  915. else
  916. {
  917. selectionAnchorStart = -1;
  918. selectionAnchorEnd = -1;
  919. }
  920. List<string> elementPaths = new List<string>();
  921. foreach (var elem in foundElements)
  922. elementPaths.Add(elem.path);
  923. SetSelection(elementPaths);
  924. }
  925. private void RefreshDirectoryBar()
  926. {
  927. if (folderListLayout != null)
  928. {
  929. folderListLayout.Destroy();
  930. folderListLayout = null;
  931. }
  932. folderListLayout = folderBarLayout.AddLayoutX();
  933. string[] folders = null;
  934. string[] fullPaths = null;
  935. if (IsSearchActive)
  936. {
  937. folders = new[] {searchQuery};
  938. fullPaths = new[] { searchQuery };
  939. }
  940. else
  941. {
  942. string currentDir = Path.Combine("Resources", currentDirectory);
  943. folders = currentDir.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar },
  944. StringSplitOptions.RemoveEmptyEntries);
  945. fullPaths = new string[folders.Length];
  946. for (int i = 0; i < folders.Length; i++)
  947. {
  948. if (i == 0)
  949. fullPaths[i] = "";
  950. else
  951. fullPaths[i] = Path.Combine(fullPaths[i - 1], folders[i]);
  952. }
  953. }
  954. int availableWidth = folderBarLayout.Bounds.width - FOLDER_BUTTON_WIDTH * 2;
  955. int numFolders = 0;
  956. for (int i = folders.Length - 1; i >= 0; i--)
  957. {
  958. GUIButton folderButton = new GUIButton(folders[i]);
  959. if (!IsSearchActive)
  960. {
  961. string fullPath = fullPaths[i];
  962. folderButton.OnClick += () => OnFolderButtonClicked(fullPath);
  963. }
  964. GUILabel separator = new GUILabel("/", GUIOption.FixedWidth(FOLDER_SEPARATOR_WIDTH));
  965. folderListLayout.InsertElement(0, separator);
  966. folderListLayout.InsertElement(0, folderButton);
  967. numFolders++;
  968. Rect2I folderListBounds = folderListLayout.Bounds;
  969. if (folderListBounds.width > availableWidth)
  970. {
  971. if (numFolders > 2)
  972. {
  973. separator.Destroy();
  974. folderButton.Destroy();
  975. break;
  976. }
  977. }
  978. }
  979. }
  980. private void SortEntries(LibraryEntry[] input)
  981. {
  982. Array.Sort(input, (x, y) =>
  983. {
  984. if (x.Type == y.Type)
  985. return x.Name.CompareTo(y.Name);
  986. else
  987. return x.Type == LibraryEntryType.File ? 1 : -1;
  988. });
  989. }
  990. private void OnSelectionChanged(SceneObject[] sceneObjects, string[] resourcePaths)
  991. {
  992. if(sceneObjects.Length > 0)
  993. DeselectAll(true);
  994. }
  995. private void OnPing(string path)
  996. {
  997. Ping(path);
  998. }
  999. private void OnFolderButtonClicked(string path)
  1000. {
  1001. EnterDirectory(path);
  1002. }
  1003. private void OnContentsFocusChanged(bool focus)
  1004. {
  1005. hasContentFocus = focus;
  1006. }
  1007. private void OnEntryClicked(string path)
  1008. {
  1009. Select(path);
  1010. }
  1011. private void OnEntryDoubleClicked(string path)
  1012. {
  1013. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  1014. if (entry != null)
  1015. {
  1016. if (entry.Type == LibraryEntryType.Directory)
  1017. EnterDirectory(path);
  1018. else
  1019. {
  1020. FileEntry resEntry = (FileEntry) entry;
  1021. if (resEntry.ResType == ResourceType.Prefab)
  1022. {
  1023. EditorApplication.LoadScene(resEntry.Path);
  1024. }
  1025. }
  1026. }
  1027. }
  1028. private void OnCatchAllClicked()
  1029. {
  1030. DeselectAll();
  1031. }
  1032. private void OnHomeClicked()
  1033. {
  1034. currentDirectory = ProjectLibrary.Root.Path;
  1035. Refresh();
  1036. }
  1037. private void OnUpClicked()
  1038. {
  1039. currentDirectory = currentDirectory.Trim(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  1040. if (!string.IsNullOrEmpty(currentDirectory))
  1041. {
  1042. string parent = Path.GetDirectoryName(currentDirectory);
  1043. currentDirectory = parent;
  1044. Refresh();
  1045. }
  1046. }
  1047. private void CutSelection()
  1048. {
  1049. if (selectionPaths.Count > 0)
  1050. Cut(selectionPaths);
  1051. }
  1052. private void CopySelection()
  1053. {
  1054. if (selectionPaths.Count > 0)
  1055. Copy(selectionPaths);
  1056. }
  1057. private void DuplicateSelection()
  1058. {
  1059. if (selectionPaths.Count > 0)
  1060. Duplicate(selectionPaths);
  1061. }
  1062. private void PasteToSelection()
  1063. {
  1064. DirectoryEntry selectedDirectory = null;
  1065. if (selectionPaths.Count == 1)
  1066. {
  1067. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  1068. if (entry != null && entry.Type == LibraryEntryType.Directory)
  1069. selectedDirectory = (DirectoryEntry) entry;
  1070. }
  1071. if(selectedDirectory != null)
  1072. Paste(selectedDirectory.Path);
  1073. else
  1074. Paste(currentDirectory);
  1075. }
  1076. private void RenameSelection()
  1077. {
  1078. if (selectionPaths.Count == 0)
  1079. return;
  1080. if (selectionPaths.Count > 1)
  1081. {
  1082. DeselectAll();
  1083. Select(selectionPaths[0]);
  1084. }
  1085. ElementEntry entry;
  1086. if (entryLookup.TryGetValue(selectionPaths[0], out entry))
  1087. {
  1088. entry.StartRename();
  1089. inProgressRenameElement = entry;
  1090. }
  1091. }
  1092. private void StopRename()
  1093. {
  1094. if (inProgressRenameElement != null)
  1095. {
  1096. inProgressRenameElement.StopRename();
  1097. inProgressRenameElement = null;
  1098. }
  1099. }
  1100. private void DeleteSelection()
  1101. {
  1102. if (selectionPaths.Count == 0)
  1103. return;
  1104. DialogBox.Open(new LocEdString("Confirm deletion"), new LocEdString("Are you sure you want to delete the selected object(s)?"),
  1105. DialogBox.Type.YesNo,
  1106. type =>
  1107. {
  1108. if (type == DialogBox.ResultType.Yes)
  1109. {
  1110. foreach (var path in selectionPaths)
  1111. {
  1112. ProjectLibrary.Delete(path);
  1113. }
  1114. DeselectAll();
  1115. Refresh();
  1116. }
  1117. });
  1118. }
  1119. private void OnSearchChanged(string newValue)
  1120. {
  1121. searchQuery = newValue;
  1122. Refresh();
  1123. }
  1124. private void ClearSearch()
  1125. {
  1126. searchField.Value = "";
  1127. searchQuery = "";
  1128. Refresh();
  1129. }
  1130. private void OpenOptionsWindow()
  1131. {
  1132. Vector2I openPosition;
  1133. Rect2I buttonBounds = GUILayoutUtility.CalculateBounds(optionsButton, GUI);
  1134. openPosition.x = buttonBounds.x + buttonBounds.width / 2;
  1135. openPosition.y = buttonBounds.y + buttonBounds.height / 2;
  1136. LibraryDropDown dropDown = DropDownWindow.Open<LibraryDropDown>(this, openPosition);
  1137. dropDown.SetParent(this);
  1138. }
  1139. private Rect2I GetScrollAreaBounds()
  1140. {
  1141. Rect2I bounds = GUI.Bounds;
  1142. Rect2I folderListBounds = folderListLayout.Bounds;
  1143. Rect2I searchBarBounds = searchBarLayout.Bounds;
  1144. bounds.y += folderListBounds.height + searchBarBounds.height;
  1145. bounds.height -= folderListBounds.height + searchBarBounds.height;
  1146. return bounds;
  1147. }
  1148. protected override void WindowResized(int width, int height)
  1149. {
  1150. base.WindowResized(width, height);
  1151. Refresh();
  1152. dropTarget.Bounds = contentScrollArea.Bounds;
  1153. }
  1154. private class ContentInfo
  1155. {
  1156. public ContentInfo(LibraryWindow window, ProjectViewType viewType, int numEntries)
  1157. {
  1158. GUIPanel parentPanel = window.scrollAreaPanel;
  1159. GUIPanel contentPanel = parentPanel.AddPanel(1);
  1160. overlay = parentPanel.AddPanel(0);
  1161. underlay = parentPanel.AddPanel(2);
  1162. inputOverlay = parentPanel.AddPanel(-1);
  1163. main = contentPanel.AddLayoutY();
  1164. if (viewType == ProjectViewType.List16)
  1165. {
  1166. tileSize = 16;
  1167. gridLayout = false;
  1168. elementsPerRow = 1;
  1169. }
  1170. else
  1171. {
  1172. switch (viewType)
  1173. {
  1174. case ProjectViewType.Grid64:
  1175. tileSize = 64;
  1176. break;
  1177. case ProjectViewType.Grid48:
  1178. tileSize = 48;
  1179. break;
  1180. case ProjectViewType.Grid32:
  1181. tileSize = 32;
  1182. break;
  1183. }
  1184. gridLayout = true;
  1185. Rect2I scrollBounds = window.contentScrollArea.Bounds;
  1186. int availableWidth = scrollBounds.width;
  1187. int elemSize = tileSize + GRID_ENTRY_SPACING;
  1188. elementsPerRow = (availableWidth - GRID_ENTRY_SPACING * 2) / elemSize;
  1189. elementsPerRow = Math.Max(elementsPerRow, 1);
  1190. int numRows = MathEx.CeilToInt(numEntries / (float)elementsPerRow);
  1191. int neededHeight = numRows * (elemSize);
  1192. bool requiresScrollbar = neededHeight > scrollBounds.height;
  1193. if (requiresScrollbar)
  1194. {
  1195. availableWidth -= window.contentScrollArea.ScrollBarWidth;
  1196. elementsPerRow = (availableWidth - GRID_ENTRY_SPACING * 2) / elemSize;
  1197. }
  1198. if (elementsPerRow > 0)
  1199. labelWidth = (availableWidth - (elementsPerRow + 1)*MIN_HORZ_SPACING)/elementsPerRow;
  1200. else
  1201. labelWidth = 0;
  1202. }
  1203. this.window = window;
  1204. }
  1205. public GUILayout main;
  1206. public GUIPanel overlay;
  1207. public GUIPanel underlay;
  1208. public GUIPanel inputOverlay;
  1209. public LibraryWindow window;
  1210. public int tileSize;
  1211. public bool gridLayout;
  1212. public int elementsPerRow;
  1213. public int labelWidth;
  1214. }
  1215. private class ElementEntry
  1216. {
  1217. // Note: Order of these is relevant
  1218. enum UnderlayState
  1219. {
  1220. None, Hovered, Selected, Pinged
  1221. }
  1222. public int index;
  1223. public string path;
  1224. public GUITexture icon;
  1225. public GUILabel label;
  1226. public Rect2I bounds;
  1227. private GUITexture underlay;
  1228. private ContentInfo info;
  1229. private UnderlayState underlayState;
  1230. private GUITextBox renameTextBox;
  1231. public ElementEntry(ContentInfo info, GUILayout parent, LibraryEntry entry, int index)
  1232. {
  1233. GUILayout entryLayout;
  1234. if (info.gridLayout)
  1235. entryLayout = parent.AddLayoutY();
  1236. else
  1237. entryLayout = parent.AddLayoutX();
  1238. SpriteTexture iconTexture = GetIcon(entry);
  1239. icon = new GUITexture(iconTexture, GUIImageScaleMode.ScaleToFit,
  1240. true, GUIOption.FixedHeight(info.tileSize), GUIOption.FixedWidth(info.tileSize));
  1241. label = null;
  1242. if (info.gridLayout)
  1243. {
  1244. label = new GUILabel(entry.Name, EditorStyles.MultiLineLabelCentered,
  1245. GUIOption.FixedWidth(info.labelWidth), GUIOption.FlexibleHeight(0, MAX_LABEL_HEIGHT));
  1246. }
  1247. else
  1248. {
  1249. label = new GUILabel(entry.Name);
  1250. }
  1251. entryLayout.AddElement(icon);
  1252. entryLayout.AddElement(label);
  1253. this.info = info;
  1254. this.index = index;
  1255. this.path = entry.Path;
  1256. this.bounds = new Rect2I();
  1257. this.underlay = null;
  1258. }
  1259. public void Initialize()
  1260. {
  1261. bounds = icon.Bounds;
  1262. Rect2I labelBounds = label.Bounds;
  1263. bounds.x = MathEx.Min(bounds.x, labelBounds.x - SELECTION_EXTRA_WIDTH);
  1264. bounds.y = MathEx.Min(bounds.y, labelBounds.y) - 5; // 5 - Just padding for better look
  1265. bounds.width = MathEx.Max(bounds.x + bounds.width,
  1266. labelBounds.x + labelBounds.width) - bounds.x + SELECTION_EXTRA_WIDTH;
  1267. bounds.height = MathEx.Max(bounds.y + bounds.height,
  1268. labelBounds.y + labelBounds.height) - bounds.y;
  1269. LibraryWindow hoistedWindow = info.window;
  1270. string hoistedPath = path;
  1271. GUIButton overlayBtn = new GUIButton("", EditorStyles.Blank);
  1272. overlayBtn.Bounds = bounds;
  1273. overlayBtn.OnClick += () => hoistedWindow.OnEntryClicked(hoistedPath);
  1274. overlayBtn.OnDoubleClick += () => hoistedWindow.OnEntryDoubleClicked(hoistedPath);
  1275. overlayBtn.SetContextMenu(info.window.entryContextMenu);
  1276. info.overlay.AddElement(overlayBtn);
  1277. }
  1278. public Rect2I Bounds
  1279. {
  1280. get { return bounds; }
  1281. }
  1282. public void MarkAsCut(bool enable)
  1283. {
  1284. if (enable)
  1285. icon.SetTint(CUT_COLOR);
  1286. else
  1287. icon.SetTint(Color.White);
  1288. }
  1289. public void MarkAsSelected(bool enable)
  1290. {
  1291. if ((int)underlayState > (int)UnderlayState.Selected)
  1292. return;
  1293. if (enable)
  1294. {
  1295. CreateUnderlay();
  1296. underlay.SetTint(SELECTION_COLOR);
  1297. }
  1298. else
  1299. ClearUnderlay();
  1300. underlayState = UnderlayState.Selected;
  1301. }
  1302. public void MarkAsPinged(bool enable)
  1303. {
  1304. if ((int)underlayState > (int)UnderlayState.Pinged)
  1305. return;
  1306. if (enable)
  1307. {
  1308. CreateUnderlay();
  1309. underlay.SetTint(PING_COLOR);
  1310. }
  1311. else
  1312. ClearUnderlay();
  1313. underlayState = UnderlayState.Pinged;
  1314. }
  1315. public void MarkAsHovered(bool enable)
  1316. {
  1317. if ((int)underlayState > (int)UnderlayState.Hovered)
  1318. return;
  1319. if (enable)
  1320. {
  1321. CreateUnderlay();
  1322. underlay.SetTint(HOVER_COLOR);
  1323. }
  1324. else
  1325. ClearUnderlay();
  1326. underlayState = UnderlayState.Hovered;
  1327. }
  1328. public void StartRename()
  1329. {
  1330. if (renameTextBox != null)
  1331. return;
  1332. renameTextBox = new GUITextBox(false);
  1333. renameTextBox.Bounds = label.Bounds;
  1334. info.inputOverlay.AddElement(renameTextBox);
  1335. string name = Path.GetFileNameWithoutExtension(PathEx.GetTail(path));
  1336. renameTextBox.Text = name;
  1337. renameTextBox.Focus = true;
  1338. label.Visible = false;
  1339. }
  1340. public void StopRename()
  1341. {
  1342. if (renameTextBox != null)
  1343. {
  1344. renameTextBox.Destroy();
  1345. renameTextBox = null;
  1346. }
  1347. label.Visible = true;
  1348. }
  1349. public string GetRenamedName()
  1350. {
  1351. if (renameTextBox != null)
  1352. return renameTextBox.Text;
  1353. return "";
  1354. }
  1355. private void ClearUnderlay()
  1356. {
  1357. if (underlay != null)
  1358. {
  1359. underlay.Destroy();
  1360. underlay = null;
  1361. }
  1362. underlayState = UnderlayState.None;
  1363. }
  1364. private void CreateUnderlay()
  1365. {
  1366. if (underlay == null)
  1367. {
  1368. underlay = new GUITexture(Builtin.WhiteTexture);
  1369. underlay.Bounds = Bounds;
  1370. info.underlay.AddElement(underlay);
  1371. }
  1372. }
  1373. private static SpriteTexture GetIcon(LibraryEntry entry)
  1374. {
  1375. if (entry.Type == LibraryEntryType.Directory)
  1376. {
  1377. return EditorBuiltin.FolderIcon;
  1378. }
  1379. else
  1380. {
  1381. FileEntry fileEntry = (FileEntry)entry;
  1382. switch (fileEntry.ResType)
  1383. {
  1384. case ResourceType.Font:
  1385. return EditorBuiltin.FontIcon;
  1386. case ResourceType.Mesh:
  1387. return EditorBuiltin.MeshIcon;
  1388. case ResourceType.Texture:
  1389. return EditorBuiltin.TextureIcon;
  1390. case ResourceType.PlainText:
  1391. return EditorBuiltin.PlainTextIcon;
  1392. case ResourceType.ScriptCode:
  1393. return EditorBuiltin.ScriptCodeIcon;
  1394. case ResourceType.SpriteTexture:
  1395. return EditorBuiltin.SpriteTextureIcon;
  1396. case ResourceType.Shader:
  1397. return EditorBuiltin.ShaderIcon;
  1398. case ResourceType.Material:
  1399. return EditorBuiltin.MaterialIcon;
  1400. case ResourceType.Prefab:
  1401. return EditorBuiltin.PrefabIcon;
  1402. }
  1403. }
  1404. return null;
  1405. }
  1406. }
  1407. enum MoveDirection
  1408. {
  1409. Up, Down, Left, Right
  1410. }
  1411. }
  1412. internal class LibraryDropDown : DropDownWindow
  1413. {
  1414. private LibraryWindow parent;
  1415. public LibraryDropDown()
  1416. :base(150, 30)
  1417. { }
  1418. internal void SetParent(LibraryWindow parent)
  1419. {
  1420. this.parent = parent;
  1421. GUIToggleGroup group = new GUIToggleGroup();
  1422. GUIToggle list16 = new GUIToggle(new LocEdString("16"), group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1423. GUIToggle grid32 = new GUIToggle(new LocEdString("32"), group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1424. GUIToggle grid48 = new GUIToggle(new LocEdString("48"), group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1425. GUIToggle grid64 = new GUIToggle(new LocEdString("64"), group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1426. ProjectViewType activeType = parent.ViewType;
  1427. switch (activeType)
  1428. {
  1429. case ProjectViewType.List16:
  1430. list16.Value = true;
  1431. break;
  1432. case ProjectViewType.Grid32:
  1433. grid32.Value = true;
  1434. break;
  1435. case ProjectViewType.Grid48:
  1436. grid48.Value = true;
  1437. break;
  1438. case ProjectViewType.Grid64:
  1439. grid64.Value = true;
  1440. break;
  1441. }
  1442. list16.OnToggled += (active) =>
  1443. {
  1444. if (active)
  1445. ChangeViewType(ProjectViewType.List16);
  1446. };
  1447. grid32.OnToggled += (active) =>
  1448. {
  1449. if (active)
  1450. ChangeViewType(ProjectViewType.Grid32);
  1451. };
  1452. grid48.OnToggled += (active) =>
  1453. {
  1454. if (active)
  1455. ChangeViewType(ProjectViewType.Grid48);
  1456. };
  1457. grid64.OnToggled += (active) =>
  1458. {
  1459. if (active)
  1460. ChangeViewType(ProjectViewType.Grid64);
  1461. };
  1462. GUILayoutY vertLayout = GUI.AddLayoutY();
  1463. vertLayout.AddFlexibleSpace();
  1464. GUILayoutX contentLayout = vertLayout.AddLayoutX();
  1465. contentLayout.AddFlexibleSpace();
  1466. contentLayout.AddElement(list16);
  1467. contentLayout.AddElement(grid32);
  1468. contentLayout.AddElement(grid48);
  1469. contentLayout.AddElement(grid64);
  1470. contentLayout.AddFlexibleSpace();
  1471. vertLayout.AddFlexibleSpace();
  1472. }
  1473. private void ChangeViewType(ProjectViewType viewType)
  1474. {
  1475. parent.ViewType = viewType;
  1476. }
  1477. }
  1478. }