Esempio n. 1
0
        //---------------------------------------------------------------------------------------------------------------------------------------------------------
        //----------------------------------------------------------------------- OBJECT ----------------------------------------------------------------------------
        //---------------------------------------------------------------------------------------------------------------------------------------------------------
        public static void importObject(Form1 mainForm, string filename, BackgroundWorker worker)
        {
            CoronaLayer layerParent = mainForm.getElementTreeView().LayerSelected;
            if (layerParent != null)
            {
                string projectPath = layerParent.SceneParent.projectParent.ProjectPath;
                worker.ReportProgress(10);
                FileStream fs = File.OpenRead(filename);
                if (fs.Length > 0)
                {
                    MemoryStream ms = new MemoryStream();
                    ms.SetLength(fs.Length);

                    fs.Read(ms.GetBuffer(), 0, (int)ms.Length);
                    worker.ReportProgress(30);
                    CoronaObject obj = (CoronaObject)SerializerHelper.DeSerializeBinary(ms);
                    fs.Close();
                    ms.Close();
                    worker.ReportProgress(50);
                    if (obj != null)
                    {
                        obj.LayerParent = layerParent;
                        obj.DisplayObject.Name = layerParent.SceneParent.projectParent.IncrementObjectName(obj.DisplayObject.Name);
                        //Ajouter l'entité ay layer
                        layerParent.CoronaObjects.Add(obj);

                        string currentDir = Path.GetDirectoryName(filename);
                        if (Directory.Exists(currentDir + "\\Images"))
                        {
                            string[] files = Directory.GetFiles(currentDir + "\\Images");
                            for (int i = 0; i < files.Length; i++)
                            {
                                string fileNameOnly = Path.GetFileName(files[i]);
                                File.Copy(files[i],
                                     projectPath + "\\Resources\\Images\\" + fileNameOnly, true);
                            }
                        }

                        if (Directory.Exists(currentDir + "\\SpriteSheets"))
                        {
                            string[] sheetDirectories = Directory.GetDirectories(currentDir + "\\SpriteSheets");
                            for (int i = 0; i < sheetDirectories.Length; i++)
                            {
                                string finalDirectoryDestNameOnly = Path.GetFileName(sheetDirectories[i]);
                                if (!Directory.Exists(projectPath + "\\Resources\\SpriteSheets\\" + finalDirectoryDestNameOnly))
                                {
                                    Directory.CreateDirectory(projectPath + "\\Resources\\SpriteSheets\\" + finalDirectoryDestNameOnly);
                                }

                                string[] files = Directory.GetFiles(sheetDirectories[i]);
                                for (int j = 0; j < files.Length; j++)
                                {
                                    string fileNameOnly = Path.GetFileName(files[j]);
                                    File.Copy(files[j],
                                         projectPath + "\\Resources\\SpriteSheets\\" + finalDirectoryDestNameOnly + "\\" + fileNameOnly, true);
                                }

                            }
                        }

                        mainForm.sceneEditorView1.GraphicsContentManager.UpdateSpriteContent(obj, 1, System.Drawing.Point.Empty);
                        worker.ReportProgress(90);
                        mainForm.sceneEditorView1.GraphicsContentManager.CleanProjectBitmaps();

                        obj.objectAttachedToGenerator = null;

                    }
                    else
                    {
                        MessageBox.Show("Error during object importation!\n The file seems to be corrupted!", "Object Importation Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        mainForm.currentWorkerAction = "NONE";
                    }

                }
            }
        }
Esempio n. 2
0
        //---------------------------------------------------------------------------------------------------------------------------------------------------------
        //----------------------------------------------------------------------- LAYER ----------------------------------------------------------------------------
        //---------------------------------------------------------------------------------------------------------------------------------------------------------
        public static void importLayer(Form1 mainForm, string filename, BackgroundWorker worker)
        {
            Scene sceneParent = mainForm.getElementTreeView().SceneSelected;
            if (sceneParent != null)
            {
                string directoryName = Path.GetDirectoryName(filename);

                worker.ReportProgress(10);
                FileStream fs = File.OpenRead(filename);
                if (fs.Length > 0)
                {
                    MemoryStream ms = new MemoryStream();
                    ms.SetLength(fs.Length);

                    fs.Read(ms.GetBuffer(), 0, (int)ms.Length);
                    worker.ReportProgress(30);
                    CoronaLayer layer = (CoronaLayer)SerializerHelper.DeSerializeBinary(ms);
                    fs.Close();
                    ms.Close();
                    worker.ReportProgress(50);
                    if (layer != null)
                    {
                        layer.SceneParent = sceneParent;
                        //Ajouter la scene au projet
                        sceneParent.Layers.Add(layer);

                        layer.Name = sceneParent.projectParent.IncrementObjectName(layer.Name);

                        string oldMapName = "";
                        if (layer.TilesMap != null)
                        {
                            oldMapName = layer.TilesMap.TilesMapName;
                            layer.TilesMap.TilesMapName = sceneParent.projectParent.IncrementObjectName(layer.TilesMap.TilesMapName);
                        }

                        for (int i = 0; i < layer.CoronaObjects.Count; i++)
                        {
                            CoronaObject obj = layer.CoronaObjects[i];
                            if (obj.isEntity == false)
                            {
                                obj.DisplayObject.Name = sceneParent.projectParent.IncrementObjectName(obj.DisplayObject.Name);
                            }
                            else
                            {
                                obj.Entity.Name = sceneParent.projectParent.IncrementObjectName(obj.Entity.Name);
                                for (int j = 0; j < obj.Entity.CoronaObjects.Count; j++)
                                {
                                    obj.Entity.CoronaObjects[j].DisplayObject.Name = sceneParent.projectParent.IncrementObjectName(obj.Entity.CoronaObjects[j].DisplayObject.Name);
                                }
                            }
                        }

                        //Traiter les fichiers necessaires aux tilesmap
                        worker.ReportProgress(60);
                        int prog = 60;

                        if (Directory.Exists(directoryName + "\\Images"))
                        {
                            if (!Directory.Exists(sceneParent.projectParent.ProjectPath + "\\Resources\\Images"))
                                Directory.CreateDirectory(sceneParent.projectParent.ProjectPath + "\\Resources\\Images");

                            string[] files = Directory.GetFiles(directoryName + "\\Images");
                            for (int i = 0; i < files.Length; i++)
                            {
                                string fileNameOnly = Path.GetFileName(files[i]);
                                File.Copy(files[i], sceneParent.projectParent.ProjectPath + "\\Resources\\Images\\" + fileNameOnly, true);
                            }
                        }

                        if (Directory.Exists(directoryName + "\\SpriteSheets"))
                        {
                            if (!Directory.Exists(sceneParent.projectParent.ProjectPath + "\\Resources\\SpriteSheets"))
                                Directory.CreateDirectory(sceneParent.projectParent.ProjectPath + "\\Resources\\SpriteSheets");

                            string[] directories = Directory.GetDirectories(directoryName + "\\SpriteSheets");
                            for (int i = 0; i < directories.Length; i++)
                            {
                                string directoryNameOnly = Path.GetFileName(directories[i]);

                                if (!Directory.Exists(sceneParent.projectParent.ProjectPath + "\\Resources\\SpriteSheets\\" + directoryNameOnly))
                                    Directory.CreateDirectory(sceneParent.projectParent.ProjectPath + "\\Resources\\SpriteSheets\\" + directoryNameOnly);

                                string[] files = Directory.GetFiles(directories[i]);
                                for (int j = 0; j < files.Length; j++)
                                {
                                    string fileNameOnly = Path.GetFileName(files[j]);
                                    File.Copy(files[j], sceneParent.projectParent.ProjectPath + "\\Resources\\SpriteSheets\\" +
                                        directoryNameOnly + "\\" + fileNameOnly, true);
                                }
                            }

                        }

                        if (Directory.Exists(directoryName + "\\TileMaps") && layer.TilesMap!=null)
                        {
                            if (!Directory.Exists(sceneParent.projectParent.ProjectPath + "\\Resources\\TileMaps\\"+layer.TilesMap.TilesMapName))
                                Directory.CreateDirectory(sceneParent.projectParent.ProjectPath + "\\Resources\\TileMaps\\"+layer.TilesMap.TilesMapName);

                            string[] directories = Directory.GetDirectories(directoryName + "\\TileMaps");
                            for (int i = 0; i < directories.Length; i++)
                            {
                                string directoryNameOnly = Path.GetFileName(directories[i]);

                                string[] files = Directory.GetFiles(directories[i]);
                                for (int j = 0; j < files.Length; j++)
                                {
                                    string fileNameOnly = Path.GetFileName(files[j]);
                                    File.Copy(files[j], sceneParent.projectParent.ProjectPath + "\\Resources\\TileMaps\\" +
                                        layer.TilesMap.TilesMapName + "\\" + fileNameOnly, true);
                                }
                            }

                            //Copy all JSON Config files of the tile map
                            string[] jsonFiles = Directory.GetFiles(directoryName,"*.json");
                            for (int j = 0; j < jsonFiles.Length; j++)
                            {
                                string fileNameOnly = Path.GetFileName(jsonFiles[j]);
                                string newFileName = sceneParent.projectParent.SourceFolderPath + "\\" + fileNameOnly.Replace(oldMapName,layer.TilesMap.TilesMapName);
                                File.Copy(jsonFiles[j], newFileName, true);
                            }
                        }

                        if (layer.TilesMap != null)
                        {
                            worker.ReportProgress(prog + 5);

                            layer.TilesMap.reloadMapContent(sceneParent.projectParent.SourceFolderPath);

                        }

                        for (int i = 0; i < layer.CoronaObjects.Count; i++)
                        {
                            CoronaObject obj = layer.CoronaObjects[i];
                            mainForm.sceneEditorView1.GraphicsContentManager.UpdateSpriteContent(obj, 1, System.Drawing.Point.Empty);
                        }

                        for (int i = 0; i < layer.Controls.Count; i++)
                        {
                            Corona_Classes.Controls.CoronaControl obj = layer.Controls[i];
                            mainForm.sceneEditorView1.GraphicsContentManager.UpdateSpriteContent(obj, 1, System.Drawing.Point.Empty);
                        }

                        mainForm.sceneEditorView1.GraphicsContentManager.CleanProjectBitmaps();
                        worker.ReportProgress(90);

                    }
                    else
                    {
                        MessageBox.Show("Error during layer importation!\n The file seems to be corrupted!", "Layer Importation Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        mainForm.currentWorkerAction = "NONE";
                    }

                }
            }
        }