JD2022-TU1/main/tools/framework/JD.Serialization/Timeline/Curves/BezierCurveFloat.cs

105 lines
No EOL
2.7 KiB
C#

using System;
using System.Collections.Specialized;
using System.Drawing;
using System.Linq;
using JD.Core;
using JD.Core.Collections;
using JD.Core.Helpers;
namespace JD.Serialization
{
public partial class BezierCurveFloat
{
public BezierCurveFloat()
{
_keys.CollectionChanged += Keys_CollectionChanged;
_keys.ItemChanged += Keys_ItemsChanged;
}
/// <summary>
/// The edition type of the curve.
/// </summary>
public BezierCurveType Type { get; set; }
public Clip Clip { get; set; }
public override string ToString()
{
return $"{Type} - {Keys.Count} points";
}
void Keys_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (KeyFloat key in e.NewItems)
{
key.Curve = this;
}
SortFrames();
}
if (e.OldItems != null)
{
foreach (KeyFloat key in e.OldItems)
{
key.Curve = null;
}
}
OnPropertyChanged(nameof(Keys));
}
private void Keys_ItemsChanged(object sender, ItemChangedEventArgs<KeyFloat> args)
{
OnPropertyChanged(nameof(Keys));
}
/// <summary>
/// Adds the bezier point to the curve.
/// </summary>
public KeyFloat AddBezierPoint(PointF position, PointF @in, PointF @out)
{
int frame = (int)Math.Round(position.X);
KeyFloat key;
if (TryGetValue(frame, out key))
{
ToKey(key, position, @in, @out);
}
else
{
key = new KeyFloat();
ToKey(key, position, @in, @out);
Keys.Add(key);
}
return key;
}
private void ToKey(KeyFloat key, PointF position, PointF @in, PointF @out)
{
key.Position = position;
key.In = @in;
key.Out = @out;
}
public IDisposable BeginScopedUpdate()
{
return Disposable.FromAction(SortFrames);
}
private void SortFrames()
{
_keys.Sort((f1, f2) => f1.Frame.CompareTo(f2.Frame));
}
public bool TryGetValue(int frame, out KeyFloat key)
{
key = Keys.FirstOrDefault(x => x.Frame.AlmostEquals(frame));
return key != null;
}
}
public partial class KeyFloat
{
public BezierCurveFloat Curve { get; set; }
}
}