Add shared rectangle selection for all timeline tracks to select keyframes

#519
This commit is contained in:
Wojtek Figat
2021-08-24 17:14:41 +02:00
parent 603c9fac07
commit 0063ec3527
11 changed files with 493 additions and 94 deletions

View File

@@ -15,7 +15,7 @@ namespace FlaxEditor.GUI
/// </summary>
/// <seealso cref="FlaxEngine.GUI.ContainerControl" />
[HideInEditor]
public class KeyframesEditor : ContainerControl
public class KeyframesEditor : ContainerControl, IKeyframesEditor
{
/// <summary>
/// A single keyframe.
@@ -91,7 +91,14 @@ namespace FlaxEditor.GUI
private void UpdateSelectionRectangle()
{
var selectionRect = Rectangle.FromPoints(_leftMouseDownPos, _mousePos);
if (_editor.KeyframesEditorContext != null)
_editor.KeyframesEditorContext.OnKeyframesSelection(_editor, this, selectionRect);
else
UpdateSelection(ref selectionRect);
}
internal void UpdateSelection(ref Rectangle selectionRect)
{
// Find controls to select
for (int i = 0; i < Children.Count; i++)
{
@@ -123,6 +130,21 @@ namespace FlaxEditor.GUI
{
_mousePos = location;
// Start moving selection if movement started from the keyframe
if (_leftMouseDown && !_isMovingSelection && GetChildAt(_leftMouseDownPos) is KeyframePoint)
{
// 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();
}
// Moving view
if (_rightMouseDown)
{
@@ -248,28 +270,20 @@ namespace FlaxEditor.GUI
// Check if user is pressing control
if (Root.GetKey(KeyboardKeys.Control))
{
// Add to selection
keyframe.Select();
// Toggle selection
keyframe.IsSelected = !keyframe.IsSelected;
}
// Check if node isn't selected
else if (!keyframe.IsSelected)
{
// Select node
_editor.ClearSelection();
keyframe.Select();
if (_editor.KeyframesEditorContext != null)
_editor.KeyframesEditorContext.OnKeyframesDeselect(_editor);
else
_editor.ClearSelection();
keyframe.IsSelected = true;
}
// Start moving selected nodes
StartMouseCapture();
_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();
Focus();
Tooltip?.Hide();
return true;
@@ -281,7 +295,10 @@ namespace FlaxEditor.GUI
{
// Start selecting
StartMouseCapture();
_editor.ClearSelection();
if (_editor.KeyframesEditorContext != null)
_editor.KeyframesEditorContext.OnKeyframesDeselect(_editor);
else
_editor.ClearSelection();
Focus();
return true;
}
@@ -319,11 +336,6 @@ namespace FlaxEditor.GUI
_editor.UpdateKeyframes();
}
}
// Selecting
else
{
UpdateSelectionRectangle();
}
_isMovingSelection = false;
_movedKeyframes = false;
@@ -343,7 +355,7 @@ namespace FlaxEditor.GUI
{
// Select node
selectionCount = 1;
point.Select();
point.IsSelected = true;
}
var viewRect = _editor._mainPanel.GetClientArea();
@@ -351,18 +363,15 @@ namespace FlaxEditor.GUI
var cm = new ContextMenu.ContextMenu();
cm.AddButton("Add keyframe", () => _editor.AddKeyframe(_cmShowPos)).Enabled = _editor.Keyframes.Count < _editor.MaxKeyframes;
if (selectionCount == 0)
if (selectionCount > 0)
{
cm.AddButton(selectionCount == 1 ? "Edit keyframe" : "Edit keyframes", () => _editor.EditKeyframes(this, location));
}
else if (selectionCount == 1)
var totalSelectionCount = _editor.KeyframesEditorContext?.OnKeyframesSelectionCount() ?? selectionCount;
Debug.Log(totalSelectionCount);
if (totalSelectionCount > 0)
{
cm.AddButton("Edit keyframe", () => _editor.EditKeyframes(this, location));
cm.AddButton("Remove keyframe", _editor.RemoveKeyframes);
}
else
{
cm.AddButton("Edit keyframes", () => _editor.EditKeyframes(this, location));
cm.AddButton("Remove keyframes", _editor.RemoveKeyframes);
cm.AddButton(totalSelectionCount == 1 ? "Remove keyframe" : "Remove keyframes", _editor.RemoveKeyframes);
}
cm.AddButton("Edit all keyframes", () => _editor.EditAllKeyframes(this, location));
if (_editor.EnableZoom && _editor.EnablePanning)
@@ -482,16 +491,6 @@ namespace FlaxEditor.GUI
return false;
}
public void Select()
{
IsSelected = true;
}
public void Deselect()
{
IsSelected = false;
}
/// <summary>
/// Updates the tooltip.
/// </summary>
@@ -980,7 +979,16 @@ namespace FlaxEditor.GUI
private void RemoveKeyframes()
{
bool edited = false;
if (KeyframesEditorContext != null)
KeyframesEditorContext.OnKeyframesDelete(this);
else
RemoveKeyframesInner();
}
private void RemoveKeyframesInner()
{
if (SelectionCount == 0)
return;
var keyframes = new Dictionary<int, Keyframe>(_keyframes.Count);
for (int i = 0; i < _points.Count; i++)
{
@@ -991,12 +999,9 @@ namespace FlaxEditor.GUI
}
else
{
p.Deselect();
edited = true;
p.IsSelected = false;
}
}
if (!edited)
return;
OnEditingStart();
_keyframes.Clear();
@@ -1088,19 +1093,25 @@ namespace FlaxEditor.GUI
}
}
private void ClearSelection()
/// <summary>
/// Clears the selection.
/// </summary>
public void ClearSelection()
{
for (int i = 0; i < _points.Count; i++)
{
_points[i].Deselect();
_points[i].IsSelected = false;
}
}
private void SelectAll()
/// <summary>
/// Selects all keyframes.
/// </summary>
public void SelectAll()
{
for (int i = 0; i < _points.Count; i++)
{
_points[i].Select();
_points[i].IsSelected = true;
}
}
@@ -1182,8 +1193,39 @@ namespace FlaxEditor.GUI
// Cleanup
_points.Clear();
_keyframes.Clear();
KeyframesEditorContext = null;
base.OnDestroy();
}
/// <inheritdoc />
public IKeyframesEditorContext KeyframesEditorContext { get; set; }
/// <inheritdoc />
public void OnKeyframesDeselect(IKeyframesEditor editor)
{
ClearSelection();
}
/// <inheritdoc />
public void OnKeyframesSelection(IKeyframesEditor editor, ContainerControl control, Rectangle selection)
{
if (_keyframes.Count == 0)
return;
var selectionRect = Rectangle.FromPoints(_contents.PointFromParent(control, selection.UpperLeft), _contents.PointFromParent(control, selection.BottomRight));
_contents.UpdateSelection(ref selectionRect);
}
/// <inheritdoc />
public int OnKeyframesSelectionCount()
{
return SelectionCount;
}
/// <inheritdoc />
public void OnKeyframesDelete(IKeyframesEditor editor)
{
RemoveKeyframesInner();
}
}
}

