ProjectWindow.cs 48 KB

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