314 行
6.0 KiB
C#
314 行
6.0 KiB
C#
using System.Collections.Immutable;
|
|
using System.Collections.ObjectModel;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using YukkuriMovieMaker.Plugin;
|
|
using YukkuriMovieMaker.UndoRedo;
|
|
|
|
namespace UndoTree.Tests;
|
|
|
|
public sealed class Part : UndoRedoable
|
|
{
|
|
int value;
|
|
|
|
public Part(string label) => Label = label;
|
|
|
|
public string Label { get; }
|
|
|
|
public int Value
|
|
{
|
|
get => value;
|
|
set => Set(ref this.value, value);
|
|
}
|
|
|
|
public override string ToString() => $"{Label}:{Value}";
|
|
}
|
|
|
|
public sealed class Document : UndoRedoable
|
|
{
|
|
int x;
|
|
int y;
|
|
|
|
public Document() => SubscribeObservableCollectionChangedAndChild(Parts);
|
|
|
|
public int X
|
|
{
|
|
get => x;
|
|
set => Set(ref x, value);
|
|
}
|
|
|
|
public int Y
|
|
{
|
|
get => y;
|
|
set => Set(ref y, value);
|
|
}
|
|
|
|
public ObservableCollection<Part> Parts { get; } = [];
|
|
|
|
public string Snapshot() => $"X={X} Y={Y} Parts=[{string.Join(",", Parts)}]";
|
|
}
|
|
|
|
public sealed class Sheet : UndoRedoable
|
|
{
|
|
ImmutableList<Part> parts = [];
|
|
int x;
|
|
|
|
public int X
|
|
{
|
|
get => x;
|
|
set => Set(ref x, value);
|
|
}
|
|
|
|
public ImmutableList<Part> Parts
|
|
{
|
|
get => parts;
|
|
set => Set(ref parts, value);
|
|
}
|
|
|
|
public void Seed(ImmutableList<Part> seeded) => parts = seeded;
|
|
|
|
public string Snapshot() => $"X={X} Parts=[{string.Join(",", Parts.Select(part => part.Label))}]";
|
|
}
|
|
|
|
public sealed class Slot : UndoRedoable
|
|
{
|
|
int level;
|
|
|
|
public int Level
|
|
{
|
|
get => level;
|
|
set => Set(ref level, value);
|
|
}
|
|
}
|
|
|
|
public sealed class Named : UndoRedoable
|
|
{
|
|
string name = string.Empty;
|
|
|
|
public string Name
|
|
{
|
|
get => name;
|
|
set => Set(ref name, value);
|
|
}
|
|
}
|
|
|
|
public sealed class Speaker : UndoRedoable
|
|
{
|
|
public int Calls { get; private set; }
|
|
|
|
public Task SpeakAsync()
|
|
{
|
|
Calls++;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public async Task SpeakLaterAsync()
|
|
{
|
|
await Task.Delay(50);
|
|
Calls++;
|
|
}
|
|
}
|
|
|
|
public sealed class Holder : UndoRedoable
|
|
{
|
|
int level;
|
|
Slot? slot;
|
|
|
|
public int Level
|
|
{
|
|
get => level;
|
|
set => Set(ref level, value);
|
|
}
|
|
|
|
public Slot? Slot
|
|
{
|
|
get => slot;
|
|
set => Set(ref slot, value);
|
|
}
|
|
}
|
|
|
|
public sealed class Stage : UndoRedoable
|
|
{
|
|
public Stage() => SubscribeObservableCollectionChangedAndChild(Speakers);
|
|
|
|
public ObservableCollection<Speaker> Speakers { get; } = [];
|
|
}
|
|
|
|
public sealed class Poisoned
|
|
{
|
|
public int Level
|
|
{
|
|
get => 0;
|
|
set => throw new InvalidOperationException("cannot be read back");
|
|
}
|
|
}
|
|
|
|
public sealed class Vault : UndoRedoable
|
|
{
|
|
Poisoned? item;
|
|
|
|
public Poisoned? Item
|
|
{
|
|
get => item;
|
|
set => Set(ref item, value);
|
|
}
|
|
}
|
|
|
|
public sealed class Counted : UndoRedoable
|
|
{
|
|
readonly ObservableCollection<Part> parts = [];
|
|
|
|
public Counted() => SubscribeObservableCollectionChangedAndChild(parts);
|
|
|
|
public int Reads { get; private set; }
|
|
|
|
public ObservableCollection<Part> Parts
|
|
{
|
|
get
|
|
{
|
|
Reads++;
|
|
return parts;
|
|
}
|
|
}
|
|
}
|
|
|
|
public sealed class Quiet : UndoRedoable
|
|
{
|
|
public ObservableCollection<Part> Parts { get; } = [];
|
|
}
|
|
|
|
public sealed class Flaky : UndoRedoable
|
|
{
|
|
readonly ObservableCollection<Part> parts = [new Part("a")];
|
|
|
|
public bool Fails { get; set; }
|
|
|
|
public ObservableCollection<Part> Parts => Fails ? throw new InvalidOperationException("unreadable") : parts;
|
|
|
|
public Part First => parts[0];
|
|
}
|
|
|
|
public sealed class Panel : UndoRedoable
|
|
{
|
|
public Panel()
|
|
{
|
|
SubscribeChildUndoRedoable(Gain);
|
|
SubscribeObservableCollectionChangedAndChild(Slots);
|
|
}
|
|
|
|
public string Label => "panel";
|
|
|
|
public Slot Gain { get; } = new();
|
|
|
|
public ObservableCollection<Slot> Slots { get; } = [];
|
|
}
|
|
|
|
public sealed class Rig : UndoRedoable
|
|
{
|
|
public Part Knob { get; } = new("knob");
|
|
|
|
public IEnumerable<Part> Generated => Enumerable.Range(0, 3).Select(index => new Part(index.ToString()));
|
|
}
|
|
|
|
public sealed class Link : UndoRedoable
|
|
{
|
|
Link? next;
|
|
|
|
public Link? Next
|
|
{
|
|
get => next;
|
|
set => Set(ref next, value);
|
|
}
|
|
}
|
|
|
|
public sealed class Group : UndoRedoable
|
|
{
|
|
Document? child;
|
|
|
|
public Document? Child
|
|
{
|
|
get => child;
|
|
set => Set(ref child, value);
|
|
}
|
|
}
|
|
|
|
public sealed class Labelled : UndoRedoable
|
|
{
|
|
int amount;
|
|
int broken;
|
|
|
|
[Display(Name = "Visible name")]
|
|
public int Amount
|
|
{
|
|
get => amount;
|
|
set => Set(ref amount, value);
|
|
}
|
|
|
|
[Display(Name = "Missing", ResourceType = typeof(Texts))]
|
|
public int Broken
|
|
{
|
|
get => broken;
|
|
set => Set(ref broken, value);
|
|
}
|
|
}
|
|
|
|
public enum Shade
|
|
{
|
|
[Display(Name = "Visible shade")]
|
|
Dark,
|
|
Light,
|
|
}
|
|
|
|
[Flags]
|
|
public enum Marks
|
|
{
|
|
[Display(Name = "First mark")]
|
|
First = 1,
|
|
Second = 2,
|
|
}
|
|
|
|
public sealed class VisiblePlugin : IPlugin
|
|
{
|
|
public string Name => "Visible plugin";
|
|
}
|
|
|
|
public sealed class BrokenPlugin : IPlugin
|
|
{
|
|
public BrokenPlugin() => throw new InvalidOperationException("broken");
|
|
|
|
public string Name => "Broken plugin";
|
|
}
|
|
|
|
public abstract class AbstractPlugin : IPlugin
|
|
{
|
|
public string Name => "Abstract plugin";
|
|
}
|
|
|
|
public static class Histories
|
|
{
|
|
public static (UndoRedoManager Manager, Document Document) NewDocument(params string[] partLabels)
|
|
{
|
|
var document = new Document();
|
|
foreach (var label in partLabels)
|
|
document.Parts.Add(new Part(label));
|
|
|
|
var manager = new UndoRedoManager();
|
|
manager.Subscribe(document);
|
|
return (manager, document);
|
|
}
|
|
|
|
public static (UndoRedoManager Manager, Sheet Sheet) NewSheet(params string[] partLabels)
|
|
{
|
|
var sheet = new Sheet();
|
|
sheet.Seed([.. partLabels.Select(label => new Part(label))]);
|
|
var manager = new UndoRedoManager();
|
|
manager.Subscribe(sheet);
|
|
return (manager, sheet);
|
|
}
|
|
|
|
public static void Edit(UndoRedoManager manager, Action change)
|
|
{
|
|
change();
|
|
manager.Record();
|
|
}
|
|
}
|