Browse Source

Bugfix: Clicking on the gradient picker texture won't recursively open another gradient picker

BearishSun 7 years ago
parent
commit
75464a6662

+ 41 - 9
Source/Scripting/MBansheeEditor/Windows/GradientPicker.cs

@@ -15,17 +15,23 @@ namespace BansheeEditor
     /// </summary>
     /// </summary>
     public class GradientPicker : ModalWindow
     public class GradientPicker : ModalWindow
     {
     {
-        private int EDITOR_HORZ_PADDING = 10;
+        private const int EDITOR_HORZ_PADDING = 10;
+        private const int TEX_WIDTH = 256;
+        private const int TEX_HEIGHT = 4;
 
 
         private ColorGradient gradient;
         private ColorGradient gradient;
         private GradientKeyEditor editor;
         private GradientKeyEditor editor;
-        private GUIColorGradient guiGradient;
+        private GUITexture guiGradientTexture;
         private GUICanvas overlayCanvas;
         private GUICanvas overlayCanvas;
 
 
         private GUIPanel editorPanel;
         private GUIPanel editorPanel;
         private GUIButton guiOK;
         private GUIButton guiOK;
         private GUIButton guiCancel;
         private GUIButton guiCancel;
 
 
+        private Texture texture;
+        private SpriteTexture spriteTexture;
+
+
         private Action<bool, ColorGradient> closedCallback;
         private Action<bool, ColorGradient> closedCallback;
 
 
         /// <summary>
         /// <summary>
@@ -108,11 +114,15 @@ namespace BansheeEditor
             GUILayout guiGradientLayout = editorVertLayout.AddLayoutX();
             GUILayout guiGradientLayout = editorVertLayout.AddLayoutX();
             guiGradientLayout.AddSpace(GradientKeyEditor.RECT_WIDTH / 2);
             guiGradientLayout.AddSpace(GradientKeyEditor.RECT_WIDTH / 2);
 
 
-            guiGradient = new GUIColorGradient();
-            guiGradient.Gradient = gradient;
-            guiGradient.SetHeight(30);
+            texture = Texture.Create2D(TEX_WIDTH, TEX_HEIGHT);
+            spriteTexture = new SpriteTexture(texture);
+
+            guiGradientTexture = new GUITexture(spriteTexture, GUITextureScaleMode.ScaleToFit);
+            guiGradientTexture.SetHeight(30);
+
+            UpdateTexture();
 
 
-            guiGradientLayout.AddElement(guiGradient);
+            guiGradientLayout.AddElement(guiGradientTexture);
             guiGradientLayout.AddSpace(GradientKeyEditor.RECT_WIDTH / 2);
             guiGradientLayout.AddSpace(GradientKeyEditor.RECT_WIDTH / 2);
 
 
             editorVertLayout.AddSpace(10);
             editorVertLayout.AddSpace(10);
@@ -122,7 +132,7 @@ namespace BansheeEditor
             {
             {
                 gradient = colorGradient;
                 gradient = colorGradient;
 
 
-                guiGradient.Gradient = gradient;
+                UpdateTexture();
                 UpdateKeyLines();
                 UpdateKeyLines();
             };
             };
 
 
@@ -173,6 +183,28 @@ namespace BansheeEditor
             }
             }
         }
         }
 
 
+        /// <summary>
+        /// Updates the gradient texture.
+        /// </summary>
+        public void UpdateTexture()
+        {
+            const int width = 256;
+            const int height = 4;
+            Color[] colors = new Color[width * height];
+
+            float halfPixel = 0.5f / width;
+            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;
+            }
+
+            texture.SetPixels(colors);
+            guiGradientTexture.SetTexture(spriteTexture);
+        }
+
         /// <summary>
         /// <summary>
         /// Triggered when the user selects a gradient and closes the dialog.
         /// Triggered when the user selects a gradient and closes the dialog.
         /// </summary>
         /// </summary>
@@ -216,7 +248,7 @@ namespace BansheeEditor
                 return;
                 return;
 
 
             Vector2I panelPos = ScreenToKeyEditorPos(ev.ScreenPos);
             Vector2I panelPos = ScreenToKeyEditorPos(ev.ScreenPos);
-            Rect2I guiGradientBounds = guiGradient.Bounds;
+            Rect2I guiGradientBounds = guiGradientTexture.Bounds;
 
 
             if (guiGradientBounds.Contains(panelPos))
             if (guiGradientBounds.Contains(panelPos))
             {
             {
@@ -232,7 +264,7 @@ namespace BansheeEditor
 
 
                     gradient = new ColorGradient(keys.ToArray());
                     gradient = new ColorGradient(keys.ToArray());
 
 
-                    guiGradient.Gradient = gradient;
+                    UpdateTexture();
                     editor.Rebuild(keys);
                     editor.Rebuild(keys);
                     UpdateKeyLines();
                     UpdateKeyLines();
                 }
                 }

+ 1 - 1
Source/Scripting/SBansheeEngine/Extensions/BsTextureEx.h

@@ -75,7 +75,7 @@ namespace bs
 		static SPtr<PixelData> getPixels(const HTexture& thisPtr, UINT32 face = 0, UINT32 mipLevel = 0);
 		static SPtr<PixelData> getPixels(const HTexture& thisPtr, UINT32 face = 0, UINT32 mipLevel = 0);
 
 
 		/**
 		/**
-		 * Reads texture pixels directly from the GPU. This is similar to GetPixels" but the texture doesn't
+		 * Reads texture pixels directly from the GPU. This is similar to GetPixels but the texture doesn't
 		 * need to be created with TextureUsage.CPUCached, and the data will contain any updates performed by
 		 * need to be created with TextureUsage.CPUCached, and the data will contain any updates performed by
 		 * the GPU. This method can be potentially slow as it introduces a CPU-GPU synchronization point. Additionally
 		 * the GPU. This method can be potentially slow as it introduces a CPU-GPU synchronization point. Additionally
 		 * this method is asynchronous which means the data is not available immediately.
 		 * this method is asynchronous which means the data is not available immediately.