TextureInspector.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /** @addtogroup Inspectors
  8. * @{
  9. */
  10. /// <summary>
  11. /// Renders an inspector for the <see cref="Texture"/> resource.
  12. /// </summary>
  13. [CustomInspector(typeof(Texture))]
  14. internal class TextureInspector : Inspector
  15. {
  16. private GUIEnumField formatField = new GUIEnumField(typeof(PixelFormat), new LocEdString("Format"));
  17. private GUIToggleField generateMipsField = new GUIToggleField(new LocEdString("Generate mipmaps"));
  18. private GUIIntField maximumMipsField = new GUIIntField(new LocEdString("Maximum mipmap level"));
  19. private GUIToggleField srgbField = new GUIToggleField(new LocEdString("Gamma space"));
  20. private GUIToggleField cpuCachedField = new GUIToggleField(new LocEdString("CPU cached"));
  21. private GUIToggleField isCubemapField = new GUIToggleField(new LocEdString("Cubemap"));
  22. private GUIEnumField cubemapSourceTypeField =
  23. new GUIEnumField(typeof(CubemapSourceType), new LocEdString("Cubemap source"));
  24. private GUIButton reimportButton = new GUIButton(new LocEdString("Reimport"));
  25. private TextureImportOptions importOptions;
  26. /// <inheritdoc/>
  27. protected internal override void Initialize()
  28. {
  29. if (InspectedObject != null)
  30. {
  31. importOptions = GetImportOptions();
  32. formatField.OnSelectionChanged += x => importOptions.Format = (PixelFormat)x;
  33. generateMipsField.OnChanged += x => importOptions.GenerateMipmaps = x;
  34. maximumMipsField.OnChanged += x => importOptions.MaxMipmapLevel = x;
  35. srgbField.OnChanged += x => importOptions.IsSRGB = x;
  36. cpuCachedField.OnChanged += x => importOptions.CPUCached = x;
  37. isCubemapField.OnChanged += x => importOptions.IsCubemap = x;
  38. cubemapSourceTypeField.OnSelectionChanged += x => importOptions.CubemapSourceType = (CubemapSourceType)x;
  39. reimportButton.OnClick += TriggerReimport;
  40. Layout.AddElement(formatField);
  41. Layout.AddElement(generateMipsField);
  42. Layout.AddElement(maximumMipsField);
  43. Layout.AddElement(srgbField);
  44. Layout.AddElement(cpuCachedField);
  45. Layout.AddElement(isCubemapField);
  46. Layout.AddElement(cubemapSourceTypeField);
  47. Layout.AddSpace(10);
  48. GUILayout reimportButtonLayout = Layout.AddLayoutX();
  49. reimportButtonLayout.AddFlexibleSpace();
  50. reimportButtonLayout.AddElement(reimportButton);
  51. }
  52. }
  53. /// <inheritdoc/>
  54. protected internal override InspectableState Refresh()
  55. {
  56. TextureImportOptions newImportOptions = GetImportOptions();
  57. formatField.Value = (ulong)newImportOptions.Format;
  58. generateMipsField.Value = newImportOptions.GenerateMipmaps;
  59. maximumMipsField.Value = newImportOptions.MaxMipmapLevel;
  60. srgbField.Value = newImportOptions.IsSRGB;
  61. cpuCachedField.Value = newImportOptions.CPUCached;
  62. isCubemapField.Value = newImportOptions.IsCubemap;
  63. cubemapSourceTypeField.Value = (ulong) newImportOptions.CubemapSourceType;
  64. cubemapSourceTypeField.Active = importOptions.IsCubemap;
  65. importOptions = newImportOptions;
  66. return InspectableState.NotModified;
  67. }
  68. /// <summary>
  69. /// Retrieves import options for the texture we're currently inspecting.
  70. /// </summary>
  71. /// <returns>Texture import options object.</returns>
  72. private TextureImportOptions GetImportOptions()
  73. {
  74. Texture texture = InspectedObject as Texture;
  75. TextureImportOptions output = null;
  76. if (texture != null)
  77. {
  78. LibraryEntry texEntry = ProjectLibrary.GetEntry(ProjectLibrary.GetPath(texture));
  79. if (texEntry != null && texEntry.Type == LibraryEntryType.File)
  80. {
  81. FileEntry texFileEntry = (FileEntry)texEntry;
  82. output = texFileEntry.Options as TextureImportOptions;
  83. }
  84. }
  85. if (output == null)
  86. {
  87. if (importOptions == null)
  88. output = new TextureImportOptions();
  89. else
  90. output = importOptions;
  91. }
  92. return output;
  93. }
  94. /// <summary>
  95. /// Reimports the texture resource according to the currently set import options.
  96. /// </summary>
  97. private void TriggerReimport()
  98. {
  99. Texture texture = (Texture)InspectedObject;
  100. string resourcePath = ProjectLibrary.GetPath(texture);
  101. ProjectLibrary.Reimport(resourcePath, importOptions, true);
  102. }
  103. }
  104. /** @} */
  105. }