-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassetCache.js
More file actions
195 lines (177 loc) · 4.8 KB
/
assetCache.js
File metadata and controls
195 lines (177 loc) · 4.8 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
//
// tangle/assetCache
//
// author: Rylee Corradini
// license: MIT
//
define(["atto/core", "atto/event"], function (atto, AttoEvent) {
function constructor(args) {
var _assets = {},
_loadStatus = {},
_events = {
error: new AttoEvent("tangle.assetCache.error"),
ready: new AttoEvent("tangle.assetCache.ready"),
loaded: new AttoEvent("tangle.assetCache.loaded"),
},
_types = {
IMAGE: 1,
AUDIO: 2,
},
_extension_to_type = {
png: 1,
bmp: 1,
gif: 1,
jpg: 1,
jpeg: 1,
wav: 2,
webm: 2,
ogg: 2,
mp3: 2,
aac: 2,
};
function _assetTypeFromFilename(fileName) {
var segs = fileName.split("."),
ext = segs[segs.length - 1],
retVal = _extension_to_type[ext.toLowerCase()];
// default to image if we couldn't figure it out
return retVal || _types.IMAGE;
}
function _createLoadCallback(assetName) {
return function () {
_loadStatus[assetName] = true;
_events.loaded.dispatch({ name: assetName });
if (_ready()) {
_events.ready.dispatch({});
}
};
}
function _createErrorCallback(assetName) {
return function () {
_loadStatus[assetName] = false;
_events.error.dispatch({
name: assetName,
details: "Asset '" + assetName + "' could not be loaded.",
});
};
}
function _addAsset(name, src) {
if (name in _assets) {
_events.error.dispatch({
name: name,
details: "Asset '" + name + "' is already in the cache",
});
return;
} else {
var assetType = _assetTypeFromFilename(src);
switch (assetType) {
case _types.IMAGE:
// image: this is by far the simple case
_assets[name] = new Image();
atto.addEvent(
_assets[name],
"load",
_createLoadCallback(name),
false
);
atto.addEvent(
_assets[name],
"error",
_createErrorCallback(name),
false
);
break;
case _types.AUDIO:
// audio; this will take a little more magic, due to browser codec issues
_assets[name] = new Audio();
atto.addEvent(
_assets[name],
"canplaythrough",
_createLoadCallback(name),
false
);
atto.addEvent(
_assets[name],
"error",
_createErrorCallback(name),
false
);
break;
default:
// unsupported asset type
_events.error.dispatch({
name: name,
details: "Asset type '" + assetType + "' not supported.",
});
return false;
break;
}
_loadStatus[name] = false;
_assets[name].src = src;
}
}
function _hasAsset(name) {
return name in _assets;
}
function _getAsset(name) {
if (_assets[name]) {
return _assets[name];
} else {
_events.error.dispatch({
name: name,
details: "Asset named '" + name + "' not found in cache.",
});
return null;
}
}
function _getAssetType(name) {
var obj;
if (_assets[name]) {
obj = _assets[name];
if (obj) {
return obj.tagName === "IMG" ? _types.IMAGE : _types.AUDIO;
}
} else {
_events.error.dispatch({
name: name,
details: "Asset named '" + name + "' not found in cache.",
});
}
return -1;
}
function _ready() {
var retVal = true;
for (var id in _assets) {
if (id in _loadStatus) {
retVal &= _loadStatus[id];
} else {
return false;
}
}
return retVal;
}
function _completion() {
var total = 0,
loadCount = 0,
loaded = false;
for (var id in _assets) {
total++;
if (id in _loadStatus) {
loaded = _loadStatus[id];
if (loaded) loadCount++;
}
}
return total ? loadCount / total : 0;
}
return {
TYPES: _types,
addAsset: _addAsset,
hasAsset: _hasAsset,
getAsset: _getAsset,
getAssetType: _getAssetType,
ready: _ready,
percentage: _completion,
events: _events,
}; // end of public interface
} // end of constructor
return constructor;
}); // end AMD callback function