Add shared selected keyframes moving ability for timeline tracks

#519
This commit is contained in:
Wojtek Figat
2021-08-25 15:07:53 +02:00
parent 2beb0e137b
commit b85521decd
10 changed files with 266 additions and 127 deletions

View File

@@ -276,5 +276,8 @@ namespace FlaxEditor.GUI
/// <inheritdoc /> /// <inheritdoc />
public abstract void OnKeyframesDelete(IKeyframesEditor editor); public abstract void OnKeyframesDelete(IKeyframesEditor editor);
/// <inheritdoc />
public abstract void OnKeyframesMove(IKeyframesEditor editor, ContainerControl control, Vector2 location, bool start, bool end);
} }
} }

View File

@@ -59,6 +59,97 @@ namespace FlaxEditor.GUI
_editor.UpdateTangents(); _editor.UpdateTangents();
} }
internal void OnMoveStart(Vector2 location)
{
// Start moving selected keyframes
_isMovingSelection = true;
_movedKeyframes = false;
var viewRect = _editor._mainPanel.GetClientArea();
_movingSelectionStart = PointToKeyframes(location, ref viewRect);
if (_movingSelectionOffsets == null || _movingSelectionOffsets.Length != _editor._points.Count)
_movingSelectionOffsets = new Vector2[_editor._points.Count];
for (int i = 0; i < _movingSelectionOffsets.Length; i++)
_movingSelectionOffsets[i] = _editor._points[i].Point - _movingSelectionStart;
_editor.OnEditingStart();
}
internal void OnMove(Vector2 location)
{
var viewRect = _editor._mainPanel.GetClientArea();
var locationKeyframes = PointToKeyframes(location, ref viewRect);
var accessor = _editor.Accessor;
var components = accessor.GetCurveComponents();
for (var i = 0; i < _editor._points.Count; i++)
{
var p = _editor._points[i];
if (p.IsSelected)
{
var k = _editor.GetKeyframe(p.Index);
float time = _editor.GetKeyframeTime(k);
float value = _editor.GetKeyframeValue(k, p.Component);
float minTime = p.Index != 0 ? _editor.GetKeyframeTime(_editor.GetKeyframe(p.Index - 1)) + Mathf.Epsilon : float.MinValue;
float maxTime = p.Index != _editor.KeyframesCount - 1 ? _editor.GetKeyframeTime(_editor.GetKeyframe(p.Index + 1)) - Mathf.Epsilon : float.MaxValue;
var offset = _movingSelectionOffsets[i];
if (!_editor.ShowCollapsed)
{
// Move on value axis
value = locationKeyframes.Y + offset.Y;
}
// Let the first selected point of this keyframe to edit time
bool isFirstSelected = false;
for (var j = 0; j < components; j++)
{
var idx = p.Index * components + j;
if (idx == i)
{
isFirstSelected = true;
break;
}
if (_editor._points[idx].IsSelected)
break;
}
if (isFirstSelected)
{
time = locationKeyframes.X + offset.X;
if (_editor.FPS.HasValue)
{
float fps = _editor.FPS.Value;
time = Mathf.Floor(time * fps) / fps;
}
time = Mathf.Clamp(time, minTime, maxTime);
}
// TODO: snapping keyframes to grid when moving
_editor.SetKeyframeInternal(p.Index, time, value, p.Component);
}
_editor.UpdateKeyframes();
_editor.UpdateTooltips();
if (_editor.EnablePanning == UseMode.On)
{
//_editor._mainPanel.ScrollViewTo(PointToParent(_editor._mainPanel, location));
}
Cursor = CursorType.SizeAll;
_movedKeyframes = true;
}
}
internal void OnMoveEnd(Vector2 location)
{
if (_movedKeyframes)
{
_editor.OnEdited();
_editor.OnEditingEnd();
_movedKeyframes = false;
}
_isMovingSelection = false;
}
/// <inheritdoc /> /// <inheritdoc />
public override bool IntersectsContent(ref Vector2 locationParent, out Vector2 location) public override bool IntersectsContent(ref Vector2 locationParent, out Vector2 location)
{ {
@@ -83,16 +174,10 @@ namespace FlaxEditor.GUI
// Start moving selection if movement started from the keyframe // Start moving selection if movement started from the keyframe
if (_leftMouseDown && !_isMovingSelection && GetChildAt(_leftMouseDownPos) is KeyframePoint) if (_leftMouseDown && !_isMovingSelection && GetChildAt(_leftMouseDownPos) is KeyframePoint)
{ {
// Start moving selected nodes if (_editor.KeyframesEditorContext != null)
_isMovingSelection = true; _editor.KeyframesEditorContext.OnKeyframesMove(_editor, this, location, true, false);
_movedKeyframes = false; else
var viewRect = _editor._mainPanel.GetClientArea(); OnMoveStart(location);
_movingSelectionStart = PointToKeyframes(location, ref viewRect);
if (_movingSelectionOffsets == null || _movingSelectionOffsets.Length != _editor._points.Count)
_movingSelectionOffsets = new Vector2[_editor._points.Count];
for (int i = 0; i < _movingSelectionOffsets.Length; i++)
_movingSelectionOffsets[i] = _editor._points[i].Point - _movingSelectionStart;
_editor.OnEditingStart();
} }
// Moving view // Moving view
@@ -132,68 +217,10 @@ namespace FlaxEditor.GUI
// Moving selection // Moving selection
else if (_isMovingSelection) else if (_isMovingSelection)
{ {
var viewRect = _editor._mainPanel.GetClientArea(); if (_editor.KeyframesEditorContext != null)
var locationKeyframes = PointToKeyframes(location, ref viewRect); _editor.KeyframesEditorContext.OnKeyframesMove(_editor, this, location, false, false);
var accessor = _editor.Accessor; else
var components = accessor.GetCurveComponents(); OnMove(location);
for (var i = 0; i < _editor._points.Count; i++)
{
var p = _editor._points[i];
if (p.IsSelected)
{
var k = _editor.GetKeyframe(p.Index);
float time = _editor.GetKeyframeTime(k);
float value = _editor.GetKeyframeValue(k, p.Component);
float minTime = p.Index != 0 ? _editor.GetKeyframeTime(_editor.GetKeyframe(p.Index - 1)) + Mathf.Epsilon : float.MinValue;
float maxTime = p.Index != _editor.KeyframesCount - 1 ? _editor.GetKeyframeTime(_editor.GetKeyframe(p.Index + 1)) - Mathf.Epsilon : float.MaxValue;
var offset = _movingSelectionOffsets[i];
if (!_editor.ShowCollapsed)
{
// Move on value axis
value = locationKeyframes.Y + offset.Y;
}
// Let the first selected point of this keyframe to edit time
bool isFirstSelected = false;
for (var j = 0; j < components; j++)
{
var idx = p.Index * components + j;
if (idx == i)
{
isFirstSelected = true;
break;
}
if (_editor._points[idx].IsSelected)
break;
}
if (isFirstSelected)
{
time = locationKeyframes.X + offset.X;
if (_editor.FPS.HasValue)
{
float fps = _editor.FPS.Value;
time = Mathf.Floor(time * fps) / fps;
}
time = Mathf.Clamp(time, minTime, maxTime);
}
// TODO: snapping keyframes to grid when moving
_editor.SetKeyframeInternal(p.Index, time, value, p.Component);
}
_editor.UpdateKeyframes();
_editor.UpdateTooltips();
if (_editor.EnablePanning == UseMode.On)
{
//_editor._mainPanel.ScrollViewTo(PointToParent(_editor._mainPanel, location));
}
Cursor = CursorType.SizeAll;
_movedKeyframes = true;
}
return; return;
} }
// Moving tangent // Moving tangent
@@ -374,11 +401,10 @@ namespace FlaxEditor.GUI
// Moving keyframes // Moving keyframes
else if (_isMovingSelection) else if (_isMovingSelection)
{ {
if (_movedKeyframes) if (_editor.KeyframesEditorContext != null)
{ _editor.KeyframesEditorContext.OnKeyframesMove(_editor, this, location, false, true);
_editor.OnEdited(); else
_editor.OnEditingEnd(); OnMoveEnd(location);
}
} }
_isMovingSelection = false; _isMovingSelection = false;
@@ -509,5 +535,19 @@ namespace FlaxEditor.GUI
{ {
RemoveKeyframesInner(); RemoveKeyframesInner();
} }
/// <inheritdoc />
public override void OnKeyframesMove(IKeyframesEditor editor, ContainerControl control, Vector2 location, bool start, bool end)
{
if (_points.Count == 0)
return;
location = _contents.PointFromParent(control, location);
if (start)
_contents.OnMoveStart(location);
else if (end)
_contents.OnMoveEnd(location);
else
_contents.OnMove(location);
}
} }
} }

