Exemple #1
0
        public void Save(Uri uri, SchemaLoader schemaLoader)
        {
            if (Dirty || m_uriChanged)
            {
                string   filePath = uri.LocalPath;
                FileMode fileMode = File.Exists(filePath) ? FileMode.Truncate : FileMode.OpenOrCreate;
                using (FileStream stream = new FileStream(filePath, fileMode))
                {
                    var writer = new CustomDomXmlWriter(Globals.ResourceRoot, schemaLoader.TypeCollection);
                    writer.Write(DomNode, stream, uri);
                }
                m_uriChanged = false;
            }

            // save all the game-references
            foreach (var gameRef in GetChildList <GameReference>(Schema.gameType.gameReferenceChild))
            {
                GameDocument subDoc = Adapters.As <GameDocument>(gameRef.Target);
                if (subDoc == null)
                {
                    continue;
                }
                subDoc.Save(subDoc.Uri, schemaLoader);
            }

            Dirty = false;
        }
Exemple #2
0
        public virtual void Save(Uri uri, ISchemaLoader schemaLoader)
        {
            LevelEditorXLE.Patches.SelectNameForReferencedDocuments(this);

            if (Dirty || m_uriChanged)
            {
                SaveDomDocument(DomNode, uri, schemaLoader);
                m_uriChanged = false;
            }

            // save all the game-references
            foreach (var gameRef in GetChildList <GameReference>(Schema.gameType.gameReferenceChild))
            {
                GameDocument subDoc = Adapters.As <GameDocument>(gameRef.Target);
                if (subDoc == null)
                {
                    continue;
                }
                subDoc.Save(subDoc.Uri, schemaLoader);
            }

            LevelEditorXLE.Patches.SaveReferencedDocuments(this, schemaLoader);

            Dirty = false;
        }
Exemple #3
0
        public void Close(IDocument document)
        {
            GameDocument gameDocument = document as GameDocument;

            if (gameDocument != null)
            {
                m_gameDocumentRegistry.Remove((IGameDocument)document);
                List <object> removeList = new List <object>(m_contextRegistry.Contexts);
                foreach (object cnt in removeList)
                {
                    m_contextRegistry.RemoveContext(cnt);
                }
                m_documentRegistry.Remove(document);
            }
        }
Exemple #4
0
        public void Save(IDocument document, Uri uri)
        {
            GameDocument gameDocument = document as GameDocument;

            gameDocument.Save(uri, m_schemaLoader);
            // save external resources.
            foreach (var obj in Util.FindAll <IEditableResourceOwner>())
            {
                if (obj.Dirty)
                {
                    obj.Save();
                }
            }

            m_projLister.Refresh();
        }
Exemple #5
0
        public IDocument Open(Uri uri)
        {
            IGameDocument activeDoc = m_documentRegistry.GetActiveDocument <IGameDocument>();

            if (activeDoc != null)
            {
                if (!m_documentService.Close(activeDoc))
                {
                    return(null);
                }
            }

            List <IResource> resources = new List <IResource>(m_resourceService.Resources);

            foreach (var res in resources)
            {
                m_resourceService.Unload(res.Uri);
            }

            GameDocument document = GameDocument.OpenOrCreate(uri, m_schemaLoader);

            m_contextRegistry.ActiveContext = document.As <GameContext>();
            return(document);
        }
