ProjectWindow.cs 57 KB

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