public bool ApplyCreationSettingsToObject(object createdObject, ref bool disableFileCreation)
        {
            var createdComponent         = createdObject as Component;
            var fileCreationRealFileName = GetFileCreationRealFileName();            // IsFileCreation() ? VirtualPathUtils.GetRealPathByVirtual( textBoxName.Text ) : "";

            if (!IsFileCreation() && createdComponent != null)
            {
                //!!!!!проверки
                createdComponent.Name = textBoxName.Text;
            }

            createdComponent?.NewObjectSetDefaultConfiguration(true);

            foreach (var c in tableLayoutPanel1.Controls)
            {
                NewObjectCell cell = c as NewObjectCell;
                if (cell != null)
                {
                    var context = new NewObjectCell.ObjectCreationContext();
                    context.newObject = createdObject;
                    context.fileCreationRealFileName = fileCreationRealFileName;
                    context.disableFileCreation      = disableFileCreation;

                    if (!cell.ObjectCreation(context))
                    //if( !cell.ObjectCreation( createdObject, fileCreationRealFileName, ref disableFileWriting ) )
                    {
                        //!!!!!

                        return(false);
                    }

                    disableFileCreation = context.disableFileCreation;
                }
            }

            return(true);
        }
Beispiel #2
0
            public override bool Creation(NewObjectCell.ObjectCreationContext context)
            {
                string code = @"using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using NeoAxis;
using NeoAxis.Editor;

namespace Project
{
	public class {Name}{BasedOnComponentClass}
	{
{Body}
	}
}";

                //{Name}
                var className = Path.GetFileNameWithoutExtension(context.fileCreationRealFileName);

                code = code.Replace("{Name}", className);

                //{BasedOnComponentClass}
                if (!string.IsNullOrEmpty(BaseClass))
                {
                    code = code.Replace("{BasedOnComponentClass}", " : " + BaseClass);
                }
                else
                {
                    code = code.Replace("{BasedOnComponentClass}", "");
                }

                //{Body}
                {
                    var body = new List <string>();

                    if (AddExampleProperties)
                    {
                        body.Add("[DefaultValue( 1 )]");
                        body.Add("[Range( 0, 2 )]");
                        body.Add("public double Power { get; set; } = 1;");
                        body.Add("");
                        body.Add("[DefaultValue( \"1 1 1\" )]");
                        body.Add("public ColorValue Color { get; set; } = new ColorValue( 1, 1, 1 );");

                        //!!!!пример ссылки на компоненту
                        //!!!!пример ссылки на файл
                    }

                    var isComponent = false;
                    if (!string.IsNullOrEmpty(BaseClass))
                    {
                        if (BaseClass == "NeoAxis.Component")
                        {
                            isComponent = true;
                        }
                        var type = MetadataManager.GetType(BaseClass);
                        if (type != null && typeof(Component).IsAssignableFrom(type.GetNetType()))
                        {
                            isComponent = true;
                        }
                        var type2 = MetadataManager.GetType("NeoAxis." + BaseClass);
                        if (type2 != null && typeof(Component).IsAssignableFrom(type2.GetNetType()))
                        {
                            isComponent = true;
                        }
                    }

                    if (isComponent)
                    {
                        if (body.Count != 0)
                        {
                            body.Add("");
                        }

                        body.Add("protected override void OnEnabledInSimulation()");
                        body.Add("{");
                        body.Add("}");
                        body.Add("");

                        body.Add("protected override void OnUpdate( float delta )");
                        body.Add("{");
                        body.Add("}");
                        body.Add("");

                        body.Add("protected override void OnSimulationStep()");
                        body.Add("{");
                        body.Add("}");
                    }

                    var bodyStr = "";
                    foreach (var line in body)
                    {
                        if (bodyStr != "")
                        {
                            bodyStr += "\r\n";
                        }
                        bodyStr += "\t\t" + line;
                    }
                    code = code.Replace("{Body}", bodyStr);
                }

                //write file
                try
                {
                    File.WriteAllText(context.fileCreationRealFileName, code);
                }
                catch (Exception e)
                {
                    Log.Warning(e.Message);
                    return(false);
                }

                //add to Project.csproj
                if (AddToProjectCsproj)
                {
                    var toAdd = new List <string>();

                    var fileName = Path.Combine("Assets", VirtualPathUtility.GetVirtualPathByReal(context.fileCreationRealFileName));
                    toAdd.Add(fileName);

                    if (CSharpProjectFileUtility.UpdateProjectFile(toAdd, null, out var error))
                    {
                        if (toAdd.Count > 1)
                        {
                            Log.Info(EditorLocalization.Translate("General", "Items have been added to the Project.csproj."));
                        }
                        else
                        {
                            Log.Info(EditorLocalization.Translate("General", "The item has been added to the Project.csproj."));
                        }
                    }
                    else
                    {
                        Log.Warning(error);
                    }
                }

                return(true);
            }
Beispiel #3
0
        public virtual bool Creation(NewObjectCell.ObjectCreationContext context)
        {
            if (Window.IsFileCreation() && CreateCSharpClass)
            {
                context.disableFileCreation = true;

                GetCreateCSharpClassInfo(out var csharpRealFileName, out var csharpClassName, out var csharpClassNameWithoutNamespace);

                try
                {
                    //main file
                    {
                        string className = csharpClassName;
                        //string className = CreateCSharpClass ? csharpClassName : Window.SelectedType.Name;
                        var text = ".component " + className + "\r\n{\r\n}";

                        File.WriteAllText(context.fileCreationRealFileName, text);
                    }

                    //cs file
                    //if( CreateCSharpClass )
                    {
                        string code = @"using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using NeoAxis;

namespace Project
{
	public class {Name} : {Base}
	{
	}
}";

                        code = code.Replace("{Name}", csharpClassNameWithoutNamespace);
                        code = code.Replace("{Base}", Window.SelectedType.Name);

                        File.WriteAllText(csharpRealFileName, code);
                    }
                }
                catch (Exception e)
                {
                    EditorMessageBox.ShowWarning(e.Message);
                    //Log.Warning( e.Message );
                    return(false);
                }

                //if( CreateCSharpClass )
                {
                    //add to Project.csproj
                    {
                        var toAdd = new List <string>();

                        var fileName = Path.Combine("Assets", VirtualPathUtility.GetVirtualPathByReal(csharpRealFileName));
                        toAdd.Add(fileName);

                        if (CSharpProjectFileUtility.UpdateProjectFile(toAdd, null, out var error))
                        {
                            if (toAdd.Count > 1)
                            {
                                Log.Info(EditorLocalization.Translate("General", "Items have been added to the Project.csproj."));
                            }
                            else
                            {
                                Log.Info(EditorLocalization.Translate("General", "The item has been added to the Project.csproj."));
                            }
                        }
                        else
                        {
                            EditorMessageBox.ShowWarning(error);
                            //Log.Warning( error );
                            return(false);
                        }
                    }

                    Window.DisableUnableToCreateReason = true;

                    //restart application
                    var text = EditorLocalization.Translate("General", "To apply changes need restart the editor. Restart?\r\n\r\nThe editor must be restarted to compile and enable a new created C# class.");
                    if (EditorMessageBox.ShowQuestion(text, EMessageBoxButtons.YesNo) == EDialogResult.Yes)
                    {
                        EditorAPI.BeginRestartApplication();
                    }

                    Window.DisableUnableToCreateReason = false;
                }
            }

            return(true);
        }