Texture2DInspector.cs 4.4 KB

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