select and move exhibits
[laserbrain_demo] / src / exman.cc
index 28f3700..21e0074 100644 (file)
@@ -1,3 +1,4 @@
+#include <float.h>
 #include <algorithm>
 #include "exman.h"
 #include "exhibit.h"
@@ -73,6 +74,25 @@ bool ExhibitManager::load(MetaScene *mscn, const char *fname)
                                continue;
                        }
 
+                       const char *desc = ts_get_attr_str(node, "description");
+                       const char *voice = ts_get_attr_str(node, "voiceover");
+                       if(desc || voice) {
+                               ExData exd;
+
+                               if(desc) {
+                                       exd.text = std::string(desc);
+                               }
+                               if(voice) {
+                                       exd.voice = new OggVorbisStream;
+                                       if(!exd.voice->open(voice)) {
+                                               error_log("failed to open voiceover: %s\n", voice);
+                                               delete exd.voice;
+                                               exd.voice = 0;
+                                       }
+                               }
+                               ex->data.push_back(exd);
+                       }
+
                        ex->set_node(snode);
                        items.push_back(ex);
                }
@@ -84,18 +104,40 @@ bool ExhibitManager::load(MetaScene *mscn, const char *fname)
 
 ExSelection ExhibitManager::select(const Ray &ray) const
 {
-       return ExSelection();   // TODO
+       ExSelection nearest;
+       nearest.dist = FLT_MAX;
+
+       int nitems = items.size();
+       for(int i=0; i<nitems; i++) {
+               ExSelection sel = items[i]->select(ray);
+               if(sel && sel.dist < nearest.dist) {
+                       nearest = sel;
+               }
+       }
+
+       return nearest;
 }
 
 ExSelection ExhibitManager::select(const Sphere &sph) const
 {
-       return ExSelection();   // TODO
+       ExSelection sel;
+       if(!items.empty()) {
+               sel.ex = items[0];
+               sel.selsphere = sph;
+               sel.validmask = EXSEL_SPHERE;
+       }
+       return sel;     // TODO
 }
 
 void ExhibitManager::update(float dt)
 {
        int num = items.size();
        for(int i=0; i<num; i++) {
+               // if the exhibit is not part of a scene graph, first call its
+               // node's update function (otherwise it'll have been called recursively earlier)
+               if(!items[i]->node->get_parent()) {
+                       items[i]->node->update(dt);
+               }
                items[i]->update(dt);
        }
 }