-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPresets.j
More file actions
217 lines (181 loc) · 8.23 KB
/
Presets.j
File metadata and controls
217 lines (181 loc) · 8.23 KB
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
/*
This file is part of FrACT10, a vision test battery.
© 2022 Michael Bach, bach@uni-freiburg.de, <https://michaelbach.de>
Presets.j
*/
@import <Foundation/CPObject.j>
@import "Globals.j"
@import "Settings.j"
@import "ControlDispatcher.j"
@import "SoundManager.j"
//after applying the preset, respond via GUI or send back to caller?
@typedef feedbackTypeType
kFeedbackTypeNone = 0; kFeedbackTypeGUI = 1; kFeedbackTypeHTMLMessage = 2;
@implementation Presets: CPObject {
id allPresets;
CPString presetName, allPresetNames;
CPPopUpButton popUpButton;
}
- (id) initWithPopup: (CPPopUpButton) thePopUpButton {
self = [super init];
if (self) {
popUpButton = thePopUpButton;
[popUpButton removeAllItems];
[popUpButton addItemWithTitle: "PRESETS"];
[popUpButton setSelectedIndex: 0];
const path = [[CPBundle mainBundle] pathForResource: "Presets.json"];
fetch(path)
.then(response => response.json())
.then(presets => {
allPresets = presets;
allPresetNames = Object.keys(allPresets);
allPresetNames.sort();
for (const name of allPresetNames) {
[popUpButton addItemWithTitle: name];
}
})
.catch(error => console.error("Failed to load presets for popup:", error));
[[CPNotificationCenter defaultCenter] addObserver: self selector: @selector(notificationApplyPresetNamed:) name: "notificationApplyPresetNamed" object: nil];
}
return self;
}
- (void) applyPresetNamed: (CPString) aPresetName {
presetName = aPresetName;
[self apply2withFeedbackType: kFeedbackTypeNone];
}
//called from the GUI Presets popup
- (void) apply: (id) sender {
const presetIndex = [sender indexOfSelectedItem];
if (presetIndex === 0) return;
presetName = [sender itemTitleAtIndex: presetIndex];
const messageText = "Really all Settings to “" + presetName + "” ?";
gLatestAlert = [CPAlert alertWithMessageText: messageText
defaultButton: "NO (ߵnߴ)" alternateButton: "YES (ߵyߴ)" otherButton: nil
informativeTextWithFormat: "Many Settings will change. You should know what you are doing here. Luckily, you can always return to defaults."];
[[gLatestAlert buttons][0] setKeyEquivalent: "y"];
[[gLatestAlert buttons][1] setKeyEquivalent: "n"];
[gLatestAlert runModalWithDidEndBlock: function(alert, returnCode) {
if (returnCode === 1) {
[self apply2withFeedbackType: kFeedbackTypeGUI];
}
gLatestAlert = null;
}];
}
//called from `ControlDispatcher`
- (void) notificationApplyPresetNamed: (CPNotification) aNotification {
presetName = [aNotification object];
[self apply2withFeedbackType: kFeedbackTypeHTMLMessage];
}
- (void) apply2withFeedbackType: (feedbackTypeType) feedbackType {
if (!allPresetNames.includes(presetName)) {
console.log("FrACT10▸Presets▸unknown preset: ", presetName);
if (feedbackType === kFeedbackTypeHTMLMessage) {
[ControlDispatcher post2parentM1: "Settings" m2: "Preset" m3: presetName success: NO];
} else if ([Settings isAutoPreset]) {
[Settings setIsAutoPreset: NO];
gLatestAlert = [CPAlert alertWithMessageText: "WARNING" defaultButton: "OK" alternateButton: nil otherButton: nil informativeTextWithFormat: "\rTrying to apply unknown preset: “" + presetName + "”.\r\rI suggest to reselect the desired preset.\r"];
[gLatestAlert runModal];
}
return;
}
[self applyPresetLogic];
[popUpButton setSelectedIndex: 0];
switch (feedbackType) {
case kFeedbackTypeGUI:
gLatestAlert = [CPAlert alertWithMessageText: "Preset »" + presetName + "« was applied." defaultButton: "OK" alternateButton: nil otherButton: nil informativeTextWithFormat: ""];
[gLatestAlert runModal];
break;
case kFeedbackTypeHTMLMessage:
[ControlDispatcher post2parentM1: "Settings" m2: "Preset" m3: presetName success: true];
break;
}
}
- (void) applyPresetLogic { //console.info("applyPresetLogic")
const config = allPresets[presetName];
if (!config) { //this was already tested, can't hurt
alert("Config for »"+ presetName + "« does not exist");
return;
}
switch (config.action) { //Reset defaults
case "setDefaults": [Settings setDefaults]; break;
case "setDefaultsKeepingCalBarLength": [Settings setDefaultsKeepingCalBarLength]; break;
case "applyTestingPresets": [self applyTestingPresets]; break;
default: console.warn("`switch (config.action)` finds unknown action: ", config.action);
}
if (config.settings) { //Apply settings
const ConstantMap = { //mapping so we can use symbolic constants in the json file
"kResultsToClipNone": kResultsToClipNone,
"kResultsToClipFullHistory": kResultsToClipFullHistory,
"kResultsToClipFinalOnly": kResultsToClipFinalOnly,
"kNAlternativesIndex2": kNAlternativesIndex2,
"kNAlternativesIndex4": kNAlternativesIndex4,
"kNAlternativesIndex8plus": kNAlternativesIndex8plus,
"kauditoryFeedback4trialIndexNone": kauditoryFeedback4trialIndexNone,
"kauditoryFeedback4trialIndexAlways": kauditoryFeedback4trialIndexAlways,
"kauditoryFeedback4trialIndexOncorrect": kauditoryFeedback4trialIndexOncorrect,
"kauditoryFeedback4trialIndexWithinfo": kauditoryFeedback4trialIndexWithinfo,
"kTestNone": kTestNone,
"kTestAcuityLetters": kTestAcuityLetters,
"kTestAcuityLandolt": kTestAcuityLandolt,
"kTestAcuityE": kTestAcuityE,
"kTestAcuityTAO": kTestAcuityTAO,
"kTestAcuityVernier": kTestAcuityVernier,
"kTestContrastLetters": kTestContrastLetters,
"kTestContrastLandolt": kTestContrastLandolt,
"kTestContrastE": kTestContrastE,
"kTestContrastG": kTestContrastG,
"kDecimalMarkCharIndexAuto": kDecimalMarkCharIndexAuto,
"kDecimalMarkCharIndexDot": kDecimalMarkCharIndexDot,
"kDecimalMarkCharIndexComma": kDecimalMarkCharIndexComma,
"kVernierType3bars": kVernierType3bars,
};
for (const key in config.settings) {
let value = config.settings[key];
if (typeof value === "string" && ConstantMap[value] !== undefined) {
value = ConstantMap[value];
}
const setterName = "set" + key.charAt(0).toUpperCase() + key.substring(1) + ":";
const selector = sel_getUid(setterName);
if ([Settings respondsToSelector: selector]) {
//console.info("Setter ", setterName, "to be applied.")
[Settings performSelector: selector withObject: value];
} else {
console.warn("Setter ", setterName, "not defined.")
}
}
}
[Settings setPresetName: presetName];
[Settings calculateMinMaxPossibleAcuity];
[[SoundManager sharedManager] updateSoundFiles];
}
- (void) applyTestingPresets {
[Settings setDefaults];
[Settings setNTrials02: 24];
[Settings setNTrials04: 18];
[Settings setNTrials08: 18];
[Settings setDistanceInCM: 400];
[Settings setCalBarLengthInMM: 140];
[Settings setShowResponseInfoAtStart: NO];
[Settings setShowCI95: YES];
[Settings setSoundTrialYesIndex: 0]; [Settings setSoundTrialNoIndex: 1];
[Settings setSoundRunEndIndex: 1];
}
/**
Perform logic unit tests for Presets (loading and existence of standard preset).
@return YES if all tests pass
*/
- (BOOL) unittest {
let success = YES, report = crlf + "Presets▸unittest:" + crlf;
if (!allPresets || Object.keys(allPresets).length === 0) {
report += " ERROR: allPresets not loaded!" + crlf; success = NO;
}
if (!allPresetNames || !allPresetNames.includes("Standard Defaults")) {
report += " ERROR: 'Standard Defaults' missing from allPresetNames!" + crlf; success = NO;
}
if (success) {
report += " Preset loading and validation tests passed." + crlf;
}
console.info(report);
return success;
}
@end