-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelerapp.cpp
213 lines (166 loc) · 4.97 KB
/
modelerapp.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
#pragma warning(disable : 4786)
#include "particleSystem.h"
#include "modelerapp.h"
#include "modelerview.h"
#include "modelerui.h"
#include "camera.h"
#include <FL/Fl_Value_Slider.H>
#include <FL/Fl_Box.H>
#include <cstring>
#include <cstdio>
#include <cstdlib>
// CLASS ModelerControl METHODS
ModelerControl::ModelerControl() : m_minimum(0.0f), m_maximum(1.0f), m_stepsize(0.1f), m_value(0.0f)
{
}
ModelerControl::ModelerControl(const char* name, float minimum, float maximum, float stepsize, float value)
{
SetVals(name, minimum, maximum, stepsize, value);
}
ModelerControl::ModelerControl(const ModelerControl &o)
{
operator=(o);
}
ModelerControl& ModelerControl::operator=(const ModelerControl &o)
{
if (this != &o)
SetVals(o.m_name, o.m_minimum, o.m_maximum, o.m_stepsize, o.m_value);
return *this;
}
void ModelerControl::SetVals(const char* name, float minimum, float maximum, float stepsize, float value)
{
strncpy(m_name,name, 128);
m_minimum = minimum;
m_maximum = maximum;
m_stepsize = stepsize;
m_value = value;
}
// ****************************************************************************
// Set the singleton initially to a NULL instance
ModelerApplication* ModelerApplication::m_instance = NULL;
// CLASS ModelerApplication METHODS
ModelerApplication* ModelerApplication::Instance()
{
// Make a new instance if none exists, otherwise, return
// the existing instance of the ModelerApplication
return (m_instance) ? (m_instance) : (m_instance = new ModelerApplication());
}
void ModelerApplication::Init(ModelerViewCreator_f createView,
const ModelerControl controls[], unsigned numControls)
{
int i;
m_animating = false;
m_numControls = numControls;
DWORD dwBtnFaceColor = GetSysColor(COLOR_BTNFACE);
// Get consistent background color
Fl::background(GetRValue(dwBtnFaceColor),
GetGValue(dwBtnFaceColor),
GetBValue(dwBtnFaceColor));
// ********************************************************
// Create the FLTK user interface
// ********************************************************
m_ui = new ModelerUI();
// For each control, add appropriate objects to the user interface
for (i=0; i<m_numControls; i++)
{
m_ui->addControl(controls[i].m_name,
controls[i].m_minimum, controls[i].m_maximum,
controls[i].m_stepsize, controls[i].m_value);
}
// Setup value-changed callback
m_ui->setValueChangedCallback(ModelerApplication::ValueChangedCallback);
ModelerView* modelerView = createView(0, 0, 100, 100 ,NULL);
m_ui->replaceModelerView(modelerView);
}
ModelerApplication::~ModelerApplication()
{
// FLTK handles widget deletion
delete m_ui;
}
int ModelerApplication::Run()
{
if (m_numControls == -1)
{
fprintf(stderr, "ERROR: ModelerApplication must be initialized before Run()!\n");
return -1;
}
// Just tell FLTK to go for it.
Fl::visual( FL_RGB | FL_DOUBLE );
m_ui->show();
Fl::add_timeout(0, ModelerApplication::RedrawLoop, NULL);
// Automatically load animator.ani and animator.ani.cam if they exist
m_ui->autoLoadNPlay();
return Fl::run();
}
double ModelerApplication::GetControlValue(int controlNumber)
{
return m_ui->controlValue(controlNumber);
}
void ModelerApplication::SetControlValue(int controlNumber, double value)
{
m_ui->controlValue(controlNumber, value);
}
ParticleSystem *ModelerApplication::GetParticleSystem()
{
return ps;
}
void ModelerApplication::SetParticleSystem(ParticleSystem *s)
{
ps = s;
}
float ModelerApplication::GetTime()
{
return m_ui->currTime();
}
int ModelerApplication::GetFps()
{
return m_ui->fps();
}
bool ModelerApplication::Animating()
{
return m_animating;
}
void ModelerApplication::ValueChangedCallback()
{
ModelerApplication *m_app = ModelerApplication::Instance();
ModelerUI *m_ui = m_app->m_ui;
float currTime = m_ui->currTime();
float endTime = m_ui->endTime();
float playEndTime = m_ui->playEndTime();
ParticleSystem *ps = m_app->GetParticleSystem();
if (ps != NULL) {
bool simulating = ps->isSimulate();
// stop simulation if we're at endTime
double TIME_EPSILON = 0.05;
if (simulating && (currTime >= (playEndTime - TIME_EPSILON))) {
ps->stopSimulation(currTime);
}
// check to see if we're simulating still
simulating = ps->isSimulate();
if (simulating != m_ui->simulate()) {
// if the psystem is dirty,
// we need to sync to it
if (ps->isDirty()) {
m_ui->simulate(simulating == true);
}
// otherwise, we sync the psystem
// to the ui
else if (m_ui->simulate()) {
ps->startSimulation(currTime);
} else {
ps->stopSimulation(currTime);
}
}
ps->setDirty(false);
}
// update camera position
m_ui->m_pwndModelerView->m_camera->update(currTime);
m_ui->redrawModelerView();
}
void ModelerApplication::RedrawLoop(void*)
{
if (ModelerApplication::Instance()->m_animating)
ModelerApplication::Instance()->m_ui->redrawModelerView();
// 1/50 second update is good enough
Fl::add_timeout(0.025, ModelerApplication::RedrawLoop, NULL);
}