Files
LuaCsForBarotraumaEP/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/SettingRangeEntry.cs
MapleWheels 1fe68aa861 - Added float, double settingentry types. Adjusted display formatting.
- Added menu refresh on Apply button.
- Fixed package name resolution for invalid chars.
- Added helper console command to get xml-safe name for use in localization files.
- Made error messages for bad settings xml more descriptive.
- Modified GUISlider.
- Converted HarmonyEventPatchesService to a System.
- Fixed package name resolution for incompatible names in Xml and files, in many places.
- Fixed base textbox implementation for settings.
2026-03-05 15:45:00 -05:00

105 lines
3.8 KiB
C#

using System;
using System.Globalization;
using System.Xml.Linq;
using Barotrauma.LuaCs.Data;
using Microsoft.Toolkit.Diagnostics;
using Microsoft.Xna.Framework;
using OneOf;
namespace Barotrauma.LuaCs.Data;
public abstract class SettingRangeBase<T> : SettingEntry<T>, ISettingRangeBase<T> where T : IEquatable<T>, IConvertible
{
public SettingRangeBase(IConfigInfo configInfo, Func<OneOf<string, XElement, object>, bool> valueChangePredicate) : base(configInfo, valueChangePredicate)
{
}
public T MinValue { get; protected init; }
public T MaxValue { get; protected init; }
public int IncrementalSteps { get; protected init; }
}
public class SettingRangeFloat : SettingRangeBase<float>
{
public class RangeFactory : ISettingBase.IFactory<SettingRangeFloat>
{
public SettingRangeFloat CreateInstance(IConfigInfo configInfo, Func<OneOf<string, XElement, object>, bool> valueChangePredicate)
{
Guard.IsNotNull(configInfo, nameof(configInfo));
return new SettingRangeFloat(configInfo, valueChangePredicate);
}
}
public SettingRangeFloat(IConfigInfo configInfo, Func<OneOf<string, XElement, object>, bool> valueChangePredicate) : base(configInfo, valueChangePredicate)
{
// funny values in case they forget to set them in the config.
MinValue = configInfo.Element.GetAttributeFloat("Min", float.MinValue);
MaxValue = configInfo.Element.GetAttributeFloat("Max", float.MaxValue);
IncrementalSteps = configInfo.Element.GetAttributeInt("Steps", 3);
}
public override bool TrySetValue(float value)
{
if (value > MaxValue || value < MinValue)
{
return false;
}
return base.TrySetValue(value);
}
#if CLIENT
public override void AddDisplayComponent(GUILayoutGroup layoutGroup, Vector2 relativeSize, Action<string> onSerializedValue)
{
GUIUtil.Slider(layoutGroup, new Vector2(MinValue, MaxValue), IncrementalSteps, labelFunc: val =>
{
return val.ToString("G4", CultureInfo.InvariantCulture);
}, Value, setter: val =>
{
onSerializedValue?.Invoke(val.ToString());
}, TextManager.Get(this.GetDisplayInfo().Tooltip), relativeSize);
}
#endif
}
public class SettingRangeInt : SettingRangeBase<int>
{
public class RangeFactory : ISettingBase.IFactory<SettingRangeInt>
{
public SettingRangeInt CreateInstance(IConfigInfo configInfo, Func<OneOf<string, XElement, object>, bool> valueChangePredicate)
{
Guard.IsNotNull(configInfo, nameof(configInfo));
return new SettingRangeInt(configInfo, valueChangePredicate);
}
}
public SettingRangeInt(IConfigInfo configInfo, Func<OneOf<string, XElement, object>, bool> valueChangePredicate) : base(configInfo, valueChangePredicate)
{
// funny values in case they forget to set them in the config.
MinValue = configInfo.Element.GetAttributeInt("Min", int.MinValue);
MaxValue = configInfo.Element.GetAttributeInt("Max", int.MaxValue);
IncrementalSteps = configInfo.Element.GetAttributeInt("Steps", 3);
}
public override bool TrySetValue(int value)
{
if (value > MaxValue || value < MinValue)
{
return false;
}
return base.TrySetValue(value);
}
#if CLIENT
public override void AddDisplayComponent(GUILayoutGroup layoutGroup, Vector2 relativeSize, Action<string> onSerializedValue)
{
GUIUtil.Slider(layoutGroup, new Vector2(MinValue, MaxValue), IncrementalSteps, labelFunc: val =>
{
return ((int)val).ToString();
}, Value, setter: val =>
{
onSerializedValue?.Invoke(((int)val).ToString());
}, TextManager.Get(this.GetDisplayInfo().Tooltip), relativeSize);
}
#endif
}