Texture2DInspector.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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="Texture2D"/> resource.
  12. /// </summary>
  13. [CustomInspector(typeof(Texture2D))]
  14. internal class Texture2DInspector : 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 cpuReadableField = new GUIToggleField(new LocEdString("CPU readable"));
  21. private GUIButton reimportButton = new GUIButton(new LocEdString("Reimport"));
  22. private TextureImportOptions importOptions;
  23. /// <inheritdoc/>
  24. protected internal override void Initialize()
  25. {
  26. if (InspectedObject != null)
  27. {
  28. importOptions = GetImportOptions();
  29. formatField.OnSelectionChanged += x => importOptions.Format = (PixelFormat)x;
  30. generateMipsField.OnChanged += x => importOptions.GenerateMipmaps = x;
  31. maximumMipsField.OnChanged += x => importOptions.MaxMipmapLevel = x;
  32. srgbField.OnChanged += x => importOptions.IsSRGB = x;
  33. cpuReadableField.OnChanged += x => importOptions.CPUReadable = x;
  34. reimportButton.OnClick += TriggerReimport;
  35. Layout.AddElement(formatField);
  36. Layout.AddElement(generateMipsField);
  37. Layout.AddElement(maximumMipsField);
  38. Layout.AddElement(srgbField);
  39. Layout.AddElement(cpuReadableField);
  40. Layout.AddSpace(10);
  41. GUILayout reimportButtonLayout = Layout.AddLayoutX();
  42. reimportButtonLayout.AddFlexibleSpace();
  43. reimportButtonLayout.AddElement(reimportButton);
  44. }
  45. }
  46. /// <inheritdoc/>
  47. protected internal override InspectableState Refresh()
  48. {
  49. TextureImportOptions newImportOptions = GetImportOptions();
  50. formatField.Value = (ulong)newImportOptions.Format;
  51. generateMipsField.Value = newImportOptions.GenerateMipmaps;
  52. maximumMipsField.Value = newImportOptions.MaxMipmapLevel;
  53. srgbField.Value = newImportOptions.IsSRGB;
  54. cpuReadableField.Value = newImportOptions.CPUReadable;
  55. importOptions = newImportOptions;
  56. return InspectableState.NotModified;
  57. }
  58. /// <summary>
  59. /// Retrieves import options for the texture we're currently inspecting.
  60. /// </summary>
  61. /// <returns>Texture import options object.</returns>
  62. private TextureImportOptions GetImportOptions()
  63. {
  64. Texture2D texture = InspectedObject as Texture2D;
  65. TextureImportOptions output = null;
  66. if (texture != null)
  67. {
  68. LibraryEntry texEntry = ProjectLibrary.GetEntry(ProjectLibrary.GetPath(texture));
  69. if (texEntry != null && texEntry.Type == LibraryEntryType.File)
  70. {
  71. FileEntry texFileEntry = (FileEntry)texEntry;
  72. output = texFileEntry.Options as TextureImportOptions;
  73. }
  74. }
  75. if (output == null)
  76. {
  77. if (importOptions == null)
  78. output = new TextureImportOptions();
  79. else
  80. output = importOptions;
  81. }
  82. return output;
  83. }
  84. /// <summary>
  85. /// Reimports the texture resource according to the currently set import options.
  86. /// </summary>
  87. private void TriggerReimport()
  88. {
  89. Texture2D texture = (Texture2D)InspectedObject;
  90. string resourcePath = ProjectLibrary.GetPath(texture);
  91. ProjectLibrary.Reimport(resourcePath, importOptions, true);
  92. }
  93. }
  94. /** @} */
  95. }