ProjectWindow.cs 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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 = pathNoExtension;
  714. while (ProjectLibrary.Exists(destination))
  715. {
  716. destination = pathNoExtension + "_" + idx;
  717. idx++;
  718. }
  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. Rect2I maxBounds = contentScrollArea.Layout.Bounds;
  992. maxBounds.x = 0;
  993. maxBounds.y = 0;
  994. selectionArea.Clip(maxBounds);
  995. return selectionArea;
  996. }
  997. private void SelectInArea(Rect2I scrollBounds)
  998. {
  999. ElementEntry[] foundElements = FindElementsOverlapping(scrollBounds);
  1000. if (foundElements.Length > 0)
  1001. {
  1002. selectionAnchorStart = foundElements[0].index;
  1003. selectionAnchorEnd = foundElements[foundElements.Length - 1].index;
  1004. }
  1005. else
  1006. {
  1007. selectionAnchorStart = -1;
  1008. selectionAnchorEnd = -1;
  1009. }
  1010. List<string> elementPaths = new List<string>();
  1011. foreach (var elem in foundElements)
  1012. elementPaths.Add(elem.path);
  1013. SetSelection(elementPaths);
  1014. }
  1015. private void RefreshDirectoryBar()
  1016. {
  1017. if (folderListLayout != null)
  1018. {
  1019. folderListLayout.Destroy();
  1020. folderListLayout = null;
  1021. }
  1022. folderListLayout = folderBarLayout.AddLayoutX();
  1023. string[] folders = null;
  1024. string[] fullPaths = null;
  1025. if (IsSearchActive)
  1026. {
  1027. folders = new[] {searchQuery};
  1028. fullPaths = new[] { searchQuery };
  1029. }
  1030. else
  1031. {
  1032. string currentDir = Path.Combine("Resources", currentDirectory);
  1033. folders = currentDir.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar },
  1034. StringSplitOptions.RemoveEmptyEntries);
  1035. fullPaths = new string[folders.Length];
  1036. for (int i = 0; i < folders.Length; i++)
  1037. {
  1038. if (i == 0)
  1039. fullPaths[i] = "";
  1040. else
  1041. fullPaths[i] = Path.Combine(fullPaths[i - 1], folders[i]);
  1042. }
  1043. }
  1044. int availableWidth = folderBarLayout.Bounds.width - FOLDER_BUTTON_WIDTH * 2;
  1045. int numFolders = 0;
  1046. for (int i = folders.Length - 1; i >= 0; i--)
  1047. {
  1048. GUIButton folderButton = new GUIButton(folders[i]);
  1049. if (!IsSearchActive)
  1050. {
  1051. string fullPath = fullPaths[i];
  1052. folderButton.OnClick += () => OnFolderButtonClicked(fullPath);
  1053. }
  1054. GUILabel separator = new GUILabel("/", GUIOption.FixedWidth(FOLDER_SEPARATOR_WIDTH));
  1055. folderListLayout.InsertElement(0, separator);
  1056. folderListLayout.InsertElement(0, folderButton);
  1057. numFolders++;
  1058. Rect2I folderListBounds = folderListLayout.Bounds;
  1059. if (folderListBounds.width > availableWidth)
  1060. {
  1061. if (numFolders > 2)
  1062. {
  1063. separator.Destroy();
  1064. folderButton.Destroy();
  1065. break;
  1066. }
  1067. }
  1068. }
  1069. }
  1070. private void SortEntries(LibraryEntry[] input)
  1071. {
  1072. Array.Sort(input, (x, y) =>
  1073. {
  1074. if (x.Type == y.Type)
  1075. return x.Name.CompareTo(y.Name);
  1076. else
  1077. return x.Type == LibraryEntryType.File ? 1 : -1;
  1078. });
  1079. }
  1080. private void OnFolderButtonClicked(string path)
  1081. {
  1082. EnterDirectory(path);
  1083. }
  1084. private void OnContentsFocusChanged(bool focus)
  1085. {
  1086. hasContentFocus = focus;
  1087. }
  1088. private void OnEntryClicked(string path)
  1089. {
  1090. Select(path);
  1091. }
  1092. private void OnEntryDoubleClicked(string path)
  1093. {
  1094. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  1095. if (entry != null && entry.Type == LibraryEntryType.Directory)
  1096. {
  1097. EnterDirectory(path);
  1098. }
  1099. }
  1100. private void OnCatchAllClicked()
  1101. {
  1102. DeselectAll();
  1103. }
  1104. private void OnHomeClicked()
  1105. {
  1106. currentDirectory = ProjectLibrary.Root.Path;
  1107. Refresh();
  1108. }
  1109. private void OnUpClicked()
  1110. {
  1111. currentDirectory = currentDirectory.Trim(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  1112. if (!string.IsNullOrEmpty(currentDirectory))
  1113. {
  1114. string parent = Path.GetDirectoryName(currentDirectory);
  1115. currentDirectory = parent;
  1116. Refresh();
  1117. }
  1118. }
  1119. private void CutSelection()
  1120. {
  1121. if (selectionPaths.Count > 0)
  1122. Cut(selectionPaths);
  1123. }
  1124. private void CopySelection()
  1125. {
  1126. if (selectionPaths.Count > 0)
  1127. Copy(selectionPaths);
  1128. }
  1129. private void DuplicateSelection()
  1130. {
  1131. if (selectionPaths.Count > 0)
  1132. Duplicate(selectionPaths);
  1133. }
  1134. private void PasteToSelection()
  1135. {
  1136. DirectoryEntry selectedDirectory = null;
  1137. if (selectionPaths.Count == 1)
  1138. {
  1139. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  1140. if (entry != null && entry.Type == LibraryEntryType.Directory)
  1141. selectedDirectory = (DirectoryEntry) entry;
  1142. }
  1143. if(selectedDirectory != null)
  1144. Paste(selectedDirectory.Path);
  1145. else
  1146. Paste(currentDirectory);
  1147. }
  1148. private void OnSearchChanged(string newValue)
  1149. {
  1150. searchQuery = newValue;
  1151. Refresh();
  1152. }
  1153. private void ClearSearch()
  1154. {
  1155. searchField.Value = "";
  1156. searchQuery = "";
  1157. Refresh();
  1158. }
  1159. private void OpenOptionsWindow()
  1160. {
  1161. Vector2I openPosition;
  1162. Rect2I buttonBounds = GUILayoutUtility.CalculateBounds(optionsButton, GUI);
  1163. openPosition.x = buttonBounds.x + buttonBounds.width / 2;
  1164. openPosition.y = buttonBounds.y + buttonBounds.height / 2;
  1165. ProjectDropDown dropDown = DropDownWindow.Open<ProjectDropDown>(this, openPosition);
  1166. dropDown.SetParent(this);
  1167. }
  1168. private void Reset()
  1169. {
  1170. currentDirectory = ProjectLibrary.Root.Path;
  1171. selectionAnchorStart = -1;
  1172. selectionAnchorEnd = -1;
  1173. selectionPaths.Clear();
  1174. pingPath = "";
  1175. hoverHighlightPath = "";
  1176. Refresh();
  1177. }
  1178. private Rect2I GetScrollAreaBounds()
  1179. {
  1180. Rect2I bounds = GUI.Bounds;
  1181. Rect2I folderListBounds = folderListLayout.Bounds;
  1182. Rect2I searchBarBounds = searchBarLayout.Bounds;
  1183. bounds.y += folderListBounds.height + searchBarBounds.height;
  1184. bounds.height -= folderListBounds.height + searchBarBounds.height;
  1185. return bounds;
  1186. }
  1187. protected override void WindowResized(int width, int height)
  1188. {
  1189. base.WindowResized(width, height);
  1190. Refresh();
  1191. dropTarget.Bounds = contentScrollArea.Bounds;
  1192. }
  1193. }
  1194. internal class ProjectDropDown : DropDownWindow
  1195. {
  1196. private ProjectWindow parent;
  1197. public ProjectDropDown()
  1198. :base(150, 30)
  1199. { }
  1200. internal void SetParent(ProjectWindow parent)
  1201. {
  1202. this.parent = parent;
  1203. GUIToggleGroup group = new GUIToggleGroup();
  1204. GUIToggle list16 = new GUIToggle("16", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1205. GUIToggle grid32 = new GUIToggle("32", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1206. GUIToggle grid48 = new GUIToggle("48", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1207. GUIToggle grid64 = new GUIToggle("64", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1208. ProjectViewType activeType = parent.ViewType;
  1209. switch (activeType)
  1210. {
  1211. case ProjectViewType.List16:
  1212. list16.ToggleOn();
  1213. break;
  1214. case ProjectViewType.Grid32:
  1215. grid32.ToggleOn();
  1216. break;
  1217. case ProjectViewType.Grid48:
  1218. grid48.ToggleOn();
  1219. break;
  1220. case ProjectViewType.Grid64:
  1221. grid64.ToggleOn();
  1222. break;
  1223. }
  1224. list16.OnToggled += (active) =>
  1225. {
  1226. if (active)
  1227. ChangeViewType(ProjectViewType.List16);
  1228. };
  1229. grid32.OnToggled += (active) =>
  1230. {
  1231. if (active)
  1232. ChangeViewType(ProjectViewType.Grid32);
  1233. };
  1234. grid48.OnToggled += (active) =>
  1235. {
  1236. if (active)
  1237. ChangeViewType(ProjectViewType.Grid48);
  1238. };
  1239. grid64.OnToggled += (active) =>
  1240. {
  1241. if (active)
  1242. ChangeViewType(ProjectViewType.Grid64);
  1243. };
  1244. GUILayoutY vertLayout = GUI.AddLayoutY();
  1245. vertLayout.AddFlexibleSpace();
  1246. GUILayoutX contentLayout = vertLayout.AddLayoutX();
  1247. contentLayout.AddFlexibleSpace();
  1248. contentLayout.AddElement(list16);
  1249. contentLayout.AddElement(grid32);
  1250. contentLayout.AddElement(grid48);
  1251. contentLayout.AddElement(grid64);
  1252. contentLayout.AddFlexibleSpace();
  1253. vertLayout.AddFlexibleSpace();
  1254. }
  1255. private void ChangeViewType(ProjectViewType viewType)
  1256. {
  1257. parent.ViewType = viewType;
  1258. }
  1259. }
  1260. }