-
Notifications
You must be signed in to change notification settings - Fork 12
/
func.devel.js
155 lines (144 loc) · 4.52 KB
/
func.devel.js
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
var clips = $(document).data('clips');
var names = $(document).data('names');
var playing = false;
function renderTags(objs) {
$(Object.keys(objs)).each(function(i, key) {
var obj = $(objs[key]);
if (obj.length > 0) {
var path = obj[0];
var tag = $('<li data-token="' + key + '">' + names[key] + '</li>');
tag.on('mousedown', function() {
var sound = new Howl({
urls: genPath(path),
onend: function() {
this.unload();
}
});
sound.play();
});
tag.holdEvent({handler: tokenUpdater(), time: 600});
$('#tags').append(tag);
}
});
$('#tags').append('<hr class="clear"></hr>');
}
function sayTokens() {
if (true === playing) {
return;
}
var tokens1 = $('#tokens1 li');
var keys1 = convertTokToKey(tokens1);
var tokens2 = $('#tokens2 li');
var keys2 = convertTokToKey(tokens2);
if (keys1.length > 0 || keys2.length > 0) {
$('.talking').removeClass('talking');
playing = true;
sayToken(tokens1, keys1, 0);
sayToken(tokens2, keys2, 0);
}
}
function genPath(key) {
var filename = 'convert/' + key;
return [filename + '.ogg', filename + '.mp3'];
}
function convertTokToKey(tokens) {
var keys = [];
$(tokens).each(function(i, obj) {
var key = $(obj).data('token');
var tokenSet = clips[key];
if ('undefined' !== typeof(tokenSet)) {
var token = tokenSet[Math.floor(Math.random()*tokenSet.length)];
keys.push(token);
}
});
return keys;
}
function sayToken(tokens, keys, pos) {
if (0 === tokens.length) {
return;
}
var sound = new Howl({
urls: genPath(keys[pos]),
onplay: function() {
$(tokens[pos]).addClass('talking');
if (pos+1 < keys.length) {
var next = new Howl({
urls: genPath(keys[pos+1]),
}).unload();
}
if ('+' === keys[pos][0]) {
setTimeout(function() {
if (pos+1 < keys.length) {
sayToken(tokens, keys, pos+1);
} else {
tokens.removeClass('talking');
playing = false;
}
}, 250);
}
},
onloaderror: function() {
if (pos+1 < keys.length) {
sayToken(tokens, keys, pos+1);
}
},
onend: function() {
if ('+' !== keys[pos][0]) {
if (pos+1 < keys.length) {
sayToken(tokens, keys, pos+1);
} else {
tokens.removeClass('talking');
playing = false;
}
}
this.unload();
}
}).play();
}
function tokenMake(key) {
return $('<li data-token="' + key + '">' + names[key] + '</li>').on('dblclick', function() {$(this).remove()});
}
function tokenUpdater() {
return function(elementClicked) {
var obj = $('#tokens' + $('input[name=zone]:checked').val());
tokenMake(elementClicked.data('token')).appendTo(obj);
obj.sortable();
}
}
$(document).ready(function() {
renderTags(clips);
var string = window.location.hash.substr(1);
if (string.length > 0) {
var parts = string.replace(/_/g, ' ').trim().split('|');
$(parts).each(function(n, k) {
var tokens = k.split(' ');
$(tokens).each(function(i, key) {
if ('undefined' !== typeof(names[key])) {
$('#tokens' + (n+1)).append(tokenMake(key));
}
});
});
$('#tokens1').sortable();
$('#tokens2').sortable();
sayTokens();
}
$('#say').on('click', function() {
var tokens1 = $('#tokens1 li');
if (tokens1.length > 0) {
var hash = '';
tokens1.each(function (i, obj) {
hash += $(obj).data('token') + ' ';
});
window.location.hash = hash.trim().replace(/ /g, '_');
}
var tokens2 = $('#tokens2 li');
if (tokens2.length > 0) {
var hash = '';
tokens2.each(function (i, obj) {
hash += $(obj).data('token') + ' ';
});
window.location.hash += '|' + hash.trim().replace(/ /g, '_');
}
sayTokens();
});
});