// Copyright (c) Craftwork Games. All rights reserved.
// Licensed under the MIT license.
// See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
namespace MonoGame.Extended.Graphics;
///
/// A builder class for creating instances.
///
public sealed class SpriteSheetAnimationBuilder
{
private readonly string _name;
private readonly SpriteSheet _spriteSheet;
private readonly List _frames = new List();
private bool _isLooping;
private bool _isReversed;
private bool _isPingPong;
internal SpriteSheetAnimationBuilder(string name, SpriteSheet spriteSheet)
{
_name = name;
_spriteSheet = spriteSheet;
}
///
/// Adds a frame to the animation using the region index and duration.
///
/// The index of the region in the sprite sheet.
/// The duration of the frame.
/// The instance for chaining.
public SpriteSheetAnimationBuilder AddFrame(int regionIndex, TimeSpan duration)
{
SpriteSheetAnimationFrame frame = new SpriteSheetAnimationFrame(regionIndex, duration);
_frames.Add(frame);
return this;
}
///
/// Adds a frame to the animation using the region name and duration.
///
/// The name of the region in the sprite sheet.
/// The duration of the frame.
/// The instance for chaining.
public SpriteSheetAnimationBuilder AddFrame(string regionName, TimeSpan duration)
{
int index = _spriteSheet.TextureAtlas.GetIndexOfRegion(regionName);
return AddFrame(index, duration);
}
///
/// Sets whether the animation should loop.
///
/// If set to true, the animation will loop.
/// The instance for chaining.
public SpriteSheetAnimationBuilder IsLooping(bool isLooping)
{
_isLooping = isLooping;
return this;
}
///
/// Sets whether the animation should be reversed.
///
/// If set to true, the animation will play in reverse.
/// The instance for chaining.
public SpriteSheetAnimationBuilder IsReversed(bool isReversed)
{
_isReversed = isReversed;
return this;
}
///
/// Sets whether the animation should ping-pong (reverse direction at the ends).
///
/// If set to true, the animation will ping-pong.
/// The instance for chaining.
public SpriteSheetAnimationBuilder IsPingPong(bool isPingPong)
{
_isPingPong = isPingPong;
return this;
}
internal SpriteSheetAnimation Build() =>
new SpriteSheetAnimation(_name, _frames.ToArray(), _isLooping, _isReversed, _isPingPong);
}