/
ofApproxPiSetupGUI.cpp
129 lines (107 loc) · 4.14 KB
/
ofApproxPiSetupGUI.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
#include "ofApproxPiSetupGUI.h"
void ofApproxPiSetupGUI::setup(){
ofSetWindowPosition(0, 0);
ofSetWindowShape(getGuiWindowWidth(),getGuiWindowHeight());
int x=20, y=20;
gui = new ofxDatGui(ofxDatGuiAnchor::TOP_LEFT);
gui->setWidth(400);
gui->setPosition(x, y);
gui->addLabel("Approx Pi Setup");
gui->addDropdown("NChannels", nChannels_options_str);
gui->getDropdown("NChannels")->setLabel("NChannels"+spacer+nChannels_options_str[0]);
gui->addDropdown("Resolution", screen_resolutions_options_str);
gui->getDropdown("Resolution")->setLabel("Resolution"+spacer+screen_resolutions_options_str[0]);
audio_devices = ofSoundStreamListDevices();
for(int i=0; i<audio_devices.size(); i++)
{
if(audio_devices[i].outputChannels >0)
output_devices.push_back(audio_devices[i]);
}
int default_device_index=-1;
for(int i=0; i<audio_devices.size(); i++)
{
if(audio_devices[i].outputChannels != 0){
audio_device_options_str.push_back(audio_devices[i].name);
audio_device_options_int.push_back(audio_devices[i].deviceID);
if(audio_devices[i].isDefaultOutput)
default_device_index = i;
}
}
gui->addDropdown("Audio Device", audio_device_options_str);
gui->getDropdown("Audio Device")->setLabel("Audio Device"+spacer+audio_device_options_str[0]);
gui->addTextInput("Duration (in mins)");
gui->addButton("Save and Run");
gui->addButton("Abort");
gui->onDropdownEvent(this, &ofApproxPiSetupGUI::onDropDown);
gui->onButtonEvent(this, &ofApproxPiSetupGUI::onButton);
gui->onTextInputEvent(this, &ofApproxPiSetupGUI::onTextInput);
}
//--------------------------------------------------------------
void ofApproxPiSetupGUI::onDropDown(ofxDatGuiDropdownEvent e)
{
e.target->setLabel(e.target->getName() + spacer +e.target->getSelected()->getLabel());
}
//--------------------------------------------------------------
void ofApproxPiSetupGUI::onTextInput(ofxDatGuiTextInputEvent e)
{
try
{
settings->dur_in_mins = std::stof(e.target->getText());
e.target->setText(std::to_string(settings->dur_in_mins));
}
catch(...)
{
e.target->setText("Invalid Entry! Try Again");
}
}
//--------------------------------------------------------------
void ofApproxPiSetupGUI::onButton(ofxDatGuiButtonEvent e)
{
if(e.target->getName() == "Abort")
{
settings->exit = true;
}
else
{
settings->exit = false;
if(settings->dur_in_mins == -1)
{
gui->getTextInput("Duration (in mins)")->setText("*ENTER DURATION*");
return;
}
int selected_channels = nChannels_options_int[getSelectedIndex("NChannels")];
int available_channels = output_devices[getSelectedIndex("Audio Device")].outputChannels;
if(selected_channels > available_channels)
{
gui->addLabel("***INSUFFICIENT CHANNELS ON OUTPUT DEVICE***");
return;
}
}
gui->setVisible(false);
exit();
}
//--------------------------------------------------------------
void ofApproxPiSetupGUI::exit(void)
{
settings->nChannels = nChannels_options_int[getSelectedIndex("NChannels")];
settings->windowWidth = widths_options_int[getSelectedIndex("Resolution")];
settings->windowHeight = heights_options_int[getSelectedIndex("Resolution")];
settings->audioDeviceID = audio_device_options_int[getSelectedIndex("Audio Device")];
settings->doneSetup = true;
}
//--------------------------------------------------------------
int ofApproxPiSetupGUI::getSelectedIndex(std::string dropDownName)
{
std::string label = (gui->getDropdown(dropDownName)->getSelected()->getLabel());
int index = -1;
for(int i=0; i<gui->getDropdown(dropDownName)->children.size(); i++)
{
string childLabel = gui->getDropdown(dropDownName)->children[i]->getLabel();
if((gui->getDropdown(dropDownName)->children[i]->getLabel() == label))
{
index = i;
break;
}
}
return index;
}