This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TextureGIF.cs
209 lines (178 loc) · 7.58 KB
/
TextureGIF.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ProjectStarlight.Interchange.Utilities;
namespace ProjectStarlight.Interchange
{
/// <summary>
/// Simple class containing GIF data for drawing and updating.
/// </summary>
public class TextureGIF
{
/// <summary>
/// The width of the GIF.
/// </summary>
public int Width { get; private set; }
/// <summary>
/// The height of the GIF.
/// </summary>
public int Height { get; private set; }
/// <summary>
/// Whether the GIF is paused.
/// </summary>
public bool IsPaused { get; private set; }
/// <summary>
/// Whether the GIF has ended. Never true of the GIF loops (<seealso cref="ShouldLoop"/>).
/// </summary>
public bool HasEnded => FrameIndex >= Frames.Length && FrameTick >= TicksPerFrame && !ShouldLoop;
/// <summary>
/// Whether the GIF loops. Prevents the GIF from ending unless <seealso cref="Stop"/> is called.
/// </summary>
public bool ShouldLoop { get; set; }
/// <summary>
/// The amount of ticks per frame. Once the tick threshold is reached, goes to a new frame.
/// </summary>
public int TicksPerFrame { get; set; }
/// <summary>
/// The current tick the frame is on.
/// </summary>
public int FrameTick { get; private set; }
/// <summary>
/// The index of <seealso cref="Frames"/> that should be drawn.
/// </summary>
public int FrameIndex { get; private set; }
/// <summary>
/// An array of <seealso cref="Texture2D"/>s representing the frames of a GIF.
/// </summary>
public Texture2D[] Frames { get; private set; }
/// <summary>
/// Gets the current frame in accordance to <seealso cref="Frames"/>, using <seealso cref="FrameIndex"/> as the index.
/// </summary>
public Texture2D CurrentFrame => HasEnded ? Frames.Last() : Frames[FrameIndex];
/// <summary>
/// Creates a <see cref="TextureGIF"/> instance from an array of <see cref="Texture2D"/>s, where the <see cref="Texture2D"/>s serve as frames.
/// </summary>
internal TextureGIF(Texture2D[] frames, int ticksPerFrame)
{
FrameUtilities.VerifyGIFDimensions(frames, out int width, out int height);
Width = width;
Height = height;
TicksPerFrame = ticksPerFrame;
FrameIndex = 0;
Frames = frames;
}
/// <summary>
/// Restarts (or officially starts, if not started previously) the GIF. Render the GIF with <seealso cref="Draw(SpriteBatch, Vector2, Color)"/> or one of its overloads.
/// </summary>
public void Play()
{
FrameTick = 0;
FrameIndex = 0;
if (IsPaused)
SwitchPauseState();
}
/// <summary>
/// Completely stops a GIF. If you want to pause a GIF, use <see cref="SwitchPauseState"/>.
/// </summary>
public void Stop()
{
// This essentially sets HasEnded to true.
FrameTick = Frames.Length - 1;
FrameTick = TicksPerFrame;
ShouldLoop = false;
}
/// <summary>
/// Attempts to change the array of <see cref="Texture2D"/>s to draw.
/// </summary>
/// <param name="frames">The <see cref="Texture2D"/>s to set.</param>
/// <returns>Returns <c>true</c> if successful, otherwise <c>false</c>.</returns>
public bool TryChangeGIF(Texture2D[] frames)
{
if (FrameIndex >= frames.Length)
return false;
try
{
FrameUtilities.VerifyGIFDimensions(frames, out int width, out int height);
Width = width;
Height = height;
}
catch
{
return false;
}
return true;
}
/// <summary>
/// Pauses and unpauses the GIF depending on its current state.
/// </summary>
public void SwitchPauseState() =>
// TODO: Find out if there's anything that needs to be done when pausing and unpausing.
IsPaused = !IsPaused;
/// <summary>
/// Draws the current GIF frame.
/// </summary>
public void Draw(SpriteBatch spriteBatch, Vector2 position, Color color) =>
spriteBatch.Draw(CurrentFrame, position, color);
/// <summary>
/// Draws the current GIF frame.
/// </summary>
public void Draw(SpriteBatch spriteBatch, Vector2 position, Rectangle? sourceRectangle, Color color) =>
spriteBatch.Draw(CurrentFrame, position, sourceRectangle, color);
/// <summary>
/// Draws the current GIF frame.
/// </summary>
public void Draw(SpriteBatch spriteBatch, Vector2 position, Rectangle? sourceRectangle, Color color,
float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) =>
spriteBatch.Draw(CurrentFrame, position, sourceRectangle, color, rotation, origin, scale, effects,
layerDepth);
/// <summary>
/// Draws the current GIF frame.
/// </summary>
public void Draw(SpriteBatch spriteBatch, Vector2 position, Rectangle? sourceRectangle, Color color,
float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) =>
spriteBatch.Draw(CurrentFrame, position, sourceRectangle, color, rotation, origin, scale, effects,
layerDepth);
/// <summary>
/// Draws the current GIF frame.
/// </summary>
public void Draw(SpriteBatch spriteBatch, Rectangle destinationRectangle, Color color) =>
spriteBatch.Draw(CurrentFrame, destinationRectangle, color);
/// <summary>
/// Draws the current GIF frame.
/// </summary>
public void Draw(SpriteBatch spriteBatch, Rectangle destinationRectangle, Rectangle? sourceRectangle,
Color color) => spriteBatch.Draw(CurrentFrame, destinationRectangle, sourceRectangle, color);
/// <summary>
/// Draws the current GIF frame.
/// </summary>
public void Draw(SpriteBatch spriteBatch, Rectangle destinationRectangle, Rectangle? sourceRectangle,
Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth) =>
spriteBatch.Draw(CurrentFrame, destinationRectangle, sourceRectangle, color, rotation, origin, effects,
layerDepth);
/// <summary>
/// Increment ticks by one if the GIF is not paused and has not ended.
/// </summary>
public void UpdateGIF()
{
if (!IsPaused && !HasEnded)
ForwardTicks(1);
}
/// <summary>
/// Increments the specified amount of ticks. You can use this to skip frames as well.
/// </summary>
public void ForwardTicks(int tickAmount)
{
for (int i = 0; i < tickAmount; i++)
{
FrameTick++;
if (FrameTick < TicksPerFrame)
return;
FrameTick = 0;
if (FrameIndex < Frames.Length - 1)
FrameIndex++;
else if (ShouldLoop)
FrameIndex = 0;
}
}
}
}