using Chronos.Core.Advice;
using Chronos.Core.Analysis;
using Chronos.Core.Analysis.Deep;
using Chronos.Core.Export;
using Chronos.Core.Motion;
using Chronos.Core.Project;
namespace Chronos.App.Editor;
/// An advisor-decided setting of the editor, whatever its value type.
public interface IAutoSetting
{
/// id.
string Id { get; }
AdvisorSection Section { get; }
bool IsAuto(ProjectSettings project);
/// Drops the manual value so the advice applies.
ProjectSettings Reset(ProjectSettings project);
}
///
/// One member of the project (BRIEF §10.4) and the value the engine uses when the advisor has
/// none (, from the deep analysis when it measured one).
///
public sealed class AutoSetting(
string id,
AdvisorSection section,
Func> get,
Func, ProjectSettings> set,
Func fallback) : IAutoSetting
where T : notnull
{
public string Id { get; } = id;
public AdvisorSection Section { get; } = section;
public Auto Get(ProjectSettings project) => get(project);
public bool IsAuto(ProjectSettings project) => get(project).IsAuto;
/// The advisor's value, else what the engine uses without advice.
public T AutoValue(ProjectSettings project, DeepAnalysisReport? deep) => get(project) is { HasAdvice: true, AutoValue: { } advised } ? advised : fallback(project, deep);
/// The value in force.
public T Effective(ProjectSettings project, DeepAnalysisReport? deep) => get(project).Resolve(fallback(project, deep));
/// Touching the control: the value becomes manual.
public ProjectSettings WithManual(ProjectSettings project, T value) => set(project, get(project).WithManual(value));
public ProjectSettings Reset(ProjectSettings project) => set(project, get(project).Reset());
}
///
/// Which collapsible groups of the editor are open, by group key ("export.encoding", "preset.social"): a group nobody
/// toggled opens as its default says. Until attaches the app's settings the state lives for the run;
/// bound, each toggle is written there and saved.
///
public sealed class GroupStates
{
private readonly Lock _lock = new();
private IDictionary _store = new Dictionary(StringComparer.Ordinal);
private Action? _save;
/// Keeps the state in (the app's settings), calling after each toggle.
public void Bind(IDictionary store, Action? save)
{
ArgumentNullException.ThrowIfNull(store);
lock (_lock)
{
// What was toggled before the settings were attached stays.
foreach (var (key, open) in _store)
{
if (!store.ContainsKey(key)) store[key] = open;
}
(_store, _save) = (store, save);
}
}
/// Whether the group is open; when it was never toggled.
public bool IsExpanded(string key, bool fallback)
{
lock (_lock) return _store.TryGetValue(key, out var open) ? open : fallback;
}
/// Remembers that the group is open or closed.
public void Set(string key, bool expanded)
{
ArgumentNullException.ThrowIfNull(key);
Action? save;
lock (_lock)
{
if (_store.TryGetValue(key, out var open) && open == expanded) return;
_store[key] = expanded;
save = _save;
}
save?.Invoke();
}
}
/// The Auto settings of the editor's sections, in panel order.
public static class EditorSettings
{
public static readonly AutoSetting TimingMode = new(AdvisorParameters.TimingMode, AdvisorSection.Timing,
p => p.Timing.Mode, (p, a) => p with { Timing = p.Timing with { Mode = a } }, (_, _) => Core.Project.TimingMode.Constant);
/// Playback rate; without a value it equals the export frame rate (DECISIONS: export fps vs playback rate).
public static readonly AutoSetting FrameRate = new(AdvisorParameters.FrameRate, AdvisorSection.Timing,
p => p.Timing.FrameRate, (p, a) => p with { Timing = p.Timing with { FrameRate = a } },
(p, _) => FrameRateText.Format(ExportResolver.Resolve(p.Export).FrameRate));
public static readonly AutoSetting Deflicker = new(AdvisorParameters.Deflicker, AdvisorSection.Exposure,
p => p.Exposure.Deflicker, (p, a) => p with { Exposure = p.Exposure with { Deflicker = a } }, (_, _) => false);
public static readonly AutoSetting DeflickerWindow = new(AdvisorParameters.DeflickerWindow, AdvisorSection.Exposure,
p => p.Exposure.DeflickerWindow, (p, a) => p with { Exposure = p.Exposure with { DeflickerWindow = a } },
(_, deep) => deep?.Flicker.RecommendedWindow ?? DeflickerOptions.Default.Window);
public static readonly AutoSetting DeflickerStrength = new(AdvisorParameters.DeflickerStrength, AdvisorSection.Exposure,
p => p.Exposure.DeflickerStrength, (p, a) => p with { Exposure = p.Exposure with { DeflickerStrength = a } }, (_, _) => DeflickerOptions.Default.Strength);
public static readonly AutoSetting Regions = new(AdvisorParameters.Regions, AdvisorSection.Exposure,
p => p.Exposure.Regions, (p, a) => p with { Exposure = p.Exposure with { Regions = a } }, (_, _) => RegionMode.Off);
public static readonly AutoSetting HolyGrail = new(AdvisorParameters.HolyGrail, AdvisorSection.Exposure,
p => p.Exposure.HolyGrail, (p, a) => p with { Exposure = p.Exposure with { HolyGrail = a } }, (_, _) => false);
public static readonly AutoSetting TransitionLength = new(AdvisorParameters.TransitionLength, AdvisorSection.Exposure,
p => p.Exposure.TransitionLength, (p, a) => p with { Exposure = p.Exposure with { TransitionLength = a } },
(_, deep) => deep?.Exposure.TransitionLength ?? new HolyGrailOptions().SingleStepTransitionFrames);
public static readonly AutoSetting WhiteBalance = new(AdvisorParameters.WhiteBalanceSmoothing, AdvisorSection.Exposure,
p => p.Exposure.WhiteBalanceSmoothing, (p, a) => p with { Exposure = p.Exposure with { WhiteBalanceSmoothing = a } }, (_, _) => false);
public static readonly AutoSetting MotionBlur = new(AdvisorParameters.MotionBlur, AdvisorSection.Motion,
p => p.Motion.MotionBlur, (p, a) => p with { Motion = p.Motion with { MotionBlur = a } }, (_, _) => false);
public static readonly AutoSetting ShutterAngle = new(AdvisorParameters.TargetShutterAngle, AdvisorSection.Motion,
p => p.Motion.TargetShutterAngle, (p, a) => p with { Motion = p.Motion with { TargetShutterAngle = a } }, (_, _) => 180.0);
public static readonly AutoSetting FlowQuality = new(AdvisorParameters.FlowQuality, AdvisorSection.Motion,
p => p.Motion.FlowQuality, (p, a) => p with { Motion = p.Motion with { FlowQuality = a } }, (_, _) => FlowQualitySetting.Balanced);
public static readonly AutoSetting Stabilize = new(AdvisorParameters.Stabilize, AdvisorSection.Motion,
p => p.Motion.Stabilize, (p, a) => p with { Motion = p.Motion with { Stabilize = a } }, (_, _) => false);
public static readonly AutoSetting StabilizeSmoothing = new(AdvisorParameters.StabilizeSmoothing, AdvisorSection.Motion,
p => p.Motion.StabilizeSmoothing, (p, a) => p with { Motion = p.Motion with { StabilizeSmoothing = a } }, (_, _) => new StabilizerOptions().SmoothingSigma);
public static readonly AutoSetting Aspect = new(AdvisorParameters.Aspect, AdvisorSection.Camera,
p => p.Camera.Aspect, (p, a) => p with { Camera = p.Camera with { Aspect = a } }, (_, _) => "source");
public static readonly AutoSetting ExportPreset = new(AdvisorParameters.ExportPreset, AdvisorSection.Export,
p => p.Export.Preset, (p, a) => p with { Export = p.Export with { Preset = a } }, (_, _) => ExportResolver.DefaultPresetId);
public static IReadOnlyList All { get; } =
[
TimingMode, FrameRate, Deflicker, DeflickerWindow, DeflickerStrength, Regions, HolyGrail, TransitionLength, WhiteBalance,
MotionBlur, ShutterAngle, FlowQuality, Stabilize, StabilizeSmoothing, Aspect, ExportPreset,
];
/// "Reset": every Auto setting of back to Auto; stacking (no advice) back to off.
public static ProjectSettings ResetSection(ProjectSettings project, AdvisorSection section)
{
ArgumentNullException.ThrowIfNull(project);
foreach (var setting in All)
{
if (setting.Section == section) project = setting.Reset(project);
}
return section == AdvisorSection.Stack ? project with { Stack = new StackSettings() } : project;
}
/// True when every Auto setting of is in Auto.
public static bool IsSectionAuto(ProjectSettings project, AdvisorSection section) =>
All.Where(s => s.Section == section).All(s => s.IsAuto(project));
///
/// The collapse state of the Export step's setting groups and preset groups (owner review 1, R3; DESIGN §4 collapsible
/// group): this user's, kept by the app and not in the project.
///
public static GroupStates Groups { get; } = new();
///
/// Whether applies under the switches in force: the deflicker window, strength and regions
/// with deflicker on, the transition length with holy grail on, the shutter angle with motion blur on, the flow quality
/// with motion blur or interpolated timing, the stabiliser's smoothing with stabilisation on. A setting that does not
/// apply shows n/a in its row (which cannot be taken over by hand then), and the Edit path asks nothing about it.
///
public static bool Applies(IAutoSetting setting, ProjectSettings project, DeepAnalysisReport? deep)
{
ArgumentNullException.ThrowIfNull(setting);
ArgumentNullException.ThrowIfNull(project);
return setting.Id switch
{
AdvisorParameters.DeflickerWindow or AdvisorParameters.DeflickerStrength or AdvisorParameters.Regions => Deflicker.Effective(project, deep),
AdvisorParameters.TransitionLength => HolyGrail.Effective(project, deep),
AdvisorParameters.TargetShutterAngle => MotionBlur.Effective(project, deep),
AdvisorParameters.FlowQuality => MotionBlur.Effective(project, deep) || TimingMode.Effective(project, deep) == Core.Project.TimingMode.Interpolated,
AdvisorParameters.StabilizeSmoothing => Stabilize.Effective(project, deep),
_ => true,
};
}
}