ProjectWindow.cs 57 KB

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