ProjectWindow.cs 60 KB

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