-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMushraComponent.cpp
More file actions
367 lines (279 loc) · 11.7 KB
/
MushraComponent.cpp
File metadata and controls
367 lines (279 loc) · 11.7 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
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
/*
==============================================================================
MushraComponent.cpp
Created: 29 Jun 2019 5:31:27pm
Author: Chris Yeoward
==============================================================================
*/
#include <JuceHeader.h>
#include "MushraComponent.h"
//==============================================================================
MushraComponent::MushraComponent(MushraProcessor &p, int count, String destFileDirectory) :
processor(p),
infoTextLabel("", ""),
stimulusCount(count),
ExcellentLabel("", "Excellent"),
GoodLabel("", "Good"),
FairLabel("", "Fair"),
PoorLabel("", "Poor"),
BadLabel("", "Bad"),
statusLabel("", "Stage 1"),
sceneDescriptionLabel("", ""),
fileDirectory(destFileDirectory)
{
// In your constructor, you should add any child components, and
// initialise any special settings that your component needs.
getLookAndFeel().setColour(TextButton::ColourIds::buttonOnColourId, Colours::white);
getLookAndFeel().setColour(TextButton::ColourIds::textColourOnId, Colours::black);
addAndMakeVisible(infoTextLabel);
infoTextLabel.setJustificationType(Justification::centred);
addAndMakeVisible(ExcellentLabel);
addAndMakeVisible(GoodLabel);
addAndMakeVisible(FairLabel);
addAndMakeVisible(PoorLabel);
addAndMakeVisible(BadLabel);
ExcellentLabel.setJustificationType(Justification::centred);
GoodLabel.setJustificationType(Justification::centred);
FairLabel.setJustificationType(Justification::centred);
PoorLabel.setJustificationType(Justification::centred);
BadLabel.setJustificationType(Justification::centred);
// addAndMakeVisible (referenceButton);
referenceButton.setRadioGroupId(1);
referenceButton.setButtonText ("R");
referenceButton.addListener(this);
stimulusButtons = new TextButton[stimulusCount];
stimulusRatingSliders = new Slider[stimulusCount];
for(int stimulusIndex = 0; stimulusIndex < stimulusCount; stimulusIndex++)
{
addAndMakeVisible(stimulusButtons[stimulusIndex]);
stimulusButtons[stimulusIndex].setButtonText(String(stimulusIndex + 1));
stimulusButtons[stimulusIndex].addListener(this);
stimulusButtons[stimulusIndex].setRadioGroupId(1);
addAndMakeVisible(stimulusRatingSliders[stimulusIndex]);
stimulusRatingSliders[stimulusIndex].addListener(this);
stimulusRatingSliders[stimulusIndex].setRange (0.0, 100.0, 0.1);
stimulusRatingSliders[stimulusIndex].setSliderStyle(Slider::LinearVertical);
stimulusRatingSliders[stimulusIndex].hideTextBox(true);
indexToStimulusMapping.push_back(stimulusIndex);
}
resetForm();
processor.resetEvaluation();
addAndMakeVisible(playPauseButton);
playPauseButton.setButtonText("Play");
playPauseButton.addListener(this);
addAndMakeVisible(submitButton);
submitButton.setButtonText("Next");
submitButton.addListener(this);
addAndMakeVisible(statusLabel);
statusLabel.setJustificationType(Justification::centred);
addAndMakeVisible(sceneDescriptionLabel);
sceneDescriptionLabel.setJustificationType(Justification::centred);
addAndMakeVisible(saveAndExitButton);
saveAndExitButton.addListener(this);
// saveAndExitButton.setButtonText("Save and exit");
}
MushraComponent::~MushraComponent()
{
delete[] stimulusButtons;
delete[] stimulusRatingSliders;
}
void MushraComponent::paint (Graphics& g)
{
/* This demo code just fills the component's background and
draws some placeholder text to get you started.
You should replace everything in this method with your own
drawing code..
*/
g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId)); // clear the background
g.setColour (Colours::grey);
g.setColour (Colours::white);
g.setFont (14.0f);
float buttonSpacing = 50;
float sectionWidth = (getWidth() / (stimulusCount + 2));
float sectionHeight = getHeight()/7;
float buttonWidth = sectionWidth - buttonSpacing;
float buttonHeight = sectionHeight/2;
float sliderHeight = (5 * sectionHeight) + 20;
infoTextLabel.setBounds(0, 0, getWidth(), sectionHeight);
g.setColour (Colours::grey);
float lineWidth = sectionWidth * (stimulusCount + 1) - buttonWidth;
for(int line = 0; line < 4; line++) {
g.drawLine(0, (line + 3) * sectionHeight, lineWidth, (line + 3) * sectionHeight);
}
referenceButton.setBounds(0, buttonHeight/2, buttonWidth, buttonHeight);
referenceButton.setToggleState(false, NotificationType::dontSendNotification);
for(int stimulusIndex = 0; stimulusIndex < stimulusCount; stimulusIndex++)
{
float x = (buttonWidth + buttonSpacing) * (stimulusIndex + 1);
stimulusButtons[stimulusIndex].setBounds(x, buttonHeight/2 + sectionHeight, buttonWidth, buttonHeight);
stimulusRatingSliders[stimulusIndex].setBounds(x, 2*sectionHeight - 10, buttonWidth, sliderHeight);
stimulusRatingSliders[stimulusIndex].setTextBoxStyle(juce::Slider::TextBoxBelow, true, 0, 0);
stimulusButtons[stimulusIndex].setToggleState(false, NotificationType::dontSendNotification);
}
ExcellentLabel.setBounds(0, 2*sectionHeight, buttonWidth, sectionHeight);
GoodLabel.setBounds(0, 3*sectionHeight, buttonWidth, sectionHeight);
FairLabel.setBounds(0, 4*sectionHeight, buttonWidth, sectionHeight);
PoorLabel.setBounds(0, 5*sectionHeight, buttonWidth, sectionHeight);
BadLabel.setBounds(0, 6*sectionHeight, buttonWidth, sectionHeight);
submitButton.setBounds((stimulusCount + 1) * sectionWidth, getHeight()/2, sectionWidth, buttonHeight);
String buttonText = processor.getIsPlaying() ? "Pause" : "Play";
playPauseButton.setButtonText(buttonText);
playPauseButton.setBounds((stimulusCount + 1) * sectionWidth, (getHeight()/2) - sectionHeight, sectionWidth, buttonHeight);
playPauseButton.setToggleState(!processor.getIsPlaying(), NotificationType::dontSendNotification);
String status = "Scene " + String(processor.getCurrentStage() + 1) + " of " + String(processor.getNumberOfPermutations());
statusLabel.setText(status, NotificationType::dontSendNotification);
statusLabel.setBounds((stimulusCount + 1) * sectionWidth, (getHeight()/2) + sectionHeight, sectionWidth, buttonHeight);
sceneDescriptionLabel.setBounds((stimulusCount + 1) * sectionWidth, (getHeight()/2) + 2 * sectionHeight, sectionWidth, buttonHeight);
if(activeButton != NULL)
{
activeButton->setToggleState(true, NotificationType::dontSendNotification);
}
saveAndExitButton.setBounds(getWidth() - buttonHeight, getHeight() - buttonHeight, buttonHeight, buttonHeight);
}
void MushraComponent::setScaleLabels(String* labels){
ExcellentLabel.setText(labels[4], dontSendNotification);
GoodLabel.setText(labels[3], dontSendNotification);
FairLabel.setText(labels[2], dontSendNotification);
PoorLabel.setText(labels[1], dontSendNotification);
BadLabel.setText(labels[0], dontSendNotification);
}
void MushraComponent::resized()
{
// This method is where you should set the bounds of any child
// components that your component contains..
}
void MushraComponent::setInfoText(String text) {
infoTextLabel.setText(text, NotificationType::dontSendNotification);
}
void MushraComponent::buttonClicked (Button* button)
{
if(button == &submitButton) {
int scores[stimulusCount];
for(int stimulusIndex = 0; stimulusIndex<stimulusCount; stimulusIndex++)
{
scores[indexToStimulusMapping.at(stimulusIndex)] = stimulusRatingSliders[stimulusIndex].getValue();
}
processor.setScoresForScene(scores, processor.getCurrentPermutation());
Component::BailOutChecker checker (this);
if(processor.isFinished()) {
writeValuesToFile();
submitButtonListeners.callChecked (checker, [this] (Listener& l) { l.mushraFormCompleted (this); });
processor.resetEvaluation();
resetForm();
} else {
resetForm();
processor.goToNextStage();
submitButtonListeners.callChecked (checker, [this] (Listener& l) { l.mushraFormSubmitted (this); });
}
} else if (button == &playPauseButton) {
if(processor.getIsPlaying()) {
activeButton = nullptr;
processor.setIsPlaying(false);
} else {
processor.setActiveStimulus(indexToStimulusMapping[0]);
activeButton = &stimulusButtons[0];
}
} else if (button == &referenceButton)
{
processor.setActiveStimulus(0);
activeButton = button;
} else if (button == &saveAndExitButton){
int scores[stimulusCount];
for(int stimulusIndex = 0; stimulusIndex<stimulusCount; stimulusIndex++)
{
scores[indexToStimulusMapping.at(stimulusIndex)] = stimulusRatingSliders[stimulusIndex].getValue();
}
processor.setScoresForScene(scores, processor.getCurrentPermutation());
Component::BailOutChecker checker (this);
writeValuesToFile();
submitButtonListeners.callChecked (checker, [this] (Listener& l) { l.mushraFormCompleted (this); });
processor.resetEvaluation();
resetForm();
} else {
for(int buttonIndex = 0; buttonIndex < stimulusCount; buttonIndex++)
{
if(button == &(stimulusButtons[buttonIndex])) {
activeButton = button;
processor.setActiveStimulus(indexToStimulusMapping[buttonIndex]);
}
}
}
}
void MushraComponent::sliderValueChanged(Slider* slider)
{
// DBG(slider->getValue());
}
void MushraComponent::writeValuesToFile()
{
std::cout << "write to file!";
String filename = filePrefix != "" ? filePrefix : "evaluation";
filename += "_" + Time::getCurrentTime().toISO8601(true);
filename += ".csv";
String filepath = fileDirectory + filename;
File file(filepath);
TemporaryFile tempFile (file);
FileOutputStream output (tempFile.getFile());
if (! output.openedOk())
{
DBG ("FileOutputStream didn't open correctly ...");
// ... some other error handling
}
int outputs[stimulusCount];
for(int stimulusIndex = 0; stimulusIndex<stimulusCount; stimulusIndex++)
{
outputs[indexToStimulusMapping.at(stimulusIndex)] = stimulusRatingSliders[stimulusIndex].getValue();
}
String line1 = "scene\\method," + String(0);
for(int stimulusIndex = 1; stimulusIndex < stimulusCount; stimulusIndex++)
{
line1 += ",";
line1 += String(stimulusIndex);
}
line1 += "\n";
output.setNewLineString("\n");
output.writeString(line1);
for(int scene = 0; scene < processor.getNumberOfPermutations(); scene++)
{
String line = String(scene);
for(int method = 0; method < stimulusCount; method ++) {
line += ",";
line += String(processor.getScoreFor(scene, method));
}
line += "\n";
output.writeString(line);
}
// output.writeString(line2);
output.flush(); // (called explicitly to force an fsync on posix)
if (output.getStatus().failed())
{
DBG ("An error occurred in the FileOutputStream");
// ... some other error handling
}
tempFile.overwriteTargetFileWithTemporary();
}
void MushraComponent::addListener (Listener* l) { submitButtonListeners.add (l); }
void MushraComponent::removeListener (Listener* l) { submitButtonListeners.remove (l); }
void MushraComponent::resetForm() {
activeButton = nullptr;
// processor.setActiveStimulus(-1);
shuffleMapping();
for(int stimulusIndex = 0; stimulusIndex < stimulusCount; stimulusIndex++)
{
stimulusRatingSliders[stimulusIndex].setValue(0.0);
}
}
void MushraComponent::setFilePrefix(String prefix)
{
filePrefix = prefix;
}
void MushraComponent::setSceneDescription(String description)
{
sceneDescriptionLabel.setText(description, dontSendNotification);
}
void MushraComponent::shuffleMapping() {
unsigned seed = (unsigned) std::chrono::system_clock::now().time_since_epoch().count();
shuffle (indexToStimulusMapping.begin(), indexToStimulusMapping.end(), std::default_random_engine(seed));
std::cout << "shuffled elements:";
for (int& x: indexToStimulusMapping) std::cout << ' ' << x;
std::cout << '\n';
}