Skip to content

Commit

Permalink
Implement Slider for Mod Config
Browse files Browse the repository at this point in the history
  • Loading branch information
Sora-yx committed Oct 20, 2024
1 parent 3e68fce commit 1f6a689
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 5 deletions.
3 changes: 3 additions & 0 deletions SA-Mod-Manager/Configuration/ConfigSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public class ConfigSchemaProperty
public string MaxValue { get; set; }
[XmlElement]
public string HelpText { get; set; }
[XmlAttribute("slider")]
public bool Slider { get; set; }

public ConfigSchemaProperty()
{
Expand All @@ -82,6 +84,7 @@ public ConfigSchemaProperty()
MinValue = string.Empty;
MaxValue = string.Empty;
HelpText = string.Empty;
Slider = false;
}
}

Expand Down
138 changes: 135 additions & 3 deletions SA-Mod-Manager/FormBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Windows.Media;
using SAModManager.ModsCommon;
using SAModManager.Configuration;
using System.Reflection.Metadata;
using System.Windows.Documents;

namespace SAModManager
{
Expand All @@ -18,6 +20,17 @@ public class FormBuilder
#region Mod Config Form Build
static private ConfigSettings settings;

//Helper method for formatting the number
private static string FormatNumber(decimal number, bool intType = false)
{
if (number >= 1000 && number < 1000000)
return (number / 1000).ToString("0.#") + "k"; //For thousands (e.g. 30k)
else if (number >= 1000000)
return (number / 1000000).ToString("0.#") + "M"; //For millions (e.g. 1.5M)
else
return number.ToString(intType ? "" : "F2"); // Default to showing two decimal places
}

public static UIElement CreateLabel(ConfigSchemaProperty property, bool addColon = true)
{
string content = GetElementName(property);
Expand Down Expand Up @@ -146,11 +159,11 @@ public static UIElement CreateNumericBox(ConfigSchemaProperty property, CustomPr
numVal = 0;
Decimal numMax = Decimal.MaxValue;
if (property.MaxValue.ToString() != "")
if (!Decimal.TryParse(property.MaxValue.ToString(), out numMax))
if (!Decimal.TryParse(property.MaxValue.ToString().ToLower(), out numMax))
numMax = Decimal.MaxValue;
Decimal numMin = Decimal.MinValue;
if (property.MinValue.ToString() != "")
if (!Decimal.TryParse(property.MinValue.ToString(), out numMin))
if (!Decimal.TryParse(property.MinValue.ToString().ToLower(), out numMin))
numMin = Decimal.MinValue;

NumberBox element = new()
Expand Down Expand Up @@ -296,6 +309,100 @@ public static UIElement CreateCheckBox(ConfigSchemaProperty property, CustomProp
return panel;
}

public static UIElement CreateSlider(ConfigSchemaProperty property, CustomPropertyStore storeInfo)
{
bool isFloatType = property.Type.Equals("float", StringComparison.CurrentCultureIgnoreCase);
Grid panel = new()
{
ColumnDefinitions =
{
new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) }, // For the label
new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }, // For the slider
new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) }, // For the value text
},
Margin = ElementMargin,
Tag = property.HelpText
};

// Create the label on the left
panel.Children.Add(CreateLabel(property));
Grid.SetColumn(panel.Children[0], 0);

Border backing = new()
{
Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)),
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Margin = new Thickness(0)
};
panel.Children.Add(backing);
Grid.SetColumn(backing, 1);
Grid.SetColumnSpan(backing, 2);

if (!Decimal.TryParse(storeInfo.GetConfigValue().ToString(), out decimal numVal))
numVal = 0;
Decimal numMax = Decimal.MaxValue;
if (property.MaxValue.ToString() != "")
if (!Decimal.TryParse(property.MaxValue.ToString(), out numMax))
numMax = Decimal.MaxValue;
Decimal numMin = Decimal.MinValue;
if (property.MinValue.ToString() != "")
if (!Decimal.TryParse(property.MinValue.ToString(), out numMin))
numMin = Decimal.MinValue;

// Create the number label
TextBlock textBlock = new()
{
Text = isFloatType ? FormatNumber(numVal) : FormatNumber(numVal, true),

};