View File

@@ -21,7 +21,7 @@ namespace FlaxEditor.GUI.Timeline
/// </summary>
/// <seealso cref="FlaxEngine.GUI.ContainerControl" />
[HideInEditor]
public class Timeline : ContainerControl
public class Timeline : ContainerControl, IKeyframesEditorContext
{
private static readonly KeyValuePair<float, string>[] FPSValues =
{
@@ -2393,5 +2393,49 @@ namespace FlaxEditor.GUI.Timeline
base.OnDestroy();
}
/// <inheritdoc />
public void OnKeyframesDeselect(IKeyframesEditor editor)
{
for (int i = 0; i < _tracks.Count; i++)
{
if (_tracks[i] is IKeyframesEditorContext trackContext)
trackContext.OnKeyframesDeselect(editor);
}
}
/// <inheritdoc />
public void OnKeyframesSelection(IKeyframesEditor editor, ContainerControl control, Rectangle selection)
{
var globalControl = _backgroundArea;
var globalRect = Rectangle.FromPoints(control.PointToParent(globalControl, selection.UpperLeft), control.PointToParent(globalControl, selection.BottomRight));
for (int i = 0; i < _tracks.Count; i++)
{
if (_tracks[i] is IKeyframesEditorContext trackContext)
trackContext.OnKeyframesSelection(editor, globalControl, globalRect);
}
}
/// <inheritdoc />
public int OnKeyframesSelectionCount()
{
int result = 0;
for (int i = 0; i < _tracks.Count; i++)
{
if (_tracks[i] is IKeyframesEditorContext trackContext)
result += trackContext.OnKeyframesSelectionCount();
}
return result;
}
/// <inheritdoc />
public void OnKeyframesDelete(IKeyframesEditor editor)
{
for (int i = 0; i < _tracks.Count; i++)
{
if (_tracks[i] is IKeyframesEditorContext trackContext)
trackContext.OnKeyframesDelete(editor);
}
}
}
}

