-
Notifications
You must be signed in to change notification settings - Fork 0
/
udata.pas
328 lines (282 loc) · 7.2 KB
/
udata.pas
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
unit uData;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Contnrs, LResources, Math;
const
ALL_CATEGORIES = 'all';
type
{ TPreset }
TPreset = class
private
FCC0: integer;
FPGM: integer;
FCategory: string;
FCharacters: string;
FCharacterList: TStrings;
FPresetName: string;
procedure UpdatePresetCharacterList;
procedure SetCharacters(AValue: String);
public
constructor Create;
destructor Destroy; override;
function MatchesCharacter(SearchCharacter: String): Boolean;
property CC0: integer read FCC0 write FCC0;
property PGM: integer read FPGM write FPGM;
property Category: string read FCategory write FCategory;
property Characters: String read FCharacters write SetCharacters;
property CharacterList: TStrings read FCharacterList;
property PresetName: string read FPresetName write FPresetName;
end;
PPreset = ^TPreset;
{ TCategory }
TCategory = class
private
FName: string;
public
constructor Create(AName: string);
property Name: string read FName write FName;
end;
PCategory = ^TCategory;
{ TPresetList }
TPresetList = class(TFPObjectList)
public
procedure Update(ALines: TStrings);
end;
{ TSortablePresetList }
TSortablePresetList = class(TPresetList)
public
procedure SortByPresetName;
end;
{ TPresetData }
TPresetData = class
private
// private members
FCurrentCategory: string;
FPresetsByCategory: TPresetList;
FPresetsByPresetName: TSortablePresetList;
Flines: TStrings;
FSortedAllPresetNames: TStrings;
FCategories: TFPObjectList;
FCharacterList: TStrings;
// private methods
procedure CreateObjects;
procedure FreeObjects;
procedure BuildCategories;
procedure SetCurrentCategory(AValue: string);
procedure UpdateCharacterList;
public
// lifetime
constructor Create;
destructor Destroy; override;
// public methods
procedure LoadPresetList;
procedure UpdateAllPresetsList;
// public properties
property Categories: TFPObjectList read FCategories;
property CharacterList: TStrings read FCharacterList;
property CurrentCategory: string read FCurrentCategory write SetCurrentCategory;
property Lines: TStrings read Flines;
property PresetsByCategory: TPresetList read FPresetsByCategory;
property PresetsByPresetName: TSortablePresetList read FPresetsByPresetName;
end;
implementation
{$R 'OsmosePresetsResources.res'}
{ TPreset }
constructor TPreset.Create;
begin
inherited;
FCharacterList := TStringList.Create;
TStringList(FCharacterList).Duplicates := dupIgnore;
end;
destructor TPreset.Destroy;
begin
FCharacterList.Free;
inherited Destroy;
end;
function TPreset.MatchesCharacter(SearchCharacter: String): Boolean;
var
Character: string;
begin
for Character in FCharacterList do
if Character = SearchCharacter then
MatchesCharacter := True;
MatchesCharacter := False;
end;
procedure TPreset.UpdatePresetCharacterList;
var
CharacterArray: array of string;
Index: Integer;
begin
FCharacterList.Clear;
CharacterArray := FCharacters.Split(' + ');
for Index := 0 to High(CharacterArray) do
if CharacterArray[Index] <> '' then
FCharacterList.Add(CharacterArray[Index]);
end;
procedure TPreset.SetCharacters(AValue: String);
begin
if FCharacters = AValue then Exit;
FCharacters := AValue;
UpdatePresetCharacterList;
end;
{$R 'OsmoseCharactersResources.res'}
{ TCategory }
constructor TCategory.Create(AName: string);
begin
inherited Create;
FName := AName;
end;
{ TPresetList }
// 30, 1, "acid bass", "aggressive + analog + distorted + stereo"
procedure TPresetList.Update(ALines: TStrings);
var
Line: string;
Parts: TStringArray;
Preset: TPreset;
Category: string;
begin
Clear;
for Line in ALines do
begin
if not Line.Contains(',') then
Category := Line
else
begin
if Line.Contains(',') then
begin
Parts := Line.Split(',');
Preset := TPreset.Create;
try
Preset.CC0 := Parts[0].ToInteger;
Preset.PGM := Parts[1].ToInteger;
Preset.PresetName := Parts[2].Trim.DeQuotedString(#34).Trim;
Preset.Category := Category;
Preset.Characters := Parts[3].Trim.DeQuotedString(#34).Trim;
finally
Add(Preset);
end;
end;
end;
end;
end;
{ TSortablePresetList }
function Compare(Item1, Item2: Pointer): Integer;
var
Preset1: TPreset absolute Item1;
Preset2: TPreset absolute Item2;
begin
if Preset1.PresetName < Preset2.PresetName then
Result := -1
else if Preset1.PresetName > Preset2.PresetName then
Result := 1
else
Result := 0;
end;
procedure TSortablePresetList.SortByPresetName;
begin
Sort(@Compare);
end;
{ TPresetData }
constructor TPresetData.Create;
begin
inherited;
FCurrentCategory := '';
CreateObjects;
LoadPresetList;
FPresetsByCategory.Update(Flines);
FPresetsByPresetName.Update(Flines);
FPresetsByPresetName.SortByPresetName;
BuildCategories;
UpdateCharacterList;
end;
destructor TPresetData.Destroy;
begin
FreeObjects;
inherited;
end;
procedure TPresetData.CreateObjects;
begin
FPresetsByCategory := TPresetList.Create(True);
FPresetsByPresetName := TSortablePresetList.Create(True);
FLines := TStringList.Create;
FSortedAllPresetNames := TStringList.Create;
FCategories := TFPObjectList.Create(True);
FCharacterList := TStringList.Create;
TStringList(FCharacterList).Sorted := True;
end;
procedure TPresetData.FreeObjects;
begin
FPresetsByCategory.Free;
FPresetsByPresetName.Free;
Flines.Free;
FSortedAllPresetNames.Free;
FCategories.Free;
FCharacterList.Free;
end;
procedure TPresetData.BuildCategories;
var
Line: string;
Category: TCategory;
begin
FCategories.Clear;
FCategories.Add(TCategory.Create(ALL_CATEGORIES));
for Line in FLines do
begin
if not Line.Contains(',') then
FCategories.Add(TCategory.Create(Line));
end;
if FCategories.Count > 0 then
FCurrentCategory := TCategory(FCategories[0]).Name;
end;
procedure TPresetData.SetCurrentCategory(AValue: string);
begin
if FCurrentCategory = AValue then Exit;
FCurrentCategory := AValue;
UpdateCharacterList;
end;
procedure TPresetData.UpdateCharacterList;
var
Preset: TPreset;
PresetIndex: Integer;
Character: string;
CharToAdd: string;
begin
FCharacterList.Clear;
for PresetIndex := 0 to Pred(FPresetsByCategory.Count) do
begin
Preset := TPreset(FPresetsByCategory[PresetIndex]);
if (Preset.Category = FCurrentCategory) or (FCurrentCategory = ALL_CATEGORIES) then
begin
for Character in Preset.CharacterList do
begin
CharToAdd := specialize IfThen<string>(Character = 'UNASSIGNED', '_UNASSIGNED', Character);
FCharacterList.Add(CharToAdd);
end;
end;
end;
end;
procedure TPresetData.LoadPresetList;
{$ifdef windows}
const
RT_RCDATA = PChar(10);
{$endif}
var
rs: TResourceStream;
begin
rs := TResourceStream.Create(HInstance, 'OSMOSEPRESETSRESOURCES', RT_RCDATA);
try
FLines.LoadFromStream(rs);
finally
rs.Free;
end;
end;
procedure TPresetData.UpdateAllPresetsList;
var
Line: string;
begin
FSortedAllPresetNames.Clear;
for Line in FLines do
FSortedAllPresetNames.Add(Line);
end;
end.