ProjectWindow.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. internal sealed class ProjectWindow : EditorWindow
  7. {
  8. private enum ViewType
  9. {
  10. Grid64, Grid48, Grid32, List16
  11. }
  12. private struct EntryGUI
  13. {
  14. public EntryGUI(GUITexture icon, GUILabel label)
  15. {
  16. this.icon = icon;
  17. this.label = label;
  18. }
  19. public GUITexture icon;
  20. public GUILabel label;
  21. }
  22. private const int GRID_ENTRY_SPACING = 15;
  23. private const int LIST_ENTRY_SPACING = 7;
  24. private const int MAX_LABEL_HEIGHT = 50;
  25. private static readonly Color PING_COLOR = Color.BansheeOrange;
  26. private static readonly Color SELECTION_COLOR = Color.DarkCyan;
  27. private bool hasContentFocus = false;
  28. private bool HasContentFocus { get { return HasFocus && hasContentFocus; } } // TODO - This is dummy and never set
  29. private ViewType viewType = ViewType.Grid32;
  30. private string currentDirectory = "";
  31. private List<string> selectionPaths = new List<string>();
  32. private string pingPath = "";
  33. private GUIScrollArea contentScrollArea;
  34. private GUIPanel scrollAreaPanel;
  35. private Dictionary<string, EntryGUI> pathToGUIEntry = new Dictionary<string, EntryGUI>();
  36. // Cut/Copy/Paste
  37. private List<string> copyPaths = new List<string>();
  38. private List<string> cutPaths = new List<string>();
  39. [MenuItem("Windows/Project", ButtonModifier.Ctrl, ButtonCode.P)]
  40. private static void OpenProjectWindow()
  41. {
  42. OpenWindow<ProjectWindow>();
  43. }
  44. private void OnInitialize()
  45. {
  46. ProjectLibrary.OnEntryAdded += OnEntryChanged;
  47. ProjectLibrary.OnEntryRemoved += OnEntryChanged;
  48. // TODO - Add search bar + options button with drop-down
  49. // TODO - Add directory bar + home button
  50. contentScrollArea = new GUIScrollArea(GUIOption.FlexibleWidth(), GUIOption.FlexibleHeight());
  51. GUI.AddElement(contentScrollArea);
  52. Reset();
  53. }
  54. public void Ping(Resource resource)
  55. {
  56. pingPath = ProjectLibrary.GetPath(resource);
  57. Refresh();
  58. ScrollToEntry(pingPath);
  59. }
  60. private void Select(List<string> paths)
  61. {
  62. selectionPaths = paths;
  63. pingPath = "";
  64. Refresh();
  65. }
  66. private void EnterDirectory(string directory)
  67. {
  68. currentDirectory = directory;
  69. pingPath = "";
  70. selectionPaths.Clear();
  71. Refresh();
  72. }
  73. private void Cut(IEnumerable<string> sourcePaths)
  74. {
  75. cutPaths.Clear();
  76. cutPaths.AddRange(sourcePaths);
  77. copyPaths.Clear();
  78. Refresh();
  79. }
  80. private void Copy(IEnumerable<string> sourcePaths)
  81. {
  82. copyPaths.Clear();
  83. copyPaths.AddRange(sourcePaths);
  84. cutPaths.Clear();
  85. Refresh();
  86. }
  87. private void Duplicate(IEnumerable<string> sourcePaths)
  88. {
  89. foreach (var source in sourcePaths)
  90. {
  91. int idx = 0;
  92. string destination;
  93. do
  94. {
  95. destination = source + "_" + idx;
  96. idx++;
  97. } while (!ProjectLibrary.Exists(destination));
  98. ProjectLibrary.Copy(source, destination);
  99. }
  100. }
  101. private void Paste(string destinationFolder)
  102. {
  103. if (copyPaths.Count > 0)
  104. {
  105. for (int i = 0; i < copyPaths.Count; i++)
  106. {
  107. string destination = Path.Combine(destinationFolder, Path.GetFileName(copyPaths[i]));
  108. ProjectLibrary.Copy(copyPaths[i], destination, true);
  109. }
  110. Refresh();
  111. }
  112. else if (cutPaths.Count > 0)
  113. {
  114. for (int i = 0; i < cutPaths.Count; i++)
  115. {
  116. string destination = Path.Combine(destinationFolder, Path.GetFileName(cutPaths[i]));
  117. ProjectLibrary.Move(cutPaths[i], destination, true);
  118. }
  119. cutPaths.Clear();
  120. Refresh();
  121. }
  122. }
  123. private void SetView(ViewType type)
  124. {
  125. viewType = type;
  126. Refresh();
  127. }
  128. private void EditorUpdate()
  129. {
  130. if (HasContentFocus)
  131. {
  132. if (Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl))
  133. {
  134. if (Input.IsButtonUp(ButtonCode.C))
  135. {
  136. if(selectionPaths.Count > 0)
  137. Copy(selectionPaths);
  138. }
  139. else if (Input.IsButtonUp(ButtonCode.X))
  140. {
  141. if (selectionPaths.Count > 0)
  142. Cut(selectionPaths);
  143. }
  144. else if (Input.IsButtonUp(ButtonCode.D))
  145. {
  146. if (selectionPaths.Count > 0)
  147. Duplicate(selectionPaths);
  148. }
  149. else if (Input.IsButtonUp(ButtonCode.V))
  150. {
  151. Paste(currentDirectory);
  152. }
  153. }
  154. }
  155. // TODO - Handle input, drag and drop and whatever else might be needed
  156. // TODO - Animate ping?
  157. // TODO - Automatically scroll window when dragging near border?
  158. // TODO - Drag and drop from Explorer should work to import an asset (i.e. DragAndDropArea)
  159. // - This should be something that should be enabled per editor window perhaps?
  160. }
  161. private void OnEntryChanged(string entry)
  162. {
  163. Refresh();
  164. }
  165. private void ScrollToEntry(string path)
  166. {
  167. Rect2I contentBounds = scrollAreaPanel.Bounds;
  168. Rect2I scrollAreaBounds = contentScrollArea.ContentBounds;
  169. EntryGUI entryGUI;
  170. if (!pathToGUIEntry.TryGetValue(path, out entryGUI))
  171. return;
  172. Rect2I entryBounds = entryGUI.icon.Bounds;
  173. float percent = (entryBounds.x - scrollAreaBounds.height * 0.5f) / contentBounds.height;
  174. percent = MathEx.Clamp01(percent);
  175. contentScrollArea.VerticalScroll = percent;
  176. }
  177. private SpriteTexture GetIcon(LibraryEntry entry)
  178. {
  179. if (entry.Type == LibraryEntryType.Directory)
  180. {
  181. return EditorBuiltin.FolderIcon;
  182. }
  183. else
  184. {
  185. FileEntry fileEntry = (FileEntry)entry;
  186. switch (fileEntry.ResType)
  187. {
  188. case ResourceType.Font:
  189. return EditorBuiltin.FontIcon;
  190. case ResourceType.Mesh:
  191. return EditorBuiltin.MeshIcon;
  192. case ResourceType.Texture:
  193. return EditorBuiltin.TextureIcon;
  194. case ResourceType.PlainText:
  195. return null; // TODO
  196. case ResourceType.ScriptCode:
  197. return null; // TODO
  198. case ResourceType.SpriteTexture:
  199. return null; // TODO
  200. case ResourceType.Shader:
  201. return null; // TODO
  202. case ResourceType.Material:
  203. return null; // TODO
  204. }
  205. }
  206. return null;
  207. }
  208. private void Refresh()
  209. {
  210. DirectoryEntry entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  211. if (entry == null)
  212. {
  213. Reset();
  214. return;
  215. }
  216. if (scrollAreaPanel != null)
  217. scrollAreaPanel.Destroy();
  218. pathToGUIEntry.Clear();
  219. scrollAreaPanel = contentScrollArea.Layout.AddPanel();
  220. GUIPanel contentPanel = scrollAreaPanel.AddPanel(1);
  221. GUIPanel contentOverlayPanel = scrollAreaPanel.AddPanel(0);
  222. GUIPanel contentUnderlayPanel = scrollAreaPanel.AddPanel(2);
  223. GUILayout contentLayout = contentPanel.AddLayoutY();
  224. Rect2I scrollBounds = contentScrollArea.Bounds;
  225. LibraryEntry[] childEntries = entry.Children;
  226. if (childEntries.Length == 0)
  227. return;
  228. if (viewType == ViewType.List16)
  229. {
  230. int tileSize = 16;
  231. for (int i = 0; i < childEntries.Length; i++)
  232. {
  233. LibraryEntry currentEntry = childEntries[i];
  234. CreateEntryGUI(contentLayout, tileSize, false, currentEntry);
  235. if (i != childEntries.Length - 1)
  236. contentLayout.AddSpace(LIST_ENTRY_SPACING);
  237. }
  238. contentLayout.AddFlexibleSpace();
  239. }
  240. else
  241. {
  242. int tileSize = 64;
  243. switch (viewType)
  244. {
  245. case ViewType.Grid64: tileSize = 64; break;
  246. case ViewType.Grid48: tileSize = 48; break;
  247. case ViewType.Grid32: tileSize = 32; break;
  248. }
  249. GUILayoutX rowLayout = contentLayout.AddLayoutX();
  250. rowLayout.AddFlexibleSpace();
  251. int currentWidth = GRID_ENTRY_SPACING * 2;
  252. bool addedAny = false;
  253. for (int i = 0; i < childEntries.Length; i++)
  254. {
  255. if (currentWidth >= scrollBounds.width && addedAny) // We force at least one entry per row, even if it doesn't fit
  256. {
  257. rowLayout = contentLayout.AddLayoutX();
  258. contentLayout.AddFlexibleSpace();
  259. rowLayout.AddFlexibleSpace();
  260. currentWidth = GRID_ENTRY_SPACING * 2;
  261. }
  262. LibraryEntry currentEntry = childEntries[i];
  263. CreateEntryGUI(rowLayout, tileSize, true, currentEntry);
  264. rowLayout.AddFlexibleSpace();
  265. addedAny = true;
  266. currentWidth += tileSize + GRID_ENTRY_SPACING;
  267. }
  268. }
  269. for (int i = 0; i < childEntries.Length; i++)
  270. {
  271. LibraryEntry currentEntry = childEntries[i];
  272. CreateEntryOverlayGUI(contentOverlayPanel, contentUnderlayPanel, pathToGUIEntry[currentEntry.Path], currentEntry);
  273. }
  274. Rect2I contentBounds = contentLayout.Bounds;
  275. GUIButton catchAll = new GUIButton("", EditorStyles.Blank);
  276. catchAll.Bounds = contentBounds;
  277. catchAll.OnClick += OnCatchAllClicked;
  278. contentUnderlayPanel.AddElement(catchAll);
  279. Debug.Log("REFRESHED " + Time.FrameNumber);
  280. }
  281. private void CreateEntryGUI(GUILayout parentLayout, int tileSize, bool grid, LibraryEntry entry)
  282. {
  283. GUILayout entryLayout;
  284. if(grid)
  285. entryLayout = parentLayout.AddLayoutY();
  286. else
  287. entryLayout = parentLayout.AddLayoutX();
  288. SpriteTexture iconTexture = GetIcon(entry);
  289. GUITexture icon = new GUITexture(iconTexture, GUIImageScaleMode.ScaleToFit,
  290. true, GUIOption.FixedHeight(tileSize), GUIOption.FixedWidth(tileSize));
  291. GUILabel label = new GUILabel(entry.Name, EditorStyles.MultiLineLabel,
  292. GUIOption.FixedWidth(tileSize), GUIOption.FlexibleHeight(0, MAX_LABEL_HEIGHT));
  293. entryLayout.AddElement(icon);
  294. entryLayout.AddElement(label);
  295. pathToGUIEntry[entry.Path] = new EntryGUI(icon, label);
  296. }
  297. private void CreateEntryOverlayGUI(GUIPanel overlayPanel, GUIPanel underlayPanel, EntryGUI gui, LibraryEntry entry)
  298. {
  299. // Add overlay button
  300. Rect2I entryButtonBounds = gui.icon.Bounds;
  301. Rect2I labelBounds = gui.label.Bounds;
  302. entryButtonBounds.x = MathEx.Min(entryButtonBounds.x, labelBounds.x);
  303. entryButtonBounds.y = MathEx.Min(entryButtonBounds.y, labelBounds.y);
  304. entryButtonBounds.width = MathEx.Max(entryButtonBounds.x + entryButtonBounds.width,
  305. labelBounds.x + labelBounds.width) - entryButtonBounds.x;
  306. entryButtonBounds.height = MathEx.Max(entryButtonBounds.y + entryButtonBounds.height,
  307. labelBounds.y + labelBounds.height) - entryButtonBounds.y;
  308. GUIButton overlayBtn = new GUIButton("", EditorStyles.Blank);
  309. overlayBtn.Bounds = entryButtonBounds;
  310. overlayBtn.OnClick += () => OnEntryClicked(entry.Path);
  311. overlayBtn.OnDoubleClick += () => OnEntryDoubleClicked(entry.Path);
  312. overlayPanel.AddElement(overlayBtn);
  313. if (cutPaths.Contains(entry.Path))
  314. {
  315. gui.icon.SetTint(new Color(1.0f, 1.0f, 1.0f, 0.5f));
  316. }
  317. if (selectionPaths.Contains(entry.Path))
  318. {
  319. GUITexture underlay = new GUITexture(Builtin.WhiteTexture);
  320. underlay.Bounds = entryButtonBounds;
  321. underlay.SetTint(SELECTION_COLOR);
  322. underlayPanel.AddElement(underlay);
  323. }
  324. else if (pingPath == entry.Path)
  325. {
  326. GUITexture underlay = new GUITexture(Builtin.WhiteTexture);
  327. underlay.Bounds = entryButtonBounds;
  328. underlay.SetTint(PING_COLOR);
  329. underlayPanel.AddElement(underlay);
  330. }
  331. }
  332. private void OnEntryClicked(string path)
  333. {
  334. Select(new List<string> { path });
  335. Selection.resourcePaths = new string[] {path};
  336. Debug.Log("CLICKED " + Time.FrameNumber);
  337. }
  338. private void OnEntryDoubleClicked(string path)
  339. {
  340. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  341. if (entry != null && entry.Type == LibraryEntryType.Directory)
  342. {
  343. EnterDirectory(path);
  344. }
  345. }
  346. private void OnCatchAllClicked()
  347. {
  348. Select(new List<string> { });
  349. Selection.resourcePaths = new string[] { };
  350. }
  351. private void Reset()
  352. {
  353. currentDirectory = ProjectLibrary.Root.Path;
  354. Refresh();
  355. }
  356. protected override void WindowResized(int width, int height)
  357. {
  358. base.WindowResized(width, height);
  359. Refresh();
  360. }
  361. }
  362. }