View File

@@ -40,5 +40,15 @@ namespace FlaxEditor.GUI
/// </summary> /// </summary>
/// <param name="editor">The source editor.</param> /// <param name="editor">The source editor.</param>
void OnKeyframesDelete(IKeyframesEditor editor); void OnKeyframesDelete(IKeyframesEditor editor);
/// <summary>
/// Called when keyframes selection should be moved.
/// </summary>
/// <param name="editor">The source editor.</param>
/// <param name="control">The source movement control.</param>
/// <param name="location">The source movement location (in source control local space).</param>
/// <param name="start">The movement start flag.</param>
/// <param name="end">The movement end flag.</param>
void OnKeyframesMove(IKeyframesEditor editor, ContainerControl control, Vector2 location, bool start, bool end);
} }
} }

View File

@@ -35,5 +35,15 @@ namespace FlaxEditor.GUI
/// </summary> /// </summary>
/// <param name="editor">The source editor.</param> /// <param name="editor">The source editor.</param>
void OnKeyframesDelete(IKeyframesEditor editor); void OnKeyframesDelete(IKeyframesEditor editor);
/// <summary>
/// Called when keyframes selection should be moved.
/// </summary>
/// <param name="editor">The source editor.</param>
/// <param name="control">The source movement control.</param>
/// <param name="location">The source movement location (in source control local space).</param>
/// <param name="start">The movement start flag.</param>
/// <param name="end">The movement end flag.</param>
void OnKeyframesMove(IKeyframesEditor editor, ContainerControl control, Vector2 location, bool start, bool end);
} }
} }

