exhibit manager progress
[laserbrain_demo] / src / exman.cc
index e0a12bb..d93c2ff 100644 (file)
@@ -1,3 +1,4 @@
+#include <algorithm>
 #include "exman.h"
 #include "exhibit.h"
 #include "blob_exhibit.h"
@@ -18,7 +19,25 @@ ExhibitManager::~ExhibitManager()
        items.clear();
 }
 
-bool ExhibitManager::load(const MetaScene *mscn, const char *fname)
+void ExhibitManager::add(Exhibit *ex)
+{
+       std::vector<Exhibit*>::iterator it = std::find(items.begin(), items.end(), ex);
+       if(it == items.end()) {
+               items.push_back(ex);
+       }
+}
+
+bool ExhibitManager::remove(Exhibit *ex)
+{
+       std::vector<Exhibit*>::iterator it = std::find(items.begin(), items.end(), ex);
+       if(it != items.end()) {
+               items.erase(it);
+               return true;
+       }
+       return false;
+}
+
+bool ExhibitManager::load(MetaScene *mscn, const char *fname)
 {
        struct ts_node *root = ts_load(fname);
        if(!root || strcmp(root->name, "exhibits") != 0) {
@@ -27,10 +46,43 @@ bool ExhibitManager::load(const MetaScene *mscn, const char *fname)
                return false;
        }
 
+       struct ts_node *iter = root->child_list;
+       while(iter) {
+               struct ts_node *node = iter;
+               iter = iter->next;
+
+               if(strcmp(node->name, "item") == 0) {
+                       SceneNode *snode;
+
+                       const char *amatch = ts_get_attr_str(node, "match_node");
+                       if(!amatch || !(snode = mscn->match_node(amatch))) {
+                               error_log("regexp \"%s\" didn't match any nodes\n", amatch ? amatch : "");
+                               continue;
+                       }
+
+                       Exhibit *ex;
+                       const char *atype = ts_get_attr_str(node, "type");
+                       if(!atype || !(ex = create_exhibit(atype))) {
+                               error_log("failed to create exhibit of type: %s\n", atype);
+                               continue;
+                       }
+
+                       ex->set_node(snode);
+                       items.push_back(ex);
+               }
+       }
+
        ts_free_tree(root);
        return true;
 }
 
+void ExhibitManager::update(float dt)
+{
+       int num = items.size();
+       for(int i=0; i<num; i++) {
+               items[i]->update(dt);
+       }
+}
 
 static Exhibit *create_exhibit(const char *type)
 {
@@ -39,5 +91,6 @@ static Exhibit *create_exhibit(const char *type)
        } else if(strcmp(type, "blobs") == 0) {
                return new BlobExhibit;
        }
+       error_log("unknown exhibit type: %s\n", type);
        return 0;
 }