Exemplo n.º 1
0
        private void SaveHpi(UndoableMapModel map, string filename)
        {
            // flatten before save --- only the base tile is written to disk
            map.ClearSelection();

            var randomValue  = this.rng.Next(1000);
            var tmpExtension = $".mappytemp-{randomValue}";

            var tmpFileName = Path.ChangeExtension(filename, tmpExtension);

            try
            {
                MapSaver.SaveHpi(map, tmpFileName);
                File.Delete(filename);
                File.Move(tmpFileName, filename);
            }
            catch
            {
                // Normally the temp file is deleted by File.Replace.
                // Ensure that it is always deleted if an error occurs.
                File.Delete(tmpFileName);
                throw;
            }

            map.MarkSaved(filename);
        }
Exemplo n.º 2
0
        private void RefreshMinimapHighQualityWithProgressHelper(UndoableMapModel map)
        {
            var worker = Mappy.Util.Util.RenderMinimapWorker();

            var dlg = this.dialogService.CreateProgressView();

            dlg.Title       = "Generating Minimap";
            dlg.MessageText = "Generating high quality minimap...";

            dlg.CancelPressed         += (o, args) => worker.CancelAsync();
            worker.ProgressChanged    += (o, args) => dlg.Progress = args.ProgressPercentage;
            worker.RunWorkerCompleted += (o, args) =>
            {
                if (args.Error != null)
                {
                    Program.HandleUnexpectedException(args.Error);
                    Application.Exit();
                    return;
                }

                if (!args.Cancelled)
                {
                    var img = (Bitmap)args.Result;
                    map.SetMinimap(img);
                }

                dlg.Close();
            };

            worker.RunWorkerAsync(map);
            dlg.Display();
        }
Exemplo n.º 3
0
        private static void Save(UndoableMapModel map, string filename)
        {
            // flatten before save --- only the base tile is written to disk
            map.ClearSelection();

            var tntName = filename;
            var otaName = Path.ChangeExtension(filename, ".ota");

            var tmpTntName = tntName + ".mappytemp";
            var tmpOtaName = otaName + ".mappytemp";

            try
            {
                MapSaver.SaveTnt(map, tmpTntName);
                MapSaver.SaveOta(map.Attributes, tmpOtaName);
                File.Delete(tntName);
                File.Delete(otaName);
                File.Move(tmpTntName, tntName);
                File.Move(tmpOtaName, otaName);
            }
            catch
            {
                // Normally the temp files are deleted by File.Replace.
                // Ensure that they are always deleted if an error occurs.
                File.Delete(tmpTntName);
                File.Delete(tmpOtaName);
                throw;
            }

            map.MarkSaved(filename);
        }
Exemplo n.º 4
0
        private void ImportCustomSectionHelper(UndoableMapModel map)
        {
            var paths = this.dialogService.AskUserToChooseSectionImportPaths();

            if (paths == null)
            {
                return;
            }

            var dlg = this.dialogService.CreateProgressView();

            var bg = new BackgroundWorker();

            bg.WorkerSupportsCancellation = true;
            bg.WorkerReportsProgress      = true;
            bg.DoWork += (sender, args) =>
            {
                var w    = (BackgroundWorker)sender;
                var sect = this.imageImportingService.ImportSection(
                    paths.GraphicPath,
                    paths.HeightmapPath,
                    w.ReportProgress,
                    () => w.CancellationPending);
                if (sect == null)
                {
                    args.Cancel = true;
                    return;
                }

                args.Result = sect;
            };

            bg.ProgressChanged += (sender, args) => dlg.Progress = args.ProgressPercentage;
            dlg.CancelPressed  += (sender, args) => bg.CancelAsync();

            bg.RunWorkerCompleted += (sender, args) =>
            {
                dlg.Close();

                if (args.Error != null)
                {
                    this.dialogService.ShowError(
                        "There was a problem importing the section: " + args.Error.Message);
                    return;
                }

                if (args.Cancelled)
                {
                    return;
                }

                map.PasteMapTileNoDeduplicateTopLeft((IMapTile)args.Result);
            };

            bg.RunWorkerAsync();

            dlg.Display();
        }
Exemplo n.º 5
0
        private static bool TryCopyForFill(UndoableMapModel map)
        {
            if (map.SelectedTile.HasValue)
            {
                FillTile = map.FloatingTiles[map.SelectedTile.Value].Item;
                return(true);
            }

            return(false);
        }
Exemplo n.º 6
0
        public VoidLayer(UndoableMapModel map)
        {
            var grid = new BindingGrid <bool>(map.Voids);

            this.painter = new VoidPainter(map.Voids, map.BaseTile.HeightGrid, new Size(CellSize, CellSize));

            var voidChangeEvents = Observable.FromEventPattern <GridEventArgs>(
                e => grid.CellsChanged += e,
                e => grid.CellsChanged -= e)
                                   .Select(e => new GridCoordinates(e.EventArgs.X, e.EventArgs.Y));

            var heightchangeEvents = Observable.FromEventPattern <GridEventArgs>(
                e => map.BaseTileHeightChanged += e,
                e => map.BaseTileHeightChanged -= e)
                                     .Select(e => new GridCoordinates(e.EventArgs.X, e.EventArgs.Y));

            this.eventSubscription = voidChangeEvents.Merge(heightchangeEvents)
                                     .Select(GetCellRectangle)
                                     .Subscribe(this.OnLayerChanged);
        }