View File

@@ -225,7 +225,7 @@ namespace FlaxEditor.GUI.Timeline.Tracks
/// The child volume track for audio track. Used to animate audio volume over time.
/// </summary>
/// <seealso cref="FlaxEditor.GUI.Timeline.Track" />
class AudioVolumeTrack : Track
class AudioVolumeTrack : Track, IKeyframesEditorContext
{
/// <summary>
/// Gets the archetype.
@@ -478,7 +478,11 @@ namespace FlaxEditor.GUI.Timeline.Tracks
return;
Curve.Visible = Visible;
if (!Visible)
{
Curve.ClearSelection();
return;
}
Curve.KeyframesEditorContext = Timeline;
Curve.CustomViewPanning = Timeline.OnKeyframesViewPanning;
Curve.Bounds = new Rectangle(_audioMedia.X, Y + 1.0f, _audioMedia.Width, Height - 2.0f);
@@ -639,5 +643,32 @@ namespace FlaxEditor.GUI.Timeline.Tracks
base.OnDestroy();
}
/// <inheritdoc />
public void OnKeyframesDeselect(IKeyframesEditor editor)
{
if (Curve != null && Curve.Visible)
Curve.OnKeyframesDeselect(editor);
}
/// <inheritdoc />
public void OnKeyframesSelection(IKeyframesEditor editor, ContainerControl control, Rectangle selection)
{
if (Curve != null && Curve.Visible)
Curve.OnKeyframesSelection(editor, control, selection);
}
/// <inheritdoc />
public int OnKeyframesSelectionCount()
{
return Curve != null && Curve.Visible ? Curve.OnKeyframesSelectionCount() : 0;
}
/// <inheritdoc />
public void OnKeyframesDelete(IKeyframesEditor editor)
{
if (Curve != null && Curve.Visible)
Curve.OnKeyframesDelete(editor);
}
}
}

View File

@@ -15,7 +15,7 @@ namespace FlaxEditor.GUI.Timeline.Tracks
/// The timeline track for animating object property via Curve.
/// </summary>
/// <seealso cref="MemberTrack" />
public abstract class CurvePropertyTrackBase : MemberTrack
public abstract class CurvePropertyTrackBase : MemberTrack, IKeyframesEditorContext
{
private sealed class Splitter : Control
{
@@ -226,8 +226,12 @@ namespace FlaxEditor.GUI.Timeline.Tracks
return;
Curve.Visible = Visible;
if (!Visible)
{
Curve.ClearSelection();
return;
}
var expanded = IsExpanded;
Curve.KeyframesEditorContext = Timeline;
Curve.CustomViewPanning = Timeline.OnKeyframesViewPanning;
Curve.Bounds = new Rectangle(Timeline.StartOffset, Y + 1.0f, Timeline.Duration * Timeline.UnitsPerSecond * Timeline.Zoom, Height - 2.0f);
Curve.ViewScale = new Vector2(Timeline.Zoom, Curve.ViewScale.Y);
@@ -240,11 +244,13 @@ namespace FlaxEditor.GUI.Timeline.Tracks
if (expanded)
{
if (_splitter == null)
{
_splitter = new Splitter
{
_track = this,
Parent = Curve,
};
}
var splitterHeight = 4.0f;
_splitter.Bounds = new Rectangle(0, Curve.Height - splitterHeight, Curve.Width, splitterHeight);
}
@@ -424,6 +430,33 @@ namespace FlaxEditor.GUI.Timeline.Tracks
base.OnDestroy();
}
/// <inheritdoc />
public void OnKeyframesDeselect(IKeyframesEditor editor)
{
if (Curve != null && Curve.Visible)
Curve.OnKeyframesDeselect(editor);
}
/// <inheritdoc />
public void OnKeyframesSelection(IKeyframesEditor editor, ContainerControl control, Rectangle selection)
{
if (Curve != null && Curve.Visible)
Curve.OnKeyframesSelection(editor, control, selection);
}
/// <inheritdoc />
public int OnKeyframesSelectionCount()
{
return Curve != null && Curve.Visible ? Curve.OnKeyframesSelectionCount() : 0;
}
/// <inheritdoc />
public void OnKeyframesDelete(IKeyframesEditor editor)
{
if (Curve != null && Curve.Visible)
Curve.OnKeyframesDelete(editor);
}
}
/// <summary>

