mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
* added AnimationStartedEvent * added AnimationStarted event to Control * reordered Control.AnimationStarted above Control.AnimationCompleted * fixed comment * switched to internal constructor and proxy method
70 lines
2.0 KiB
C#
70 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Robust.Client.Animations;
|
|
using Robust.Shared.Collections;
|
|
using Robust.Shared.Timing;
|
|
using static Robust.Client.Animations.AnimationPlaybackShared;
|
|
|
|
namespace Robust.Client.UserInterface
|
|
{
|
|
public partial class Control
|
|
{
|
|
private Dictionary<string, AnimationPlayback>? _playingAnimations;
|
|
|
|
public Action<string>? AnimationStarted;
|
|
public Action<string>? AnimationCompleted;
|
|
|
|
/// <summary>
|
|
/// Start playing an animation.
|
|
/// </summary>
|
|
/// <param name="animation">The animation to play.</param>
|
|
/// <param name="key">
|
|
/// The key for this animation play. This key can be used to stop playback short later.
|
|
/// </param>
|
|
public void PlayAnimation(Animation animation, string key)
|
|
{
|
|
var playback = new AnimationPlayback(animation);
|
|
|
|
_playingAnimations ??= new Dictionary<string, AnimationPlayback>();
|
|
_playingAnimations.Add(key, playback);
|
|
AnimationStarted?.Invoke(key);
|
|
}
|
|
|
|
public bool HasRunningAnimation(string key)
|
|
{
|
|
return _playingAnimations?.ContainsKey(key) ?? false;
|
|
}
|
|
|
|
public void StopAnimation(string key)
|
|
{
|
|
_playingAnimations?.Remove(key);
|
|
}
|
|
|
|
private void ProcessAnimations(FrameEventArgs args)
|
|
{
|
|
if (_playingAnimations == null || _playingAnimations.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var toRemove = new ValueList<string>();
|
|
|
|
foreach (var (key, playback) in _playingAnimations)
|
|
{
|
|
var keep = UpdatePlayback(this, playback, args.DeltaSeconds);
|
|
if (keep)
|
|
continue;
|
|
|
|
toRemove.Add(key);
|
|
}
|
|
|
|
foreach (var key in toRemove.Span)
|
|
{
|
|
_playingAnimations.Remove(key);
|
|
AnimationCompleted?.Invoke(key);
|
|
}
|
|
}
|
|
}
|
|
}
|