Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

タイムラインでファイルをドロップしたときの動作を実装 #716

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/Beutl/Helpers/ColorGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

using Avalonia.Media;

namespace Beutl.Helpers;

public static class ColorGenerator
{
// https://qiita.com/pira/items/dd4057ef499154968f69
public static Media.Color GenerateColor(string str)
{
try
{
byte[] utf8 = Encoding.UTF8.GetBytes(str);
byte[] hash = MD5.HashData(utf8);
Span<byte> hashSpan = hash.AsSpan(hash.Length - 7);

int pos = hash.Length - 7;
ReadOnlySpan<char> hashStr = Convert.ToHexString(hash.AsSpan().Slice(pos)).AsSpan();

ReadOnlySpan<char> hueSpan = hashStr.Slice(0, 3);
ReadOnlySpan<char> satSpan = hashStr.Slice(3, 2);
ReadOnlySpan<char> valSpan = hashStr.Slice(5, 2);

int hue = int.Parse(hueSpan, NumberStyles.HexNumber);
int sat = int.Parse(satSpan, NumberStyles.HexNumber);
int lit = int.Parse(valSpan, NumberStyles.HexNumber);

var hsl = new HslColor(
1,
hue / 4095d * 360d,
(65 - (sat / 255d * 20d)) / 100d,
(75 - (lit / 255d * 20d)) / 100d);

return hsl.ToRgb().ToMedia();
}
catch
{
return Media.Colors.Teal;
}
}
}
3 changes: 2 additions & 1 deletion src/Beutl/Models/ElementDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public record struct ElementDescription(
TimeSpan Length,
int Layer,
string Name = "",
Type? InitialOperator = null);
Type? InitialOperator = null,
string? FileName = null);
177 changes: 157 additions & 20 deletions src/Beutl/ViewModels/TimelineViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
using Avalonia.Input.Platform;

using Beutl.Animation;
using Beutl.Helpers;
using Beutl.Media.Decoding;
using Beutl.Media.Source;
using Beutl.Models;
using Beutl.Operation;
using Beutl.Operators.Configure;
using Beutl.Operators.Source;
using Beutl.ProjectSystem;
using Beutl.Reactive;
using Beutl.Services;
Expand Down Expand Up @@ -75,26 +79,7 @@ public TimelineViewModel(EditViewModel editViewModel)
.Subscribe(v => Scene.CacheOptions = Scene.CacheOptions with { IsEnabled = v })
.AddTo(_disposables);

AddLayer.Subscribe(item =>
{
var sLayer = new Element()
{
Start = item.Start,
Length = item.Length,
ZIndex = item.Layer,
FileName = RandomFileNameGenerator.Generate(Path.GetDirectoryName(Scene.FileName)!, Constants.ElementFileExtension)
};

if (item.InitialOperator != null)
{
//Todo: レイヤーのアクセントカラー
//sLayer.AccentColor = item.InitialOperator.AccentColor;
sLayer.Operation.AddChild((SourceOperator)Activator.CreateInstance(item.InitialOperator)!).Do();
}

sLayer.Save(sLayer.FileName);
Scene.AddChild(sLayer).DoAndRecord(CommandRecorder.Default);
}).AddTo(_disposables);
AddLayer.Subscribe(OnAddElement).AddTo(_disposables);

TimelineOptions options = editViewModel.Options.Value;
LayerHeaders.AddRange(Enumerable.Range(0, options.MaxLayerCount).Select(num => new LayerHeaderViewModel(num, this)));
Expand Down Expand Up @@ -564,6 +549,158 @@ internal void RaiseLayerHeightChanged(LayerHeaderViewModel value)
return Layers.FirstOrDefault(x => x.Model == element);
}