Exemple #6
0
        /// <summary>
        /// Open or create new document.
        /// It opens if the file exist otherwise it will creates new document
        /// </summary>
        public static GameDocument OpenOrCreate(Uri uri, SchemaLoader schemaLoader)
        {
            if (!uri.IsAbsoluteUri)
            {
                return(null);
            }

            var docRegistry = Globals.MEFContainer.GetExportedValue <GameDocumentRegistry>();

            GameDocument document = docRegistry.FindDocument(uri) as GameDocument;

            if (document != null)
            {
                return(document);
            }

            string filePath = uri.LocalPath;

            DomNode rootNode = null;

            if (File.Exists(filePath))
            {
                // read existing document using custom dom XML reader
                using (FileStream stream = File.OpenRead(filePath))
                {
                    var reader = new CustomDomXmlReader(Globals.ResourceRoot, schemaLoader);
                    rootNode = reader.Read(stream, uri);
                }
            }
            else
            {
                // create new document by creating a Dom node of the root type defined by the schema
                rootNode = new DomNode(Schema.gameType.Type, Schema.gameRootElement);
                rootNode.SetAttribute(Schema.gameType.nameAttribute, "Game".Localize());
            }


            GameObjectFolder rootFolder = Adapters.As <GameObjectFolder>(rootNode.GetChild(Schema.gameType.gameObjectFolderChild));

            if (rootFolder == null)
            {
                // create the game object folder
                rootFolder      = (GameObjectFolder)GameObjectFolder.Create();
                rootFolder.Name = "GameObjects".Localize("this is the name of a folder in the project lister");
                rootNode.SetChild(Schema.gameType.gameObjectFolderChild, rootFolder.DomNode);
            }


            // create bookmarks
            DomNode bookmarks = rootNode.GetChild(Schema.gameType.bookmarksChild);

            if (bookmarks == null)
            {
                bookmarks = new DomNode(Schema.bookmarksType.Type);
                rootNode.SetChild(Schema.gameType.bookmarksChild, bookmarks);
            }

            DomNode layersNode = rootNode.GetChild(Schema.gameType.layersChild);

            if (layersNode == null)
            {
                layersNode = new DomNode(Schema.layersType.Type);
                rootNode.SetChild(Schema.gameType.layersChild, layersNode);
            }

            // Create the grid
            DomNode gridNode = rootNode.GetChild(Schema.gameType.gridChild);

            if (gridNode == null)
            {
                gridNode = new DomNode(Schema.gridType.Type);
                rootNode.SetChild(Schema.gameType.gridChild, gridNode);
            }

            document     = rootNode.As <GameDocument>();
            document.Uri = uri;

            // Initialize Dom extensions now that the data is complete
            rootNode.InitializeExtensions();

            docRegistry.Add(document);


            UniqueNamer uniqueNamer = new UniqueNamer('_');

            foreach (DomNode node in rootNode.Subtree)
            {
                if (node.Type.IdAttribute != null)
                {
                    uniqueNamer.Name(node.GetId());
                }
            }


            // sync all the prefab instances
            DomNode folderNode = document.RootGameObjectFolder.As <DomNode>();

            foreach (DomNode node in folderNode.Subtree)
            {
                PrefabInstance prefab = node.As <PrefabInstance>();
                if (prefab == null)
                {
                    continue;
                }
                prefab.Resolve(uniqueNamer);
            }

            if (ResolveOnLoad)
            {
                // resovle all the game references.
                foreach (var subGame in document.GetChildList <GameReference>(Schema.gameType.gameReferenceChild))
                {
                    subGame.Resolve();
                }
            }

            document.Dirty = false;
            return(document);
        }
