5 #include "blob_exhibit.h"
10 static Exhibit *create_exhibit(const char *type);
13 ExhibitSlot::ExhibitSlot(Exhibit *ex)
20 ExhibitSlot::~ExhibitSlot()
24 SceneNode *par = node.get_parent();
26 par->remove_child(&node);
28 while(node.get_num_children()) {
29 par->add_child(node.get_child(0));
35 void ExhibitSlot::init(Exhibit *ex)
37 std::string node_name = "ExhibitSlot";
40 node_name += std::string(":") + std::string(ex->get_name());
44 if(ex->node->get_parent()) {
45 ex->node->get_parent()->add_child(&node);
47 node.set_position(ex->node->get_node_position());
48 node.set_rotation(ex->node->get_node_rotation());
49 ex->node->set_position(Vec3(0, 0, 0));
50 ex->node->set_rotation(Quat::identity);
57 node.set_name(node_name.c_str());
60 bool ExhibitSlot::empty() const
65 Exhibit *ExhibitSlot::get_exhibit() const
70 /* In the process of attaching the exhibit, we also steal the exhibit's node
71 * from its previous parent, and reparent it to the slot's node. As the slot's
72 * node itself should have been made a child of the original parent of the
73 * exhibit during init(), the initial state is we're interjecting a null node in
74 * the scene graph between the exhibit and its original parent.
76 * Attaching to a slot, implicitly detaches from the previous slot.
78 bool ExhibitSlot::attach_exhibit(Exhibit *ex, ExSlotAttachMode mode)
80 if(!ex || this->ex) return false;
82 if(ex->prev_slot && ex->prev_slot->ex == ex) {
83 ex->prev_slot->detach_exhibit();
86 if(mode != EXSLOT_ATTACH_TRANSIENT) {
90 node.add_child(ex->node);
95 bool ExhibitSlot::detach_exhibit()
99 node.remove_child(ex->node);
105 // ---- exhibit manager implementation ----
107 ExhibitManager::ExhibitManager()
112 ExhibitManager::~ExhibitManager()
117 void ExhibitManager::clear()
119 // not deleting exhibit objects, as they will be deleted the own_scn destructor
122 int num = (int)exslots.size();
123 for(int i=0; i<num; i++) {
128 delete own_scn; // this must be the last thing to destroy
131 void ExhibitManager::add(Exhibit *ex)
133 std::vector<Exhibit*>::iterator it = std::find(items.begin(), items.end(), ex);
134 if(it == items.end()) {
136 own_scn->add_object(ex);
137 if(ex->node) own_scn->add_node(ex->node);
141 bool ExhibitManager::remove(Exhibit *ex)
143 std::vector<Exhibit*>::iterator it = std::find(items.begin(), items.end(), ex);
144 if(it != items.end()) {
146 own_scn->remove_object(ex);
147 if(ex->node) own_scn->remove_node(ex->node);
153 bool ExhibitManager::load(MetaScene *mscn, const char *fname)
155 struct ts_node *root = ts_load(fname);
156 if(!root || strcmp(root->name, "exhibits") != 0) {
158 error_log("failed to load exhibits\n");
162 /* create our own scene to manage all exhibits not already in an existing scene
163 * and add it to the metascene.
164 * Also exhibit drawing happens due to the renderer drawing the metascene
168 own_scn->name = "ad-hoc exhibits";
169 mscn->scenes.push_back(own_scn);
172 struct ts_node *iter = root->child_list;
174 struct ts_node *node = iter;
177 SceneNode *snode = 0;
179 if(strcmp(node->name, "item") == 0) {
181 const char *atype = ts_get_attr_str(node, "type");
182 if(!atype || !(ex = create_exhibit(atype))) {
183 error_log("failed to create exhibit of type: %s\n", atype);
186 const char *alabel = ts_get_attr_str(node, "label");
188 ex->set_name(alabel);
191 const char *amatch = ts_get_attr_str(node, "match_node");
193 if(!(snode = mscn->match_node(amatch))) {
194 error_log("ExhibitManager::load: regexp \"%s\" didn't match any nodes\n",
195 amatch ? amatch : "");
200 // add everything to our data structures
201 // equivalent to add_exhibit(ex), but without all the searching
202 own_scn->add_object(ex);
204 snode = new SceneNode;
205 snode->set_name(ex->get_name());
206 own_scn->add_node(snode);
211 float *apos = ts_get_attr_vec(node, "pos");
213 snode->set_position(Vec3(apos[0], apos[1], apos[2]));
215 float *arot_axis = ts_get_attr_vec(node, "rotaxis");
217 float arot_angle = ts_get_attr_num(node, "rotangle", 0.0f);
218 Vec3 axis = Vec3(arot_axis[0], arot_axis[1], arot_axis[2]);
220 q.set_rotation(axis, deg_to_rad(arot_angle));
221 snode->set_rotation(q);
223 struct ts_attr *ascale = ts_get_attr(node, "scale");
225 switch(ascale->val.type) {
227 snode->set_scaling(Vec3(ascale->val.fnum, ascale->val.fnum, ascale->val.fnum));
230 snode->set_scaling(Vec3(ascale->val.vec[0], ascale->val.vec[1], ascale->val.vec[2]));
236 // create a new slot and attach the exhibit to it
237 ExhibitSlot *slot = new ExhibitSlot(ex);
238 exslots.push_back(slot);
240 // grab any extra exhibit data
241 const char *desc = ts_get_attr_str(node, "description");
242 const char *voice = ts_get_attr_str(node, "voiceover");
247 exd.text = std::string(desc);
250 exd.voice = new OggVorbisStream;
251 if(!exd.voice->open(voice)) {
252 error_log("failed to open voiceover: %s\n", voice);
257 ex->data.push_back(exd);
266 ExSelection ExhibitManager::select(const Ray &ray) const
269 nearest.dist = FLT_MAX;
271 int nitems = items.size();
272 for(int i=0; i<nitems; i++) {
273 ExSelection sel = items[i]->select(ray);
274 if(sel && sel.dist < nearest.dist) {
282 ExSelection ExhibitManager::select(const Sphere &sph) const
288 sel.validmask = EXSEL_SPHERE;
294 ExhibitSlot *ExhibitManager::nearest_empty_slot(const Vec3 &pos, float max_dist) const
296 ExhibitSlot *nearest = 0;
297 float nearest_sqdist = max_dist * max_dist;
299 int nslots = exslots.size();
300 for(int i=0; i<nslots; i++) {
301 ExhibitSlot *slot = exslots[i];
302 if(!slot->empty()) continue;
304 Vec3 slotpos = slot->node.get_position();
305 float dsq = length_sq(pos - slotpos);
306 if(dsq < nearest_sqdist) {
308 nearest_sqdist = dsq;
315 void ExhibitManager::stash_exhibit(Exhibit *ex)
317 // make sure it's not already stashed
318 if(std::find(stashed.begin(), stashed.end(), ex) != stashed.end()) {
321 stashed.push_back(ex);
324 if(ex->node->get_parent()) {
325 ex->node->get_parent()->remove_child(ex->node);
329 void ExhibitManager::update(float dt)
331 int num = items.size();
332 for(int i=0; i<num; i++) {
333 // if the exhibit is not part of a scene graph, first call its
334 // node's update function (otherwise it would have been called recursively earlier)
335 if(items[i]->node && !items[i]->node->get_parent()) {
336 items[i]->node->update(dt);
338 items[i]->update(dt);
342 void ExhibitManager::draw() const
344 int num = items.size();
345 for(int i=0; i<num; i++) {
346 if(exsel_hover.ex == items[i]) {
347 const AABox &bvol = items[i]->get_aabox();
348 draw_geom_object(&bvol);
353 static Exhibit *create_exhibit(const char *type)
355 if(strcmp(type, "static") == 0) {
356 debug_log("creating static exhibit\n");
358 } else if(strcmp(type, "blobs") == 0) {
359 debug_log("creating blobs exhibit\n");
360 BlobExhibit *b = new BlobExhibit;
363 error_log("failed to initialize blobs exhibit\n");
368 error_log("unknown exhibit type: %s\n", type);