Explorar o código

Refactor: Inspector field creation encapsulated in its own method

BearishSun %!s(int64=7) %!d(string=hai) anos
pai
achega
6ff5692662

+ 4 - 4
Source/EditorManaged/MBansheeEditor.csproj.in

@@ -1,16 +1,17 @@
 <?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
-    <ProjectGuid>{ABC62E37-342E-4345-A374-E37CA06C036E}</ProjectGuid>
+    <ProjectGuid>{abc62e37-342e-4345-a374-e37ca06c036e}</ProjectGuid>
     <OutputType>Library</OutputType>
     <AppDesignerFolder>Properties</AppDesignerFolder>
     <RootNamespace>${BS_SHARP_ROOT_NS}</RootNamespace>
     <AssemblyName>${BS_SHARP_ASSEMBLY_NAME}</AssemblyName>
     <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
+	<Deterministic>true</Deterministic>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
@@ -20,10 +21,9 @@
     <DefineConstants>${BS_SHARP_DEFINES}DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
-    <UseVSHostingProcess>false</UseVSHostingProcess>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
-    <DebugType>none</DebugType>
+    <DebugType>pdbonly</DebugType>
     <Optimize>true</Optimize>
     <OutputPath>${BS_SHARP_ASSEMBLY_OUTPUT}\Release\</OutputPath>
     <DefineConstants>${BS_SHARP_DEFINES}TRACE</DefineConstants>

+ 2 - 15
Source/EditorManaged/Windows/Inspector/GenericInspector.cs

@@ -26,22 +26,9 @@ namespace BansheeEditor
 
             if (InspectedObject != null)
             {
-                int currentIndex = 0;
                 SerializableObject serializableObject = new SerializableObject(InspectedObject.GetType(), InspectedObject);
-                foreach (var field in serializableObject.Fields)
-                {
-                    if (!field.Flags.HasFlag(SerializableFieldAttributes.Inspectable))
-                        continue;
-                    
-                    string path = field.Name;
-                    InspectableField inspectableField = InspectableField.CreateInspectable(this, field.Name, path,
-                        currentIndex, 0, new InspectableFieldLayout(Layout), field.GetProperty(), InspectableFieldStyle.Create(field));
-
-                    inspectableFields.Add(inspectableField);
-                    isEmpty = false;
-
-                    currentIndex += inspectableField.GetNumLayoutElements();
-                }
+                inspectableFields = InspectableField.CreateFields(serializableObject, this, "", 0, Layout);
+                isEmpty = inspectableFields.Count > 0;
 
                 base.SetVisible(!isEmpty);
             }

+ 1 - 1
Source/EditorManaged/Windows/Inspector/InspectableArray.cs

@@ -383,7 +383,7 @@ namespace BansheeEditor
                 styleInfo.StyleFlags &= ~InspectableFieldStyleFlags.NativeWrapper;
 
                 string entryPath = arrayParent.Path + "[" + SeqIndex + "]";