Exemplo n.º 7
0
        private void ExportMinimapHelper(UndoableMapModel map)
        {
            var loc = this.dialogService.AskUserToSaveMinimap();

            if (loc == null)
            {
                return;
            }

            try
            {
                using (var s = File.Create(loc))
                {
                    map.Minimap.Save(s, ImageFormat.Png);
                }
            }
            catch (Exception)
            {
                this.dialogService.ShowError("There was a problem saving the minimap.");
            }
        }
Exemplo n.º 8
0
        private void ExportHeightmapHelper(UndoableMapModel map)
        {
            var loc = this.dialogService.AskUserToSaveHeightmap();

            if (loc == null)
            {
                return;
            }

            try
            {
                var b = Mappy.Util.Util.ExportHeightmap(map.BaseTile.HeightGrid);
                using (var s = File.Create(loc))
                {
                    b.Save(s, ImageFormat.Png);
                }
            }
            catch (Exception)
            {
                this.dialogService.ShowError("There was a problem saving the heightmap.");
            }
        }
Exemplo n.º 9
0
        private bool SaveHelper(UndoableMapModel map, string filename)
        {
            if (filename == null)
            {
                throw new ArgumentNullException(nameof(filename));
            }

            var extension = Path.GetExtension(filename).ToUpperInvariant();

            try
            {
                switch (extension)
                {
                case ".TNT":
                    Save(map, filename);
                    return(true);

                case ".HPI":
                case ".UFO":
                case ".CCX":
                case ".GPF":
                case ".GP3":
                    this.SaveHpi(map, filename);
                    return(true);

                default:
                    this.dialogService.ShowError("Unrecognized file extension: " + extension);
                    return(false);
                }
            }
            catch (IOException e)
            {
                this.dialogService.ShowError("Error saving map: " + e.Message);
                return(false);
            }
        }
Exemplo n.º 10
0
        private static bool TryCopyToClipboard(UndoableMapModel map)
        {
            if (map.SelectedFeatures.Count > 0)
            {
                if (map.SelectedFeatures.Count == 1)
                {
                    var id   = map.SelectedFeatures.First();
                    var inst = map.GetFeatureInstance(id);
                    var rec  = new FeatureClipboardRecord(inst.FeatureName);
                    Clipboard.SetData(DataFormats.Serializable, rec);
                    return(true);
                }

                var loc      = map.ViewportLocation;
                var ids      = map.SelectedFeatures.ToArray();
                var features = new List <FeatureClipboardRecord>();

                for (int i = 0; i < ids.Length; i++)
                {
                    var ins = map.GetFeatureInstance(ids[i]);
                    features.Add(new FeatureClipboardRecord(ins.FeatureName, (ins.X * 16) - loc.X, (ins.Y * 16) - loc.Y));
                }

                Clipboard.SetData(DataFormats.Serializable, features);
                return(true);
            }

            if (map.SelectedTile.HasValue)
            {
                var tile = map.FloatingTiles[map.SelectedTile.Value].Item;
                Clipboard.SetData(DataFormats.Serializable, tile);
                return(true);
            }

            return(false);
        }
Exemplo n.º 11
0
        private void ExportMapImageHelper(UndoableMapModel map)
        {
            var loc = this.dialogService.AskUserToSaveMapImage();

            if (loc == null)
            {
                return;
            }

            var pv = this.dialogService.CreateProgressView();

            var tempLoc = loc + ".mappy-partial";

            var bg = new BackgroundWorker();

            bg.WorkerReportsProgress      = true;
            bg.WorkerSupportsCancellation = true;
            bg.DoWork += (sender, args) =>
            {
                var worker = (BackgroundWorker)sender;
                using (var s = File.Create(tempLoc))
                {
                    var success = Mappy.Util.Util.WriteMapImage(
                        s,
                        map.BaseTile.TileGrid,
                        worker.ReportProgress,
                        () => worker.CancellationPending);
                    args.Cancel = !success;
                }
            };

            bg.ProgressChanged += (sender, args) => pv.Progress = args.ProgressPercentage;
            pv.CancelPressed   += (sender, args) => bg.CancelAsync();

            bg.RunWorkerCompleted += (sender, args) =>
            {
                try
                {
                    pv.Close();

                    if (args.Cancelled)
                    {
                        return;
                    }

                    if (args.Error != null)
                    {
                        this.dialogService.ShowError("There was a problem saving the map image.");
                        return;
                    }

                    if (File.Exists(loc))
                    {
                        File.Replace(tempLoc, loc, null);
                    }
                    else
                    {
                        File.Move(tempLoc, loc);
                    }
                }
                finally
                {
                    if (File.Exists(tempLoc))
                    {
                        File.Delete(tempLoc);
                    }
                }
            };

            bg.RunWorkerAsync();
            pv.Display();
        }