diff --git a/MonkeyLoader.Resonite.Integration/ResoniteEventHandlerMonkeys.cs b/MonkeyLoader.Resonite.Integration/ResoniteEventHandlerMonkeys.cs index 2955b68..f55e4f8 100644 --- a/MonkeyLoader.Resonite.Integration/ResoniteEventHandlerMonkeys.cs +++ b/MonkeyLoader.Resonite.Integration/ResoniteEventHandlerMonkeys.cs @@ -39,9 +39,12 @@ void ICancelableEventHandler.Handle(TEvent eventData) /// Determines whether the given event should be handled /// by this event handler based on its data. /// + /// + /// By default: Returns this monkey's Enabled state. + /// /// An object containing all the relevant information for the event. /// true if this event handler applies to the event; otherwise, false. - protected abstract bool AppliesTo(TEvent eventData); + protected virtual bool AppliesTo(TEvent eventData) => Enabled; /// /// Handles the given event based on its data, if AppliesTo returned true. @@ -110,9 +113,12 @@ void IEventHandler.Handle(TEvent eventData) /// Determines whether the given event should be handled /// by this event handler based on its data. /// + /// + /// By default: Returns this monkey's Enabled state. + /// /// An object containing all the relevant information for the event. /// true if this event handler applies to the event; otherwise, false. - protected abstract bool AppliesTo(TEvent eventData); + protected virtual bool AppliesTo(TEvent eventData) => Enabled; /// /// Handles the given event based on its data, if AppliesTo returned true. diff --git a/MonkeyLoader.Resonite.Integration/UI/BuildInspectorEvents.cs b/MonkeyLoader.Resonite.Integration/UI/BuildInspectorEvents.cs index 4d28207..2f58e77 100644 --- a/MonkeyLoader.Resonite.Integration/UI/BuildInspectorEvents.cs +++ b/MonkeyLoader.Resonite.Integration/UI/BuildInspectorEvents.cs @@ -2,6 +2,7 @@ using FrooxEngine; using System; using MonkeyLoader.Resonite.Events; +using MonkeyLoader.Events; namespace MonkeyLoader.Resonite.UI { @@ -27,8 +28,12 @@ internal BuildInspectorBodyEvent(UIBuilder ui, WorkerInspector inspector, Worker } /// - /// Represents the base class for the events fired during construction of a + /// Represents the base class for the events fired during construction of a . /// + /// + /// This base class is dispatched as an event as well. + /// + [DispatchableBaseEvent] public abstract class BuildInspectorEvent : BuildUIEvent { /// diff --git a/MonkeyLoader.Resonite.Integration/UI/DefaultInspectorHeaderConfig.cs b/MonkeyLoader.Resonite.Integration/UI/DefaultInspectorHeaderConfig.cs index a8ded55..6271d16 100644 --- a/MonkeyLoader.Resonite.Integration/UI/DefaultInspectorHeaderConfig.cs +++ b/MonkeyLoader.Resonite.Integration/UI/DefaultInspectorHeaderConfig.cs @@ -1,5 +1,7 @@ -using MonkeyLoader.Configuration; +using FrooxEngine; +using MonkeyLoader.Configuration; using MonkeyLoader.Resonite.Configuration; +using MonkeyLoader.Resonite.UI.Inspectors; using System; namespace MonkeyLoader.Resonite.UI @@ -9,30 +11,35 @@ namespace MonkeyLoader.Resonite.UI /// public sealed class DefaultInspectorHeaderConfig : SingletonConfigSection { - private static readonly DefiningConfigKey _destroyOffset = new("DestroyOffset", "The Order Offset of the Destroy button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 12, valueValidator: ValidateRange) + private readonly DefiningConfigKey _destroyOffset = new("DestroyOffset", "The Order Offset of the Destroy button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 12) { - new ConfigKeyRange(0, 16), - new ConfigKeySessionShare(IntToLong, LongToInt, 12) + OffsetRange, + MakeOffsetRangeShare(12) }; - private static readonly DefiningConfigKey _duplicateOffset = new("DuplicateOffset", "The Order Offset of the Duplicate button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 11, valueValidator: ValidateRange) + private readonly DefiningConfigKey _duplicateOffset = new("DuplicateOffset", "The Order Offset of the Duplicate button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 11) { - new ConfigKeyRange(0, 16), - new ConfigKeySessionShare(IntToLong, LongToInt, 11) + OffsetRange, + MakeOffsetRangeShare(11) }; - private static readonly DefiningConfigKey _openContainerOffset = new("OpenContainerOffset", "The Order Offset of the Open Container button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 4, valueValidator: ValidateRange) + private readonly DefiningConfigKey _openContainerOffset = new("OpenContainerOffset", "The Order Offset of the Open Container button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 4) { - new ConfigKeyRange(0, 16), - new ConfigKeySessionShare(IntToLong, LongToInt, 10) + OffsetRange, + MakeOffsetRangeShare(10) }; - private readonly DefiningConfigKey _workerNameOffset = new("NameOffset", "The Order Offset of the Worker Name button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 6, valueValidator: ValidateRange) + private readonly DefiningConfigKey _workerNameOffset = new("NameOffset", "The Order Offset of the Worker Name button on Inspector Headers. Range: 0-16 - Higher is further right.", () => 6) { - new ConfigKeyRange(0, 16), - new ConfigKeySessionShare(IntToLong, LongToInt, 6) + OffsetRange, + MakeOffsetRangeShare(6) }; + /// + /// Gets the range component used for the offset of header items. + /// + public static ConfigKeyRange OffsetRange { get; } = new ConfigKeyRange(0, 16); + /// public override string Description => "Options for the default inspector header generation."; @@ -62,11 +69,17 @@ public sealed class DefaultInspectorHeaderConfig : SingletonConfigSection public ConfigKeySessionShare WorkerNameOffset => _workerNameOffset.Components.Get>(); + /// + /// Makes a new session share component used for the offset of + /// header items, optionally using the given default. + /// + /// The default value for the shared config item for users that don't have it themselves. + /// The newly created share component with the optional default. + public static ConfigKeySessionShare MakeOffsetRangeShare(int defaultValue = default) + => new(IntToLong, LongToInt, defaultValue); + private static long IntToLong(int value) => value; private static int LongToInt(long value) => (int)value; - - private static bool ValidateRange(int value) - => value is >= 0 and <= 16; } } \ No newline at end of file diff --git a/MonkeyLoader.Resonite.Integration/UI/Inspectors/BuildMemberEditorEvent.cs b/MonkeyLoader.Resonite.Integration/UI/Inspectors/BuildMemberEditorEvent.cs index c53e3a7..8128cd5 100644 --- a/MonkeyLoader.Resonite.Integration/UI/Inspectors/BuildMemberEditorEvent.cs +++ b/MonkeyLoader.Resonite.Integration/UI/Inspectors/BuildMemberEditorEvent.cs @@ -9,6 +9,9 @@ namespace MonkeyLoader.Resonite.UI.Inspectors /// /// Represents the base class for the events fired during construction of a /// + /// + /// This base class is dispatched as an event as well. + /// [DispatchableBaseEvent] public abstract class BuildMemberEditorEvent : CancelableBuildUIEvent { diff --git a/MonkeyLoader.Resonite.Integration/UI/CustomInspectorInjector.cs b/MonkeyLoader.Resonite.Integration/UI/Inspectors/CustomInspectorInjector.cs similarity index 96% rename from MonkeyLoader.Resonite.Integration/UI/CustomInspectorInjector.cs rename to MonkeyLoader.Resonite.Integration/UI/Inspectors/CustomInspectorInjector.cs index 8f2c285..e289e1f 100644 --- a/MonkeyLoader.Resonite.Integration/UI/CustomInspectorInjector.cs +++ b/MonkeyLoader.Resonite.Integration/UI/Inspectors/CustomInspectorInjector.cs @@ -1,17 +1,14 @@ -using Elements.Core; -using FrooxEngine; +using FrooxEngine; using FrooxEngine.UIX; using HarmonyLib; using MonkeyLoader.Events; using MonkeyLoader.Patching; -using MonoMod.Utils; using System; using System.Collections; using System.Collections.Generic; using System.Linq; -using System.Reflection; -namespace MonkeyLoader.Resonite.UI +namespace MonkeyLoader.Resonite.UI.Inspectors { [HarmonyPatchCategory(nameof(CustomInspectorInjector))] [HarmonyPatch(typeof(WorkerInspector), nameof(WorkerInspector.BuildUIForComponent))] diff --git a/MonkeyLoader.Resonite.Integration/UI/DefaultInspectorHeaderHandler.cs b/MonkeyLoader.Resonite.Integration/UI/Inspectors/DefaultInspectorHeaderHandler.cs similarity index 92% rename from MonkeyLoader.Resonite.Integration/UI/DefaultInspectorHeaderHandler.cs rename to MonkeyLoader.Resonite.Integration/UI/Inspectors/DefaultInspectorHeaderHandler.cs index 300ec07..10682c8 100644 --- a/MonkeyLoader.Resonite.Integration/UI/DefaultInspectorHeaderHandler.cs +++ b/MonkeyLoader.Resonite.Integration/UI/Inspectors/DefaultInspectorHeaderHandler.cs @@ -8,17 +8,13 @@ using System.Text; using System.Threading.Tasks; -namespace MonkeyLoader.Resonite.UI +namespace MonkeyLoader.Resonite.UI.Inspectors { internal sealed class DefaultInspectorHeaderHandler : ConfiguredResoniteEventHandlerMonkey { public override int Priority => HarmonyLib.Priority.Normal; - protected override bool AppliesTo(BuildInspectorHeaderEvent eventData) => true; - - protected override IEnumerable GetFeaturePatches() => []; - protected override void Handle(BuildInspectorHeaderEvent eventData) { var ui = eventData.UI; diff --git a/MonkeyLoader.Resonite.Integration/UI/OpenLinkedDynamicVariableSpace.cs b/MonkeyLoader.Resonite.Integration/UI/Inspectors/OpenLinkedDynamicVariableSpace.cs similarity index 81% rename from MonkeyLoader.Resonite.Integration/UI/OpenLinkedDynamicVariableSpace.cs rename to MonkeyLoader.Resonite.Integration/UI/Inspectors/OpenLinkedDynamicVariableSpace.cs index 9ab61a6..5e3e4fa 100644 --- a/MonkeyLoader.Resonite.Integration/UI/OpenLinkedDynamicVariableSpace.cs +++ b/MonkeyLoader.Resonite.Integration/UI/Inspectors/OpenLinkedDynamicVariableSpace.cs @@ -7,22 +7,17 @@ using System.Text; using System.Threading.Tasks; -namespace MonkeyLoader.Resonite.UI +namespace MonkeyLoader.Resonite.UI.Inspectors { internal sealed class OpenLinkedDynamicVariableSpace : ResoniteInspectorMonkey { - public override int Priority => HarmonyLib.Priority.First; - public override bool CanBeDisabled => true; + public override int Priority => HarmonyLib.Priority.First; public OpenLinkedDynamicVariableSpace() : base(typeof(DynamicVariableBase<>)) { } - protected override bool AppliesTo(BuildInspectorHeaderEvent eventData) => Enabled; - - protected override IEnumerable GetFeaturePatches() => []; - protected override void Handle(BuildInspectorHeaderEvent eventData) { if (Traverse.Create(eventData.Worker).Field("handler").Field("_currentSpace").GetValue() is not DynamicVariableSpace space) diff --git a/MonkeyLoader.Resonite.Integration/UI/ResoniteInspectorMonkeys.cs b/MonkeyLoader.Resonite.Integration/UI/ResoniteInspectorMonkeys.cs index fb9a108..00628cb 100644 --- a/MonkeyLoader.Resonite.Integration/UI/ResoniteInspectorMonkeys.cs +++ b/MonkeyLoader.Resonite.Integration/UI/ResoniteInspectorMonkeys.cs @@ -1,5 +1,6 @@ using FrooxEngine; using MonkeyLoader.Events; +using MonkeyLoader.Patching; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -19,7 +20,7 @@ public abstract class ResoniteInspectorMonkey : ResoniteEventHa where TMonkey : ResoniteInspectorMonkey, new() where TEvent : BuildInspectorEvent { - private readonly Dictionary _matchCache = new(); + private readonly Dictionary _matchCache = []; /// /// Gets the generic type definition of the base type. @@ -41,11 +42,15 @@ protected ResoniteInspectorMonkey(Type baseType) } /// - /// Ensures that the worker given in the event derives from the base type. + /// Ensures that this monkey is enabled + /// and that the worker given in the event derives from the base type. /// /// protected override bool AppliesTo(TEvent eventData) { + if (!base.AppliesTo(eventData)) + return false; + var type = eventData.Worker.GetType(); if (!_matchCache.TryGetValue(type, out var matches)) @@ -91,9 +96,11 @@ protected ResoniteInspectorMonkey() { } /// - /// Ensures that the worker given in the event is a . + /// Ensures that this monkey is enabled + /// and that the worker given in the event is a . /// /// - protected override bool AppliesTo(TEvent eventData) => eventData.Worker is TWorker; + protected override bool AppliesTo(TEvent eventData) + => base.AppliesTo(eventData) && eventData.Worker is TWorker; } } \ No newline at end of file diff --git a/MonkeyLoader.Resonite.Integration/UI/Tooltips/ButtonDelegateTooltipResolver.cs b/MonkeyLoader.Resonite.Integration/UI/Tooltips/ButtonDelegateTooltipResolver.cs index 55ca6be..f0c1531 100644 --- a/MonkeyLoader.Resonite.Integration/UI/Tooltips/ButtonDelegateTooltipResolver.cs +++ b/MonkeyLoader.Resonite.Integration/UI/Tooltips/ButtonDelegateTooltipResolver.cs @@ -18,10 +18,6 @@ internal sealed class ButtonDelegateTooltipResolver : ResoniteCancelableEventHan public override int Priority => HarmonyLib.Priority.HigherThanNormal; public override bool SkipCanceled => true; - protected override bool AppliesTo(ResolveTooltipLabelEvent eventData) => Enabled; - - protected override IEnumerable GetFeaturePatches() => []; - protected override void Handle(ResolveTooltipLabelEvent eventData) { ISyncDelegate pressed; diff --git a/MonkeyLoader.Resonite.Integration/UI/Tooltips/CommentTooltipResolver.cs b/MonkeyLoader.Resonite.Integration/UI/Tooltips/CommentTooltipResolver.cs index b628a94..dbce374 100644 --- a/MonkeyLoader.Resonite.Integration/UI/Tooltips/CommentTooltipResolver.cs +++ b/MonkeyLoader.Resonite.Integration/UI/Tooltips/CommentTooltipResolver.cs @@ -50,12 +50,6 @@ public static bool TryGetTooltipLabel(IButton button, [NotNullWhen(true)] out st return label is not null; } - /// - protected override bool AppliesTo(ResolveTooltipLabelEvent eventData) => Enabled; - - /// - protected override IEnumerable GetFeaturePatches() => []; - /// protected override void Handle(ResolveTooltipLabelEvent eventData) {