ProjectWindow.cs 60 KB

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