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