Add buttons for play/pause/stop Animation timeline in editor window

This commit is contained in:
Wojtek Figat
2021-07-23 16:18:11 +02:00
parent a7a7d816ac
commit 46a65f99fa
6 changed files with 150 additions and 13 deletions

View File

@@ -1,6 +1,8 @@
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
using System;
using FlaxEditor.GUI.Timeline.Tracks;
using FlaxEditor.Viewport.Previews;
using FlaxEngine;
namespace FlaxEditor.GUI.Timeline
@@ -20,12 +22,35 @@ namespace FlaxEditor.GUI.Timeline
}
}
private AnimationPreview _preview;
/// <summary>
/// Gets or sets the animated preview used for the animation playback.
/// </summary>
public AnimationPreview Preview
{
get => _preview;
set
{
if (_preview == value)
return;
_preview = value;
UpdatePlaybackState();
PreviewChanged?.Invoke();
}
}
/// <summary>
/// Occurs when the selected animated model preview gets changed.
/// </summary>
public event Action PreviewChanged;
/// <summary>
/// Initializes a new instance of the <see cref="AnimationTimeline"/> class.
/// </summary>
/// <param name="undo">The undo/redo to use for the history actions recording. Optional, can be null to disable undo support.</param>
public AnimationTimeline(FlaxEditor.Undo undo)
: base(PlaybackButtons.None, undo, false, false)
: base(PlaybackButtons.Play | PlaybackButtons.Stop, undo, false, false)
{
PlaybackState = PlaybackStates.Seeking;
ShowPreviewValues = false;
@@ -62,6 +87,60 @@ namespace FlaxEditor.GUI.Timeline
asset.Reload();
}
private void UpdatePlaybackState()
{
PlaybackStates state;
ShowPlaybackButtonsArea = _preview != null;
if (_preview != null)
{
if (_preview.PlayAnimation)
{
state = PlaybackStates.Playing;
}
else
{
state = PlaybackStates.Paused;
}
}
else
{
state = PlaybackStates.Seeking;
}
PlaybackState = state;
}
/// <inheritdoc />
public override void Update(float deltaTime)
{
base.Update(deltaTime);
UpdatePlaybackState();
}
/// <inheritdoc />
public override void OnPlay()
{
_preview.Play();
base.OnPlay();
}
/// <inheritdoc />
public override void OnPause()
{
_preview.Pause();
base.OnPause();
}
/// <inheritdoc />
public override void OnStop()
{
_preview.Stop();
base.OnStop();
}
/// <inheritdoc />
public override void OnSeek(int frame)
{

View File

@@ -37,9 +37,7 @@ namespace FlaxEditor.GUI.Timeline
{
if (_player == value)
return;
_player = value;
UpdatePlaybackState();
PlayerChanged?.Invoke();
}

View File

@@ -289,6 +289,7 @@ namespace FlaxEditor.GUI.Timeline
private Image _playbackStop;
private Image _playbackPlay;
private Label _noTracksLabel;
private ContainerControl _playbackButtonsArea;
private PositionHandle _positionHandle;
private bool _isRightMouseButtonDown;
private Vector2 _rightMouseButtonDownPos;
@@ -597,6 +598,19 @@ namespace FlaxEditor.GUI.Timeline
}
}
/// <summary>
/// Gets or sets a value indicating whether show playback buttons area.
/// </summary>
public bool ShowPlaybackButtonsArea
{
get => _playbackButtonsArea?.Visible ?? false;
set
{
if (_playbackButtonsArea != null)
_playbackButtonsArea.Visible = value;
}
}
/// <summary>
/// Gets a value indicating whether user is moving position handle (seeking).
/// </summary>
@@ -723,6 +737,7 @@ namespace FlaxEditor.GUI.Timeline
Offsets = new Margin(0, 0, -playbackButtonsSize, playbackButtonsSize),
Parent = _splitter.Panel1
};
_playbackButtonsArea = playbackButtonsArea;
var playbackButtonsPanel = new ContainerControl
{
AutoFocus = false,
@@ -2305,6 +2320,7 @@ namespace FlaxEditor.GUI.Timeline
_playbackStop = null;
_playbackPlay = null;
_noTracksLabel = null;
_playbackButtonsArea = null;
_positionHandle = null;
DragHandlers.Clear();