From b71d030433d8065c2b26b5768913f989f274f6ef Mon Sep 17 00:00:00 2001 From: indigo-san Date: Tue, 12 Sep 2023 21:06:35 +0900 Subject: [PATCH] =?UTF-8?q?=E8=A6=81=E7=B4=A0=E3=82=92=E5=88=86=E5=89=B2?= =?UTF-8?q?=E3=81=97=E3=81=9F=E3=81=A8=E3=81=8D=E3=80=81=E5=8B=95=E7=94=BB?= =?UTF-8?q?=E3=81=AE=E5=86=8D=E7=94=9F=E4=BD=8D=E7=BD=AE=E3=82=92=E8=AA=BF?= =?UTF-8?q?=E6=95=B4=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/SourceVideoOperator.cs | 31 +++++++++++++++++++ .../ChangeSetterValueCommand.cs | 29 +++++++++++++++++ .../Operation/SourceOperation.cs | 8 +++++ .../Operation/SourceOperator.cs | 16 ++++++++++ src/Beutl/ViewModels/ElementViewModel.cs | 4 +++ 5 files changed, 88 insertions(+) create mode 100644 src/Beutl.ProjectSystem/ChangeSetterValueCommand.cs diff --git a/src/Beutl.Operators/Source/SourceVideoOperator.cs b/src/Beutl.Operators/Source/SourceVideoOperator.cs index fe6a0b972..ddd83af34 100644 --- a/src/Beutl.Operators/Source/SourceVideoOperator.cs +++ b/src/Beutl.Operators/Source/SourceVideoOperator.cs @@ -54,4 +54,35 @@ protected override void OnAttachedToHierarchy(in HierarchyAttachmentEventArgs ar setter.Value = videoSource; } } + + public override bool HasOriginalLength() + { + return Source.Value?.IsDisposed == false; + } + + public override bool TryGetOriginalLength(out TimeSpan timeSpan) + { + if (Source.Value?.IsDisposed == false) + { + timeSpan = Source.Value.Duration - OffsetPosition.Value; + return true; + } + else + { + timeSpan = TimeSpan.Zero; + return false; + } + } + + public override IRecordableCommand? OnSplit(bool backward, TimeSpan startDelta, TimeSpan lengthDelta) + { + if (backward) + { + return new ChangeSetterValueCommand(OffsetPosition, OffsetPosition.Value, OffsetPosition.Value + startDelta); + } + else + { + return base.OnSplit(backward, startDelta, lengthDelta); + } + } } diff --git a/src/Beutl.ProjectSystem/ChangeSetterValueCommand.cs b/src/Beutl.ProjectSystem/ChangeSetterValueCommand.cs new file mode 100644 index 000000000..23ae3d0f8 --- /dev/null +++ b/src/Beutl.ProjectSystem/ChangeSetterValueCommand.cs @@ -0,0 +1,29 @@ +using Beutl.Styling; + +namespace Beutl; + +public sealed class ChangeSetterValueCommand : IRecordableCommand +{ + private readonly Setter _setter; + private readonly T _oldValue; + private readonly T _newValue; + + public ChangeSetterValueCommand(Setter setter, T oldValue, T newValue) + { + _setter = setter; + _oldValue = oldValue; + _newValue = newValue; + } + + public void Do() + { + _setter.Value = _newValue; + } + + public void Redo() => Do(); + + public void Undo() + { + _setter.Value = _oldValue; + } +} diff --git a/src/Beutl.ProjectSystem/Operation/SourceOperation.cs b/src/Beutl.ProjectSystem/Operation/SourceOperation.cs index 384a7de10..b2789544b 100644 --- a/src/Beutl.ProjectSystem/Operation/SourceOperation.cs +++ b/src/Beutl.ProjectSystem/Operation/SourceOperation.cs @@ -40,6 +40,14 @@ public SourceOperation() [NotAutoSerialized] public ICoreList Children => _children; + public IRecordableCommand OnSplit(bool backward, TimeSpan startDelta,TimeSpan lengthDelta) + { + return _children.Select(v => v.OnSplit(backward, startDelta, lengthDelta)) + .Where(v => v != null) + .ToArray()! + .ToCommand(); + } + public override void ReadFromJson(JsonObject json) { base.ReadFromJson(json); diff --git a/src/Beutl.ProjectSystem/Operation/SourceOperator.cs b/src/Beutl.ProjectSystem/Operation/SourceOperator.cs index 2a42a6ae5..b99029622 100644 --- a/src/Beutl.ProjectSystem/Operation/SourceOperator.cs +++ b/src/Beutl.ProjectSystem/Operation/SourceOperator.cs @@ -86,6 +86,22 @@ public virtual void Exit() { } + public virtual bool HasOriginalLength() + { + return false; + } + + public virtual bool TryGetOriginalLength(out TimeSpan timeSpan) + { + timeSpan = default; + return false; + } + + public virtual IRecordableCommand? OnSplit(bool backward, TimeSpan startDelta, TimeSpan lengthDelta) + { + return null; + } + protected void RaiseInvalidated(RenderInvalidatedEventArgs args) { Invalidated?.Invoke(this, args); diff --git a/src/Beutl/ViewModels/ElementViewModel.cs b/src/Beutl/ViewModels/ElementViewModel.cs index c6388534c..aac63ba27 100644 --- a/src/Beutl/ViewModels/ElementViewModel.cs +++ b/src/Beutl/ViewModels/ElementViewModel.cs @@ -359,8 +359,12 @@ private void OnSplit(TimeSpan timeSpan) backwardLayer.Save(RandomFileNameGenerator.Generate(Path.GetDirectoryName(Scene.FileName)!, Constants.ElementFileExtension)); IRecordableCommand command2 = Scene.AddChild(backwardLayer); + IRecordableCommand command3 = backwardLayer.Operation.OnSplit(true, forwardLength, -forwardLength); + IRecordableCommand command4 = Model.Operation.OnSplit(false, TimeSpan.Zero, -backwardLength); command1.Append(command2) + .Append(command3) + .Append(command4) .DoAndRecord(CommandRecorder.Default); }