rt300@6
|
1 //
|
rt300@6
|
2 // ExplorePresetManager.mm
|
rt300@6
|
3 // riftathon
|
rt300@6
|
4 //
|
rt300@6
|
5 // Created by Robert Tubb on 16/10/2014.
|
rt300@6
|
6 //
|
rt300@6
|
7 //
|
rt300@6
|
8
|
rt300@6
|
9 #include "ExplorePresetManager.h"
|
rt300@6
|
10 ExplorePresetManager expPresetManager;
|
rt300@18
|
11
|
rt300@18
|
12 //-----------------------------------------------------------------------------
|
rt300@18
|
13
|
rt300@18
|
14 void ExplorePresetManager::onAppLoad(){
|
rt300@18
|
15 // check for already saved stuff
|
rt300@18
|
16 startLoadAll();
|
rt300@19
|
17 initPresetSlots();
|
rt300@19
|
18
|
rt300@18
|
19
|
rt300@31
|
20 makeNeutralPreset();
|
rt300@31
|
21
|
rt300@18
|
22 }
|
rt300@18
|
23
|
rt300@18
|
24 //----------------------------------------------------------------------------
|
rt300@18
|
25 void ExplorePresetManager::goToFirstEmptySlot(){
|
rt300@18
|
26 currentPresetSlotIndex = 0;
|
rt300@18
|
27
|
rt300@18
|
28 // loop thru slots, find first empty slot
|
rt300@18
|
29 }
|
rt300@18
|
30 //----------------------------------------------------------------------------
|
rt300@18
|
31 bool ExplorePresetManager::writeValuesToSlot(vector<int> values){
|
rt300@18
|
32 getPresetSlotAtIndex(currentPresetSlotIndex)->setValues(values);
|
rt300@18
|
33
|
rt300@18
|
34 // now put it into a real preset, so it will be saved to file
|
rt300@18
|
35 savePreset(getPresetSlotAtIndex(currentPresetSlotIndex));
|
rt300@18
|
36
|
rt300@44
|
37 if (!getPresetSlotAtIndex(currentPresetSlotIndex)->isFilled){
|
rt300@44
|
38 filledSlots++;
|
rt300@44
|
39 }
|
rt300@44
|
40
|
rt300@18
|
41 if(filledSlots == MAX_PRESETS){
|
rt300@18
|
42 cout << "FINISHED EXP STAGE!" << endl;
|
rt300@19
|
43 currentPresetSlotIndex = 0;
|
rt300@18
|
44 return true;
|
rt300@18
|
45 }
|
rt300@18
|
46 return false;
|
rt300@18
|
47 }
|
rt300@18
|
48 //--------------------------------------------------------------------------------
|
rt300@44
|
49 void ExplorePresetManager::nextSlot(){
|
rt300@44
|
50 currentPresetSlotIndex++;
|
rt300@44
|
51 if (currentPresetSlotIndex >= thePresetSlots.size()){
|
rt300@44
|
52 currentPresetSlotIndex = 0;
|
rt300@44
|
53 }
|
rt300@44
|
54 }
|
rt300@44
|
55
|
rt300@44
|
56 //--------------------------------------------------------------------------------
|
rt300@19
|
57 PresetSlot* ExplorePresetManager::getCurrentPresetSlot(){
|
rt300@18
|
58
|
rt300@18
|
59 return getPresetSlotAtIndex(currentPresetSlotIndex);
|
rt300@18
|
60 }
|
rt300@18
|
61 //----------------------------------------------------------------------------
|
rt300@18
|
62
|
rt300@18
|
63 PresetSlot* ExplorePresetManager::getPresetSlotAtIndex(int index){
|
rt300@18
|
64
|
rt300@18
|
65 if (index >= thePresetSlots.size()){
|
rt300@18
|
66 cout << "ERROR: index " << index << " exceeds number of presetslots " << thePresets.size() << endl;
|
rt300@42
|
67 return thePresetSlots.back();
|
rt300@18
|
68 return NULL;
|
rt300@18
|
69 }else{
|
rt300@18
|
70 return thePresetSlots[index];
|
rt300@18
|
71
|
rt300@18
|
72 }
|
rt300@18
|
73 };
|
rt300@18
|
74 //---------------------------------------------------------------------------
|
rt300@18
|
75 void ExplorePresetManager::initPresetSlots(){
|
rt300@18
|
76
|
rt300@18
|
77 presetSlotFilename = ofFilePath::getAbsolutePath(ofToDataPath("presetSlots.json"));
|
rt300@18
|
78
|
rt300@18
|
79 // set up preset slots with names and images
|
rt300@18
|
80 Json::Value root;
|
rt300@18
|
81 Json::Reader reader;
|
rt300@18
|
82
|
rt300@18
|
83
|
rt300@18
|
84 ifstream theFile(presetSlotFilename.c_str());
|
rt300@18
|
85 stringstream fileText;
|
rt300@18
|
86 string line;
|
rt300@18
|
87 if(!theFile){
|
rt300@18
|
88 cout<<"can't find presetSlot file \n";
|
rt300@18
|
89 return;
|
rt300@18
|
90 }else{
|
rt300@18
|
91
|
rt300@18
|
92 while(theFile){
|
rt300@18
|
93 theFile >> line;
|
rt300@18
|
94 // cout << line << "\n"; // lots?
|
rt300@18
|
95 fileText << line;
|
rt300@18
|
96
|
rt300@18
|
97 }
|
rt300@18
|
98
|
rt300@18
|
99 theFile.close();
|
rt300@18
|
100 }
|
rt300@18
|
101
|
rt300@18
|
102 bool parsingSuccessful = reader.parse( fileText.str(), root );
|
rt300@18
|
103
|
rt300@18
|
104 if ( !parsingSuccessful )
|
rt300@18
|
105 {
|
rt300@18
|
106 // report to the user the failure and their locations in the document.
|
rt300@18
|
107 std::cout << "Failed to parse preset slot JSON: \n"
|
rt300@18
|
108 << reader.getFormattedErrorMessages();
|
rt300@18
|
109 return;
|
rt300@18
|
110 }
|
rt300@18
|
111
|
rt300@18
|
112 // now put into variables
|
rt300@18
|
113 const Json::Value jv = root["presetSlots"];
|
rt300@18
|
114
|
rt300@18
|
115 for ( int index = 0; index < jv.size(); ++index ){
|
rt300@18
|
116 string name = jv[index]["name"].asString();
|
rt300@18
|
117 string imageFileName = jv[index]["imageFileName"].asString();
|
rt300@18
|
118 vector<int> empty;
|
rt300@18
|
119 generatePresetSlot(name, imageFileName);
|
rt300@18
|
120 }
|
rt300@18
|
121
|
rt300@18
|
122 // now look at existing presets to see if slots are filled
|
rt300@18
|
123 fillSlotsWithLoadedPresets();
|
rt300@18
|
124
|
rt300@18
|
125 filledSlots = 0;
|
rt300@18
|
126 }
|
rt300@18
|
127 //-----------------------------------------------------------------------------
|
rt300@32
|
128 vector<string> drumPresetNames(){
|
rt300@32
|
129 vector<string> vs;
|
rt300@32
|
130
|
rt300@32
|
131 vs.push_back("boom kick");
|
rt300@32
|
132 vs.push_back("punch kick");
|
rt300@32
|
133 vs.push_back("snare");
|
rt300@32
|
134 vs.push_back("clap");
|
rt300@32
|
135
|
rt300@32
|
136 vs.push_back("closed hihat");
|
rt300@32
|
137 vs.push_back("open hihat");
|
rt300@32
|
138 vs.push_back("cowbell");
|
rt300@32
|
139 vs.push_back("bongo");
|
rt300@32
|
140
|
rt300@32
|
141
|
rt300@32
|
142 }
|
rt300@32
|
143 //-----------------------------------------------------------------------------
|
rt300@18
|
144
|
rt300@18
|
145 void ExplorePresetManager::fillSlotsWithLoadedPresets(){
|
rt300@18
|
146 for(auto pi = thePresets.begin(); pi < thePresets.end(); pi++){
|
rt300@18
|
147 string presetName = (*pi)->name;
|
rt300@18
|
148
|
rt300@18
|
149 for(auto psi = thePresetSlots.begin(); psi < thePresetSlots.end(); psi++){
|
rt300@18
|
150 string slotName = (*psi)->name;
|
rt300@18
|
151 if (slotName == presetName){
|
rt300@18
|
152 fillSlotFromLoadedPreset(psi, pi);
|
rt300@18
|
153 }
|
rt300@18
|
154 }
|
rt300@18
|
155 }
|
rt300@18
|
156 }
|
rt300@18
|
157 //-----------------------------------------------------------------------------
|
rt300@18
|
158
|
rt300@19
|
159 void ExplorePresetManager::fillSlotFromLoadedPreset(vector<PresetSlot*>::iterator slotToFill, vector<Preset*>::iterator preset){
|
rt300@19
|
160 (*slotToFill)->setValues((*preset)->CCValues);
|
rt300@19
|
161 (*slotToFill)->isFilled = true;
|
rt300@19
|
162 filledSlots++;
|
rt300@19
|
163
|
rt300@19
|
164 if(filledSlots == MAX_PRESETS){
|
rt300@19
|
165 cout << "ALL SLOTS ARE FILLED!" << endl;
|
rt300@19
|
166 }
|
rt300@19
|
167
|
rt300@19
|
168
|
rt300@18
|
169 }
|
rt300@18
|
170 //-----------------------------------------------------------------------------
|
rt300@18
|
171 void ExplorePresetManager::generatePresetSlot(const string name, const string imagefn){
|
rt300@18
|
172 vector<int> values = makeVector8(int(rand() % 128),int(rand() % 128),int(rand() % 128),int(rand() % 128),int(rand() % 128),0,0,0); // empty
|
rt300@18
|
173 thePresetSlots.push_back(new PresetSlot(values, name, nextID, eventLogger.userName, eventLogger.deviceID, imagefn));
|
rt300@18
|
174
|
rt300@18
|
175 }
|