ProjectWindow.cs 59 KB

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