Skip to content

Commit

Permalink
要素を分割したとき、動画の再生位置を調整するようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
yuto-trd committed Sep 12, 2023
1 parent 35b2aec commit b71d030
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Beutl.Operators/Source/SourceVideoOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TimeSpan>(OffsetPosition, OffsetPosition.Value, OffsetPosition.Value + startDelta);
}
else
{
return base.OnSplit(backward, startDelta, lengthDelta);
}
}
}
29 changes: 29 additions & 0 deletions src/Beutl.ProjectSystem/ChangeSetterValueCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Beutl.Styling;

namespace Beutl;

public sealed class ChangeSetterValueCommand<T> : IRecordableCommand
{
private readonly Setter<T> _setter;
private readonly T _oldValue;
private readonly T _newValue;

public ChangeSetterValueCommand(Setter<T> 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;
}
}
8 changes: 8 additions & 0 deletions src/Beutl.ProjectSystem/Operation/SourceOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public SourceOperation()
[NotAutoSerialized]
public ICoreList<SourceOperator> 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);
Expand Down
16 changes: 16 additions & 0 deletions src/Beutl.ProjectSystem/Operation/SourceOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/Beutl/ViewModels/ElementViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit b71d030

Please sign in to comment.