View File

@@ -109,6 +109,69 @@ namespace FlaxEditor.GUI
} }
} }
internal void OnMoveStart(Vector2 location)
{
// Start moving selected nodes
_isMovingSelection = true;
_movedKeyframes = false;
var viewRect = _editor._mainPanel.GetClientArea();
_movingSelectionStart = PointToKeyframes(location, ref viewRect).X;
if (_movingSelectionOffsets == null || _movingSelectionOffsets.Length != _editor._keyframes.Count)
_movingSelectionOffsets = new float[_editor._keyframes.Count];
for (int i = 0; i < _movingSelectionOffsets.Length; i++)
_movingSelectionOffsets[i] = _editor._keyframes[i].Time - _movingSelectionStart;
_editor.OnEditingStart();
}
internal void OnMove(Vector2 location)
{
var viewRect = _editor._mainPanel.GetClientArea();
var locationKeyframes = PointToKeyframes(location, ref viewRect);
for (var i = 0; i < _editor._points.Count; i++)
{
var p = _editor._points[i];
if (p.IsSelected)
{
var k = _editor._keyframes[p.Index];
float minTime = p.Index != 0 ? _editor._keyframes[p.Index - 1].Time : float.MinValue;
float maxTime = p.Index != _editor._keyframes.Count - 1 ? _editor._keyframes[p.Index + 1].Time : float.MaxValue;
var offset = _movingSelectionOffsets[p.Index];
k.Time = locationKeyframes.X + offset;
if (_editor.FPS.HasValue)
{
float fps = _editor.FPS.Value;
k.Time = Mathf.Floor(k.Time * fps) / fps;
}
k.Time = Mathf.Clamp(k.Time, minTime, maxTime);
// TODO: snapping keyframes to grid when moving
_editor._keyframes[p.Index] = k;
}
_editor.UpdateKeyframes();
if (_editor.EnablePanning)
{
//_editor._mainPanel.ScrollViewTo(PointToParent(_editor._mainPanel, location));
}
Cursor = CursorType.SizeAll;
_movedKeyframes = true;
}
}
internal void OnMoveEnd(Vector2 location)
{
if (_movedKeyframes)
{
_editor.OnEdited();
_editor.OnEditingEnd();
_editor.UpdateKeyframes();
_movedKeyframes = false;
}
_isMovingSelection = false;
}
/// <inheritdoc /> /// <inheritdoc />
public override bool IntersectsContent(ref Vector2 locationParent, out Vector2 location) public override bool IntersectsContent(ref Vector2 locationParent, out Vector2 location)
{ {
@@ -133,16 +196,10 @@ namespace FlaxEditor.GUI
// Start moving selection if movement started from the keyframe // Start moving selection if movement started from the keyframe
if (_leftMouseDown && !_isMovingSelection && GetChildAt(_leftMouseDownPos) is KeyframePoint) if (_leftMouseDown && !_isMovingSelection && GetChildAt(_leftMouseDownPos) is KeyframePoint)
{ {
// Start moving selected nodes if (_editor.KeyframesEditorContext != null)
_isMovingSelection = true; _editor.KeyframesEditorContext.OnKeyframesMove(_editor, this, location, true, false);
_movedKeyframes = false; else
var viewRect = _editor._mainPanel.GetClientArea(); OnMoveStart(location);
_movingSelectionStart = PointToKeyframes(location, ref viewRect).X;
if (_movingSelectionOffsets == null || _movingSelectionOffsets.Length != _editor._keyframes.Count)
_movingSelectionOffsets = new float[_editor._keyframes.Count];
for (int i = 0; i < _movingSelectionOffsets.Length; i++)
_movingSelectionOffsets[i] = _editor._keyframes[i].Time - _movingSelectionStart;
_editor.OnEditingStart();
} }
// Moving view // Moving view
@@ -170,40 +227,10 @@ namespace FlaxEditor.GUI
// Moving selection // Moving selection
else if (_isMovingSelection) else if (_isMovingSelection)
{ {
var viewRect = _editor._mainPanel.GetClientArea(); if (_editor.KeyframesEditorContext != null)
var locationKeyframes = PointToKeyframes(location, ref viewRect); _editor.KeyframesEditorContext.OnKeyframesMove(_editor, this, location, false, false);
for (var i = 0; i < _editor._points.Count; i++) else
{ OnMove(location);
var p = _editor._points[i];
if (p.IsSelected)
{
var k = _editor._keyframes[p.Index];
float minTime = p.Index != 0 ? _editor._keyframes[p.Index - 1].Time : float.MinValue;
float maxTime = p.Index != _editor._keyframes.Count - 1 ? _editor._keyframes[p.Index + 1].Time : float.MaxValue;
var offset = _movingSelectionOffsets[p.Index];
k.Time = locationKeyframes.X + offset;
if (_editor.FPS.HasValue)
{
float fps = _editor.FPS.Value;
k.Time = Mathf.Floor(k.Time * fps) / fps;
}
k.Time = Mathf.Clamp(k.Time, minTime, maxTime);
// TODO: snapping keyframes to grid when moving
_editor._keyframes[p.Index] = k;
}
_editor.UpdateKeyframes();
if (_editor.EnablePanning)
{
//_editor._mainPanel.ScrollViewTo(PointToParent(_editor._mainPanel, location));
}
Cursor = CursorType.SizeAll;
_movedKeyframes = true;
}
return; return;
} }
// Selecting // Selecting
@@ -329,12 +356,10 @@ namespace FlaxEditor.GUI
// Moving keyframes // Moving keyframes
if (_isMovingSelection) if (_isMovingSelection)
{ {
if (_movedKeyframes) if (_editor.KeyframesEditorContext != null)
{ _editor.KeyframesEditorContext.OnKeyframesMove(_editor, this, location, false, true);
_editor.OnEdited(); else
_editor.OnEditingEnd(); OnMoveEnd(location);
_editor.UpdateKeyframes();
}
} }
_isMovingSelection = false; _isMovingSelection = false;
@@ -1227,5 +1252,17 @@ namespace FlaxEditor.GUI
{ {
RemoveKeyframesInner(); RemoveKeyframesInner();
} }
/// <inheritdoc />
public void OnKeyframesMove(IKeyframesEditor editor, ContainerControl control, Vector2 location, bool start, bool end)
{
location = _contents.PointFromParent(control, location);
if (start)
_contents.OnMoveStart(location);
else if (end)
_contents.OnMoveEnd(location);
else
_contents.OnMove(location);
}
} }
} }

