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
|
/**
bespoke synth, a software modular synthesizer
Copyright (C) 2021 Ryan Challinor (contact: awwbees@gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
//
// ModuleSaveData.cpp
// modularSynth
//
// Created by Ryan Challinor on 1/19/14.
//
//
#include "ModuleSaveData.h"
#include "ModularSynth.h"
#include "ofxJSONElement.h"
#include "SynthGlobals.h"
#include "DropdownList.h"
#include "RadioButton.h"
#include "Slider.h"
ModuleSaveData::ModuleSaveData()
{
}
ModuleSaveData::~ModuleSaveData()
{
for (auto iter = mValues.begin(); iter != mValues.end(); ++iter)
delete *iter;
}
ModuleSaveData::SaveVal* ModuleSaveData::GetVal(std::string prop)
{
for (auto iter = mValues.begin(); iter != mValues.end(); ++iter)
{
if ((*iter)->mProperty == prop)
return *iter;
}
//didn't find it, add
ModuleSaveData::SaveVal* newVal = new ModuleSaveData::SaveVal(prop);
mValues.push_back(newVal);
return newVal;
}
void ModuleSaveData::Save(ofxJSONElement& moduleInfo)
{
for (auto i = mValues.begin(); i != mValues.end(); ++i)
{
const SaveVal* save = *i;
assert(save);
switch (save->mType)
{
case kInt:
moduleInfo[save->mProperty] = save->mInt;
break;
case kFloat:
moduleInfo[save->mProperty] = save->mFloat;
break;
case kBool:
moduleInfo[save->mProperty] = save->mBool;
break;
case kString:
moduleInfo[save->mProperty] = save->mString;
break;
}
}
}
void ModuleSaveData::SetInt(std::string prop, int val)
{
SaveVal* save = GetVal(prop);
assert(save && save->mType == kInt);
save->mInt = val;
}
void ModuleSaveData::SetInt(std::string prop, int val, int min, int max, bool isTextField)
{
SaveVal* save = GetVal(prop);
assert(save);
save->mType = kInt;
save->mInt = val;
save->mMin = min;
save->mMax = max;
save->mIsTextField = isTextField;
}
void ModuleSaveData::SetFloat(std::string prop, float val)
{
SaveVal* save = GetVal(prop);
assert(save && save->mType == kFloat);
save->mFloat = val;
}
void ModuleSaveData::SetFloat(std::string prop, float val, float min, float max, bool isTextField)
{
SaveVal* save = GetVal(prop);
assert(save);
save->mType = kFloat;
save->mFloat = val;
save->mMin = min;
save->mMax = max;
save->mIsTextField = isTextField;
}
void ModuleSaveData::SetBool(std::string prop, bool val)
{
SaveVal* save = GetVal(prop);
assert(save);
save->mType = kBool;
save->mBool = val;
}
void ModuleSaveData::SetString(std::string prop, std::string val)
{
SaveVal* save = GetVal(prop);
assert(save);
save->mType = kString;
StringCopy(save->mString, val.c_str(), MAX_TEXTENTRY_LENGTH);
}
void ModuleSaveData::SetExtents(std::string prop, float min, float max)
{
SaveVal* save = GetVal(prop);
assert(save);
save->mMin = min;
save->mMax = max;
}
bool ModuleSaveData::HasProperty(std::string prop)
{
for (auto i = mValues.begin(); i != mValues.end(); ++i)
{
if ((*i)->mProperty == prop)
return true;
}
return false;
}
int ModuleSaveData::GetInt(std::string prop)
{
const SaveVal* save = GetVal(prop);
assert(save);
assert(save->mType == kInt);
return save->mInt;
}
float ModuleSaveData::GetFloat(std::string prop)
{
const SaveVal* save = GetVal(prop);
assert(save);
assert(save->mType == kFloat);
return save->mFloat;
}
bool ModuleSaveData::GetBool(std::string prop)
{
const SaveVal* save = GetVal(prop);
assert(save);
assert(save->mType == kBool);
return save->mBool;
}
std::string ModuleSaveData::GetString(std::string prop)
{
const SaveVal* save = GetVal(prop);
assert(save);
assert(save->mType == kString);
return save->mString;
}
int ModuleSaveData::LoadInt(std::string prop, const ofxJSONElement& moduleInfo, int defaultValue, int min, int max, bool isTextField)
{
int val = defaultValue;
try
{
if (!moduleInfo[prop].isNull())
val = moduleInfo[prop].asInt();
}
catch (Json::LogicError& e)
{
TheSynth->LogEvent(__PRETTY_FUNCTION__ + std::string(" json error: ") + e.what(), kLogEventType_Error);
}
SetInt(prop, val, min, max, isTextField);
return val;
}
int ModuleSaveData::LoadInt(std::string prop, const ofxJSONElement& moduleInfo, int defaultValue, IntSlider* slider, bool isTextField)
{
int min = 0;
int max = 10;
if (slider)
slider->GetRange(min, max);
return LoadInt(prop, moduleInfo, defaultValue, min, max, isTextField);
}
float ModuleSaveData::LoadFloat(std::string prop, const ofxJSONElement& moduleInfo, float defaultValue, float min, float max, bool isTextField)
{
float val = defaultValue;
try
{
if (!moduleInfo[prop].isNull())
val = moduleInfo[prop].asDouble();
}
catch (Json::LogicError& e)
{
TheSynth->LogEvent(__PRETTY_FUNCTION__ + std::string(" json error: ") + e.what(), kLogEventType_Error);
}
SetFloat(prop, val, min, max, isTextField);
return val;
}
float ModuleSaveData::LoadFloat(std::string prop, const ofxJSONElement& moduleInfo, float defaultValue, FloatSlider* slider, bool isTextField)
{
float min = 0;
float max = 1;
if (slider)
slider->GetRange(min, max);
return LoadFloat(prop, moduleInfo, defaultValue, min, max, isTextField);
}
bool ModuleSaveData::LoadBool(std::string prop, const ofxJSONElement& moduleInfo, bool defaultValue)
{
bool val = defaultValue;
try
{
if (!moduleInfo[prop].isNull())
val = moduleInfo[prop].asBool();
}
catch (Json::LogicError& e)
{
TheSynth->LogEvent(__PRETTY_FUNCTION__ + std::string(" json error: ") + e.what(), kLogEventType_Error);
}
SetBool(prop, val);
return val;
}
std::string ModuleSaveData::LoadString(std::string prop, const ofxJSONElement& moduleInfo, std::string defaultValue, FillDropdownFn fillFn)
{
std::string val = defaultValue;
try
{
if (!moduleInfo[prop].isNull())
val = moduleInfo[prop].asString();
}
catch (Json::LogicError& e)
{
TheSynth->LogEvent(__PRETTY_FUNCTION__ + std::string(" json error: ") + e.what(), kLogEventType_Error);
}
SetString(prop, val);
SaveVal* save = GetVal(prop);
assert(save);
save->mFillDropdownFn = fillFn;
return val;
}
void ModuleSaveData::UpdatePropertyMax(std::string prop, float max)
{
if (HasProperty(prop))
{
SaveVal* save = GetVal(prop);
assert(save);
save->mMax = max;
}
}
void ModuleSaveData::SetEnumMapFromList(std::string prop, IUIControl* list)
{
SaveVal* save = GetVal(prop);
assert(save);
DropdownList* dropdownList = dynamic_cast<DropdownList*>(list);
RadioButton* radioButton = dynamic_cast<RadioButton*>(list);
if (dropdownList)
save->mEnumValues = dropdownList->GetEnumMap();
if (radioButton)
save->mEnumValues = radioButton->GetEnumMap();
}
void ModuleSaveData::SetEnumMap(std::string prop, EnumMap* map)
{
SaveVal* save = GetVal(prop);
assert(save);
save->mEnumValues = *map;
}
|