| 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
This commit is contained in:
Alban Nanty
2020-09-22 16:59:33 +08:00
parent 91ccf71f74
commit 7d0f87cb42
4 changed files with 47 additions and 36 deletions

View File

@@ -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();
}

View File

@@ -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;
}
}
}
}

View File

@@ -382,5 +382,8 @@
<Setting Name="OtherHullThickness" Type="System.Single" Scope="User">
<Value Profile="(Default)">1</Value>
</Setting>
<Setting Name="TemplateFilenameWhenCreatingANewMap" Type="System.String" Scope="User">
<Value Profile="(Default)">config/ClassicTemplate.bbm</Value>
</Setting>
</Settings>
</SettingsFile>

View File

@@ -400,6 +400,9 @@
<setting name="OtherHullThickness" serializeAs="String">
<value>1</value>
</setting>
<setting name="TemplateFilenameWhenCreatingANewMap" serializeAs="String">
<value>config/ClassicTemplate.bbm</value>
</setting>
</BlueBrick.Properties.Settings>
</userSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/></startup></configuration>