ProjectWindow.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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.Grid32;
  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 currentWidth = GRID_ENTRY_SPACING * 2;
  270. bool addedAny = false;
  271. for (int i = 0; i < childEntries.Length; i++)
  272. {
  273. if (currentWidth >= scrollBounds.width && addedAny) // We force at least one entry per row, even if it doesn't fit
  274. {
  275. rowLayout = contentLayout.AddLayoutX();
  276. contentLayout.AddFlexibleSpace();
  277. rowLayout.AddFlexibleSpace();
  278. currentWidth = GRID_ENTRY_SPACING * 2;
  279. }
  280. LibraryEntry currentEntry = childEntries[i];
  281. CreateEntryGUI(rowLayout, tileSize, true, currentEntry);
  282. rowLayout.AddFlexibleSpace();
  283. addedAny = true;
  284. currentWidth += tileSize + GRID_ENTRY_SPACING;
  285. }
  286. }
  287. for (int i = 0; i < childEntries.Length; i++)
  288. {
  289. LibraryEntry currentEntry = childEntries[i];
  290. CreateEntryOverlayGUI(contentOverlayPanel, contentUnderlayPanel, pathToGUIEntry[currentEntry.Path], currentEntry);
  291. }
  292. Rect2I contentBounds = contentLayout.Bounds;
  293. GUIButton catchAll = new GUIButton("", EditorStyles.Blank);
  294. catchAll.Bounds = contentBounds;
  295. catchAll.OnClick += OnCatchAllClicked;
  296. catchAll.SetContextMenu(entryContextMenu);
  297. contentUnderlayPanel.AddElement(catchAll);
  298. Debug.Log("REFRESHED " + Time.FrameNumber);
  299. }
  300. private void CreateEntryGUI(GUILayout parentLayout, int tileSize, bool grid, LibraryEntry entry)
  301. {
  302. GUILayout entryLayout;
  303. if(grid)
  304. entryLayout = parentLayout.AddLayoutY();
  305. else
  306. entryLayout = parentLayout.AddLayoutX();
  307. SpriteTexture iconTexture = GetIcon(entry);
  308. GUITexture icon = new GUITexture(iconTexture, GUIImageScaleMode.ScaleToFit,
  309. true, GUIOption.FixedHeight(tileSize), GUIOption.FixedWidth(tileSize));
  310. GUILabel label = new GUILabel(entry.Name, EditorStyles.MultiLineLabel,
  311. GUIOption.FixedWidth(tileSize), GUIOption.FlexibleHeight(0, MAX_LABEL_HEIGHT));
  312. entryLayout.AddElement(icon);
  313. entryLayout.AddElement(label);
  314. pathToGUIEntry[entry.Path] = new EntryGUI(icon, label);
  315. }
  316. private void CreateEntryOverlayGUI(GUIPanel overlayPanel, GUIPanel underlayPanel, EntryGUI gui, LibraryEntry entry)
  317. {
  318. // Add overlay button
  319. Rect2I entryButtonBounds = gui.icon.Bounds;
  320. Rect2I labelBounds = gui.label.Bounds;
  321. entryButtonBounds.x = MathEx.Min(entryButtonBounds.x, labelBounds.x);
  322. entryButtonBounds.y = MathEx.Min(entryButtonBounds.y, labelBounds.y);
  323. entryButtonBounds.width = MathEx.Max(entryButtonBounds.x + entryButtonBounds.width,
  324. labelBounds.x + labelBounds.width) - entryButtonBounds.x;
  325. entryButtonBounds.height = MathEx.Max(entryButtonBounds.y + entryButtonBounds.height,
  326. labelBounds.y + labelBounds.height) - entryButtonBounds.y;
  327. GUIButton overlayBtn = new GUIButton("", EditorStyles.Blank);
  328. overlayBtn.Bounds = entryButtonBounds;
  329. overlayBtn.OnClick += () => OnEntryClicked(entry.Path);
  330. overlayBtn.OnDoubleClick += () => OnEntryDoubleClicked(entry.Path);
  331. overlayBtn.SetContextMenu(entryContextMenu);
  332. overlayPanel.AddElement(overlayBtn);
  333. if (cutPaths.Contains(entry.Path))
  334. {
  335. gui.icon.SetTint(new Color(1.0f, 1.0f, 1.0f, 0.5f));
  336. }
  337. if (selectionPaths.Contains(entry.Path))
  338. {
  339. GUITexture underlay = new GUITexture(Builtin.WhiteTexture);
  340. underlay.Bounds = entryButtonBounds;
  341. underlay.SetTint(SELECTION_COLOR);
  342. underlayPanel.AddElement(underlay);
  343. }
  344. else if (pingPath == entry.Path)
  345. {
  346. GUITexture underlay = new GUITexture(Builtin.WhiteTexture);
  347. underlay.Bounds = entryButtonBounds;
  348. underlay.SetTint(PING_COLOR);
  349. underlayPanel.AddElement(underlay);
  350. }
  351. }
  352. private void OnEntryClicked(string path)
  353. {
  354. Select(new List<string> { path });
  355. Selection.resourcePaths = new string[] {path};
  356. Debug.Log("CLICKED " + Time.FrameNumber);
  357. }
  358. private void OnEntryDoubleClicked(string path)
  359. {
  360. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  361. if (entry != null && entry.Type == LibraryEntryType.Directory)
  362. {
  363. EnterDirectory(path);
  364. }
  365. }
  366. private void OnCatchAllClicked()
  367. {
  368. Select(new List<string> { });
  369. Selection.resourcePaths = new string[] { };
  370. }
  371. private void CutSelection()
  372. {
  373. if (selectionPaths.Count > 0)
  374. Cut(selectionPaths);
  375. }
  376. private void CopySelection()
  377. {
  378. if (selectionPaths.Count > 0)
  379. Copy(selectionPaths);
  380. }
  381. private void DuplicateSelection()
  382. {
  383. if (selectionPaths.Count > 0)
  384. Duplicate(selectionPaths);
  385. }
  386. private void PasteToSelection()
  387. {
  388. DirectoryEntry selectedDirectory = null;
  389. if (selectionPaths.Count == 1)
  390. {
  391. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  392. if (entry != null && entry.Type == LibraryEntryType.Directory)
  393. selectedDirectory = (DirectoryEntry) entry;
  394. }
  395. if(selectedDirectory != null)
  396. Paste(selectedDirectory.Path);
  397. else
  398. Paste(currentDirectory);
  399. }
  400. private void OnSearchChanged(string newValue)
  401. {
  402. // TODO
  403. }
  404. private void ClearSearch()
  405. {
  406. // TODO
  407. }
  408. private void OpenOptionsWindow()
  409. {
  410. Vector2I openPosition;
  411. Rect2I buttonBounds = optionsButton.Bounds;
  412. openPosition.x = buttonBounds.x + buttonBounds.width / 2;
  413. openPosition.y = buttonBounds.y + buttonBounds.height / 2;
  414. ProjectDropDown dropDown = DropDownWindow.Open<ProjectDropDown>(this, openPosition);
  415. dropDown.SetParent(this);
  416. }
  417. private void Reset()
  418. {
  419. currentDirectory = ProjectLibrary.Root.Path;
  420. Refresh();
  421. }
  422. protected override void WindowResized(int width, int height)
  423. {
  424. base.WindowResized(width, height);
  425. Refresh();
  426. }
  427. }
  428. internal class ProjectDropDown : DropDownWindow
  429. {
  430. private ProjectWindow parent;
  431. public ProjectDropDown()
  432. :base(100, 50)
  433. { }
  434. internal void SetParent(ProjectWindow parent)
  435. {
  436. this.parent = parent;
  437. GUIToggleGroup group = new GUIToggleGroup();
  438. GUIToggle list16 = new GUIToggle("16", group);
  439. GUIToggle grid32 = new GUIToggle("32", group);
  440. GUIToggle grid48 = new GUIToggle("32", group);
  441. GUIToggle grid64 = new GUIToggle("64", group);
  442. ProjectViewType activeType = parent.ViewType;
  443. switch (activeType)
  444. {
  445. case ProjectViewType.List16:
  446. list16.ToggleOn();
  447. break;
  448. case ProjectViewType.Grid32:
  449. grid32.ToggleOn();
  450. break;
  451. case ProjectViewType.Grid48:
  452. grid48.ToggleOn();
  453. break;
  454. case ProjectViewType.Grid64:
  455. grid64.ToggleOn();
  456. break;
  457. }
  458. list16.OnToggled += (active) =>
  459. {
  460. if (active)
  461. ChangeViewType(ProjectViewType.List16);
  462. };
  463. grid32.OnToggled += (active) =>
  464. {
  465. if (active)
  466. ChangeViewType(ProjectViewType.Grid32);
  467. };
  468. grid48.OnToggled += (active) =>
  469. {
  470. if (active)
  471. ChangeViewType(ProjectViewType.Grid48);
  472. };
  473. grid64.OnToggled += (active) =>
  474. {
  475. if (active)
  476. ChangeViewType(ProjectViewType.Grid64);
  477. };
  478. GUILayoutX contentLayout = GUI.AddLayoutX();
  479. contentLayout.AddElement(list16);
  480. contentLayout.AddElement(grid32);
  481. contentLayout.AddElement(grid48);
  482. contentLayout.AddElement(grid64);
  483. }
  484. private void ChangeViewType(ProjectViewType viewType)
  485. {
  486. parent.ViewType = viewType;
  487. }
  488. }
  489. }