using System; using System.Collections.Generic; namespace NBF.Setting { public interface IMultiOption : IOptionBase { List GetOptionNames(); int GetIndex(); void SetIndex(int index); string GetName(); } public abstract class MultiOption : OptionBase, IMultiOption { private int _index; protected OptionTable OptionTable = new OptionTable(); /// /// 默认值 /// protected abstract T DefaultValue { get; } protected override void OnLoad() { } protected void AddOption(string name, T value) { OptionTable.Add(name, value); } public List GetOptionNames() => OptionTable.GetNames(); public string GetName() { return OptionTable.GetName(_index); } public int GetIndex() { return _index; } public void SetIndex(int index) { _index = index; } protected void SelectOption(T value, int defaultIndex = 0) { SetIndex(TryGetIndex(value, out var index) ? index : defaultIndex); } protected void SelectOption(Predicate predicate, int defaultIndex = 0) { SetIndex(TryGetIndex(predicate, out var index) ? index : defaultIndex); } protected bool TryGetIndex(T option, out int index) { return TryGetIndex(entry => entry.Equals(option), out index); } protected bool TryGetIndex(Predicate predicate, out int index) { index = -1; var entries = OptionTable.GetValues(); for (var i = 0; i < entries.Count; i++) { if (!predicate(entries[i])) continue; index = i; return true; } return false; } public T GetSelectedOption() => OptionTable.GetValue(_index); } }