Initial Code
This commit is contained in:
13
ModRegistry/Settings/ISettingsElement.cs
Normal file
13
ModRegistry/Settings/ISettingsElement.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace VMM.ModRegistry.Settings
|
||||
{
|
||||
public interface ISettingsElement
|
||||
{
|
||||
string Name { get; set; }
|
||||
object GetValue();
|
||||
void SetValue(object value);
|
||||
}
|
||||
}
|
||||
24
ModRegistry/Settings/ModSettings.cs
Normal file
24
ModRegistry/Settings/ModSettings.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
namespace VMM.ModRegistry.Settings
|
||||
{
|
||||
public class ModSettings
|
||||
{
|
||||
private readonly List<ISettingsElement> settings = new();
|
||||
|
||||
public void AddSetting(ISettingsElement newSetting)
|
||||
{
|
||||
settings.Add(newSetting);
|
||||
}
|
||||
|
||||
public T GetSetting<T>(string name) where T : class, ISettingsElement
|
||||
{
|
||||
foreach (var setting in settings)
|
||||
{
|
||||
if(setting.Name == name && setting is T typed)
|
||||
return typed;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public IEnumerable<ISettingsElement> GetAllSettings() => settings;
|
||||
}
|
||||
}
|
||||
31
ModRegistry/Settings/SettingsElement.cs
Normal file
31
ModRegistry/Settings/SettingsElement.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using VMM.ModRegistry.Settings;
|
||||
|
||||
namespace VMM.ModRegistry.Settings
|
||||
{
|
||||
public abstract class SettingsElement<T> : ISettingsElement
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public T Value { get; set; }
|
||||
|
||||
public Action<T> OnChanged { get; set; }
|
||||
|
||||
public object GetValue() => Value;
|
||||
|
||||
public void SetValue(object value)
|
||||
{
|
||||
if (value is T typedValue)
|
||||
{
|
||||
Value = typedValue;
|
||||
OnChanged?.Invoke(typedValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidCastException($"Invalid value type. Expected {typeof(T)}, got {value?.GetType()}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
12
ModRegistry/Settings/Types/SliderSetting.cs
Normal file
12
ModRegistry/Settings/Types/SliderSetting.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace VMM.ModRegistry.Settings.Types
|
||||
{
|
||||
public class SliderSetting : SettingsElement<float>
|
||||
{
|
||||
public float Min { get; set; }
|
||||
public float Max { get; set; }
|
||||
}
|
||||
}
|
||||
10
ModRegistry/Settings/Types/ToggleSetting.cs
Normal file
10
ModRegistry/Settings/Types/ToggleSetting.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace VMM.ModRegistry.Settings.Types
|
||||
{
|
||||
public class ToggleSetting : SettingsElement<bool>
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user