Example #1
0
            public override bool Creation(NewObjectCell.ObjectCreationContext context)
            {
                if (Window.IsFileCreation())
                {
                    context.disableFileCreation = true;

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

                    try
                    {
                        //main file
                        {
                            string name       = Template.Name + ".scene";
                            var    sourceFile = VirtualPathUtility.GetRealPathByVirtual(@"Base\Tools\NewResourceTemplates\" + name);

                            //copy scene file

                            var text = File.ReadAllText(sourceFile);

                            if (CreateCSharpClass)
                            {
                                text = text.Replace(".component NeoAxis.Component_Scene", ".component " + csharpClassName);
                            }

                            File.WriteAllText(context.fileCreationRealFileName, text);

                            //copy additional folder if exist
                            var sourceFolderPath = sourceFile + "_Files";
                            if (Directory.Exists(sourceFolderPath))
                            {
                                var destFolderPath = context.fileCreationRealFileName + "_Files";
                                IOUtility.CopyDirectory(sourceFolderPath, destFolderPath);
                            }
                        }

                        //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);
            }
Example #2
0
        public bool DoUpdate(ReimportSettings reimportSettings, out string error)
        {
            try
            {
                insideDoUpdate = true;

                if (reimportSettings == null)
                {
                    reimportSettings = new ReimportSettings();
                }

                error = "";

                //bool createDocumentConfig = Components.Count == 0;

                //disable _Import3D
                bool wasEnabled = Enabled;
                Enabled = false;
                try
                {
                    var settings = new ImportGeneral.Settings();
                    settings.updateMaterials      = reimportSettings.UpdateMaterials;
                    settings.updateMeshes         = reimportSettings.UpdateMeshes;
                    settings.updateObjectsInSpace = reimportSettings.UpdateObjectsInSpace;

                    //remove old objects
                    if (settings.updateObjectsInSpace)
                    {
                        var c = GetComponent("Object In Space");
                        if (c != null)
                        {
                            RemoveComponent(c, false);
                        }
                        c = GetComponent("Objects In Space");
                        if (c != null)
                        {
                            RemoveComponent(c, false);
                        }
                    }
                    if (settings.updateMeshes)
                    {
                        var c = GetComponent("Mesh");
                        if (c != null)
                        {
                            RemoveComponent(c, false);
                        }
                        c = GetComponent("Meshes");
                        if (c != null)
                        {
                            RemoveComponent(c, false);
                        }
                    }
                    if (settings.updateMaterials)
                    {
                        var c = GetComponent("Material");
                        if (c != null)
                        {
                            RemoveComponent(c, false);
                        }
                        c = GetComponent("Materials");
                        if (c != null)
                        {
                            RemoveComponent(c, false);
                        }
                    }

                    if (Parent != null || ParentRoot.HierarchyController == null || ParentRoot.HierarchyController.CreatedByResource == null ||
                        !ParentRoot.HierarchyController.CreatedByResource.Owner.LoadFromFile)
                    {
                        error = "This component must be root of the resource.";
                        return(false);
                    }
                    var virtualFileName = ParentRoot.HierarchyController.CreatedByResource.Owner.Name;

                    settings.component       = this;
                    settings.virtualFileName = virtualFileName;

                    if (Path.GetExtension(virtualFileName).ToLower() == ".json")
                    {
                        //Quixel Megascans

                        ImportMegascans.DoImport(settings, out error);
                        if (!string.IsNullOrEmpty(error))
                        {
                            return(false);
                        }
                    }
                    else if (Path.GetExtension(virtualFileName).ToLower() == ".fbx")
                    {
                        //FBX

                        //settings.loadAnimations = true;
                        //!!!!to options?
                        settings.frameStep = .25;

                        Import.FBX.ImportFBX.DoImport(settings, out error);
                        if (!string.IsNullOrEmpty(error))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        //Assimp

                        //settings.loadAnimations = false;

                        ImportAssimp.DoImport(settings, out error);
                        if (!string.IsNullOrEmpty(error))
                        {
                            return(false);
                        }
                    }

                    UpdatePostProcess?.Invoke(this, settings);

                    //DeleteUnusedMaterials
                    if (settings.updateMaterials && DeleteUnusedMaterials && !settings.disableDeletionUnusedMaterials)
                    {
                        var usedMaterials = new ESet <Component_Material>();
                        foreach (var meshGeometry in GetComponents <Component_MeshGeometry>(false, true))
                        {
                            var material = meshGeometry.Material.Value;
                            if (material != null)
                            {
                                usedMaterials.AddWithCheckAlreadyContained(material);
                            }
                        }

again:
                        foreach (var material in GetComponents <Component_Material>(false, true))
                        {
                            if (!usedMaterials.Contains(material))
                            {
                                material.RemoveFromParent(false);
                                material.Dispose();
                                goto again;
                            }
                        }
                    }
                }
                finally
                {
                    //enable _Import3D
                    if (wasEnabled)
                    {
                        Enabled = true;
                    }
                }

                //set EditorDocumentConfiguration
                //if( createDocumentConfig )
                {
                    //!!!!что еще при Mode = Scene

                    var toSelect = new List <Component>();

                    //root object
                    toSelect.Add(this);

                    //mesh
                    var mesh = GetComponent("Mesh") as Component_Mesh;
                    if (mesh != null)
                    {
                        toSelect.Add(mesh);
                    }

                    //materials
                    var materials = GetComponents <Component_Material>(false, true);
                    if (materials.Length <= 4)
                    {
                        foreach (var material in materials)
                        {
                            var graph = material.GetComponent <Component_FlowGraph>();
                            if (graph != null && graph.TypeSettingsIsPublic())
                            {
                                toSelect.Add(graph);
                            }
                        }
                    }

                    //select window with mesh or select window with root object
                    var selectObject = mesh != null ? (Component)mesh : this;

                    EditorDocumentConfiguration = KryptonConfigGenerator.CreateEditorDocumentXmlConfiguration(toSelect, selectObject);

                    //update windows
                    //!!!!какие-то еще проверки?
                    if (EngineApp.ApplicationType == EngineApp.ApplicationTypeEnum.Editor)
                    {
                        var document = EditorAPI.GetDocumentByObject(this);
                        if (document != null)
                        {
                            ////close old windows
                            //EditorAPI.CloseAllDocumentWindowsOnSecondLevel( document );

                            //open windows
                            foreach (var obj in toSelect)
                            {
                                if (obj != this)
                                {
                                    EditorAPI.OpenDocumentWindowForObject(document, obj);
                                }
                            }

                            //delete old windows
                            EditorAPI.CloseAllDocumentWindowsOnSecondLevelWithDeletedObjects(document);

                            //select window
                            var windows = EditorAPI.FindDocumentWindowsWithObject(selectObject);
                            if (windows.Count != 0)
                            {
                                EditorAPI.SelectDockWindow(windows[0]);
                            }
                        }

                        //!!!!or restore window configuration from EditorDocumentConfiguration
                    }
                }
            }
            finally
            {
                insideDoUpdate = false;
            }

            return(true);
        }