ProjectWindow.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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)
  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. }
  27. else
  28. {
  29. switch (viewType)
  30. {
  31. case ProjectViewType.Grid64:
  32. tileSize = 64;
  33. break;
  34. case ProjectViewType.Grid48:
  35. tileSize = 48;
  36. break;
  37. case ProjectViewType.Grid32:
  38. tileSize = 32;
  39. break;
  40. }
  41. gridLayout = true;
  42. }
  43. this.window = window;
  44. }
  45. public GUILayout main;
  46. public GUIPanel overlay;
  47. public GUIPanel underlay;
  48. public ProjectWindow window;
  49. public int tileSize;
  50. public bool gridLayout;
  51. }
  52. private class ElementEntry
  53. {
  54. // Note: Order of these is relevant
  55. enum UnderlayState
  56. {
  57. None, Hovered, Selected, Pinged
  58. }
  59. public int index;
  60. public string path;
  61. public GUITexture icon;
  62. public GUILabel label;
  63. public Rect2I bounds;
  64. private GUITexture underlay;
  65. private ContentInfo info;
  66. private UnderlayState underlayState;
  67. public ElementEntry(ContentInfo info, GUILayout parent, LibraryEntry entry, int index)
  68. {
  69. GUILayout entryLayout;
  70. if (info.gridLayout)
  71. entryLayout = parent.AddLayoutY();
  72. else
  73. entryLayout = parent.AddLayoutX();
  74. SpriteTexture iconTexture = GetIcon(entry);
  75. icon = new GUITexture(iconTexture, GUIImageScaleMode.ScaleToFit,
  76. true, GUIOption.FixedHeight(info.tileSize), GUIOption.FixedWidth(info.tileSize));
  77. label = null;
  78. if (info.gridLayout)
  79. {
  80. int labelWidth = info.tileSize + LABEL_EXTRA_WIDTH;
  81. label = new GUILabel(entry.Name, EditorStyles.MultiLineLabel,
  82. GUIOption.FixedWidth(labelWidth), GUIOption.FlexibleHeight(0, MAX_LABEL_HEIGHT));
  83. }
  84. else
  85. {
  86. label = new GUILabel(entry.Name);
  87. }
  88. entryLayout.AddElement(icon);
  89. entryLayout.AddElement(label);
  90. this.info = info;
  91. this.index = index;
  92. this.path = entry.Path;
  93. this.bounds = new Rect2I();
  94. this.underlay = null;
  95. }
  96. public void Initialize()
  97. {
  98. bounds = icon.Bounds;
  99. Rect2I labelBounds = label.Bounds;
  100. bounds.x = MathEx.Min(bounds.x, labelBounds.x);
  101. bounds.y = MathEx.Min(bounds.y, labelBounds.y);
  102. bounds.width = MathEx.Max(bounds.x + bounds.width,
  103. labelBounds.x + labelBounds.width) - bounds.x;
  104. bounds.height = MathEx.Max(bounds.y + bounds.height,
  105. labelBounds.y + labelBounds.height) - bounds.y;
  106. ProjectWindow hoistedWindow = info.window;
  107. string hoistedPath = path;
  108. GUIButton overlayBtn = new GUIButton("", EditorStyles.Blank);
  109. overlayBtn.Bounds = bounds;
  110. overlayBtn.OnClick += () => hoistedWindow.OnEntryClicked(hoistedPath);
  111. overlayBtn.OnDoubleClick += () => hoistedWindow.OnEntryDoubleClicked(hoistedPath);
  112. overlayBtn.SetContextMenu(info.window.entryContextMenu);
  113. info.overlay.AddElement(overlayBtn);
  114. }
  115. public Rect2I Bounds
  116. {
  117. get { return bounds; }
  118. }
  119. public void MarkAsCut(bool enable)
  120. {
  121. if (enable)
  122. icon.SetTint(CUT_COLOR);
  123. else
  124. icon.SetTint(Color.White);
  125. }
  126. public void MarkAsSelected(bool enable)
  127. {
  128. if ((int)underlayState > (int) UnderlayState.Selected)
  129. return;
  130. if (enable)
  131. {
  132. CreateUnderlay();
  133. underlay.SetTint(SELECTION_COLOR);
  134. }
  135. else
  136. ClearUnderlay();
  137. underlayState = UnderlayState.Selected;
  138. }
  139. public void MarkAsPinged(bool enable)
  140. {
  141. if ((int)underlayState > (int)UnderlayState.Pinged)
  142. return;
  143. if (enable)
  144. {
  145. CreateUnderlay();
  146. underlay.SetTint(PING_COLOR);
  147. }
  148. else
  149. ClearUnderlay();
  150. underlayState = UnderlayState.Pinged;
  151. }
  152. public void MarkAsHovered(bool enable)
  153. {
  154. if ((int)underlayState > (int)UnderlayState.Hovered)
  155. return;
  156. if (enable)
  157. {
  158. CreateUnderlay();
  159. underlay.SetTint(HOVER_COLOR);
  160. }
  161. else
  162. ClearUnderlay();
  163. underlayState = UnderlayState.Hovered;
  164. }
  165. private void ClearUnderlay()
  166. {
  167. if (underlay != null)
  168. {
  169. underlay.Destroy();
  170. underlay = null;
  171. }
  172. underlayState = UnderlayState.None;
  173. }
  174. private void CreateUnderlay()
  175. {
  176. if (underlay == null)
  177. {
  178. underlay = new GUITexture(Builtin.WhiteTexture);
  179. underlay.Bounds = Bounds;
  180. info.underlay.AddElement(underlay);
  181. }
  182. }
  183. private static SpriteTexture GetIcon(LibraryEntry entry)
  184. {
  185. if (entry.Type == LibraryEntryType.Directory)
  186. {
  187. return EditorBuiltin.FolderIcon;
  188. }
  189. else
  190. {
  191. FileEntry fileEntry = (FileEntry)entry;
  192. switch (fileEntry.ResType)
  193. {
  194. case ResourceType.Font:
  195. return EditorBuiltin.FontIcon;
  196. case ResourceType.Mesh:
  197. return EditorBuiltin.MeshIcon;
  198. case ResourceType.Texture:
  199. return EditorBuiltin.TextureIcon;
  200. case ResourceType.PlainText:
  201. return EditorBuiltin.PlainTextIcon;
  202. case ResourceType.ScriptCode:
  203. return EditorBuiltin.ScriptCodeIcon;
  204. case ResourceType.SpriteTexture:
  205. return EditorBuiltin.SpriteTextureIcon;
  206. case ResourceType.Shader:
  207. return EditorBuiltin.ShaderIcon;
  208. case ResourceType.Material:
  209. return EditorBuiltin.MaterialIcon;
  210. }
  211. }
  212. return null;
  213. }
  214. }
  215. enum MoveDirection
  216. {
  217. Up, Down, Left, Right
  218. }
  219. private const int GRID_ENTRY_SPACING = 15;
  220. private const int LIST_ENTRY_SPACING = 7;
  221. private const int MAX_LABEL_HEIGHT = 50;
  222. private const int LABEL_EXTRA_WIDTH = 10;
  223. private const int DRAG_SCROLL_HEIGHT = 20;
  224. private const int DRAG_SCROLL_AMOUNT_PER_SECOND = 100;
  225. private static readonly Color PING_COLOR = Color.BansheeOrange;
  226. private static readonly Color SELECTION_COLOR = Color.DarkCyan;
  227. private static readonly Color HOVER_COLOR = new Color(Color.DarkCyan.r, Color.DarkCyan.g, Color.DarkCyan.b, 0.5f);
  228. private static readonly Color CUT_COLOR = new Color(1.0f, 1.0f, 1.0f, 0.5f);
  229. private bool hasContentFocus = false;
  230. private bool HasContentFocus { get { return HasFocus && hasContentFocus; } }
  231. private ProjectViewType viewType = ProjectViewType.Grid32;
  232. private string currentDirectory = "";
  233. private List<string> selectionPaths = new List<string>();
  234. private int selectionAnchorStart = -1;
  235. private int selectionAnchorEnd = -1;
  236. private string pingPath = "";
  237. private string hoverHighlightPath = "";
  238. private GUIScrollArea contentScrollArea;
  239. private GUIPanel scrollAreaPanel;
  240. private GUILayoutX searchBarLayout;
  241. private GUIButton optionsButton;
  242. private ContextMenu entryContextMenu;
  243. private ProjectDropTarget dropTarget;
  244. private List<ElementEntry> entries = new List<ElementEntry>();
  245. private Dictionary<string, ElementEntry> entryLookup = new Dictionary<string, ElementEntry>();
  246. private int elementsPerRow;
  247. private int autoScrollAmount;
  248. // Cut/Copy/Paste
  249. private List<string> copyPaths = new List<string>();
  250. private List<string> cutPaths = new List<string>();
  251. internal ProjectViewType ViewType
  252. {
  253. get { return viewType; }
  254. set { viewType = value; Refresh(); }
  255. }
  256. [MenuItem("Windows/Project", ButtonModifier.Ctrl, ButtonCode.P)]
  257. private static void OpenProjectWindow()
  258. {
  259. OpenWindow<ProjectWindow>();
  260. }
  261. private void OnInitialize()
  262. {
  263. ProjectLibrary.OnEntryAdded += OnEntryChanged;
  264. ProjectLibrary.OnEntryRemoved += OnEntryChanged;
  265. GUILayoutY contentLayout = GUI.AddLayoutY();
  266. searchBarLayout = contentLayout.AddLayoutX();
  267. GUITextField searchField = new GUITextField();
  268. searchField.OnChanged += OnSearchChanged;
  269. GUIButton clearSearchBtn = new GUIButton("C");
  270. clearSearchBtn.OnClick += ClearSearch;
  271. clearSearchBtn.SetWidth(40);
  272. optionsButton = new GUIButton("O");
  273. optionsButton.OnClick += OpenOptionsWindow;
  274. optionsButton.SetWidth(40);
  275. searchBarLayout.AddElement(searchField);
  276. searchBarLayout.AddElement(clearSearchBtn);
  277. searchBarLayout.AddElement(optionsButton);
  278. // TODO - Add search bar + options button with drop-down
  279. // TODO - Add directory bar + home button
  280. contentScrollArea = new GUIScrollArea(GUIOption.FlexibleWidth(), GUIOption.FlexibleHeight());
  281. contentLayout.AddElement(contentScrollArea);
  282. contentLayout.AddFlexibleSpace();
  283. entryContextMenu = new ContextMenu();
  284. entryContextMenu.AddItem("Cut", CutSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.X));
  285. entryContextMenu.AddItem("Copy", CopySelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.C));
  286. entryContextMenu.AddItem("Duplicate", DuplicateSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.D));
  287. entryContextMenu.AddItem("Paste", PasteToSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.V));
  288. Reset();
  289. dropTarget = new ProjectDropTarget(this);
  290. dropTarget.Bounds = contentScrollArea.Bounds;
  291. dropTarget.OnStart += DoOnDragStart;
  292. dropTarget.OnDrag += DoOnDragMove;
  293. dropTarget.OnLeave += DoOnDragLeave;
  294. dropTarget.OnDrop += DoOnDragDropped;
  295. }
  296. private ElementEntry FindElementAt(Vector2I windowPos)
  297. {
  298. Rect2I scrollBounds = contentScrollArea.Layout.Bounds;
  299. Vector2I scrollPos = windowPos;
  300. scrollPos.x -= scrollBounds.x;
  301. scrollPos.y -= scrollBounds.y;
  302. foreach (var element in entries)
  303. {
  304. if (element.bounds.Contains(scrollPos))
  305. return element;
  306. }
  307. return null;
  308. }
  309. private void DoOnDragStart(Vector2I windowPos)
  310. {
  311. ElementEntry underCursorElem = FindElementAt(windowPos);
  312. if (underCursorElem == null)
  313. return;
  314. if (!selectionPaths.Contains(underCursorElem.path))
  315. Select(underCursorElem.path);
  316. ResourceDragDropData dragDropData = new ResourceDragDropData(selectionPaths.ToArray());
  317. DragDrop.StartDrag(dragDropData);
  318. }
  319. private void DoOnDragMove(Vector2I windowPos)
  320. {
  321. ElementEntry underCursorElem = FindElementAt(windowPos);
  322. if (underCursorElem == null)
  323. {
  324. ClearHoverHighlight();
  325. }
  326. else
  327. {
  328. if (underCursorElem.path != hoverHighlightPath)
  329. {
  330. ClearHoverHighlight();
  331. hoverHighlightPath = underCursorElem.path;
  332. underCursorElem.MarkAsHovered(true);
  333. }
  334. }
  335. Rect2I scrollAreaBounds = contentScrollArea.Bounds;
  336. int scrollAreaTop = scrollAreaBounds.y;
  337. int scrollAreaBottom = scrollAreaBounds.y + scrollAreaBounds.height;
  338. if (windowPos.y > scrollAreaTop && windowPos.y <= (scrollAreaTop + DRAG_SCROLL_HEIGHT))
  339. autoScrollAmount = -DRAG_SCROLL_AMOUNT_PER_SECOND;
  340. else if (windowPos.y >= (scrollAreaBottom - DRAG_SCROLL_HEIGHT) && windowPos.y < scrollAreaBottom)
  341. autoScrollAmount = DRAG_SCROLL_AMOUNT_PER_SECOND;
  342. else
  343. autoScrollAmount = 0;
  344. }
  345. private void DoOnDragLeave()
  346. {
  347. ClearHoverHighlight();
  348. autoScrollAmount = 0;
  349. }
  350. private void DoOnDragDropped(Vector2I windowPos, string[] paths)
  351. {
  352. string resourceDir = ProjectLibrary.ResourceFolder;
  353. string destinationFolder = Path.Combine(resourceDir, currentDirectory);
  354. ElementEntry underCursorElement = FindElementAt(windowPos);
  355. if (underCursorElement != null)
  356. {
  357. LibraryEntry entry = ProjectLibrary.GetEntry(underCursorElement.path);
  358. if (entry != null && entry.Type == LibraryEntryType.Directory)
  359. destinationFolder = Path.Combine(resourceDir, entry.Path);
  360. }
  361. if (paths != null)
  362. {
  363. foreach (var path in paths)
  364. {
  365. if (path == null)
  366. continue;
  367. string absolutePath = path;
  368. if (!Path.IsPathRooted(absolutePath))
  369. absolutePath = Path.Combine(resourceDir, path);
  370. if (string.IsNullOrEmpty(absolutePath))
  371. continue;
  372. if (PathEx.IsPartOf(destinationFolder, absolutePath) || PathEx.Compare(absolutePath, destinationFolder))
  373. continue;
  374. string destination = Path.Combine(destinationFolder, Path.GetFileName(absolutePath));
  375. if (ProjectLibrary.Exists(path))
  376. ProjectLibrary.Move(path, destination, true);
  377. else
  378. ProjectLibrary.Copy(path, destination, true);
  379. }
  380. }
  381. ClearHoverHighlight();
  382. autoScrollAmount = 0;
  383. }
  384. private void ClearHoverHighlight()
  385. {
  386. if (!string.IsNullOrEmpty(hoverHighlightPath))
  387. {
  388. ElementEntry previousUnderCursorElem;
  389. if (entryLookup.TryGetValue(hoverHighlightPath, out previousUnderCursorElem))
  390. previousUnderCursorElem.MarkAsHovered(false);
  391. }
  392. hoverHighlightPath = "";
  393. }
  394. public void Ping(Resource resource)
  395. {
  396. if (!string.IsNullOrEmpty(pingPath))
  397. {
  398. ElementEntry entry;
  399. if (entryLookup.TryGetValue(pingPath, out entry))
  400. entry.MarkAsPinged(false);
  401. }
  402. if (resource != null)
  403. pingPath = ProjectLibrary.GetPath(resource);
  404. else
  405. pingPath = "";
  406. if (!string.IsNullOrEmpty(pingPath))
  407. {
  408. ElementEntry entry;
  409. if (entryLookup.TryGetValue(pingPath, out entry))
  410. entry.MarkAsPinged(true);
  411. ScrollToEntry(pingPath);
  412. }
  413. }
  414. private void DeselectAll()
  415. {
  416. SetSelection(new List<string>());
  417. selectionAnchorStart = -1;
  418. selectionAnchorEnd = -1;
  419. }
  420. private void Select(string path)
  421. {
  422. ElementEntry entry;
  423. if (!entryLookup.TryGetValue(path, out entry))
  424. return;
  425. bool ctrlDown = Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl);
  426. bool shiftDown = Input.IsButtonHeld(ButtonCode.LeftShift) || Input.IsButtonHeld(ButtonCode.RightShift);
  427. if (shiftDown)
  428. {
  429. if (selectionAnchorStart != -1 && selectionAnchorStart < entries.Count)
  430. {
  431. int start = Math.Min(entry.index, selectionAnchorStart);
  432. int end = Math.Max(entry.index, selectionAnchorStart);
  433. List<string> newSelection = new List<string>();
  434. for(int i = start; i <= end; i++)
  435. newSelection.Add(entries[i].path);
  436. SetSelection(newSelection);
  437. selectionAnchorEnd = entry.index;
  438. }
  439. else
  440. {
  441. SetSelection(new List<string>() {path});
  442. selectionAnchorStart = entry.index;
  443. selectionAnchorEnd = entry.index;
  444. }
  445. }
  446. else if (ctrlDown)
  447. {
  448. List<string> newSelection = new List<string>(selectionPaths);
  449. if (selectionPaths.Contains(path))
  450. {
  451. newSelection.Remove(path);
  452. if (newSelection.Count == 0)
  453. DeselectAll();
  454. else
  455. {
  456. if (selectionAnchorStart == entry.index)
  457. {
  458. ElementEntry newAnchorEntry;
  459. if (!entryLookup.TryGetValue(newSelection[0], out newAnchorEntry))
  460. selectionAnchorStart = -1;
  461. else
  462. selectionAnchorStart = newAnchorEntry.index;
  463. }
  464. if (selectionAnchorEnd == entry.index)
  465. {
  466. ElementEntry newAnchorEntry;
  467. if (!entryLookup.TryGetValue(newSelection[newSelection.Count - 1], out newAnchorEntry))
  468. selectionAnchorEnd = -1;
  469. else
  470. selectionAnchorEnd = newAnchorEntry.index;
  471. }
  472. SetSelection(newSelection);
  473. }
  474. }
  475. else
  476. {
  477. newSelection.Add(path);
  478. SetSelection(newSelection);
  479. selectionAnchorEnd = entry.index;
  480. }
  481. }
  482. else
  483. {
  484. SetSelection(new List<string>() { path });
  485. selectionAnchorStart = entry.index;
  486. selectionAnchorEnd = entry.index;
  487. }
  488. }
  489. private void MoveSelection(MoveDirection dir)
  490. {
  491. string newPath = "";
  492. if (selectionPaths.Count == 0 || selectionAnchorEnd == -1)
  493. {
  494. // Nothing is selected so we arbitrarily select first or last element
  495. if (entries.Count > 0)
  496. {
  497. switch (dir)
  498. {
  499. case MoveDirection.Left:
  500. case MoveDirection.Up:
  501. newPath = entries[0].path;
  502. break;
  503. case MoveDirection.Right:
  504. case MoveDirection.Down:
  505. newPath = entries[entries.Count - 1].path;
  506. break;
  507. }
  508. }
  509. }
  510. else
  511. {
  512. switch (dir)
  513. {
  514. case MoveDirection.Left:
  515. if (selectionAnchorEnd - 1 > 0)
  516. newPath = entries[selectionAnchorEnd - 1].path;
  517. break;
  518. case MoveDirection.Up:
  519. if (selectionAnchorEnd - elementsPerRow > 0)
  520. newPath = entries[selectionAnchorEnd - elementsPerRow].path;
  521. break;
  522. case MoveDirection.Right:
  523. if (selectionAnchorEnd + 1 < entries.Count)
  524. newPath = entries[selectionAnchorEnd + 1].path;
  525. break;
  526. case MoveDirection.Down:
  527. if (selectionAnchorEnd + elementsPerRow > 0)
  528. newPath = entries[selectionAnchorEnd + elementsPerRow].path;
  529. break;
  530. }
  531. }
  532. if (!string.IsNullOrEmpty(newPath))
  533. {
  534. Select(newPath);
  535. ScrollToEntry(newPath);
  536. }
  537. }
  538. private void SetSelection(List<string> paths)
  539. {
  540. if (selectionPaths != null)
  541. {
  542. foreach (var path in selectionPaths)
  543. {
  544. ElementEntry entry;
  545. if (entryLookup.TryGetValue(path, out entry))
  546. entry.MarkAsSelected(false);
  547. }
  548. }
  549. selectionPaths = paths;
  550. if (selectionPaths != null)
  551. {
  552. foreach (var path in selectionPaths)
  553. {
  554. ElementEntry entry;
  555. if (entryLookup.TryGetValue(path, out entry))
  556. entry.MarkAsSelected(true);
  557. }
  558. }
  559. Ping(null);
  560. if (selectionPaths != null)
  561. Selection.resourcePaths = selectionPaths.ToArray();
  562. else
  563. Selection.resourcePaths = new string[0];
  564. }
  565. private void EnterDirectory(string directory)
  566. {
  567. currentDirectory = directory;
  568. DeselectAll();
  569. Refresh();
  570. }
  571. private void Cut(IEnumerable<string> sourcePaths)
  572. {
  573. foreach (var path in cutPaths)
  574. {
  575. ElementEntry entry;
  576. if (entryLookup.TryGetValue(path, out entry))
  577. entry.MarkAsCut(false);
  578. }
  579. cutPaths.Clear();
  580. cutPaths.AddRange(sourcePaths);
  581. foreach (var path in cutPaths)
  582. {
  583. ElementEntry entry;
  584. if (entryLookup.TryGetValue(path, out entry))
  585. entry.MarkAsCut(true);
  586. }
  587. copyPaths.Clear();
  588. }
  589. private void Copy(IEnumerable<string> sourcePaths)
  590. {
  591. copyPaths.Clear();
  592. copyPaths.AddRange(sourcePaths);
  593. foreach (var path in cutPaths)
  594. {
  595. ElementEntry entry;
  596. if (entryLookup.TryGetValue(path, out entry))
  597. entry.MarkAsCut(false);
  598. }
  599. cutPaths.Clear();
  600. }
  601. private void Duplicate(IEnumerable<string> sourcePaths)
  602. {
  603. foreach (var source in sourcePaths)
  604. {
  605. int idx = 0;
  606. string destination;
  607. do
  608. {
  609. destination = source + "_" + idx;
  610. idx++;
  611. } while (!ProjectLibrary.Exists(destination));
  612. ProjectLibrary.Copy(source, destination);
  613. }
  614. }
  615. private void Paste(string destinationFolder)
  616. {
  617. if (copyPaths.Count > 0)
  618. {
  619. for (int i = 0; i < copyPaths.Count; i++)
  620. {
  621. string destination = Path.Combine(destinationFolder, Path.GetFileName(copyPaths[i]));
  622. ProjectLibrary.Copy(copyPaths[i], destination, true);
  623. }
  624. Refresh();
  625. }
  626. else if (cutPaths.Count > 0)
  627. {
  628. for (int i = 0; i < cutPaths.Count; i++)
  629. {
  630. string destination = Path.Combine(destinationFolder, Path.GetFileName(cutPaths[i]));
  631. ProjectLibrary.Move(cutPaths[i], destination, true);
  632. }
  633. cutPaths.Clear();
  634. Refresh();
  635. }
  636. }
  637. private void EditorUpdate()
  638. {
  639. if (HasContentFocus)
  640. {
  641. if (Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl))
  642. {
  643. if (Input.IsButtonUp(ButtonCode.C))
  644. {
  645. CopySelection();
  646. }
  647. else if (Input.IsButtonUp(ButtonCode.X))
  648. {
  649. CutSelection();
  650. }
  651. else if (Input.IsButtonUp(ButtonCode.D))
  652. {
  653. DuplicateSelection();
  654. }
  655. else if (Input.IsButtonUp(ButtonCode.V))
  656. {
  657. PasteToSelection();
  658. }
  659. }
  660. if (Input.IsButtonDown(ButtonCode.Return))
  661. {
  662. if (selectionPaths.Count == 1)
  663. {
  664. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  665. if (entry != null && entry.Type == LibraryEntryType.Directory)
  666. {
  667. EnterDirectory(entry.Path);
  668. }
  669. }
  670. }
  671. else if (Input.IsButtonDown(ButtonCode.Back))
  672. {
  673. LibraryEntry entry = ProjectLibrary.GetEntry(currentDirectory);
  674. if (entry != null && entry.Parent != null)
  675. {
  676. EnterDirectory(entry.Parent.Path);
  677. }
  678. }
  679. else if (Input.IsButtonDown(ButtonCode.Up))
  680. {
  681. MoveSelection(MoveDirection.Up);
  682. }
  683. else if (Input.IsButtonDown(ButtonCode.Down))
  684. {
  685. MoveSelection(MoveDirection.Down);
  686. }
  687. else if (Input.IsButtonDown(ButtonCode.Left))
  688. {
  689. MoveSelection(MoveDirection.Left);
  690. }
  691. else if (Input.IsButtonDown(ButtonCode.Right))
  692. {
  693. MoveSelection(MoveDirection.Right);
  694. }
  695. }
  696. if (autoScrollAmount != 0)
  697. {
  698. Rect2I contentBounds = contentScrollArea.ContentBounds;
  699. float scrollPct = autoScrollAmount / (float)contentBounds.height;
  700. contentScrollArea.VerticalScroll += scrollPct * Time.FrameDelta;
  701. }
  702. dropTarget.Update();
  703. }
  704. private void OnEntryChanged(string entry)
  705. {
  706. Refresh();
  707. }
  708. private void ScrollToEntry(string path)
  709. {
  710. ElementEntry entryGUI;
  711. if (!entryLookup.TryGetValue(path, out entryGUI))
  712. return;
  713. Rect2I entryBounds = entryGUI.Bounds;
  714. Rect2I contentBounds = contentScrollArea.Layout.Bounds;
  715. entryBounds.x += contentBounds.x;
  716. entryBounds.y += contentBounds.y;
  717. Rect2I scrollAreaBounds = scrollAreaPanel.Bounds;
  718. bool requiresScroll = entryBounds.y < scrollAreaBounds.y ||
  719. (entryBounds.y + entryBounds.height) > (scrollAreaBounds.y + scrollAreaBounds.height);
  720. if (!requiresScroll)
  721. return;
  722. float percent = (entryBounds.y - entryBounds.height * 0.5f - scrollAreaBounds.height * 0.5f) / contentBounds.height;
  723. percent = MathEx.Clamp01(percent);
  724. contentScrollArea.VerticalScroll = percent;
  725. }
  726. private void Refresh()
  727. {
  728. DirectoryEntry entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  729. if (entry == null)
  730. {
  731. Reset();
  732. return;
  733. }
  734. if (scrollAreaPanel != null)
  735. scrollAreaPanel.Destroy();
  736. entries.Clear();
  737. entryLookup.Clear();
  738. scrollAreaPanel = contentScrollArea.Layout.AddPanel();
  739. ContentInfo contentInfo = new ContentInfo(this, viewType);
  740. Rect2I scrollBounds = contentScrollArea.Bounds;
  741. int availableWidth = scrollBounds.width;
  742. LibraryEntry[] childEntries = entry.Children;
  743. if (childEntries.Length == 0)
  744. return;
  745. if (viewType == ProjectViewType.List16)
  746. {
  747. for (int i = 0; i < childEntries.Length; i++)
  748. {
  749. ElementEntry guiEntry = new ElementEntry(contentInfo, contentInfo.main, childEntries[i], i);
  750. entries.Add(guiEntry);
  751. entryLookup[guiEntry.path] = guiEntry;
  752. if (i != childEntries.Length - 1)
  753. contentInfo.main.AddSpace(LIST_ENTRY_SPACING);
  754. }
  755. contentInfo.main.AddFlexibleSpace();
  756. elementsPerRow = 1;
  757. }
  758. else
  759. {
  760. int tileSize = 64;
  761. switch (viewType)
  762. {
  763. case ProjectViewType.Grid64: tileSize = 64; break;
  764. case ProjectViewType.Grid48: tileSize = 48; break;
  765. case ProjectViewType.Grid32: tileSize = 32; break;
  766. }
  767. GUILayoutX rowLayout = contentInfo.main.AddLayoutX();
  768. rowLayout.AddFlexibleSpace();
  769. int elemSize = tileSize + GRID_ENTRY_SPACING;
  770. elementsPerRow = (availableWidth - GRID_ENTRY_SPACING * 2) / elemSize;
  771. int numRows = MathEx.CeilToInt(childEntries.Length / (float)elementsPerRow);
  772. int neededHeight = numRows*(elemSize);
  773. bool requiresScrollbar = neededHeight > scrollBounds.height;
  774. if (requiresScrollbar)
  775. {
  776. availableWidth -= contentScrollArea.ScrollBarWidth;
  777. elementsPerRow = (availableWidth - GRID_ENTRY_SPACING * 2) / elemSize;
  778. }
  779. int elemsInRow = 0;
  780. for (int i = 0; i < childEntries.Length; i++)
  781. {
  782. if (elemsInRow == elementsPerRow && elemsInRow > 0)
  783. {
  784. rowLayout = contentInfo.main.AddLayoutX();
  785. contentInfo.main.AddSpace(GRID_ENTRY_SPACING);
  786. rowLayout.AddFlexibleSpace();
  787. elemsInRow = 0;
  788. }
  789. ElementEntry guiEntry = new ElementEntry(contentInfo, rowLayout, childEntries[i], i);
  790. entries.Add(guiEntry);
  791. entryLookup[guiEntry.path] = guiEntry;
  792. rowLayout.AddFlexibleSpace();
  793. elemsInRow++;
  794. }
  795. int extraElements = elementsPerRow - elemsInRow;
  796. for (int i = 0; i < extraElements; i++)
  797. {
  798. rowLayout.AddSpace(tileSize);
  799. rowLayout.AddFlexibleSpace();
  800. }
  801. contentInfo.main.AddFlexibleSpace();
  802. }
  803. for (int i = 0; i < entries.Count; i++)
  804. {
  805. ElementEntry guiEntry = entries[i];
  806. guiEntry.Initialize();
  807. if (cutPaths.Contains(guiEntry.path))
  808. guiEntry.MarkAsCut(true);
  809. if (selectionPaths.Contains(guiEntry.path))
  810. guiEntry.MarkAsSelected(true);
  811. else if (pingPath == guiEntry.path)
  812. guiEntry.MarkAsPinged(true);
  813. }
  814. Rect2I contentBounds = contentInfo.main.Bounds;
  815. Rect2I minimalBounds = GetScrollAreaBounds();
  816. contentBounds.height = Math.Max(contentBounds.height, minimalBounds.height);
  817. GUIButton catchAll = new GUIButton("", EditorStyles.Blank);
  818. catchAll.Bounds = contentBounds;
  819. catchAll.OnClick += OnCatchAllClicked;
  820. catchAll.SetContextMenu(entryContextMenu);
  821. catchAll.OnFocusChanged += OnContentsFocusChanged;
  822. contentInfo.underlay.AddElement(catchAll);
  823. }
  824. private void OnContentsFocusChanged(bool focus)
  825. {
  826. hasContentFocus = focus;
  827. }
  828. private void OnEntryClicked(string path)
  829. {
  830. Select(path);
  831. }
  832. private void OnEntryDoubleClicked(string path)
  833. {
  834. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  835. if (entry != null && entry.Type == LibraryEntryType.Directory)
  836. {
  837. EnterDirectory(path);
  838. }
  839. }
  840. private void OnCatchAllClicked()
  841. {
  842. DeselectAll();
  843. }
  844. private void CutSelection()
  845. {
  846. if (selectionPaths.Count > 0)
  847. Cut(selectionPaths);
  848. }
  849. private void CopySelection()
  850. {
  851. if (selectionPaths.Count > 0)
  852. Copy(selectionPaths);
  853. }
  854. private void DuplicateSelection()
  855. {
  856. if (selectionPaths.Count > 0)
  857. Duplicate(selectionPaths);
  858. }
  859. private void PasteToSelection()
  860. {
  861. DirectoryEntry selectedDirectory = null;
  862. if (selectionPaths.Count == 1)
  863. {
  864. LibraryEntry entry = ProjectLibrary.GetEntry(selectionPaths[0]);
  865. if (entry != null && entry.Type == LibraryEntryType.Directory)
  866. selectedDirectory = (DirectoryEntry) entry;
  867. }
  868. if(selectedDirectory != null)
  869. Paste(selectedDirectory.Path);
  870. else
  871. Paste(currentDirectory);
  872. }
  873. private void OnSearchChanged(string newValue)
  874. {
  875. // TODO
  876. }
  877. private void ClearSearch()
  878. {
  879. // TODO
  880. }
  881. private void OpenOptionsWindow()
  882. {
  883. Vector2I openPosition;
  884. Rect2I buttonBounds = GUILayoutUtility.CalculateBounds(optionsButton, GUI);
  885. openPosition.x = buttonBounds.x + buttonBounds.width / 2;
  886. openPosition.y = buttonBounds.y + buttonBounds.height / 2;
  887. ProjectDropDown dropDown = DropDownWindow.Open<ProjectDropDown>(this, openPosition);
  888. dropDown.SetParent(this);
  889. }
  890. private void Reset()
  891. {
  892. currentDirectory = ProjectLibrary.Root.Path;
  893. selectionAnchorStart = -1;
  894. selectionAnchorEnd = -1;
  895. selectionPaths.Clear();
  896. pingPath = "";
  897. hoverHighlightPath = "";
  898. Refresh();
  899. }
  900. private Rect2I GetScrollAreaBounds()
  901. {
  902. Rect2I bounds = GUI.Bounds;
  903. Rect2I searchBarBounds = searchBarLayout.Bounds;
  904. bounds.y += searchBarBounds.height;
  905. bounds.height -= searchBarBounds.height;
  906. return bounds;
  907. }
  908. protected override void WindowResized(int width, int height)
  909. {
  910. base.WindowResized(width, height);
  911. Refresh();
  912. dropTarget.Bounds = contentScrollArea.Bounds;
  913. }
  914. }
  915. internal class ProjectDropDown : DropDownWindow
  916. {
  917. private ProjectWindow parent;
  918. public ProjectDropDown()
  919. :base(150, 30)
  920. { }
  921. internal void SetParent(ProjectWindow parent)
  922. {
  923. this.parent = parent;
  924. GUIToggleGroup group = new GUIToggleGroup();
  925. GUIToggle list16 = new GUIToggle("16", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  926. GUIToggle grid32 = new GUIToggle("32", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  927. GUIToggle grid48 = new GUIToggle("48", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  928. GUIToggle grid64 = new GUIToggle("64", group, EditorStyles.Button, GUIOption.FixedWidth(30));
  929. ProjectViewType activeType = parent.ViewType;
  930. switch (activeType)
  931. {
  932. case ProjectViewType.List16:
  933. list16.ToggleOn();
  934. break;
  935. case ProjectViewType.Grid32:
  936. grid32.ToggleOn();
  937. break;
  938. case ProjectViewType.Grid48:
  939. grid48.ToggleOn();
  940. break;
  941. case ProjectViewType.Grid64:
  942. grid64.ToggleOn();
  943. break;
  944. }
  945. list16.OnToggled += (active) =>
  946. {
  947. if (active)
  948. ChangeViewType(ProjectViewType.List16);
  949. };
  950. grid32.OnToggled += (active) =>
  951. {
  952. if (active)
  953. ChangeViewType(ProjectViewType.Grid32);
  954. };
  955. grid48.OnToggled += (active) =>
  956. {
  957. if (active)
  958. ChangeViewType(ProjectViewType.Grid48);
  959. };
  960. grid64.OnToggled += (active) =>
  961. {
  962. if (active)
  963. ChangeViewType(ProjectViewType.Grid64);
  964. };
  965. GUILayoutY vertLayout = GUI.AddLayoutY();
  966. vertLayout.AddFlexibleSpace();
  967. GUILayoutX contentLayout = vertLayout.AddLayoutX();
  968. contentLayout.AddFlexibleSpace();
  969. contentLayout.AddElement(list16);
  970. contentLayout.AddElement(grid32);
  971. contentLayout.AddElement(grid48);
  972. contentLayout.AddElement(grid64);
  973. contentLayout.AddFlexibleSpace();
  974. vertLayout.AddFlexibleSpace();
  975. }
  976. private void ChangeViewType(ProjectViewType viewType)
  977. {
  978. parent.ViewType = viewType;
  979. }
  980. }
  981. }