ProjectWindow.cs 58 KB

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