SpriteTextureInspector.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using bs;
  6. namespace bs.Editor
  7. {
  8. /** @addtogroup Inspectors
  9. * @{
  10. */
  11. /// <summary>
  12. /// Renders an inspector for the <see cref="SpriteTexture"/> resource.
  13. /// </summary>
  14. [CustomInspector(typeof(SpriteTexture))]
  15. internal class SpriteTextureInspector : Inspector
  16. {
  17. private GUILayoutWithBackground previewTitleLayout;
  18. private GUILayoutWithBackground previewContentLayout;
  19. private GUITexture previewTexture;
  20. /// <inheritdoc/>
  21. protected internal override void Initialize()
  22. {
  23. LoadResource();
  24. SpriteTexture spriteTexture = InspectedObject as SpriteTexture;
  25. if (spriteTexture == null)
  26. return;
  27. drawer.AddDefault(spriteTexture);
  28. GUILayout previewLayout = PreviewGUI.AddLayoutY();
  29. previewTitleLayout = GUILayoutWithBackground.Create<GUILayoutX>(previewLayout, Builtin.WhiteTexture,
  30. new Color(0.129f, 0.129f, 0.129f), new RectOffset(11, 0, 2, 0));
  31. GUILabel title = new GUILabel(new LocEdString("Preview"));
  32. previewTitleLayout.Layout.AddElement(title);
  33. previewTitleLayout.Layout.AddFlexibleSpace();
  34. previewContentLayout = GUILayoutWithBackground.Create<GUILayoutX>(previewLayout, Builtin.WhiteTexture,
  35. new Color(0.09f, 0.09f, 0.09f), new RectOffset(5, 5, 5, 5));
  36. previewContentLayout.MainPanel.SetHeight(250);
  37. previewTexture = new GUITexture(spriteTexture, GUITextureScaleMode.ScaleToFit,
  38. GUIOption.FlexibleWidth(), GUIOption.FlexibleHeight());
  39. previewContentLayout.Layout.AddElement(previewTexture);
  40. }
  41. /// <inheritdoc/>
  42. protected internal override InspectableState Refresh()
  43. {
  44. SpriteTexture spriteTexture = InspectedObject as SpriteTexture;
  45. if (spriteTexture == null)
  46. return InspectableState.NotModified;
  47. InspectableState state = drawer.Refresh();
  48. if (state != InspectableState.NotModified)
  49. {
  50. EditorApplication.SetDirty(spriteTexture);
  51. // The inspector will by default just assign a resource reference without loading it, make sure we load it
  52. // so it can be previewed
  53. if (spriteTexture.Texture != null && !spriteTexture.Texture.IsLoaded)
  54. Resources.Load<Texture>(spriteTexture.Texture.UUID);
  55. // Make sure GUI redraws as the sprite texture properties were updated
  56. previewTexture.SetTexture(spriteTexture);
  57. }
  58. return state;
  59. }
  60. }
  61. /** @} */
  62. }