Browse Source

Feature: Material inspector now accepts sprite textures, color gradients and animation curves
- Also fixed some visual issues with the gradient picker

BearishSun 6 years ago
parent
commit
c80e1e3905

+ 26 - 0
Source/EditorCore/Build/BsBuiltinEditorResources.cpp

@@ -608,6 +608,32 @@ namespace bs
 		return HSpriteTexture();
 	}
 
+	GUIContentImages BuiltinEditorResources::getEditorToggleIcon(EditorToggleIcon icon) const
+	{
+		HSpriteTexture off;
+		HSpriteTexture on;
+
+		switch (icon)
+		{
+		case EditorToggleIcon::AnimateProperty:
+			off = getGUIIcon("AnimPropertyIcon.png");
+			on = getGUIIcon("AnimPropertyIconOn.png");
+			break;
+		}
+
+		GUIContentImages output;
+		output.normal = off;
+		output.hover = off;
+		output.active = on;
+		output.focused = off;
+		output.normalOn = on;
+		output.hoverOn = on;
+		output.activeOn = on;
+		output.focusedOn = on;
+
+		return output;
+	}
+
 	HSpriteTexture BuiltinEditorResources::getLogMessageIcon(LogMessageIcon icon, UINT32 size, bool dark) const
 	{
 		if (size < 24) // Round to 16

+ 10 - 0
Source/EditorCore/Utility/BsBuiltinEditorResources.h

@@ -65,6 +65,12 @@ namespace bs
 		X, Component, SceneObject
 	};
 
+	/**	Types of icons used in various areas throughout the editor, for toggleable elements. */
+	enum BS_SCRIPT_EXPORT(api:bed) class EditorToggleIcon
+	{
+		AnimateProperty
+	};
+
 	/**	Types of icons to be used along with log messages depending on their severity. */
 	enum BS_SCRIPT_EXPORT(api:bed) class LogMessageIcon
 	{
@@ -176,6 +182,10 @@ namespace bs
 		BS_SCRIPT_EXPORT()
 		BS_NORREF HSpriteTexture getEditorIcon(EditorIcon icon) const;
 
+		/**	Retrieves an icon that represents a specific generic editor icon used for toggleable elements. */
+		BS_SCRIPT_EXPORT()
+		GUIContentImages getEditorToggleIcon(EditorToggleIcon icon) const;
+
 		/**	Retrieves an icon that represents a specific log message type. */
 		BS_SCRIPT_EXPORT()
 		BS_NORREF HSpriteTexture getLogMessageIcon(LogMessageIcon icon, UINT32 size, bool dark) const;

+ 10 - 0
Source/EditorManaged/Generated/BuiltinEditorResources.generated.cs

@@ -107,6 +107,14 @@ namespace bs.Editor
 			return Internal_getEditorIcon(icon);
 		}
 
