ProjectWindow.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 struct EntryGUI
  14. {
  15. public EntryGUI(GUITexture icon, GUILabel label)
  16. {
  17. this.icon = icon;
  18. this.label = label;
  19. }
  20. public GUITexture icon;
  21. public GUILabel label;
  22. }
  23. private const int GRID_ENTRY_SPACING = 15;
  24. private const int LIST_ENTRY_SPACING = 7;
  25. private const int MAX_LABEL_HEIGHT = 50;
  26. private static readonly Color PING_COLOR = Color.BansheeOrange;
  27. private static readonly Color SELECTION_COLOR = Color.DarkCyan;
  28. private bool hasContentFocus = false;
  29. private bool HasContentFocus { get { return HasFocus && hasContentFocus; } } // TODO - This is dummy and never set
  30. private ProjectViewType viewType = ProjectViewType.Grid64;
  31. private string currentDirectory = "";
  32. private List<string> selectionPaths = new List<string>();
  33. private string pingPath = "";
  34. private GUIScrollArea contentScrollArea;
  35. private GUIPanel scrollAreaPanel;
  36. private GUIButton optionsButton;
  37. private ContextMenu entryContextMenu;
  38. private Dictionary<string, EntryGUI> pathToGUIEntry = new Dictionary<string, EntryGUI>();
  39. // Cut/Copy/Paste
  40. private List<string> copyPaths = new List<string>();
  41. private List<string> cutPaths = new List<string>();
  42. internal ProjectViewType ViewType
  43. {
  44. get { return viewType; }
  45. set { viewType = value; Refresh(); }
  46. }
  47. [MenuItem("Windows/Project", ButtonModifier.Ctrl, ButtonCode.P)]
  48. private static void OpenProjectWindow()
  49. {
  50. OpenWindow<ProjectWindow>();
  51. }
  52. private void OnInitialize()
  53. {
  54. ProjectLibrary.OnEntryAdded += OnEntryChanged;
  55. ProjectLibrary.OnEntryRemoved += OnEntryChanged;
  56. GUILayoutY contentLayout = GUI.AddLayoutY();
  57. GUILayoutX searchBarLayout = contentLayout.AddLayoutX();
  58. GUITextField searchField = new GUITextField();
  59. searchField.OnChanged += OnSearchChanged;
  60. GUIButton clearSearchBtn = new GUIButton("C");
  61. clearSearchBtn.OnClick += ClearSearch;
  62. clearSearchBtn.SetWidth(40);
  63. optionsButton = new GUIButton("O");
  64. optionsButton.OnClick += OpenOptionsWindow;
  65. optionsButton.SetWidth(40);
  66. searchBarLayout.AddElement(searchField);
  67. searchBarLayout.AddElement(clearSearchBtn);
  68. searchBarLayout.AddElement(optionsButton);
  69. // TODO - Add search bar + options button with drop-down
  70. // TODO - Add directory bar + home button
  71. contentScrollArea = new GUIScrollArea(GUIOption.FlexibleWidth(), GUIOption.FlexibleHeight());
  72. contentLayout.AddElement(contentScrollArea);
  73. entryContextMenu = new ContextMenu();
  74. entryContextMenu.AddItem("Cut", CutSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.X));
  75. entryContextMenu.AddItem("Copy", CopySelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.C));
  76. entryContextMenu.AddItem("Duplicate", DuplicateSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.D));
  77. entryContextMenu.AddItem("Paste", PasteToSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.V));
  78. Reset();
  79. }
  80. public void Ping(Resource resource)
  81. {
  82. pingPath = ProjectLibrary.GetPath(resource);
  83. Refresh();
  84. ScrollToEntry(pingPath);
  85. }
  86. private void Select(List<string> paths)
  87. {
  88. selectionPaths = paths;
  89. pingPath = "";
  90. Refresh();
  91. }
  92. private void EnterDirectory(string directory)
  93. {
  94. currentDirectory = directory;
  95. pingPath = "";
  96. selectionPaths.Clear();
  97. Refresh();
  98. }
  99. private void Cut(IEnumerable<string> sourcePaths)
  100. {
  101. cutPaths.Clear();
  102. cutPaths.AddRange(sourcePaths);
  103. copyPaths.Clear();
  104. Refresh();
  105. }
  106. private void Copy(IEnumerable<string> sourcePaths)
  107. {
  108. copyPaths.Clear();
  109. copyPaths.AddRange(sourcePaths);
  110. cutPaths.Clear();
  111. Refresh();
  112. }
  113. private void Duplicate(IEnumerable<string> sourcePaths)
  114. {
  115. foreach (var source in sourcePaths)
  116. {
  117. int idx = 0;
  118. string destination;
  119. do
  120. {
  121. destination = source + "_" + idx;
  122. idx++;
  123. } while (!ProjectLibrary.Exists(destination));
  124. ProjectLibrary.Copy(source, destination);
  125. }
  126. }
  127. private void Paste(string destinationFolder)
  128. {
  129. if (copyPaths.Count > 0)
  130. {
  131. for (int i = 0; i < copyPaths.Count; i++)
  132. {
  133. string destination = Path.Combine(destinationFolder, Path.GetFileName(copyPaths[i]));
  134. ProjectLibrary.Copy(copyPaths[i], destination, true);
  135. }
  136. Refresh();
  137. }
  138. else if (cutPaths.Count > 0)
  139. {
  140. for (int i = 0; i < cutPaths.Count; i++)
  141. {
  142. string destination = Path.Combine(destinationFolder, Path.GetFileName(cutPaths[i]));
  143. ProjectLibrary.Move(cutPaths[i], destination, true);
  144. }
  145. cutPaths.Clear();
  146. Refresh();
  147. }
  148. }
  149. private void EditorUpdate()
  150. {
  151. if (HasContentFocus)
  152. {
  153. if (Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl))
  154. {
  155. if (Input.IsButtonUp(ButtonCode.C))
  156. {
  157. CopySelection();
  158. }
  159. else if (Input.IsButtonUp(ButtonCode.X))
  160. {
  161. CutSelection();
  162. }
  163. else if (Input.IsButtonUp(ButtonCode.D))
  164. {
  165. DuplicateSelection();
  166. }
  167. else if (Input.IsButtonUp(ButtonCode.V))
  168. {
  169. PasteToSelection();
  170. }
  171. }
  172. }
  173. // TODO - Handle input, drag and drop and whatever else might be needed
  174. // TODO - Animate ping?
  175. // TODO - Automatically scroll window when dragging near border?
  176. // TODO - Drag and drop from Explorer should work to import an asset (i.e. DragAndDropArea)
  177. // - This should be something that should be enabled per editor window perhaps?
  178. }
  179. private void OnEntryChanged(string entry)
  180. {
  181. Refresh();
  182. }
  183. private void ScrollToEntry(string path)
  184. {
  185. Rect2I contentBounds = scrollAreaPanel.Bounds;
  186. Rect2I scrollAreaBounds = contentScrollArea.ContentBounds;
  187. EntryGUI entryGUI;
  188. if (!pathToGUIEntry.TryGetValue(path, out entryGUI))
  189. return;
  190. Rect2I entryBounds = entryGUI.icon.Bounds;
  191. float percent = (entryBounds.x - scrollAreaBounds.height * 0.5f) / contentBounds.height;
  192. percent = MathEx.Clamp01(percent);
  193. contentScrollArea.VerticalScroll = percent;
  194. }
  195. private SpriteTexture GetIcon(LibraryEntry entry)
  196. {
  197. if (entry.Type == LibraryEntryType.Directory)
  198. {
  199. return EditorBuiltin.FolderIcon;
  200. }
  201. else
  202. {
  203. FileEntry fileEntry = (FileEntry)entry;
  204. switch (fileEntry.ResType)
  205. {
  206. case ResourceType.Font:
  207. return EditorBuiltin.FontIcon;
  208. case ResourceType.Mesh:
  209. return EditorBuiltin.MeshIcon;
  210. case ResourceType.Texture:
  211. return EditorBuiltin.TextureIcon;
  212. case ResourceType.PlainText:
  213. return null; // TODO
  214. case ResourceType.ScriptCode:
  215. return null; // TODO
  216. case ResourceType.SpriteTexture:
  217. return null; // TODO
  218. case ResourceType.Shader:
  219. return null; // TODO
  220. case ResourceType.Material:
  221. return null; // TODO
  222. }
  223. }
  224. return null;
  225. }
  226. private void Refresh()
  227. {
  228. DirectoryEntry entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  229. if (entry == null)
  230. {
  231. Reset();
  232. return;
  233. }
  234. if (scrollAreaPanel != null)
  235. scrollAreaPanel.Destroy();
  236. pathToGUIEntry.Clear();
  237. scrollAreaPanel = contentScrollArea.Layout.AddPanel();
  238. GUIPanel contentPanel = scrollAreaPanel.AddPanel(1);
  239. GUIPanel contentOverlayPanel = scrollAreaPanel.AddPanel(0);
  240. GUIPanel contentUnderlayPanel = scrollAreaPanel.AddPanel(2);
  241. GUILayout contentLayout = contentPanel.AddLayoutY();
  242. Rect2I scrollBounds = contentScrollArea.Bounds;
  243. LibraryEntry[] childEntries = entry.Children;
  244. if (childEntries.Length == 0)
  245. return;
  246. if (viewType == ProjectViewType.List16)
  247. {
  248. int tileSize = 16;
  249. for (int i = 0; i < childEntries.Length; i++)
  250. {
  251. LibraryEntry currentEntry = childEntries[i];
  252. CreateEntryGUI(contentLayout, tileSize, false, currentEntry);
  253. if (i != childEntries.Length - 1)
  254. contentLayout.AddSpace(LIST_ENTRY_SPACING);
  255. }
  256. contentLayout.AddFlexibleSpace();
  257. }
  258. else
  259. {
  260. int tileSize = 64;
  261. switch (viewType)
  262. {
  263. case ProjectViewType.Grid64: tileSize = 64; break;
  264. case ProjectViewType.Grid48: tileSize = 48; break;
  265. case ProjectViewType.Grid32: tileSize = 32; break;
  266. }
  267. GUILayoutX rowLayout = contentLayout.AddLayoutX();
  268. rowLayout.AddFlexibleSpace();
  269. int elemSize = tileSize + GRID_ENTRY_SPACING;
  270. int elemsPerRow = (scrollBounds.width - GRID_ENTRY_SPACING*2)/elemSize;
  271. int elemsInRow = 0;
  272. for (int i = 0; i < childEntries.Length; i++)
  273. {
  274. if (elemsInRow == elemsPerRow && elemsInRow > 0)
  275. {
  276. rowLayout = contentLayout.AddLayoutX();
  277. contentLayout.AddSpace(GRID_ENTRY_SPACING);
  278. rowLayout.AddFlexibleSpace();
  279. elemsInRow = 0;
  280. }
  281. LibraryEntry currentEntry = childEntries[i];
  282. CreateEntryGUI(rowLayout, tileSize, true, currentEntry);
  283. rowLayout.AddFlexibleSpace();
  284. elemsInRow++;
  285. }
  286. int extraElements = elemsPerRow - elemsInRow;
  287. for (int i = 0; i < extraElements; i++)
  288. {
  289. rowLayout.AddSpace(tileSize);
  290. rowLayout.AddFlexibleSpace();
  291. }
  292. contentLayout.AddFlexibleSpace();
  293. }
  294. for (int i = 0; i < childEntries.Length; i++)
  295. {
  296. LibraryEntry currentEntry = childEntries[i];
  297. CreateEntryOverlayGUI(contentOverlayPanel, contentUnderlayPanel, pathToGUIEntry[currentEntry.Path], currentEntry);
  298. }
  299. Rect2I contentBounds = contentLayout.Bounds;
  300. GUIButton catchAll = new GUIButton("", EditorStyles.Blank);
  301. catchAll.Bounds = contentBounds;
  302. catchAll.OnClick += OnCatchAllClicked;
  303. catchAll.SetContextMenu(entryContextMenu);
  304. contentUnderlayPanel.AddElement(catchAll);
  305. Debug.Log("REFRESHED " + Time.FrameNumber);
  306. }
  307. private void CreateEntryGUI(GUILayout parentLayout, int tileSize, bool grid, LibraryEntry entry)
  308. {
  309. GUILayout entryLayout;
  310. if(grid)
  311. entryLayout = parentLayout.AddLayoutY();
  312. else
  313. entryLayout = parentLayout.AddLayoutX();
  314. SpriteTexture iconTexture = GetIcon(entry);
  315. GUITexture icon = new GUITexture(iconTexture, GUIImageScaleMode.ScaleToFit,
  316. true, GUIOption.FixedHeight(tileSize), GUIOption.FixedWidth(tileSize));
  317. GUILabel label = null;
  318. if (grid)
  319. {
  320. label = new GUILabel(entry.Name, EditorStyles.MultiLineLabel,
  321. GUIOption.FixedWidth(tileSize), GUIOption.FlexibleHeight(0, MAX_LABEL_HEIGHT));
  322. }
  323. else
  324. {
  325. label = new GUILabel(entry.Name);
  326. }
  327. entryLayout.AddElement(icon);
  328. entryLayout.AddElement(label);
  329. pathToGUIEntry[entry.Path] = new EntryGUI(icon, label);
  330. }
  331. private void CreateEntryOverlayGUI(GUIPanel overlayPanel, GUIPanel underlayPanel, EntryGUI gui, LibraryEntry entry)
  332. {
  333. // Add overlay button
  334. Rect2I entryButtonBounds = gui.icon.Bounds;
  335. Rect2I labelBounds = gui.label.Bounds;
  336. entryButtonBounds.x = MathEx.Min(entryButtonBounds.x, labelBounds.x);
  337. entryButtonBounds.y = MathEx.Min(entryButtonBounds.y, labelBounds.y);
  338. entryButtonBounds.width = MathEx.Max(entryButtonBounds.x + entryButtonBounds.width,
  339. labelBounds.x + labelBounds.width) - entryButtonBounds.x;
  340. entryButtonBounds.height = MathEx.Max(entryButtonBounds.y + entryButtonBounds.height,
  341. labelBounds.y + labelBounds.height) - entryButtonBounds.y;
  342. GUIButton overlayBtn = new GUIButton("", EditorStyles.Blank);
  343. overlayBtn.Bounds = entryButtonBounds;
  344. overlayBtn.OnClick += () => OnEntryClicked(entry.Path);
  345. overlayBtn.OnDoubleClick += () => OnEntryDoubleClicked(entry.Path);
  346. overlayBtn.SetContextMenu(entryContextMenu);
  347. overlayPanel.AddElement(overlayBtn);
  348. if (cutPaths.Contains(entry.Path))
  349. {
  350. gui.icon.SetTint(new Color(1.0f, 1.0f, 1.0f, 0.5f));
  351. }
  352. if (selectionPaths.Contains(entry.Path))
  353. {
  354. GUITexture underlay = new GUITexture(Builtin.WhiteTexture);
  355. underlay.Bounds = entryButtonBounds;
  356. underlay.SetTint(SELECTION_COLOR);
  357. underlayPanel.AddElement(underlay);
  358. }
  359. else if (pingPath == entry.Path)
  360. {
  361. GUITexture underlay = new GUITexture(Builtin.WhiteTexture);
  362. underlay.Bounds = entryButtonBounds;
  363. underlay.SetTint(PING_COLOR);
  364. underlayPanel.AddElement(underlay);
  365. }
  366. }
  367. private void OnEntryClicked(string path)
  368. {
  369. Select(new List<string> { path });
  370. Selection.resourcePaths = new string[] {path};
  371. Debug.Log("CLICKED " + Time.FrameNumber);
  372. }
  373. private void OnEntryDoubleClicked(string path)
  374. {
  375. Debug.Log("DOUBLE CLICK " + path);
  376. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  377. if (entry != null && entry.Type == LibraryEntryType.Directory)
  378. {
  379. EnterDirectory(path);
  380. }
  381. }
  382. private void OnCatchAllClicked()
  383. {
  384. Select(new List<string> { });
  385. Selection.resourcePaths = new string[] { };
  386. }
  387. private void CutSelection()
  388. {
  389. if (selectionPaths.Count > 0)
  390. Cut(selectionPaths);
  391. }
  392. private void CopySelection()
  393. {
  394. if (selectionPaths.Count > 0)
  395. Copy(selectionPaths);
  396. }
  397. private void DuplicateSelection()
  398. {
  399. if (selectionPaths.Count > 0)
  400. Duplicate(selectionPaths);
  401. }
  402. private void PasteToSelection()
  403. {
  404. DirectoryEntry selectedDirectory = null;
  405. if (selectionPaths.Count == 1)
  406. {
  407. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  408. if (entry != null && entry.Type == LibraryEntryType.Directory)
  409. selectedDirectory = (DirectoryEntry) entry;
  410. }
  411. if(selectedDirectory != null)
  412. Paste(selectedDirectory.Path);
  413. else
  414. Paste(currentDirectory);
  415. }
  416. private void OnSearchChanged(string newValue)
  417. {
  418. // TODO
  419. }
  420. private void ClearSearch()
  421. {
  422. // TODO
  423. }
  424. private void OpenOptionsWindow()
  425. {
  426. Vector2I openPosition;
  427. Rect2I buttonBounds = GUILayoutUtility.CalculateBounds(optionsButton, GUI);
  428. openPosition.x = buttonBounds.x + buttonBounds.width / 2;
  429. openPosition.y = buttonBounds.y + buttonBounds.height / 2;
  430. ProjectDropDown dropDown = DropDownWindow.Open<ProjectDropDown>(this, openPosition);
  431. dropDown.SetParent(this);
  432. }
  433. private void Reset()
  434. {
  435. currentDirectory = ProjectLibrary.Root.Path;
  436. Refresh();
  437. }
  438. protected override void WindowResized(int width, int height)
  439. {
  440. base.WindowResized(width, height);
  441. Refresh();
  442. }
  443. }
  444. internal class ProjectDropDown : DropDownWindow
  445. {
  446. private ProjectWindow parent;
  447. public ProjectDropDown()
  448. :base(150, 30)
  449. { }
  450. internal void SetParent(ProjectWindow parent)
  451. {
  452. this.parent = parent;
  453. GUIToggleGroup group = new GUIToggleGroup();
  454. GUIToggle list16 = new GUIToggle("16", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  455. GUIToggle grid32 = new GUIToggle("32", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  456. GUIToggle grid48 = new GUIToggle("48", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  457. GUIToggle grid64 = new GUIToggle("64", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  458. ProjectViewType activeType = parent.ViewType;
  459. switch (activeType)
  460. {
  461. case ProjectViewType.List16:
  462. list16.ToggleOn();
  463. break;
  464. case ProjectViewType.Grid32:
  465. grid32.ToggleOn();
  466. break;
  467. case ProjectViewType.Grid48:
  468. grid48.ToggleOn();
  469. break;
  470. case ProjectViewType.Grid64:
  471. grid64.ToggleOn();
  472. break;
  473. }
  474. list16.OnToggled += (active) =>
  475. {
  476. if (active)
  477. ChangeViewType(ProjectViewType.List16);
  478. };
  479. grid32.OnToggled += (active) =>
  480. {
  481. if (active)
  482. ChangeViewType(ProjectViewType.Grid32);
  483. };
  484. grid48.OnToggled += (active) =>
  485. {
  486. if (active)
  487. ChangeViewType(ProjectViewType.Grid48);
  488. };
  489. grid64.OnToggled += (active) =>
  490. {
  491. if (active)
  492. ChangeViewType(ProjectViewType.Grid64);
  493. };
  494. GUILayoutY vertLayout = GUI.AddLayoutY();
  495. vertLayout.AddFlexibleSpace();
  496. GUILayoutX contentLayout = vertLayout.AddLayoutX();
  497. contentLayout.AddFlexibleSpace();
  498. contentLayout.AddElement(list16);
  499. contentLayout.AddElement(grid32);
  500. contentLayout.AddElement(grid48);
  501. contentLayout.AddElement(grid64);
  502. contentLayout.AddFlexibleSpace();
  503. vertLayout.AddFlexibleSpace();
  504. }
  505. private void ChangeViewType(ProjectViewType viewType)
  506. {
  507. parent.ViewType = viewType;
  508. }
  509. }
  510. }