-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo.js
197 lines (164 loc) · 5.55 KB
/
demo.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
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
'use strict';
const createArtPacks = require('./');
//urls = ['test2.zip', 'test.zip', 'testsnd.zip'];
//urls = ['test2.zip', 'test.zip'];
const urls = ['https://dl.dropboxusercontent.com/u/258156216/artpacks/ProgrammerArt-2.2-dev-ResourcePack-20140308.zip', 'invalid.zip', 'README.md'];
const container = document.createElement('div');
container.style.position = 'absolute';
container.style.height = '90%';
container.style.width = '90%';
container.style.border = '5px dotted black';
document.body.appendChild(container);
const packs = createArtPacks(urls);
packs.on('loadedURL', (url) => {
console.log('Loaded ',url);
});
// show some sample textures
// TODO: show _all_? In a grid? might be handy
function showTextures() {
for (let name of [
'dirt', // block, any namespace
'blocks/dirt', // longhand block
'i/stick', // item shorthand, any namespace
'items/stick', // longhand item
'misc/shadow', // other texture type
'minecraft:dirt', // explicit namespace, block
'grass_top', // colorize
'water_flow', // animated
//'somethingelse:dirt', // non-existent namespace
'invalid' // non-existent block
]) {
show(name);
}
}
function show(name) {
packs.getTextureImage(name, (img) => {
if (Array.isArray(img)) {
container.appendChild(document.createTextNode(`${name} = (animated ${img.length}) `));
for (let im of img) {
im.title = name;
container.appendChild(im);
container.appendChild(document.createTextNode(', '));
}
} else {
container.appendChild(document.createTextNode(`${name} = `));
img.title = name;
container.appendChild(img);
container.appendChild(document.createElement('br'));
}
}, (err) => {
container.appendChild(document.createTextNode(name + ' = '));
container.appendChild(document.createTextNode('(not found)'));
container.appendChild(document.createElement('br'));
});
}
function showSounds() {
for (let name of ['liquid/splash']) {
container.appendChild(document.createTextNode(`sound: ${name} = `));
const url = packs.getSound(name);
if (url === undefined) {
container.appendChild(document.createTextNode('(not found)'));
} else {
console.log(url);
const audio = document.createElement('audio');
audio.src = url;
audio.controls = true;
audio.title = name;
container.appendChild(audio);
}
container.appendChild(document.createElement('br'));
}
}
function showControls() {
const controls = document.createElement('div');
const input = document.createElement('input');
input.setAttribute('id', 'input');
controls.appendChild(input);
controls.appendChild(document.createTextNode(' = '));
const img = document.createElement('img');
img.setAttribute('id', 'outputImg');
img.style.visibility = 'hidden';
controls.appendChild(img);
const audio = document.createElement('audio');
audio.setAttribute('id', 'outputAudio');
audio.controls = true;
audio.style.visibility = 'hidden';
controls.appendChild(audio);
function showSample() {
let url = packs.getTexture(input.value);
console.log(`lookup ${input.value} = ${url}`);
if (url === undefined) {
img.src = url;
img.style.visibility = '';
} else {
img.style.visibility = 'hidden';
// maybe it is a sound? (note: different namespaces, obviously)
url = packs.getSound(input.value);
if (url === undefined) {
audio.src = url;
audio.style.visibility = '';
} else {
audio.style.visibility = 'hidden';
}
}
}
document.body.addEventListener('keyup', showSample);
input.value = 'stone';
container.appendChild(controls);
showSample();
}
function showInfo() {
const ps = packs.getLoadedPacks();
let s = `Loaded ${ps.length} packs: `;
for (let p of ps) {
s += `${p} `;
}
container.appendChild(document.createTextNode(s));
container.appendChild(document.createElement('br'));
container.appendChild(document.createTextNode('Drop a pack here to load (hold shift to replace), or enter a name in text box below to lookup:'));
container.appendChild(document.createElement('br'));
container.appendChild(document.createElement('br'));
}
packs.on('loadedAll', (packs) => {
console.log("Loaded all packs");
while (container.firstChild) {
container.removeChild(container.firstChild);
}
showInfo();
showTextures();
showSounds();
showControls();
});
function dragover(ev) {
ev.stopPropagation();
ev.preventDefault();
container.style.border = '5px dashed black';
}
function dragleave(ev) {
ev.stopPropagation();
ev.preventDefault();
container.style.border = '5px dotted black';
}
function drop(mouseEvent) {
dragleave(mouseEvent);
const files = mouseEvent.target.files || mouseEvent.dataTransfer.files;
console.log('Dropped',files);
for (let i = 0; i < files.length; ++i) {
const file = files[i];
console.log('Reading dropped',file);
const reader = new FileReader();
reader.addEventListener('load', (readEvent) => {
if (readEvent.total !== readEvent.loaded) return; // TODO: progress bar
const arrayBuffer = readEvent.currentTarget.result;
if (mouseEvent.shiftKey) {
// if shift is held down: start over, replacing all current packs
packs.clear();
}
packs.addPack(arrayBuffer, file.name);
});
reader.readAsArrayBuffer(file);
}
}
container.addEventListener('dragover', dragover, false);
container.addEventListener('dragleave', dragleave, false);
container.addEventListener('drop', drop, false);