-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelerui.cpp
1654 lines (1369 loc) · 50.5 KB
/
modelerui.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cctype>
#include "modelerui.h"
#include "modelerdraw.h"
using namespace std;
// A special FLTK tiling window that keeps the bottom window the same size.
class Special_Tile : public Fl_Tile {
public:
Special_Tile(int x, int y, int w, int h, const char *L=0):
Fl_Tile(x,y,w,h,L){};
void resize(int x, int y, int w, int h) {
Fl_Group::resize(x, y, w, h);
}
};
// List of curve types for the dropdown menu.
// TODO: handle this in a more automatic fashion?
Fl_Menu_Item ModelerUserInterface::curveTypeMenu[] = {
{"Linear", 0, 0, 0, 0, 0, 0, 12, 0},
{"B-Spline", 0, 0, 0, 0, 0, 0, 12, 0},
{"Bezier", 0, 0, 0, 0, 0, 0, 12, 0},
{"Catmull-Rom", 0, 0, 0, 0, 0, 0, 12, 0},
{"C2-Interpolating", 0, 0, 0, 0, 0, 0, 12, 0},
{0}
};
ModelerUserInterface* ModelerUserInterface::instance;
void ModelerUserInterface::cb_m_controlsWindow_i(Fl_Double_Window* o, void*) {
ModelerUserInterface* pUI = (ModelerUserInterface*)(o->user_data());
if(Fl::e_number == FL_CLOSE) {
pUI->m_controlsWindow->hide();
};
}
void ModelerUserInterface::cb_m_controlsWindow(Fl_Double_Window* o, void* v) {
((ModelerUserInterface*)(o->user_data()))->cb_m_controlsWindow_i(o,v);
}
void ModelerUserInterface::cb_Save_i(Fl_Menu_*, void*) {
const char *filename = fileDialog(Fl_Native_File_Chooser::BROWSE_SAVE_FILE,
"RAY File (*.ray)\t*.ray",
"Save Raytracer File");
if(!filename)
return;
if(openRayFile(filename) == false) {
fl_alert("Error opening file.");
}
else {
m_modelerView->draw();
closeRayFile();
};
}
void ModelerUserInterface::cb_Save(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->cb_Save_i(o,v);
}
void ModelerUserInterface::cb_SaveMovie(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->saveMovie();
}
void ModelerUserInterface::cb_LoadScript(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->loadScript();
}
void ModelerUserInterface::cb_SaveScript(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->saveScript();
}
int ModelerUserInterface::loadFile(std::string& buffer,
const bool save,
const char* title,
const char* filter,
const char* extensions) {
while (true) {
// Ask user
const char* fileName = fileDialog(
save ? Fl_Native_File_Chooser::BROWSE_SAVE_FILE :
Fl_Native_File_Chooser::BROWSE_FILE,
filter, title);
if (fileName == NULL) {
return 0;
}
buffer = std::string(fileName);
// Get desired file type
int fileType = m_nativeChooser->filter_value();
int retVal = fileType + 1;
if (save) {
// Get the extension for the file type
const char* ext = extensions - 1;
while (ext && fileType) {
fileType--;
ext = strchr(ext + 1, '\t');
}
// Append extension if the file name is missing one.
if (ext) {
ext++;
int end = strlen(ext);
const char* extEnd = strchr(ext, '\t');
if (extEnd != NULL) {
end -= strlen(extEnd);
}
std::string realExt(ext, end);
unsigned int pos = buffer.find_last_of('.');
if (pos == buffer.npos || buffer.find_last_of("\\/") > pos) {
buffer.append(".");
buffer.append(realExt);
} else {
std::string bufferExt = buffer.substr(pos + 1);
// Make sure the extension matches the file type.
if (bufferExt.compare(realExt) != 0) {
switch (fl_choice("You selected the file type %s, but your "
"file name's extension is %s.\n\nDo you want to fix "
"the extension, or save with a different name?",
"Cancel", "Fix Extension", "Different Name",
realExt.c_str(), bufferExt.c_str())) {
case 0:
return 0;
case 1:
buffer = buffer.substr(0, pos + 1);
buffer += realExt;
break;
case 2:
continue;
}
}
}
}
}
return retVal;
}
}
void ModelerUserInterface::loadScript() {
// Ask user for filename
std::string fileName;
if (!loadFile(fileName, false, "Load Animation Script",
"Animation Scripts (*.ani)\t*.ani", "ani")) { return; }
// Attempt to load the model script
if (!graph->loadModelScript(fileName.c_str())) {
fl_alert("Error loading the model animation script.");
}
}
void ModelerUserInterface::saveScript() {
// Ask user for filename
std::string fileName;
if (!loadFile(fileName, true, "Save Animation Script",
"Animation Scripts (*.ani)\t*.ani", "ani")) { return; }
// Attempt to save the model script
if (!graph->saveModelScript(fileName.c_str())) {
fl_alert("Error saving the model animation script.");
}
}
void ModelerUserInterface::finishSavingMovie() {
if (!rendering) { return; }
// Restore the original interface
rendering = false;
m_controlsWindow->resizable(m_viewPane);
m_controlsMenuBar->show();
m_modelerView->resize(250, 25, m_controlsWindow->w() - 250,
curvePane->y() - 25);
m_modelerView->activate();
leftPane->show();
curvePane->show();
m_viewPane->init_sizes();
m_controlsWindow->init_sizes();
m_controlsWindow->size(oldWidth, oldHeight);
}
void ModelerUserInterface::cancelRender(Fl_Button* button, ModelerUserInterface* ui) {
ui->stop();
}
void ModelerUserInterface::setResolution() {
int width, height;
// Prompt for width.
const char* szWidth = NULL;
do {
szWidth = fl_input("New Animation Width (in pixels) (300 ~ 2500)", "");
if (szWidth) {
width = atoi(szWidth);
} else {
return;
}
} while (szWidth && (width < 300 || width > 2500));
// Prompt for height.
const char* szHeight = NULL;
do {
szHeight = fl_input("New Animation Height (in pixels) (200 ~ 2500)", "");
if (szHeight) {
height = atoi(szHeight);
} else {
return;
}
} while (szHeight && (height < 200 || height > 2500));
// Set width and height only if both were provided.
movieWidth = width; movieHeight = height;
}
void ModelerUserInterface::cb_setResolution(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->setResolution();
}
void ModelerUserInterface::saveMovie() {
// Don't save the movie while one is being saved.
if (rendering) { return; }
// Get file name and type.
std::string fileName;
int fileType = loadFile(fileName, true, "Save Movie As",
"PNG Image File (*.png)\t*.png\nJPEG Image File (*.jpg)\t*.jpg",
"png\tjpg");
if (!fileType) { return; }
renderFileType = fileType - 1;
int extPos = fileName.find_last_of('.');
renderFileName = fileName.substr(0, extPos);
renderFileExt = fileName.substr(extPos);
// Set JPEG rendering quality.
renderJPEGQuality = 95;
if (fileType == 2) {
Dialog2 x(0,0,0,0,"ok");
renderJPEGQuality = x.getValue();
}
// Stop and rewind.
rewind();
// Hide all controls and make the viewing window fill the screen.
oldWidth = m_controlsWindow->w();
oldHeight = m_controlsWindow->h();
m_controlsWindow->size(movieWidth, movieHeight + 25);
m_controlsMenuBar->hide();
leftPane->hide();
curvePane->hide();
m_modelerView->resize(0, 25, m_controlsWindow->w(), m_controlsWindow->h() - 25);
m_modelerView->deactivate(); // prevent user input
// Show some rendering controls
renderGroup = new Fl_Group(5, 5, m_controlsWindow->w(), 15);
// Status text
Fl_Box* box = new Fl_Box(5, 5, 155, 15, "Rendering animation...");
box->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE);
// Render progress bar
renderProgress = new Fl_Progress(155, 5, m_controlsWindow->w() - 215, 15);
renderProgress->minimum(0);
renderProgress->value(0);
renderProgress->maximum(graph->endTime());
renderProgress->selection_color(FL_BLUE);
// Cancel button
Fl_Button* buttonCancelRender = new Fl_Button(m_controlsWindow->w() - 55, 5, 50, 15, "Cancel");
buttonCancelRender->callback((Fl_Callback*)cancelRender, this);
// End this group
renderGroup->end();
// Add group to the window
m_controlsWindow->add(renderGroup);
// Initialize sizes and disable resizing
m_controlsWindow->init_sizes();
m_viewPane->init_sizes();
m_controlsWindow->resizable(NULL);
// Turn off light and camera markers
ModelerDrawState::Instance()->showMarkers = false;
// Enable particle system simulation
setSimulate(true);
// Begin rendering
rendering = true;
renderFrame = 0;
play();
}
void ModelerUserInterface::saveFrame(const char* filename, int fileType,
int quality) {
int x = m_modelerView->x();
int y = m_modelerView->y();
int w = m_modelerView->w();
int h = m_modelerView->h();
m_modelerView->make_current();
m_modelerView->draw();
unsigned char *imageBuffer = new unsigned char[3*w*h];
// Have OpenGL read pixels from its back buffer
glReadBuffer(GL_BACK);
glPixelStorei( GL_PACK_ALIGNMENT, 1 );
glPixelStorei( GL_PACK_ROW_LENGTH, w );
glReadPixels( 0, 0, w, h,
GL_RGB, GL_UNSIGNED_BYTE,
imageBuffer );
// Save the image
saveImage(filename, imageBuffer, w, h, fileType == 0 ? ".png" : ".jpg", quality);
delete [] imageBuffer;
}
/**
* Save a screenshot.
*/
void ModelerUserInterface::cb_Save1_i(Fl_Menu_*, void*) {
try {
// Ask the user where to save the screenshot, and in what format.
std::string fileName;
int fileType = loadFile(fileName, true, "Save Screenshot",
"PNG Image File (*.png)\t*.png\nJPEG Image File (*.jpg)\t*.jpg",
"png\tjpg");
// Exit if the user cancelled the dialog.
if (!fileType) {
return;
}
// Get quality setting for JPEG images.
int quality = 95;
if (fileType == 2) {
Dialog2 x(0,0,0,0,"ok");
quality = x.getValue();
}
// Take the screenshot and save it.
saveFrame(fileName.c_str(), fileType - 1, quality);
} catch (...) {
// Assume that errors were reported when thrown...
}
}
void ModelerUserInterface::cb_Save1(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->cb_Save1_i(o,v);
}
void ModelerUserInterface::cb_loadcurve_i(Fl_Menu_*, void*) {
const char *filename = fileDialog(Fl_Native_File_Chooser::BROWSE_FILE,
"Dense Point Sample File (*.apts)\t*.apts",
"Open Dense Point Sample File");
if(!filename)
return;
reload_curve_file( filename );
m_modelerView->redraw();
}
void ModelerUserInterface::cb_Open_i(Fl_Menu_*, void*) {
/* TODO: load position files again
const char *filename = fileDialog(Fl_Native_File_Chooser::BROWSE_FILE,
"Position File (*.pos)\t*.pos",
"Open Position File");
if(!filename)
return;
std::ifstream ifs( filename );
if( !ifs ) {
std::cerr << "Error: couldn't read position file " << filename << std::endl;
return;
}
float dolly, x, y, z;
float quat[4];
ifs >> dolly >> x >> y >> z >> quat[0] >> quat[1] >> quat[2] >> quat[3];
m_modelerView->m_camera->setDolly( dolly );
m_modelerView->m_camera->setLookAt( Vec3f(x, y, z) );
m_modelerView->m_camera->setQuat( quat );
unsigned int controlNum;
float value;
while( ifs >> controlNum >> value ) {
if( controlNum >= ModelerApplication::Instance()->GetNumControls() ) {
break;
}
ModelerApplication::Instance()->SetControlValue(controlNum, value);
}
m_modelerView->redraw();*/
}
void ModelerUserInterface::cb_loadcurve(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->cb_loadcurve_i(o,v);
}
void ModelerUserInterface::cb_Open(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->cb_Open_i(o,v);
}
void ModelerUserInterface::cb_Save2_i(Fl_Menu_*, void*) {
/* TODO: save position files again
const char *filename = fileDialog(Fl_Native_File_Chooser::BROWSE_SAVE_FILE,
"Position File (*.pos)\t*.pos",
"Save Position File");
if(!filename)
return;
FILE* posFile = NULL;
fopen_s(&posFile, filename, "w");
if(!posFile) {
fl_alert("Save failed!");
return;
}
float dolly;
const float* quat;
Vec3f lookAt;
dolly = m_modelerView->m_camera->getDolly();
lookAt = m_modelerView->m_camera->getLookAt();
quat = m_modelerView->m_camera->getQuat();
fprintf(posFile, "%f %f %f %f %f %f %f %f\n", dolly, lookAt[0], lookAt[1], lookAt[2], quat[0], quat[1], quat[2], quat[3]);
double value;
for(unsigned int i = 0; i < ModelerApplication::Instance()->GetNumControls(); i++) {
value = ModelerApplication::Instance()->GetControlValue(i);
fprintf(posFile, "%d %f\n", i, value);
}
fclose(posFile);*/
}
void ModelerUserInterface::cb_Save2(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->cb_Save2_i(o,v);
}
void ModelerUserInterface::cb_Exit_i(Fl_Menu_*, void*) {
m_controlsWindow->hide();
}
void ModelerUserInterface::cb_Exit(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->cb_Exit_i(o,v);
}
void ModelerUserInterface::cb_Normal_i(Fl_Menu_*, void*) {
setDrawMode(NORMAL);
m_modelerView->redraw();
}
void ModelerUserInterface::cb_Normal(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->cb_Normal_i(o,v);
}
void ModelerUserInterface::cb_Flat_i(Fl_Menu_*, void*) {
setDrawMode(FLATSHADE);
m_modelerView->redraw();
}
void ModelerUserInterface::cb_Flat(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->cb_Flat_i(o,v);
}
void ModelerUserInterface::cb_Wireframe_i(Fl_Menu_*, void*) {
setDrawMode(WIREFRAME);
m_modelerView->redraw();
}
void ModelerUserInterface::cb_Wireframe(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->cb_Wireframe_i(o,v);
}
void ModelerUserInterface::cb_High_i(Fl_Menu_*, void*) {
setQuality(HIGH);
m_modelerView->redraw();
}
void ModelerUserInterface::cb_High(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->cb_High_i(o,v);
}
void ModelerUserInterface::cb_Medium_i(Fl_Menu_*, void*) {
setQuality(MEDIUM);
m_modelerView->redraw();
}
void ModelerUserInterface::cb_Medium(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->cb_Medium_i(o,v);
}
void ModelerUserInterface::cb_Low_i(Fl_Menu_*, void*) {
setQuality(LOW);
m_modelerView->redraw();
}
void ModelerUserInterface::cb_Low(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->cb_Low_i(o,v);
}
void ModelerUserInterface::cb_Poor_i(Fl_Menu_*, void*) {
setQuality(POOR);
m_modelerView->redraw();
}
void ModelerUserInterface::cb_Poor(Fl_Menu_* o, void* v) {
((ModelerUserInterface*)(o->parent()->user_data()))->cb_Poor_i(o,v);
}
Fl_Menu_Item ModelerUserInterface::menu_m_controlsMenuBar[] = {
{"File", 0, 0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},
{"Save Raytracer File", 0, (Fl_Callback*)ModelerUserInterface::cb_Save, 0, 0, FL_NORMAL_LABEL, 0, 14, 0},
{"Save Image", 0, (Fl_Callback*)ModelerUserInterface::cb_Save1, 0, 128, FL_NORMAL_LABEL, 0, 14, 0},
// TODO: fix these and re-enable
// {"Open Position File", 0, (Fl_Callback*)ModelerUserInterface::cb_Open, 0, 0, FL_NORMAL_LABEL, 0, 14, 0},
// {"Save Position File", 0, (Fl_Callback*)ModelerUserInterface::cb_Save2, 0, 128, FL_NORMAL_LABEL, 0, 14, 0},
{"Reload Textures and Shaders", 0, (Fl_Callback*)ModelerUserInterface::cb_Reload, 0, 128, FL_NORMAL_LABEL, 0, 14, 0},
#ifdef _DEBUG
{"Generate shaders.cpp", 0, (Fl_Callback*)ModelerUserInterface::cb_GenShaders, 0, 128, FL_NORMAL_LABEL, 0, 14, 0},
#endif
{"Save Movie Frames", 0, (Fl_Callback*)ModelerUserInterface::cb_SaveMovie, 0, 0, FL_NORMAL_LABEL, 0, 14, 0},
{"Set Movie Dimensions", 0, (Fl_Callback*)ModelerUserInterface::cb_setResolution, 0, 0, FL_NORMAL_LABEL, 0, 14, 0},
{"Set Frames Per Second", 0, (Fl_Callback*)ModelerUserInterface::cb_setFPS, 0, 0, FL_NORMAL_LABEL, 0, 14, 0},
{"Set Movie Length", 0, (Fl_Callback*)ModelerUserInterface::cb_setMovieLength, 0, 128, FL_NORMAL_LABEL, 0, 14, 0},
{"Open Animation Script", 0, (Fl_Callback*)ModelerUserInterface::cb_LoadScript, 0, 0, FL_NORMAL_LABEL, 0, 14, 0},
{"Save Animation Script", 0, (Fl_Callback*)ModelerUserInterface::cb_SaveScript, 0, 128, FL_NORMAL_LABEL, 0, 14, 0},
{"Exit", 0, (Fl_Callback*)ModelerUserInterface::cb_Exit, 0, 0, FL_NORMAL_LABEL, 0, 14, 0},
{0,0,0,0,0,0,0,0,0},
{"View", 0, 0, 0, 64, FL_NORMAL_LABEL, 0, 14, 0},
{"Normal", 0, (Fl_Callback*)ModelerUserInterface::cb_Normal, 0, 12, FL_NORMAL_LABEL, 0, 14, 0},
{"Flat Shaded", 0, (Fl_Callback*)ModelerUserInterface::cb_Flat, 0, 8, FL_NORMAL_LABEL, 0, 14, 0},
{"Wireframe", 0, (Fl_Callback*)ModelerUserInterface::cb_Wireframe, 0, 136, FL_NORMAL_LABEL, 0, 14, 0},
{"High Quality", 0, (Fl_Callback*)ModelerUserInterface::cb_High, 0, 8, FL_NORMAL_LABEL, 0, 14, 0},
{"Medium Quality", 0, (Fl_Callback*)ModelerUserInterface::cb_Medium, 0, 12, FL_NORMAL_LABEL, 0, 14, 0},
{"Low Quality", 0, (Fl_Callback*)ModelerUserInterface::cb_Low, 0, 8, FL_NORMAL_LABEL, 0, 14, 0},
{"Poor Quality", 0, (Fl_Callback*)ModelerUserInterface::cb_Poor, 0, 8 + 128, FL_NORMAL_LABEL, 0, 14, 0},
{"Show Light/Camera Markers", 0, (Fl_Callback*)ModelerUserInterface::showMarkersCallback, 0, 2, FL_NORMAL_LABEL, 0, 14, 0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0}
};
/** Called when the Show Light/Camera Markers menu item is clicked. */
void ModelerUserInterface::showMarkersCallback(Fl_Menu_* m, void* p) {
ModelerDrawState::Instance()->showMarkers =
m->mvalue()->value() ? true : false;
ModelerUserInterface::getInstance()->m_modelerView->redraw();
}
ModelerUserInterface::ModelerUserInterface() {
// Make this instance the current one
instance = this;
// Initialize pointers to NULL
m_nativeChooser = NULL;
model = NULL;
currentGroup = NULL;
renderGroup = NULL;
defaultCam = NULL;
ps = NULL;
movieWidth = 720;
movieHeight = 480;
// Set appearance to GTK+ for a nice look
Fl::scheme("gtk+");
// Set the animation speed to 24 frames/second
framesPerSecond = 24;
// We're not animating yet.
animating = false;
simulating = false;
rendering = false;
drawing = false;
// Set the color scheme
Fl::set_color(FL_BACKGROUND_COLOR, 240, 240, 240);
Fl::set_color(FL_BACKGROUND2_COLOR, 255, 255, 255);
Fl::set_color(FL_FOREGROUND_COLOR, 0, 0, 0);
Fl::set_color(FL_INACTIVE_COLOR, 128, 128, 128);
Fl::set_color(FL_SELECTION_COLOR, 51, 153, 255);
// Create all of the UI elements
// (autogenerated by FLUID, the FLTK UI Designer)
Fl_Double_Window* w;
const char* title = "CSE 557 Animator";
{
Fl_Double_Window* o = m_controlsWindow = new Fl_Double_Window(800, 625, title);
w = o;
o->callback((Fl_Callback*)cb_m_controlsWindow, (void*)(this));
o->when(FL_WHEN_NEVER);
{ Fl_Menu_Bar* o = m_controlsMenuBar = new Fl_Menu_Bar(0, 0, 800, 25);
o->menu(menu_m_controlsMenuBar);
}
// Contains the controls on the left
{ leftPane = new Fl_Group(0, 25, 250, 600);
int tabSpace = 0, controlSpace = 0;
int controlTop = 25 + 600 - controlSpace;
// Modeler and Curves tabs
{ Fl_Tabs* t = new Fl_Tabs(0, 25, 250, 600 - controlSpace);
// Make the tab area stretch.
leftPane->resizable(t);
// Curves tab
{ Fl_Group* o = new Fl_Group(0, 50, 250, 575 - controlSpace, "Curves");
o->box(FL_FLAT_BOX);
o->color(FL_BACKGROUND_COLOR);
{ Fl_Tree* o = curvesTree = new Fl_Tree(0, 50, 250, 575 - controlSpace);
o->when(FL_WHEN_CHANGED);
o->callback((Fl_Callback*)CurveTreeCallback);
o->marginleft(-5);
o->end();
}
o->end();
}
// Modeler tab
{ Fl_Tile* o = m_controlSplitPane =
new Fl_Tile(0, 50 + tabSpace, 250, 575 + tabSpace - controlSpace, "Modeler");
// Make only the content area of the tabs resize.
t->resizable(o);
o->box(FL_FLAT_BOX);
{ Fl_Tree* o = m_controlsTree = new Fl_Tree(0, 50 + tabSpace, 250, 100);
o->when(FL_WHEN_CHANGED);
o->callback((Fl_Callback*)TreeCallback);
o->marginleft(-5);
o->end();
}
{ Fl_Scroll* o = m_controlsScroll =
new Fl_Scroll(0, 150 + tabSpace, 250, 475 - tabSpace - controlSpace);
o->type(Fl_Scroll::VERTICAL);
o->when(FL_WHEN_CHANGED);
{ Fl_Pack* o = m_controlsPack =
new Fl_Pack(10, 150 + tabSpace, 215, 475 - tabSpace - controlSpace);
Fl_Group::current()->resizable(o);
o->spacing(2);
o->end();
}
o->end();
}
o->end();
} // end Modeler group/tab
t->end();
} // end tabs
leftPane->end();
} // left pane
{ // TODO: remove this extra brace!
{ Fl_Group* o = m_viewPane = new Special_Tile(250, 25, 550, 600);
o->box(FL_NO_BOX);
o->color(FL_BACKGROUND_COLOR);
// show a smaller modeler view
m_modelerView = new ModelerView(250, 25, 550, 350, "");
// show a curve window with animation controls underneath
{ curvePane = new Fl_Group(250, 375, 550, 250);
// Row containing the curve options
{ Fl_Group* o = new Fl_Group(255, 380, 540, 20, "Curve Editor");
// Put particle system label inside
o->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE);
o->labelfont(FL_HELVETICA_BOLD);
// Curve type chooser
chooserCurveType = new Fl_Choice(445, 380, 120, 20, "Curve Type: ");
chooserCurveType->menu(curveTypeMenu);
chooserCurveType->callback((Fl_Callback*)chooserCurveTypeCallback, this);
chooserCurveType->deactivate();
// Wrap checkbox
checkboxWrap = new Fl_Check_Button(570, 380, 60, 20, "Wrap");
checkboxWrap->callback((Fl_Callback*)checkboxWrapCallback, this);
checkboxWrap->deactivate();
// Zoom All button
buttonZoomAll = new Fl_Button(640, 380, 80, 20, "Zoom All");
buttonZoomAll->callback((Fl_Callback*)buttonZoomAllCallback, this);
buttonZoomAll->deactivate();
// No resizing
o->resizable(NULL);
o->end();
}
// The graph widget
graph = new GraphWidget(255, 405, 540, 140);
graph->callback((Fl_Callback*)graphCallback, this);
curvePane->resizable(graph); // stretch the graph on resize
// Camera buttons
{ cameraPane = new Fl_Group(255, 550, 540, 20, "Camera");
// Put camera label inside
cameraPane->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE);
cameraPane->labelfont(FL_HELVETICA_BOLD);
// Prevent internal widgets from resizing when the group is resized.
cameraPane->resizable(NULL);
// Use Camera button
checkboxUseCamera = new Fl_Check_Button(315, 550, 165, 20, "Look Through Camera");
checkboxUseCamera->callback((Fl_Callback*)checkboxUseCameraCallback);
// Plot Camera checkbox
Fl_Button* buttonPlotCamera = new Fl_Button(485, 550, 120, 20, "Plot Keyframe");
buttonPlotCamera->callback((Fl_Callback*)buttonPlotCameraCallback);
// Clear Plot button
Fl_Button* buttonClearPlot = new Fl_Button(610, 550, 120, 20, "Clear Keyframe");
buttonClearPlot->callback((Fl_Callback*)buttonClearCameraCallback);
// End this group
cameraPane->end();
}
// Particle system buttons
{ particlePane = new Fl_Group(255, 575, 540, 20, "Particle System");
// Put particle system label inside
particlePane->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE);
particlePane->labelfont(FL_HELVETICA_BOLD);
// Simulate button
checkboxSimulate = new Fl_Check_Button(375, 575, 75, 20, "Simulate");
checkboxSimulate->callback((Fl_Callback*)checkboxSimulateCallback);
// Clear Particles button
buttonClearParticles = new Fl_Button(455, 575, 50, 20, "Clear");
buttonClearParticles->callback((Fl_Callback*)buttonClearParticlesCallback);
// Particle "bake" indicator
particleBakeIndicator = new IndicatorWindow(510, 575, 285, 20);
// TODO: fix this:
particleBakeIndicator->range(0, 20);
particleBakeIndicator->deactivate();
particlePane->resizable(particleBakeIndicator);
// End this group
particlePane->end();
}
// The playback controls
{ Fl_Group* o = new Fl_Group(255, 600, 540, 20, "Playback");
// Put particle system label inside
o->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE);
o->labelfont(FL_HELVETICA_BOLD);
// Rewind button
Fl_Button* rewind = new Fl_Button(335, 600, 20, 20, "@|<");
rewind->labeltype(FL_SYMBOL_LABEL);
rewind->callback((Fl_Callback*)buttonRewindCallback, this);
// Play button
buttonPlay = new Fl_Button(360, 600, 20, 20, "@>");
buttonPlay->labeltype(FL_SYMBOL_LABEL);
buttonPlay->callback((Fl_Callback*)buttonPlayCallback, this);
buttonPlay->labelcolor(FL_GREEN);
// Fast Forward button
Fl_Button* fastForward = new Fl_Button(385, 600, 20, 20, "@>|");
fastForward->labeltype(FL_SYMBOL_LABEL);
fastForward->callback((Fl_Callback*)buttonFastForwardCallback, this);
// Time slider
sliderTime = new Fl_Value_Slider(455, 600, 340, 20, "Time:");
sliderTime->type(FL_HORIZONTAL);
sliderTime->align(FL_ALIGN_LEFT);
sliderTime->callback((Fl_Callback*)sliderTimeCallback, this);
sliderTime->bounds(0, 20);
// Stretch the time slider on resize
o->resizable(sliderTime);
o->end();
}
curvePane->end();
}
w->resizable(m_modelerView);
o->end();
Fl_Group::current()->resizable(o);
}
}
o->end();
}
}
static char* argv[1];
void ModelerUserInterface::show() {
m_controlsWindow->show();
m_modelerView->show();
}
const char* ModelerUserInterface::fileDialog(Fl_Native_File_Chooser::Type dialogType, const char* filter, const char* title) {
//------------------------------------------------------------------------
// This displays a modal file chooser with a native look-and-feel.
// The available dialog types are:
// BROWSE_DIRECTORY - Open a single directory
// BROWSE_FILE - Open a single file
// BROWSE_MULTI_DIRECTORY - Open directories, allowing the user to
// select more than one at a time
// BROWSE_MULTI_FILE - Open files, allowing the user to select
// more than one at a time
// BROWSE_SAVE_DIRECTORY - Save a directory
// BROWSE_SAVE_FILE - Save a file
//
// The filter limits the displayed files. See cb_load_image for an example.
// title is optional, use NULL for the OS default title.
// The return value is the filepath.
//------------------------------------------------------------------------
if(!m_nativeChooser)
m_nativeChooser = new Fl_Native_File_Chooser(dialogType);
else
m_nativeChooser->type(dialogType);
m_nativeChooser->filter(filter);
m_nativeChooser->title(title);
int ret = m_nativeChooser->show();
if(ret == -1 || ret == 1) {
//error or cancel respectively
return NULL;
}
return m_nativeChooser->filename();
}
ModelerUserInterface::~ModelerUserInterface() {
if(m_nativeChooser) delete m_nativeChooser;
if (model) delete model;
if (defaultCam) delete defaultCam;
}
void ModelerUserInterface::cb_Reload(Fl_Menu*, void*) {
ModelerUserInterface::getInstance()->cb_Reload_i();
}
void ModelerUserInterface::cb_Reload_i() {
// TODO: better resource error handling
m_modelerView->retry();
}
void ModelerUserInterface::cb_GenShaders(Fl_Menu*, void*) {
// Call instance method
ModelerUserInterface::getInstance()->cb_GenShaders_i();
}
void ModelerUserInterface::cb_GenShaders_i() {
// Generate shaders
shaderSourceCode = new map<string, string>();
m_modelerView->retry();
}
void ModelerUserInterface::cb_setFPS(Fl_Menu*, void*) {
ModelerUserInterface::getInstance()->setFPS();
}
int ModelerUserInterface::getFPS() { return framesPerSecond; }
void ModelerUserInterface::setFPS(int fps) {
framesPerSecond = fps;
}
void ModelerUserInterface::setFPS() {
int testFPS;
// Prompt for width.
const char* szFPS = NULL;
do {
szFPS = fl_input("New Frames Per Second (1 ~ 50)", "");
if (szFPS) {
testFPS = atoi(szFPS);
} else {
return;
}
} while (szFPS && (testFPS < 1 || testFPS > 50));
setFPS(testFPS);
}
void ModelerUserInterface::setModel(Model* model) {
// Get rid of the old model.
if (this->model) {
delete this->model;
}
// Get rid of the default camera.
if (defaultCam) {
delete defaultCam;
}
// Set the model
this->model = model;
m_modelerView->setModel(model);
// Set particle system
ps = model->getParticleSystem();
// Populate list with model items
m_controlsTree->clear();
pickGroupProperty(NULL);
// Populate the list with group properties
populateList(model->getProperties());
// Select the first one
if (m_controlsTree->first()) {
m_controlsTree->select(m_controlsTree->first(), 1);
}
// Clear the curve list
curvesTree->clear();
// Populate the curve list
populateCurveList(model->getProperties());
// Create a default camera if the model doesn't provide one
// TODO: move the other default camera things into #ANIMATOR guards
if (!model->getCamera()) {
defaultCam = new SimpleCameraModel("Default Camera");
populateList(defaultCam->getProperties(), m_controlsTree->root());
populateCurveList(defaultCam->getProperties(), curvesTree->root());
}
useCamera(true);
}
// TODO: populate the curve list once we actually have a list of curves.
void ModelerUserInterface::populateCurveList(Property* prop, Fl_Tree_Item* parent) {
// Create a tree node for this property
Fl_Tree_Item* item;
if (parent == NULL) {
// HACK: We have to add and remove a fake list item so that the tree
// control will create a root node to put it under.
curvesTree->remove(curvesTree->add("You shouldn't see this."));
item = curvesTree->root();
item->label(prop->getName());
} else {
item = curvesTree->add(parent, prop->getName());
}
item->labelfont(FL_HELVETICA_BOLD);
if (GroupProperty* g = dynamic_cast<GroupProperty*>(prop)) {
if (g->getCollapsed()) {
item->close();
}
} else if (dynamic_cast<RGBProperty*>(prop)) {
item->close();
}
// Examine the list of properties.
PropertyList* controls = prop->getProperties();
for (PropertyList::iterator iter = controls->begin();
iter != controls->end();
iter++) {
// For now, only RangeProperty is supported by the Curve interface.
// The RGBProperty is also indirectly supported because it can provide
// RangeProperties when its getProperties() method is called.
// TODO: make this work with more property types (using a new
// plotting widget?)
if (RangeProperty* range = dynamic_cast<RangeProperty*>(*iter)) {
// TODO: connect to Curve object instead of Property object.
Fl_Tree_Item* childNode = curvesTree->add(item, range->getName());
curveProps.push_back(range);
childNode->user_data((void*)(curveProps.size() - 1));
range->setCurveIndex(curveProps.size() - 1);
graph->addCurve(range->getValue(), range->getMin(),
range->getMax());
} else if ((*iter)->getProperties() != NULL) {
// Try to get a list of GroupProperties out of it.
populateCurveList(*iter, item);
}
}
}
void ModelerUserInterface::populateList(GroupProperty* group, Fl_Tree_Item* parent) {
// Create a tree node for this group.
Fl_Tree_Item* item;
if (parent == NULL) {
// HACK: We have to add and remove a fake list item so that the tree
// control will create a root node to put it under.
m_controlsTree->remove(m_controlsTree->add("You shouldn't see this."));
item = m_controlsTree->root();
item->label(group->getName());
} else {