private void RedrawMap()
 {
     Log.Write("{0}: Rendering Map", GetType().Name);
     BackgroundImage.Dispose();
     if (F != null)
     {
         using (var BMP = MapRender.RenderFile(F))
         {
             //Add file info string
             using (var G = Graphics.FromImage(BMP))
             {
                 var Info = string.Format("{0}: {1} {2}",
                                          Path.GetFileName(FileName),
                                          DateTime.Now.ToShortDateString(),
                                          DateTime.Now.ToShortTimeString());
                 using (var F = new Font("Arial", 16))
                 {
                     var Pos = G.MeasureString(Info, F);
                     G.DrawString(
                         Info, F,
                         Brushes.Red,
                         (int)(BMP.Width - Pos.Width),
                         (int)(BMP.Height - Pos.Height));
                 }
             }
             BackgroundImage = new Bitmap(BMP);
             BMP.Save(Path.ChangeExtension(FileName, "png"));
         }
     }
     else
     {
         BackgroundImage = MapRender.GetMap();
     }
 }
        public frmMain(string InitialFile = null)
        {
            SettingsFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "settings.xml");
            if (File.Exists(SettingsFile))
            {
                try
                {
                    S = Settings.Load(File.ReadAllText(SettingsFile));
                }
                catch (Exception ex)
                {
                    Log.Write(new Exception("Unable to load the settings", ex));
                    Tools.E($"Unable to load your settings. Defaults will be applied. Reason:\r\n{ex.Message}", "Settings Loader");
                    S = new Settings();
                }
            }
            else
            {
                Log.Write("{0}: Creating settings for first run", GetType().Name);
                S = new Settings();
            }

            InitializeComponent();

            MapRender.MapForm = this;

            //Don't block the application startup with the image rendering stuff
            Thread T = new Thread(delegate()
            {
                Log.Write("{0}: Rendering map", GetType().Name);
                MapRender.Init();
                var img = MapRender.GetMap();
                Invoke((MethodInvoker) delegate
                {
                    BackgroundImage = img;
                    if (S.ShowWelcomeMessage)
                    {
                        S.ShowWelcomeMessage = false;
                        Tools.ShowHelp("Welcome");
                    }
                    if (!string.IsNullOrEmpty(InitialFile))
                    {
                        try
                        {
                            OpenFile(InitialFile);
                        }
                        catch (Exception ex)
                        {
                            Log.Write(new Exception("Unable to load file from command line argument", ex));
                            Tools.E($"Unable to open {InitialFile}\r\n{ex.Message}", "File error");
                        }
                    }
                    Log.Write("{0}: Initializer complete", GetType().Name);
                });
            });

            T.Start();

            SFD.InitialDirectory = OFD.InitialDirectory = Program.SaveDirectory;
            Tools.SetupEscHandler(this);
#if DEBUG
            Log.Write("{0}: Enabling debug menu items", GetType().Name);
            //Enable not fully implemented items
            inventoriesToolStripMenuItem.Visible = true;
#endif
        }
