SpriteSheet.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. /// Represents a sprite sheet containing textures and animations.
  9. /// </summary>
  10. public class SpriteSheet
  11. {
  12. private readonly Dictionary<string, SpriteSheetAnimation> _animations = new Dictionary<string, SpriteSheetAnimation>();
  13. /// <summary>
  14. /// Gets the number of animations defined in the sprite sheet.
  15. /// </summary>
  16. public int AnimationCount => _animations.Count;
  17. /// <summary>
  18. /// Gets the name of the sprite sheet.
  19. /// </summary>
  20. public string Name { get; }
  21. /// <summary>
  22. /// Gets the texture atlas associated with the sprite sheet.
  23. /// </summary>
  24. public Texture2DAtlas TextureAtlas { get; }
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="SpriteSheet"/> class.
  27. /// </summary>
  28. /// <param name="name">The name of the sprite sheet.</param>
  29. /// <param name="textureAtlas">The texture atlas to use for the sprite sheet.</param>
  30. /// <exception cref="ArgumentNullException">Thrown when the <paramref name="textureAtlas"/> is <c>null</c>.</exception>
  31. public SpriteSheet(string name, Texture2DAtlas textureAtlas)
  32. {
  33. ArgumentNullException.ThrowIfNull(textureAtlas);
  34. TextureAtlas = textureAtlas;
  35. Name = name;
  36. }
  37. /// <summary>
  38. /// Creates a sprite from the specified region index.
  39. /// </summary>
  40. /// <param name="regionIndex">The index of the region in the texture atlas.</param>
  41. /// <returns>A new <see cref="Sprite"/> instance.</returns>
  42. public Sprite CreateSprite(int regionIndex) => TextureAtlas.CreateSprite(regionIndex);
  43. /// <summary>
  44. /// Creates a sprite from the specified region name.
  45. /// </summary>
  46. /// <param name="regionName">The name of the region in the texture atlas.</param>
  47. /// <returns>A new <see cref="Sprite"/> instance.</returns>
  48. public Sprite CreateSprite(string regionName) => TextureAtlas.CreateSprite(regionName);
  49. /// <summary>
  50. /// Defines a new animation for the sprite sheet.
  51. /// </summary>
  52. /// <param name="name">The name of the animation.</param>
  53. /// <param name="buildAction">The action to build the animation definition.</param>
  54. public void DefineAnimation(string name, Action<SpriteSheetAnimationBuilder> buildAction)
  55. {
  56. SpriteSheetAnimationBuilder builder = new SpriteSheetAnimationBuilder(name, this);
  57. buildAction(builder);
  58. SpriteSheetAnimation definition = builder.Build();
  59. _animations.Add(name, definition);
  60. }
  61. public SpriteSheetAnimation GetAnimation(string name) => _animations[name];
  62. /// <summary>
  63. /// Removes the animation definition with the specified name.
  64. /// </summary>
  65. /// <param name="name">The name of the animation to remove.</param>
  66. /// <returns><c>true</c> if the animation was successfully removed; otherwise, <c>false</c>.</returns>
  67. public bool RemoveAnimationDefinition(string name) => _animations.Remove(name);
  68. }