Ejemplo n.º 1
0
        private void buttonExportAsPng_Click(object sender, RoutedEventArgs e)
        {
            this.StatusText = "Starting export.";

            if (this.IsRunning)
            {
                this.StatusText = "Please wait until current process is completed.";
                return;
            }

            if (!System.IO.File.Exists(this.Settings.MapFilePath))
            {
                this.StatusText = "Please select a map file first.";
                return;
            }

            var dlgresult = m_SaveFileDialog.ShowDialog();

            if (dlgresult.Value == true)
            {
                string imagefilename = this.m_SaveFileDialog.FileName;
                this.Settings.ImageFilePath = imagefilename;

                // Ausgabedatei prüfen, ob Ordner existiert und Dateiname mit .png endet
                string outputFolder = System.IO.Path.GetDirectoryName(imagefilename);
                if (!Directory.Exists(outputFolder))
                {
                    Directory.CreateDirectory(outputFolder);
                }

                if (!imagefilename.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
                {
                    imagefilename += ".png";
                }


                this.m_Stopwatch.Restart();
                this.IsRunning = true;
                var bw = new BackgroundWorker();
                bw.DoWork += (s, a) =>
                {
                    var pois = POI.FromCsvFile(this.Settings.PoiFilePath);

                    //var waypoints = Util.GetPOIsFromSavegame(this.Settings.MapFilePath);
                    string directory   = System.IO.Path.GetDirectoryName(this.Settings.MapFilePath);
                    string ttpfilename = System.IO.Path.GetFileNameWithoutExtension(this.Settings.MapFilePath) + ".ttp";
                    ttpfilename = System.IO.Path.Combine(directory, ttpfilename);
                    var waypoints = POI.FromTtpFile(ttpfilename);
                    pois = pois.Concat(waypoints);

                    var map = new MapRenderer();
                    map.TileSize         = (int)this.Settings.SelectedTileSize;
                    map.RenderBackground = this.Settings.RenderBackground;
                    map.RenderGrid       = this.Settings.RenderGrid;
                    map.GridSize         = this.Settings.GridSize;
                    map.GridColor        = System.Drawing.Color.FromArgb(this.Settings.AlphaValue,
                                                                         System.Drawing.Color.FromName(this.Settings.SelectedGridColorName));
                    map.RenderRegionNumbers = this.Settings.RenderRegionNumbers;
                    map.RegionFontName      = this.Settings.RegionFontName;
                    map.RegionFontEmSize    = this.Settings.RegionFontEmSize;
                    map.RenderWaypoints     = this.Settings.RenderWaypoints;
                    map.WaypointFontColor   = System.Drawing.Color.FromName(this.Settings.SelectedWaypointFontColorName);
                    map.WaypointFontName    = this.Settings.WaypointFontName;
                    map.WaypointFontEmSize  = this.Settings.WaypointFontEmSize;
                    map.UseDataStore        = this.Settings.UseDataStore;

                    map.RenderWholeMap(this.Settings.MapFilePath, imagefilename, pois, (IProgressService)this);
                };
                bw.RunWorkerCompleted += (s, a) =>
                {
                    this.m_Stopwatch.Stop();
                    this.IsRunning = false;
                    if (a.Error != null)
                    {
                        this.StatusText = a.Error.GetType().ToString() + ": " + a.Error.Message.ToString();
                    }
                    else
                    {
                        this.StatusText = String.Format("Done in {0}.", this.m_Stopwatch.Elapsed);
                    }
                };
                bw.RunWorkerAsync();

                this.m_SaveFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(this.m_SaveFileDialog.FileName);
            }
            else
            {
                this.StatusText = "Export aborted.";
            }
        }
Ejemplo n.º 2
0
        public static int Main(string[] args)
        {
            // Applikation ist "Windows-Anwendung" -> losgelöst von bestehender Konsole
            // App wird geladen, damit der MapRenderer Zugriff auf die Ressourcen hat.
            App app = new App();

            if (args != null && args.Length > 0)
            {
                bool attached      = false;
                bool helpRequested = false;
                try
                {
                    // mit bestehender Konsole verbinden, sonst neue erstellen
                    attached = AttachConsole(-1);
                    if (!attached)
                    {
                        ShowConsoleWindow();
                    }

                    // weil gelöst von aktueller Konsole war, steht jetzt der Prompt in der Zeile -> Zeile leeren
                    int pos = Console.CursorLeft;
                    Console.CursorLeft = 0;
                    Console.Write(new String(' ', pos));
                    Console.CursorLeft = 0;

                    // Argumente parsen
                    AppSettings set = CommandLine <AppSettings> .Parse(args);

                    // wenn Hilfe angefordert, dann diese ausgeben
                    if (args.Contains("/?") || args.Contains("--help") || args.Contains("-h"))
                    {
                        CommandLine <AppSettings> .PrintHelp();

                        helpRequested = true;
                    }
                    else
                    {
                        IProgressService progress = new ConsoleOutput();
                        var mapfilename           = System.IO.Path.GetFileNameWithoutExtension(set.MapFilePath);
                        var mapdirectory          = System.IO.Path.GetDirectoryName(set.MapFilePath);
                        if (String.IsNullOrEmpty(set.ImageFilePath))
                        {
                            set.ImageFilePath = System.IO.Path.Combine(mapdirectory, mapfilename + ".png");
                        }

                        var pois = POI.FromCsvFile(set.PoiFilePath);
                        // PlayerProfile hat den Namen der Map-Datei, nur mit der Endung .ttp
                        string ttpfilename = System.IO.Path.Combine(mapdirectory, mapfilename + ".ttp");
                        var    waypoints   = POI.FromTtpFile(ttpfilename);
                        pois = pois.Concat(waypoints);

                        var map = new MapRenderer();
                        map.TileSize         = (int)set.SelectedTileSize;
                        map.RenderBackground = set.RenderBackground;
                        map.RenderGrid       = set.RenderGrid;
                        map.GridSize         = set.GridSize;
                        map.GridColor        = System.Drawing.Color.FromArgb(set.AlphaValue,
                                                                             System.Drawing.Color.FromName(set.SelectedGridColorName));
                        map.RenderRegionNumbers = set.RenderRegionNumbers;
                        map.RegionFontName      = set.RegionFontName;
                        map.RegionFontEmSize    = set.RegionFontEmSize;
                        map.RenderWaypoints     = set.RenderWaypoints;
                        map.WaypointFontColor   = System.Drawing.Color.FromName(set.SelectedWaypointFontColorName);
                        map.WaypointFontName    = set.WaypointFontName;
                        map.WaypointFontEmSize  = set.WaypointFontEmSize;
                        map.UseDataStore        = set.UseDataStore;

                        map.RenderWholeMap(set.MapFilePath, set.ImageFilePath, pois, progress);
                    }
                }
                catch (Exception ex)
                {
                    var oldfore = Console.ForegroundColor;
                    var oldback = Console.BackgroundColor;
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.WriteLine(ex.Message);
                    Console.ForegroundColor = oldfore;
                    Console.BackgroundColor = oldback;
                }
                finally
                {
                    /*
                     * Der auskommentierte Code sollte das Problem beheben, dass die Konsole nicht wieder auf den Prompt zurückspringt.
                     * Grund für das Verhalten: Applikation ist eine "Windows-Anwendung"
                     * Das Verhalten des Workarounds ist aber unlogisch, da einfach die Konsole aktiviert wird und damit das aktuelle Fenster seinen Fokus verliert.
                     * Besonders nervig, falls man grade in einem anderen Fenster etwas tippt.
                     *
                     * Wird die Anwendung über einen Shortcut gestartet, dann tritt das Verhalten nicht auf und die Konsole schließt sich automatisch.
                     */

                    //var hnd = GetConsoleWindow();
                    //bool res = false;
                    //if (hnd != IntPtr.Zero)
                    //    res = SetForegroundWindow(hnd);

                    if (!helpRequested)
                    {
                        Console.WriteLine("Please press Enter.");
                    }

                    if (attached)
                    {
                        FreeConsole();
                    }

                    //if (res != false)
                    if (helpRequested)
                    {
                        // normalen prompt wieder holen
                        // HACK from http://stackoverflow.com/questions/1305257/using-attachconsole-user-must-hit-enter-to-get-%20regular-command-line/2463039#2463039
                        System.Windows.Forms.SendKeys.SendWait("{ENTER}");
                    }
                }
            }
            else
            {
                app.Run(new MainWindow());
            }
            app.Shutdown(0);
            return(0);
        }