-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
494 lines (438 loc) · 15.5 KB
/
index.html
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<html>
<head>
<title>Oración Seis</title>
<!-- JQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<!-- <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script> -->
<!-- Google Material Icons -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/css/materialize.min.css">
<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/js/materialize.min.js"></script>
<!-- Our styles -->
<link rel="stylesheet" href="style.css" type="text/css">
<!-- Favicon -->
<link rel="icon" href="oracion-seis.png" type="image/png">
<!-- Keyboard shortcuts -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/mousetrap/1.4.6/mousetrap.min.js"></script>
</head>
<body>
<script>
document.body.className += ' fade-out';
$(function() {
$('body').removeClass('fade-out');
});
</script>
<div class="no-select">
<div class="row"></div> <!-- Filler row -->
<div class="row"> <!-- Row 1 -->
<div class="col s8">
<center>
<table class="tone-matrix center-align" style="width:512px">
<!-- Dynamic content via JQuery -->
</table>
</center>
</div> <!-- End of col s8 no-select div -->
<div class="col s4">
<h1>Tone Dead Matrix</h1>
<p>
<ol>
<li>You may choose an instrument below to begin.</li>
<li>You may click anywhere on the matrix to play a tune.</li>
<li>You may toggle the selection of the matrix cells.</li>
<li>You may form a spell with a combination of instruments.</li>
</ol>
</p>
<div class="row">
<a class="waves-effect waves-light btn" onClick="resetActiveSelect();">
<i class="material-icons left">clear_all</i>Purify Matrix</a>
</div>
<div class="row">
<a class="waves-effect waves-light btn" onClick="resetAllSelect();">
<i class="material-icons left">stop</i>End The World</a>
</div>
<div class="row">
<a id='pauseBtn' class="waves-effect waves-light btn" onClick="pauseSelect();">
<i class="material-icons left">pause</i>Pause The World</a>
</div>
<div class="row">
<div class="input-field col s8 offset s2">
<p class="range-field">
<input type="range" id="tempo" min="100" max="900" />
</p>
</div>
</div>
</div> <!-- End of row 1 div -->
<div class="row"> <!-- Row 2 -->
<div class="col s12 no-select">
<center>
<!-- Warning: Do not change the below ID unless it remains in the form of x0 -->
<!-- See current_instrument = $(this).attr('id')[1]; -->
<a id="i0" class="piano change-instrument waves-effect waves-light active-instrument">
<img class="five-instru-img-btn" src="./Exports/piano_on.png"></a>
<a id="i1" class="guitar change-instrument waves-effect waves-light">
<img class="five-instru-img-btn" src="./Exports/guitar_off.png"></a>
<a id="i2" class="drums change-instrument waves-effect waves-light">
<img class="five-instru-img-btn" src="./Exports/drums_off.png"></a>
<a id="i3" class="cowbell change-instrument waves-effect waves-light">
<img class="five-instru-img-btn" src="./Exports/cowbell_off.png"></a>
<a id="i4" class="xylophone change-instrument waves-effect waves-light">
<img class="five-instru-img-btn" src="./Exports/xylophone_off.png"></a>
<a id="i5" class="konami change-instrument waves-effect waves-light">
<img class="five-instru-img-btn" src="./Exports/konami_off.png"></a>
</center>
</div> <!-- End of col s12 no-select div -->
</div> <!-- End of row 2 div -->
<div class="row">
</div>
</div> <!-- End of no-select div -->
<script>
// HELPER: Print formatted for JavaScript (only for string data type)
function sprintf(format) {
for (var i = 1; i < arguments.length; i++) {
format = format.replace(/%s/, arguments[i]);
}
return format;
}
var order = 16; // size of matrix
var img_dir = './Exports/';
var mat_sq_img = [img_dir + "/square.png",
img_dir + "/square_on.png",
img_dir + "/square_on_playing.png",
img_dir + "/square_playing.png"]; // matrix square img
// Initialise tone-matrix table (HTML)
for (var i = 0; i < order; i++) {
$('.tone-matrix').append(sprintf('<tr class="r%s"></tr>', i)); // row
for (var j = 0; j < order; j++) {
$('.r' + i).append(
sprintf('<td class="cell r%sc%s" data-row="%s" data-col="%s" style="padding: 1px 0px;"><img id="r%sc%s" src="%s"></td>',
i, j, i, j, i, j, mat_sq_img[0]));
}
}
// Initialise tone-matrix table (ordered by time and pitch)
var toPlay = true;
var sounds_to_play = [];
//if (localStorage.sounds_to_play == undefined) {
for (var i = 0; i < order; i++) {
sounds_to_play[i] = [];
}
//} else {
//sounds_to_play = JSON.parse(localStorage.sounds_to_play);
//}
// OnOffMatrix class which stores a 2D array of booleans (to play or not to play)
function OnOffMatrix(sounds) {
this.on_off_matrix = [];
for (var i = 0; i < order; i++) { // row
this.on_off_matrix[i] = [];
for (var j = 0; j < order; j++) { // col
this.on_off_matrix[i][j] = false;
}
}
this.sounds = sounds;
}
// Initialise on-off matrix table (tracks if a sound should be played)
var sound_folder_names = ["piano", "guitar", "drum", "alarma", "xylophone","nyans"];
var total_instruments = 6;
var number_of_instruments = 5;
var number_of_instruments_fixed = 5;
var instruments = [];
//if (localStorage.instruments == undefined) {
for (var i = 0; i < total_instruments; i++) {
var sound_file_names = [];
for (var j=1;j<=16;j++){
sound_file_names[j-1]="Sound/"+sound_folder_names[i]+"/"+j+".mp3";
}
instruments[i] = new OnOffMatrix(sound_file_names);
}
//} else {
//instruments = JSON.parse(localStorage.instruments);
//}
// When a cell is clicked, toggle the boolean (to play or not to play)
// and subsequently the corresponding image and sound state
var addSound;
var removeSound;
var current_instrument = 0; // ID of active instrument/matrix
// Retrieve
//var current_instrument = (localStorage.current_instrument == undefined) ?
//0 : localStorage.current_instrument;
var isDown = false; // Tracks status of mouse button
var isSet = false;
var isSwitchOn = true;
$('.cell').mouseover(function(){
if(isDown){
var row = $(this).data("row");
var col = $(this).data("col");
if(!isSet){
isSet = true;
isSwitchOn = !instruments[current_instrument].on_off_matrix[row][col];
}
if(isSwitchOn == !instruments[current_instrument].on_off_matrix[row][col]){
toggle(row,col);
}
}
});
$('.cell').click(function(){
var row = $(this).data("row");
var col = $(this).data("col");
toggle(row,col);
});
function toggle(row,col){
var flag = instruments[current_instrument].on_off_matrix[row][col];
instruments[current_instrument].on_off_matrix[row][col] = !flag;
// update image and sound
var square_selector = '#r'+row+'c'+col;
if (!flag) {
$(square_selector).attr('src', mat_sq_img[1]); // square_on
addSound(instruments[current_instrument].sounds[row], col);
} else {
$(square_selector).attr('src', mat_sq_img[0]);
removeSound(instruments[current_instrument].sounds[row], col);
}
}
// if instrument is changed
// 1. toggle active-instrument classes for both the active and inactive instrument
// 2. update to the corresponding images
// 3. re-renders the tone-matrix to that of the active instrument (state is kept)
var active_instru_class = 'active-instrument';
$('.change-instrument').click(function() {
// if the clicked instrument was not an active instrument
// make the active inactive and make the clicked instrument active
if (!$(this).hasClass(active_instru_class)) {
$('.'+active_instru_class).removeClass(active_instru_class); // deactivate
$(this).toggleClass(active_instru_class); // activate
}
// else the active but clicked instrument remains active
// re-render the image
var instru_img_ref = ['piano', 'guitar', 'drums', 'cowbell', 'xylophone', "konami"];
for (var i = 0; i < number_of_instruments; i++) {
var instru_selector = '.' + instru_img_ref[i];
if ($(instru_selector).hasClass(active_instru_class)) {
$(instru_selector).html(
'<img class="five-instru-img-btn" src="'+img_dir+instru_img_ref[i]+'_on.png">');
} else {
$(instru_selector).html(
'<img class="five-instru-img-btn" src="'+img_dir+instru_img_ref[i]+'_off.png">');
}
}
current_instrument = $(this).attr('id')[1]; // will break if id > 9
// re-render tone-matrix
for (var i = 0; i < order; i++){
for (var j = 0;j < order; j++){
var square_selector = '#r'+i+'c'+j;
if (instruments[current_instrument].on_off_matrix[i][j]) {
$(square_selector).attr('src', mat_sq_img[1]); // square_on
} else {
$(square_selector).attr('src', mat_sq_img[0]);
}
}
}
});
// RESET SELECTION ON ACTIVE INSTRU
function resetActiveSelect() {
for (var i = 0; i < order; i++) {
for (var j = 0; j < order; j++) {
// if current selection is selected, deselect it
var flag = instruments[current_instrument].on_off_matrix[i][j];
if (flag) {
instruments[current_instrument].on_off_matrix[i][j] = !flag;
$('#r'+i+'c'+j).attr('src', mat_sq_img[0]);
removeSound(instruments[current_instrument].sounds[i], j);
}
}
}
}
// RESET SELECTION ON ALL INSTRU
function resetAllSelect() {
// Note that current_instrument is NOT used here because we are parsing ALL
for (var instru = 0; instru < number_of_instruments; instru++) {
for (var i = 0; i < order; i++) {
for (var j = 0; j < order; j++) {
// if current selection is selected, deselect it
var flag = instruments[instru].on_off_matrix[i][j];
if (flag) {
instruments[instru].on_off_matrix[i][j] = !flag;
$('#r'+i+'c'+j).attr('src', mat_sq_img[0]);
removeSound(instruments[instru].sounds[i], j);
}
}
}
}
}
var playPauseHtml=['<i class="material-icons left">pause</i>Pause The World','<i class="material-icons left">play_arrow</i>Resume The World'];
// PAUSE PLAYING
function pauseSelect() {
if(toPlay){
toPlay = false;
$('#pauseBtn').html(playPauseHtml[1]);
}
else{
toPlay = true;
$('#pauseBtn').html(playPauseHtml[0]);
}
}
// play.js
var tick = 0;
var tempo = $('#tempo').val();
var tickTime = 60000/tempo;
$('#tempo').on('input', function() {
tickTime = 60000/$('#tempo').val();
});
for (var t = 100; t <= 900; t++) {
var key = t.toString();
key = sprintf('t %s %s %s', key[0], key[1], key[2]);
Mousetrap.bind(key, function(e, combo) {
combo = combo[2]+combo[4]+combo[6];
$('#tempo').val(parseInt(combo));
tickTime = 60000/$('#tempo').val();
});
}
// NO DRAGGING (:
$('img').on('dragstart', function(event) { event.preventDefault(); });
// Konami Code
Mousetrap.bind('up up down down left right left right b a enter', function() {
alert('Nani wa Konami Code?');
$(".konami").show();
number_of_instruments++;
});
// Keyboard shortcut: Pause selected
Mousetrap.bind('p', function() {
pauseSelect();
});
// Keyboard shortcut: Reset active
Mousetrap.bind('x', function() {
resetActiveSelect();
});
// Keyboard shortcut: Reset all
Mousetrap.bind('z', function() {
resetAllSelect();
});
// Keyboard shortcut: Navigate across instruments
Mousetrap.bind('right', function() {
navigate(true);
});
Mousetrap.bind('left', function() {
navigate(false);
});
function navigate(right) {
$('.'+active_instru_class).removeClass(active_instru_class); // deactivate
if (right) {
current_instrument = (current_instrument+1) % number_of_instruments_fixed;
} else {
current_instrument = (current_instrument == 0) ?
number_of_instruments_fixed-1 : current_instrument-1;
}
$('#i'+current_instrument).toggleClass(active_instru_class); // activate
// re-render the image
var instru_img_ref = ['piano', 'guitar', 'drums', 'cowbell', 'xylophone',"konami"];
for (var i = 0; i < number_of_instruments; i++) {
var instru_selector = '.' + instru_img_ref[i];
if ($(instru_selector).hasClass(active_instru_class)) {
$(instru_selector).html(
'<img class="five-instru-img-btn" src="'+img_dir+instru_img_ref[i]+'_on.png">');
} else {
$(instru_selector).html(
'<img class="five-instru-img-btn" src="'+img_dir+instru_img_ref[i]+'_off.png">');
}
}
// re-render tone-matrix
for (var i = 0; i < order; i++){
for (var j = 0;j < order; j++){
var square_selector = '#r'+i+'c'+j;
if (instruments[current_instrument].on_off_matrix[i][j]) {
$(square_selector).attr('src', mat_sq_img[1]); // square_on
} else {
$(square_selector).attr('src', mat_sq_img[0]);
}
}
}
} // End of navigate function
// Keyboard shortcut: Select cell
// breaks if order < 9
for (var r = 0; r < 10; r++) {
for (var c = 0; c < 10; c++) {
var key = '0 ' + r + ' 0 ' + c;
Mousetrap.bind(key, function(e, combo) {
updateBoolImgSound(combo[2], combo[6]);
});
}
}
for (var r = 0; r < 10; r++) {
for (var c = 10; c < order; c++) {
var row = r.toString();
var col = c.toString();
var key = sprintf('0 %s %s %s', r, col[0], col[1]);
Mousetrap.bind(key, function(e, combo) {
updateBoolImgSound(combo[2], combo[4]+combo[6]);
});
}
}
for (var r = 10; r < order; r++) {
for (var c = 0; c < 10; c++) {
var row = r.toString();
var col = c.toString();
var key = sprintf('%s %s 0 %s', row[0], row[1], c);
Mousetrap.bind(key, function(e, combo) {
updateBoolImgSound(combo[0]+combo[2], combo[6]);
});
}
}
for (var r = 10; r < order; r++) {
for (var c = 10; c < order; c++) {
var row = r.toString();
var col = c.toString();
var key = sprintf('%s %s %s %s', row[0], row[1], col[0], col[1]);
Mousetrap.bind(key, function(e, combo) {
updateBoolImgSound(combo[0]+combo[2], combo[4]+combo[6]);
});
}
}
// Updates the boolean, image, and sound upon keyboard shortcut
function updateBoolImgSound(row, col) {
// console.log(row + ' ' + col);
row = parseInt(row);
col = parseInt(col);
var flag = instruments[current_instrument].on_off_matrix[row][col];
instruments[current_instrument].on_off_matrix[row][col] = !flag;
// update image and sound
var square_selector = '#r'+row+'c'+col;
if (!flag) {
$(square_selector).attr('src', mat_sq_img[1]); // square_on
addSound(instruments[current_instrument].sounds[row], col);
} else {
$(square_selector).attr('src', mat_sq_img[0]);
removeSound(instruments[current_instrument].sounds[row], col);
}
}
</script>
<script src="timer.js"></script>
<script src="play.js"></script>
<!-- <script src="playtest.js"></script> -->
<script>
// Store
function store() {
if (typeof(Storage) !== "undefined") {
//localStorage.setItem("current_instrument", current_instrument);
//localStorage.instruments = JSON.stringify(instruments);
//localStorage.sounds_to_play = JSON.stringify(sounds_to_play);
setTimeout(store, 1000);
} else {
alert('There will be no back-up for your tones ):');
}
}
$(document).ready(function() {
$('.konami').hide();
$(document).mousedown(function() {
isDown = true; // When mouse goes down, set isDown to true
})
.mouseup(function() {
isDown = false; // When mouse goes up, set isDown to false
isSet = false;
});
play();
//store();
});
</script>
<footer>© Conan, Chia Hui, Jason, Jia Yee, Yi Min, Yu Feng</footer>
</body>
</html>