Repository URL to install this package:
Version:
1.3.1 ▾
|
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.SettingsManagement;
namespace Fluctio.FluctioSim.EditorUtils.SettingsManagement.Table
{
public class TableSetting<TLabel, TInfo> : UserSetting<TableValues<TInfo>>
{
public TableDefinition<TLabel, TInfo> TableDefinition { get; }
public TableSetting(Settings settings, string key, TableDefinition<TLabel, TInfo> tableDefinition, SettingsScope scope) : base(settings, key, tableDefinition.GetDefaults(), scope)
{
TableDefinition = tableDefinition;
FixRowsIfDefinitionUpdated();
}
private void FixRowsIfDefinitionUpdated()
{
var savedKeys = value.Dictionary.Keys;
var definitionKeys = TableDefinition.Rows.Keys;
var toRemove = savedKeys.Except(definitionKeys).ToList();
foreach (var rowKey in toRemove)
{
value.Dictionary.Remove(rowKey);
}
var toAdd = definitionKeys.Except(savedKeys).ToList();
foreach (var rowKey in toAdd)
{
value.Dictionary[rowKey] = TableDefinition.Rows[rowKey].DefaultInfo;
}
}
public TInfo this[string rowKey]
{
get => value.GetValueOrDefault(rowKey, TableDefinition);
set => this.value.Dictionary[rowKey] = value;
}
public ILookup<T, TInfo> GetLookup<T>(Func<TableRowDefinition<TLabel, TInfo>, T> keyGetter)
{
return TableDefinition.Rows.ToLookup(
pair => keyGetter(pair.Value),
pair => this[pair.Key]);
}
public Dictionary<T, TInfo> GetDictionary<T>(Func<TableRowDefinition<TLabel, TInfo>, T> keyGetter)
{
return TableDefinition.Rows.ToDictionary(
pair => keyGetter(pair.Value),
pair => this[pair.Key]);
}
}
}