Texture2DInspector.cs 4.7 KB

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