forked from Gota7/SM64DSe-Ultimate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimationEditorForm.cs
383 lines (321 loc) · 13.4 KB
/
AnimationEditorForm.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using SM64DSe.ImportExport;
namespace SM64DSe
{
public partial class AnimationEditorForm : Form
{
private BMD m_BMD;
private BCA m_BCA;
private System.Windows.Forms.Timer m_AnimationTimer;
private int m_AnimationFrameNumber = 0;
private int m_AnimationNumFrames = -1;
private bool m_LoopAnimation = true;
private bool m_Running = false;
private float m_ModelPreviewScale = 1f;
private int[] m_DisplayLists = new int[1];
private BMDImporter.BCAImportationOptions m_BCAImportationOptions;
private ROMFileSelect m_ROMFileSelect = new ROMFileSelect();
public AnimationEditorForm()
{
InitializeComponent();
InitTimer();
glModelView.Initialise();
glModelView.ProvideDisplayLists(m_DisplayLists);
m_BCAImportationOptions = BMDImporter.BCAImportationOptions.DEFAULT;
}
private void PrerenderModel()
{
if (m_DisplayLists[0] == 0)
{
m_DisplayLists[0] = GL.GenLists(1);
}
GL.NewList(m_DisplayLists[0], ListMode.Compile);
GL.FrontFace(FrontFaceDirection.Ccw);
GL.Enable(EnableCap.Lighting);
GL.Light(LightName.Light0, LightParameter.Position, new Vector4(1.0f, 1.0f, 1.0f, 0.0f));
GL.Light(LightName.Light0, LightParameter.Ambient, Color.White);
GL.Light(LightName.Light0, LightParameter.Diffuse, Color.White);
GL.Light(LightName.Light0, LightParameter.Specular, Color.White);
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.PushMatrix();
GL.Scale(1f, 1f, 1f);
GL.FrontFace(FrontFaceDirection.Ccw);
m_BMD.PrepareToRender();
int[] dl = new int[2];
dl[0] = GL.GenLists(1);
GL.NewList(dl[0], ListMode.Compile);
m_BMD.Render(RenderMode.Opaque, m_ModelPreviewScale, m_BCA, m_AnimationFrameNumber);
//GL.EndList();
dl[1] = GL.GenLists(1);
GL.NewList(dl[1], ListMode.Compile);
m_BMD.Render(RenderMode.Translucent, m_ModelPreviewScale, m_BCA, m_AnimationFrameNumber);
GL.EndList();
GL.PopMatrix();
glModelView.Refresh();
}
private void InitTimer()
{
m_AnimationTimer = new System.Windows.Forms.Timer();
m_AnimationTimer.Interval = (int)(1000f / 30f);
m_AnimationTimer.Tick += new EventHandler(m_AnimationTimer_Tick);
}
private void StartTimer()
{
m_AnimationFrameNumber = 0;
m_AnimationTimer.Start();
m_Running = true;
}
private void StopTimer()
{
m_AnimationTimer.Stop();
m_Running = false;
}
private void m_AnimationTimer_Tick(object sender, EventArgs e)
{
if (m_AnimationFrameNumber < m_AnimationNumFrames - 1)
{
IncrementFrame();
}
else
{
StopTimer();
if (m_LoopAnimation)
{
StartTimer();
}
}
}
private void IncrementFrame()
{
if (m_AnimationFrameNumber < m_AnimationNumFrames - 1)
SetFrame(++m_AnimationFrameNumber);
else
SetFrame(0);
}
private void SetFrame(int frame)
{
m_AnimationFrameNumber = frame;
PrerenderModel();
txtCurrentFrameNum.Text = "" + m_AnimationFrameNumber;
}
private void btnOpenBMD_Click(object sender, EventArgs e)
{
m_ROMFileSelect.ReInitialize("Please select a model (BMD) file to open.", new String[] { ".bmd" });
var result = m_ROMFileSelect.ShowDialog();
if (result == DialogResult.OK)
{
StopTimer();
m_BMD = new BMD(Program.m_ROM.GetFileFromName(m_ROMFileSelect.m_SelectedFile));
txtBMDName.Text = m_BMD.m_FileName;
PrerenderModel();
}
}
private void btnOpenBCA_Click(object sender, EventArgs e)
{
bool wasRunning = m_Running;
String startFolder = "";
if (m_BMD != null)
{
startFolder = Helper.getDirectory(m_BMD.m_FileName);
}
m_ROMFileSelect.ReInitialize("Please select an animation (BCA) file to open.", new String[] { ".bca" }, startFolder);
var result = m_ROMFileSelect.ShowDialog();
if (result == DialogResult.OK)
{
StopTimer();
m_BCA = new BCA(Program.m_ROM.GetFileFromName(m_ROMFileSelect.m_SelectedFile));
txtBCAName.Text = m_BCA.m_FileName;
m_AnimationFrameNumber = 0;
m_AnimationNumFrames = m_BCA.m_NumFrames;
txtCurrentFrameNum.Text = "" + m_AnimationFrameNumber;
txtNumFrames.Text = "" + (m_BCA.m_NumFrames - 1);
if (wasRunning)
{
StartTimer();
}
}
}
private void btnPlayAnimation_Click(object sender, EventArgs e)
{
if (m_BMD == null || m_BCA == null)
return;
StartTimer();
}
private void chkLoopAnimation_CheckedChanged(object sender, EventArgs e)
{
m_LoopAnimation = !m_LoopAnimation;
}
private void btnStopAnimation_Click(object sender, EventArgs e)
{
StopTimer();
}
private void btnFirstFrame_Click(object sender, EventArgs e)
{
SetFrame(0);
}
private void btnLastFrame_Click(object sender, EventArgs e)
{
SetFrame(m_AnimationNumFrames - 1);
}
private void btnPreviousFrame_Click(object sender, EventArgs e)
{
if (m_AnimationFrameNumber > 0)
SetFrame(--m_AnimationFrameNumber);
else
SetFrame((m_AnimationFrameNumber = m_AnimationNumFrames - 1));
}
private void btnNextFrame_Click(object sender, EventArgs e)
{
if (m_AnimationFrameNumber == m_AnimationNumFrames - 1 && !m_LoopAnimation)
return;
else
IncrementFrame();
}
private void btnExportToDAE_Click(object sender, EventArgs e)
{
if (m_BMD == null)
return;
SaveFileDialog saveModel = new SaveFileDialog();
saveModel.FileName = "SM64DS_Animated_Model_" +
m_BMD.m_FileName.Substring(m_BMD.m_FileName.LastIndexOf("/") + 1) + ".DAE";//Default name
saveModel.DefaultExt = ".dae";//Default file extension
saveModel.Filter = "COLLADA DAE (.dae)|*.dae";//Filter by .DAE
if (saveModel.ShowDialog() == DialogResult.Cancel)
return;
if (m_BCA != null)
BMD_BCA_KCLExporter.ExportAnimatedModel(new BMD(m_BMD.m_File), new BCA(m_BCA.m_File), saveModel.FileName);
else
BMD_BCA_KCLExporter.ExportBMDModel(new BMD(m_BMD.m_File), saveModel.FileName);
}
private void txtCurrentFrameNum_TextChanged(object sender, EventArgs e)
{
if (txtCurrentFrameNum.Text == null || txtCurrentFrameNum.Text.Equals(""))
return;
try
{
int goFrame = int.Parse(txtCurrentFrameNum.Text);
if (goFrame > -1 && goFrame < m_AnimationNumFrames - 1)
{
SetFrame(goFrame);
}
}
catch { }
}
private void btnSelectInputAnimation_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = Strings.MODEL_ANIMATION_FORMATS_FILTER;
if (ofd.ShowDialog() == DialogResult.OK)
{
txtInputAnimation.Text = ofd.FileName;
string modelFormat = ofd.FileName.Substring(ofd.FileName.Length - 3, 3).ToLower();
if (modelFormat.Equals("dae"))
txtInputModel.Text = ofd.FileName;
}
}
private void btnSelectInputModel_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = Strings.MODEL_FORMATS_FILTER;
if (ofd.ShowDialog() == DialogResult.OK)
{
txtInputModel.Text = ofd.FileName;
}
}
private void chkOptimise_CheckedChanged(object sender, EventArgs e)
{
m_BCAImportationOptions.m_Optimise = chkOptimise.Checked;
}
private void btnImportAnimation_Click(object sender, EventArgs e)
{
if (m_BMD == null || m_BCA == null)
{
MessageBox.Show("Please select a valid model (BMD) and animation (BCA) to replace.");
return;
}
if (txtInputAnimation.Text == null || txtInputAnimation.Text.Equals(""))
{
MessageBox.Show("Please select an animation file or model to import.");
return;
}
float scale;
if (!Helper.TryParseFloat(txtScale.Text, out scale))
{
MessageBox.Show("Please enter a valid Scale value as a decimal value, eg. 1.234");
return;
}
string animationFormat = txtInputAnimation.Text.Substring(txtInputAnimation.Text.Length - 3).ToLowerInvariant();
bool wasRunning = m_Running;
StopTimer();
try
{
ModelBase loadedModel = null;
switch (animationFormat)
{
case "dae":
{
if (txtInputModel.Text != null && !txtInputModel.Text.Equals(""))
{
loadedModel = BMDImporter.LoadModel(txtInputAnimation.Text, scale);
m_BMD = BMDImporter.CallBMDWriter(ref m_BMD.m_File, loadedModel,
BMDImporter.BMDExtraImportOptions.DEFAULT, true);
}
// >>> TODO <<<
// Below line in necessary to an obscure bug with NARC files, if you have two file from the same
// NARC open and modify and save the first, when you then go to save the second, it won't have
// picked up the changes from the first file and when saved will write the original first file and
// the modified second file.
NitroFile animationFile = Program.m_ROM.GetFileFromName(m_BCA.m_FileName);
m_BCA = BMDImporter.ConvertAnimatedDAEToBCA(ref animationFile, txtInputAnimation.Text, m_BCAImportationOptions, true);
}
break;
case "ica":
{
if (txtInputModel.Text != null && !txtInputModel.Text.Equals(""))
{
loadedModel = BMDImporter.LoadModel(txtInputModel.Text, scale);
m_BMD = BMDImporter.CallBMDWriter(ref m_BMD.m_File, loadedModel,
BMDImporter.BMDExtraImportOptions.DEFAULT, true);
}
NitroFile animationFile = Program.m_ROM.GetFileFromName(m_BCA.m_FileName);
m_BCA = BMDImporter.ConvertICAToBCA(ref animationFile, txtInputAnimation.Text, loadedModel,
scale, BMDImporter.BMDExtraImportOptions.DEFAULT, m_BCAImportationOptions, true);
}
break;
}
m_AnimationFrameNumber = 0;
m_AnimationNumFrames = m_BCA.m_NumFrames;
txtCurrentFrameNum.Text = "" + m_AnimationFrameNumber;
txtNumFrames.Text = "" + (m_BCA.m_NumFrames - 1);
PrerenderModel();
if (wasRunning) StartTimer();
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: \n" + ex.Message + "\n\n" + ex.StackTrace);
m_BMD = new BMD(Program.m_ROM.GetFileFromName(m_BMD.m_FileName));
m_BCA = new BCA(Program.m_ROM.GetFileFromName(m_BCA.m_FileName));
}
}
private void AnimationEditorForm_FormClosing(object sender, FormClosingEventArgs e)
{
StopTimer();
}
private void txtModelPreviewScale_TextChanged(object sender, EventArgs e)
{
if (Helper.TryParseFloat(txtModelPreviewScale, out m_ModelPreviewScale))
{
PrerenderModel();
}
}
}
}