From 634a27ff34ce3c5964b9cffd2586ad28d64ddc70 Mon Sep 17 00:00:00 2001
From: Tolly <34184902+TollyH@users.noreply.github.com>
Date: Sat, 26 Nov 2022 17:47:53 +0000
Subject: [PATCH] Implement config editor fully
---
CSMazeConfigEditor/ControlTag.cs | 20 ++++-
CSMazeConfigEditor/MainWindow.xaml | 64 +++++++--------
CSMazeConfigEditor/MainWindow.xaml.cs | 114 +++++++++++++++++++++++---
3 files changed, 150 insertions(+), 48 deletions(-)
diff --git a/CSMazeConfigEditor/ControlTag.cs b/CSMazeConfigEditor/ControlTag.cs
index 74b20b0..a95f46e 100644
--- a/CSMazeConfigEditor/ControlTag.cs
+++ b/CSMazeConfigEditor/ControlTag.cs
@@ -5,10 +5,10 @@ namespace CSMaze.ConfigEditor
{
internal class ControlTag : DependencyObject
{
- public static readonly DependencyProperty HeaderLabelProperty = DependencyProperty.Register("HeaderLabel", typeof(Label), typeof(ControlTag));
- public Label HeaderLabel
+ public static readonly DependencyProperty HeaderLabelProperty = DependencyProperty.Register("HeaderLabel", typeof(string), typeof(ControlTag));
+ public string HeaderLabel
{
- get => (Label)GetValue(HeaderLabelProperty);
+ get => (string)GetValue(HeaderLabelProperty);
set => SetValue(HeaderLabelProperty, value);
}
@@ -18,5 +18,19 @@ public string ConfigOption
get => (string)GetValue(ConfigOptionProperty);
set => SetValue(ConfigOptionProperty, value);
}
+
+ public static readonly DependencyProperty DecimalPlacesProperty = DependencyProperty.Register("DecimalPlaces", typeof(int), typeof(ControlTag));
+ public int DecimalPlaces
+ {
+ get => (int)GetValue(DecimalPlacesProperty);
+ set => SetValue(DecimalPlacesProperty, value);
+ }
+
+ public static readonly DependencyProperty DefaultValueProperty = DependencyProperty.Register("DefaultValue", typeof(double), typeof(ControlTag));
+ public double DefaultValue
+ {
+ get => (double)GetValue(DefaultValueProperty);
+ set => SetValue(DefaultValueProperty, value);
+ }
}
}
diff --git a/CSMazeConfigEditor/MainWindow.xaml b/CSMazeConfigEditor/MainWindow.xaml
index 63b5c55..cfe9168 100644
--- a/CSMazeConfigEditor/MainWindow.xaml
+++ b/CSMazeConfigEditor/MainWindow.xaml
@@ -10,142 +10,142 @@
-
+
-
+
-
+
-
-
-
-
-
+
+
+
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
-
+
+
+
-
+
diff --git a/CSMazeConfigEditor/MainWindow.xaml.cs b/CSMazeConfigEditor/MainWindow.xaml.cs
index 118411b..ef4ff33 100644
--- a/CSMazeConfigEditor/MainWindow.xaml.cs
+++ b/CSMazeConfigEditor/MainWindow.xaml.cs
@@ -1,17 +1,10 @@
-using System;
-using System.Collections.Generic;
+using IniParser;
+using IniParser.Model;
+using System;
+using System.IO;
using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
-using System.Windows.Data;
-using System.Windows.Documents;
-using System.Windows.Input;
-using System.Windows.Media;
-using System.Windows.Media.Imaging;
-using System.Windows.Navigation;
-using System.Windows.Shapes;
namespace CSMaze.ConfigEditor
{
@@ -20,19 +13,114 @@ namespace CSMaze.ConfigEditor
///
public partial class MainWindow : Window
{
- public MainWindow()
+ private readonly string configIniPath;
+ private readonly KeyDataCollection configOptions;
+
+ public MainWindow() : this(null) { }
+
+ public MainWindow(string? configIniPath)
{
+ this.configIniPath = configIniPath is not null ? configIniPath : "config.ini";
+ configOptions = File.Exists(this.configIniPath) ? new FileIniDataParser().ReadFile(this.configIniPath)["OPTIONS"] : new KeyDataCollection();
InitializeComponent();
+ ((ControlTag)displayColumnsSlider.Tag).DefaultValue = configOptions.ContainsKey("VIEWPORT_WIDTH")
+ ? double.Parse(configOptions["VIEWPORT_WIDTH"]) : ((ControlTag)viewportWidthSlider.Tag).DefaultValue;
+ foreach (UIElement child in basicPanel.Children.OfType().Concat(advancedPanel.Children.OfType()))
+ {
+ if (child.GetType() == typeof(Slider))
+ {
+ Slider sld = (Slider)child;
+ string configOption = ((ControlTag)sld.Tag).ConfigOption;
+ sld.Value = configOptions.ContainsKey(configOption) ? double.Parse(configOptions[configOption] == "" ? "-0.01" : configOptions[configOption])
+ : ((ControlTag)sld.Tag).DefaultValue;
+ // Force update function to fire even if value was the same
+ Slider_ValueChanged(sld, new RoutedPropertyChangedEventArgs(sld.Value, sld.Value));
+ }
+ else if (child.GetType() == typeof(CheckBox))
+ {
+ CheckBox cbx = (CheckBox)child;
+ string configOption = (string)cbx.Tag;
+ if (configOptions.ContainsKey(configOption))
+ {
+ cbx.IsChecked = configOptions[configOption] != "0";
+ }
+ }
+ }
}
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
{
-
+ if (((Slider)sender).Tag is not null)
+ {
+ if (sender == viewportWidthSlider && displayColumnsSlider is not null)
+ {
+ int newWidth = (int)e.NewValue;
+ // Display columns must always be less than or equal to view width.
+ displayColumnsSlider.Maximum = newWidth;
+ }
+ ControlTag tag = (ControlTag)((Slider)sender).Tag;
+ // Truncate the number of decimal places on a float represented as a string.
+ // If the float is negative, it will be converted to an empty string to represent None.
+ string toStore = e.NewValue >= 0 ? Math.Round(e.NewValue, tag.DecimalPlaces).ToString() : "";
+ // INI files can only contain strings
+ configOptions[tag.ConfigOption] = toStore;
+ Label headerLabel = (Label)FindName(tag.HeaderLabel);
+ headerLabel.Content = (string)headerLabel.Tag + $" — ({(toStore == "" ? "None" : toStore)})";
+ }
}
private void Check_Click(object sender, RoutedEventArgs e)
{
+ CheckBox cbSender = (CheckBox)sender;
+ configOptions[(string)cbSender.Tag] = cbSender.IsChecked is not null && cbSender.IsChecked.Value ? "1" : "0";
+ }
+ private void SaveButton_Click(object sender, RoutedEventArgs e)
+ {
+ IniData data = new();
+ _ = data.Sections.AddSection("OPTIONS");
+ data["OPTIONS"].Merge(configOptions);
+ new FileIniDataParser().WriteFile(configIniPath, data);
+ }
+
+ private int ParseInt(string fieldName, int defaultValue)
+ {
+ if (!configOptions.ContainsKey(fieldName))
+ {
+ return defaultValue;
+ }
+ string value = configOptions[fieldName];
+ return int.TryParse(value, out int intValue) ? intValue : defaultValue;
+ }
+
+ private float ParseFloat(string fieldName, float defaultValue)
+ {
+ if (!configOptions.ContainsKey(fieldName))
+ {
+ return defaultValue;
+ }
+ string value = configOptions[fieldName];
+ return float.TryParse(value, out float floatValue) ? floatValue : defaultValue;
+ }
+
+ private float? ParseOptionalFloat(string fieldName, float? defaultValue)
+ {
+ if (!configOptions.ContainsKey(fieldName))
+ {
+ return defaultValue;
+ }
+ string value = configOptions[fieldName];
+ return value == "" ? null : int.TryParse(value, out int floatValue) ? floatValue : defaultValue;
+ }
+
+ private bool ParseBool(string fieldName, bool defaultValue)
+ {
+ if (!configOptions.ContainsKey(fieldName))
+ {
+ return defaultValue;
+ }
+ string value = configOptions[fieldName];
+ return value != "0";
}
}
}