LibraryWindow.cs 60 KB

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