-                field = CreateInspectable(arrayParent.Inspector, SeqIndex + ".", entryPath, 0, Depth + 1,
+                field = CreateField(arrayParent.Inspector, SeqIndex + ".", entryPath, 0, Depth + 1,
                     new InspectableFieldLayout(layout), property, styleInfo);
 
                 return field.GetTitleLayout();

+ 2 - 2
Source/EditorManaged/Windows/Inspector/InspectableDictionary.cs

@@ -374,7 +374,7 @@ namespace BansheeEditor
                 SerializableProperty property = GetKey<SerializableProperty>();
 
                 string entryPath = dictParent.Path + "Key[" + RowIdx + "]";
-                fieldKey = CreateInspectable(dictParent.Inspector, "Key", entryPath, 0, Depth + 1,
+                fieldKey = CreateField(dictParent.Inspector, "Key", entryPath, 0, Depth + 1,
                     new InspectableFieldLayout(layout), property);
 
                 return fieldKey.GetTitleLayout();
@@ -387,7 +387,7 @@ namespace BansheeEditor
                 SerializableProperty property = GetValue<SerializableProperty>();
 
                 string entryPath = dictParent.Path + "Value[" + RowIdx + "]";
-                fieldValue = CreateInspectable(dictParent.Inspector, "Value", entryPath, 0, Depth + 1,
+                fieldValue = CreateField(dictParent.Inspector, "Value", entryPath, 0, Depth + 1,
                     new InspectableFieldLayout(layout), property);
             }
 

+ 73 - 1
Source/EditorManaged/Windows/Inspector/InspectableField.cs

@@ -1,6 +1,7 @@
 //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
 //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
 using System;
+using System.Collections.Generic;
 using BansheeEngine;
  
 namespace BansheeEditor
@@ -115,6 +116,77 @@ namespace BansheeEditor
             layout.DestroyElements();
         }
 
+        /// <summary>
+        /// Allows the user to override the default inspector GUI for a specific field in an object. If this method
+        /// returns null the default field will be used instead.
+        /// </summary>
+        /// <param name="field">Field to generate inspector GUI for.</param>
+        /// <param name="parent">Parent Inspector the GUI will be rendered on.</param>
+        /// <param name="path">Full path to the provided field (includes name of this field and all parent fields).</param>
+        /// <param name="layout">Parent layout that all the field elements will be added to.</param>
+        /// <param name="layoutIndex">Index into the parent layout at which to insert the GUI elements for the field .</param>
+        /// <param name="depth">
+        /// Determines how deep within the inspector nesting hierarchy is this field. Some fields may contain other fields,
+        /// in which case you should increase this value by one.
+        /// </param>
+        /// <returns>
+        /// Inspectable field implementation that can be used for displaying the GUI for the provided field. Or null if
+        /// default field GUI should be used instead.
+        /// </returns>
+        public delegate InspectableField FieldOverrideCallback(SerializableField field, Inspector parent, string path,
+            InspectableFieldLayout layout, int layoutIndex, int depth);
+
+        /// <summary>
+        /// Creates inspectable fields all the fields/properties of the specified object.
+        /// </summary>
+        /// <param name="obj">Object whose fields the GUI will be drawn for.</param>
+        /// <param name="parent">Parent Inspector to draw in.</param>
+        /// <param name="path">Full path to the field this provided object was retrieved from.</param>
+        /// <param name="depth">
+        /// Determines how deep within the inspector nesting hierarchy is this objects. Some fields may contain other
+        /// fields, in which case you should increase this value by one.
+        /// </param>
+        /// <param name="layout">Parent layout that all the field GUI elements will be added to.</param>
+        /// <param name="overrideCallback">
+        /// Optional callback that allows you to override the look of individual fields in the object. If non-null the
+        /// callback will be called with information about every field in the provided object. If the callback returns
+        /// non-null that inspectable field will be used for drawing the GUI, otherwise the default inspector field type
+        /// will be used.
+        /// </param>
+        public static List<InspectableField> CreateFields(SerializableObject obj, Inspector parent, string path, 
+            int depth, GUILayoutY layout, FieldOverrideCallback overrideCallback = null)
+        {
+            List<InspectableField> fields = new List<InspectableField>();
+
+            int currentIndex = 0;
+            foreach (var field in obj.Fields)
+            {
+                if (!field.Flags.HasFlag(SerializableFieldAttributes.Inspectable))
+                    continue;
+
+                string fieldName = field.Name;
+                string childPath = string.IsNullOrEmpty(path) ? fieldName : path + "/" + fieldName;
+
+                InspectableField inspectableField = null;
+
+                if(overrideCallback != null)
+                    inspectableField = overrideCallback(field, parent, path, new InspectableFieldLayout(layout), 
+                        currentIndex, depth);
+
+                if (inspectableField == null)
+                {
+                    inspectableField = CreateField(parent, fieldName, childPath,
+                        currentIndex, depth, new InspectableFieldLayout(layout), field.GetProperty(), 
+                        InspectableFieldStyle.Create(field));
+                }
+
+                fields.Add(inspectableField);
+                currentIndex += inspectableField.GetNumLayoutElements();
+            }
+
+            return fields;
+        }
+
         /// <summary>
         /// Creates a new inspectable field, automatically detecting the most appropriate implementation for the type
         /// contained in the provided serializable property. This may be one of the built-in inspectable field implemetations
@@ -132,7 +204,7 @@ namespace BansheeEditor
         /// <param name="style">Information that can be used for customizing field rendering and behaviour.</param>
         /// <returns>Inspectable field implementation that can be used for displaying the GUI for a serializable property
         ///          of the provided type.</returns>
-        public static InspectableField CreateInspectable(Inspector parent, string title, string path, int layoutIndex, 
+        public static InspectableField CreateField(Inspector parent, string title, string path, int layoutIndex, 
             int depth, InspectableFieldLayout layout, SerializableProperty property, InspectableFieldStyleInfo style = null)
         {
             InspectableField field = null;

+ 1 - 1
Source/EditorManaged/Windows/Inspector/InspectableList.cs

@@ -290,7 +290,7 @@ namespace BansheeEditor
                 SerializableProperty property = GetValue<SerializableProperty>();
 
                 string entryPath = listParent.Path + "[" + SeqIndex + "]";
-                field = CreateInspectable(listParent.Inspector, SeqIndex + ".", entryPath, 0, Depth + 1,
+                field = CreateField(listParent.Inspector, SeqIndex + ".", entryPath, 0, Depth + 1,
                     new InspectableFieldLayout(layout), property, new InspectableFieldStyleInfo());
 
                 return field.GetTitleLayout();

+ 1 - 14
Source/EditorManaged/Windows/Inspector/InspectableObject.cs

@@ -193,20 +193,7 @@ namespace BansheeEditor
                        GUITexture inspectorContentBg = new GUITexture(null, bgPanelStyle);
                        backgroundPanel.AddElement(inspectorContentBg);
 
-                       int currentIndex = 0;
-                       foreach (var field in fields)
-                       {
-                           if (!field.Flags.HasFlag(SerializableFieldAttributes.Inspectable))
-                               continue;
-
-                           string childPath = path + "/" + field.Name;
-
-                           InspectableField inspectable = CreateInspectable(parent, field.Name, childPath,
-                               currentIndex, depth + 1, new InspectableFieldLayout(guiContentLayout), field.GetProperty(), InspectableFieldStyle.Create(field));
-
-                           children.Add(inspectable);
-                           currentIndex += inspectable.GetNumLayoutElements();
-                       }
+                       children = CreateFields(serializableObject, parent, path, depth + 1, guiContentLayout);
                    }
                }
                else