private void OnAddElement(ElementDescription desc)
{
Element CreateElement()
{
return new Element()
{
Start = desc.Start,
Length = desc.Length,
ZIndex = desc.Layer,
FileName = RandomFileNameGenerator.Generate(Path.GetDirectoryName(Scene.FileName)!, Constants.ElementFileExtension)
};
}

void SetAccentColor(Element element, string str)
{
element.AccentColor = ColorGenerator.GenerateColor(str);
}

if (desc.FileName != null)
{
Element CreateElementFor<T>(out T t)
where T : SourceOperator, new()
{
Element element = CreateElement();
element.Name = Path.GetFileName(desc.FileName!);
SetAccentColor(element, typeof(T).FullName!);

element.Operation.AddChild(t = new T()).Do();

return element;
}

var list = new List<IRecordableCommand>();
if (MatchFileImage(desc.FileName))
{
Element element = CreateElementFor(out SourceImageOperator t);
MediaSourceManager.Shared.OpenImageSource(desc.FileName, out IImageSource? image);
t.Source.Value = image;

element.Save(element.FileName);
list.Add(Scene.AddChild(element));
}
else if (MatchFileVideoOnly(desc.FileName))
{
Element element1 = CreateElementFor(out SourceVideoOperator t1);
Element element2 = CreateElementFor(out SourceSoundOperator t2);
element2.ZIndex++;
MediaSourceManager.Shared.OpenVideoSource(desc.FileName, out IVideoSource? video);
MediaSourceManager.Shared.OpenSoundSource(desc.FileName, out ISoundSource? sound);
t1.Source.Value = video;
t2.Source.Value = sound;

if (video != null)
element1.Length = video.Duration;
if (sound != null)
element2.Length = sound.Duration;

element1.Save(element1.FileName);
element2.Save(element2.FileName);
list.Add(Scene.AddChild(element1));
list.Add(Scene.AddChild(element2));
}
else if (MatchFileAudioOnly(desc.FileName))
{
Element element = CreateElementFor(out SourceSoundOperator t);
MediaSourceManager.Shared.OpenSoundSource(desc.FileName, out ISoundSource? sound);
t.Source.Value = sound;
if (sound != null)
{
element.Length = sound.Duration;
}

element.Save(element.FileName);
list.Add(Scene.AddChild(element));
}

list.ToArray()
.ToCommand()
.DoAndRecord(CommandRecorder.Default);
}
else
{
Element element = CreateElement();
if (desc.InitialOperator != null)
{
LibraryItem? item = LibraryService.Current.FindItem(desc.InitialOperator);
if (item != null)
{
element.Name = item.DisplayName;
}

//Todo: レイヤーのアクセントカラー
//sLayer.AccentColor = item.InitialOperator.AccentColor;
element.AccentColor = ColorGenerator.GenerateColor(desc.InitialOperator.FullName ?? desc.InitialOperator.Name);
element.Operation.AddChild((SourceOperator)Activator.CreateInstance(desc.InitialOperator)!).Do();
}

element.Save(element.FileName);
Scene.AddChild(element).DoAndRecord(CommandRecorder.Default);
}
}

private static bool MatchFileExtensions(string filePath, IEnumerable<string> extensions)
{
return extensions
.Select(x =>
{
int idx = x.LastIndexOf('.');
if (0 <= idx)
return x.Substring(idx);
else
return x;
})
.Any(filePath.EndsWith);
}

private static bool MatchFileAudioOnly(string filePath)
{
return MatchFileExtensions(filePath, DecoderRegistry.EnumerateDecoder()
.SelectMany(x => x.AudioExtensions())
.Distinct());
}

private static bool MatchFileVideoOnly(string filePath)
{
return MatchFileExtensions(filePath, DecoderRegistry.EnumerateDecoder()
.SelectMany(x => x.VideoExtensions())
.Distinct());
}

private static bool MatchFileImage(string filePath)
{
string[] extensions = new string[]
{
"*.bmp",
"*.gif",
"*.ico",
"*.jpg",
"*.jpeg",
"*.png",
"*.wbmp",
"*.webp",
"*.pkm",
"*.ktx",
"*.astc",
"*.dng",
"*.heif",
"*.avif",
};
return MatchFileExtensions(filePath, extensions);
}

private sealed class TrackedLayerTopObservable : LightweightObservableBase<double>, IDisposable
{
private readonly TimelineViewModel _timeline;
Expand Down
13 changes: 10 additions & 3 deletions src/Beutl/Views/Timeline.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@

using Avalonia;
using Avalonia.Controls;
using Avalonia.Data;
using Avalonia.Data.Converters;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using Avalonia.Threading;

using Beutl.Media;
using Beutl.Models;
using Beutl.ProjectSystem;
using Beutl.Services;
using Beutl.ViewModels;
using Beutl.ViewModels.Tools;
using Beutl.ViewModels.Dialogs;
using Beutl.ViewModels.Tools;
using Beutl.Views.Dialogs;

using FluentAvalonia.UI.Controls;
Expand Down Expand Up @@ -382,6 +381,14 @@ private async void TimelinePanel_Drop(object? sender, DragEventArgs e)
viewModel.ClickedFrame, TimeSpan.FromSeconds(5), viewModel.CalculateClickedLayer(), InitialOperator: type));
}
}
else if (e.Data.GetFiles()
?.Where(v => v is IStorageFile)
?.Select(v => v.TryGetLocalPath())
.FirstOrDefault(v => v != null) is { } fileName)
{
viewModel.AddLayer.Execute(new ElementDescription(
viewModel.ClickedFrame, TimeSpan.FromSeconds(5), viewModel.CalculateClickedLayer(), FileName: fileName));
}
}

private void TimelinePanel_DragOver(object? sender, DragEventArgs e)
Expand Down