Skip to content

Commit

Permalink
- Moves settings.json to AppdataLocal/CoordinateConverter (Fixes #17)
Browse files Browse the repository at this point in the history
- Adds more Exceptions
  • Loading branch information
FalcoGer committed Jul 29, 2024
1 parent e6093a4 commit 90aa17d
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 49 deletions.
165 changes: 119 additions & 46 deletions CoordinateConverter/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace CoordinateConverter
/// <seealso cref="Form" />
public partial class MainForm : Form
{
private readonly GitHub.Version VERSION = new GitHub.Version(0, 10, 2);
private readonly GitHub.Version VERSION = new GitHub.Version(0, 10, 3);

#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public static readonly Color ERROR_COLOR = Color.Pink;
Expand Down Expand Up @@ -2729,36 +2729,61 @@ private void Lbl_DCS_Status_BackColorChanged(object objSender, EventArgs e)

private void Tsmi_TerrainElevationUnderCamera_Click(object sender, EventArgs e)
{
SettingsData.CameraPosMode = Settings.ECameraPosMode.TerrainElevation;
SettingsData.Save();
tsmi_TerrainElevationUnderCamera.Checked = true;
tsmi_CameraAltitude.Checked = false;
try
{

SettingsData.CameraPosMode = Settings.ECameraPosMode.TerrainElevation;
SettingsData.Save();
tsmi_TerrainElevationUnderCamera.Checked = true;
tsmi_CameraAltitude.Checked = false;
}
catch (Exception ex)
{
lbl_Error.Visible = true;
lbl_Error.Text = ex.Message;
}
}

private void Tsmi_CameraAltitude_Click(object sender, EventArgs e)
{
SettingsData.CameraPosMode = Settings.ECameraPosMode.CameraAltitude;
SettingsData.Save();
tsmi_TerrainElevationUnderCamera.Checked = false;
tsmi_CameraAltitude.Checked = true;
try
{
SettingsData.CameraPosMode = Settings.ECameraPosMode.CameraAltitude;
SettingsData.Save();
tsmi_TerrainElevationUnderCamera.Checked = false;
tsmi_CameraAltitude.Checked = true;
}
catch (Exception ex)
{
lbl_Error.Visible = true;
lbl_Error.Text = ex.Message;
}
}

#region ReticleSettings

private void Tsmi_Screen_Click(object objSender, EventArgs e)
{
// Gets the screen associated with the menu item and sets the reticle to the center of that screen
ToolStripMenuItem sender = objSender as ToolStripMenuItem;
SettingsData.DCSMonitor = int.Parse(sender.Name.Split('_').Last());
SettingsData.Save();

// Unsets all checkboxes except the one clicked
foreach (ToolStripMenuItem mi in tsmi_DCSMainScreenMenu.DropDownItems)
try
{
mi.Checked = mi.Name == sender.Name;
}
// Gets the screen associated with the menu item and sets the reticle to the center of that screen
ToolStripMenuItem sender = objSender as ToolStripMenuItem;
SettingsData.DCSMonitor = int.Parse(sender.Name.Split('_').Last());
SettingsData.Save();

CenterReticle();
// Unsets all checkboxes except the one clicked
foreach (ToolStripMenuItem mi in tsmi_DCSMainScreenMenu.DropDownItems)
{
mi.Checked = mi.Name == sender.Name;
}

CenterReticle();
}
catch (Exception ex)
{
lbl_Error.Visible = true;
lbl_Error.Text = ex.Message;
}
}

private void CenterReticle()
Expand All @@ -2769,23 +2794,47 @@ private void CenterReticle()
}
private void Tsmi_Reticle_WhenInF10Map_Click(object sender, EventArgs e)
{
SettingsData.ReticleSetting = Settings.EReticleSetting.WhenF10;
SettingsData.Save();
SetReticleSettingsCheckmarks();
try
{
SettingsData.ReticleSetting = Settings.EReticleSetting.WhenF10;
SettingsData.Save();
SetReticleSettingsCheckmarks();
}
catch (Exception ex)
{
lbl_Error.Visible = true;
lbl_Error.Text = ex.Message;
}
}

private void Tsmi_Reticle_Always_Click(object sender, EventArgs e)
{
SettingsData.ReticleSetting = Settings.EReticleSetting.Always;
SettingsData.Save();
SetReticleSettingsCheckmarks();
try
{
SettingsData.ReticleSetting = Settings.EReticleSetting.Always;
SettingsData.Save();
SetReticleSettingsCheckmarks();
}
catch (Exception ex)
{
lbl_Error.Visible = true;
lbl_Error.Text = ex.Message;
}
}

