mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 03:48:56 +00:00
Implemented keyframe easing
This commit is contained in:
1 parent
1a96707e5b
commit
ac7e0d4f78
33 files changed
+651
-164
No files matched your search
+135
-35
@@ -2,28 +2,39 @@
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using Artemis.Core.Events;
|
||||
using Artemis.Core.Models.Profile;
|
||||
using Artemis.Core.Services;
|
||||
using Artemis.Core.Services.Interfaces;
|
||||
using Artemis.UI.Ninject.Factories;
|
||||
using Artemis.UI.Screens.Module.ProfileEditor.LayerProperties.PropertyTree;
|
||||
using Artemis.UI.Screens.Module.ProfileEditor.LayerProperties.Timeline;
|
||||
using Artemis.UI.Services.Interfaces;
|
||||
using Stylet;
|
||||
|
||||
namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties
|
||||
{
|
||||
public class LayerPropertiesViewModel : ProfileEditorPanelViewModel
|
||||
{
|
||||
private readonly ICoreService _coreService;
|
||||
private readonly ILayerPropertyViewModelFactory _layerPropertyViewModelFactory;
|
||||
private readonly IProfileEditorService _profileEditorService;
|
||||
private readonly ISettingsService _settingsService;
|
||||
|
||||
public LayerPropertiesViewModel(IProfileEditorService profileEditorService,
|
||||
ICoreService coreService,
|
||||
ISettingsService settingsService,
|
||||
ILayerPropertyViewModelFactory layerPropertyViewModelFactory,
|
||||
IPropertyTreeViewModelFactory propertyTreeViewModelFactory,
|
||||
IPropertyTimelineViewModelFactory propertyTimelineViewModelFactory)
|
||||
{
|
||||
_profileEditorService = profileEditorService;
|
||||
_coreService = coreService;
|
||||
_settingsService = settingsService;
|
||||
_layerPropertyViewModelFactory = layerPropertyViewModelFactory;
|
||||
|
||||
PixelsPerSecond = 1;
|
||||
PixelsPerSecond = 31;
|
||||
PropertyTree = propertyTreeViewModelFactory.Create(this);
|
||||
PropertyTimeline = propertyTimelineViewModelFactory.Create(this);
|
||||
|
||||
@@ -33,17 +44,9 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties
|
||||
_profileEditorService.CurrentTimeChanged += ProfileEditorServiceOnCurrentTimeChanged;
|
||||
}
|
||||
|
||||
public string FormattedCurrentTime
|
||||
{
|
||||
get
|
||||
{
|
||||
if (PixelsPerSecond > 200)
|
||||
return $"{Math.Floor(_profileEditorService.CurrentTime.TotalSeconds):00}.{_profileEditorService.CurrentTime.Milliseconds:000}";
|
||||
if (PixelsPerSecond > 60)
|
||||
return $"{Math.Floor(_profileEditorService.CurrentTime.TotalSeconds):00}.{_profileEditorService.CurrentTime.Milliseconds:000}";
|
||||
return $"{Math.Floor(_profileEditorService.CurrentTime.TotalMinutes):0}:{_profileEditorService.CurrentTime.Seconds:00}";
|
||||
}
|
||||
}
|
||||
public bool Playing { get; set; }
|
||||
|
||||
public string FormattedCurrentTime => $"{Math.Floor(_profileEditorService.CurrentTime.TotalSeconds):00}.{_profileEditorService.CurrentTime.Milliseconds:000}";
|
||||
|
||||
public int PixelsPerSecond
|
||||
{
|
||||
@@ -90,48 +93,145 @@ namespace Artemis.UI.Screens.Module.ProfileEditor.LayerProperties
|
||||
NotifyOfPropertyChange(() => TimeCaretPosition);
|
||||
}
|
||||
|
||||
protected override void OnDeactivate()
|
||||
{
|
||||
Pause();
|
||||
base.OnDeactivate();
|
||||
}
|
||||
|
||||
#region Controls
|
||||
|
||||
public void PlayFromStart()
|
||||
{
|
||||
if (!IsActive)
|
||||
return;
|
||||
if (Playing)
|
||||
Pause();
|
||||
|
||||
_profileEditorService.CurrentTime = TimeSpan.Zero;
|
||||
Play();
|
||||
}
|
||||
|
||||
public void Play()
|
||||
{
|
||||
if (!IsActive)
|
||||
return;
|
||||
if (Playing)
|
||||
{
|
||||
Pause();
|
||||
return;
|
||||
}
|
||||
|
||||
_coreService.FrameRendering += CoreServiceOnFrameRendering;
|
||||
Playing = true;
|
||||
}
|
||||
|
||||
public void Pause()
|
||||
{
|
||||
if (!Playing)
|
||||
return;
|
||||
|
||||
_coreService.FrameRendering -= CoreServiceOnFrameRendering;
|
||||
Playing = false;
|
||||
}
|
||||
|
||||
|
||||
public void GoToStart()
|
||||
{
|
||||
_profileEditorService.CurrentTime = TimeSpan.Zero;
|
||||
}
|
||||
|
||||
public void GoToEnd()
|
||||
{
|
||||
_profileEditorService.CurrentTime = CalculateEndTime();
|
||||
}
|
||||
|
||||
public void GoToPreviousFrame()
|
||||
{
|
||||
var frameTime = 1000.0 / _settingsService.GetSetting("Core.TargetFrameRate", 25).Value;
|
||||
var newTime = Math.Max(0, Math.Round((_profileEditorService.CurrentTime.TotalMilliseconds - frameTime) / frameTime) * frameTime);
|
||||
_profileEditorService.CurrentTime = TimeSpan.FromMilliseconds(newTime);
|
||||
}
|
||||
|
||||
public void GoToNextFrame()
|
||||
{
|
||||
var frameTime = 1000.0 / _settingsService.GetSetting("Core.TargetFrameRate", 25).Value;
|
||||
var newTime = Math.Round((_profileEditorService.CurrentTime.TotalMilliseconds + frameTime) / frameTime) * frameTime;
|
||||
newTime = Math.Min(newTime, CalculateEndTime().TotalMilliseconds);
|
||||
_profileEditorService.CurrentTime = TimeSpan.FromMilliseconds(newTime);
|
||||
}
|
||||
|
||||
private TimeSpan CalculateEndTime()
|
||||
{
|
||||
// End time is the last keyframe + 10 sec
|
||||
var lastKeyFrame = PropertyTimeline.PropertyTrackViewModels.SelectMany(r => r.KeyframeViewModels).OrderByDescending(t => t.Keyframe.Position).FirstOrDefault();
|
||||
return lastKeyFrame?.Keyframe.Position.Add(new TimeSpan(0, 0, 0, 10)) ?? TimeSpan.FromSeconds(10);
|
||||
}
|
||||
|
||||
private void CoreServiceOnFrameRendering(object sender, FrameRenderingEventArgs e)
|
||||
{
|
||||
Execute.PostToUIThread(() =>
|
||||
{
|
||||
var newTime = _profileEditorService.CurrentTime.Add(TimeSpan.FromSeconds(e.DeltaTime));
|
||||
if (newTime > CalculateEndTime())
|
||||
{
|
||||
newTime = CalculateEndTime();
|
||||
Pause();
|
||||
}
|
||||
|
||||
_profileEditorService.CurrentTime = newTime;
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Caret movement
|
||||
|
||||
private double _caretStartMouseStartOffset;
|
||||
private bool _mouseOverCaret;
|
||||
private int _pixelsPerSecond;
|
||||
|
||||
public void TimelineMouseDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
// TODO Preserve mouse offset
|
||||
_caretStartMouseStartOffset = e.GetPosition((IInputElement) sender).X - TimeCaretPosition.Left;
|
||||
((IInputElement) sender).CaptureMouse();
|
||||
}
|
||||
|
||||
public void CaretMouseEnter(object sender, MouseEventArgs e)
|
||||
public void TimelineMouseUp(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
_mouseOverCaret = true;
|
||||
}
|
||||
|
||||
public void CaretMouseLeave(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (e.LeftButton != MouseButtonState.Pressed)
|
||||
_mouseOverCaret = false;
|
||||
((IInputElement) sender).ReleaseMouseCapture();
|
||||
}
|
||||
|
||||
public void TimelineMouseMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (_mouseOverCaret && e.LeftButton == MouseButtonState.Pressed)
|
||||
if (e.LeftButton == MouseButtonState.Pressed)
|
||||
{
|
||||
// Snap to visible keyframes
|
||||
var visibleKeyframes = PropertyTimeline.PropertyTrackViewModels.Where(t => t.LayerPropertyViewModel.Parent != null &&
|
||||
t.LayerPropertyViewModel.Parent.IsExpanded)
|
||||
// Get the parent grid, need that for our position
|
||||
var parent = (IInputElement) VisualTreeHelper.GetParent((DependencyObject) sender);
|
||||
var x = Math.Max(0, e.GetPosition(parent).X);
|
||||
var newTime = TimeSpan.FromSeconds(x / PixelsPerSecond);
|
||||
|
||||
// Round the time to something that fits the current zoom level
|
||||
if (PixelsPerSecond < 200)
|
||||
newTime = TimeSpan.FromMilliseconds(Math.Round(newTime.TotalMilliseconds / 5.0) * 5.0);
|
||||
else if (PixelsPerSecond < 500)
|
||||
newTime = TimeSpan.FromMilliseconds(Math.Round(newTime.TotalMilliseconds / 2.0) * 2.0);
|
||||
else
|
||||
newTime = TimeSpan.FromMilliseconds(Math.Round(newTime.TotalMilliseconds));
|
||||
|
||||
if (!Keyboard.IsKeyDown(Key.LeftShift) && !Keyboard.IsKeyDown(Key.RightShift))
|
||||
{
|
||||
_profileEditorService.CurrentTime = newTime;
|
||||
return;
|
||||
}
|
||||
|
||||
// If shift is held, snap to closest keyframe
|
||||
var visibleKeyframes = PropertyTimeline.PropertyTrackViewModels
|
||||
.Where(t => t.LayerPropertyViewModel.Parent != null && t.LayerPropertyViewModel.Parent.IsExpanded)
|
||||
.SelectMany(t => t.KeyframeViewModels);
|
||||
|
||||
TimeCaretPosition = new Thickness(Math.Max(0, e.GetPosition((IInputElement) sender).X + _caretStartMouseStartOffset), 0, 0, 0);
|
||||
|
||||
|
||||
// Take a tolerance of 5 pixels (half a keyframe width)
|
||||
var tolerance = 1000f / PixelsPerSecond * 5;
|
||||
var closeKeyframe = visibleKeyframes.FirstOrDefault(
|
||||
kf => Math.Abs(kf.Keyframe.Position.TotalMilliseconds - _profileEditorService.CurrentTime.TotalMilliseconds) < tolerance
|
||||
kf => Math.Abs(kf.Keyframe.Position.TotalMilliseconds - newTime.TotalMilliseconds) < tolerance
|
||||
);
|
||||
if (closeKeyframe != null)
|
||||
_profileEditorService.CurrentTime = closeKeyframe.Keyframe.Position;
|
||||
_profileEditorService.CurrentTime = closeKeyframe?.Keyframe.Position ?? newTime;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user