LibraryGUIEntry.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Diagnostics;
  4. using System.IO;
  5. using bs;
  6. namespace bs.Editor
  7. {
  8. /** @addtogroup Library
  9. * @{
  10. */
  11. /// <summary>
  12. /// Represents GUI for a single resource tile used in <see cref="LibraryGUIContent"/>.
  13. /// </summary>
  14. internal class LibraryGUIEntry
  15. {
  16. private static readonly Color PING_COLOR = Color.BansheeOrange;
  17. private static readonly Color SELECTION_COLOR = Color.DarkCyan;
  18. private static readonly Color HOVER_COLOR = new Color(Color.DarkCyan.r, Color.DarkCyan.g, Color.DarkCyan.b, 0.5f);
  19. private static readonly Color CUT_COLOR = new Color(1.0f, 1.0f, 1.0f, 0.5f);
  20. private const int VERT_PADDING = 3;
  21. private const int BG_HORZ_PADDING = 2;
  22. private const int BG_VERT_PADDING = 2;
  23. private const string LibraryEntryFirstBg = "LibraryEntryFirstBg";
  24. private const string LibraryEntryBg = "LibraryEntryBg";
  25. private const string LibraryEntryLastBg = "LibraryEntryLastBg";
  26. private const string LibraryEntryVertFirstBg = "LibraryEntryVertFirstBg";
  27. private const string LibraryEntryVertBg = "LibraryEntryVertBg";
  28. private const string LibraryEntryVertLastBg = "LibraryEntryVertLastBg";
  29. /// <summary>
  30. /// Possible visual states for the resource tile.
  31. /// </summary>
  32. enum UnderlayState // Note: Order of these is relevant
  33. {
  34. None, Hovered, Selected, Pinged
  35. }
  36. public int index;
  37. public string path;
  38. public GUITexture icon;
  39. public GUILabel label;
  40. public Rect2I bounds;
  41. public int spacing;
  42. private GUITexture underlay;
  43. private GUITexture groupUnderlay;
  44. private LibraryGUIContent owner;
  45. private UnderlayState underlayState;
  46. private GUITextBox renameTextBox;
  47. private int width;
  48. private LibraryGUIEntryType type;
  49. private bool delayedSelect;
  50. private float delayedSelectTime;
  51. private ulong delayedOpenCodeEditorFrame = ulong.MaxValue;
  52. /// <summary>
  53. /// Bounds of the entry relative to part content area.
  54. /// </summary>
  55. public Rect2I Bounds
  56. {
  57. get { return bounds; }
  58. }
  59. /// <summary>
  60. /// Constructs a new resource tile entry.
  61. /// </summary>
  62. /// <param name="owner">Content area this entry is part of.</param>
  63. /// <param name="parent">Parent layout to add this entry's GUI elements to.</param>
  64. /// <param name="path">Path to the project library entry to display data for.</param>
  65. /// <param name="index">Sequential index of the entry in the conent area.</param>
  66. /// <param name="width">Width of the GUI entry.</param>
  67. /// <param name="height">Maximum allowed height for the label.</param>
  68. /// <param name="spacing">Spacing between this element and the next element on the same row. 0 if last.</param>
  69. /// <param name="type">Type of the entry, which controls its style and/or behaviour.</param>
  70. public LibraryGUIEntry(LibraryGUIContent owner, GUILayout parent, string path, int index, int width, int height,
  71. int spacing, LibraryGUIEntryType type)
  72. {
  73. GUILayout entryLayout;
  74. if (owner.GridLayout)
  75. entryLayout = parent.AddLayoutY();
  76. else
  77. entryLayout = parent.AddLayoutX();
  78. SpriteTexture iconTexture = GetIcon(path, owner.TileSize);
  79. icon = new GUITexture(iconTexture, GUITextureScaleMode.ScaleToFit,
  80. true, GUIOption.FixedHeight(owner.TileSize), GUIOption.FixedWidth(owner.TileSize));
  81. label = null;
  82. string name = PathEx.GetTail(path);
  83. if (owner.GridLayout)
  84. {
  85. int labelHeight = height - owner.TileSize;
  86. label = new GUILabel(name, EditorStyles.MultiLineLabelCentered,
  87. GUIOption.FixedWidth(width), GUIOption.FixedHeight(labelHeight));
  88. switch (type)
  89. {
  90. case LibraryGUIEntryType.Single:
  91. break;
  92. case LibraryGUIEntryType.MultiFirst:
  93. groupUnderlay = new GUITexture(null, LibraryEntryFirstBg);
  94. break;
  95. case LibraryGUIEntryType.MultiElement:
  96. groupUnderlay = new GUITexture(null, LibraryEntryBg);
  97. break;
  98. case LibraryGUIEntryType.MultiLast:
  99. groupUnderlay = new GUITexture(null, LibraryEntryLastBg);
  100. break;
  101. }
  102. }
  103. else
  104. {
  105. label = new GUILabel(name, GUIOption.FixedWidth(width - owner.TileSize), GUIOption.FixedHeight(height));
  106. switch (type)
  107. {
  108. case LibraryGUIEntryType.Single:
  109. break;
  110. case LibraryGUIEntryType.MultiFirst:
  111. groupUnderlay = new GUITexture(null, LibraryEntryVertFirstBg);
  112. break;
  113. case LibraryGUIEntryType.MultiElement:
  114. groupUnderlay = new GUITexture(null, LibraryEntryVertBg);
  115. break;
  116. case LibraryGUIEntryType.MultiLast:
  117. groupUnderlay = new GUITexture(null, LibraryEntryVertLastBg);
  118. break;
  119. }
  120. }
  121. entryLayout.AddElement(icon);
  122. entryLayout.AddElement(label);
  123. if (groupUnderlay != null)
  124. owner.DeepUnderlay.AddElement(groupUnderlay);
  125. this.owner = owner;
  126. this.index = index;
  127. this.path = path;
  128. this.bounds = new Rect2I();
  129. this.underlay = null;
  130. this.type = type;
  131. this.width = width;
  132. this.spacing = spacing;
  133. }
  134. /// <summary>
  135. /// Positions the GUI elements. Must be called after construction, but only after all content area entries have
  136. /// been constructed so that entry's final bounds are known.
  137. /// </summary>
  138. public void Initialize()
  139. {
  140. Rect2I iconBounds = icon.Bounds;
  141. bounds = iconBounds;
  142. Rect2I labelBounds = label.Bounds;
  143. if (owner.GridLayout)
  144. {
  145. bounds.x = labelBounds.x;
  146. bounds.y -= VERT_PADDING;
  147. bounds.width = labelBounds.width;
  148. bounds.height = (labelBounds.y + labelBounds.height + VERT_PADDING) - bounds.y;
  149. }
  150. else
  151. {
  152. bounds.y -= VERT_PADDING;
  153. bounds.width = width;
  154. bounds.height += VERT_PADDING;
  155. }
  156. string hoistedPath = path;
  157. GUIButton overlayBtn = new GUIButton("", EditorStyles.Blank);
  158. overlayBtn.Bounds = bounds;
  159. overlayBtn.OnClick += () => OnEntryClicked(hoistedPath);
  160. overlayBtn.OnDoubleClick += () => OnEntryDoubleClicked(hoistedPath);
  161. overlayBtn.SetContextMenu(owner.Window.ContextMenu);
  162. overlayBtn.AcceptsKeyFocus = false;
  163. owner.Overlay.AddElement(overlayBtn);
  164. if (groupUnderlay != null)
  165. {
  166. if (owner.GridLayout)
  167. {
  168. bool firstInRow = index % owner.ElementsPerRow == 0;
  169. bool lastInRow = index % owner.ElementsPerRow == (owner.ElementsPerRow - 1);
  170. int offsetToPrevious = 0;
  171. if (type == LibraryGUIEntryType.MultiFirst)
  172. {
  173. if (firstInRow)
  174. offsetToPrevious = owner.PaddingLeft / 3;
  175. else
  176. offsetToPrevious = spacing / 3;
  177. }
  178. else if (firstInRow)
  179. offsetToPrevious = owner.PaddingLeft;
  180. int offsetToNext = spacing;
  181. if (type == LibraryGUIEntryType.MultiLast)
  182. {
  183. if (lastInRow)
  184. offsetToNext = owner.PaddingRight / 3;
  185. else
  186. offsetToNext = spacing / 3;
  187. }
  188. else if (lastInRow)
  189. offsetToNext = owner.PaddingRight + spacing;
  190. Rect2I bgBounds = new Rect2I(bounds.x - offsetToPrevious, bounds.y,
  191. bounds.width + offsetToNext + offsetToPrevious, bounds.height);
  192. groupUnderlay.Bounds = bgBounds;
  193. }
  194. else
  195. {
  196. int offsetToNext = BG_VERT_PADDING + LibraryGUIContent.LIST_ENTRY_SPACING;
  197. if (type == LibraryGUIEntryType.MultiLast)
  198. offsetToNext = BG_VERT_PADDING * 2;
  199. Rect2I bgBounds = new Rect2I(bounds.x, bounds.y - BG_VERT_PADDING,
  200. bounds.width, bounds.height + offsetToNext);
  201. groupUnderlay.Bounds = bgBounds;
  202. }
  203. }
  204. }
  205. /// <summary>
  206. /// Called every frame.
  207. /// </summary>
  208. public void Update()
  209. {
  210. if (delayedSelect && Time.RealElapsed > delayedSelectTime)
  211. {
  212. owner.Window.Select(path);
  213. delayedSelect = false;
  214. }
  215. if (delayedOpenCodeEditorFrame == Time.FrameIdx)
  216. {
  217. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  218. if (entry != null && entry.Type == LibraryEntryType.File)
  219. {
  220. FileEntry resEntry = (FileEntry) entry;
  221. CodeEditor.OpenFile(resEntry.Path, 0);
  222. }
  223. ProgressBar.Hide();
  224. }
  225. }
  226. /// <summary>
  227. /// Changes the visual representation of the element as being cut.
  228. /// </summary>
  229. /// <param name="enable">True if mark as cut, false to reset to normal.</param>
  230. public void MarkAsCut(bool enable)
  231. {
  232. if (enable)
  233. icon.SetTint(CUT_COLOR);
  234. else
  235. icon.SetTint(Color.White);
  236. }
  237. /// <summary>
  238. /// Changes the visual representation of the element as being selected.
  239. /// </summary>
  240. /// <param name="enable">True if mark as selected, false to reset to normal.</param>
  241. public void MarkAsSelected(bool enable)
  242. {
  243. if ((int)underlayState > (int)UnderlayState.Selected)
  244. return;
  245. if (enable)
  246. {
  247. CreateUnderlay();
  248. underlay.SetTint(SELECTION_COLOR);
  249. underlayState = UnderlayState.Selected;
  250. }
  251. else
  252. {
  253. ClearUnderlay();
  254. underlayState = UnderlayState.None;
  255. }
  256. }
  257. /// <summary>
  258. /// Changes the visual representation of the element as being pinged.
  259. /// </summary>
  260. /// <param name="enable">True if mark as pinged, false to reset to normal.</param>
  261. public void MarkAsPinged(bool enable)
  262. {
  263. if ((int)underlayState > (int)UnderlayState.Pinged)
  264. return;
  265. if (enable)
  266. {
  267. CreateUnderlay();
  268. underlay.SetTint(PING_COLOR);
  269. underlayState = UnderlayState.Pinged;
  270. }
  271. else
  272. {
  273. ClearUnderlay();
  274. underlayState = UnderlayState.None;
  275. }
  276. }
  277. /// <summary>
  278. /// Changes the visual representation of the element as being hovered over.
  279. /// </summary>
  280. /// <param name="enable">True if mark as hovered, false to reset to normal.</param>
  281. public void MarkAsHovered(bool enable)
  282. {
  283. if ((int)underlayState > (int)UnderlayState.Hovered)
  284. return;
  285. if (enable)
  286. {
  287. CreateUnderlay();
  288. underlay.SetTint(HOVER_COLOR);
  289. underlayState = UnderlayState.Hovered;
  290. }
  291. else
  292. {
  293. ClearUnderlay();
  294. underlayState = UnderlayState.None;
  295. }
  296. }
  297. /// <summary>
  298. /// Starts a rename operation over the entry, displaying the rename input box.
  299. /// </summary>
  300. public void StartRename()
  301. {
  302. if (renameTextBox != null)
  303. return;
  304. renameTextBox = new GUITextBox(true);
  305. Rect2I renameBounds = label.Bounds;
  306. // Rename box allows for less space for text than label, so adjust it slightly so it's more likely to be able
  307. // to display all visible text.
  308. renameBounds.x -= 4;
  309. renameBounds.width += 8;
  310. renameBounds.height += 8;
  311. renameTextBox.Bounds = renameBounds;
  312. owner.RenameOverlay.AddElement(renameTextBox);
  313. string name = Path.GetFileNameWithoutExtension(PathEx.GetTail(path));
  314. renameTextBox.Text = name;
  315. renameTextBox.Focus = true;
  316. }
  317. /// <summary>
  318. /// Stops a rename operation over the entry, hiding the rename input box.
  319. /// </summary>
  320. public void StopRename()
  321. {
  322. if (renameTextBox != null)
  323. {
  324. renameTextBox.Destroy();
  325. renameTextBox = null;
  326. }
  327. }
  328. /// <summary>
  329. /// Gets the new name of the entry. Only valid while a rename operation is in progress.
  330. /// </summary>
  331. /// <returns>New name of the entry currently entered in the rename input box.</returns>
  332. public string GetRenamedName()
  333. {
  334. if (renameTextBox != null)
  335. return renameTextBox.Text;
  336. return "";
  337. }
  338. /// <summary>
  339. /// Clears the underlay GUI element (for example ping, hover, select).
  340. /// </summary>
  341. private void ClearUnderlay()
  342. {
  343. if (underlay != null)
  344. {
  345. underlay.Destroy();
  346. underlay = null;
  347. }
  348. underlayState = UnderlayState.None;
  349. }
  350. /// <summary>
  351. /// Creates a GUI elements that may be used for underlay effects (for example ping, hover, select).
  352. /// </summary>
  353. private void CreateUnderlay()
  354. {
  355. if (underlay == null)
  356. {
  357. underlay = new GUITexture(Builtin.WhiteTexture);
  358. underlay.Bounds = Bounds;
  359. owner.Underlay.AddElement(underlay);
  360. }
  361. }
  362. /// <summary>
  363. /// Triggered when the user clicks on the entry.
  364. /// </summary>
  365. /// <param name="path">Project library path of the clicked entry.</param>
  366. private void OnEntryClicked(string path)
  367. {
  368. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  369. if (entry != null && entry.Type == LibraryEntryType.Directory)
  370. {
  371. // If entry is a directory delay selection as it might be a double-click, in which case we want to keep
  372. // whatever selection is active currently so that user can perform drag and drop with its inspector
  373. // from the folder he is browsing to.
  374. delayedSelect = true;
  375. delayedSelectTime = Time.RealElapsed + 0.5f;
  376. }
  377. else
  378. owner.Window.Select(path);
  379. }
  380. /// <summary>
  381. /// Triggered when the user double-clicked on the entry.
  382. /// </summary>
  383. /// <param name="path">Project library path of the double-clicked entry.</param>
  384. private void OnEntryDoubleClicked(string path)
  385. {
  386. delayedSelect = false;
  387. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  388. if (entry != null)
  389. {
  390. if (entry.Type == LibraryEntryType.Directory)
  391. owner.Window.EnterDirectory(path);
  392. else
  393. {
  394. ResourceMeta meta = ProjectLibrary.GetMeta(path);
  395. FileEntry fileEntry = (FileEntry)entry;
  396. if (meta.ResType == ResourceType.Prefab)
  397. {
  398. EditorApplication.LoadScene(fileEntry.Path);
  399. }
  400. else if (meta.ResType == ResourceType.ScriptCode)
  401. {
  402. ProgressBar.Show("Opening external code editor...", 1.0f);
  403. delayedOpenCodeEditorFrame = Time.FrameIdx + 1;
  404. }
  405. else if(meta.ResType == ResourceType.PlainText || meta.ResType == ResourceType.Shader || meta.ResType == ResourceType.ShaderInclude)
  406. {
  407. string absPath = Path.Combine(ProjectLibrary.ResourceFolder, fileEntry.Path);
  408. Process.Start(absPath);
  409. }
  410. }
  411. }
  412. }
  413. /// <summary>
  414. /// Returns an icon that can be used for displaying a resource of the specified type.
  415. /// </summary>
  416. /// <param name="path">Path to the project library entry to display data for.</param>
  417. /// <param name="size">Size of the icon to retrieve, in pixels.</param>
  418. /// <returns>Icon to display for the specified entry.</returns>
  419. private static SpriteTexture GetIcon(string path, int size)
  420. {
  421. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  422. if (entry.Type == LibraryEntryType.Directory)
  423. {
  424. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Folder, size);
  425. }
  426. else
  427. {
  428. ResourceMeta meta = ProjectLibrary.GetMeta(path);
  429. ProjectResourceIcons icons = meta.Icons;
  430. RRef<Texture> icon;
  431. if (size <= 16)
  432. icon = icons.icon16;
  433. else if (size <= 32)
  434. icon = icons.icon32;
  435. else if (size <= 48)
  436. icon = icons.icon48;
  437. else
  438. icon = icons.icon64;
  439. if (icon.Value != null)
  440. return new SpriteTexture(icon);
  441. switch (meta.ResType)
  442. {
  443. case ResourceType.Font:
  444. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Font, size);
  445. case ResourceType.Mesh:
  446. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Mesh, size);
  447. case ResourceType.Texture:
  448. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Texture, size);
  449. case ResourceType.PlainText:
  450. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.PlainText, size);
  451. case ResourceType.ScriptCode:
  452. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.ScriptCode, size);
  453. case ResourceType.SpriteTexture:
  454. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.SpriteTexture, size);
  455. case ResourceType.Shader:
  456. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Shader, size);
  457. case ResourceType.ShaderInclude:
  458. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Shader, size);
  459. case ResourceType.Material:
  460. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Material, size);
  461. case ResourceType.Prefab:
  462. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Prefab, size);
  463. case ResourceType.GUISkin:
  464. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.GUISkin, size);
  465. case ResourceType.PhysicsMaterial:
  466. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.PhysicsMaterial, size);
  467. case ResourceType.PhysicsMesh:
  468. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.PhysicsMesh, size);
  469. case ResourceType.AudioClip:
  470. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.AudioClip, size);
  471. case ResourceType.AnimationClip:
  472. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.AnimationClip, size);
  473. case ResourceType.VectorField:
  474. return EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.VectorField, size);
  475. }
  476. }
  477. return null;
  478. }
  479. }
  480. /// <summary>
  481. /// Type of <see cref="LibraryGUIEntry"/> that controls its look.
  482. /// </summary>
  483. internal enum LibraryGUIEntryType
  484. {
  485. /// <summary>
  486. /// Represents a single resource.
  487. /// </summary>
  488. Single,
  489. /// <summary>
  490. /// Represents the first entry in a multi-resource group.
  491. /// </summary>
  492. MultiFirst,
  493. /// <summary>
  494. /// Represents one of the mid entries in a multi-resource group.
  495. /// </summary>
  496. MultiElement,
  497. /// <summary>
  498. /// Represents the last entry in a multi-resource group.
  499. /// </summary>
  500. MultiLast
  501. }
  502. /** @} */
  503. }