Kaynağa Gözat

Added GUIListField and slightly refactored how GUI lists store their types

BearishSun 10 yıl önce
ebeveyn
işleme
9e2a00e41a

+ 0 - 5
MBansheeEditor/GUI/GUIDictionaryField.cs

@@ -243,10 +243,7 @@ namespace BansheeEditor
         /// Array object whose contents are displayed.
         /// </summary>
         public IDictionary Dictionary { get { return dictionary; } }
-
         protected IDictionary dictionary;
-        protected Type keyType;
-        protected Type valueType;
 
         /// <summary>
         /// Constructs a new empty dictionary GUI.
@@ -268,8 +265,6 @@ namespace BansheeEditor
             GUILayout layout, int depth = 0)
             where RowType : GUIDictionaryFieldRow, new()
         {
-            this.keyType = typeof(Key);
-            this.valueType = typeof(Value);
             this.dictionary = dictionary;
 
             if (dictionary != null)

+ 172 - 18
MBansheeEditor/GUI/GUIListField.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Collections;
 using System.Collections.Generic;
 using BansheeEngine;
 
@@ -245,26 +246,25 @@ namespace BansheeEditor
     /// Creates GUI elements that allow viewing and manipulation of a <see cref="System.Array"/>. When constructing the
     /// object user can provide a custom type that manages GUI for individual array elements.
     /// </summary>
-    public class GUIArrayField : GUIListFieldBase
+    /// <typeparam name="ElementType">Type of elements stored in the array.</typeparam>
+    public class GUIArrayField<ElementType> : GUIListFieldBase
     {
         /// <summary>
         /// Triggered when the reference array has been changed. This does not include changes that only happen to its 
         /// internal elements.
         /// </summary>
-        public Action<Array> OnChanged;
+        public Action<ElementType[]> OnChanged;
 
         /// <summary>
-        /// Triggered when an element in the list has been changed.
+        /// Triggered when an element in the array has been changed.
         /// </summary>
         public Action OnValueChanged;
 
         /// <summary>
         /// Array object whose contents are displayed.
         /// </summary>
-        public Array Array { get { return array; } }
-
-        protected Array array;
-        protected Type arrayType;
+        public ElementType[] Array { get { return array; } }
+        protected ElementType[] array;
 
         /// <summary>
         /// Constructs a new empty GUI array.
@@ -275,18 +275,16 @@ namespace BansheeEditor
         /// <summary>
         /// Updates the GUI array contents. Must be called at least once in order for the contents to be populated.
         /// </summary>
-        /// <typeparam name="RowType">Type of rows that are used to handle GUI for individual list elements.</typeparam>
-        /// <typeparam name="ElementType">Type of elements stored in the array.</typeparam>
-        /// <param name="title">Label to display on the list GUI title.</param>
-        /// <param name="array">Object containing the list data. Can be null.</param>
-        /// <param name="layout">Layout to which to append the list GUI elements to.</param>
+        /// <typeparam name="RowType">Type of rows that are used to handle GUI for individual array elements.</typeparam>
+        /// <param name="title">Label to display on the array GUI title.</param>
+        /// <param name="array">Object containing the array data. Can be null.</param>
+        /// <param name="layout">Layout to which to append the array GUI elements to.</param>
         /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
         ///                     nested containers whose backgrounds are overlaping. Also determines background style,
         ///                     depths divisible by two will use an alternate style.</param>
-        public void Update<RowType, ElementType>(LocString title, ElementType[] array, GUILayout layout, int depth = 0) 
+        public void Update<RowType>(LocString title, ElementType[] array, GUILayout layout, int depth = 0) 
             where RowType : GUIListFieldRow, new() 
         {
-            this.arrayType = typeof(ElementType[]);
             this.array = array;
 
             if (array != null)
@@ -313,7 +311,7 @@ namespace BansheeEditor
         /// <inheritdoc/>
         protected override void OnCreateButtonClicked()
         {
-            array = Array.CreateInstance(arrayType.GetElementType(), 0);
+            array = new ElementType[0];
 
             if (OnChanged != null)
                 OnChanged(array);
@@ -324,7 +322,7 @@ namespace BansheeEditor
         {
             int size = guiSizeField.Value;
 
-            Array newArray = Array.CreateInstance(arrayType.GetElementType(), size);
+            ElementType[] newArray = new ElementType[size];
 
             int maxSize = MathEx.Min(size, array.GetLength(0));
 
@@ -350,7 +348,7 @@ namespace BansheeEditor
         protected internal override void OnDeleteButtonClicked(int index)
         {
             int size = MathEx.Max(0, array.GetLength(0) - 1);
-            Array newArray = Array.CreateInstance(arrayType.GetElementType(), size);
+            ElementType[] newArray = new ElementType[size];
 
             int destIdx = 0;
             for (int i = 0; i < array.GetLength(0); i++)
@@ -372,7 +370,7 @@ namespace BansheeEditor
         protected internal override void OnCloneButtonClicked(int index)
         {
             int size = array.GetLength(0) + 1;
-            Array newArray = Array.CreateInstance(arrayType.GetElementType(), size);
+            ElementType[] newArray = new ElementType[size];
 
             object clonedEntry = null;
             for (int i = 0; i < array.GetLength(0); i++)
@@ -428,6 +426,162 @@ namespace BansheeEditor
         }
     }
 
+    /// <summary>
+    /// Creates GUI elements that allow viewing and manipulation of a <see cref="List{T}"/>. When constructing the
+    /// object user can provide a custom type that manages GUI for individual list elements.
+    /// </summary>
+    /// <typeparam name="ElementType">Type of elements stored in the list.</typeparam>
+    public class GUIListField<ElementType> : GUIListFieldBase
+    {
+        /// <summary>
+        /// Triggered when the reference list has been changed. This does not include changes that only happen to its 
+        /// internal elements.
+        /// </summary>
+        public Action<List<ElementType>> OnChanged;
+
+        /// <summary>
+        /// Triggered when an element in the list has been changed.
+        /// </summary>
+        public Action OnValueChanged;
+
+        /// <summary>
+        /// List object whose contents are displayed.
+        /// </summary>
+        public List<ElementType> List { get { return list; } }
+        protected List<ElementType> list;
+
+        /// <summary>
+        /// Constructs a new empty GUI array.
+        /// </summary>
+        public GUIListField()
+        { }
+
+        /// <summary>
+        /// Updates the GUI list contents. Must be called at least once in order for the contents to be populated.
+        /// </summary>
+        /// <typeparam name="RowType">Type of rows that are used to handle GUI for individual list elements.</typeparam>
+        /// <param name="title">Label to display on the list GUI title.</param>
+        /// <param name="list">Object containing the list data. Can be null.</param>
+        /// <param name="layout">Layout to which to append the list GUI elements to.</param>
+        /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
+        ///                     nested containers whose backgrounds are overlaping. Also determines background style,
+        ///                     depths divisible by two will use an alternate style.</param>
+        public void Update<RowType>(LocString title, List<ElementType> list, GUILayout layout, int depth = 0)
+            where RowType : GUIListFieldRow, new()
+        {
+            this.list = list;
+
+            if (list != null)
+                base.Update<RowType>(title, false, list.Count, layout, depth);
+            else
+                base.Update<RowType>(title, true, 0, layout, depth);
+        }
+
+        /// <inheritdoc/>
+        protected internal override object GetValue(int seqIndex)
+        {
+            return list[seqIndex];
+        }
+
+        /// <inheritdoc/>
+        protected internal override void SetValue(int seqIndex, object value)
+        {
+            list[seqIndex] = (ElementType)value;
+
+            if (OnValueChanged != null)
+                OnValueChanged();
+        }
+
+        /// <inheritdoc/>
+        protected override void OnCreateButtonClicked()
+        {
+            list = new List<ElementType>();
+
+            if (OnChanged != null)
+                OnChanged(list);
+        }
+
+        /// <inheritdoc/>
+        protected override void OnResizeButtonClicked()
+        {
+            int size = guiSizeField.Value;
+            if(size == list.Count)
+                return;
+
+            if (size < list.Count)
+                list.RemoveRange(size, list.Count - size);
+            else
+            {
+                ElementType[] extraElements = new ElementType[size - list.Count];
+                list.AddRange(extraElements);
+            }
+
+            if (OnChanged != null)
+                OnValueChanged();
+        }
+
+        /// <inheritdoc/>
+        protected override void OnClearButtonClicked()
+        {
+            list = null;
+
+            if (OnChanged != null)
+                OnChanged(list);
+        }
+
+        /// <inheritdoc/>
+        protected internal override void OnDeleteButtonClicked(int index)
+        {
+            list.RemoveAt(index);
+
+            if (OnValueChanged != null)
+                OnValueChanged();
+        }
+
+        /// <inheritdoc/>
+        protected internal override void OnCloneButtonClicked(int index)
+        {
+            object clonedEntry = null;
+            if (list[index] != null)
+                clonedEntry = SerializableUtility.Clone(list[index]);
+
+            list.Add((ElementType)clonedEntry);
+            
+            if (OnValueChanged != null)
+                OnValueChanged();
+        }
+
+        /// <inheritdoc/>
+        protected internal override void OnMoveUpButtonClicked(int index)
+        {
+            if ((index - 1) >= 0)
+            {
+                ElementType previousEntry = list[index - 1];
+
+                list[index - 1] = list[index];
+                list[index] = previousEntry;
+
+                if (OnValueChanged != null)
+                    OnValueChanged();
+            }
+        }
+
+        /// <inheritdoc/>
+        protected internal override void OnMoveDownButtonClicked(int index)
+        {
+            if ((index + 1) < list.Count)
+            {
+                ElementType nextEntry = list[index + 1];
+
+                list[index + 1] = list[index];
+                list[index] = nextEntry;
+
+                if (OnValueChanged != null)
+                    OnValueChanged();
+            }
+        }
+    }
+
     /// <summary>
     /// Contains GUI elements for a single entry in a list.
     /// </summary>

+ 4 - 4
MBansheeEditor/Inspectors/FontInspector.cs

@@ -10,8 +10,8 @@ namespace BansheeEditor
     [CustomInspector(typeof(Font))]
     internal class FontInspector : Inspector
     {
-        private GUIArrayField fontSizes = new GUIArrayField();
-        private GUIArrayField charRanges = new GUIArrayField();
+        private GUIArrayField<int> fontSizes = new GUIArrayField<int>();
+        private GUIArrayField<CharRange> charRanges = new GUIArrayField<CharRange>();
         private GUIToggleField antialiasingField;
         private GUIIntField dpiField;
         private GUIButton reimportButton;
@@ -88,7 +88,7 @@ namespace BansheeEditor
         {
             layout.Clear();
 
-            fontSizes.Update<FontSizeArrayRow, int>(
+            fontSizes.Update<FontSizeArrayRow>(
                 new LocEdString("Font sizes"), importOptions.FontSizes, layout);
             fontSizes.OnChanged += x =>
             {
@@ -99,7 +99,7 @@ namespace BansheeEditor
                 Refresh();
             };
 
-            charRanges.Update<CharRangeArrayRow, CharRange>(
+            charRanges.Update<CharRangeArrayRow>(
                 new LocEdString("Character ranges"), importOptions.CharRanges, layout);
             charRanges.OnChanged += x =>
             {

+ 2 - 2
MBansheeEditor/Inspectors/RenderableInspector.cs

@@ -12,7 +12,7 @@ namespace BansheeEditor
     {
         private GUIResourceField meshField;
         private GUIListBoxField layersField;
-        private GUIArrayField materialsField = new GUIArrayField();
+        private GUIArrayField<Material> materialsField = new GUIArrayField<Material>();
         private List<MaterialParamGUI[]> materialParams = new List<MaterialParamGUI[]>();
 
         private ulong layersValue = 0;
@@ -115,7 +115,7 @@ namespace BansheeEditor
 
             layersValue = 0;
             materials = renderable.Materials;
-            materialsField.Update<MaterialArrayRow, Material>(new LocEdString("Materials"), materials, layout);
+            materialsField.Update<MaterialArrayRow>(new LocEdString("Materials"), materials, layout);
 
             materialsField.OnChanged += x =>
             {

+ 24 - 0
MBansheeEditor/Inspectors/ShaderInspector.cs

@@ -0,0 +1,24 @@
+using System.Collections.Generic;
+using BansheeEngine;
+
+namespace BansheeEditor
+{
+    /// <summary>
+    /// Renders an inspector for the <see cref="Shader"/> resource.
+    /// </summary>
+    [CustomInspector(typeof(Shader))]
+    internal class ShaderInspector : Inspector
+    {
+        /// <inheritdoc/>
+        protected internal override void Initialize()
+        {
+            // No GUI for shader resource
+        }
+
+        /// <inheritdoc/>
+        protected internal override bool Refresh()
+        {
+            return false;
+        }
+    }
+}

+ 2 - 0
MBansheeEditor/MBansheeEditor.csproj

@@ -65,7 +65,9 @@
     <Compile Include="Inspectors\PrefabInspector.cs" />
     <Compile Include="Inspectors\RenderableInspector.cs" />
     <Compile Include="Inspectors\ScriptCodeInspector.cs" />
+    <Compile Include="Inspectors\ShaderInspector.cs" />
     <Compile Include="Inspectors\SpriteTextureInspector.cs" />
+    <Compile Include="Inspectors\StringTableInspector.cs" />
     <Compile Include="Inspectors\Texture2DInspector.cs" />
     <Compile Include="Inspector\InspectorUtility.cs" />
     <Compile Include="Library\LibraryGUIContent.cs" />