diff --git a/MonkeyLoader.Resonite.Integration/DataFeeds/Settings/ConfigSectionSettingsItems.cs b/MonkeyLoader.Resonite.Integration/DataFeeds/Settings/ConfigSectionSettingsItems.cs index 3468cbf..c9523ab 100644 --- a/MonkeyLoader.Resonite.Integration/DataFeeds/Settings/ConfigSectionSettingsItems.cs +++ b/MonkeyLoader.Resonite.Integration/DataFeeds/Settings/ConfigSectionSettingsItems.cs @@ -84,12 +84,12 @@ private static async IAsyncEnumerable EnumerateConfigAsync(Enumera } } - private static async IAsyncEnumerable GenerateEnumItemsAsync(IReadOnlyList path, IReadOnlyList groupKeys, IDefiningConfigKey configKey) + private static async IAsyncEnumerable GenerateEnumItemsAsync(IReadOnlyList path, IReadOnlyList groupKeys, IDefiningConfigKey configKey) where T : Enum { await Task.CompletedTask; - if (configKey.ValueType.GetCustomAttribute() is null) + if (typeof(T).GetCustomAttribute() is null) { var enumField = new DataFeedEnum(); enumField.InitBase(path, groupKeys, configKey); @@ -99,11 +99,11 @@ private static async IAsyncEnumerable GenerateEnumItemsAsync(IR yield break; } - var items = (IEnumerable)_generateFlagsEnumItems + var items = (IAsyncEnumerable)_generateFlagsEnumItems .MakeGenericMethod(typeof(T)) .Invoke(null, [path, groupKeys, configKey]); - foreach (var item in items) + await foreach (var item in items) { if (item is DataFeedGroup) { @@ -152,11 +152,11 @@ private static IAsyncEnumerable GenerateItemsForConfigKey(Enume if (configKey.ValueType.IsEnum) { - var flagsEnumItems = (IAsyncEnumerable)_generateEnumItemsAsync + var enumItems = (IAsyncEnumerable)_generateEnumItemsAsync .MakeGenericMethod(configKey.ValueType) .Invoke(null, [path, groupKeys, configKey]); - return flagsEnumItems; + return enumItems; } if (configKey.ValueType.IsNullable()) @@ -164,8 +164,6 @@ private static IAsyncEnumerable GenerateItemsForConfigKey(Enume var nullableType = configKey.ValueType.GetGenericArguments()[0]; if (nullableType.IsEnum) { - //return GenerateNullableEnumItemsAsync(path, groupKeys, configKey, nullableType); - var nullableEnumItems = (IAsyncEnumerable)_generateNullableEnumItemsAsync .MakeGenericMethod(nullableType) .Invoke(null, [path, groupKeys, configKey]); @@ -211,44 +209,12 @@ private static async IAsyncEnumerable GenerateNullableEnumItemsAsy nullableToggle.InitSetupValue(field => field.SyncWithNullableConfigKeyHasValue(configKey)); yield return nullableToggle; - if (typeof(T).GetCustomAttribute() is null) - { - var enumField = new DataFeedEnum(); - enumField.InitBase(configKey.FullId, path, nullableGroupKeys, configKey.GetLocaleString("Name"), configKey.GetLocaleString("Description")); - enumField.InitSetupValue(field => - { - var slot = field.FindNearestParent(); - - if (slot.GetComponentInParents() is FeedItemInterface feedItemInterface) - { - // Adding the config key's full id to make it easier to create standalone facets - feedItemInterface.Slot.AttachComponent().Text.Value = configKey.FullId; - } - - field.SyncWithNullableConfigKeyValue(configKey, SettingsHelpers.ConfigKeyChangeLabel); - }); - yield return enumField; - } - else - { - var items = (IEnumerable)_generateFlagsEnumItems + var enumItems = (IAsyncEnumerable)_generateEnumItemsAsync .MakeGenericMethod(typeof(T)) - .Invoke(null, [path, groupKeys, configKey]); + .Invoke(null, [path, nullableGroupKeys, configKey]); - foreach (var item in items) - { - if (item is DataFeedGroup) - { - item.ItemKey = configKey.FullId + ".Flags"; - item.GroupingParameters = nullableGroupKeys; - } - else if (item is DataFeedToggle) - { - item.GroupingParameters = nullableGroupKeys.Concat([configKey.FullId + ".Flags"]).ToArray(); - } - yield return item; - } - } + await foreach (var item in enumItems) + yield return item; } private static async IAsyncEnumerable GenerateFlagsEnumFields(IReadOnlyList path, IReadOnlyList groupKeys, IDefiningConfigKey configKey) @@ -261,7 +227,6 @@ private static async IAsyncEnumerable GenerateFlagsEnumFields(I flagsEnumGroup.InitDescription(configKey.GetLocaleKey("Description").AsLocaleKey()); yield return flagsEnumGroup; - //var flagsGrouping = new[] { configKey.Section.Id, configKey.FullId }; var flagsGrouping = groupKeys.Concat(configKey.FullId).ToArray(); var enumType = typeof(T); diff --git a/MonkeyLoader.Resonite.Integration/DataFeeds/Settings/SettingsHelpers.cs b/MonkeyLoader.Resonite.Integration/DataFeeds/Settings/SettingsHelpers.cs index 0b80ba3..4e2725a 100644 --- a/MonkeyLoader.Resonite.Integration/DataFeeds/Settings/SettingsHelpers.cs +++ b/MonkeyLoader.Resonite.Integration/DataFeeds/Settings/SettingsHelpers.cs @@ -126,7 +126,7 @@ public static bool IsInjectableEditorType(this Type type) /// The type of the field and config item's value. /// The field to synchronize with the . /// The config key to synchronize with the . - public static void SetupConfigKeyField(this IField field, IDefiningConfigKey configKey) + public static void SetupConfigKeyField(this IField field, IDefiningConfigKey configKey) { var slot = field.FindNearestParent(); diff --git a/MonkeyLoader.Resonite.Integration/FieldExtensions.cs b/MonkeyLoader.Resonite.Integration/FieldExtensions.cs index 48ccae7..2b81654 100644 --- a/MonkeyLoader.Resonite.Integration/FieldExtensions.cs +++ b/MonkeyLoader.Resonite.Integration/FieldExtensions.cs @@ -73,9 +73,9 @@ public static string GetReferenceLabel(this IWorldElement? target) /// Whether changes of the should be written back to the config key. /// The delegate subscribed to the 's Changed event. public static Action SyncWithConfigKey(this IField field, - IDefiningConfigKey configKey, string? eventLabel = null, bool allowWriteBack = true) + IDefiningConfigKey configKey, string? eventLabel = null, bool allowWriteBack = true) { - field.Value = configKey.GetValue()!; + field.Value = (T)(configKey.GetValue() ?? default(T)!); eventLabel ??= field.GetWriteBackEventLabel(); var parent = field.FindNearestParent(); @@ -90,14 +90,18 @@ void ParentDestroyedHandler(IDestroyable _) void FieldChangedHandler(IChangeable _) { - if (Equals(field.Value, configKey.GetValue()) && (!allowWriteBack || !configKey.TrySetValue(field.Value, eventLabel))) - field.World.RunSynchronously(() => field.Value = configKey.GetValue()!); + if (!Equals(field.Value, configKey.GetValue()) && + !(configKey.ValueType.IsNullable() && configKey.ValueType.GetGenericArguments()[0].IsEnum && configKey.GetValue() is null && Equals(field.Value, default(T))) && + (!allowWriteBack || !configKey.TrySetValue(field.Value, eventLabel))) + { + field.World.RunSynchronously(() => field.Value = (T)(configKey.GetValue() ?? default(T)!)); + } } - void ConfigKeyChangedHandler(object sender, ConfigKeyChangedEventArgs args) + void ConfigKeyChangedHandler(object sender, IConfigKeyChangedEventArgs args) { if (!Equals(field.Value, configKey.GetValue())) - field.World.RunSynchronously(() => field.Value = configKey.GetValue()!); + field.World.RunSynchronously(() => field.Value = (T)(configKey.GetValue() ?? default(T)!)); } field.Changed += FieldChangedHandler; @@ -162,46 +166,46 @@ void ConfigKeyChangedHandler(object sender, ConfigKeyChangedEventArgs args) /// the is set to default(). /// /// - public static Action SyncWithNullableConfigKeyValue(this IField field, - IDefiningConfigKey configKey, string? eventLabel = null, bool allowWriteBack = true) - where T : unmanaged - { - // support for nullables requires this to never be null - field.Value = configKey.GetValue() ?? default; - eventLabel ??= field.GetWriteBackEventLabel(); - - var parent = field.FindNearestParent(); - - void ParentDestroyedHandler(IDestroyable _) - { - parent.Destroyed -= ParentDestroyedHandler; - - field.Changed -= FieldChangedHandler; - configKey.Changed -= ConfigKeyChangedHandler; - } - - void FieldChangedHandler(IChangeable _) - { - var keyValue = configKey.GetValue() ?? default; - - if (!Equals(field.Value, keyValue) && (!allowWriteBack || !configKey.TrySetValue(field.Value, eventLabel))) - field.World.RunSynchronously(() => field.Value = keyValue); - } - - void ConfigKeyChangedHandler(object sender, ConfigKeyChangedEventArgs args) - { - var keyValue = configKey.GetValue() ?? default; - - if (!Equals(field.Value, keyValue)) - field.World.RunSynchronously(() => field.Value = keyValue); - } - - field.Changed += FieldChangedHandler; - configKey.Changed += ConfigKeyChangedHandler; - parent.Destroyed += ParentDestroyedHandler; - - return FieldChangedHandler; - } + //public static Action SyncWithNullableConfigKeyValue(this IField field, + // IDefiningConfigKey configKey, string? eventLabel = null, bool allowWriteBack = true) + // where T : unmanaged + //{ + // // support for nullables requires this to never be null + // field.Value = configKey.GetValue() ?? default; + // eventLabel ??= field.GetWriteBackEventLabel(); + + // var parent = field.FindNearestParent(); + + // void ParentDestroyedHandler(IDestroyable _) + // { + // parent.Destroyed -= ParentDestroyedHandler; + + // field.Changed -= FieldChangedHandler; + // configKey.Changed -= ConfigKeyChangedHandler; + // } + + // void FieldChangedHandler(IChangeable _) + // { + // var keyValue = configKey.GetValue() ?? default; + + // if (!Equals(field.Value, keyValue) && (!allowWriteBack || !configKey.TrySetValue(field.Value, eventLabel))) + // field.World.RunSynchronously(() => field.Value = keyValue); + // } + + // void ConfigKeyChangedHandler(object sender, ConfigKeyChangedEventArgs args) + // { + // var keyValue = configKey.GetValue() ?? default; + + // if (!Equals(field.Value, keyValue)) + // field.World.RunSynchronously(() => field.Value = keyValue); + // } + + // field.Changed += FieldChangedHandler; + // configKey.Changed += ConfigKeyChangedHandler; + // parent.Destroyed += ParentDestroyedHandler; + + // return FieldChangedHandler; + //} private static string GetWriteBackEventLabel(this IField field) => $"SyncedField.WriteBack.{field.World.GetIdentifier()}"; diff --git a/MonkeyLoader.Resonite.Integration/UI/Tooltips/TooltipConfig.cs b/MonkeyLoader.Resonite.Integration/UI/Tooltips/TooltipConfig.cs index 55aae13..e8eaa14 100644 --- a/MonkeyLoader.Resonite.Integration/UI/Tooltips/TooltipConfig.cs +++ b/MonkeyLoader.Resonite.Integration/UI/Tooltips/TooltipConfig.cs @@ -29,6 +29,11 @@ public sealed class TooltipConfig : SingletonConfigSection new ConfigKeyRange(.5f, 4) }; + private static readonly DefiningConfigKey _testKey0 = new("testKey0", "Test key0.", () => ShadowType.Hard); + private static readonly DefiningConfigKey _testKey = new("testKey", "Test key.", () => ShadowType.Soft); + private static readonly DefiningConfigKey _testKey3 = new("testKey3", "Test key3.", () => MappingTarget.NONE); + private static readonly DefiningConfigKey _testKey2 = new("testKey2", "Test key2.", () => null); + /// /// Gets the background color for tooltips. ///