Ejemplo n.º 1
0
        void SchemaWindow()
        {
            GUILayout.BeginVertical(skin.box);
            GUILayout.Label("Drop here some scripts you want to add to the schema", skin.FindStyle("Title"));

            MonoScript script = null;

            script = EditorGUILayout.ObjectField(script, typeof(MonoScript), false) as MonoScript;

            if (script != null)
            {
                Type type = script.GetClass();
                try
                {
                    schemaBuilder.activeSchema.AddTableSchema(new TableSchema(type));
                    log = string.Format("Table {0} added to list!", type);

                    if (autoSaveSchema)
                    {
                        schemaBuilder.SaveActiveSchema();
                    }
                }
                catch (Exception e)
                {
                    log = e.Message;
                }
            }
            GUILayout.EndVertical();

            //Table Header
            GUILayout.BeginHorizontal();

            GUILayout.Label("Version", skin.FindStyle("Title"));
            versionIndex = schemaBuilder.availableVersions.IndexOf(schemaBuilder.activeSchema.version);
            int index = EditorGUILayout.Popup(versionIndex, schemaBuilder.availableVersions.ToArray());

            if (index != versionIndex)
            {
                schemaBuilder.LoadSchema(schemaBuilder.availableVersions[index]);
                if (selectedTable != null)
                {
                    try
                    {
                        selectedTable = schemaBuilder.activeSchema.GetTableSchema(selectedTable.itemType);
                    }
                    catch (Exception e)
                    {
                        log = e.Message;
                    }
                }
            }

            GUILayout.FlexibleSpace();
            autoSaveSchema = EditorGUILayout.Toggle("Auto-save Schema", autoSaveSchema);


            if (GUILayout.Button("Save Schema", skin.button))
            {
                try
                {
                    schemaBuilder.SaveActiveSchema();
                    log = string.Format("Schema v{0} successfully saved!", schemaBuilder.activeSchema.version);
                }
                catch (Exception e)
                {
                    log = e.Message;
                }
            }

            if (GUILayout.Button("New Schema", skin.button))
            {
                try
                {
                    schemaBuilder.CreateSchemaForCurrentVersion();
                    log = string.Format("Schema v{0} successfully created!", schemaBuilder.activeSchema.version);

                    if (autoSaveSchema)
                    {
                        schemaBuilder.SaveActiveSchema();
                    }
                }
                catch (Exception e)
                {
                    log = e.Message;
                }
            }

            GUILayout.EndHorizontal();

            try
            {
                //Grid Block
                GUILayout.BeginHorizontal(skin.box);

                //Left block
                GUILayout.BeginVertical(skin.box);
                GUILayout.Label("Tables", skin.FindStyle("Title"));

                leftscrollPosition = GUILayout.BeginScrollView(leftscrollPosition);
                foreach (TableSchema schema in schemaBuilder.activeSchema.GetTableSchemas())
                {
                    if (GUILayout.Button(schema.itemType.ToString()))
                    {
                        selectedTable       = schema;
                        rightscrollPosition = Vector2.zero;
                    }
                }
                GUILayout.EndScrollView();
                GUILayout.EndVertical();

                //Right block
                GUILayout.BeginVertical(skin.box);
                GUILayout.Label("Fields", skin.FindStyle("Title"));

                rightscrollPosition = GUILayout.BeginScrollView(rightscrollPosition);

                if (selectedTable != null)
                {
                    GUILayout.BeginVertical();

                    List <TableField> fields = selectedTable.fields.Where(f => refTable.ContainsField(f)).ToList();
                    if (fields.Count > 0)
                    {
                        GUILayout.Label("Fields to use", skin.FindStyle("Title"), GUILayout.ExpandWidth(false));

                        foreach (TableField field in fields)
                        {
                            GUILayout.BeginHorizontal();
                            try
                            {
                                GUILayout.Label(field.isProperty ? "P:" : "F:", GUILayout.ExpandWidth(false));
                                GUILayout.Label(field.fieldName, skin.FindStyle("Italic"), GUILayout.ExpandWidth(false));
                                GUILayout.Label("(" + field.fieldType.ToString() + ")", GUILayout.ExpandWidth(false));
                            }
                            catch
                            {
                                GUILayout.Label("Field type not supported!");
                            }

                            GUILayout.FlexibleSpace();
                            if (GUILayout.Button("Don't Use", GUILayout.ExpandWidth(false)))
                            {
                                selectedTable.RemoveField(field);
                                if (autoSaveSchema)
                                {
                                    schemaBuilder.SaveActiveSchema();
                                }
                            }
                            GUILayout.EndHorizontal();
                        }
                        EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
                    }

                    fields = refTable.fields.Where(f => !selectedTable.ContainsField(f)).ToList();
                    if (fields.Count > 0)
                    {
                        GUILayout.Label("Available Fields", skin.FindStyle("Title"), GUILayout.ExpandWidth(false));
                        foreach (TableField field in fields)
                        {
                            GUILayout.BeginHorizontal();
                            try
                            {
                                GUILayout.Label(field.isProperty ? "P:" : "F:", GUILayout.ExpandWidth(false));
                                GUILayout.Label(field.fieldName, skin.FindStyle("Italic"), GUILayout.ExpandWidth(false));
                                GUILayout.Label("(" + field.fieldType.ToString() + ")", GUILayout.ExpandWidth(false));
                            }
                            catch
                            {
                                GUILayout.Label("Field type not supported!");
                            }

                            GUILayout.FlexibleSpace();
                            if (GUILayout.Button("Use", GUILayout.ExpandWidth(false)))
                            {
                                selectedTable.AddField(field);
                                if (autoSaveSchema)
                                {
                                    schemaBuilder.SaveActiveSchema();
                                }
                            }
                            GUILayout.EndHorizontal();
                        }
                        EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
                    }

                    fields = selectedTable.fields.Where(f => !refTable.ContainsField(f)).ToList();
                    if (fields.Count > 0)
                    {
                        GUILayout.Label("Deprecated Fields", skin.FindStyle("Title"), GUILayout.ExpandWidth(false));
                        foreach (TableField field in fields)
                        {
                            GUILayout.BeginHorizontal();
                            try
                            {
                                GUILayout.Label(field.isProperty ? "P:" : "F:", GUILayout.ExpandWidth(false));
                                GUILayout.Label(field.fieldName, skin.FindStyle("Italic"), GUILayout.ExpandWidth(false));
                                GUILayout.Label("(" + field.fieldType.ToString() + ")", GUILayout.ExpandWidth(false));
                            }
                            catch
                            {
                                GUILayout.Label("Field type not supported!");
                            }

                            GUILayout.FlexibleSpace();
                            if (GUILayout.Button("Delete", GUILayout.ExpandWidth(false)))
                            {
                                selectedTable.RemoveField(field);
                                if (autoSaveSchema)
                                {
                                    schemaBuilder.SaveActiveSchema();
                                }
                            }
                            GUILayout.EndHorizontal();
                        }
                    }

                    GUILayout.EndVertical();

                    GUILayout.Space(10);
                    GUILayout.BeginHorizontal();
                    if (GUILayout.Button("Remove Table", skin.button))
                    {
                        schemaBuilder.activeSchema.RemoveTableSchema(selectedTable);
                        selectedTable = null;
                        if (autoSaveSchema)
                        {
                            schemaBuilder.SaveActiveSchema();
                        }
                    }

                    if (GUILayout.Button("Update all fields", skin.button))
                    {
                        Type tableType = selectedTable.itemType;
                        schemaBuilder.activeSchema.RebuildTableSchema(tableType);
                        selectedTable = schemaBuilder.activeSchema.GetTableSchemas().Where(t => t.itemType == tableType).First();
                        if (autoSaveSchema)
                        {
                            schemaBuilder.SaveActiveSchema();
                        }
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndScrollView();
                GUILayout.EndVertical();

                GUILayout.EndHorizontal();
            }
            catch (Exception e)
            {
                log = e.Message;
            }
        }