ProjectWindow.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 = null;
  311. if (grid)
  312. {
  313. label = new GUILabel(entry.Name, EditorStyles.MultiLineLabel,
  314. GUIOption.FixedWidth(tileSize), GUIOption.FlexibleHeight(0, MAX_LABEL_HEIGHT));
  315. }
  316. else
  317. {
  318. label = new GUILabel(entry.Name);
  319. }
  320. entryLayout.AddElement(icon);
  321. entryLayout.AddElement(label);
  322. pathToGUIEntry[entry.Path] = new EntryGUI(icon, label);
  323. }
  324. private void CreateEntryOverlayGUI(GUIPanel overlayPanel, GUIPanel underlayPanel, EntryGUI gui, LibraryEntry entry)
  325. {
  326. // Add overlay button
  327. Rect2I entryButtonBounds = gui.icon.Bounds;
  328. Rect2I labelBounds = gui.label.Bounds;
  329. entryButtonBounds.x = MathEx.Min(entryButtonBounds.x, labelBounds.x);
  330. entryButtonBounds.y = MathEx.Min(entryButtonBounds.y, labelBounds.y);
  331. entryButtonBounds.width = MathEx.Max(entryButtonBounds.x + entryButtonBounds.width,
  332. labelBounds.x + labelBounds.width) - entryButtonBounds.x;
  333. entryButtonBounds.height = MathEx.Max(entryButtonBounds.y + entryButtonBounds.height,
  334. labelBounds.y + labelBounds.height) - entryButtonBounds.y;
  335. GUIButton overlayBtn = new GUIButton("", EditorStyles.Blank);
  336. overlayBtn.Bounds = entryButtonBounds;
  337. overlayBtn.OnClick += () => OnEntryClicked(entry.Path);
  338. overlayBtn.OnDoubleClick += () => OnEntryDoubleClicked(entry.Path);
  339. overlayBtn.SetContextMenu(entryContextMenu);
  340. overlayPanel.AddElement(overlayBtn);
  341. if (cutPaths.Contains(entry.Path))
  342. {
  343. gui.icon.SetTint(new Color(1.0f, 1.0f, 1.0f, 0.5f));
  344. }
  345. if (selectionPaths.Contains(entry.Path))
  346. {
  347. GUITexture underlay = new GUITexture(Builtin.WhiteTexture);
  348. underlay.Bounds = entryButtonBounds;
  349. underlay.SetTint(SELECTION_COLOR);
  350. underlayPanel.AddElement(underlay);
  351. }
  352. else if (pingPath == entry.Path)
  353. {
  354. GUITexture underlay = new GUITexture(Builtin.WhiteTexture);
  355. underlay.Bounds = entryButtonBounds;
  356. underlay.SetTint(PING_COLOR);
  357. underlayPanel.AddElement(underlay);
  358. }
  359. }
  360. private void OnEntryClicked(string path)
  361. {
  362. Select(new List<string> { path });
  363. Selection.resourcePaths = new string[] {path};
  364. Debug.Log("CLICKED " + Time.FrameNumber);
  365. }
  366. private void OnEntryDoubleClicked(string path)
  367. {
  368. Debug.Log("DOUBLE CLICK " + path);
  369. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  370. if (entry != null && entry.Type == LibraryEntryType.Directory)
  371. {
  372. EnterDirectory(path);
  373. }
  374. }
  375. private void OnCatchAllClicked()
  376. {
  377. Select(new List<string> { });
  378. Selection.resourcePaths = new string[] { };
  379. }
  380. private void CutSelection()
  381. {
  382. if (selectionPaths.Count > 0)
  383. Cut(selectionPaths);
  384. }
  385. private void CopySelection()
  386. {
  387. if (selectionPaths.Count > 0)
  388. Copy(selectionPaths);
  389. }
  390. private void DuplicateSelection()
  391. {
  392. if (selectionPaths.Count > 0)
  393. Duplicate(selectionPaths);
  394. }
  395. private void PasteToSelection()
  396. {
  397. DirectoryEntry selectedDirectory = null;
  398. if (selectionPaths.Count == 1)
  399. {
  400. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  401. if (entry != null && entry.Type == LibraryEntryType.Directory)
  402. selectedDirectory = (DirectoryEntry) entry;
  403. }
  404. if(selectedDirectory != null)
  405. Paste(selectedDirectory.Path);
  406. else
  407. Paste(currentDirectory);
  408. }
  409. private void OnSearchChanged(string newValue)
  410. {
  411. // TODO
  412. }
  413. private void ClearSearch()
  414. {
  415. // TODO
  416. }
  417. private void OpenOptionsWindow()
  418. {
  419. Vector2I openPosition;
  420. Rect2I buttonBounds = GUILayoutUtility.CalculateBounds(optionsButton, GUI);
  421. openPosition.x = buttonBounds.x + buttonBounds.width / 2;
  422. openPosition.y = buttonBounds.y + buttonBounds.height / 2;
  423. ProjectDropDown dropDown = DropDownWindow.Open<ProjectDropDown>(this, openPosition);
  424. dropDown.SetParent(this);
  425. }
  426. private void Reset()
  427. {
  428. currentDirectory = ProjectLibrary.Root.Path;
  429. Refresh();
  430. }
  431. protected override void WindowResized(int width, int height)
  432. {
  433. base.WindowResized(width, height);
  434. Refresh();
  435. }
  436. }
  437. internal class ProjectDropDown : DropDownWindow
  438. {
  439. private ProjectWindow parent;
  440. public ProjectDropDown()
  441. :base(100, 50)
  442. { }
  443. internal void SetParent(ProjectWindow parent)
  444. {
  445. this.parent = parent;
  446. GUIToggleGroup group = new GUIToggleGroup();
  447. GUIToggle list16 = new GUIToggle("16", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  448. GUIToggle grid32 = new GUIToggle("32", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  449. GUIToggle grid48 = new GUIToggle("48", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  450. GUIToggle grid64 = new GUIToggle("64", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  451. ProjectViewType activeType = parent.ViewType;
  452. switch (activeType)
  453. {
  454. case ProjectViewType.List16:
  455. list16.ToggleOn();
  456. break;
  457. case ProjectViewType.Grid32:
  458. grid32.ToggleOn();
  459. break;
  460. case ProjectViewType.Grid48:
  461. grid48.ToggleOn();
  462. break;
  463. case ProjectViewType.Grid64:
  464. grid64.ToggleOn();
  465. break;
  466. }
  467. list16.OnToggled += (active) =>
  468. {
  469. if (active)
  470. ChangeViewType(ProjectViewType.List16);
  471. };
  472. grid32.OnToggled += (active) =>
  473. {
  474. if (active)
  475. ChangeViewType(ProjectViewType.Grid32);
  476. };
  477. grid48.OnToggled += (active) =>
  478. {
  479. if (active)
  480. ChangeViewType(ProjectViewType.Grid48);
  481. };
  482. grid64.OnToggled += (active) =>
  483. {
  484. if (active)
  485. ChangeViewType(ProjectViewType.Grid64);
  486. };
  487. GUILayoutY vertLayout = GUI.AddLayoutY();
  488. vertLayout.AddFlexibleSpace();
  489. GUILayoutX contentLayout = vertLayout.AddLayoutX();
  490. contentLayout.AddFlexibleSpace();
  491. contentLayout.AddElement(list16);
  492. contentLayout.AddElement(grid32);
  493. contentLayout.AddElement(grid48);
  494. contentLayout.AddElement(grid64);
  495. contentLayout.AddFlexibleSpace();
  496. vertLayout.AddFlexibleSpace();
  497. }
  498. private void ChangeViewType(ProjectViewType viewType)
  499. {
  500. parent.ViewType = viewType;
  501. }
  502. }
  503. }