+		/// <summary>Retrieves an icon that represents a specific generic editor icon used for toggleable elements.</summary>
+		public static GUIContentImages GetEditorToggleIcon(EditorToggleIcon icon)
+		{
+			GUIContentImages temp;
+			Internal_getEditorToggleIcon(icon, out temp);
+			return temp;
+		}
+
 		/// <summary>Retrieves an icon that represents a specific log message type.</summary>
 		public static SpriteTexture GetLogMessageIcon(LogMessageIcon icon, int size, bool dark)
 		{
@@ -142,6 +150,8 @@ namespace bs.Editor
 		[MethodImpl(MethodImplOptions.InternalCall)]
 		private static extern SpriteTexture Internal_getEditorIcon(EditorIcon icon);
 		[MethodImpl(MethodImplOptions.InternalCall)]
+		private static extern void Internal_getEditorToggleIcon(EditorToggleIcon icon, out GUIContentImages __output);
+		[MethodImpl(MethodImplOptions.InternalCall)]
 		private static extern SpriteTexture Internal_getLogMessageIcon(LogMessageIcon icon, int size, bool dark);
 		[MethodImpl(MethodImplOptions.InternalCall)]
 		private static extern SpriteTexture Internal_getSprite(EditorSprites sprite);

+ 15 - 0
Source/EditorManaged/Generated/EditorToggleIcon.generated.cs

@@ -0,0 +1,15 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//************** Copyright (c) 2016-2019 Marko Pintera ([email protected]). All rights reserved. *******************//
+using System;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using bs;
+
+namespace bs.Editor
+{
+	/// <summary>Types of icons used in various areas throughout the editor, for toggleable elements.</summary>
+	public enum EditorToggleIcon
+	{
+		AnimateProperty = 0
+	}
+}

+ 105 - 24
Source/EditorManaged/Inspectors/MaterialInspector.cs

@@ -1,5 +1,7 @@
 //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
 //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+
+using System;
 using System.Collections.Generic;
 using bs;
 
@@ -113,13 +115,15 @@ namespace bs.Editor
         /// <returns>Type of builtin shader, if any.</returns>
         private BuiltinShader ShaderToBuiltin(Shader shader)
         {
-            // Note: Need a better way to detect the builtin shader perhaps (store it in Material?)
-            Shader standardShader = Builtin.GetShader(BuiltinShader.Standard);
-            Shader transparentShader = Builtin.GetShader(BuiltinShader.Transparent);
-;            if(standardShader == shader)
-                return BuiltinShader.Standard;
-            else if(transparentShader == shader)
-                return BuiltinShader.Transparent;
+            // Note: Might be nice to have a better way to detect the builtin shader perhaps (store it in Material?)
+            //   - Or I could just compare UUID's here to avoid loading the shaders
+            var enumValues = Enum.GetValues(typeof(BuiltinShader));
+            for (int i = 1; i < enumValues.Length; i++)
+            {
+                Shader builtinShader = Builtin.GetShader((BuiltinShader) i);
+                if (builtinShader == shader)
+                    return (BuiltinShader) i;
+            }
             
             return BuiltinShader.Custom;
         }
@@ -220,7 +224,9 @@ namespace bs.Editor
     /// </summary>
     internal class MaterialParamFloatGUI : MaterialParamGUI
     {
-        private GUIFloatField guiElem;
+        private GUILayout fieldLayout;
+        private GUIFloatField guiConstant;
+        private GUICurvesField guiCurves;
 
         /// <summary>
         /// Creates a new material parameter GUI.
@@ -232,26 +238,55 @@ namespace bs.Editor
             : base(shaderParam)
         {
             LocString title = new LocEdString(shaderParam.name);
-            guiElem = new GUIFloatField(title);
-            guiElem.OnChanged += (x) =>
+
+            var guiToggle = new GUIToggle(new GUIContent(
+                EditorBuiltin.GetEditorToggleIcon(EditorToggleIcon.AnimateProperty), new LocString("Animate")));
+            guiConstant = new GUIFloatField(title);
+            guiCurves = new GUICurvesField(title);
+
+            bool isAnimated = material.IsAnimated(shaderParam.name);
+            guiConstant.Active = !isAnimated;
+            guiCurves.Active = isAnimated;
+
+            fieldLayout = layout.AddLayoutX();
+            fieldLayout.AddElement(guiConstant);
+            fieldLayout.AddElement(guiCurves);
+            fieldLayout.AddSpace(10);
+            fieldLayout.AddElement(guiToggle);
+
+            guiConstant.OnChanged += (x) =>
             {
                 material.SetFloat(shaderParam.name, x);
                 EditorApplication.SetDirty(material);
             };
 
-            layout.AddElement(guiElem);
+            guiCurves.OnChanged += x =>
+            {
+                material.SetFloatCurve(shaderParam.name, x);
+                EditorApplication.SetDirty(material);
+            };
+
+            guiToggle.OnToggled += x =>
+            {
+                guiConstant.Active = !x;
+                guiCurves.Active = x;
+            };
         }
 
         /// <inheritdoc/>
         internal override void Refresh(Material material)
         {
-            guiElem.Value = material.GetFloat(shaderParam.name);
+            bool isAnimated = material.IsAnimated(shaderParam.name);
+            if (isAnimated)
+                guiCurves.SetCurve(material.GetFloatCurve(shaderParam.name));
+            else
+                guiConstant.Value = material.GetFloat(shaderParam.name);
         }
 
         /// <inheritdoc/>
         internal override void Destroy()
         {
-            guiElem.Destroy();
+            fieldLayout.Destroy();
         }
     }
 
@@ -538,7 +573,9 @@ namespace bs.Editor
     /// </summary>
     internal class MaterialParamColorGUI : MaterialParamGUI
     {
-        private GUIColorField guiElem;
+        private GUILayout fieldLayout;
+        private GUIColorField guiColor;
+        private GUIColorGradientField guiColorGradient;
 
         /// <summary>
         /// Creates a new material parameter GUI.
@@ -550,26 +587,62 @@ namespace bs.Editor
             : base(shaderParam)
         {
             LocString title = new LocEdString(shaderParam.name);
-            guiElem = new GUIColorField(title);
-            guiElem.OnChanged += (x) =>
+
+            var guiToggle = new GUIToggle(new GUIContent( 
+                EditorBuiltin.GetEditorToggleIcon(EditorToggleIcon.AnimateProperty), new LocString("Animate")));
+            guiColor = new GUIColorField(title);
+            guiColorGradient = new GUIColorGradientField(title);
+
+            bool isAnimated = material.IsAnimated(shaderParam.name);
+            guiColor.Active = !isAnimated;
+            guiColorGradient.Active = isAnimated;
+
+            fieldLayout = layout.AddLayoutX();
+            fieldLayout.AddElement(guiColor);
+            fieldLayout.AddElement(guiColorGradient);
+            fieldLayout.AddSpace(10);
+            fieldLayout.AddElement(guiToggle);
+
+            guiColor.OnChanged += (x) =>
             {
                 material.SetColor(shaderParam.name, x);
                 EditorApplication.SetDirty(material);
             };
 
-            layout.AddElement(guiElem);
+            guiColorGradient.OnChanged += x =>
+            {
+                material.SetColorGradient(shaderParam.name, x);
+                EditorApplication.SetDirty(material);
+            };
+
+            guiToggle.OnToggled += x =>
+            {
+                guiColor.Active = !x;
+                guiColorGradient.Active = x;
+
+                if (x)
+                {
+                    ColorGradient gradient = material.GetColorGradient(shaderParam.name);
+                    if (gradient.NumKeys == 0)
+                        material.SetColorGradient(shaderParam.name, new ColorGradient(material.GetColor(shaderParam.name)));
+                }
+            };
         }
 
         /// <inheritdoc/>
         internal override void Refresh(Material material)
         {
-            guiElem.Value = material.GetColor(shaderParam.name);
+            bool isAnimated = material.IsAnimated(shaderParam.name);
+            if (isAnimated)
+                guiColorGradient.Value = material.GetColorGradient(shaderParam.name);
+            else
+                guiColor.Value = material.GetColor(shaderParam.name);
         }
 
         /// <inheritdoc/>
         internal override void Destroy()
         {
-            guiElem.Destroy();
+            fieldLayout.Destroy();
         }
     }
 
@@ -590,7 +663,12 @@ namespace bs.Editor
             : base(shaderParam)
         {
             LocString title = new LocEdString(shaderParam.name);
-            guiElem = new GUITextureField(title);
+
+            GUITextureFieldType type = shaderParam.type == ShaderParameterType.Texture2D ? 
+                GUITextureFieldType.TextureOrSpriteTexture :
+                GUITextureFieldType.Texture;
+
+            guiElem = new GUITextureField(type, title);
 
             switch (shaderParam.type)
             {
@@ -617,22 +695,25 @@ namespace bs.Editor
         /// <inheritdoc/>
         internal override void Refresh(Material material)
         {
+            RRef<Texture> texture;
             switch (shaderParam.type)
             {
                 case ShaderParameterType.Texture2D:
-                case ShaderParameterType.Texture3D:
-                case ShaderParameterType.TextureCube:
                     RRef<SpriteTexture> spriteTex = material.GetSpriteTexture(shaderParam.name);
 
                     if (spriteTex != null && spriteTex.IsLoaded)
                         guiElem.SpriteTextureRef = spriteTex;
                     else
                     {
-                        RRef<Texture> texture = material.GetTexture(shaderParam.name);
+                        texture = material.GetTexture(shaderParam.name);
                         guiElem.TextureRef = texture;
                     }
 
-
+                    break;
+                case ShaderParameterType.Texture3D:
+                case ShaderParameterType.TextureCube:
+                    texture = material.GetTexture(shaderParam.name);
+                    guiElem.TextureRef = texture;
                     break;
             }
         }

+ 16 - 5
Source/EditorManaged/Windows/GradientPicker.cs

@@ -117,7 +117,7 @@ namespace bs.Editor
             texture = Texture.Create2D(TEX_WIDTH, TEX_HEIGHT);
             spriteTexture = new SpriteTexture(texture);
 
-            guiGradientTexture = new GUITexture(spriteTexture, GUITextureScaleMode.ScaleToFit);
+            guiGradientTexture = new GUITexture(spriteTexture, GUITextureScaleMode.StretchToFit);
             guiGradientTexture.SetHeight(30);
 
             UpdateTexture();
@@ -193,12 +193,23 @@ namespace bs.Editor
             Color[] colors = new Color[width * height];
 
             float halfPixel = 0.5f / width;
-            for (int x = 0; x < width; x++)
+            if (gradient.NumKeys > 0)
             {
-                Color color = gradient.Evaluate(x / (float)width + halfPixel);
+                for (int x = 0; x < width; x++)
+                {
+                    Color color = gradient.Evaluate(x / (float) width + halfPixel);
 
-                for (int y = 0; y < height; y++)
-                    colors[y * width + x] = color;
+                    for (int y = 0; y < height; y++)
+                        colors[y * width + x] = color;
+                }
+            }
+            else
+            {
+                for (int x = 0; x < width; x++)
+                {
+                    for (int y = 0; y < height; y++)
+                        colors[y * width + x] = Color.Black;
+                }
             }
 
             texture.SetPixels(colors);

+ 11 - 0
Source/EditorScript/Generated/BsScriptBuiltinEditorResources.generated.cpp

@@ -31,6 +31,7 @@ namespace bs
 		metaData.scriptClass->addInternalCall("Internal_getInspectorWindowIcon", (void*)&ScriptBuiltinEditorResources::Internal_getInspectorWindowIcon);
 		metaData.scriptClass->addInternalCall("Internal_getAnimationWindowIcon", (void*)&ScriptBuiltinEditorResources::Internal_getAnimationWindowIcon);
 		metaData.scriptClass->addInternalCall("Internal_getEditorIcon", (void*)&ScriptBuiltinEditorResources::Internal_getEditorIcon);
+		metaData.scriptClass->addInternalCall("Internal_getEditorToggleIcon", (void*)&ScriptBuiltinEditorResources::Internal_getEditorToggleIcon);
 		metaData.scriptClass->addInternalCall("Internal_getLogMessageIcon", (void*)&ScriptBuiltinEditorResources::Internal_getLogMessageIcon);
 		metaData.scriptClass->addInternalCall("Internal_getSprite", (void*)&ScriptBuiltinEditorResources::Internal_getSprite);
 		metaData.scriptClass->addInternalCall("Internal_getEmptyShaderCode", (void*)&ScriptBuiltinEditorResources::Internal_getEmptyShaderCode);
@@ -202,6 +203,16 @@ namespace bs
 		return __output;
 	}
 
+	void ScriptBuiltinEditorResources::Internal_getEditorToggleIcon(EditorToggleIcon icon, __GUIContentImagesInterop* __output)
+	{
+		GUIContentImages tmp__output;
+		tmp__output = BuiltinEditorResources::instance().getEditorToggleIcon(icon);
+
+		__GUIContentImagesInterop interop__output;
+		interop__output = ScriptGUIContentImages::toInterop(tmp__output);
+		MonoUtil::valueCopy(__output, &interop__output, ScriptGUIContentImages::getMetaData()->scriptClass->_getInternalClass());
+	}
+
 	MonoObject* ScriptBuiltinEditorResources::Internal_getLogMessageIcon(LogMessageIcon icon, uint32_t size, bool dark)
 	{
 		ResourceHandle<SpriteTexture> tmp__output;

+ 2 - 0
Source/EditorScript/Generated/BsScriptBuiltinEditorResources.generated.h

@@ -15,6 +15,7 @@
 #include "../../EditorCore/Utility/BsBuiltinEditorResources.h"
 #include "../../EditorCore/Utility/BsBuiltinEditorResources.h"
 #include "../../EditorCore/Utility/BsBuiltinEditorResources.h"
+#include "../../EditorCore/Utility/BsBuiltinEditorResources.h"
 
 namespace bs
 {
@@ -40,6 +41,7 @@ namespace bs
 		static MonoObject* Internal_getInspectorWindowIcon(InspectorWindowIcon icon);
 		static void Internal_getAnimationWindowIcon(AnimationWindowIcon icon, __GUIContentImagesInterop* __output);
 		static MonoObject* Internal_getEditorIcon(EditorIcon icon);
+		static void Internal_getEditorToggleIcon(EditorToggleIcon icon, __GUIContentImagesInterop* __output);
 		static MonoObject* Internal_getLogMessageIcon(LogMessageIcon icon, uint32_t size, bool dark);
 		static MonoObject* Internal_getSprite(EditorSprites sprite);
 		static MonoString* Internal_getEmptyShaderCode();

+ 1 - 1
Source/bsf

@@ -1 +1 @@
-Subproject commit 353a183169c1382b7c65a18af78f684877b852b2
+Subproject commit 0ed090aff80bcdcb8e42fd93a9bf4d3172f8dc8c