-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathUndertaleAnimationCurve.cs
More file actions
299 lines (259 loc) · 10.2 KB
/
UndertaleAnimationCurve.cs
File metadata and controls
299 lines (259 loc) · 10.2 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using System;
namespace UndertaleModLib.Models;
/// <summary>
/// An animation curve entry in a data file. These were introduced in GameMaker 2.3.0
/// </summary>
[PropertyChanged.AddINotifyPropertyChangedInterface]
public class UndertaleAnimationCurve : UndertaleNamedResource, IDisposable
{
/// <summary>
/// Unused enum: The curve type is set per channel unlike in GMS2 where it is set per animation curve
/// </summary>
public enum GraphTypeEnum : uint
{
/// <summary>
/// Value is never set by the compiler
/// </summary>
Bezier = 0,
/// <summary>
/// Value is always set by the compiler
/// </summary>
Graph = 1
}
/// <summary>
/// The name of this animation curve.
/// </summary>
public UndertaleString Name { get; set; }
/// <summary>
/// The graph type of this animation curve.
/// </summary>
public GraphTypeEnum GraphType { get; set; }
/// <summary>
/// The channels this animation curve has.
/// </summary>
public UndertaleSimpleList<Channel> Channels { get; set; } = new UndertaleSimpleList<Channel>();
/// <inheritdoc />
public void Serialize(UndertaleWriter writer)
{
Serialize(writer, true);
}
/// <summary>
/// Serializes the data file into a specified <see cref="UndertaleWriter"/>.
/// </summary>
/// <param name="writer">Where to serialize to.</param>
/// <param name="includeName">Whether to include <see cref="Name"/> in the serialization.</param>
public void Serialize(UndertaleWriter writer, bool includeName)
{
if (includeName)
writer.WriteUndertaleString(Name);
writer.Write((uint)GraphType);
Channels.Serialize(writer);
}
/// <inheritdoc />
public void Unserialize(UndertaleReader reader)
{
Unserialize(reader, true);
}
/// <summary>
/// Deserializes from a specified <see cref="UndertaleReader"/> to the current data file.
/// </summary>
/// <param name="reader">Where to deserialize from.</param>
/// <param name="includeName">Whether to include <see cref="Name"/> in the deserialization.</param>
public void Unserialize(UndertaleReader reader, bool includeName)
{
if (includeName)
Name = reader.ReadUndertaleString();
GraphType = (GraphTypeEnum)reader.ReadUInt32();
Channels = reader.ReadUndertaleObject<UndertaleSimpleList<Channel>>();
}
/// <inheritdoc cref="UndertaleObject.UnserializeChildObjectCount(UndertaleReader)"/>
public static uint UnserializeChildObjectCount(UndertaleReader reader)
{
return UnserializeChildObjectCount(reader, true);
}
/// <inheritdoc cref="UndertaleObject.UnserializeChildObjectCount(UndertaleReader)"/>
/// <param name="reader">Where to deserialize from.</param>
/// <param name="includeName">Whether to include <see cref="Name"/> in the deserialization.</param>
public static uint UnserializeChildObjectCount(UndertaleReader reader, bool includeName)
{
if (!includeName)
reader.Position += 4; // "GraphType"
else
reader.Position += 4 + 4; // + "Name"
return 1 + UndertaleSimpleList<Channel>.UnserializeChildObjectCount(reader);
}
/// <inheritdoc />
public override string ToString()
{
return Name?.Content;
}
/// <inheritdoc/>
public void Dispose()
{
GC.SuppressFinalize(this);
if (Channels is not null)
{
foreach (Channel channel in Channels)
channel?.Dispose();
}
Name = null;
Channels = null;
}
/// <summary>
/// A channel in an animation curve.
/// </summary>
[PropertyChanged.AddINotifyPropertyChangedInterface]
public class Channel : UndertaleNamedResource, IDisposable
{
/// <summary>
/// The curve type determines how points flow to each other in a channel.
/// </summary>
public enum CurveType : uint
{
/// <summary>
/// Creates a linear progression between points.
/// </summary>
Linear = 0,
/// <summary>
/// Creates a smooth progression between points using catmull-rom interpolation.
/// </summary>
Smooth = 1,
/// <summary>
/// Creates a curved progression by using points as the control points of a 2D Bézier curve.
/// </summary>
Bezier = 2
}
/// <inheritdoc />
public UndertaleString Name { get; set; }
/// <summary>
/// The curve type this channel uses.
/// </summary>
public CurveType Curve { get; set; }
/// <summary>
/// The amount of resolution generated between control points in both Catmull-Rom (smooth) and Bezier interpolation.
/// </summary>
public uint Iterations { get; set; }
/// <summary>
/// The points in the channel.
/// </summary>
public UndertaleSimpleList<Point> Points { get; set; } = new UndertaleSimpleList<Point>();
/// <inheritdoc />
public void Serialize(UndertaleWriter writer)
{
writer.WriteUndertaleString(Name);
writer.Write((uint)Curve);
writer.Write(Iterations);
Points.Serialize(writer);
}
/// <inheritdoc />
public void Unserialize(UndertaleReader reader)
{
Name = reader.ReadUndertaleString();
Curve = (CurveType)reader.ReadUInt32();
Iterations = reader.ReadUInt32();
Points = reader.ReadUndertaleObject<UndertaleSimpleList<Point>>();
}
/// <inheritdoc cref="UndertaleObject.UnserializeChildObjectCount(UndertaleReader)"/>
public static uint UnserializeChildObjectCount(UndertaleReader reader)
{
reader.Position += 12;
// Read the number of points in the curve
uint pointCount = reader.ReadUInt32();
// This check is partly duplicated from UndertaleChunks.cs, but it's necessary to handle embedded curves
// (For example, those in SEQN in the TS!Underswap v1.0 demo; see issue #1414)
if (!reader.undertaleData.IsVersionAtLeast(2, 3, 1))
{
long returnPosition = reader.AbsPosition;
if (pointCount > 0)
{
reader.AbsPosition += 8;
if (reader.ReadUInt32() != 0) // In 2.3 an int with the value of 0 would be set here,
{ // It cannot be version 2.3 if this value isn't 0
reader.undertaleData.SetGMS2Version(2, 3, 1);
}
else
{
if (reader.ReadUInt32() == 0) // At all points (besides the first one)
reader.undertaleData.SetGMS2Version(2, 3, 1); // If BezierX0 equals to 0 (the above check)
// Then BezierY0 equals to 0 as well (the current check)
}
}
reader.AbsPosition = returnPosition;
}
// "Points"
if (reader.undertaleData.IsVersionAtLeast(2, 3, 1))
reader.Position += 24 * pointCount;
else
reader.Position += 12 * pointCount;
return 1 + pointCount;
}
/// <inheritdoc/>
public void Dispose()
{
GC.SuppressFinalize(this);
Name = null;
Points = null;
}
/// <summary>
/// A point which can exist on a <see cref="Channel"/>.
/// </summary>
[PropertyChanged.AddINotifyPropertyChangedInterface]
public class Point : UndertaleObject
{
/// <summary>
/// The X coordinate of this point. GameMaker abbreviates this to "h".
/// </summary>
public float X { get; set; }
/// <summary>
/// The Y coordinate of this point. GameMaker abbreviates this to "v".
/// </summary>
public float Value { get; set; }
/// <summary>
/// The Y position for the first bezier handle. Only used if the Channel is set to Bezier.
/// </summary>
public float BezierX0 { get; set; }
/// <summary>
/// The Y position for the first bezier handle. Only used if the Channel is set to Bezier.
/// </summary>
public float BezierY0 { get; set; }
/// <summary>
/// The X position for the second bezier handle. Only used if the Channel is set to Bezier.
/// </summary>
public float BezierX1 { get; set; }
/// <summary>
/// The Y position for the second bezier handle. Only used if the Channel is set to Bezier.
/// </summary>
public float BezierY1 { get; set; }
/// <inheritdoc />
public void Serialize(UndertaleWriter writer)
{
writer.Write(X);
writer.Write(Value);
if (writer.undertaleData.IsVersionAtLeast(2, 3, 1))
{
writer.Write(BezierX0);
writer.Write(BezierY0);
writer.Write(BezierX1);
writer.Write(BezierY1);
}
else
writer.Write(0);
}
/// <inheritdoc />
public void Unserialize(UndertaleReader reader)
{
X = reader.ReadSingle();
Value = reader.ReadSingle();
if (reader.undertaleData.IsVersionAtLeast(2, 3, 1))
{
BezierX0 = reader.ReadSingle();
BezierY0 = reader.ReadSingle();
BezierX1 = reader.ReadSingle();
BezierY1 = reader.ReadSingle();
}
else
reader.Position += 4;
}
}
}
}