SpriteSheetAnimationBuilder.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright (c) Craftwork Games. All rights reserved.
  2. // Licensed under the MIT license.
  3. // See LICENSE file in the project root for full license information.
  4. using System;
  5. using System.Collections.Generic;
  6. namespace MonoGame.Extended.Graphics;
  7. /// <summary>
  8. /// A builder class for creating <see cref="SpriteSheetAnimation"/> instances.
  9. /// </summary>
  10. public sealed class SpriteSheetAnimationBuilder
  11. {
  12. private readonly string _name;
  13. private readonly SpriteSheet _spriteSheet;
  14. private readonly List<SpriteSheetAnimationFrame> _frames = new List<SpriteSheetAnimationFrame>();
  15. private bool _isLooping;
  16. private bool _isReversed;
  17. private bool _isPingPong;
  18. internal SpriteSheetAnimationBuilder(string name, SpriteSheet spriteSheet)
  19. {
  20. _name = name;
  21. _spriteSheet = spriteSheet;
  22. }
  23. /// <summary>
  24. /// Adds a frame to the animation using the region index and duration.
  25. /// </summary>
  26. /// <param name="regionIndex">The index of the region in the sprite sheet.</param>
  27. /// <param name="duration">The duration of the frame.</param>
  28. /// <returns>The <see cref="SpriteSheetAnimationBuilder"/> instance for chaining.</returns>
  29. public SpriteSheetAnimationBuilder AddFrame(int regionIndex, TimeSpan duration)
  30. {
  31. SpriteSheetAnimationFrame frame = new SpriteSheetAnimationFrame(regionIndex, duration);
  32. _frames.Add(frame);
  33. return this;
  34. }
  35. /// <summary>
  36. /// Adds a frame to the animation using the region name and duration.
  37. /// </summary>
  38. /// <param name="regionName">The name of the region in the sprite sheet.</param>
  39. /// <param name="duration">The duration of the frame.</param>
  40. /// <returns>The <see cref="SpriteSheetAnimationBuilder"/> instance for chaining.</returns>
  41. public SpriteSheetAnimationBuilder AddFrame(string regionName, TimeSpan duration)
  42. {
  43. int index = _spriteSheet.TextureAtlas.GetIndexOfRegion(regionName);
  44. return AddFrame(index, duration);
  45. }
  46. /// <summary>
  47. /// Sets whether the animation should loop.
  48. /// </summary>
  49. /// <param name="isLooping">If set to <c>true</c>, the animation will loop.</param>
  50. /// <returns>The <see cref="SpriteSheetAnimationBuilder"/> instance for chaining.</returns>
  51. public SpriteSheetAnimationBuilder IsLooping(bool isLooping)
  52. {
  53. _isLooping = isLooping;
  54. return this;
  55. }
  56. /// <summary>
  57. /// Sets whether the animation should be reversed.
  58. /// </summary>
  59. /// <param name="isReversed">If set to <c>true</c>, the animation will play in reverse.</param>
  60. /// <returns>The <see cref="SpriteSheetAnimationBuilder"/> instance for chaining.</returns>
  61. public SpriteSheetAnimationBuilder IsReversed(bool isReversed)
  62. {
  63. _isReversed = isReversed;
  64. return this;
  65. }
  66. /// <summary>
  67. /// Sets whether the animation should ping-pong (reverse direction at the ends).
  68. /// </summary>
  69. /// <param name="isPingPong">If set to <c>true</c>, the animation will ping-pong.</param>
  70. /// <returns>The <see cref="SpriteSheetAnimationBuilder"/> instance for chaining.</returns>
  71. public SpriteSheetAnimationBuilder IsPingPong(bool isPingPong)
  72. {
  73. _isPingPong = isPingPong;
  74. return this;
  75. }
  76. internal SpriteSheetAnimation Build() =>
  77. new SpriteSheetAnimation(_name, _frames.ToArray(), _isLooping, _isReversed, _isPingPong);
  78. }