Texture2DInspector.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System.Collections.Generic;
  2. using BansheeEngine;
  3. namespace BansheeEditor
  4. {
  5. /// <summary>
  6. /// Renders an inspector for the <see cref="Texture2D"/> resource.
  7. /// </summary>
  8. [CustomInspector(typeof(Texture2D))]
  9. internal class Texture2DInspector : Inspector
  10. {
  11. private GUIEnumField formatField = new GUIEnumField(typeof(PixelFormat), new LocEdString("Format"));
  12. private GUIToggleField generateMipsField = new GUIToggleField(new LocEdString("Generate mipmaps"));
  13. private GUIIntField maximumMipsField = new GUIIntField(new LocEdString("Maximum mipmap level"));
  14. private GUIToggleField srgbField = new GUIToggleField(new LocEdString("Gamma space"));
  15. private GUIToggleField cpuReadableField = new GUIToggleField(new LocEdString("CPU readable"));
  16. private GUIButton reimportButton = new GUIButton(new LocEdString("Reimport"));
  17. private TextureImportOptions importOptions;
  18. /// <inheritdoc/>
  19. protected internal override void Initialize()
  20. {
  21. if (InspectedObject != null)
  22. {
  23. importOptions = GetImportOptions();
  24. formatField.OnSelectionChanged += x => importOptions.Format = (PixelFormat)x;
  25. generateMipsField.OnChanged += x => importOptions.GenerateMipmaps = x;
  26. maximumMipsField.OnChanged += x => importOptions.MaxMipmapLevel = x;
  27. srgbField.OnChanged += x => importOptions.IsSRGB = x;
  28. cpuReadableField.OnChanged += x => importOptions.CPUReadable = x;
  29. reimportButton.OnClick += TriggerReimport;
  30. Layout.AddElement(formatField);
  31. Layout.AddElement(generateMipsField);
  32. Layout.AddElement(maximumMipsField);
  33. Layout.AddElement(srgbField);
  34. Layout.AddElement(cpuReadableField);
  35. Layout.AddSpace(10);
  36. GUILayout reimportButtonLayout = Layout.AddLayoutX();
  37. reimportButtonLayout.AddFlexibleSpace();
  38. reimportButtonLayout.AddElement(reimportButton);
  39. }
  40. }
  41. /// <inheritdoc/>
  42. protected internal override InspectableState Refresh()
  43. {
  44. TextureImportOptions newImportOptions = GetImportOptions();
  45. formatField.Value = (ulong)newImportOptions.Format;
  46. generateMipsField.Value = newImportOptions.GenerateMipmaps;
  47. maximumMipsField.Value = newImportOptions.MaxMipmapLevel;
  48. srgbField.Value = newImportOptions.IsSRGB;
  49. cpuReadableField.Value = newImportOptions.CPUReadable;
  50. importOptions = newImportOptions;
  51. return InspectableState.NotModified;
  52. }
  53. /// <summary>
  54. /// Retrieves import options for the texture we're currently inspecting.
  55. /// </summary>
  56. /// <returns>Texture import options object.</returns>
  57. private TextureImportOptions GetImportOptions()
  58. {
  59. Texture2D texture = InspectedObject as Texture2D;
  60. TextureImportOptions output = null;
  61. if (texture != null)
  62. {
  63. LibraryEntry texEntry = ProjectLibrary.GetEntry(ProjectLibrary.GetPath(texture));
  64. if (texEntry != null && texEntry.Type == LibraryEntryType.File)
  65. {
  66. FileEntry texFileEntry = (FileEntry)texEntry;
  67. output = texFileEntry.Options as TextureImportOptions;
  68. }
  69. }
  70. if (output == null)
  71. {
  72. if (importOptions == null)
  73. output = new TextureImportOptions();
  74. else
  75. output = importOptions;
  76. }
  77. return output;
  78. }
  79. /// <summary>
  80. /// Reimports the texture resource according to the currently set import options.
  81. /// </summary>
  82. private void TriggerReimport()
  83. {
  84. Texture2D texture = (Texture2D)InspectedObject;
  85. string resourcePath = ProjectLibrary.GetPath(texture);
  86. ProjectLibrary.Reimport(resourcePath, importOptions, true);
  87. }
  88. }
  89. }