Label label = new()
{
Height = 30,
Width = numMax >= 100000 ? 45 : 30,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Right,
Content = textBlock,
Tag = property.HelpText,
};

if (isFloatType)
label.Width += 10;

// Create the slider
Slider element = new()
{
Width = numMax < 10000 ? 210 : 250,
Height = 22,
Value = ((double)numVal),
HorizontalAlignment = HorizontalAlignment.Right,
VerticalAlignment = VerticalAlignment.Center,
Minimum = ((double)numMin),
Maximum = ((double)numMax),
Tag = (storeInfo, textBlock),
};

element.ValueChanged += isFloatType ? ModSetting_SliderChangedFloat : ModSetting_SliderChanged;

// Add the slider to the second column
panel.Children.Add(element);
Grid.SetColumn(element, 1);


panel.Children.Add(label);
Grid.SetColumn(label, 2);

panel.Children.Add(new Separator()
{
Margin = new Thickness(0, 2, 0, 0),
VerticalAlignment = VerticalAlignment.Bottom
});
Grid.SetColumnSpan(panel.Children[4], 3);

return panel;
}

private static UIElement ConfigCreateItem(ConfigSchemaProperty elem, ConfigSettings config, CustomPropertyStore storeInfo)
{
switch (elem.Type.ToLower())
Expand All @@ -304,7 +411,7 @@ private static UIElement ConfigCreateItem(ConfigSchemaProperty elem, ConfigSetti
return CreateCheckBox(elem, storeInfo);
case "int":
case "float":
return CreateNumericBox(elem, storeInfo);
return (elem.Slider == true) ? CreateSlider(elem, storeInfo) : CreateNumericBox(elem, storeInfo);
case "string":
return CreateStringBox(elem, storeInfo);
default:
Expand Down Expand Up @@ -448,6 +555,31 @@ private static void ModSetting_stringElementChanged(object sender, RoutedEventAr
settings.SetPropertyValue(info.groupName, info.propertyName, text.Text);
}

private static void ModSetting_SliderChanged(object sender, RoutedEventArgs e)
{
Slider slide = (Slider)sender;

if (slide.Tag is (CustomPropertyStore storeInfo, TextBlock textBlock))
{

string rounded = FormatNumber(Math.Floor(((decimal)slide.Value)), true);
settings.SetPropertyValue(storeInfo.groupName, storeInfo.propertyName, rounded);
textBlock.Text = rounded;
}
}

private static void ModSetting_SliderChangedFloat(object sender, RoutedEventArgs e)
{
Slider slide = (Slider)sender;

if (slide.Tag is (CustomPropertyStore storeInfo, TextBlock textBlock))
{
string floatRes = FormatNumber(((decimal)slide.Value));
settings.SetPropertyValue(storeInfo.groupName, storeInfo.propertyName, floatRes);
textBlock.Text = floatRes;
}
}

#endregion
}
}
3 changes: 2 additions & 1 deletion SA-Mod-Manager/ModsCommon/ModConfig.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ public ConfigSettings(string path)
{
if (!configINI.ContainsKey(group.Name))
configINI.Add(group.Name, new Dictionary<string, string>());

foreach (ConfigSchemaProperty prop in group.Properties)
{
if (!configINI[group.Name].ContainsKey(prop.Name))
Expand All @@ -291,7 +292,7 @@ public ConfigSettings(string path)
if (modINI is not null && prop.Name.ToLower().Contains("includedir"))
{
if (!modINI.ContainsKey("Config"))
modINI.Add("Config", new Dictionary<string, string>());
modINI.Add("Config", []);

var value = prop.DefaultValue;
if (IsEnum(prop.Type) && int.TryParse(value, out int result))
Expand Down
2 changes: 1 addition & 1 deletion SA-Mod-Manager/UI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
mc:Ignorable="d"
Title="SA Mod Manager"
Style="{DynamicResource SAWindow}"
MinHeight="680" MinWidth="590" Width="630" Height="705" WindowStartupLocation="CenterScreen"
MinHeight="660" MinWidth="580" Width="640" Height="705" WindowStartupLocation="CenterScreen"
Loaded="MainWindowManager_Loaded" PreviewKeyDown="Window_PreviewKeyDown"
Closed="MainForm_FormClosing">

Expand Down

0 comments on commit 1f6a689

Please sign in to comment.