1using System.Collections;
2using System.Runtime.Serialization;
20 [JsonProperty]
private List<Frame> frameList;
22 [JsonProperty]
private List<double?>? frameDurations;
28 frameDurations is not
null && index < frameDurations.Count ? frameDurations[index] :
null;
36 throw new ArgumentOutOfRangeException(nameof(index));
38 if (seconds is { } duration && (!
double.IsFinite(duration) || duration <= 0))
39 throw new ArgumentOutOfRangeException(nameof(seconds));
42 frameDurations = frameDurations is
null ?
new List<double?>() :
new(frameDurations);
45 frameDurations.Add(
null);
47 frameDurations[index] = seconds;
50 internal void SetDurations(IEnumerable<double?> durations)
52 var values = durations.ToList();
54 if (values.Count !=
FrameCount || values.Any(v => v is { } d && (!
double.IsFinite(d) || d <= 0)))
55 throw new ArgumentException(
56 "Durations must match the sequence and be positive finite values or null.",
59 frameDurations = values.Any(v => v.HasValue) ? values :
null;
62 private int currentFrameIdx;
63 private int curFrameIncrement;
64 private bool cycleFinished;
68 #region constructors / finalizer
76 frameList =
new List<Frame>();
80 curFrameIncrement = 1;
93 curFrameIncrement = 1;
98 private void OnDeserialized(StreamingContext context)
101 curFrameIncrement = 1;
102 cycleFinished =
true;
105 #endregion constructors / finalizer
115 get {
return cycleFinished; }
126 if (frameList ==
null)
129 return frameList.Count;
141 if (frameList ==
null)
144 return frameList[currentFrameIdx];
154 get {
return currentFrameIdx; }
163 get {
return frameList.AsReadOnly(); }
166 #endregion properties
168 #region public methods
216 if (frameList ==
null)
219 frameList.Add(frame);
229 if (idx < frameList.Count)
231 frameList.RemoveAt(idx);
233 if (frameDurations is not
null && idx < frameDurations.Count)
235 frameDurations =
new(frameDurations);
236 frameDurations.RemoveAt(idx);
247 curFrameIncrement = 1;
250 #endregion public methods
252 #region internal methods
254 internal void StopCycle()
256 cycleFinished =
true;
259 internal Frame AdvanceFrame()
264 currentFrameIdx += curFrameIncrement;
266 if ((currentFrameIdx <= 0) || (currentFrameIdx >= frameList.Count - 1))
267 curFrameIncrement *= -1;
269 if (currentFrameIdx < 0)
272 if (currentFrameIdx > frameList.Count - 1)
273 currentFrameIdx = frameList.Count - 1;
275 cycleFinished =
false;
279 if (++currentFrameIdx >= frameList.Count)
282 cycleFinished =
false;
286 if (++currentFrameIdx > frameList.Count - 1)
288 currentFrameIdx = frameList.Count - 1;
289 cycleFinished =
true;
292 cycleFinished =
false;
297 throw new InvalidOperationException(
"Invalid CycleType: " +
SequenceCycleType.ToString());
300 return frameList[currentFrameIdx];
303 #endregion internal methods
305 #region private methods
307 private void SetDefaults()
309 frameList =
new List<Frame>();
312 curFrameIncrement = 1;
313 cycleFinished =
true;
316 #endregion private methods
327 get {
return frameList[frameIdx]; }
332 #region IEnumerable Members
340 for (
int i = 0; i < frameList.Count; i++)
341 yield
return frameList[i];
348 IEnumerator<Frame> IEnumerable<Frame>.GetEnumerator()
350 return frameList.GetEnumerator();
353 #endregion IEnumerable Members
Represents a rectangular region within a tilesheet that contains a grid of frames.
static readonly string DefaultRegionName
The default name assigned to tilesheet regions when no name is specified.
Represents a tilesheet image and metadata for rendering tiles.
CycleType
Simple is self-terminating; the other two are repeating.
double? GetDurationSeconds(int index)
Gets the optional display duration for a frame occurrence.
void RemoveFrame(int idx)
Removes the frame at the specified index from the sequence.
Frame AddFrame(Frame frame)
Adds an existing frame to the sequence.
Frame AddFrame(Tilesheet bmp, int xTile, int yTile)
Creates and adds a new frame to the sequence using tilesheet coordinates.
void Reset()
Resets the sequence to its initial state, starting from the first frame.
IList< Frame > FrameList
Gets a read-only view of the list of frames in the sequence.
Frame CurrentFrame
Gets the frame currently active in the animation sequence.
FrameSequence(Frame frame)
Initializes a new instance of the FrameSequence struct with a single frame.
int FrameCount
Gets the total number of frames in the sequence.
CycleType SequenceCycleType
The type of cycle pattern used when animating through the frame sequence.
void SetDurationSeconds(int index, double? seconds)
Sets a positive display duration, or null to use the cycle default.
Frame AddFrame(Tilesheet bmp, string regionName, int xTile, int yTile)
Creates and adds a new frame to the sequence using tilesheet coordinates.
FrameSequence(List< Frame > frames)
Initializes a new instance of the FrameSequence struct with a collection of frames.
int CurrentFrameIdx
Gets the zero-based index of the current frame in the sequence.
IEnumerator GetEnumerator()
Returns an enumerator that iterates through the frame sequence.
bool CycleFinished
Gets a value indicating whether the animation cycle has finished playing through the sequence.
Represents the source Tilesheet and its coordinates to render on a destination.