Ignore data presets files
[SugarCubes.git] / _Presets.pde
CommitLineData
e0794d3a
MS
1interface PresetListener {
2 public void onPresetLoaded(Preset preset);
3 public void onPresetDirty(Preset preset);
4 public void onPresetStored(Preset preset);
5}
6
7class PresetManager {
8
9 public static final int NUM_PRESETS = 8;
10 public static final String FILENAME = "data/presets.txt";
11 public static final String DELIMITER = "\t";
12
13 private final Preset[] presets = new Preset[NUM_PRESETS];
14 private final List<PresetListener> listeners = new ArrayList<PresetListener>();
15
16 private LXPattern loadedPreset = null;
17
18 PresetManager() {
19 for (int i = 0; i < presets.length; ++i) {
20 presets[i] = new Preset(this, i);
21 }
22 String[] values = loadStrings(FILENAME);
23 if (values == null) {
24 write();
25 } else {
26 int i = 0;
27 for (String serialized : values) {
28 presets[i++].load(serialized);
29 if (i >= NUM_PRESETS) {
30 break;
31 }
32 }
33 }
34 }
35
36 public void addListener(PresetListener listener) {
37 listeners.add(listener);
38 }
39
40 public void select(int index) {
41 presets[index].select();
42 }
43
44 public void store(int index) {
45 presets[index].store(midiEngine.getFocusedPattern());
46 }
47
48 public void onPresetLoaded(Preset preset, LXPattern pattern) {
49 for (PresetListener listener : listeners) {
50 listener.onPresetLoaded(preset);
51 }
52 }
53
54 public void write() {
55 String[] lines = new String[NUM_PRESETS];
56 int i = 0;
57 for (Preset preset : presets) {
58 lines[i++] = preset.serialize();
59 }
60 saveStrings(FILENAME, lines);
61 }
62}
63
64class Preset {
65
66 final PresetManager manager;
67 final int index;
68
69 String className;
70 final Map<String, Float> parameters = new HashMap<String, Float>();
71
72 Preset(PresetManager manager, int index) {
73 this.manager = manager;
74 this.index = index;
75 }
76
77 public void load(String serialized) {
78 className = null;
79 parameters.clear();
80 try {
81 String[] parts = serialized.split(PresetManager.DELIMITER);
82 className = parts[0];
83 int i = 1;
84 while (i < parts.length - 1) {
85 parameters.put(parts[i], Float.parseFloat(parts[i+1]));
86 i += 2;
87 }
88 } catch (Exception x) {
89 className = null;
90 parameters.clear();
91 }
92 }
93
94 public String serialize() {
95 if (className == null) {
96 return "null";
97 }
98 String val = className + PresetManager.DELIMITER;
99 for (String pKey : parameters.keySet()) {
100 val += pKey + PresetManager.DELIMITER + parameters.get(pKey) + PresetManager.DELIMITER;
101 }
102 return val;
103 }
104
105 public void store(LXPattern pattern) {
106 className = null;
107 parameters.clear();
108 className = pattern.getClass().getName();
109 for (LXParameter p : pattern.getParameters()) {
110 parameters.put(p.getLabel(), p.getValuef());
111 }
112 manager.write();
113 }
114
115 public void select() {
116 Engine.Deck deck = midiEngine.getFocusedDeck();
117 for (LXPattern pattern : deck.getPatterns()) {
118 if (pattern.getClass().getName().equals(className)) {
119 for (String pLabel : parameters.keySet()) {
120 for (LXParameter p : pattern.getParameters()) {
121 if (p.getLabel().equals(pLabel)) {
122 p.setValue(parameters.get(pLabel));
123 }
124 }
125 }
126 deck.goPattern(pattern);
127 manager.onPresetLoaded(this, pattern);
128 break;
129 }
130 }
131 }
132}
133