From 7d0f87cb4264ee9efbee1054405d1475f6afe5d2 Mon Sep 17 00:00:00 2001 From: Alban Nanty Date: Tue, 22 Sep 2020 16:59:33 +0800 Subject: [PATCH] | first pass of refactoring to use template files when creating a new Map + add a parameter in the openMap() to specify if we open a template file or not + add a settings to store the template file name to load when creating a new map | change the createNewMap function to open the template file --- BlueBrick/MainForm.cs | 63 ++++++++++------------- BlueBrick/Properties/Settings.Designer.cs | 14 ++++- BlueBrick/Properties/Settings.settings | 3 ++ BlueBrick/app.config | 3 ++ 4 files changed, 47 insertions(+), 36 deletions(-) diff --git a/BlueBrick/MainForm.cs b/BlueBrick/MainForm.cs index 7268a62..425403e 100644 --- a/BlueBrick/MainForm.cs +++ b/BlueBrick/MainForm.cs @@ -505,17 +505,6 @@ namespace BlueBrick enablePasteButton(false); enableToolbarButtonOnItemSelection(false); enableToolbarButtonOnLayerSelection(false, false, false); - // check if we need to open a file or create a new map - if ((fileToOpen != null) && canOpenThisFile(fileToOpen)) - { - openMap(fileToOpen); - } - else - { - createNewMap(); - // we update the list in the else because it is already updated in the openMap() - UpdateRecentFileMenuFromConfigFile(); - } // check if we need to open a budget at startup if (Properties.Settings.Default.BudgetFilenameToLoadAtStartup != string.Empty) { @@ -530,6 +519,17 @@ namespace BlueBrick updateEnableStatusForBudgetMenuItem(); this.PartUsageListView.updateBudgetNotification(); } + // check if we need to open a file or create a new map + if ((fileToOpen != null) && canOpenThisFile(fileToOpen)) + { + openMap(fileToOpen); + } + else + { + createNewMap(); + // we update the list in the else because it is already updated in the openMap() + UpdateRecentFileMenuFromConfigFile(); + } } private void MainForm_Shown(object sender, EventArgs e) @@ -1300,31 +1300,23 @@ namespace BlueBrick { // trash the previous map reinitializeCurrentMap(); - // declare a variable to eventually choose the best layer to select after adding all of them - Layer layerToSelect = null; - // check if we need to add layer - if (Properties.Settings.Default.AddGridLayerOnNewMap) - ActionManager.Instance.doAction(new AddLayer("LayerGrid", false)); - if (Properties.Settings.Default.AddBrickLayerOnNewMap) + + // check the name of the template file to load when creating a new map, and load it if it is valid + string templateFileToOpen = Properties.Settings.Default.TemplateFilenameWhenCreatingANewMap; + if ((templateFileToOpen != null) && File.Exists(templateFileToOpen) && canOpenThisFile(templateFileToOpen)) { + // the template file seems valid, so open it + openMap(templateFileToOpen, true); + } + else + { + // no valid template file, create a default map with default settings + ActionManager.Instance.doAction(new AddLayer("LayerGrid", false)); ActionManager.Instance.doAction(new AddLayer("LayerBrick", false)); // by preference we want to select the brick layer - layerToSelect = Map.Instance.SelectedLayer; + Map.Instance.SelectedLayer = Map.Instance.SelectedLayer; } - if (Properties.Settings.Default.AddAreaLayerOnNewMap) - ActionManager.Instance.doAction(new AddLayer("LayerArea", false)); - if (Properties.Settings.Default.AddTextLayerOnNewMap) - { - ActionManager.Instance.doAction(new AddLayer("LayerText", false)); - // if there's no brick layer, the second choice is to select the text layer - if (layerToSelect == null) - layerToSelect = Map.Instance.SelectedLayer; - } - if (Properties.Settings.Default.AddRulerLayerOnNewMap) - ActionManager.Instance.doAction(new AddLayer("LayerRuler", false)); - // now select the prefered layer if any - if (layerToSelect != null) - Map.Instance.SelectedLayer = layerToSelect; + // after adding the two default layer, we reset the WasModified flag of the map // (and before the update of the title bar) Map.Instance.WasModified = false; @@ -1348,7 +1340,7 @@ namespace BlueBrick updateTitleBar(); } - private void openMap(string filename) + private void openMap(string filename, bool isTemplateFile = false) { // set the wait cursor this.Cursor = Cursors.WaitCursor; @@ -1388,12 +1380,13 @@ namespace BlueBrick // restore the cursor after loading this.Cursor = Cursors.Default; // save the current file name of the loaded map - if (isFileValid) + if (isFileValid && !isTemplateFile) changeCurrentMapFileName(filename, true); else changeCurrentMapFileName(Properties.Resources.DefaultSaveFileName, false); // update the recent file list - UpdateRecentFileMenuFromConfigFile(filename, isFileValid); + if (!isTemplateFile) + UpdateRecentFileMenuFromConfigFile(filename, isFileValid); // force a garbage collect because we just trashed the previous map GC.Collect(); } diff --git a/BlueBrick/Properties/Settings.Designer.cs b/BlueBrick/Properties/Settings.Designer.cs index a69e2d1..5ebe7ba 100644 --- a/BlueBrick/Properties/Settings.Designer.cs +++ b/BlueBrick/Properties/Settings.Designer.cs @@ -12,7 +12,7 @@ namespace BlueBrick.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.4.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.7.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -1391,5 +1391,17 @@ namespace BlueBrick.Properties { this["OtherHullThickness"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("config/ClassicTemplate.bbm")] + public string TemplateFilenameWhenCreatingANewMap { + get { + return ((string)(this["TemplateFilenameWhenCreatingANewMap"])); + } + set { + this["TemplateFilenameWhenCreatingANewMap"] = value; + } + } } } diff --git a/BlueBrick/Properties/Settings.settings b/BlueBrick/Properties/Settings.settings index 2af94f2..c8ce5b6 100644 --- a/BlueBrick/Properties/Settings.settings +++ b/BlueBrick/Properties/Settings.settings @@ -382,5 +382,8 @@ 1 + + config/ClassicTemplate.bbm + \ No newline at end of file diff --git a/BlueBrick/app.config b/BlueBrick/app.config index 4bc2ab0..73305b1 100644 --- a/BlueBrick/app.config +++ b/BlueBrick/app.config @@ -400,6 +400,9 @@ 1 + + config/ClassicTemplate.bbm +