5 #include "blob_exhibit.h"
10 static Exhibit *create_exhibit(const char *type);
11 static void clean_desc_text(char *dest, const char *src);
14 ExhibitSlot::ExhibitSlot(Exhibit *ex)
21 ExhibitSlot::~ExhibitSlot()
25 SceneNode *par = node.get_parent();
27 par->remove_child(&node);
29 while(node.get_num_children()) {
30 par->add_child(node.get_child(0));
36 void ExhibitSlot::init(Exhibit *ex)
38 std::string node_name = "ExhibitSlot";
41 node_name += std::string(":") + std::string(ex->get_name());
45 if(ex->node->get_parent()) {
46 ex->node->get_parent()->add_child(&node);
48 node.set_position(ex->node->get_node_position());
49 node.set_rotation(ex->node->get_node_rotation());
50 ex->node->set_position(Vec3(0, 0, 0));
51 ex->node->set_rotation(Quat::identity);
58 node.set_name(node_name.c_str());
61 bool ExhibitSlot::empty() const
66 Exhibit *ExhibitSlot::get_exhibit() const
71 /* In the process of attaching the exhibit, we also steal the exhibit's node
72 * from its previous parent, and reparent it to the slot's node. As the slot's
73 * node itself should have been made a child of the original parent of the
74 * exhibit during init(), the initial state is we're interjecting a null node in
75 * the scene graph between the exhibit and its original parent.
77 * Attaching to a slot, implicitly detaches from the previous slot.
79 bool ExhibitSlot::attach_exhibit(Exhibit *ex, ExSlotAttachMode mode)
81 if(!ex || this->ex) return false;
83 if(ex->prev_slot && ex->prev_slot->ex == ex) {
84 ex->prev_slot->detach_exhibit();
87 if(mode != EXSLOT_ATTACH_TRANSIENT) {
91 node.add_child(ex->node);
96 bool ExhibitSlot::detach_exhibit()
100 node.remove_child(ex->node);
106 // ---- exhibit manager implementation ----
108 ExhibitManager::ExhibitManager()
113 ExhibitManager::~ExhibitManager()
118 void ExhibitManager::clear()
120 // not deleting exhibit objects, as they will be deleted the own_scn destructor
123 int 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()) {
137 own_scn->add_object(ex);
138 if(ex->node) own_scn->add_node(ex->node);
142 bool ExhibitManager::remove(Exhibit *ex)
144 std::vector<Exhibit*>::iterator it = std::find(items.begin(), items.end(), ex);
145 if(it != items.end()) {
147 own_scn->remove_object(ex);
148 if(ex->node) own_scn->remove_node(ex->node);
154 bool ExhibitManager::load(MetaScene *mscn, const char *fname)
156 struct ts_node *root = ts_load(fname);
157 if(!root || strcmp(root->name, "exhibits") != 0) {
159 error_log("failed to load exhibits\n");
163 /* create our own scene to manage all exhibits not already in an existing scene
164 * and add it to the metascene.
165 * Also exhibit drawing happens due to the renderer drawing the metascene
169 own_scn->name = "ad-hoc exhibits";
170 mscn->scenes.push_back(own_scn);
173 struct ts_node *iter = root->child_list;
175 struct ts_node *node = iter;
178 SceneNode *snode = 0;
180 if(strcmp(node->name, "item") == 0) {
182 const char *atype = ts_get_attr_str(node, "type");
183 if(!atype || !(ex = create_exhibit(atype))) {
184 error_log("failed to create exhibit of type: %s\n", atype);
187 const char *alabel = ts_get_attr_str(node, "label");
189 ex->set_name(alabel);
192 const char *amatch = ts_get_attr_str(node, "match_node");
194 if(!(snode = mscn->match_node(amatch))) {
195 error_log("ExhibitManager::load: regexp \"%s\" didn't match any nodes\n",
196 amatch ? amatch : "");
201 // add everything to our data structures
202 // equivalent to add_exhibit(ex), but without all the searching
203 own_scn->add_object(ex);
205 snode = new SceneNode;
206 snode->set_name(ex->get_name());
207 own_scn->add_node(snode);
212 float *apos = ts_get_attr_vec(node, "pos");
214 snode->set_position(Vec3(apos[0], apos[1], apos[2]));
216 float *arot_axis = ts_get_attr_vec(node, "rotaxis");
218 float arot_angle = ts_get_attr_num(node, "rotangle", 0.0f);
219 Vec3 axis = Vec3(arot_axis[0], arot_axis[1], arot_axis[2]);
221 q.set_rotation(axis, deg_to_rad(arot_angle));
222 snode->set_rotation(q);
224 struct ts_attr *ascale = ts_get_attr(node, "scale");
226 switch(ascale->val.type) {
228 snode->set_scaling(Vec3(ascale->val.fnum, ascale->val.fnum, ascale->val.fnum));
231 snode->set_scaling(Vec3(ascale->val.vec[0], ascale->val.vec[1], ascale->val.vec[2]));
237 // create a new slot and attach the exhibit to it
238 ExhibitSlot *slot = new ExhibitSlot(ex);
239 exslots.push_back(slot);
241 // grab any extra exhibit data
242 const char *desc = ts_get_attr_str(node, "description");
243 const char *voice = ts_get_attr_str(node, "voiceover");
248 char *fixed_desc = new char[strlen(desc) + 1];
249 clean_desc_text(fixed_desc, desc);
250 exd.text = std::string(fixed_desc);
251 delete [] fixed_desc;
254 exd.voice = new OggVorbisStream;
255 if(!exd.voice->open(voice)) {
256 error_log("failed to open voiceover: %s\n", voice);
261 ex->data.push_back(exd);
270 ExSelection ExhibitManager::select(const Ray &ray) const
273 nearest.dist = FLT_MAX;
275 int nitems = items.size();
276 for(int i=0; i<nitems; i++) {
277 ExSelection sel = items[i]->select(ray);
278 if(sel && sel.dist < nearest.dist) {
286 ExSelection ExhibitManager::select(const Sphere &sph) const
292 sel.validmask = EXSEL_SPHERE;
298 ExhibitSlot *ExhibitManager::nearest_empty_slot(const Vec3 &pos, float max_dist) const
300 ExhibitSlot *nearest = 0;
301 float nearest_sqdist = max_dist * max_dist;
303 int nslots = exslots.size();
304 for(int i=0; i<nslots; i++) {
305 ExhibitSlot *slot = exslots[i];
306 if(!slot->empty()) continue;
308 Vec3 slotpos = slot->node.get_position();
309 float dsq = length_sq(pos - slotpos);
310 if(dsq < nearest_sqdist) {
312 nearest_sqdist = dsq;
319 void ExhibitManager::stash_exhibit(Exhibit *ex)
321 // make sure it's not already stashed
322 if(std::find(stashed.begin(), stashed.end(), ex) != stashed.end()) {
325 stashed.push_back(ex);
328 if(ex->node->get_parent()) {
329 ex->node->get_parent()->remove_child(ex->node);
333 void ExhibitManager::update(float dt)
335 int num = items.size();
336 for(int i=0; i<num; i++) {
337 // if the exhibit is not part of a scene graph, first call its
338 // node's update function (otherwise it would have been called recursively earlier)
339 if(items[i]->node && !items[i]->node->get_parent()) {
340 items[i]->node->update(dt);
342 items[i]->update(dt);
346 void ExhibitManager::draw() const
348 int num = items.size();
349 for(int i=0; i<num; i++) {
350 if(exsel_hover.ex == items[i]) {
351 const AABox &bvol = items[i]->get_aabox();
352 draw_geom_object(&bvol);
357 static Exhibit *create_exhibit(const char *type)
359 if(strcmp(type, "static") == 0) {
360 debug_log("creating static exhibit\n");
362 } else if(strcmp(type, "blobs") == 0) {
363 debug_log("creating blobs exhibit\n");
364 BlobExhibit *b = new BlobExhibit;
367 error_log("failed to initialize blobs exhibit\n");
372 error_log("unknown exhibit type: %s\n", type);
376 /* clean up description text to be more easily layed out later.
378 * - remove redundant spaces
379 * - remove all newlines except as paragraph separators
380 * - remove all other whitespace chars
381 * destination buffer must be as large as the source buffer
383 static void clean_desc_text(char *dest, const char *src)
387 if(*src == '\n' && *(src + 1) == '\n') {
392 while(*src && isspace(*src)) ++src;