LibraryWindow.cs 60 KB

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