-
Notifications
You must be signed in to change notification settings - Fork 0
/
CModelController.cpp
executable file
·320 lines (263 loc) · 8.01 KB
/
CModelController.cpp
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
/*
CModelController.cpp
Author: Tom Naughton
Description: <describe the CModelController class here>
*/
#include <glut.h>
#include "CModelController.h"
#include "AppConstants.h"
#include "CPakRatApp.h"
#include "CPreferences.h"
#include "utilities.h"
CModelController::CModelController(CModelInstance *inModel)
{
_model = inModel;
_animating = true;
_animations = new vector<Sequence*>;
_animationSequence = nil;
init();
}
CModelController::~CModelController()
{
if (_animations)
delete _animations;
}
void CModelController::Reset()
{
}
Boolean CModelController::init()
{
// find all the animation sequences
UInt32 frames = _model->modelClass()->frameCount();
Sequence *currSequence = nil;
for (int i = 0; i < frames; i++) {
string name = _model->modelClass()->frameName(i);
// index can be one or two digits
int digits = 1;
if (name[name.length()-2] >= '0' && name[name.length()-2] <= '9')
digits = 2;
string sequence = name.substr(0,name.length()-digits );
string index = name.substr(name.length()-digits ,name.length());
// continue or end
if (currSequence != nil) {
if (sequence != currSequence->name) {
_animations->insert(_animations->end(), currSequence);
currSequence = nil;
} else {
currSequence->endIndex++;
}
}
// start new
if (currSequence == nil) {
currSequence = new Sequence();
currSequence->name = sequence;
currSequence->startIndex = i;
currSequence->endIndex = i;
}
}
// make sure the last one gets in the vector
if (currSequence != nil) {
_animations->insert(_animations->end(), currSequence);
}
for(int i = 0; i < _animations->size(); i++) {
currSequence = (*_animations)[i];
dprintf("%s %d %d\n", currSequence->name.c_str(), currSequence->startIndex, currSequence->endIndex);
}
StartSequenceAtIndex(0);
return true;
}
void CModelController::appendAnimationNames(StringList *result)
{
if (_animations) {
vector<Sequence*>::iterator e = _animations->begin();
while (e != _animations->end()) {
result->push_back((*e)->name);
e++;
}
}
}
int CModelController::animationCount()
{
return _animations->size();
}
void CModelController::Draw(renderingAttributes_t *renderingAttributes)
{
if (renderingAttributes->_wireframe)
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
else
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
if (renderingAttributes->_lighting)
glEnable( GL_LIGHTING );
else
glDisable( GL_LIGHTING );
if (renderingAttributes->_textured)
glEnable( GL_TEXTURE_2D );
else
glDisable( GL_TEXTURE_2D );
glColor4f(1,1,1,1);
_model->Draw(renderingAttributes);
_needsRedraw = false;
// draw links after the blend phase
if (renderingAttributes->_pickTag && renderingAttributes->_renderBlend) {
_model->DrawLinks(renderingAttributes);
}
}
void CModelController::BeginSequence(Sequence *sequence)
{
_theTime = (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0;
if (_model) {
_animationSequence = sequence;
if (_animationQueue.queueLength > 1)
_animationQueue.queueLength = 1;
_animationQueue.startTime = _theTime;
_animationQueue.PushSequence(sequence->startIndex, sequence->endIndex - sequence->startIndex + 1);
}
}
const char *CModelController::DisplayString()
{
static string message;
message = _model->modelClass()->frameName(_animationQueue.currentFrame);
return message.c_str();
}
void CModelController::NextFrame(Sequence *sequence)
{
float elapsedTime = _theTime - _animationQueue.startTime;
float frame = elapsedTime * 10; // fps
SInt16 intFrame = (SInt16)frame;
// haven't been here in a while, just continue from where we left off
if (intFrame > ANIMATION_MAX_LOST_FRAMES) {
frame = intFrame = 0;
_animationQueue.startTime = _theTime;
}
// loop animation
int loopCount = 0;
while (_animationQueue.queueLength < intFrame + 2 && loopCount < 50) {
_animationQueue.PushSequence(sequence->startIndex, sequence->endIndex - sequence->startIndex + 1);
loopCount++;
}
// skip to current frame
if (intFrame) {
_animationQueue.PopFrames(intFrame);
_animationQueue.startTime = _theTime;
}
_animationQueue.currentFrame = _animationQueue.PeekFrame(0);
_animationQueue.nextFrame = _animationQueue.PeekFrame(1);
_animationQueue.interpolation = frame - intFrame;
if (!gApplication->preferences()->booleanForKey("useInterpolation"))
_animationQueue.interpolation = 0.0;
if (intFrame != 0 || _animationQueue.interpolation != 0.0)
_needsRedraw = true;
_model->setFrame(
_animationQueue.currentFrame,
_animationQueue.nextFrame,
_animationQueue.interpolation);
}
Boolean CModelController::Animate()
{
_theTime = (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0;
if (_animating && _animationSequence) {
NextFrame(_animationSequence);
}
return _needsRedraw;
}
void CModelController::StartSequenceAtIndex(int index)
{
if (_animations->size() > 0) {
Sequence *sequence = (*_animations)[index];
if (sequence) {
_animating = true;
BeginSequence(sequence);
}
}
}
// ---------------------------------------------------------------------------
// ¥ HandleKeyPress [public]
// ---------------------------------------------------------------------------
// Tab switches the Target to the next item in the TabGroup.
// Shift-Tab to the previous item.
// All other keystrokes (and Tabs with modifiers other than Shift)
// get passed up.
Boolean CModelController::HandleKeyPress(const EventRecord &inKeyEvent)
{
CPreferences *prefs = gApplication->preferences();
string command = prefs->commandForKeyEvent(inKeyEvent);
SInt16 currentFrame, nextFrame;
float interpolation;
_needsRedraw = true;
int key = animationIndexForKeyEvent(inKeyEvent);
if (key >= 0 && key < (*_animations).size()) {
StartSequenceAtIndex(key);
return true;
}
// the key mapped to anything?
if (command.length() == 0)
return false;
if (command == "toggleAnimation") {
_animating = !_animating;
return true;
} else if (command == "toggleInterpolation") {
Boolean bonebox = prefs->booleanForKey("useInterpolation");
prefs->setBooleanForKey(!bonebox, "useInterpolation");
return true;
} else if (command == "nextSkin") {
int index = _model->getTextureIndex();
if (index < _model->modelClass()->textureCount()-1)
index++;
else
index = 0;
_model->setTextureIndex(index);
return true;
} else if (command == "previousSkin") {
int index = _model->getTextureIndex();
if (index > 0)
index--;
else
index = _model->modelClass()->textureCount()-1;
_model->setTextureIndex(index);
return true;
}
// animation commands
if (command == "incrementLower") {
_animating = false;
_model->getFrame(currentFrame, nextFrame, interpolation);
_model->setFrame(currentFrame + 1, currentFrame, 0.0);
} else if (command == "decrementLower") {
_animating = false;
_model->getFrame(currentFrame, nextFrame, interpolation);
_model->setFrame(currentFrame - 1, currentFrame, 0.0);
}
return false;
}
int CModelController::animationIndexForKeyEvent(const EventRecord &inKeyEvent)
{
// FIXME - non querty keyboards?
UInt16 keyLookup[] = {
0xc00, 0xd00, 0xe00, 0xf00, 0x1100, 0x1000, 0x2000, 0x2200, 0x1f00, 0x2300, // qwertyuiop
0x0, 0x100, 0x200, 0x300, 0x500, 0x400, 0x2600, 0x2800, 0x2500, // asdfghjkl
0x600, 0x700, 0x800, 0x900, 0xb00, 0x2d00, 0x2e00, // zxcvbnm
0xFFFF, // Sentinel
};
UInt8 theChar = (UInt16) (inKeyEvent.message & charCodeMask);
UInt16 theKey = (UInt16) (inKeyEvent.message & keyCodeMask);
Boolean commandDown = (inKeyEvent.modifiers & cmdKey) != 0;
Boolean controlDown = (inKeyEvent.modifiers & controlKey) != 0;
Boolean shiftDown = (inKeyEvent.modifiers & shiftKey) != 0;
Boolean optionDown = (inKeyEvent.modifiers & optionKey) != 0;
int index = 0;
dprintf("theChar 0x%x '%c' theKey 0x%x\n", theChar, theChar, theKey);
while (keyLookup[index] != 0xFFFF) {
if (keyLookup[index] == theKey) {
if (shiftDown)
index += 26;
if (controlDown)
index += 26*2;
if (optionDown)
index += 26*4;
// if (commandDown)
// index += 26*7;
return index;
}
index++;
}
return -1;
}