Client LuaCsForBarotrauma
BarotraumaShared/SharedSource/Sprite/SpriteSheet.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Xml.Linq;
4 
5 namespace Barotrauma
6 {
7  public partial class SpriteSheet : Sprite
8  {
9  private Rectangle[] sourceRects;
10  private int emptyFrames;
11 
12  public int FrameCount
13  {
14  get { return sourceRects.Length - emptyFrames; }
15  }
16 
17  public Point FrameSize
18  {
19  get;
20  private set;
21  }
22 
23  public SpriteSheet(ContentXElement element, string path = "", string file = "")
24  : base(element, path, file)
25  {
26  int columnCount = Math.Max(element.GetAttributeInt("columns", 1), 1);
27  int rowCount = Math.Max(element.GetAttributeInt("rows", 1), 1);
28  origin = element.GetAttributeVector2("origin", new Vector2(0.5f, 0.5f));
29  emptyFrames = element.GetAttributeInt("emptyframes", 0);
30  Init(columnCount, rowCount);
31  }
32 
33  public SpriteSheet(string filePath, int columnCount, int rowCount, Vector2 origin, Rectangle? sourceRect = null)
34  : base(filePath, origin)
35  {
36  this.origin = origin;
37  if (sourceRect.HasValue) { SourceRect = sourceRect.Value; }
38  Init(columnCount, rowCount);
39  }
40 
41  private void Init(int columnCount, int rowCount)
42  {
43  sourceRects = new Rectangle[rowCount * columnCount];
44 
45  float cellWidth = SourceRect.Width / columnCount;
46  float cellHeight = SourceRect.Height / rowCount;
47  FrameSize = new Point((int)cellWidth, (int)cellHeight);
48 
49  for (int x = 0; x < columnCount; x++)
50  {
51  for (int y = 0; y < rowCount; y++)
52  {
53  sourceRects[x + y * columnCount] = new Rectangle((int)(SourceRect.X + x * cellWidth), (int)(SourceRect.Y + y * cellHeight), (int)cellWidth, (int)cellHeight);
54  }
55  }
56 
57  origin.X = origin.X * cellWidth;
58  origin.Y = origin.Y * cellHeight;
59  }
60  }
61 }
Vector2 GetAttributeVector2(string key, in Vector2 def)
int GetAttributeInt(string key, int def)
SpriteSheet(ContentXElement element, string path="", string file="")
SpriteSheet(string filePath, int columnCount, int rowCount, Vector2 origin, Rectangle? sourceRect=null)