View File

@@ -2458,5 +2458,16 @@ namespace FlaxEditor.GUI.Timeline
trackContext.OnKeyframesDelete(editor); trackContext.OnKeyframesDelete(editor);
} }
} }
/// <inheritdoc />
public void OnKeyframesMove(IKeyframesEditor editor, ContainerControl control, Vector2 location, bool start, bool end)
{
location = control.PointToParent(_backgroundArea, location);
for (int i = 0; i < _tracks.Count; i++)
{
if (_tracks[i] is IKeyframesEditorContext trackContext)
trackContext.OnKeyframesMove(editor, _backgroundArea, location, start, end);
}
}
} }
} }

View File

@@ -670,5 +670,12 @@ namespace FlaxEditor.GUI.Timeline.Tracks
if (Curve != null && Curve.Visible) if (Curve != null && Curve.Visible)
Curve.OnKeyframesDelete(editor); Curve.OnKeyframesDelete(editor);
} }
/// <inheritdoc />
public void OnKeyframesMove(IKeyframesEditor editor, ContainerControl control, Vector2 location, bool start, bool end)
{
if (Curve != null && Curve.Visible)
Curve.OnKeyframesMove(editor, control, location, start, end);
}
} }
} }

View File

@@ -457,6 +457,13 @@ namespace FlaxEditor.GUI.Timeline.Tracks
if (Curve != null && Curve.Visible) if (Curve != null && Curve.Visible)
Curve.OnKeyframesDelete(editor); Curve.OnKeyframesDelete(editor);
} }
/// <inheritdoc />
public void OnKeyframesMove(IKeyframesEditor editor, ContainerControl control, Vector2 location, bool start, bool end)
{
if (Curve != null && Curve.Visible)
Curve.OnKeyframesMove(editor, control, location, start, end);
}
} }
/// <summary> /// <summary>

View File

@@ -438,5 +438,12 @@ namespace FlaxEditor.GUI.Timeline.Tracks
if (Events != null && Events.Visible) if (Events != null && Events.Visible)
Events.OnKeyframesDelete(editor); Events.OnKeyframesDelete(editor);
} }
/// <inheritdoc />
public void OnKeyframesMove(IKeyframesEditor editor, ContainerControl control, Vector2 location, bool start, bool end)
{
if (Events != null && Events.Visible)
Events.OnKeyframesMove(editor, control, location, start, end);
}
} }
} }

View File

@@ -418,5 +418,12 @@ namespace FlaxEditor.GUI.Timeline.Tracks
if (Keyframes != null && Keyframes.Visible) if (Keyframes != null && Keyframes.Visible)
Keyframes.OnKeyframesDelete(editor); Keyframes.OnKeyframesDelete(editor);
} }
/// <inheritdoc />
public void OnKeyframesMove(IKeyframesEditor editor, ContainerControl control, Vector2 location, bool start, bool end)
{
if (Keyframes != null && Keyframes.Visible)
Keyframes.OnKeyframesMove(editor, control, location, start, end);
}
} }
} }