ProjectWindow.cs 51 KB

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