5 #include "blob_exhibit.h"
8 static Exhibit *create_exhibit(const char *type);
11 ExhibitSlot::ExhibitSlot(Exhibit *ex)
18 ExhibitSlot::~ExhibitSlot()
22 SceneNode *par = node.get_parent();
24 par->remove_child(&node);
26 while(node.get_num_children()) {
27 par->add_child(node.get_child(0));
33 void ExhibitSlot::init(Exhibit *ex)
35 std::string node_name = "ExhibitSlot";
38 node_name += std::string(":") + std::string(ex->get_name());
42 if(ex->node->get_parent()) {
43 ex->node->get_parent()->add_child(&node);
45 node.set_position(ex->node->get_node_position());
46 node.set_rotation(ex->node->get_node_rotation());
47 ex->node->set_position(Vec3(0, 0, 0));
48 ex->node->set_rotation(Quat::identity);
55 node.set_name(node_name.c_str());
58 bool ExhibitSlot::empty() const
63 Exhibit *ExhibitSlot::get_exhibit() const
68 /* In the process of attaching the exhibit, we also steal the exhibit's node
69 * from its previous parent, and reparent it to the slot's node. As the slot's
70 * node itself should have been made a child of the original parent of the
71 * exhibit during init(), the initial state is we're interjecting a null node in
72 * the scene graph between the exhibit and its original parent.
74 * Attaching to a slot, implicitly detaches from the previous slot.
76 bool ExhibitSlot::attach_exhibit(Exhibit *ex, ExSlotAttachMode mode)
78 if(!ex || this->ex) return false;
80 if(ex->prev_slot && ex->prev_slot->ex == ex) {
81 ex->prev_slot->detach_exhibit();
84 if(mode != EXSLOT_ATTACH_TRANSIENT) {
88 node.add_child(ex->node);
93 bool ExhibitSlot::detach_exhibit()
97 node.remove_child(ex->node);
103 // ---- exhibit manager implementation ----
105 ExhibitManager::ExhibitManager()
110 ExhibitManager::~ExhibitManager()
115 void ExhibitManager::clear()
117 int num = (int)items.size();
118 for(int i=0; i<num; i++) {
123 num = (int)exslots.size();
124 for(int i=0; i<num; i++) {
129 delete own_scn; // this must be the last thing to destroy
132 void ExhibitManager::add(Exhibit *ex)
134 std::vector<Exhibit*>::iterator it = std::find(items.begin(), items.end(), ex);
135 if(it == items.end()) {
140 bool ExhibitManager::remove(Exhibit *ex)
142 std::vector<Exhibit*>::iterator it = std::find(items.begin(), items.end(), ex);
143 if(it != items.end()) {
150 bool ExhibitManager::load(MetaScene *mscn, const char *fname)
152 struct ts_node *root = ts_load(fname);
153 if(!root || strcmp(root->name, "exhibits") != 0) {
155 error_log("failed to load exhibits\n");
159 // create our own scene to manage all exhibits not already in an existing scene
160 // and add it to the metascene
163 own_scn->name = "ad-hoc exhibits";
164 mscn->scenes.push_back(own_scn);
167 struct ts_node *iter = root->child_list;
169 struct ts_node *node = iter;
172 SceneNode *snode = 0;
174 if(strcmp(node->name, "item") == 0) {
176 const char *atype = ts_get_attr_str(node, "type");
177 if(!atype || !(ex = create_exhibit(atype))) {
178 error_log("failed to create exhibit of type: %s\n", atype);
181 const char *alabel = ts_get_attr_str(node, "label");
183 ex->set_name(alabel);
186 const char *amatch = ts_get_attr_str(node, "match_node");
188 if(!(snode = mscn->match_node(amatch))) {
189 error_log("ExhibitManager::load: regexp \"%s\" didn't match any nodes\n",
190 amatch ? amatch : "");
196 snode = new SceneNode;
197 snode->set_name(ex->get_name());
198 own_scn->add_node(snode);
204 float *apos = ts_get_attr_vec(node, "pos");
206 snode->set_position(Vec3(apos[0], apos[1], apos[2]));
208 float *arot_axis = ts_get_attr_vec(node, "rotaxis");
210 float arot_angle = ts_get_attr_num(node, "rotangle", 0.0f);
211 Vec3 axis = Vec3(arot_axis[0], arot_axis[1], arot_axis[2]);
213 q.set_rotation(axis, deg_to_rad(arot_angle));
214 snode->set_rotation(q);
216 struct ts_attr *ascale = ts_get_attr(node, "scale");
218 switch(ascale->val.type) {
220 snode->set_scaling(Vec3(ascale->val.fnum, ascale->val.fnum, ascale->val.fnum));
223 snode->set_scaling(Vec3(ascale->val.vec[0], ascale->val.vec[1], ascale->val.vec[2]));
229 // create a new slot and attach the exhibit to it
230 ExhibitSlot *slot = new ExhibitSlot(ex);
231 exslots.push_back(slot);
233 // grab any extra exhibit data
234 const char *desc = ts_get_attr_str(node, "description");
235 const char *voice = ts_get_attr_str(node, "voiceover");
240 exd.text = std::string(desc);
243 exd.voice = new OggVorbisStream;
244 if(!exd.voice->open(voice)) {
245 error_log("failed to open voiceover: %s\n", voice);
250 ex->data.push_back(exd);
259 ExSelection ExhibitManager::select(const Ray &ray) const
262 nearest.dist = FLT_MAX;
264 int nitems = items.size();
265 for(int i=0; i<nitems; i++) {
266 ExSelection sel = items[i]->select(ray);
267 if(sel && sel.dist < nearest.dist) {
275 ExSelection ExhibitManager::select(const Sphere &sph) const
281 sel.validmask = EXSEL_SPHERE;
287 ExhibitSlot *ExhibitManager::nearest_empty_slot(const Vec3 &pos, float max_dist) const
289 ExhibitSlot *nearest = 0;
290 float nearest_sqdist = max_dist * max_dist;
292 int nslots = exslots.size();
293 for(int i=0; i<nslots; i++) {
294 ExhibitSlot *slot = exslots[i];
295 if(!slot->empty()) continue;
297 Vec3 slotpos = slot->node.get_position();
298 float dsq = length_sq(pos - slotpos);
299 if(dsq < nearest_sqdist) {
301 nearest_sqdist = dsq;
308 void ExhibitManager::stash_exhibit(Exhibit *ex)
310 // make sure it's not already stashed
311 if(std::find(stashed.begin(), stashed.end(), ex) != stashed.end()) {
314 stashed.push_back(ex);
317 if(ex->node->get_parent()) {
318 ex->node->get_parent()->remove_child(ex->node);
322 void ExhibitManager::update(float dt)
324 int num = items.size();
325 for(int i=0; i<num; i++) {
326 // if the exhibit is not part of a scene graph, first call its
327 // node's update function (otherwise it would have been called recursively earlier)
328 if(items[i]->node && !items[i]->node->get_parent()) {
329 items[i]->node->update(dt);
331 items[i]->update(dt);
335 void ExhibitManager::draw() const
337 int num = items.size();
338 for(int i=0; i<num; i++) {
339 items[i]->pre_draw();
341 items[i]->post_draw();
345 static Exhibit *create_exhibit(const char *type)
347 if(strcmp(type, "static") == 0) {
348 debug_log("creating static exhibit\n");
350 } else if(strcmp(type, "blobs") == 0) {
351 debug_log("creating blobs exhibit\n");
352 BlobExhibit *b = new BlobExhibit;
355 error_log("failed to initialize blobs exhibit\n");
360 error_log("unknown exhibit type: %s\n", type);