ProjectWindow.cs 60 KB

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