private void Tsmi_Reticle_Never_Click(object sender, EventArgs e)
{
SettingsData.ReticleSetting = Settings.EReticleSetting.Never;
SettingsData.Save();
SetReticleSettingsCheckmarks();
try
{
SettingsData.ReticleSetting = Settings.EReticleSetting.Never;
SettingsData.Save();
SetReticleSettingsCheckmarks();
}
catch (Exception ex)
{
lbl_Error.Visible = true;
lbl_Error.Text = ex.Message;
}
}

private void SetReticleSettingsCheckmarks()
Expand Down Expand Up @@ -2853,7 +2902,15 @@ private void Control_AlwaysOnTop_Click(object sender, EventArgs e)
/// </summary>
public MainForm()
{
SettingsData = Settings.Load();
try
{
SettingsData = Settings.Load();
}
catch (Exception ex)
{
string exText = @"Could not load settings.\n{ex.Message}\n\n{ex.StackTrace}";
MessageBox.Show(text: exText, caption: ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
}

InitializeComponent();

Expand Down Expand Up @@ -2953,10 +3010,18 @@ private void Tsmi_CheckForUpdates_Click(object sender, EventArgs e)

private void tsmi_AutoCheckForUpdates_Click(object sender, EventArgs e)
{
ToolStripMenuItem tsmi = sender as ToolStripMenuItem;
tsmi.Checked = !tsmi.Checked;
SettingsData.AutoCheckForUpdates = tsmi.Checked;
SettingsData.Save();
try
{
ToolStripMenuItem tsmi = sender as ToolStripMenuItem;
tsmi.Checked = !tsmi.Checked;
SettingsData.AutoCheckForUpdates = tsmi.Checked;
SettingsData.Save();
}
catch (Exception ex)
{
lbl_Error.Visible = true;
lbl_Error.Text = ex.Message;
}
}

private void Tsmi_AH64_DTC_Click(object sender, EventArgs e)
Expand All @@ -2980,23 +3045,31 @@ private void tsmi_exit_Click(object sender, EventArgs e)

private void tsmi_changeBaseDirectory_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog fbd = new FolderBrowserDialog()
{
Description = "Select the folder where the JSON files are located.",
SelectedPath = SettingsData.GetDirectory(Settings.ELastFileSource.Points).FullName,
ShowNewFolderButton = true
})
try
{
fbd.ShowDialog(this);

if (fbd.SelectedPath != null)
using (FolderBrowserDialog fbd = new FolderBrowserDialog()
{
SettingsData.BaseDirectory = new DirectoryInfo(fbd.SelectedPath);
SettingsData.Save();
sfd.InitialDirectory = SettingsData.GetDirectory(Settings.ELastFileSource.Points).FullName;
ofd.InitialDirectory = SettingsData.GetDirectory(Settings.ELastFileSource.Points).FullName;
Description = "Select the folder where the JSON files are located.",
SelectedPath = SettingsData.GetDirectory(Settings.ELastFileSource.Points).FullName,
ShowNewFolderButton = true
})
{
fbd.ShowDialog(this);

if (fbd.SelectedPath != null)
{
SettingsData.BaseDirectory = new DirectoryInfo(fbd.SelectedPath);
SettingsData.Save();
sfd.InitialDirectory = SettingsData.GetDirectory(Settings.ELastFileSource.Points).FullName;
ofd.InitialDirectory = SettingsData.GetDirectory(Settings.ELastFileSource.Points).FullName;
}
}
}
catch (Exception ex)
{
lbl_Error.Visible = true;
lbl_Error.Text = ex.Message;
}
}
}
}
9 changes: 8 additions & 1 deletion CoordinateConverter/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static private int GetPrimaryMonitor()
/// The settings file.
/// </value>
[JsonIgnore]
public static FileInfo SettingsFile { get; private set; } = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.json"));
public static FileInfo SettingsFile { get; private set; } = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CoordinateConverter", "settings.json"));

/// <summary>
/// The source for the last files directory
Expand Down Expand Up @@ -245,6 +245,13 @@ public void AddFile(ELastFileSource source, string filePath)
/// </summary>
static public Settings Load()
{
DirectoryInfo settingsDirectory = SettingsFile.Directory;

if (!settingsDirectory.Exists)
{
settingsDirectory.Create();
}

if (!SettingsFile.Exists)
{
Settings settings = new Settings();
Expand Down
4 changes: 2 additions & 2 deletions Installer/Installer.wixproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="WixToolset.Sdk/5.0.0">
<PropertyGroup>
<DefineConstants>Version=0.10.2</DefineConstants>
<DefineConstants>Version=0.10.3</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<DefineConstants>Version=0.10.2</DefineConstants>
<DefineConstants>Version=0.10.3</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="WixToolset.Netfx.wixext" Version="5.0.0" />
Expand Down

0 comments on commit 90aa17d

Please sign in to comment.