View File

@@ -16,7 +16,7 @@ namespace FlaxEditor.GUI.Timeline.Tracks
/// The timeline track for invoking events on a certain points in the time.
/// </summary>
/// <seealso cref="MemberTrack" />
public class EventTrack : MemberTrack
public class EventTrack : MemberTrack, IKeyframesEditorContext
{
/// <summary>
/// Gets the archetype.
@@ -285,7 +285,11 @@ namespace FlaxEditor.GUI.Timeline.Tracks
return;
Events.Visible = Visible;
if (!Visible)
{
Events.ClearSelection();
return;
}
Events.KeyframesEditorContext = Timeline;
Events.CustomViewPanning = Timeline.OnKeyframesViewPanning;
Events.Bounds = new Rectangle(Timeline.StartOffset, Y + 1.0f, Timeline.Duration * Timeline.UnitsPerSecond * Timeline.Zoom, Height - 2.0f);
Events.ViewScale = new Vector2(Timeline.Zoom, 1.0f);
@@ -407,5 +411,32 @@ namespace FlaxEditor.GUI.Timeline.Tracks
base.OnDestroy();
}
/// <inheritdoc />
public void OnKeyframesDeselect(IKeyframesEditor editor)
{
if (Events != null && Events.Visible)
Events.OnKeyframesDeselect(editor);
}
/// <inheritdoc />
public void OnKeyframesSelection(IKeyframesEditor editor, ContainerControl control, Rectangle selection)
{
if (Events != null && Events.Visible)
Events.OnKeyframesSelection(editor, control, selection);
}
/// <inheritdoc />
public int OnKeyframesSelectionCount()
{
return Events != null && Events.Visible ? Events.OnKeyframesSelectionCount() : 0;
}
/// <inheritdoc />
public void OnKeyframesDelete(IKeyframesEditor editor)
{
if (Events != null && Events.Visible)
Events.OnKeyframesDelete(editor);
}
}
}

View File

@@ -15,7 +15,7 @@ namespace FlaxEditor.GUI.Timeline.Tracks
/// The timeline track for animating object property via keyframes collection.
/// </summary>
/// <seealso cref="MemberTrack" />
public class KeyframesPropertyTrack : MemberTrack
public class KeyframesPropertyTrack : MemberTrack, IKeyframesEditorContext
{
/// <summary>
/// Gets the archetype.
@@ -252,7 +252,11 @@ namespace FlaxEditor.GUI.Timeline.Tracks
return;
Keyframes.Visible = Visible;
if (!Visible)
{
Keyframes.ClearSelection();
return;
}
Keyframes.KeyframesEditorContext = Timeline;
Keyframes.CustomViewPanning = Timeline.OnKeyframesViewPanning;
Keyframes.Bounds = new Rectangle(Timeline.StartOffset, Y + 1.0f, Timeline.Duration * Timeline.UnitsPerSecond * Timeline.Zoom, Height - 2.0f);
Keyframes.ViewScale = new Vector2(Timeline.Zoom, 1.0f);
@@ -387,5 +391,32 @@ namespace FlaxEditor.GUI.Timeline.Tracks
base.OnDestroy();
}
/// <inheritdoc />
public void OnKeyframesDeselect(IKeyframesEditor editor)
{
if (Keyframes != null && Keyframes.Visible)
Keyframes.OnKeyframesDeselect(editor);
}
/// <inheritdoc />
public void OnKeyframesSelection(IKeyframesEditor editor, ContainerControl control, Rectangle selection)
{
if (Keyframes != null && Keyframes.Visible)
Keyframes.OnKeyframesSelection(editor, control, selection);
}
/// <inheritdoc />
public int OnKeyframesSelectionCount()
{
return Keyframes != null && Keyframes.Visible ? Keyframes.OnKeyframesSelectionCount() : 0;
}
/// <inheritdoc />
public void OnKeyframesDelete(IKeyframesEditor editor)
{
if (Keyframes != null && Keyframes.Visible)
Keyframes.OnKeyframesDelete(editor);
}
}
}