ProjectWindow.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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 ContextMenu entryContextMenu;
  267. private ProjectDropTarget dropTarget;
  268. private List<ElementEntry> entries = new List<ElementEntry>();
  269. private Dictionary<string, ElementEntry> entryLookup = new Dictionary<string, ElementEntry>();
  270. private int autoScrollAmount;
  271. // Cut/Copy/Paste
  272. private List<string> copyPaths = new List<string>();
  273. private List<string> cutPaths = new List<string>();
  274. internal ProjectViewType ViewType
  275. {
  276. get { return viewType; }
  277. set { viewType = value; Refresh(); }
  278. }
  279. [MenuItem("Windows/Project", ButtonModifier.Ctrl, ButtonCode.P)]
  280. private static void OpenProjectWindow()
  281. {
  282. OpenWindow<ProjectWindow>();
  283. }
  284. private void OnInitialize()
  285. {
  286. ProjectLibrary.OnEntryAdded += OnEntryChanged;
  287. ProjectLibrary.OnEntryRemoved += OnEntryChanged;
  288. GUILayoutY contentLayout = GUI.AddLayoutY();
  289. searchBarLayout = contentLayout.AddLayoutX();
  290. searchField = new GUITextField();
  291. searchField.OnChanged += OnSearchChanged;
  292. GUIButton clearSearchBtn = new GUIButton("C");
  293. clearSearchBtn.OnClick += ClearSearch;
  294. clearSearchBtn.SetWidth(40);
  295. optionsButton = new GUIButton("O");
  296. optionsButton.OnClick += OpenOptionsWindow;
  297. optionsButton.SetWidth(40);
  298. searchBarLayout.AddElement(searchField);
  299. searchBarLayout.AddElement(clearSearchBtn);
  300. searchBarLayout.AddElement(optionsButton);
  301. folderBarLayout = contentLayout.AddLayoutX();
  302. GUIButton homeButton = new GUIButton("H", GUIOption.FixedWidth(FOLDER_BUTTON_WIDTH));
  303. homeButton.OnClick += OnHomeClicked;
  304. GUIButton upButton = new GUIButton("U", GUIOption.FixedWidth(FOLDER_BUTTON_WIDTH));
  305. upButton.OnClick += OnUpClicked;
  306. folderBarLayout.AddElement(homeButton);
  307. folderBarLayout.AddElement(upButton);
  308. folderBarLayout.AddSpace(10);
  309. contentScrollArea = new GUIScrollArea(GUIOption.FlexibleWidth(), GUIOption.FlexibleHeight());
  310. contentLayout.AddElement(contentScrollArea);
  311. contentLayout.AddFlexibleSpace();
  312. entryContextMenu = new ContextMenu();
  313. entryContextMenu.AddItem("Cut", CutSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.X));
  314. entryContextMenu.AddItem("Copy", CopySelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.C));
  315. entryContextMenu.AddItem("Duplicate", DuplicateSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.D));
  316. entryContextMenu.AddItem("Paste", PasteToSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.V));
  317. Reset();
  318. dropTarget = new ProjectDropTarget(this);
  319. dropTarget.Bounds = contentScrollArea.Bounds;
  320. dropTarget.OnStart += DoOnDragStart;
  321. dropTarget.OnDrag += DoOnDragMove;
  322. dropTarget.OnLeave += DoOnDragLeave;
  323. dropTarget.OnDrop += DoOnDragDropped;
  324. }
  325. private ElementEntry FindElementAt(Vector2I windowPos)
  326. {
  327. Rect2I scrollBounds = contentScrollArea.Layout.Bounds;
  328. Vector2I scrollPos = windowPos;
  329. scrollPos.x -= scrollBounds.x;
  330. scrollPos.y -= scrollBounds.y;
  331. foreach (var element in entries)
  332. {
  333. if (element.bounds.Contains(scrollPos))
  334. return element;
  335. }
  336. return null;
  337. }
  338. private void DoOnDragStart(Vector2I windowPos)
  339. {
  340. ElementEntry underCursorElem = FindElementAt(windowPos);
  341. if (underCursorElem == null)
  342. return;
  343. if (!selectionPaths.Contains(underCursorElem.path))
  344. Select(underCursorElem.path);
  345. ResourceDragDropData dragDropData = new ResourceDragDropData(selectionPaths.ToArray());
  346. DragDrop.StartDrag(dragDropData);
  347. }
  348. private void DoOnDragMove(Vector2I windowPos)
  349. {
  350. ElementEntry underCursorElem = FindElementAt(windowPos);
  351. if (underCursorElem == null)
  352. {
  353. ClearHoverHighlight();
  354. }
  355. else
  356. {
  357. if (underCursorElem.path != hoverHighlightPath)
  358. {
  359. ClearHoverHighlight();
  360. hoverHighlightPath = underCursorElem.path;
  361. underCursorElem.MarkAsHovered(true);
  362. }
  363. }
  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. }
  374. private void DoOnDragLeave()
  375. {
  376. ClearHoverHighlight();
  377. autoScrollAmount = 0;
  378. }
  379. private void DoOnDragDropped(Vector2I windowPos, string[] paths)
  380. {
  381. string resourceDir = ProjectLibrary.ResourceFolder;
  382. string destinationFolder = Path.Combine(resourceDir, currentDirectory);
  383. ElementEntry underCursorElement = FindElementAt(windowPos);
  384. if (underCursorElement != null)
  385. {
  386. LibraryEntry entry = ProjectLibrary.GetEntry(underCursorElement.path);
  387. if (entry != null && entry.Type == LibraryEntryType.Directory)
  388. destinationFolder = Path.Combine(resourceDir, entry.Path);
  389. }
  390. if (paths != null)
  391. {
  392. foreach (var path in paths)
  393. {
  394. if (path == null)
  395. continue;
  396. string absolutePath = path;
  397. if (!Path.IsPathRooted(absolutePath))
  398. absolutePath = Path.Combine(resourceDir, path);
  399. if (string.IsNullOrEmpty(absolutePath))
  400. continue;
  401. if (PathEx.IsPartOf(destinationFolder, absolutePath) || PathEx.Compare(absolutePath, destinationFolder))
  402. continue;
  403. string destination = Path.Combine(destinationFolder, Path.GetFileName(absolutePath));
  404. if (ProjectLibrary.Exists(path))
  405. ProjectLibrary.Move(path, destination, true);
  406. else
  407. ProjectLibrary.Copy(path, destination, true);
  408. }
  409. }
  410. ClearHoverHighlight();
  411. autoScrollAmount = 0;
  412. }
  413. private void ClearHoverHighlight()
  414. {
  415. if (!string.IsNullOrEmpty(hoverHighlightPath))
  416. {
  417. ElementEntry previousUnderCursorElem;
  418. if (entryLookup.TryGetValue(hoverHighlightPath, out previousUnderCursorElem))
  419. previousUnderCursorElem.MarkAsHovered(false);
  420. }
  421. hoverHighlightPath = "";
  422. }
  423. public void Ping(Resource resource)
  424. {
  425. if (!string.IsNullOrEmpty(pingPath))
  426. {
  427. ElementEntry entry;
  428. if (entryLookup.TryGetValue(pingPath, out entry))
  429. entry.MarkAsPinged(false);
  430. }
  431. if (resource != null)
  432. pingPath = ProjectLibrary.GetPath(resource);
  433. else
  434. pingPath = "";
  435. if (!string.IsNullOrEmpty(pingPath))
  436. {
  437. ElementEntry entry;
  438. if (entryLookup.TryGetValue(pingPath, out entry))
  439. entry.MarkAsPinged(true);
  440. ScrollToEntry(pingPath);
  441. }
  442. }
  443. private void DeselectAll()
  444. {
  445. SetSelection(new List<string>());
  446. selectionAnchorStart = -1;
  447. selectionAnchorEnd = -1;
  448. }
  449. private void Select(string path)
  450. {
  451. ElementEntry entry;
  452. if (!entryLookup.TryGetValue(path, out entry))
  453. return;
  454. bool ctrlDown = Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl);
  455. bool shiftDown = Input.IsButtonHeld(ButtonCode.LeftShift) || Input.IsButtonHeld(ButtonCode.RightShift);
  456. if (shiftDown)
  457. {
  458. if (selectionAnchorStart != -1 && selectionAnchorStart < entries.Count)
  459. {
  460. int start = Math.Min(entry.index, selectionAnchorStart);
  461. int end = Math.Max(entry.index, selectionAnchorStart);
  462. List<string> newSelection = new List<string>();
  463. for(int i = start; i <= end; i++)
  464. newSelection.Add(entries[i].path);
  465. SetSelection(newSelection);
  466. selectionAnchorEnd = entry.index;
  467. }
  468. else
  469. {
  470. SetSelection(new List<string>() {path});
  471. selectionAnchorStart = entry.index;
  472. selectionAnchorEnd = entry.index;
  473. }
  474. }
  475. else if (ctrlDown)
  476. {
  477. List<string> newSelection = new List<string>(selectionPaths);
  478. if (selectionPaths.Contains(path))
  479. {
  480. newSelection.Remove(path);
  481. if (newSelection.Count == 0)
  482. DeselectAll();
  483. else
  484. {
  485. if (selectionAnchorStart == entry.index)
  486. {
  487. ElementEntry newAnchorEntry;
  488. if (!entryLookup.TryGetValue(newSelection[0], out newAnchorEntry))
  489. selectionAnchorStart = -1;
  490. else
  491. selectionAnchorStart = newAnchorEntry.index;
  492. }
  493. if (selectionAnchorEnd == entry.index)
  494. {
  495. ElementEntry newAnchorEntry;
  496. if (!entryLookup.TryGetValue(newSelection[newSelection.Count - 1], out newAnchorEntry))
  497. selectionAnchorEnd = -1;
  498. else
  499. selectionAnchorEnd = newAnchorEntry.index;
  500. }
  501. SetSelection(newSelection);
  502. }
  503. }
  504. else
  505. {
  506. newSelection.Add(path);
  507. SetSelection(newSelection);
  508. selectionAnchorEnd = entry.index;
  509. }
  510. }
  511. else
  512. {
  513. SetSelection(new List<string>() { path });
  514. selectionAnchorStart = entry.index;
  515. selectionAnchorEnd = entry.index;
  516. }
  517. }
  518. private void MoveSelection(MoveDirection dir)
  519. {
  520. string newPath = "";
  521. if (selectionPaths.Count == 0 || selectionAnchorEnd == -1)
  522. {
  523. // Nothing is selected so we arbitrarily select first or last element
  524. if (entries.Count > 0)
  525. {
  526. switch (dir)
  527. {
  528. case MoveDirection.Left:
  529. case MoveDirection.Up:
  530. newPath = entries[entries.Count - 1].path;
  531. break;
  532. case MoveDirection.Right:
  533. case MoveDirection.Down:
  534. newPath = entries[0].path;
  535. break;
  536. }
  537. }
  538. }
  539. else
  540. {
  541. switch (dir)
  542. {
  543. case MoveDirection.Left:
  544. if (selectionAnchorEnd - 1 >= 0)
  545. newPath = entries[selectionAnchorEnd - 1].path;
  546. break;
  547. case MoveDirection.Up:
  548. if (selectionAnchorEnd - contentInfo.elementsPerRow >= 0)
  549. newPath = entries[selectionAnchorEnd - contentInfo.elementsPerRow].path;
  550. break;
  551. case MoveDirection.Right:
  552. if (selectionAnchorEnd + 1 < entries.Count)
  553. newPath = entries[selectionAnchorEnd + 1].path;
  554. break;
  555. case MoveDirection.Down:
  556. if (selectionAnchorEnd + contentInfo.elementsPerRow < entries.Count)
  557. newPath = entries[selectionAnchorEnd + contentInfo.elementsPerRow].path;
  558. break;
  559. }
  560. }
  561. if (!string.IsNullOrEmpty(newPath))
  562. {
  563. Select(newPath);
  564. ScrollToEntry(newPath);
  565. }
  566. }
  567. private void SetSelection(List<string> paths)
  568. {
  569. if (selectionPaths != null)
  570. {
  571. foreach (var path in selectionPaths)
  572. {
  573. ElementEntry entry;
  574. if (entryLookup.TryGetValue(path, out entry))
  575. entry.MarkAsSelected(false);
  576. }
  577. }
  578. selectionPaths = paths;
  579. if (selectionPaths != null)
  580. {
  581. foreach (var path in selectionPaths)
  582. {
  583. ElementEntry entry;
  584. if (entryLookup.TryGetValue(path, out entry))
  585. entry.MarkAsSelected(true);
  586. }
  587. }
  588. Ping(null);
  589. if (selectionPaths != null)
  590. Selection.resourcePaths = selectionPaths.ToArray();
  591. else
  592. Selection.resourcePaths = new string[0];
  593. }
  594. private void EnterDirectory(string directory)
  595. {
  596. currentDirectory = directory;
  597. DeselectAll();
  598. Refresh();
  599. }
  600. private void Cut(IEnumerable<string> sourcePaths)
  601. {
  602. foreach (var path in cutPaths)
  603. {
  604. ElementEntry entry;
  605. if (entryLookup.TryGetValue(path, out entry))
  606. entry.MarkAsCut(false);
  607. }
  608. cutPaths.Clear();
  609. cutPaths.AddRange(sourcePaths);
  610. foreach (var path in cutPaths)
  611. {
  612. ElementEntry entry;
  613. if (entryLookup.TryGetValue(path, out entry))
  614. entry.MarkAsCut(true);
  615. }
  616. copyPaths.Clear();
  617. }
  618. private void Copy(IEnumerable<string> sourcePaths)
  619. {
  620. copyPaths.Clear();
  621. copyPaths.AddRange(sourcePaths);
  622. foreach (var path in cutPaths)
  623. {
  624. ElementEntry entry;
  625. if (entryLookup.TryGetValue(path, out entry))
  626. entry.MarkAsCut(false);
  627. }
  628. cutPaths.Clear();
  629. }
  630. private void Duplicate(IEnumerable<string> sourcePaths)
  631. {
  632. foreach (var source in sourcePaths)
  633. {
  634. int idx = 0;
  635. string destination;
  636. do
  637. {
  638. destination = source + "_" + idx;
  639. idx++;
  640. } while (!ProjectLibrary.Exists(destination));
  641. ProjectLibrary.Copy(source, destination);
  642. }
  643. }
  644. private void Paste(string destinationFolder)
  645. {
  646. if (copyPaths.Count > 0)
  647. {
  648. for (int i = 0; i < copyPaths.Count; i++)
  649. {
  650. string destination = Path.Combine(destinationFolder, Path.GetFileName(copyPaths[i]));
  651. ProjectLibrary.Copy(copyPaths[i], destination, true);
  652. }
  653. Refresh();
  654. }
  655. else if (cutPaths.Count > 0)
  656. {
  657. for (int i = 0; i < cutPaths.Count; i++)
  658. {
  659. string destination = Path.Combine(destinationFolder, Path.GetFileName(cutPaths[i]));
  660. ProjectLibrary.Move(cutPaths[i], destination, true);
  661. }
  662. cutPaths.Clear();
  663. Refresh();
  664. }
  665. }
  666. private void EditorUpdate()
  667. {
  668. if (HasContentFocus)
  669. {
  670. if (Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl))
  671. {
  672. if (Input.IsButtonUp(ButtonCode.C))
  673. {
  674. CopySelection();
  675. }
  676. else if (Input.IsButtonUp(ButtonCode.X))
  677. {
  678. CutSelection();
  679. }
  680. else if (Input.IsButtonUp(ButtonCode.D))
  681. {
  682. DuplicateSelection();
  683. }
  684. else if (Input.IsButtonUp(ButtonCode.V))
  685. {
  686. PasteToSelection();
  687. }
  688. }
  689. if (Input.IsButtonDown(ButtonCode.Return))
  690. {
  691. if (selectionPaths.Count == 1)
  692. {
  693. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  694. if (entry != null && entry.Type == LibraryEntryType.Directory)
  695. {
  696. EnterDirectory(entry.Path);
  697. }
  698. }
  699. }
  700. else if (Input.IsButtonDown(ButtonCode.Back))
  701. {
  702. LibraryEntry entry = ProjectLibrary.GetEntry(currentDirectory);
  703. if (entry != null && entry.Parent != null)
  704. {
  705. EnterDirectory(entry.Parent.Path);
  706. }
  707. }
  708. else if (Input.IsButtonDown(ButtonCode.Up))
  709. {
  710. MoveSelection(MoveDirection.Up);
  711. }
  712. else if (Input.IsButtonDown(ButtonCode.Down))
  713. {
  714. MoveSelection(MoveDirection.Down);
  715. }
  716. else if (Input.IsButtonDown(ButtonCode.Left))
  717. {
  718. MoveSelection(MoveDirection.Left);
  719. }
  720. else if (Input.IsButtonDown(ButtonCode.Right))
  721. {
  722. MoveSelection(MoveDirection.Right);
  723. }
  724. }
  725. if (autoScrollAmount != 0)
  726. {
  727. Rect2I contentBounds = contentScrollArea.ContentBounds;
  728. float scrollPct = autoScrollAmount / (float)contentBounds.height;
  729. contentScrollArea.VerticalScroll += scrollPct * Time.FrameDelta;
  730. }
  731. dropTarget.Update();
  732. }
  733. private void OnEntryChanged(string entry)
  734. {
  735. Refresh();
  736. }
  737. private void ScrollToEntry(string path)
  738. {
  739. ElementEntry entryGUI;
  740. if (!entryLookup.TryGetValue(path, out entryGUI))
  741. return;
  742. Rect2I entryBounds = entryGUI.Bounds;
  743. Rect2I contentBounds = contentScrollArea.Layout.Bounds;
  744. Rect2I windowEntryBounds = entryBounds;
  745. windowEntryBounds.x += contentBounds.x;
  746. windowEntryBounds.y += contentBounds.y;
  747. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  748. bool requiresScroll = windowEntryBounds.y < scrollAreaBounds.y ||
  749. (windowEntryBounds.y + windowEntryBounds.height) > (scrollAreaBounds.y + scrollAreaBounds.height);
  750. if (!requiresScroll)
  751. return;
  752. int scrollableSize = contentBounds.height - scrollAreaBounds.height;
  753. float percent = (((entryBounds.y + entryBounds.height * 0.5f) - scrollAreaBounds.height * 0.5f) / (float)scrollableSize);
  754. percent = MathEx.Clamp01(percent);
  755. contentScrollArea.VerticalScroll = percent;
  756. }
  757. private void Refresh()
  758. {
  759. LibraryEntry[] entriesToDisplay = new LibraryEntry[0];
  760. if (IsSearchActive)
  761. {
  762. entriesToDisplay = ProjectLibrary.Search("*" + searchQuery + "*");
  763. }
  764. else
  765. {
  766. DirectoryEntry entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  767. if (entry == null)
  768. {
  769. currentDirectory = ProjectLibrary.Root.Path;
  770. entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  771. }
  772. if(entry != null)
  773. entriesToDisplay = entry.Children;
  774. }
  775. if (scrollAreaPanel != null)
  776. scrollAreaPanel.Destroy();
  777. entries.Clear();
  778. entryLookup.Clear();
  779. scrollAreaPanel = contentScrollArea.Layout.AddPanel();
  780. RefreshDirectoryBar();
  781. SortEntries(entriesToDisplay);
  782. if (entriesToDisplay.Length == 0)
  783. return;
  784. contentInfo = new ContentInfo(this, viewType, entriesToDisplay.Length);
  785. if (viewType == ProjectViewType.List16)
  786. {
  787. for (int i = 0; i < entriesToDisplay.Length; i++)
  788. {
  789. ElementEntry guiEntry = new ElementEntry(contentInfo, contentInfo.main, entriesToDisplay[i], i);
  790. entries.Add(guiEntry);
  791. entryLookup[guiEntry.path] = guiEntry;
  792. if (i != entriesToDisplay.Length - 1)
  793. contentInfo.main.AddSpace(LIST_ENTRY_SPACING);
  794. }
  795. contentInfo.main.AddFlexibleSpace();
  796. }
  797. else
  798. {
  799. int tileSize = 64;
  800. switch (viewType)
  801. {
  802. case ProjectViewType.Grid64: tileSize = 64; break;
  803. case ProjectViewType.Grid48: tileSize = 48; break;
  804. case ProjectViewType.Grid32: tileSize = 32; break;
  805. }
  806. contentInfo.main.AddSpace(GRID_ENTRY_SPACING / 2);
  807. GUILayoutX rowLayout = contentInfo.main.AddLayoutX();
  808. contentInfo.main.AddSpace(GRID_ENTRY_SPACING);
  809. rowLayout.AddFlexibleSpace();
  810. int elemsInRow = 0;
  811. for (int i = 0; i < entriesToDisplay.Length; i++)
  812. {
  813. if (elemsInRow == contentInfo.elementsPerRow && elemsInRow > 0)
  814. {
  815. rowLayout = contentInfo.main.AddLayoutX();
  816. contentInfo.main.AddSpace(GRID_ENTRY_SPACING);
  817. rowLayout.AddFlexibleSpace();
  818. elemsInRow = 0;
  819. }
  820. ElementEntry guiEntry = new ElementEntry(contentInfo, rowLayout, entriesToDisplay[i], i);
  821. entries.Add(guiEntry);
  822. entryLookup[guiEntry.path] = guiEntry;
  823. rowLayout.AddFlexibleSpace();
  824. elemsInRow++;
  825. }
  826. int extraElements = contentInfo.elementsPerRow - elemsInRow;
  827. for (int i = 0; i < extraElements; i++)
  828. {
  829. rowLayout.AddSpace(contentInfo.labelWidth);
  830. rowLayout.AddFlexibleSpace();
  831. }
  832. contentInfo.main.AddFlexibleSpace();
  833. }
  834. for (int i = 0; i < entries.Count; i++)
  835. {
  836. ElementEntry guiEntry = entries[i];
  837. guiEntry.Initialize();
  838. if (cutPaths.Contains(guiEntry.path))
  839. guiEntry.MarkAsCut(true);
  840. if (selectionPaths.Contains(guiEntry.path))
  841. guiEntry.MarkAsSelected(true);
  842. else if (pingPath == guiEntry.path)
  843. guiEntry.MarkAsPinged(true);
  844. }
  845. Rect2I contentBounds = contentInfo.main.Bounds;
  846. Rect2I minimalBounds = GetScrollAreaBounds();
  847. contentBounds.height = Math.Max(contentBounds.height, minimalBounds.height);
  848. GUIButton catchAll = new GUIButton("", EditorStyles.Blank);
  849. catchAll.Bounds = contentBounds;
  850. catchAll.OnClick += OnCatchAllClicked;
  851. catchAll.SetContextMenu(entryContextMenu);
  852. catchAll.OnFocusChanged += OnContentsFocusChanged;
  853. contentInfo.underlay.AddElement(catchAll);
  854. }
  855. private void RefreshDirectoryBar()
  856. {
  857. if (folderListLayout != null)
  858. {
  859. folderListLayout.Destroy();
  860. folderListLayout = null;
  861. }
  862. folderListLayout = folderBarLayout.AddLayoutX();
  863. string[] folders = null;
  864. string[] fullPaths = null;
  865. if (IsSearchActive)
  866. {
  867. folders = new[] {searchQuery};
  868. fullPaths = new[] { searchQuery };
  869. }
  870. else
  871. {
  872. string currentDir = Path.Combine("Resources", currentDirectory);
  873. folders = currentDir.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar },
  874. StringSplitOptions.RemoveEmptyEntries);
  875. fullPaths = new string[folders.Length];
  876. for (int i = 0; i < folders.Length; i++)
  877. {
  878. if (i == 0)
  879. fullPaths[i] = "";
  880. else
  881. fullPaths[i] = Path.Combine(fullPaths[i - 1], folders[i]);
  882. }
  883. }
  884. int availableWidth = folderBarLayout.Bounds.width - FOLDER_BUTTON_WIDTH * 2;
  885. int numFolders = 0;
  886. for (int i = folders.Length - 1; i >= 0; i--)
  887. {
  888. GUIButton folderButton = new GUIButton(folders[i]);
  889. if (!IsSearchActive)
  890. {
  891. string fullPath = fullPaths[i];
  892. folderButton.OnClick += () => OnFolderButtonClicked(fullPath);
  893. }
  894. GUILabel separator = new GUILabel("/", GUIOption.FixedWidth(FOLDER_SEPARATOR_WIDTH));
  895. folderListLayout.InsertElement(0, separator);
  896. folderListLayout.InsertElement(0, folderButton);
  897. numFolders++;
  898. Rect2I folderListBounds = folderListLayout.Bounds;
  899. if (folderListBounds.width > availableWidth)
  900. {
  901. if (numFolders > 2)
  902. {
  903. separator.Destroy();
  904. folderButton.Destroy();
  905. break;
  906. }
  907. }
  908. }
  909. }
  910. private void SortEntries(LibraryEntry[] input)
  911. {
  912. Array.Sort(input, (x, y) =>
  913. {
  914. if (x.Type == y.Type)
  915. return x.Name.CompareTo(y.Name);
  916. else
  917. return x.Type == LibraryEntryType.File ? 1 : -1;
  918. });
  919. }
  920. private void OnFolderButtonClicked(string path)
  921. {
  922. EnterDirectory(path);
  923. }
  924. private void OnContentsFocusChanged(bool focus)
  925. {
  926. hasContentFocus = focus;
  927. }
  928. private void OnEntryClicked(string path)
  929. {
  930. Select(path);
  931. }
  932. private void OnEntryDoubleClicked(string path)
  933. {
  934. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  935. if (entry != null && entry.Type == LibraryEntryType.Directory)
  936. {
  937. EnterDirectory(path);
  938. }
  939. }
  940. private void OnCatchAllClicked()
  941. {
  942. DeselectAll();
  943. }
  944. private void OnHomeClicked()
  945. {
  946. currentDirectory = ProjectLibrary.Root.Path;
  947. Refresh();
  948. }
  949. private void OnUpClicked()
  950. {
  951. currentDirectory = currentDirectory.Trim(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  952. if (!string.IsNullOrEmpty(currentDirectory))
  953. {
  954. string parent = Path.GetDirectoryName(currentDirectory);
  955. currentDirectory = parent;
  956. Refresh();
  957. }
  958. }
  959. private void CutSelection()
  960. {
  961. if (selectionPaths.Count > 0)
  962. Cut(selectionPaths);
  963. }
  964. private void CopySelection()
  965. {
  966. if (selectionPaths.Count > 0)
  967. Copy(selectionPaths);
  968. }
  969. private void DuplicateSelection()
  970. {
  971. if (selectionPaths.Count > 0)
  972. Duplicate(selectionPaths);
  973. }
  974. private void PasteToSelection()
  975. {
  976. DirectoryEntry selectedDirectory = null;
  977. if (selectionPaths.Count == 1)
  978. {
  979. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  980. if (entry != null && entry.Type == LibraryEntryType.Directory)
  981. selectedDirectory = (DirectoryEntry) entry;
  982. }
  983. if(selectedDirectory != null)
  984. Paste(selectedDirectory.Path);
  985. else
  986. Paste(currentDirectory);
  987. }
  988. private void OnSearchChanged(string newValue)
  989. {
  990. searchQuery = newValue;
  991. Refresh();
  992. }
  993. private void ClearSearch()
  994. {
  995. searchField.Value = "";
  996. searchQuery = "";
  997. Refresh();
  998. }
  999. private void OpenOptionsWindow()
  1000. {
  1001. Vector2I openPosition;
  1002. Rect2I buttonBounds = GUILayoutUtility.CalculateBounds(optionsButton, GUI);
  1003. openPosition.x = buttonBounds.x + buttonBounds.width / 2;
  1004. openPosition.y = buttonBounds.y + buttonBounds.height / 2;
  1005. ProjectDropDown dropDown = DropDownWindow.Open<ProjectDropDown>(this, openPosition);
  1006. dropDown.SetParent(this);
  1007. }
  1008. private void Reset()
  1009. {
  1010. currentDirectory = ProjectLibrary.Root.Path;
  1011. selectionAnchorStart = -1;
  1012. selectionAnchorEnd = -1;
  1013. selectionPaths.Clear();
  1014. pingPath = "";
  1015. hoverHighlightPath = "";
  1016. Refresh();
  1017. }
  1018. private Rect2I GetScrollAreaBounds()
  1019. {
  1020. Rect2I bounds = GUI.Bounds;
  1021. Rect2I searchBarBounds = searchBarLayout.Bounds;
  1022. bounds.y += searchBarBounds.height;
  1023. bounds.height -= searchBarBounds.height;
  1024. return bounds;
  1025. }
  1026. protected override void WindowResized(int width, int height)
  1027. {
  1028. base.WindowResized(width, height);
  1029. Refresh();
  1030. dropTarget.Bounds = contentScrollArea.Bounds;
  1031. }
  1032. }
  1033. internal class ProjectDropDown : DropDownWindow
  1034. {
  1035. private ProjectWindow parent;
  1036. public ProjectDropDown()
  1037. :base(150, 30)
  1038. { }
  1039. internal void SetParent(ProjectWindow parent)
  1040. {
  1041. this.parent = parent;
  1042. GUIToggleGroup group = new GUIToggleGroup();
  1043. GUIToggle list16 = new GUIToggle("16", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1044. GUIToggle grid32 = new GUIToggle("32", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1045. GUIToggle grid48 = new GUIToggle("48", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1046. GUIToggle grid64 = new GUIToggle("64", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1047. ProjectViewType activeType = parent.ViewType;
  1048. switch (activeType)
  1049. {
  1050. case ProjectViewType.List16:
  1051. list16.ToggleOn();
  1052. break;
  1053. case ProjectViewType.Grid32:
  1054. grid32.ToggleOn();
  1055. break;
  1056. case ProjectViewType.Grid48:
  1057. grid48.ToggleOn();
  1058. break;
  1059. case ProjectViewType.Grid64:
  1060. grid64.ToggleOn();
  1061. break;
  1062. }
  1063. list16.OnToggled += (active) =>
  1064. {
  1065. if (active)
  1066. ChangeViewType(ProjectViewType.List16);
  1067. };
  1068. grid32.OnToggled += (active) =>
  1069. {
  1070. if (active)
  1071. ChangeViewType(ProjectViewType.Grid32);
  1072. };
  1073. grid48.OnToggled += (active) =>
  1074. {
  1075. if (active)
  1076. ChangeViewType(ProjectViewType.Grid48);
  1077. };
  1078. grid64.OnToggled += (active) =>
  1079. {
  1080. if (active)
  1081. ChangeViewType(ProjectViewType.Grid64);
  1082. };
  1083. GUILayoutY vertLayout = GUI.AddLayoutY();
  1084. vertLayout.AddFlexibleSpace();
  1085. GUILayoutX contentLayout = vertLayout.AddLayoutX();
  1086. contentLayout.AddFlexibleSpace();
  1087. contentLayout.AddElement(list16);
  1088. contentLayout.AddElement(grid32);
  1089. contentLayout.AddElement(grid48);
  1090. contentLayout.AddElement(grid64);
  1091. contentLayout.AddFlexibleSpace();
  1092. vertLayout.AddFlexibleSpace();
  1093. }
  1094. private void ChangeViewType(ProjectViewType viewType)
  1095. {
  1096. parent.ViewType = viewType;
  1097. }
  1098. }
  1099. }