Exemplo n.º 1
0
        public static string BlueprintIdSelection(
            GUIContent label,
            string selectedBlueprintId,
            InspectorTypeTable inspectorComponentTypes,
            IBlueprintManager blueprintManager)
        {
            // Store all blueprint ids for access from a pulldown menu.
            var blueprintIds = blueprintManager.Blueprints.Select(blueprint => blueprint.Key).ToArray();

            // Show blueprint dropdown.
            var oldSelectedBlueprintIndex = Array.IndexOf(blueprintIds, selectedBlueprintId);
            var selectedBlueprintIndex    = EditorGUILayout.Popup(
                label,
                oldSelectedBlueprintIndex,
                blueprintIds.Select(blueprintId => new GUIContent(blueprintId)).ToArray());

            if (selectedBlueprintIndex != oldSelectedBlueprintIndex)
            {
                // Update selected blueprint of the target entity.
                return(blueprintIds[selectedBlueprintIndex >= 0 ? selectedBlueprintIndex : 0]);
            }
            else
            {
                return(selectedBlueprintId);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///   Draws an inspector for the passed attribute table.
        /// </summary>
        /// <param name="inspectorType">Type to draw inspector controls for.</param>
        /// <param name="attributeTable">Attribute to draw inspector for.</param>
        /// <param name="inspectorTypeTable"></param>
        /// <param name="blueprintManager"></param>
        public static void AttributeTableField(
            InspectorType inspectorType,
            IAttributeTable attributeTable,
            InspectorTypeTable inspectorTypeTable,
            IBlueprintManager blueprintManager)
        {
            foreach (var inspectorProperty in inspectorType.Properties)
            {
                // Get current value.
                object currentValue = attributeTable.GetValueOrDefault(
                    inspectorProperty.Name,
                    inspectorProperty.Default);

                // Draw inspector property.
                object newValue = LogicInspectorPropertyField(
                    inspectorProperty,
                    currentValue,
                    inspectorTypeTable,
                    blueprintManager);

                // Set new value if changed.
                if (!Equals(newValue, currentValue))
                {
                    attributeTable.SetValue(inspectorProperty.Name, newValue);
                }
            }
        }
        /// <summary>
        ///   Draws the game configuration inspector.
        /// </summary>
        public override void OnInspectorGUI()
        {
            this.DrawDefaultInspector();

            // Collect system types.
            if (this.inspectorSystemTypes == null)
            {
                this.inspectorSystemTypes = InspectorTypeTable.FindInspectorTypes(typeof(ISystem));
            }

            IAttributeTable configuration = this.gameConfiguration.Configuration;

            foreach (var inspectorType in this.inspectorSystemTypes)
            {
                // Draw inspector type.
                this.DrawInspector(inspectorType, configuration, this.inspectorSystemTypes, null);
            }

            if (GUILayout.Button("Reload"))
            {
                // Reload configuration.
                this.gameConfiguration.Load();
            }

            if (GUILayout.Button("Save"))
            {
                // Save configuration.
                // TODO(co): Save automatically if changed.
                this.Save();

                // Refresh assets.
                AssetDatabase.Refresh();
            }
        }
Exemplo n.º 4
0
        public static void BlueprintComponentsField(
            Blueprint blueprint,
            IAttributeTable configuration,
            InspectorTypeTable inspectorTypeTable,
            IBlueprintManager blueprintManager)
        {
            // Compute maximum label width.
            float maxLabelWidth = 0;

            foreach (var componentType in blueprint.GetAllComponentTypes())
            {
                var inspectorType = inspectorTypeTable[componentType];
                foreach (var componentProperty in inspectorType.Properties)
                {
                    var textDimensions = GUI.skin.label.CalcSize(new GUIContent(componentProperty.Name));
                    maxLabelWidth = MathUtils.Max(textDimensions.x, maxLabelWidth);
                }
            }
            EditorGUIUtility.labelWidth = maxLabelWidth;

            foreach (var componentType in blueprint.GetAllComponentTypes())
            {
                var inspectorType = inspectorTypeTable[componentType];

                // Draw inspector.
                AttributeTableField(inspectorType, configuration, inspectorTypeTable, blueprintManager);
            }
        }
        /// <summary>
        ///   Draws the game configuration inspector.
        /// </summary>
        public override void OnInspectorGUI()
        {
            this.DrawDefaultInspector();

            // Collect system types.
            if (this.inspectorSystemTypes == null)
            {
                this.inspectorSystemTypes = InspectorTypeTable.FindInspectorTypes(typeof(ISystem));
            }

            IAttributeTable configuration = this.gameConfiguration.Configuration;
            foreach (var inspectorType in this.inspectorSystemTypes)
            {
                // Draw inspector type.
                this.DrawInspector(inspectorType, configuration, this.inspectorSystemTypes, null);
            }

            if (GUILayout.Button("Reload"))
            {
                // Reload configuration.
                this.gameConfiguration.Load();
            }

            if (GUILayout.Button("Save"))
            {
                // Save configuration.
                // TODO(co): Save automatically if changed.
                this.Save();

                // Refresh assets.
                AssetDatabase.Refresh();
            }
        }
Exemplo n.º 6
0
 /// <summary>
 ///   Constructs a new entity manager without any initial entities.
 /// </summary>
 /// <param name="game"> Game to manage the entities for. </param>
 public EntityManager(Game game)
 {
     this.game              = game;
     this.nextEntityId      = 1;
     this.entities          = new HashSet <int>();
     this.removedEntities   = new HashSet <int>();
     this.inactiveEntities  = new Dictionary <int, List <IEntityComponent> >();
     this.componentManagers = new Dictionary <Type, ComponentManager>();
     this.inspectorTypes    = InspectorTypeTable.FindInspectorTypes(typeof(IEntityComponent));
 }
        private void LoadBlueprints()
        {
            inspectorComponentTypes = InspectorTypeTable.FindInspectorTypes(typeof(IEntityComponent));

            // Search all directories for blueprint files.
            DirectoryInfo directory = new DirectoryInfo(Application.dataPath);

            FileInfo[] blueprintFiles = directory.GetFiles("*" + BlueprintFileExtension, SearchOption.AllDirectories);

            if (blueprintFiles.Length == 0)
            {
                Debug.LogError(string.Format("No blueprint file ({0}) found!", BlueprintFileExtension));
            }
            else
            {
                // Create new hiearchical blueprint manager.
                hierarchicalBlueprintManager = new HierarchicalBlueprintManager();
                var blueprintManagerSerializer = new XmlSerializer(typeof(BlueprintManager));
                var filesProcessed             = 0;

                foreach (var blueprintFile in blueprintFiles)
                {
                    // Create new blueprint manager.
                    using (var blueprintFileStream = blueprintFile.OpenRead())
                    {
                        try
                        {
                            // Try to read the blueprint data.
                            var blueprintManager =
                                (BlueprintManager)blueprintManagerSerializer.Deserialize(blueprintFileStream);

                            if (blueprintManager != null)
                            {
                                hierarchicalBlueprintManager.AddChild(blueprintManager);
                                ++filesProcessed;
                            }
                        }
                        catch (InvalidOperationException)
                        {
                            Debug.LogError(blueprintFile.Name + " is no blueprint file.");
                        }
                    }
                }

                Debug.Log(
                    string.Format(
                        "Loaded {0} blueprint(s) from {1} blueprint file(s).",
                        hierarchicalBlueprintManager.Blueprints.Count(),
                        filesProcessed));
            }
        }
        private void DrawInspector(InspectorType inspectorType, IAttributeTable configuration, InspectorTypeTable inspectorTypeTable, IBlueprintManager blueprintManager)
        {
            foreach (var inspectorProperty in inspectorType.Properties)
            {
                // Get current value.
                object currentValue = configuration.GetValueOrDefault(inspectorProperty.Name, inspectorProperty.Default);
                object newValue = EditorGUIUtils.LogicInspectorPropertyField(inspectorProperty, currentValue, inspectorTypeTable, blueprintManager);

                // Set new value if changed.
                if (!Equals(newValue, currentValue))
                {
                    configuration.SetValue(inspectorProperty.Name, newValue);
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        ///   Draws an inspector for the specified logic property.
        /// </summary>
        /// <param name="inspectorProperty">Logic property to draw the inspector for.</param>
        /// <param name="currentValue">Current logic property value.</param>
        /// <param name="inspectorTypeTable"></param>
        /// <param name="blueprintManager"></param>
        /// <returns>New logic property value.</returns>
        public static object LogicInspectorPropertyField(
            InspectorPropertyAttribute inspectorProperty,
            object currentValue,
            InspectorTypeTable inspectorTypeTable,
            IBlueprintManager blueprintManager)
        {
            if (inspectorProperty.IsList)
            {
                // Build array.
                IList currentList = currentValue as IList;
                InspectorPropertyAttribute localInspectorProperty = inspectorProperty;
                IList newList;

                ListField(
                    true,
                    new GUIContent(inspectorProperty.Name),
                    currentList,
                    count =>
                {
                    IList list = localInspectorProperty.GetEmptyList();
                    for (int idx = 0; idx < count; idx++)
                    {
                        list.Add(localInspectorProperty.DefaultListItem);
                    }
                    return(list);
                },
                    (obj, index) =>
                    LogicInspectorPropertyField(
                        localInspectorProperty,
                        obj,
                        new GUIContent("Item " + index),
                        inspectorTypeTable,
                        blueprintManager),
                    out newList);

                return(newList);
            }

            // Draw inspector property.
            return(LogicInspectorPropertyField(
                       inspectorProperty,
                       currentValue,
                       new GUIContent(inspectorProperty.Name, inspectorProperty.Description),
                       inspectorTypeTable,
                       blueprintManager));
        }
        private static void LoadBlueprints()
        {
            // Build hierarchical blueprint manager for resolving parents.
            hierarchicalBlueprintManager = new HierarchicalBlueprintManager();

            var blueprintAssets = Resources.LoadAll(BlueprintsFolder, typeof(TextAsset));

            foreach (var blueprintAsset in blueprintAssets)
            {
                var blueprintTextAsset = blueprintAsset as TextAsset;

                if (blueprintTextAsset != null)
                {
                    var blueprintStream = new MemoryStream(blueprintTextAsset.bytes);

                    // Load blueprints.
                    var blueprintManagerSerializer = new XmlSerializer(typeof(BlueprintManager));

                    try
                    {
                        // Right now, only a single blueprint file is supported. This might change in the future.
                        blueprintManager  = (BlueprintManager)blueprintManagerSerializer.Deserialize(blueprintStream);
                        blueprintFileName = Application.dataPath.Substring(
                            0, Application.dataPath.Length - "Assets".Length)
                                            + AssetDatabase.GetAssetPath(blueprintTextAsset);

                        hierarchicalBlueprintManager.AddChild(blueprintManager);
                    }
                    catch (XmlException e)
                    {
                        EditorUtility.DisplayDialog(
                            string.Format("Error reading blueprint file {0}", blueprintAsset.name), e.Message, "Close");
                        return;
                    }
                }
            }

            // Resolve parents of all blueprints.
            BlueprintUtils.ResolveParents(hierarchicalBlueprintManager, hierarchicalBlueprintManager);

            // Load components.
            inspectorTypeTable = InspectorTypeTable.FindInspectorTypes(typeof(IEntityComponent));
        }
        private static void ShowBlueprintEditor()
        {
            blueprintManager   = null;
            blueprintFileName  = string.Empty;
            inspectorTypeTable = null;

            // Load blueprints.
            LoadBlueprints();

            if (blueprintManager == null)
            {
                // Creating new blueprints file.
                blueprintFileName = "Assets/Resources/" + BlueprintsFolder + "/Blueprints.xml";
                blueprintManager  = new BlueprintManager();
                hierarchicalBlueprintManager.AddChild(blueprintManager);
                EditorUtility.DisplayDialog(
                    "No blueprint file found",
                    string.Format("No blueprints could be found in resources folder {0}. Created new one called {1}.", BlueprintsFolder, blueprintFileName),
                    "Ok");
            }

            GetWindow(typeof(BlueprintEditorWindow), true, "Blueprint Editor");
        }
 /// <summary>
 ///   Finds all components accessible to the user in the landscape designer via reflection.
 /// </summary>
 public static void LoadComponents()
 {
     Instance = InspectorTypeTable.FindInspectorTypes(typeof(IEntityComponent));
 }
        private void DrawInspector(InspectorType inspectorType, IAttributeTable configuration, InspectorTypeTable inspectorTypeTable, IBlueprintManager blueprintManager)
        {
            foreach (var inspectorProperty in inspectorType.Properties)
            {
                // Get current value.
                object currentValue = configuration.GetValueOrDefault(inspectorProperty.Name, inspectorProperty.Default);
                object newValue     = EditorGUIUtils.LogicInspectorPropertyField(inspectorProperty, currentValue, inspectorTypeTable, blueprintManager);

                // Set new value if changed.
                if (!Equals(newValue, currentValue))
                {
                    configuration.SetValue(inspectorProperty.Name, newValue);
                }
            }
        }
Exemplo n.º 14
0
        /// <summary>
        ///   Draws an inspector for the specified logic property.
        /// </summary>
        /// <param name="inspectorProperty">Logic property to draw the inspector for.</param>
        /// <param name="currentValue">Current logic property value.</param>
        /// <param name="label">Text to show next to the property editor.</param>
        /// <param name="inspectorTypeTable"></param>
        /// <param name="blueprintManager"></param>
        /// <returns>New logic property value.</returns>
        public static object LogicInspectorPropertyField(
            InspectorPropertyAttribute inspectorProperty,
            object currentValue,
            GUIContent label,
            InspectorTypeTable inspectorTypeTable,
            IBlueprintManager blueprintManager)
        {
            // Draw inspector control.
            if (inspectorProperty is InspectorBoolAttribute)
            {
                return(EditorGUILayout.Toggle(label, Convert.ToBoolean(currentValue)));
            }
            if (inspectorProperty is InspectorStringAttribute || inspectorProperty is InspectorBlueprintAttribute)
            {
                return(EditorGUILayout.TextField(label, Convert.ToString(currentValue)));
            }
            if (inspectorProperty is InspectorFloatAttribute)
            {
                return(EditorGUILayout.FloatField(label, Convert.ToSingle(currentValue)));
            }
            if (inspectorProperty is InspectorIntAttribute)
            {
                return(EditorGUILayout.IntField(label, Convert.ToInt32(currentValue)));
            }
            InspectorEnumAttribute enumInspectorProperty = inspectorProperty as InspectorEnumAttribute;

            if (enumInspectorProperty != null)
            {
                object currentEnumValue = (currentValue != null)
                    ? Convert.ChangeType(currentValue, enumInspectorProperty.PropertyType)
                    : Enum.GetValues(enumInspectorProperty.PropertyType).GetValue(0);
                return(EditorGUILayout.EnumPopup(label, (Enum)currentEnumValue));
            }
            InspectorVectorAttribute vectorInspectorproperty = inspectorProperty as InspectorVectorAttribute;

            if (vectorInspectorproperty != null)
            {
                if (vectorInspectorproperty.PropertyType == typeof(Vector2I) ||
                    vectorInspectorproperty.PropertyType == typeof(List <Vector2I>))
                {
                    Vector2I currentVector2IValue = (currentValue != null) ? (Vector2I)currentValue : Vector2I.Zero;
                    return(Vector2IField(label, currentVector2IValue));
                }
                if (vectorInspectorproperty.PropertyType == typeof(Vector2F) ||
                    vectorInspectorproperty.PropertyType == typeof(List <Vector2F>))
                {
                    Vector2F currentVector2FValue = (currentValue != null) ? (Vector2F)currentValue : Vector2F.Zero;
                    return(Vector2FField(label, currentVector2FValue));
                }
            }
            InspectorEntityAttribute entityInspector = inspectorProperty as InspectorEntityAttribute;

            if (entityInspector != null)
            {
                EntityConfiguration entityConfiguration = currentValue as EntityConfiguration;
                if (entityConfiguration == null)
                {
                    entityConfiguration = new EntityConfiguration();
                }

                entityConfiguration.BlueprintId = BlueprintIdSelection(
                    label,
                    entityConfiguration.BlueprintId,
                    inspectorTypeTable,
                    blueprintManager);

                if (!string.IsNullOrEmpty(entityConfiguration.BlueprintId))
                {
                    Blueprint blueprint = blueprintManager.GetBlueprint(entityConfiguration.BlueprintId);
                    if (blueprint != null)
                    {
                        if (entityConfiguration.Configuration == null)
                        {
                            entityConfiguration.Configuration = new AttributeTable();
                        }

                        ++EditorGUI.indentLevel;
                        BlueprintComponentsField(
                            blueprint,
                            entityConfiguration.Configuration,
                            inspectorTypeTable,
                            blueprintManager);
                        --EditorGUI.indentLevel;
                    }
                }

                return(entityConfiguration);
            }

            EditorGUILayout.HelpBox(
                string.Format(
                    "No inspector found for property {0} of type {1}.",
                    inspectorProperty.Name,
                    inspectorProperty.PropertyType),
                MessageType.Warning);
            return(currentValue);
        }
        private void LoadBlueprints()
        {
            inspectorComponentTypes = InspectorTypeTable.FindInspectorTypes(typeof(IEntityComponent));

            // Search all directories for blueprint files.
            DirectoryInfo directory = new DirectoryInfo(Application.dataPath);
            FileInfo[] blueprintFiles = directory.GetFiles("*" + BlueprintFileExtension, SearchOption.AllDirectories);

            if (blueprintFiles.Length == 0)
            {
                Debug.LogError(string.Format("No blueprint file ({0}) found!", BlueprintFileExtension));
            }
            else
            {
                // Create new hiearchical blueprint manager.
                hierarchicalBlueprintManager = new HierarchicalBlueprintManager();
                var blueprintManagerSerializer = new XmlSerializer(typeof(BlueprintManager));
                var filesProcessed = 0;

                foreach (var blueprintFile in blueprintFiles)
                {
                    // Create new blueprint manager.
                    using (var blueprintFileStream = blueprintFile.OpenRead())
                    {
                        try
                        {
                            // Try to read the blueprint data.
                            var blueprintManager =
                                (BlueprintManager)blueprintManagerSerializer.Deserialize(blueprintFileStream);

                            if (blueprintManager != null)
                            {
                                hierarchicalBlueprintManager.AddChild(blueprintManager);
                                ++filesProcessed;
                            }
                        }
                        catch (InvalidOperationException)
                        {
                            Debug.LogError(blueprintFile.Name + " is no blueprint file.");
                        }
                    }
                }

                Debug.Log(
                    string.Format(
                        "Loaded {0} blueprint(s) from {1} blueprint file(s).",
                        hierarchicalBlueprintManager.Blueprints.Count(),
                        filesProcessed));
            }
        }
        private static void ShowBlueprintEditor()
        {
            blueprintManager = null;
            blueprintFileName = string.Empty;
            inspectorTypeTable = null;

            // Load blueprints.
            LoadBlueprints();

            if (blueprintManager == null)
            {
                // Creating new blueprints file.
                blueprintFileName = "Assets/Resources/" + BlueprintsFolder + "/Blueprints.xml";
                blueprintManager = new BlueprintManager();
                hierarchicalBlueprintManager.AddChild(blueprintManager);
                EditorUtility.DisplayDialog(
                    "No blueprint file found",
                    string.Format("No blueprints could be found in resources folder {0}. Created new one called {1}.", BlueprintsFolder, blueprintFileName),
                    "Ok");
            }

            GetWindow(typeof(BlueprintEditorWindow), true, "Blueprint Editor");
        }
Exemplo n.º 17
0
 /// <summary>
 ///   Finds all components accessible to the user in the landscape designer via reflection.
 /// </summary>
 public static void LoadComponents()
 {
     Instance = InspectorTypeTable.FindInspectorTypes(typeof(IEntityComponent));
 }
        private static void LoadBlueprints()
        {
            // Build hierarchical blueprint manager for resolving parents.
            hierarchicalBlueprintManager = new HierarchicalBlueprintManager();

            var blueprintAssets = Resources.LoadAll(BlueprintsFolder, typeof(TextAsset));

            foreach (var blueprintAsset in blueprintAssets)
            {
                var blueprintTextAsset = blueprintAsset as TextAsset;

                if (blueprintTextAsset != null)
                {
                    var blueprintStream = new MemoryStream(blueprintTextAsset.bytes);

                    // Load blueprints.
                    var blueprintManagerSerializer = new XmlSerializer(typeof(BlueprintManager));

                    try
                    {
                        // Right now, only a single blueprint file is supported. This might change in the future.
                        blueprintManager = (BlueprintManager)blueprintManagerSerializer.Deserialize(blueprintStream);
                        blueprintFileName = Application.dataPath.Substring(
                            0, Application.dataPath.Length - "Assets".Length)
                                            + AssetDatabase.GetAssetPath(blueprintTextAsset);

                        hierarchicalBlueprintManager.AddChild(blueprintManager);
                    }
                    catch (XmlException e)
                    {
                        EditorUtility.DisplayDialog(
                            string.Format("Error reading blueprint file {0}", blueprintAsset.name), e.Message, "Close");
                        return;
                    }
                }
            }

            // Resolve parents of all blueprints.
            BlueprintUtils.ResolveParents(hierarchicalBlueprintManager, hierarchicalBlueprintManager);

            // Load components.
            inspectorTypeTable = InspectorTypeTable.FindInspectorTypes(typeof(IEntityComponent));
        }