ProjectWindow.cs 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  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 const int GRID_ENTRY_SPACING = 15;
  14. private const int LIST_ENTRY_SPACING = 7;
  15. private const int MAX_LABEL_HEIGHT = 50;
  16. private const int MIN_HORZ_SPACING = 8;
  17. private const int DRAG_SCROLL_HEIGHT = 20;
  18. private const int DRAG_SCROLL_AMOUNT_PER_SECOND = 100;
  19. private const int FOLDER_BUTTON_WIDTH = 20;
  20. private const int FOLDER_SEPARATOR_WIDTH = 7;
  21. private const int SELECTION_EXTRA_WIDTH = 3;
  22. private static readonly Color PING_COLOR = Color.BansheeOrange;
  23. private static readonly Color SELECTION_COLOR = Color.DarkCyan;
  24. private static readonly Color HOVER_COLOR = new Color(Color.DarkCyan.r, Color.DarkCyan.g, Color.DarkCyan.b, 0.5f);
  25. private static readonly Color CUT_COLOR = new Color(1.0f, 1.0f, 1.0f, 0.5f);
  26. private bool hasContentFocus = false;
  27. private bool HasContentFocus { get { return HasFocus && hasContentFocus; } }
  28. private string searchQuery;
  29. private bool IsSearchActive { get { return !string.IsNullOrEmpty(searchQuery); } }
  30. private ProjectViewType viewType = ProjectViewType.Grid32;
  31. private bool requiresRefresh;
  32. private string currentDirectory = "";
  33. private List<string> selectionPaths = new List<string>();
  34. private int selectionAnchorStart = -1;
  35. private int selectionAnchorEnd = -1;
  36. private string pingPath = "";
  37. private string hoverHighlightPath = "";
  38. private ContentInfo contentInfo;
  39. private GUIScrollArea contentScrollArea;
  40. private GUIPanel scrollAreaPanel;
  41. private GUILayoutX searchBarLayout;
  42. private GUIButton optionsButton;
  43. private GUILayout folderBarLayout;
  44. private GUILayout folderListLayout;
  45. private GUITextField searchField;
  46. private GUITexture dragSelection;
  47. private ContextMenu entryContextMenu;
  48. private ProjectDropTarget dropTarget;
  49. private List<ElementEntry> entries = new List<ElementEntry>();
  50. private Dictionary<string, ElementEntry> entryLookup = new Dictionary<string, ElementEntry>();
  51. private int autoScrollAmount;
  52. private bool isDraggingSelection;
  53. private Vector2I dragSelectionStart;
  54. private Vector2I dragSelectionEnd;
  55. private ElementEntry inProgressRenameElement;
  56. // Cut/Copy/Paste
  57. private List<string> copyPaths = new List<string>();
  58. private List<string> cutPaths = new List<string>();
  59. internal ProjectViewType ViewType
  60. {
  61. get { return viewType; }
  62. set { viewType = value; Refresh(); }
  63. }
  64. [MenuItem("Windows/Project", ButtonModifier.CtrlAlt, ButtonCode.P)]
  65. private static void OpenProjectWindow()
  66. {
  67. OpenWindow<ProjectWindow>();
  68. }
  69. protected override LocString GetDisplayName()
  70. {
  71. return "Project";
  72. }
  73. private void OnInitialize()
  74. {
  75. ProjectLibrary.OnEntryAdded += OnEntryChanged;
  76. ProjectLibrary.OnEntryRemoved += OnEntryChanged;
  77. GUILayoutY contentLayout = GUI.AddLayoutY();
  78. searchBarLayout = contentLayout.AddLayoutX();
  79. searchField = new GUITextField();
  80. searchField.OnChanged += OnSearchChanged;
  81. GUIButton clearSearchBtn = new GUIButton("C");
  82. clearSearchBtn.OnClick += ClearSearch;
  83. clearSearchBtn.SetWidth(40);
  84. optionsButton = new GUIButton("O");
  85. optionsButton.OnClick += OpenOptionsWindow;
  86. optionsButton.SetWidth(40);
  87. searchBarLayout.AddElement(searchField);
  88. searchBarLayout.AddElement(clearSearchBtn);
  89. searchBarLayout.AddElement(optionsButton);
  90. folderBarLayout = contentLayout.AddLayoutX();
  91. GUIButton homeButton = new GUIButton("H", GUIOption.FixedWidth(FOLDER_BUTTON_WIDTH));
  92. homeButton.OnClick += OnHomeClicked;
  93. GUIButton upButton = new GUIButton("U", GUIOption.FixedWidth(FOLDER_BUTTON_WIDTH));
  94. upButton.OnClick += OnUpClicked;
  95. folderBarLayout.AddElement(homeButton);
  96. folderBarLayout.AddElement(upButton);
  97. folderBarLayout.AddSpace(10);
  98. contentScrollArea = new GUIScrollArea(GUIOption.FlexibleWidth(), GUIOption.FlexibleHeight());
  99. contentLayout.AddElement(contentScrollArea);
  100. contentLayout.AddFlexibleSpace();
  101. entryContextMenu = new ContextMenu();
  102. entryContextMenu.AddItem("Rename", RenameSelection, new ShortcutKey(ButtonModifier.None, ButtonCode.F2));
  103. entryContextMenu.AddSeparator("");
  104. entryContextMenu.AddItem("Cut", CutSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.X));
  105. entryContextMenu.AddItem("Copy", CopySelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.C));
  106. entryContextMenu.AddItem("Duplicate", DuplicateSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.D));
  107. entryContextMenu.AddItem("Paste", PasteToSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.V));
  108. entryContextMenu.AddSeparator("");
  109. entryContextMenu.AddItem("Delete", DeleteSelection, new ShortcutKey(ButtonModifier.None, ButtonCode.Delete));
  110. Reset();
  111. dropTarget = new ProjectDropTarget(this);
  112. dropTarget.Bounds = contentScrollArea.Bounds;
  113. dropTarget.OnStart += DoOnDragStart;
  114. dropTarget.OnDrag += DoOnDragMove;
  115. dropTarget.OnLeave += DoOnDragLeave;
  116. dropTarget.OnDropResource += DoOnResourceDragDropped;
  117. dropTarget.OnDropSceneObject += DoOnSceneObjectDragDropped;
  118. dropTarget.OnEnd += DoOnDragEnd;
  119. }
  120. private ElementEntry FindElementAt(Vector2I windowPos)
  121. {
  122. Vector2I scrollPos = WindowToScrollAreaCoords(windowPos);
  123. foreach (var element in entries)
  124. {
  125. if (element.bounds.Contains(scrollPos))
  126. return element;
  127. }
  128. return null;
  129. }
  130. private ElementEntry[] FindElementsOverlapping(Rect2I scrollBounds)
  131. {
  132. List<ElementEntry> elements = new List<ElementEntry>();
  133. foreach (var element in entries)
  134. {
  135. if(element.Bounds.Overlaps(scrollBounds))
  136. elements.Add(element);
  137. }
  138. return elements.ToArray();
  139. }
  140. private void DoOnDragStart(Vector2I windowPos)
  141. {
  142. ElementEntry underCursorElem = FindElementAt(windowPos);
  143. if (underCursorElem == null || !selectionPaths.Contains(underCursorElem.path))
  144. {
  145. StartDragSelection(windowPos);
  146. return;
  147. }
  148. ResourceDragDropData dragDropData = new ResourceDragDropData(selectionPaths.ToArray());
  149. DragDrop.StartDrag(dragDropData);
  150. }
  151. private void DoOnDragMove(Vector2I windowPos)
  152. {
  153. // Auto-scroll
  154. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  155. int scrollAreaTop = scrollAreaBounds.y;
  156. int scrollAreaBottom = scrollAreaBounds.y + scrollAreaBounds.height;
  157. if (windowPos.y > scrollAreaTop && windowPos.y <= (scrollAreaTop + DRAG_SCROLL_HEIGHT))
  158. autoScrollAmount = -DRAG_SCROLL_AMOUNT_PER_SECOND;
  159. else if (windowPos.y >= (scrollAreaBottom - DRAG_SCROLL_HEIGHT) && windowPos.y < scrollAreaBottom)
  160. autoScrollAmount = DRAG_SCROLL_AMOUNT_PER_SECOND;
  161. else
  162. autoScrollAmount = 0;
  163. // Selection box
  164. if (UpdateDragSelection(windowPos))
  165. return;
  166. // Drag and drop (hover element under cursor)
  167. ElementEntry underCursorElem = FindElementAt(windowPos);
  168. if (underCursorElem == null)
  169. {
  170. ClearHoverHighlight();
  171. }
  172. else
  173. {
  174. if (underCursorElem.path != hoverHighlightPath)
  175. {
  176. ClearHoverHighlight();
  177. hoverHighlightPath = underCursorElem.path;
  178. underCursorElem.MarkAsHovered(true);
  179. }
  180. }
  181. }
  182. private void DoOnDragLeave()
  183. {
  184. ClearHoverHighlight();
  185. autoScrollAmount = 0;
  186. }
  187. private void DoOnResourceDragDropped(Vector2I windowPos, string[] paths)
  188. {
  189. ClearHoverHighlight();
  190. autoScrollAmount = 0;
  191. if (EndDragSelection())
  192. return;
  193. string resourceDir = ProjectLibrary.ResourceFolder;
  194. string destinationFolder = Path.Combine(resourceDir, currentDirectory);
  195. ElementEntry underCursorElement = FindElementAt(windowPos);
  196. if (underCursorElement != null)
  197. {
  198. LibraryEntry entry = ProjectLibrary.GetEntry(underCursorElement.path);
  199. if (entry != null && entry.Type == LibraryEntryType.Directory)
  200. destinationFolder = Path.Combine(resourceDir, entry.Path);
  201. }
  202. if (paths != null)
  203. {
  204. foreach (var path in paths)
  205. {
  206. if (path == null)
  207. continue;
  208. string absolutePath = path;
  209. if (!Path.IsPathRooted(absolutePath))
  210. absolutePath = Path.Combine(resourceDir, path);
  211. if (string.IsNullOrEmpty(absolutePath))
  212. continue;
  213. if (PathEx.IsPartOf(destinationFolder, absolutePath) || PathEx.Compare(absolutePath, destinationFolder))
  214. continue;
  215. string pathTail = PathEx.GetTail(absolutePath);
  216. string destination = Path.Combine(destinationFolder, pathTail);
  217. bool doCopy = !ProjectLibrary.Exists(path);
  218. if (Directory.Exists(path))
  219. {
  220. if (doCopy)
  221. DirectoryEx.Copy(path, GetUniquePath(destination));
  222. else
  223. DirectoryEx.Move(path, GetUniquePath(destination));
  224. }
  225. else if (File.Exists(path))
  226. {
  227. if (doCopy)
  228. FileEx.Copy(path, GetUniquePath(destination));
  229. else
  230. FileEx.Move(path, GetUniquePath(destination));
  231. }
  232. ProjectLibrary.Refresh();
  233. }
  234. }
  235. }
  236. private void DoOnSceneObjectDragDropped(Vector2I windowPos, SceneObject[] objects)
  237. {
  238. ClearHoverHighlight();
  239. autoScrollAmount = 0;
  240. if (EndDragSelection())
  241. return;
  242. string resourceDir = ProjectLibrary.ResourceFolder;
  243. string destinationFolder = Path.Combine(resourceDir, currentDirectory);
  244. ElementEntry underCursorElement = FindElementAt(windowPos);
  245. if (underCursorElement != null)
  246. {
  247. LibraryEntry entry = ProjectLibrary.GetEntry(underCursorElement.path);
  248. if (entry != null && entry.Type == LibraryEntryType.Directory)
  249. destinationFolder = Path.Combine(resourceDir, entry.Path);
  250. }
  251. if (objects != null)
  252. {
  253. foreach (var so in objects)
  254. {
  255. if (so == null)
  256. continue;
  257. Prefab newPrefab = new Prefab(so);
  258. string destination = GetUniquePath(Path.Combine(destinationFolder, so.Name + ".prefab"));
  259. ProjectLibrary.Create(newPrefab, destination);
  260. ProjectLibrary.Refresh();
  261. }
  262. }
  263. }
  264. private void DoOnDragEnd(Vector2I windowPos)
  265. {
  266. EndDragSelection();
  267. }
  268. private void ClearHoverHighlight()
  269. {
  270. if (!string.IsNullOrEmpty(hoverHighlightPath))
  271. {
  272. ElementEntry previousUnderCursorElem;
  273. if (entryLookup.TryGetValue(hoverHighlightPath, out previousUnderCursorElem))
  274. previousUnderCursorElem.MarkAsHovered(false);
  275. }
  276. hoverHighlightPath = "";
  277. }
  278. public void Ping(Resource resource)
  279. {
  280. if (!string.IsNullOrEmpty(pingPath))
  281. {
  282. ElementEntry entry;
  283. if (entryLookup.TryGetValue(pingPath, out entry))
  284. entry.MarkAsPinged(false);
  285. }
  286. if (resource != null)
  287. pingPath = ProjectLibrary.GetPath(resource);
  288. else
  289. pingPath = "";
  290. if (!string.IsNullOrEmpty(pingPath))
  291. {
  292. ElementEntry entry;
  293. if (entryLookup.TryGetValue(pingPath, out entry))
  294. entry.MarkAsPinged(true);
  295. ScrollToEntry(pingPath);
  296. }
  297. }
  298. private void DeselectAll()
  299. {
  300. SetSelection(new List<string>());
  301. selectionAnchorStart = -1;
  302. selectionAnchorEnd = -1;
  303. }
  304. private void Select(string path)
  305. {
  306. ElementEntry entry;
  307. if (!entryLookup.TryGetValue(path, out entry))
  308. return;
  309. bool ctrlDown = Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl);
  310. bool shiftDown = Input.IsButtonHeld(ButtonCode.LeftShift) || Input.IsButtonHeld(ButtonCode.RightShift);
  311. if (shiftDown)
  312. {
  313. if (selectionAnchorStart != -1 && selectionAnchorStart < entries.Count)
  314. {
  315. int start = Math.Min(entry.index, selectionAnchorStart);
  316. int end = Math.Max(entry.index, selectionAnchorStart);
  317. List<string> newSelection = new List<string>();
  318. for(int i = start; i <= end; i++)
  319. newSelection.Add(entries[i].path);
  320. SetSelection(newSelection);
  321. selectionAnchorEnd = entry.index;
  322. }
  323. else
  324. {
  325. SetSelection(new List<string>() {path});
  326. selectionAnchorStart = entry.index;
  327. selectionAnchorEnd = entry.index;
  328. }
  329. }
  330. else if (ctrlDown)
  331. {
  332. List<string> newSelection = new List<string>(selectionPaths);
  333. if (selectionPaths.Contains(path))
  334. {
  335. newSelection.Remove(path);
  336. if (newSelection.Count == 0)
  337. DeselectAll();
  338. else
  339. {
  340. if (selectionAnchorStart == entry.index)
  341. {
  342. ElementEntry newAnchorEntry;
  343. if (!entryLookup.TryGetValue(newSelection[0], out newAnchorEntry))
  344. selectionAnchorStart = -1;
  345. else
  346. selectionAnchorStart = newAnchorEntry.index;
  347. }
  348. if (selectionAnchorEnd == entry.index)
  349. {
  350. ElementEntry newAnchorEntry;
  351. if (!entryLookup.TryGetValue(newSelection[newSelection.Count - 1], out newAnchorEntry))
  352. selectionAnchorEnd = -1;
  353. else
  354. selectionAnchorEnd = newAnchorEntry.index;
  355. }
  356. SetSelection(newSelection);
  357. }
  358. }
  359. else
  360. {
  361. newSelection.Add(path);
  362. SetSelection(newSelection);
  363. selectionAnchorEnd = entry.index;
  364. }
  365. }
  366. else
  367. {
  368. SetSelection(new List<string>() {path});
  369. selectionAnchorStart = entry.index;
  370. selectionAnchorEnd = entry.index;
  371. }
  372. }
  373. private void MoveSelection(MoveDirection dir)
  374. {
  375. string newPath = "";
  376. if (selectionPaths.Count == 0 || selectionAnchorEnd == -1)
  377. {
  378. // Nothing is selected so we arbitrarily select first or last element
  379. if (entries.Count > 0)
  380. {
  381. switch (dir)
  382. {
  383. case MoveDirection.Left:
  384. case MoveDirection.Up:
  385. newPath = entries[entries.Count - 1].path;
  386. break;
  387. case MoveDirection.Right:
  388. case MoveDirection.Down:
  389. newPath = entries[0].path;
  390. break;
  391. }
  392. }
  393. }
  394. else
  395. {
  396. switch (dir)
  397. {
  398. case MoveDirection.Left:
  399. if (selectionAnchorEnd - 1 >= 0)
  400. newPath = entries[selectionAnchorEnd - 1].path;
  401. break;
  402. case MoveDirection.Up:
  403. if (selectionAnchorEnd - contentInfo.elementsPerRow >= 0)
  404. newPath = entries[selectionAnchorEnd - contentInfo.elementsPerRow].path;
  405. break;
  406. case MoveDirection.Right:
  407. if (selectionAnchorEnd + 1 < entries.Count)
  408. newPath = entries[selectionAnchorEnd + 1].path;
  409. break;
  410. case MoveDirection.Down:
  411. if (selectionAnchorEnd + contentInfo.elementsPerRow < entries.Count)
  412. newPath = entries[selectionAnchorEnd + contentInfo.elementsPerRow].path;
  413. break;
  414. }
  415. }
  416. if (!string.IsNullOrEmpty(newPath))
  417. {
  418. Select(newPath);
  419. ScrollToEntry(newPath);
  420. }
  421. }
  422. private void SetSelection(List<string> paths)
  423. {
  424. if (selectionPaths != null)
  425. {
  426. foreach (var path in selectionPaths)
  427. {
  428. ElementEntry entry;
  429. if (entryLookup.TryGetValue(path, out entry))
  430. entry.MarkAsSelected(false);
  431. }
  432. }
  433. selectionPaths = paths;
  434. if (selectionPaths != null)
  435. {
  436. foreach (var path in selectionPaths)
  437. {
  438. ElementEntry entry;
  439. if (entryLookup.TryGetValue(path, out entry))
  440. entry.MarkAsSelected(true);
  441. }
  442. }
  443. Ping(null);
  444. StopRename();
  445. if (selectionPaths != null)
  446. Selection.resourcePaths = selectionPaths.ToArray();
  447. else
  448. Selection.resourcePaths = new string[0];
  449. }
  450. private void EnterDirectory(string directory)
  451. {
  452. currentDirectory = directory;
  453. DeselectAll();
  454. Refresh();
  455. }
  456. private void Cut(IEnumerable<string> sourcePaths)
  457. {
  458. foreach (var path in cutPaths)
  459. {
  460. ElementEntry entry;
  461. if (entryLookup.TryGetValue(path, out entry))
  462. entry.MarkAsCut(false);
  463. }
  464. cutPaths.Clear();
  465. cutPaths.AddRange(sourcePaths);
  466. foreach (var path in cutPaths)
  467. {
  468. ElementEntry entry;
  469. if (entryLookup.TryGetValue(path, out entry))
  470. entry.MarkAsCut(true);
  471. }
  472. copyPaths.Clear();
  473. }
  474. private void Copy(IEnumerable<string> sourcePaths)
  475. {
  476. copyPaths.Clear();
  477. copyPaths.AddRange(sourcePaths);
  478. foreach (var path in cutPaths)
  479. {
  480. ElementEntry entry;
  481. if (entryLookup.TryGetValue(path, out entry))
  482. entry.MarkAsCut(false);
  483. }
  484. cutPaths.Clear();
  485. }
  486. private void Duplicate(IEnumerable<string> sourcePaths)
  487. {
  488. foreach (var source in sourcePaths)
  489. {
  490. if (Directory.Exists(source))
  491. DirectoryEx.Copy(source, GetUniquePath(source));
  492. else if (File.Exists(source))
  493. FileEx.Copy(source, GetUniquePath(source));
  494. ProjectLibrary.Refresh();
  495. }
  496. }
  497. private void Paste(string destinationFolder)
  498. {
  499. if (copyPaths.Count > 0)
  500. {
  501. for (int i = 0; i < copyPaths.Count; i++)
  502. {
  503. string destination = Path.Combine(destinationFolder, PathEx.GetTail(copyPaths[i]));
  504. if (Directory.Exists(copyPaths[i]))
  505. DirectoryEx.Copy(copyPaths[i], GetUniquePath(destination));
  506. else if (File.Exists(copyPaths[i]))
  507. FileEx.Copy(copyPaths[i], GetUniquePath(destination));
  508. }
  509. ProjectLibrary.Refresh();
  510. }
  511. else if (cutPaths.Count > 0)
  512. {
  513. for (int i = 0; i < cutPaths.Count; i++)
  514. {
  515. string destination = Path.Combine(destinationFolder, PathEx.GetTail(cutPaths[i]));
  516. if (Directory.Exists(cutPaths[i]))
  517. DirectoryEx.Move(cutPaths[i], GetUniquePath(destination));
  518. else if (File.Exists(cutPaths[i]))
  519. FileEx.Move(cutPaths[i], GetUniquePath(destination));
  520. }
  521. cutPaths.Clear();
  522. ProjectLibrary.Refresh();
  523. }
  524. }
  525. private string GetUniquePath(string path)
  526. {
  527. string extension = Path.GetExtension(path);
  528. string pathNoExtension = path;
  529. if (!string.IsNullOrEmpty(extension))
  530. pathNoExtension = path.Remove(path.Length - extension.Length);
  531. int idx = 0;
  532. string destination = pathNoExtension;
  533. while (ProjectLibrary.Exists(destination))
  534. {
  535. destination = pathNoExtension + "_" + idx;
  536. idx++;
  537. }
  538. return destination + extension;
  539. }
  540. private void OnEditorUpdate()
  541. {
  542. bool isRenameInProgress = inProgressRenameElement != null;
  543. if (HasContentFocus)
  544. {
  545. if (!isRenameInProgress)
  546. {
  547. if (Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl))
  548. {
  549. if (Input.IsButtonUp(ButtonCode.C))
  550. {
  551. CopySelection();
  552. }
  553. else if (Input.IsButtonUp(ButtonCode.X))
  554. {
  555. CutSelection();
  556. }
  557. else if (Input.IsButtonUp(ButtonCode.D))
  558. {
  559. DuplicateSelection();
  560. }
  561. else if (Input.IsButtonUp(ButtonCode.V))
  562. {
  563. PasteToSelection();
  564. }
  565. }
  566. if (Input.IsButtonDown(ButtonCode.Return))
  567. {
  568. if (selectionPaths.Count == 1)
  569. {
  570. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  571. if (entry != null && entry.Type == LibraryEntryType.Directory)
  572. {
  573. EnterDirectory(entry.Path);
  574. }
  575. }
  576. }
  577. else if (Input.IsButtonDown(ButtonCode.Back))
  578. {
  579. LibraryEntry entry = ProjectLibrary.GetEntry(currentDirectory);
  580. if (entry != null && entry.Parent != null)
  581. {
  582. EnterDirectory(entry.Parent.Path);
  583. }
  584. }
  585. else if (Input.IsButtonDown(ButtonCode.Up))
  586. {
  587. MoveSelection(MoveDirection.Up);
  588. }
  589. else if (Input.IsButtonDown(ButtonCode.Down))
  590. {
  591. MoveSelection(MoveDirection.Down);
  592. }
  593. else if (Input.IsButtonDown(ButtonCode.Left))
  594. {
  595. MoveSelection(MoveDirection.Left);
  596. }
  597. else if (Input.IsButtonDown(ButtonCode.Right))
  598. {
  599. MoveSelection(MoveDirection.Right);
  600. }
  601. else if (Input.IsButtonDown(ButtonCode.F2))
  602. {
  603. RenameSelection();
  604. }
  605. else if (Input.IsButtonDown(ButtonCode.Delete))
  606. {
  607. DeleteSelection();
  608. }
  609. }
  610. else
  611. {
  612. if (Input.IsButtonDown(ButtonCode.Return))
  613. {
  614. string newName = inProgressRenameElement.GetRenamedName();
  615. string originalPath = inProgressRenameElement.path;
  616. originalPath = originalPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  617. string newPath = Path.GetDirectoryName(originalPath);
  618. newPath = Path.Combine(newPath, newName + Path.GetExtension(originalPath));
  619. bool renameOK = true;
  620. if (!PathEx.IsValidFileName(newName))
  621. {
  622. DialogBox.Open("Error", "The name you specified is not a valid file name. Try another.", DialogBox.Type.OK);
  623. renameOK = false;
  624. }
  625. if (renameOK)
  626. {
  627. // Windows sees paths with dot at the end as if they didn't have it
  628. // so remove the dot to ensure the project library does the same
  629. string trimmedNewPath = newPath.TrimEnd('.');
  630. if (originalPath != trimmedNewPath && ProjectLibrary.Exists(trimmedNewPath))
  631. {
  632. DialogBox.Open("Error", "File/folder with that name already exists in this folder.", DialogBox.Type.OK);
  633. renameOK = false;
  634. }
  635. }
  636. if (renameOK)
  637. {
  638. ProjectLibrary.Rename(originalPath, newPath);
  639. StopRename();
  640. }
  641. }
  642. else if (Input.IsButtonDown(ButtonCode.Escape))
  643. {
  644. StopRename();
  645. }
  646. }
  647. }
  648. else
  649. {
  650. if(isRenameInProgress)
  651. StopRename();
  652. }
  653. if (autoScrollAmount != 0)
  654. {
  655. Rect2I contentBounds = contentScrollArea.ContentBounds;
  656. float scrollPct = autoScrollAmount / (float)contentBounds.height;
  657. contentScrollArea.VerticalScroll += scrollPct * Time.FrameDelta;
  658. }
  659. if (requiresRefresh)
  660. Refresh();
  661. dropTarget.Update();
  662. }
  663. private void OnEntryChanged(string entry)
  664. {
  665. requiresRefresh = true;
  666. }
  667. private void ScrollToEntry(string path)
  668. {
  669. ElementEntry entryGUI;
  670. if (!entryLookup.TryGetValue(path, out entryGUI))
  671. return;
  672. Rect2I entryBounds = entryGUI.Bounds;
  673. Rect2I contentBounds = contentScrollArea.Layout.Bounds;
  674. Rect2I windowEntryBounds = entryBounds;
  675. windowEntryBounds.x += contentBounds.x;
  676. windowEntryBounds.y += contentBounds.y;
  677. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  678. bool requiresScroll = windowEntryBounds.y < scrollAreaBounds.y ||
  679. (windowEntryBounds.y + windowEntryBounds.height) > (scrollAreaBounds.y + scrollAreaBounds.height);
  680. if (!requiresScroll)
  681. return;
  682. int scrollableSize = contentBounds.height - scrollAreaBounds.height;
  683. float percent = (((entryBounds.y + entryBounds.height * 0.5f) - scrollAreaBounds.height * 0.5f) / (float)scrollableSize);
  684. percent = MathEx.Clamp01(percent);
  685. contentScrollArea.VerticalScroll = percent;
  686. }
  687. private void Refresh()
  688. {
  689. requiresRefresh = false;
  690. LibraryEntry[] entriesToDisplay = new LibraryEntry[0];
  691. if (IsSearchActive)
  692. {
  693. entriesToDisplay = ProjectLibrary.Search("*" + searchQuery + "*");
  694. }
  695. else
  696. {
  697. DirectoryEntry entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  698. if (entry == null)
  699. {
  700. currentDirectory = ProjectLibrary.Root.Path;
  701. entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  702. }
  703. if(entry != null)
  704. entriesToDisplay = entry.Children;
  705. }
  706. if (scrollAreaPanel != null)
  707. scrollAreaPanel.Destroy();
  708. entries.Clear();
  709. entryLookup.Clear();
  710. inProgressRenameElement = null;
  711. scrollAreaPanel = contentScrollArea.Layout.AddPanel();
  712. RefreshDirectoryBar();
  713. SortEntries(entriesToDisplay);
  714. if (entriesToDisplay.Length == 0)
  715. return;
  716. contentInfo = new ContentInfo(this, viewType, entriesToDisplay.Length);
  717. if (viewType == ProjectViewType.List16)
  718. {
  719. for (int i = 0; i < entriesToDisplay.Length; i++)
  720. {
  721. ElementEntry guiEntry = new ElementEntry(contentInfo, contentInfo.main, entriesToDisplay[i], i);
  722. entries.Add(guiEntry);
  723. entryLookup[guiEntry.path] = guiEntry;
  724. if (i != entriesToDisplay.Length - 1)
  725. contentInfo.main.AddSpace(LIST_ENTRY_SPACING);
  726. }
  727. contentInfo.main.AddFlexibleSpace();
  728. }
  729. else
  730. {
  731. int tileSize = 64;
  732. switch (viewType)
  733. {
  734. case ProjectViewType.Grid64: tileSize = 64; break;
  735. case ProjectViewType.Grid48: tileSize = 48; break;
  736. case ProjectViewType.Grid32: tileSize = 32; break;
  737. }
  738. contentInfo.main.AddSpace(GRID_ENTRY_SPACING / 2);
  739. GUILayoutX rowLayout = contentInfo.main.AddLayoutX();
  740. contentInfo.main.AddSpace(GRID_ENTRY_SPACING);
  741. rowLayout.AddFlexibleSpace();
  742. int elemsInRow = 0;
  743. for (int i = 0; i < entriesToDisplay.Length; i++)
  744. {
  745. if (elemsInRow == contentInfo.elementsPerRow && elemsInRow > 0)
  746. {
  747. rowLayout = contentInfo.main.AddLayoutX();
  748. contentInfo.main.AddSpace(GRID_ENTRY_SPACING);
  749. rowLayout.AddFlexibleSpace();
  750. elemsInRow = 0;
  751. }
  752. ElementEntry guiEntry = new ElementEntry(contentInfo, rowLayout, entriesToDisplay[i], i);
  753. entries.Add(guiEntry);
  754. entryLookup[guiEntry.path] = guiEntry;
  755. rowLayout.AddFlexibleSpace();
  756. elemsInRow++;
  757. }
  758. int extraElements = contentInfo.elementsPerRow - elemsInRow;
  759. for (int i = 0; i < extraElements; i++)
  760. {
  761. rowLayout.AddSpace(contentInfo.labelWidth);
  762. rowLayout.AddFlexibleSpace();
  763. }
  764. contentInfo.main.AddFlexibleSpace();
  765. }
  766. for (int i = 0; i < entries.Count; i++)
  767. {
  768. ElementEntry guiEntry = entries[i];
  769. guiEntry.Initialize();
  770. if (cutPaths.Contains(guiEntry.path))
  771. guiEntry.MarkAsCut(true);
  772. if (selectionPaths.Contains(guiEntry.path))
  773. guiEntry.MarkAsSelected(true);
  774. else if (pingPath == guiEntry.path)
  775. guiEntry.MarkAsPinged(true);
  776. }
  777. Rect2I contentBounds = contentInfo.main.Bounds;
  778. Rect2I minimalBounds = GetScrollAreaBounds();
  779. contentBounds.height = Math.Max(contentBounds.height, minimalBounds.height);
  780. GUIButton catchAll = new GUIButton("", EditorStyles.Blank);
  781. catchAll.Bounds = contentBounds;
  782. catchAll.OnClick += OnCatchAllClicked;
  783. catchAll.SetContextMenu(entryContextMenu);
  784. contentInfo.underlay.AddElement(catchAll);
  785. Rect2I focusBounds = contentBounds; // Contents + Folder bar
  786. Rect2I scrollBounds = contentScrollArea.Bounds;
  787. focusBounds.x += scrollBounds.x;
  788. focusBounds.y += scrollBounds.y;
  789. Rect2I folderBarBounds = folderListLayout.Bounds;
  790. focusBounds.y -= folderBarBounds.height;
  791. focusBounds.height += folderBarBounds.height;
  792. GUIButton focusCatcher = new GUIButton("", EditorStyles.Blank);
  793. focusCatcher.OnFocusChanged += OnContentsFocusChanged;
  794. focusCatcher.Bounds = focusBounds;
  795. GUIPanel focusPanel = GUI.AddPanel(3);
  796. focusPanel.AddElement(focusCatcher);
  797. UpdateDragSelection(dragSelectionEnd);
  798. }
  799. private Vector2I WindowToScrollAreaCoords(Vector2I windowPos)
  800. {
  801. Rect2I scrollBounds = contentScrollArea.Layout.Bounds;
  802. Vector2I scrollPos = windowPos;
  803. scrollPos.x -= scrollBounds.x;
  804. scrollPos.y -= scrollBounds.y;
  805. return scrollPos;
  806. }
  807. private void StartDragSelection(Vector2I windowPos)
  808. {
  809. isDraggingSelection = true;
  810. dragSelectionStart = WindowToScrollAreaCoords(windowPos);
  811. dragSelectionEnd = dragSelectionStart;
  812. }
  813. private bool UpdateDragSelection(Vector2I windowPos)
  814. {
  815. if (!isDraggingSelection)
  816. return false;
  817. if (dragSelection == null)
  818. {
  819. dragSelection = new GUITexture(null, true, EditorStyles.SelectionArea);
  820. contentInfo.overlay.AddElement(dragSelection);
  821. }
  822. dragSelectionEnd = WindowToScrollAreaCoords(windowPos);
  823. Rect2I selectionArea = CalculateSelectionArea();
  824. SelectInArea(selectionArea);
  825. dragSelection.Bounds = selectionArea;
  826. return true;
  827. }
  828. private bool EndDragSelection()
  829. {
  830. if (!isDraggingSelection)
  831. return false;
  832. if (dragSelection != null)
  833. {
  834. dragSelection.Destroy();
  835. dragSelection = null;
  836. }
  837. Rect2I selectionArea = CalculateSelectionArea();
  838. SelectInArea(selectionArea);
  839. isDraggingSelection = false;
  840. return false;
  841. }
  842. private Rect2I CalculateSelectionArea()
  843. {
  844. Rect2I selectionArea = new Rect2I();
  845. if (dragSelectionStart.x < dragSelectionEnd.x)
  846. {
  847. selectionArea.x = dragSelectionStart.x;
  848. selectionArea.width = dragSelectionEnd.x - dragSelectionStart.x;
  849. }
  850. else
  851. {
  852. selectionArea.x = dragSelectionEnd.x;
  853. selectionArea.width = dragSelectionStart.x - dragSelectionEnd.x;
  854. }
  855. if (dragSelectionStart.y < dragSelectionEnd.y)
  856. {
  857. selectionArea.y = dragSelectionStart.y;
  858. selectionArea.height = dragSelectionEnd.y - dragSelectionStart.y;
  859. }
  860. else
  861. {
  862. selectionArea.y = dragSelectionEnd.y;
  863. selectionArea.height = dragSelectionStart.y - dragSelectionEnd.y;
  864. }
  865. Rect2I maxBounds = contentScrollArea.Layout.Bounds;
  866. maxBounds.x = 0;
  867. maxBounds.y = 0;
  868. selectionArea.Clip(maxBounds);
  869. return selectionArea;
  870. }
  871. private void SelectInArea(Rect2I scrollBounds)
  872. {
  873. ElementEntry[] foundElements = FindElementsOverlapping(scrollBounds);
  874. if (foundElements.Length > 0)
  875. {
  876. selectionAnchorStart = foundElements[0].index;
  877. selectionAnchorEnd = foundElements[foundElements.Length - 1].index;
  878. }
  879. else
  880. {
  881. selectionAnchorStart = -1;
  882. selectionAnchorEnd = -1;
  883. }
  884. List<string> elementPaths = new List<string>();
  885. foreach (var elem in foundElements)
  886. elementPaths.Add(elem.path);
  887. SetSelection(elementPaths);
  888. }
  889. private void RefreshDirectoryBar()
  890. {
  891. if (folderListLayout != null)
  892. {
  893. folderListLayout.Destroy();
  894. folderListLayout = null;
  895. }
  896. folderListLayout = folderBarLayout.AddLayoutX();
  897. string[] folders = null;
  898. string[] fullPaths = null;
  899. if (IsSearchActive)
  900. {
  901. folders = new[] {searchQuery};
  902. fullPaths = new[] { searchQuery };
  903. }
  904. else
  905. {
  906. string currentDir = Path.Combine("Resources", currentDirectory);
  907. folders = currentDir.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar },
  908. StringSplitOptions.RemoveEmptyEntries);
  909. fullPaths = new string[folders.Length];
  910. for (int i = 0; i < folders.Length; i++)
  911. {
  912. if (i == 0)
  913. fullPaths[i] = "";
  914. else
  915. fullPaths[i] = Path.Combine(fullPaths[i - 1], folders[i]);
  916. }
  917. }
  918. int availableWidth = folderBarLayout.Bounds.width - FOLDER_BUTTON_WIDTH * 2;
  919. int numFolders = 0;
  920. for (int i = folders.Length - 1; i >= 0; i--)
  921. {
  922. GUIButton folderButton = new GUIButton(folders[i]);
  923. if (!IsSearchActive)
  924. {
  925. string fullPath = fullPaths[i];
  926. folderButton.OnClick += () => OnFolderButtonClicked(fullPath);
  927. }
  928. GUILabel separator = new GUILabel("/", GUIOption.FixedWidth(FOLDER_SEPARATOR_WIDTH));
  929. folderListLayout.InsertElement(0, separator);
  930. folderListLayout.InsertElement(0, folderButton);
  931. numFolders++;
  932. Rect2I folderListBounds = folderListLayout.Bounds;
  933. if (folderListBounds.width > availableWidth)
  934. {
  935. if (numFolders > 2)
  936. {
  937. separator.Destroy();
  938. folderButton.Destroy();
  939. break;
  940. }
  941. }
  942. }
  943. }
  944. private void SortEntries(LibraryEntry[] input)
  945. {
  946. Array.Sort(input, (x, y) =>
  947. {
  948. if (x.Type == y.Type)
  949. return x.Name.CompareTo(y.Name);
  950. else
  951. return x.Type == LibraryEntryType.File ? 1 : -1;
  952. });
  953. }
  954. private void OnFolderButtonClicked(string path)
  955. {
  956. EnterDirectory(path);
  957. }
  958. private void OnContentsFocusChanged(bool focus)
  959. {
  960. hasContentFocus = focus;
  961. }
  962. private void OnEntryClicked(string path)
  963. {
  964. Select(path);
  965. }
  966. private void OnEntryDoubleClicked(string path)
  967. {
  968. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  969. if (entry != null)
  970. {
  971. if (entry.Type == LibraryEntryType.Directory)
  972. EnterDirectory(path);
  973. else
  974. {
  975. FileEntry resEntry = (FileEntry) entry;
  976. if (resEntry.ResType == ResourceType.Prefab)
  977. {
  978. Scene.Load(resEntry.Path);
  979. }
  980. }
  981. }
  982. }
  983. private void OnCatchAllClicked()
  984. {
  985. DeselectAll();
  986. }
  987. private void OnHomeClicked()
  988. {
  989. currentDirectory = ProjectLibrary.Root.Path;
  990. Refresh();
  991. }
  992. private void OnUpClicked()
  993. {
  994. currentDirectory = currentDirectory.Trim(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  995. if (!string.IsNullOrEmpty(currentDirectory))
  996. {
  997. string parent = Path.GetDirectoryName(currentDirectory);
  998. currentDirectory = parent;
  999. Refresh();
  1000. }
  1001. }
  1002. private void CutSelection()
  1003. {
  1004. if (selectionPaths.Count > 0)
  1005. Cut(selectionPaths);
  1006. }
  1007. private void CopySelection()
  1008. {
  1009. if (selectionPaths.Count > 0)
  1010. Copy(selectionPaths);
  1011. }
  1012. private void DuplicateSelection()
  1013. {
  1014. if (selectionPaths.Count > 0)
  1015. Duplicate(selectionPaths);
  1016. }
  1017. private void PasteToSelection()
  1018. {
  1019. DirectoryEntry selectedDirectory = null;
  1020. if (selectionPaths.Count == 1)
  1021. {
  1022. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  1023. if (entry != null && entry.Type == LibraryEntryType.Directory)
  1024. selectedDirectory = (DirectoryEntry) entry;
  1025. }
  1026. if(selectedDirectory != null)
  1027. Paste(selectedDirectory.Path);
  1028. else
  1029. Paste(currentDirectory);
  1030. }
  1031. private void RenameSelection()
  1032. {
  1033. if (selectionPaths.Count == 0)
  1034. return;
  1035. if (selectionPaths.Count > 1)
  1036. {
  1037. DeselectAll();
  1038. Select(selectionPaths[0]);
  1039. }
  1040. ElementEntry entry;
  1041. if (entryLookup.TryGetValue(selectionPaths[0], out entry))
  1042. {
  1043. entry.StartRename();
  1044. inProgressRenameElement = entry;
  1045. }
  1046. }
  1047. private void StopRename()
  1048. {
  1049. if (inProgressRenameElement != null)
  1050. {
  1051. inProgressRenameElement.StopRename();
  1052. inProgressRenameElement = null;
  1053. }
  1054. }
  1055. private void DeleteSelection()
  1056. {
  1057. if (selectionPaths.Count == 0)
  1058. return;
  1059. DialogBox.Open("Confirm deletion", "Are you sure you want to delete the selected object(s)?",
  1060. DialogBox.Type.YesNo,
  1061. type =>
  1062. {
  1063. if (type == DialogBox.ResultType.Yes)
  1064. {
  1065. foreach (var path in selectionPaths)
  1066. {
  1067. ProjectLibrary.Delete(path);
  1068. }
  1069. DeselectAll();
  1070. Refresh();
  1071. }
  1072. });
  1073. }
  1074. private void OnSearchChanged(string newValue)
  1075. {
  1076. searchQuery = newValue;
  1077. Refresh();
  1078. }
  1079. private void ClearSearch()
  1080. {
  1081. searchField.Value = "";
  1082. searchQuery = "";
  1083. Refresh();
  1084. }
  1085. private void OpenOptionsWindow()
  1086. {
  1087. Vector2I openPosition;
  1088. Rect2I buttonBounds = GUILayoutUtility.CalculateBounds(optionsButton, GUI);
  1089. openPosition.x = buttonBounds.x + buttonBounds.width / 2;
  1090. openPosition.y = buttonBounds.y + buttonBounds.height / 2;
  1091. ProjectDropDown dropDown = DropDownWindow.Open<ProjectDropDown>(this, openPosition);
  1092. dropDown.SetParent(this);
  1093. }
  1094. private void Reset()
  1095. {
  1096. currentDirectory = ProjectLibrary.Root.Path;
  1097. selectionAnchorStart = -1;
  1098. selectionAnchorEnd = -1;
  1099. selectionPaths.Clear();
  1100. pingPath = "";
  1101. hoverHighlightPath = "";
  1102. Refresh();
  1103. }
  1104. private Rect2I GetScrollAreaBounds()
  1105. {
  1106. Rect2I bounds = GUI.Bounds;
  1107. Rect2I folderListBounds = folderListLayout.Bounds;
  1108. Rect2I searchBarBounds = searchBarLayout.Bounds;
  1109. bounds.y += folderListBounds.height + searchBarBounds.height;
  1110. bounds.height -= folderListBounds.height + searchBarBounds.height;
  1111. return bounds;
  1112. }
  1113. protected override void WindowResized(int width, int height)
  1114. {
  1115. base.WindowResized(width, height);
  1116. Refresh();
  1117. dropTarget.Bounds = contentScrollArea.Bounds;
  1118. }
  1119. private class ContentInfo
  1120. {
  1121. public ContentInfo(ProjectWindow window, ProjectViewType viewType, int numEntries)
  1122. {
  1123. GUIPanel parentPanel = window.scrollAreaPanel;
  1124. GUIPanel contentPanel = parentPanel.AddPanel(1);
  1125. overlay = parentPanel.AddPanel(0);
  1126. underlay = parentPanel.AddPanel(2);
  1127. inputOverlay = parentPanel.AddPanel(-1);
  1128. main = contentPanel.AddLayoutY();
  1129. if (viewType == ProjectViewType.List16)
  1130. {
  1131. tileSize = 16;
  1132. gridLayout = false;
  1133. elementsPerRow = 1;
  1134. }
  1135. else
  1136. {
  1137. switch (viewType)
  1138. {
  1139. case ProjectViewType.Grid64:
  1140. tileSize = 64;
  1141. break;
  1142. case ProjectViewType.Grid48:
  1143. tileSize = 48;
  1144. break;
  1145. case ProjectViewType.Grid32:
  1146. tileSize = 32;
  1147. break;
  1148. }
  1149. gridLayout = true;
  1150. Rect2I scrollBounds = window.contentScrollArea.Bounds;
  1151. int availableWidth = scrollBounds.width;
  1152. int elemSize = tileSize + GRID_ENTRY_SPACING;
  1153. elementsPerRow = (availableWidth - GRID_ENTRY_SPACING * 2) / elemSize;
  1154. elementsPerRow = Math.Max(elementsPerRow, 1);
  1155. int numRows = MathEx.CeilToInt(numEntries / (float)elementsPerRow);
  1156. int neededHeight = numRows * (elemSize);
  1157. bool requiresScrollbar = neededHeight > scrollBounds.height;
  1158. if (requiresScrollbar)
  1159. {
  1160. availableWidth -= window.contentScrollArea.ScrollBarWidth;
  1161. elementsPerRow = (availableWidth - GRID_ENTRY_SPACING * 2) / elemSize;
  1162. }
  1163. labelWidth = (availableWidth - (elementsPerRow + 1) * MIN_HORZ_SPACING) / elementsPerRow;
  1164. }
  1165. this.window = window;
  1166. }
  1167. public GUILayout main;
  1168. public GUIPanel overlay;
  1169. public GUIPanel underlay;
  1170. public GUIPanel inputOverlay;
  1171. public ProjectWindow window;
  1172. public int tileSize;
  1173. public bool gridLayout;
  1174. public int elementsPerRow;
  1175. public int labelWidth;
  1176. }
  1177. private class ElementEntry
  1178. {
  1179. // Note: Order of these is relevant
  1180. enum UnderlayState
  1181. {
  1182. None, Hovered, Selected, Pinged
  1183. }
  1184. public int index;
  1185. public string path;
  1186. public GUITexture icon;
  1187. public GUILabel label;
  1188. public Rect2I bounds;
  1189. private GUITexture underlay;
  1190. private ContentInfo info;
  1191. private UnderlayState underlayState;
  1192. private GUITextBox renameTextBox;
  1193. public ElementEntry(ContentInfo info, GUILayout parent, LibraryEntry entry, int index)
  1194. {
  1195. GUILayout entryLayout;
  1196. if (info.gridLayout)
  1197. entryLayout = parent.AddLayoutY();
  1198. else
  1199. entryLayout = parent.AddLayoutX();
  1200. SpriteTexture iconTexture = GetIcon(entry);
  1201. icon = new GUITexture(iconTexture, GUIImageScaleMode.ScaleToFit,
  1202. true, GUIOption.FixedHeight(info.tileSize), GUIOption.FixedWidth(info.tileSize));
  1203. label = null;
  1204. if (info.gridLayout)
  1205. {
  1206. label = new GUILabel(entry.Name, EditorStyles.MultiLineLabelCentered,
  1207. GUIOption.FixedWidth(info.labelWidth), GUIOption.FlexibleHeight(0, MAX_LABEL_HEIGHT));
  1208. }
  1209. else
  1210. {
  1211. label = new GUILabel(entry.Name);
  1212. }
  1213. entryLayout.AddElement(icon);
  1214. entryLayout.AddElement(label);
  1215. this.info = info;
  1216. this.index = index;
  1217. this.path = entry.Path;
  1218. this.bounds = new Rect2I();
  1219. this.underlay = null;
  1220. }
  1221. public void Initialize()
  1222. {
  1223. bounds = icon.Bounds;
  1224. Rect2I labelBounds = label.Bounds;
  1225. bounds.x = MathEx.Min(bounds.x, labelBounds.x - SELECTION_EXTRA_WIDTH);
  1226. bounds.y = MathEx.Min(bounds.y, labelBounds.y) - 5; // 5 - Just padding for better look
  1227. bounds.width = MathEx.Max(bounds.x + bounds.width,
  1228. labelBounds.x + labelBounds.width) - bounds.x + SELECTION_EXTRA_WIDTH;
  1229. bounds.height = MathEx.Max(bounds.y + bounds.height,
  1230. labelBounds.y + labelBounds.height) - bounds.y;
  1231. ProjectWindow hoistedWindow = info.window;
  1232. string hoistedPath = path;
  1233. GUIButton overlayBtn = new GUIButton("", EditorStyles.Blank);
  1234. overlayBtn.Bounds = bounds;
  1235. overlayBtn.OnClick += () => hoistedWindow.OnEntryClicked(hoistedPath);
  1236. overlayBtn.OnDoubleClick += () => hoistedWindow.OnEntryDoubleClicked(hoistedPath);
  1237. overlayBtn.SetContextMenu(info.window.entryContextMenu);
  1238. info.overlay.AddElement(overlayBtn);
  1239. }
  1240. public Rect2I Bounds
  1241. {
  1242. get { return bounds; }
  1243. }
  1244. public void MarkAsCut(bool enable)
  1245. {
  1246. if (enable)
  1247. icon.SetTint(CUT_COLOR);
  1248. else
  1249. icon.SetTint(Color.White);
  1250. }
  1251. public void MarkAsSelected(bool enable)
  1252. {
  1253. if ((int)underlayState > (int)UnderlayState.Selected)
  1254. return;
  1255. if (enable)
  1256. {
  1257. CreateUnderlay();
  1258. underlay.SetTint(SELECTION_COLOR);
  1259. }
  1260. else
  1261. ClearUnderlay();
  1262. underlayState = UnderlayState.Selected;
  1263. }
  1264. public void MarkAsPinged(bool enable)
  1265. {
  1266. if ((int)underlayState > (int)UnderlayState.Pinged)
  1267. return;
  1268. if (enable)
  1269. {
  1270. CreateUnderlay();
  1271. underlay.SetTint(PING_COLOR);
  1272. }
  1273. else
  1274. ClearUnderlay();
  1275. underlayState = UnderlayState.Pinged;
  1276. }
  1277. public void MarkAsHovered(bool enable)
  1278. {
  1279. if ((int)underlayState > (int)UnderlayState.Hovered)
  1280. return;
  1281. if (enable)
  1282. {
  1283. CreateUnderlay();
  1284. underlay.SetTint(HOVER_COLOR);
  1285. }
  1286. else
  1287. ClearUnderlay();
  1288. underlayState = UnderlayState.Hovered;
  1289. }
  1290. public void StartRename()
  1291. {
  1292. if (renameTextBox != null)
  1293. return;
  1294. renameTextBox = new GUITextBox(false);
  1295. renameTextBox.Bounds = label.Bounds;
  1296. info.inputOverlay.AddElement(renameTextBox);
  1297. string name = Path.GetFileNameWithoutExtension(PathEx.GetTail(path));
  1298. renameTextBox.Text = name;
  1299. renameTextBox.Focus = true;
  1300. label.Visible = false;
  1301. }
  1302. public void StopRename()
  1303. {
  1304. if (renameTextBox != null)
  1305. {
  1306. renameTextBox.Destroy();
  1307. renameTextBox = null;
  1308. }
  1309. label.Visible = true;
  1310. }
  1311. public string GetRenamedName()
  1312. {
  1313. if (renameTextBox != null)
  1314. return renameTextBox.Text;
  1315. return "";
  1316. }
  1317. private void ClearUnderlay()
  1318. {
  1319. if (underlay != null)
  1320. {
  1321. underlay.Destroy();
  1322. underlay = null;
  1323. }
  1324. underlayState = UnderlayState.None;
  1325. }
  1326. private void CreateUnderlay()
  1327. {
  1328. if (underlay == null)
  1329. {
  1330. underlay = new GUITexture(Builtin.WhiteTexture);
  1331. underlay.Bounds = Bounds;
  1332. info.underlay.AddElement(underlay);
  1333. }
  1334. }
  1335. private static SpriteTexture GetIcon(LibraryEntry entry)
  1336. {
  1337. if (entry.Type == LibraryEntryType.Directory)
  1338. {
  1339. return EditorBuiltin.FolderIcon;
  1340. }
  1341. else
  1342. {
  1343. FileEntry fileEntry = (FileEntry)entry;
  1344. switch (fileEntry.ResType)
  1345. {
  1346. case ResourceType.Font:
  1347. return EditorBuiltin.FontIcon;
  1348. case ResourceType.Mesh:
  1349. return EditorBuiltin.MeshIcon;
  1350. case ResourceType.Texture:
  1351. return EditorBuiltin.TextureIcon;
  1352. case ResourceType.PlainText:
  1353. return EditorBuiltin.PlainTextIcon;
  1354. case ResourceType.ScriptCode:
  1355. return EditorBuiltin.ScriptCodeIcon;
  1356. case ResourceType.SpriteTexture:
  1357. return EditorBuiltin.SpriteTextureIcon;
  1358. case ResourceType.Shader:
  1359. return EditorBuiltin.ShaderIcon;
  1360. case ResourceType.Material:
  1361. return EditorBuiltin.MaterialIcon;
  1362. case ResourceType.Prefab:
  1363. return EditorBuiltin.PrefabIcon;
  1364. }
  1365. }
  1366. return null;
  1367. }
  1368. }
  1369. enum MoveDirection
  1370. {
  1371. Up, Down, Left, Right
  1372. }
  1373. }
  1374. internal class ProjectDropDown : DropDownWindow
  1375. {
  1376. private ProjectWindow parent;
  1377. public ProjectDropDown()
  1378. :base(150, 30)
  1379. { }
  1380. internal void SetParent(ProjectWindow parent)
  1381. {
  1382. this.parent = parent;
  1383. GUIToggleGroup group = new GUIToggleGroup();
  1384. GUIToggle list16 = new GUIToggle("16", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1385. GUIToggle grid32 = new GUIToggle("32", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1386. GUIToggle grid48 = new GUIToggle("48", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1387. GUIToggle grid64 = new GUIToggle("64", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  1388. ProjectViewType activeType = parent.ViewType;
  1389. switch (activeType)
  1390. {
  1391. case ProjectViewType.List16:
  1392. list16.ToggleOn();
  1393. break;
  1394. case ProjectViewType.Grid32:
  1395. grid32.ToggleOn();
  1396. break;
  1397. case ProjectViewType.Grid48:
  1398. grid48.ToggleOn();
  1399. break;
  1400. case ProjectViewType.Grid64:
  1401. grid64.ToggleOn();
  1402. break;
  1403. }
  1404. list16.OnToggled += (active) =>
  1405. {
  1406. if (active)
  1407. ChangeViewType(ProjectViewType.List16);
  1408. };
  1409. grid32.OnToggled += (active) =>
  1410. {
  1411. if (active)
  1412. ChangeViewType(ProjectViewType.Grid32);
  1413. };
  1414. grid48.OnToggled += (active) =>
  1415. {
  1416. if (active)
  1417. ChangeViewType(ProjectViewType.Grid48);
  1418. };
  1419. grid64.OnToggled += (active) =>
  1420. {
  1421. if (active)
  1422. ChangeViewType(ProjectViewType.Grid64);
  1423. };
  1424. GUILayoutY vertLayout = GUI.AddLayoutY();
  1425. vertLayout.AddFlexibleSpace();
  1426. GUILayoutX contentLayout = vertLayout.AddLayoutX();
  1427. contentLayout.AddFlexibleSpace();
  1428. contentLayout.AddElement(list16);
  1429. contentLayout.AddElement(grid32);
  1430. contentLayout.AddElement(grid48);
  1431. contentLayout.AddElement(grid64);
  1432. contentLayout.AddFlexibleSpace();
  1433. vertLayout.AddFlexibleSpace();
  1434. }
  1435. private void ChangeViewType(ProjectViewType viewType)
  1436. {
  1437. parent.ViewType = viewType;
  1438. }
  1439. }
  1440. }