-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdropdownMenu.js
More file actions
364 lines (317 loc) · 11.7 KB
/
dropdownMenu.js
File metadata and controls
364 lines (317 loc) · 11.7 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// dropdownMenu.js
/*
Dropdown menu component that provides:
- Quick access to settings
- Dataset selection
- Range adjustment
- Screen-specific options
- Consistent menu styling across screens
*/
let dropdownMenu = {
isOpen: false,
radiusSlider: null,
datasetSelector: null,
isChangingDataset: false,
draw(currentScreen) {
if (!this.isOpen) {
// Draw settings button in top right
let buttonSize = 44;
let padding = 16;
let x = width - buttonSize - padding;
let y = padding;
noStroke();
fill(getButtonColor());
rect(x, y, buttonSize, buttonSize, 8);
imageMode(CENTER);
image(settingsIcon, x + buttonSize/2, y + buttonSize/2, buttonSize-padding, buttonSize-padding);
if (document.documentElement.getAttribute('data-theme') === 'dark') {
image(settingsIconInverted, x + buttonSize/2, y + buttonSize/2, buttonSize-padding, buttonSize-padding);
}
// Only handle click on release
if (!mouseIsPressed && lastMousePressed &&
mouseX > x && mouseX < x + buttonSize &&
mouseY > y && mouseY < y + buttonSize) {
this.isOpen = true;
this.createControls(currentScreen);
}
} else {
// Draw dropdown panel
let panelWidth = width;
let panelHeight = this.getPanelHeight(currentScreen);
let x = 0;
let y = 0;
// Check for click outside menu
if (!mouseIsPressed && lastMousePressed) {
if (mouseX < x || mouseX > x + panelWidth ||
mouseY < y || mouseY > y + panelHeight) {
this.cleanup();
return;
}
}
noStroke();
const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
if (isDark) {
fill(45, 45, 45);
} else {
fill(235, 235, 235);
}
rect(x, y, panelWidth, panelHeight);
// Bottom border
stroke(getButtonColor());
strokeWeight(1);
line(x, y + panelHeight, x + panelWidth, y + panelHeight);
// Close button
stroke(getTextColor());
strokeWeight(2);
let closeX = x + panelWidth - 40;
let closeY = y + 16;
line(closeX, closeY, closeX + 20, closeY + 20);
line(closeX + 20, closeY, closeX, closeY + 20);
// Settings content
noStroke();
fill(getTextColor());
textAlign(LEFT, CENTER);
textSize(20);
let contentY = y + 80;
// Dataset selector for specific screens
if (!['mainMenu', 'splash', 'search', 'customLocations'].includes(currentScreen)) {
text("Dataset", x + 20, contentY);
if (!this.datasetSelector) {
this.createDatasetSelector(x + panelWidth - 170, contentY);
}
this.datasetSelector.style('background-color', getComputedStyle(document.documentElement)
.getPropertyValue('--button-bg'));
this.datasetSelector.style('color', getComputedStyle(document.documentElement)
.getPropertyValue('--button-text'));
this.datasetSelector.style('border', 'none');
this.datasetSelector.style('padding', '8px');
this.datasetSelector.style('border-radius', '8px');
this.datasetSelector.style('cursor', 'pointer');
this.datasetSelector.style('font-size', '16px');
contentY += 60;
}
// Only show radius control in game/answer screen
if (currentScreen === 'game' || currentScreen === 'answer') {
text("Max Range", x + 20, contentY);
textAlign(RIGHT);
text(`${userSettings.radius} km`, x + panelWidth - 20, contentY);
contentY += 100;
}
// Add practice mode toggle for answer screen
if (currentScreen === 'answer') {
fill(getTextColor());
textAlign(LEFT);
text("Practice Mode", x + 20, contentY);
this.drawToggleSwitch(x + panelWidth - 70, contentY,
() => userSettings.practiceMode,
(enabled) => {
userSettings.practiceMode = enabled;
// Reset input values when enabling practice mode
if (enabled) {
userGuessDistance = currentAnswer.guessDistance;
userGuessDirection = currentAnswer.guessDirection;
}
}
);
contentY += 60;
}
// Dark Mode Toggle always shown
textAlign(LEFT);
fill(getTextColor());
text("Dark Mode", x + 20, contentY);
this.drawToggleSwitch(x + panelWidth - 70, contentY);
// handle close click on release
if (!mouseIsPressed && lastMousePressed) {
if (mouseX > closeX - 10 && mouseX < closeX + 30 &&
mouseY > closeY - 10 && mouseY < closeY + 30) {
this.cleanup();
}
}
}
},
getPanelHeight(currentScreen) {
switch(currentScreen) {
case 'game':
return 280;
case 'answer':
return 340;
case 'discoveries':
case 'scores':
return 180;
case 'search':
case 'customLocations':
return 130;
default:
return 130;
}
},
createDatasetSelector(x, y) {
if (!this.datasetSelector) {
const offset = getCanvasOffset();
this.datasetSelector = createSelect();
this.datasetSelector.position(x + offset.x, y - 15);
this.datasetSelector.style('width', '150px');
Object.entries(dataLoader.datasets).forEach(([key, dataset]) => {
// Only show custom dataset if it has locations
if (key !== 'custom' ||
(dataset.locations && dataset.locations.length > 0)) {
this.datasetSelector.option(dataset.name, key);
}
});
const currentDataset = localStorage.getItem('selectedDataset') || 'global';
// If current dataset is custom but empty, switch to global
if (currentDataset === 'custom' &&
(!dataLoader.datasets.custom.locations ||
dataLoader.datasets.custom.locations.length === 0)) {
localStorage.setItem('selectedDataset', 'global');
this.datasetSelector.selected('global');
} else {
this.datasetSelector.selected(currentDataset);
}
this.datasetSelector.changed(() => {
if (this.isChangingDataset) return;
this.isChangingDataset = true;
localStorage.setItem('selectedDataset', this.datasetSelector.value());
const newDataset = this.datasetSelector.value();
// Clear recent locations when changing datasets
recentLocations = [];
// Update max range based on dataset
userSettings.radius = dataLoader.datasets[newDataset].defaultRadius;
// Clean up dropdown immediately when loading starts
this.cleanup();
// Reload questions
dataLoader.loadQuestions(newDataset, (loadedQuestions) => {
console.log("Dataset changed, loading new questions");
if (loadedQuestions && loadedQuestions.length > 0) {
questions = loadedQuestions;
if (currentScreen === 'game' || currentScreen === 'answer') {
console.log("About to call pickNewQuestion from dataset change");
pickNewQuestion();
}
}
// Update views that need refreshing
if (currentScreen === 'discoveries') {
discoveryMap.updateMarkers();
}
});
});
}
},
drawToggleSwitch(x, y, getValue = () => document.documentElement.getAttribute('data-theme') === 'dark',
onToggle = (enabled) => settingsScreen.setDarkMode(enabled)) {
const isEnabled = getValue();
let toggleWidth = 50;
let toggleHeight = 24;
// Draw background
noStroke();
fill(isEnabled ? getButtonColor() : '#cccccc');
rect(x, y - toggleHeight/2, toggleWidth, toggleHeight, toggleHeight/2);
// Draw knob
fill(isEnabled ? getTextColor() : '#ffffff');
let knobSize = toggleHeight - 4;
let knobX = isEnabled ? x + toggleWidth - knobSize - 2 : x + 2;
circle(knobX + knobSize/2, y, knobSize);
// Handle click
if (mouseIsPressed && !lastMousePressed &&
mouseX > x && mouseX < x + toggleWidth &&
mouseY > y - toggleHeight/2 && mouseY < y + toggleHeight/2) {
onToggle(!isEnabled);
}
},
createControls(currentScreen) {
let contentY = 80;
// Create dataset selector for specific screens
if (!['mainMenu', 'splash', 'search', 'customLocations'].includes(currentScreen)) {
this.createDatasetSelector(width - 170, contentY);
contentY += 50;
}
// Only create radius slider for game/answer screens
if (currentScreen === 'game' || currentScreen === 'answer') {
this.createRadiusSlider();
}
},
createRadiusSlider() {
if (!this.radiusSlider) {
const offset = getCanvasOffset();
this.radiusSlider = createSlider(0, 1, 0.5, 0.001);
// Make the slider more touch-friendly
this.radiusSlider.style('width', width - 40 + 'px');
this.radiusSlider.style('height', '60px');
this.radiusSlider.style('-webkit-appearance', 'none');
this.radiusSlider.style('appearance', 'none');
this.radiusSlider.style('background', 'transparent');
this.radiusSlider.style('cursor', 'pointer');
// Add custom CSS for the slider
const sliderStyle = document.createElement('style');
sliderStyle.textContent = `
input[type=range] {
-webkit-appearance: none;
width: 100%;
background: transparent;
}
input[type=range]::-webkit-slider-runnable-track {
width: 100%;
height: 8px;
background: var(--button-bg);
border-radius: 4px;
border: none;
}
input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
height: 32px;
width: 32px;
border-radius: 50%;
background: var(--text-color);
margin-top: -12px;
box-shadow: 0 1px 3px rgba(0,0,0,0.3);
cursor: pointer;
}
input[type=range]::-moz-range-track {
width: 100%;
height: 8px;
background: var(--button-bg);
border-radius: 4px;
border: none;
}
input[type=range]::-moz-range-thumb {
height: 32px;
width: 32px;
border-radius: 50%;
background: var(--text-color);
border: none;
box-shadow: 0 1px 3px rgba(0,0,0,0.3);
cursor: pointer;
}
input[type=range]:focus {
outline: none;
}
`;
document.head.appendChild(sliderStyle);
let initVal = getSliderValueFromRadius(userSettings.radius);
this.radiusSlider.value(initVal);
this.radiusSlider.input(() => {
let sVal = this.radiusSlider.value();
userSettings.radius = getRoundedRadiusFromSlider(sVal);
});
this.radiusSlider.position(20 + offset.x, 150);
}
},
cleanup() {
this.isOpen = false;
if (this.radiusSlider) {
this.radiusSlider.remove();
this.radiusSlider = null;
}
if (this.datasetSelector) {
this.datasetSelector.remove();
this.datasetSelector = null;
}
this.isChangingDataset = false;
},
// Add method to handle screen transitions
onScreenExit() {
if (this.isOpen) {
this.cleanup();
}
}
};