-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathimage_manager.js
More file actions
171 lines (151 loc) · 5.07 KB
/
image_manager.js
File metadata and controls
171 lines (151 loc) · 5.07 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
// image_manager preloads and holds all the images used in the game
var make_image_manager = function() {
// obj to return
var obj = [];
// --- private variables ---
// this will be counted in preloading
var num_images = 0;
// object to hold all arrays of image paths
// and the dir name that they come from
// dir should be name after "images/" and must end in '/'
var all_images = {
infectable_cell : { dir : "infectable_cell/" },
empty_cell : { dir : "noninfectable_cell/" },
background : { dir : "background_objects/" },
screens : { dir : "screens/" },
wall_segments : { dir : "wall_segments/" },
macrophage : {dir : "macrophage_animation/"},
macrophage_active : {dir : "holemacrophage_animation/"},
bcell : {dir : "b_animation/"},
burst : { dir : "burstingcell_animation/"},
cell_infect : { dir : "cellinfect_animation/"},
t_animation : { dir : "t_animation/"},
buttons : { dir : "buttons/"},
// catches all images in images/
other : { dir : "" },
};
// add "arr" property to each image
for_each(
keys(all_images),
function(key) {
all_images[key].images = [];
}
);
// filter out non-images from global image file list
var image_types = ["png", "jpg", "gif"];
for_each(
keys(all_image_files),
function(key) {
all_image_files[key] = all_image_files[key].filter(
function(name) {
return member(image_types,
name.substring(name.lastIndexOf('.')+1));
}
);
}
);
// --- public methods ---
// returns all the image objects of a given type
// if preloading and initing is done
// NOTE: returns a copy of the array of pointers, but the pointers
// will all point to the same objects so don't change the objects
obj.get_images = function(type) {
if (!all_images[type]) {
console.error("Invalid image type: "+type);
}
//for_each(all_images[type], console.log()
return all_images[type].images.slice();
};
// finds and returns the first image with the given name
// (no path necessary)
obj.get_image = function(name) {
var to_return;
for_each(
keys(all_images),
function(key) {
for_each(
all_images[key].images,
function(img_obj) {
if (name === img_obj.path.substring(
img_obj.path.lastIndexOf('/')+1)) {
to_return = img_obj.image;
}
}
);
}
);
if (!to_return) {
console.error("Invalid image name: "+name);
}
return to_return;
};
// preloads all the images
obj.preload_images = function() {
console.log("preloading images");
for_each_image(
function(path) {
// let processing load it in the cache
sketch.imageCache.add(path);
// count it
num_images += 1;
//console.log("loading "+path);
}
);
};
// DO NOT CALL BEFORE CALLING PRELOAD
obj.num_loaded = function() {
return num_images - sketch.imageCache.pending;
};
// DO NOT CALL BEFORE CALLING PRELOAD
obj.num_images = function() {
return num_images;
};
// DO NOT CALL BEFORE CALLING PRELOAD
obj.is_done_preloading = function() {
return !sketch.imageCache.pending;
};
// DO NOT CALL BEFORE PRELOADING IS DONE
// should be called after preloading is done
// to make the image objects
// note: must be passed a processing instance p
obj.init_images = function(p) {
console.log("initing images");
for_each_image(
function(img_path, type) {
// make an actual image object for each preloaded image
all_images[type].images.push({
path: img_path,
image: p.loadImage(img_path),
});
}
);
};
// --- private methods ---
// gets array of paths from the object populated by the php function
var files_in_dir = function(dir_path) {
if (!all_image_files[dir_path]) {
console.error("Invalid dir: "+dir_path);
}
return all_image_files[dir_path];
};
// applies f to each image path
// f takes a path and an image type (key)
var for_each_image = function(f) {
// for each type of image
for_each(
keys(all_images),
function(key) {
// for each image in the dir
for_each(
files_in_dir("images/"+all_images[key].dir),
function(path) {
f(path, key);
}
);
}
);
};
return obj;
};
// make a global instance
var image_manager = make_image_manager();