using System; using System.Collections.Generic; using UnityEngine; namespace NBF.Setting { public interface IMultiOption : IOptionBase { List GetOptionNames(); } public abstract class MultiOption : OptionBase, IMultiOption { protected OptionTable OptionTable = new OptionTable(); protected void AddOption(string name, T value) { OptionTable.Add(name, value); } public List GetOptionNames() => OptionTable.GetNames(); protected void SelectOption(T value, int defaultIndex = 0) { SetValue(TryGetIndex(value, out var index) ? index : defaultIndex); } protected void SelectOption(Predicate predicate, int defaultIndex = 0) { SetValue(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(Value); public override string GetDisplayString() { return OptionTable.GetName(Value); } } }