Example #3
0
        /// <summary>
        /// Form constructor
        /// </summary>
        /// <param name="InitialFile">Optional file to open after startup</param>
        public frmMain(string InitialFile = null)
        {
            RespectManagerSetting = string.IsNullOrEmpty(InitialFile);
            SettingsFile          = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "settings.xml");
            if (File.Exists(SettingsFile))
            {
                try
                {
                    S = Settings.Load(File.ReadAllText(SettingsFile));
                }
                catch (Exception ex)
                {
                    Log.Write(new Exception("Unable to load the settings", ex));
                    Tools.E($"Unable to load your settings. Defaults will be applied. Reason:\r\n{ex.Message}", "Settings Loader");
                    S = new Settings();
                }
            }
            else
            {
                Log.Write("{0}: Creating settings for first run", GetType().Name);
                S = new Settings();
            }
            //Set up feature reporter
            if (!S.DisableUsageReport)
            {
                if (S.ReportId == Guid.Empty || S.UseRandomId)
                {
                    S.ReportId = Guid.NewGuid();
                }
                FeatureReport.Id = S.ReportId;
            }
            else
            {
                //Make sure the Id id is always unset
                FeatureReport.Id = S.ReportId = Guid.Empty;
            }

            InitializeComponent(); //!!!Read+Write form components only after this line!!!

            //Always start from the save file directory
            //We can't set this directly in the properties because the path is dynamic
            OFD.InitialDirectory = Program.SaveDirectory;

            OriginalTitle = Text;

            //Enable the audio extractor only if QuickPlay is present
            if (QuickPlay.HasQuickPlay)
            {
                extractAudioToolStripMenuItem.Enabled = true;
            }

            //Register this as the map rendering form
            MapRender.MapForm = this;

            //Don't block the application startup with the image rendering stuff
            Thread T = new Thread(delegate()
            {
                Log.Write("{0}: Rendering initial map", GetType().Name);
                try
                {
                    MapRender.Init();
                }
                catch (Exception ex)
                {
                    Log.Write("{0}: Rendering initial map failed", GetType().Name);
                    Log.Write(ex);
                    BackgroundImage = new Bitmap(1, 1);
                    DisableImageRendering();
                    return;
                }
                var img = MapRender.GetMap();
                Invoke((MethodInvoker) delegate
                {
                    BackgroundImage = img;
                    //Open initial file if supplied
                    if (!string.IsNullOrEmpty(InitialFile))
                    {
                        try
                        {
                            FeatureReport.Used(FeatureReport.Feature.OpenByCommandLine);
                            OpenFile(InitialFile);
                        }
                        catch (Exception ex)
                        {
                            Log.Write(new Exception("Unable to load file from command line argument", ex));
                            Tools.E($"Unable to open {InitialFile}\r\n{ex.Message}", "File error");
                        }
                    }
                    Log.Write("{0}: Initializer complete", GetType().Name);
                });
            });

            T.Start();

            SFD.InitialDirectory = OFD.InitialDirectory = Program.SaveDirectory;
            Tools.SetupKeyHandlers(this);
            HandleSettingsChange();
#if DEBUG
            Log.Write("{0}: Enabling debug menu items", GetType().Name);
            //Enable not fully implemented items
            inventoriesToolStripMenuItem.Visible = true;
            removeRocksToolStripMenuItem.Visible = true;
#endif
        }
Example #4
0
        /// <summary>
        /// Redraws the map
        /// </summary>
        private void RedrawMap()
        {
            Image I = null;

            if (!RenderImage)
            {
                Log.Write("{0}: Map Rendering is disabled in RedrawMap()", GetType().Name);
                return;
            }
            Log.Write("{0}: Rendering Map", GetType().Name);
            if (F != null)
            {
                try
                {
                    I = MapRender.RenderFile(F);
                }
                catch (Exception ex)
                {
                    Log.Write("{0}: Error when rendering bitmap", GetType().Name);
                    Log.Write(ex);
                    DisableImageRendering();
                    return;
                }
                try
                {
                    //Add file info string
                    using (var G = Graphics.FromImage(I))
                    {
                        var Info = string.Format("{0}: {1} {2}",
                                                 Path.GetFileName(FileName),
                                                 DateTime.Now.ToShortDateString(),
                                                 DateTime.Now.ToShortTimeString());
                        using (var F = new Font("Arial", 16))
                        {
                            //Align to the bottom right corner of the image
                            var Pos = G.MeasureString(Info, F);
                            G.DrawString(
                                Info, F,
                                Brushes.Red,
                                (int)(I.Width - Pos.Width),
                                (int)(I.Height - Pos.Height));
                        }
                    }
                    Log.Write("{0}: Setting map image in form", GetType().Name);
                    BackgroundImage.Dispose();
                    BackgroundImage = I;
                }
                catch (Exception ex)
                {
                    Log.Write("{0}: Error when rendering bitmap", GetType().Name);
                    Log.Write(ex);
                    try
                    {
                        I.Dispose();
                    }
                    finally
                    {
                        I = null;
                    }
                    DisableImageRendering();
                }
                if (I != null)
                {
                    //Save image in its own "try" block because if only this fails it's not a rendering error.
                    try
                    {
                        Log.Write("{0}: Saving map image to file", GetType().Name);
                        I.Save(Path.ChangeExtension(FileName, "png"));
                    }
                    catch (Exception ex)
                    {
                        Log.Write("{0}: Unable to save map image to file", GetType().Name);
                        Log.Write(ex);
                    }
                }
            }
            else
            {
                Log.Write("{0}: No file loaded. Drawing empty map", GetType().Name);
                try
                {
                    BackgroundImage.Dispose();
                    BackgroundImage = MapRender.GetMap();
                }
                catch (Exception ex)
                {
                    Log.Write("{0}: Error when rendering bitmap", GetType().Name);
                    Log.Write(ex);
                    DisableImageRendering();
                }
            }
        }