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@18
|
20 }
|
rt300@18
|
21
|
rt300@18
|
22 //----------------------------------------------------------------------------
|
rt300@18
|
23 void ExplorePresetManager::goToFirstEmptySlot(){
|
rt300@18
|
24 currentPresetSlotIndex = 0;
|
rt300@18
|
25
|
rt300@18
|
26 // loop thru slots, find first empty slot
|
rt300@18
|
27 }
|
rt300@18
|
28 //----------------------------------------------------------------------------
|
rt300@18
|
29 bool ExplorePresetManager::writeValuesToSlot(vector<int> values){
|
rt300@18
|
30 getPresetSlotAtIndex(currentPresetSlotIndex)->setValues(values);
|
rt300@18
|
31
|
rt300@18
|
32 // now put it into a real preset, so it will be saved to file
|
rt300@18
|
33 savePreset(getPresetSlotAtIndex(currentPresetSlotIndex));
|
rt300@18
|
34
|
rt300@18
|
35 filledSlots++;
|
rt300@18
|
36 currentPresetSlotIndex++;
|
rt300@18
|
37 if(filledSlots == MAX_PRESETS){
|
rt300@18
|
38 cout << "FINISHED EXP STAGE!" << endl;
|
rt300@19
|
39 currentPresetSlotIndex = 0;
|
rt300@18
|
40 return true;
|
rt300@18
|
41 }
|
rt300@18
|
42 return false;
|
rt300@18
|
43 }
|
rt300@18
|
44 //--------------------------------------------------------------------------------
|
rt300@19
|
45 PresetSlot* ExplorePresetManager::getCurrentPresetSlot(){
|
rt300@18
|
46
|
rt300@18
|
47 return getPresetSlotAtIndex(currentPresetSlotIndex);
|
rt300@18
|
48 }
|
rt300@18
|
49 //----------------------------------------------------------------------------
|
rt300@18
|
50
|
rt300@18
|
51 PresetSlot* ExplorePresetManager::getPresetSlotAtIndex(int index){
|
rt300@18
|
52
|
rt300@18
|
53 if (index >= thePresetSlots.size()){
|
rt300@18
|
54 cout << "ERROR: index " << index << " exceeds number of presetslots " << thePresets.size() << endl;
|
rt300@18
|
55 return NULL;
|
rt300@18
|
56 }else{
|
rt300@18
|
57 return thePresetSlots[index];
|
rt300@18
|
58
|
rt300@18
|
59 }
|
rt300@18
|
60 };
|
rt300@18
|
61 //---------------------------------------------------------------------------
|
rt300@18
|
62 void ExplorePresetManager::initPresetSlots(){
|
rt300@18
|
63
|
rt300@18
|
64 presetSlotFilename = ofFilePath::getAbsolutePath(ofToDataPath("presetSlots.json"));
|
rt300@18
|
65
|
rt300@18
|
66 // set up preset slots with names and images
|
rt300@18
|
67 Json::Value root;
|
rt300@18
|
68 Json::Reader reader;
|
rt300@18
|
69
|
rt300@18
|
70
|
rt300@18
|
71 ifstream theFile(presetSlotFilename.c_str());
|
rt300@18
|
72 stringstream fileText;
|
rt300@18
|
73 string line;
|
rt300@18
|
74 if(!theFile){
|
rt300@18
|
75 cout<<"can't find presetSlot file \n";
|
rt300@18
|
76 return;
|
rt300@18
|
77 }else{
|
rt300@18
|
78
|
rt300@18
|
79 while(theFile){
|
rt300@18
|
80 theFile >> line;
|
rt300@18
|
81 // cout << line << "\n"; // lots?
|
rt300@18
|
82 fileText << line;
|
rt300@18
|
83
|
rt300@18
|
84 }
|
rt300@18
|
85
|
rt300@18
|
86 theFile.close();
|
rt300@18
|
87 }
|
rt300@18
|
88
|
rt300@18
|
89 bool parsingSuccessful = reader.parse( fileText.str(), root );
|
rt300@18
|
90
|
rt300@18
|
91 if ( !parsingSuccessful )
|
rt300@18
|
92 {
|
rt300@18
|
93 // report to the user the failure and their locations in the document.
|
rt300@18
|
94 std::cout << "Failed to parse preset slot JSON: \n"
|
rt300@18
|
95 << reader.getFormattedErrorMessages();
|
rt300@18
|
96 return;
|
rt300@18
|
97 }
|
rt300@18
|
98
|
rt300@18
|
99 // now put into variables
|
rt300@18
|
100 const Json::Value jv = root["presetSlots"];
|
rt300@18
|
101
|
rt300@18
|
102 for ( int index = 0; index < jv.size(); ++index ){
|
rt300@18
|
103 string name = jv[index]["name"].asString();
|
rt300@18
|
104 string imageFileName = jv[index]["imageFileName"].asString();
|
rt300@18
|
105 vector<int> empty;
|
rt300@18
|
106 generatePresetSlot(name, imageFileName);
|
rt300@18
|
107 }
|
rt300@18
|
108
|
rt300@18
|
109 // now look at existing presets to see if slots are filled
|
rt300@18
|
110 fillSlotsWithLoadedPresets();
|
rt300@18
|
111
|
rt300@18
|
112 filledSlots = 0;
|
rt300@18
|
113 }
|
rt300@18
|
114 //-----------------------------------------------------------------------------
|
rt300@18
|
115
|
rt300@18
|
116 void ExplorePresetManager::fillSlotsWithLoadedPresets(){
|
rt300@18
|
117 for(auto pi = thePresets.begin(); pi < thePresets.end(); pi++){
|
rt300@18
|
118 string presetName = (*pi)->name;
|
rt300@18
|
119
|
rt300@18
|
120 for(auto psi = thePresetSlots.begin(); psi < thePresetSlots.end(); psi++){
|
rt300@18
|
121 string slotName = (*psi)->name;
|
rt300@18
|
122 if (slotName == presetName){
|
rt300@18
|
123 fillSlotFromLoadedPreset(psi, pi);
|
rt300@18
|
124 }
|
rt300@18
|
125 }
|
rt300@18
|
126 }
|
rt300@18
|
127 }
|
rt300@18
|
128 //-----------------------------------------------------------------------------
|
rt300@18
|
129
|
rt300@19
|
130 void ExplorePresetManager::fillSlotFromLoadedPreset(vector<PresetSlot*>::iterator slotToFill, vector<Preset*>::iterator preset){
|
rt300@19
|
131 (*slotToFill)->setValues((*preset)->CCValues);
|
rt300@19
|
132 (*slotToFill)->isFilled = true;
|
rt300@19
|
133 filledSlots++;
|
rt300@19
|
134
|
rt300@19
|
135 if(filledSlots == MAX_PRESETS){
|
rt300@19
|
136 cout << "ALL SLOTS ARE FILLED!" << endl;
|
rt300@19
|
137 }
|
rt300@19
|
138
|
rt300@19
|
139
|
rt300@18
|
140 }
|
rt300@18
|
141 //-----------------------------------------------------------------------------
|
rt300@18
|
142 void ExplorePresetManager::generatePresetSlot(const string name, const string imagefn){
|
rt300@18
|
143 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
|
144 thePresetSlots.push_back(new PresetSlot(values, name, nextID, eventLogger.userName, eventLogger.deviceID, imagefn));
|
rt300@18
|
145
|
rt300@18
|
146 }
|