|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.ObjectModel; |
| 4 | +using HitTheKit.Core; |
| 5 | +using HitTheKit.Unity.Input; |
| 6 | + |
| 7 | +namespace HitTheKit.Unity.Gameplay |
| 8 | +{ |
| 9 | + public sealed class EditableChartNote |
| 10 | + { |
| 11 | + internal EditableChartNote( |
| 12 | + int identity, |
| 13 | + DrumPad pad, |
| 14 | + int velocity, |
| 15 | + double timeSeconds, |
| 16 | + DrumInputSource source) |
| 17 | + { |
| 18 | + Identity = identity; |
| 19 | + Pad = pad; |
| 20 | + Velocity = velocity; |
| 21 | + TimeSeconds = timeSeconds; |
| 22 | + Source = source; |
| 23 | + } |
| 24 | + |
| 25 | + public int Identity { get; } |
| 26 | + public DrumPad Pad { get; internal set; } |
| 27 | + public int Velocity { get; } |
| 28 | + public double TimeSeconds { get; internal set; } |
| 29 | + public DrumInputSource Source { get; } |
| 30 | + } |
| 31 | + |
| 32 | + public sealed class ChartDraftEditor |
| 33 | + { |
| 34 | + private readonly double durationSeconds; |
| 35 | + private readonly List<EditableChartNote> notes; |
| 36 | + private readonly ReadOnlyCollection<EditableChartNote> readOnlyNotes; |
| 37 | + private int nextIdentity; |
| 38 | + |
| 39 | + public ChartDraftEditor(ChartRecordingDraft draft) |
| 40 | + { |
| 41 | + if (draft == null) throw new ArgumentNullException(nameof(draft)); |
| 42 | + durationSeconds = draft.DurationSeconds; |
| 43 | + notes = new List<EditableChartNote>(draft.Hits.Count); |
| 44 | + for (int index = 0; index < draft.Hits.Count; index++) |
| 45 | + { |
| 46 | + RecordedChartHit hit = draft.Hits[index]; |
| 47 | + notes.Add(new EditableChartNote(nextIdentity++, hit.Pad, hit.Velocity, hit.TimeSeconds, hit.Source)); |
| 48 | + } |
| 49 | + Sort(); |
| 50 | + readOnlyNotes = notes.AsReadOnly(); |
| 51 | + } |
| 52 | + |
| 53 | + public double DurationSeconds => durationSeconds; |
| 54 | + public IReadOnlyList<EditableChartNote> Notes => readOnlyNotes; |
| 55 | + |
| 56 | + public int Update(int index, double timeSeconds, DrumPad pad) |
| 57 | + { |
| 58 | + EditableChartNote note = At(index); |
| 59 | + ValidateTime(timeSeconds); |
| 60 | + ValidatePad(pad); |
| 61 | + note.TimeSeconds = timeSeconds; |
| 62 | + note.Pad = pad; |
| 63 | + Sort(); |
| 64 | + return notes.IndexOf(note); |
| 65 | + } |
| 66 | + |
| 67 | + public int Add(double timeSeconds, DrumPad pad, int velocity = 100) |
| 68 | + { |
| 69 | + ValidateTime(timeSeconds); |
| 70 | + ValidatePad(pad); |
| 71 | + if (velocity < 0 || velocity > 127) throw new ArgumentOutOfRangeException(nameof(velocity)); |
| 72 | + if (notes.Count >= ChartRecordingSession.MaximumHits) |
| 73 | + throw new InvalidOperationException($"A chart cannot exceed {ChartRecordingSession.MaximumHits} notes."); |
| 74 | + var note = new EditableChartNote(nextIdentity++, pad, velocity, timeSeconds, DrumInputSource.Test); |
| 75 | + notes.Add(note); |
| 76 | + Sort(); |
| 77 | + return notes.IndexOf(note); |
| 78 | + } |
| 79 | + |
| 80 | + public void Delete(int index) => notes.RemoveAt(ValidatedIndex(index)); |
| 81 | + |
| 82 | + public ChartRecordingDraft BuildDraft() |
| 83 | + { |
| 84 | + var hits = new RecordedChartHit[notes.Count]; |
| 85 | + for (int index = 0; index < notes.Count; index++) |
| 86 | + { |
| 87 | + EditableChartNote note = notes[index]; |
| 88 | + hits[index] = new RecordedChartHit( |
| 89 | + new DrumInputEvent(note.Pad, note.Velocity, note.TimeSeconds, note.Source), |
| 90 | + index); |
| 91 | + } |
| 92 | + return new ChartRecordingDraft(durationSeconds, hits); |
| 93 | + } |
| 94 | + |
| 95 | + private EditableChartNote At(int index) => notes[ValidatedIndex(index)]; |
| 96 | + |
| 97 | + private int ValidatedIndex(int index) |
| 98 | + { |
| 99 | + if (index < 0 || index >= notes.Count) throw new ArgumentOutOfRangeException(nameof(index)); |
| 100 | + return index; |
| 101 | + } |
| 102 | + |
| 103 | + private void ValidateTime(double value) |
| 104 | + { |
| 105 | + if (double.IsNaN(value) || double.IsInfinity(value) || value < 0 || value > durationSeconds) |
| 106 | + throw new ArgumentOutOfRangeException(nameof(value), $"Note time must be between 0 and {durationSeconds:0.###} seconds."); |
| 107 | + } |
| 108 | + |
| 109 | + private static void ValidatePad(DrumPad pad) |
| 110 | + { |
| 111 | + if (!Enum.IsDefined(typeof(DrumPad), pad)) throw new ArgumentOutOfRangeException(nameof(pad)); |
| 112 | + } |
| 113 | + |
| 114 | + private void Sort() => notes.Sort((left, right) => |
| 115 | + { |
| 116 | + int byTime = left.TimeSeconds.CompareTo(right.TimeSeconds); |
| 117 | + return byTime != 0 ? byTime : left.Identity.CompareTo(right.Identity); |
| 118 | + }); |
| 119 | + } |
| 120 | +} |
0 commit comments