-
Notifications
You must be signed in to change notification settings - Fork 0
/
PatchManagerComponent.cpp
198 lines (182 loc) · 6.24 KB
/
PatchManagerComponent.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
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
/*
==============================================================================
PatchManagerComponent.cpp
Created: 10 Oct 2020 9:42:19pm
Author: Hayden Setlik
==============================================================================
*/
#include "PatchManagerComponent.h"
void PatchSelector::initialize()
{
int nextId = 1;
if(lib->bassGroup.patches.size() > 0)
{
addSectionHeading("Bass");
addSeparator();
for(auto f : lib->bassGroup.patches)
{
std::unique_ptr<juce::XmlElement> currentXml = juce::parseXML(f);
if(currentXml != nullptr)
{
if(currentXml->hasAttribute("HexFmPatchName"))
{
auto presetName = currentXml->getStringAttribute("HexFmPatchName");
patchNames.add(presetName);
addItem(presetName, nextId);
++nextId;
}
}
}
}
if(lib->leadGroup.patches.size() > 0)
{
addSectionHeading("Lead");
addSeparator();
for(auto f : lib->leadGroup.patches)
{
std::unique_ptr<juce::XmlElement> currentXml = juce::parseXML(f);
if(currentXml != nullptr)
{
if(currentXml->hasAttribute("HexFmPatchName"))
{
auto presetName = currentXml->getStringAttribute("HexFmPatchName");
patchNames.add(presetName);
addItem(presetName, nextId);
++nextId;
}
}
}
}
if(lib->chordGroup.patches.size() > 0)
{
addSectionHeading("Chord");
addSeparator();
for(auto f : lib->chordGroup.patches)
{
std::unique_ptr<juce::XmlElement> currentXml = juce::parseXML(f);
if(currentXml != nullptr)
{
if(currentXml->hasAttribute("HexFmPatchName"))
{
auto presetName = currentXml->getStringAttribute("HexFmPatchName");
patchNames.add(presetName);
addItem(presetName, nextId);
++nextId;
}
}
}
}
if(lib->padGroup.patches.size() > 0)
{
addSectionHeading("Pad");
addSeparator();
for(auto f : lib->padGroup.patches)
{
std::unique_ptr<juce::XmlElement> currentXml = juce::parseXML(f);
if(currentXml != nullptr)
{
if(currentXml->hasAttribute("HexFmPatchName"))
{
auto presetName = currentXml->getStringAttribute("HexFmPatchName");
patchNames.add(presetName);
addItem(presetName, nextId);
++nextId;
}
}
}
}
}
PatchLoader::PatchLoader(HexFmAudioProcessor* proc, juce::Component* patchDlg) :
processor(proc),
saveDialogComponent(patchDlg)
{
auto appFolder = juce::File::getSpecialLocation(juce::File::userDocumentsDirectory);
appFolder.setAsCurrentWorkingDirectory();
auto patchFolder = appFolder.getChildFile("HexFM_Patches");
if(patchFolder.exists() && patchFolder.isDirectory())
{
//printf("patch folder exists\n");
patchFolder.setAsCurrentWorkingDirectory();
}
else
{
patchFolder.createDirectory();
patchFolder.setAsCurrentWorkingDirectory();
//printf("patch folder created\n");
}
presetFolder = patchFolder;
//auto fldr = presetFolder.getFileName().toUTF8();
//printf("Presets are at: %s\n", fldr);
addAndMakeVisible(&patchSelector);
addAndMakeVisible(&nextPatchButton);
addAndMakeVisible(&lastPatchButton);
addAndMakeVisible(&saveButton);
nextPatchButton.addListener(this);
lastPatchButton.addListener(this);
saveButton.addListener(this);
patchSelector.addListener(this);
nextPatchButton.setButtonText(">");
lastPatchButton.setButtonText("<");
saveButton.setButtonText("Save");
}
void PatchLoader::resized()
{
auto dY = getHeight() / 8;
//aspect ratio is 2/19 -> 6/57
//component width is 57 * dY
lastPatchButton.setBounds(dY, dY, 4 * dY, 4 * dY);
nextPatchButton.setBounds(5 * dY, dY, 4 * dY, 4 * dY);
patchSelector.setBounds(11 * dY, dY, 30 * dY, 4 * dY);
saveButton.setBounds(44 * dY, dY, 6 * dY, 4 * dY);
}
void PatchLoader::savePreset(juce::String name, juce::String type)
{
auto fileName = juce::File::createLegalFileName(name);
if(presetFolder.exists())
{
auto file = presetFolder.getChildFile(fileName);
bool isNew = false;
if(!file.existsAsFile())
{
file.create();
isNew = true;
}
auto state = processor->tree.copyState();
std::unique_ptr<juce::XmlElement> xml = state.createXml();
xml->setAttribute("HexFmPatchName", name);
xml->setAttribute("HexFmPatchType", type);
xml->writeTo(file);
patchSelector.reInitList();
}
}
void PatchLoader::loadPreset(juce::String presetName)
{
auto presetFiles = presetFolder.findChildFiles(juce::File::findFiles, true);
if(presetFiles.size() > 0)
{
for(int i = 0; i < presetFiles.size(); ++i)
{
auto currentFile = presetFiles.getUnchecked(i);
std::unique_ptr<juce::XmlElement> currentXml = juce::parseXML(currentFile);
if(currentXml != nullptr)
{
if(currentXml->hasAttribute("HexFmPatchName"))
{
auto checkName = currentXml->getStringAttribute("HexFmPatchName");
if(checkName == presetName)
{
auto* vTree = &processor->tree;
vTree->replaceState(juce::ValueTree::fromXml(*currentXml));
}
}
}
}
}
}
void PatchLoader::comboBoxChanged(juce::ComboBox* box)
{
if(box == &patchSelector)
{
loadPreset(patchSelector.getText());
}
}