Exemple #7
0
        void ICommandClient.DoCommand(object commandTag)
        {
            var target = TreeControlAdapter.LastHit;

            // If the command isn't one of our immediate commands, it
            // might belong to one of our sub providers.
            // Try to cast the targetted node to a command client and
            // execute from there
            if (!(commandTag is Command))
            {
                var targetClient = target.As <ICommandClient>();
                if (targetClient != null)
                {
                    targetClient.DoCommand(commandTag);
                }
                return;
            }

            IDocument gameDocument = null;
            string    filePath     = null;

            IGame game = target.As <IGame>();

            if (game == null)
            {
                GameReference gameRef = target.As <GameReference>();
                if (gameRef != null)
                {
                    game = gameRef.Target;
                }
            }

            if (game != null)
            {
                gameDocument = game.As <IDocument>();
            }

            switch ((Command)commandTag)
            {
            case Command.CreateNewSubGame:
                if (gameDocument != null)
                {
                    filePath = Util.GetFilePath(m_fileFilter,
                                                System.IO.Path.GetDirectoryName(gameDocument.Uri.LocalPath), true);
                    if (!string.IsNullOrEmpty(filePath))
                    {
                        try
                        {
                            if (!m_gameEditor.Info.IsCompatiblePath(filePath))
                            {
                                throw new Exception("Incompatible file type " + filePath);
                            }

                            Uri ur = new Uri(filePath);
                            if (m_gameDocumentRegistry.FindDocument(ur) != null)
                            {
                                throw new Exception(filePath + " is already open");
                            }
                            GameDocument subGame = GameDocument.OpenOrCreate(ur, m_schemaLoader);
                            subGame.Dirty = true;
                            GameReference gameRef = GameReference.CreateNew(subGame);

                            // try all implementation of IHierarchical until one works
                            var parent = game.AsAll <IHierarchical>();
                            foreach (var p in parent)
                            {
                                if (p.AddChild(gameRef))
                                {
                                    break;
                                }
                            }

                            // because we performing this operation outside of TransactionContext
                            // we must set Document Dirty flag.
                            gameDocument.Dirty = true;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                        }
                    }
                }
                break;

            case Command.AddSubGame:
                if (gameDocument != null)
                {
                    filePath = Util.GetFilePath(m_fileFilter,
                                                System.IO.Path.GetDirectoryName(gameDocument.Uri.LocalPath),
                                                false);

                    if (!string.IsNullOrEmpty(filePath))
                    {
                        try
                        {
                            if (!m_gameEditor.Info.IsCompatiblePath(filePath))
                            {
                                throw new Exception("Incompatible file type " + filePath);
                            }

                            Uri ur = new Uri(filePath);
                            if (m_gameDocumentRegistry.FindDocument(ur) != null)
                            {
                                throw new Exception(filePath + " is already open");
                            }

                            GameReference gameRef = GameReference.CreateNew(ur);
                            gameRef.Resolve();

                            // try all implementation of IHierarchical until one works
                            var parent = game.AsAll <IHierarchical>();
                            foreach (var p in parent)
                            {
                                if (p.AddChild(gameRef))
                                {
                                    break;
                                }
                            }

                            // because we performing this operation outside of TransactionContext
                            // we must set Document Dirty flag.
                            gameDocument.Dirty = true;
                            RefreshLayerContext();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                        }
                    }
                }
                break;

            case Command.Exclude:
            {
                GameReference gameRef = target.As <GameReference>();
                if (gameRef == null)
                {
                    break;
                }

                gameDocument = gameRef.DomNode.Parent.Cast <IDocument>();
                IGameDocument subDoc = gameRef.Target.Cast <IGameDocument>();

                bool exclue = true;
                bool save   = false;
                if (subDoc.Dirty)
                {
                    string       msg       = "Save changes\r\n" + subDoc.Uri.LocalPath;
                    DialogResult dlgResult =
                        MessageBox.Show(m_mainWindow.DialogOwner, msg, m_mainWindow.Text
                                        , MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                    save   = dlgResult == DialogResult.Yes;
                    exclue = dlgResult != DialogResult.Cancel;
                }

                if (save)
                {
                    subDoc.Save(subDoc.Uri, m_schemaLoader);
                }

                if (exclue)
                {
                    gameRef.DomNode.RemoveFromParent();
                    // because we performing this operation outside of TransactionContext
                    // we must set Document Dirty flag.
                    gameDocument.Dirty = true;
                    UpdateGameObjectReferences();
                    RefreshLayerContext();
                }
            }
            break;

            case Command.Resolve:
            {
                bool          madeChange = false;
                GameReference gameRef    = target.As <GameReference>();
                if (gameRef != null)
                {
                    gameRef.Resolve();
                    madeChange = true;
                }
                else
                {
                    var resolveable = target.As <IResolveable>();
                    if (resolveable != null && !resolveable.IsResolved())
                    {
                        resolveable.Resolve();
                        madeChange = true;
                    }
                }

                if (madeChange)
                {
                    TreeControlAdapter.Refresh(target);
                    RefreshLayerContext();
                }
            }
            break;

            case Command.Unresolve:
            {
                try
                {
                    GameReference gameRef = target.As <GameReference>();
                    if (gameRef != null)
                    {
                        IGameDocument subDoc    = gameRef.Target.Cast <IGameDocument>();
                        bool          unresolve = true;
                        bool          save      = false;
                        if (subDoc.Dirty)
                        {
                            string       msg       = "Save changes\r\n" + subDoc.Uri.LocalPath;
                            DialogResult dlgResult =
                                MessageBox.Show(m_mainWindow.DialogOwner, msg, m_mainWindow.Text
                                                , MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                            save      = dlgResult == DialogResult.Yes;
                            unresolve = dlgResult != DialogResult.Cancel;
                        }
                        //cando = gameRef != null && gameRef.Target != null;
                        if (save)
                        {
                            subDoc.Save(subDoc.Uri, m_schemaLoader);
                        }
                        if (unresolve)
                        {
                            gameRef.Unresolve();
                            UpdateGameObjectReferences();
                            RefreshLayerContext();
                        }
                        TreeControlAdapter.Refresh(gameRef);
                    }
                    else
                    {
                        var resolveable = target.As <IResolveable>();
                        if (resolveable != null && resolveable.IsResolved())
                        {
                            resolveable.Unresolve();
                            RefreshLayerContext();
                            TreeControlAdapter.Refresh(target);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                }
            }
            break;

            case Command.CreateNewResolveable:
            {
                try
                {
                    var resolveable = target.As <IResolveable>();
                    if (resolveable != null && !resolveable.IsResolved() && resolveable.CanCreateNew())
                    {
                        resolveable.CreateAndResolve();
                        RefreshLayerContext();
                        TreeControlAdapter.Refresh(target);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                }
            }
            break;

            case Command.SaveSubDoc:
            {
                try
                {
                    var resolveable = target.As <IResolveable>();
                    if (resolveable != null && resolveable.CanSave())
                    {
                        resolveable.Save(m_schemaLoader);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                }
            }
            break;

            default:
                throw new ArgumentOutOfRangeException("commandTag");
            }
            m_designView.InvalidateViews();
            Refresh();
        }
Exemple #8
0
        void ICommandClient.DoCommand(object commandTag)
        {
            IGame game = TreeControlAdapter.LastHit.As <IGame>();

            if (game == null)
            {
                GameReference gameRef = TreeControlAdapter.LastHit.As <GameReference>();
                game = gameRef.Target;
            }
            IDocument gameDocument = game.As <IDocument>();
            string    filePath     = null;

            switch ((Command)commandTag)
            {
            case Command.CreateNewSubGame:
                filePath = Util.GetFilePath(m_fileFilter,
                                            System.IO.Path.GetDirectoryName(gameDocument.Uri.LocalPath), true);
                if (!string.IsNullOrEmpty(filePath))
                {
                    try
                    {
                        if (!m_gameEditor.Info.IsCompatiblePath(filePath))
                        {
                            throw new Exception("Incompatible file type " + filePath);
                        }

                        Uri ur = new Uri(filePath);
                        if (m_gameDocumentRegistry.FindDocument(ur) != null)
                        {
                            throw new Exception(filePath + " is already open");
                        }
                        GameDocument subGame = GameDocument.OpenOrCreate(ur, m_schemaLoader);
                        subGame.Dirty = true;
                        GameReference gameRef = GameReference.CreateNew(subGame);
                        IHierarchical parent  = game.As <IHierarchical>();
                        parent.AddChild(gameRef);
                        // because we performing this operation outside of TransactionContext
                        // we must set Document Dirty flag.
                        gameDocument.Dirty = true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                    }
                }
                break;

            case Command.AddSubGame:

                filePath = Util.GetFilePath(m_fileFilter,
                                            System.IO.Path.GetDirectoryName(gameDocument.Uri.LocalPath),
                                            false);

                if (!string.IsNullOrEmpty(filePath))
                {
                    try
                    {
                        if (!m_gameEditor.Info.IsCompatiblePath(filePath))
                        {
                            throw new Exception("Incompatible file type " + filePath);
                        }

                        Uri ur = new Uri(filePath);
                        if (m_gameDocumentRegistry.FindDocument(ur) != null)
                        {
                            throw new Exception(filePath + " is already open");
                        }

                        GameReference gameRef = GameReference.CreateNew(ur);
                        gameRef.Resolve();
                        IHierarchical parent = game.As <IHierarchical>();
                        parent.AddChild(gameRef);

                        // because we performing this operation outside of TransactionContext
                        // we must set Document Dirty flag.
                        gameDocument.Dirty = true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                    }
                }
                break;

            case Command.Exclude:
            {
                GameReference gameRef = TreeControlAdapter.LastHit.As <GameReference>();
                gameDocument = gameRef.DomNode.Parent.Cast <IDocument>();
                GameDocument subDoc = gameRef.Target.Cast <GameDocument>();

                bool exclue = true;
                bool save   = false;
                if (subDoc.Dirty)
                {
                    string       msg       = "Save changes\r\n" + subDoc.Uri.LocalPath;
                    DialogResult dlgResult =
                        MessageBox.Show(m_mainWindow.DialogOwner, msg, m_mainWindow.Text
                                        , MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                    save   = dlgResult == DialogResult.Yes;
                    exclue = dlgResult != DialogResult.Cancel;
                }

                if (save)
                {
                    subDoc.Save(subDoc.Uri, m_schemaLoader);
                }

                if (exclue)
                {
                    gameRef.DomNode.RemoveFromParent();
                    // because we performing this operation outside of TransactionContext
                    // we must set Document Dirty flag.
                    gameDocument.Dirty = true;
                }
            }
            break;

            case Command.Resolve:
            {
                GameReference gameRef = TreeControlAdapter.LastHit.As <GameReference>();
                gameRef.Resolve();
                TreeControlAdapter.Refresh(gameRef);
            }
            break;

            case Command.Unresolve:
            {
                try
                {
                    GameReference gameRef   = TreeControlAdapter.LastHit.As <GameReference>();
                    GameDocument  subDoc    = gameRef.Target.Cast <GameDocument>();
                    bool          unresolve = true;
                    bool          save      = false;
                    if (subDoc.Dirty)
                    {
                        string       msg       = "Save changes\r\n" + subDoc.Uri.LocalPath;
                        DialogResult dlgResult =
                            MessageBox.Show(m_mainWindow.DialogOwner, msg, m_mainWindow.Text
                                            , MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                        save      = dlgResult == DialogResult.Yes;
                        unresolve = dlgResult != DialogResult.Cancel;
                    }
                    //cando = gameRef != null && gameRef.Target != null;
                    if (save)
                    {
                        subDoc.Save(subDoc.Uri, m_schemaLoader);
                    }
                    if (unresolve)
                    {
                        gameRef.Unresolve();
                    }
                    TreeControlAdapter.Refresh(gameRef);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                }
            }
            break;

            default:
                throw new ArgumentOutOfRangeException("commandTag");
            }
            m_designView.InvalidateViews();
            Refresh();
        }