added back the demo gui for looking up widgets
[laserbrain_demo] / src / exman.cc
1 #include <algorithm>
2 #include "exman.h"
3 #include "exhibit.h"
4 #include "blob_exhibit.h"
5 #include "treestore.h"
6
7 static Exhibit *create_exhibit(const char *type);
8
9 ExhibitManager::ExhibitManager()
10 {
11 }
12
13 ExhibitManager::~ExhibitManager()
14 {
15         int num = (int)items.size();
16         for(int i=0; i<num; i++) {
17                 delete items[i];
18         }
19         items.clear();
20 }
21
22 void ExhibitManager::add(Exhibit *ex)
23 {
24         std::vector<Exhibit*>::iterator it = std::find(items.begin(), items.end(), ex);
25         if(it == items.end()) {
26                 items.push_back(ex);
27         }
28 }
29
30 bool ExhibitManager::remove(Exhibit *ex)
31 {
32         std::vector<Exhibit*>::iterator it = std::find(items.begin(), items.end(), ex);
33         if(it != items.end()) {
34                 items.erase(it);
35                 return true;
36         }
37         return false;
38 }
39
40 bool ExhibitManager::load(MetaScene *mscn, const char *fname)
41 {
42         struct ts_node *root = ts_load(fname);
43         if(!root || strcmp(root->name, "exhibits") != 0) {
44                 ts_free_tree(root);
45                 error_log("failed to load exhibits\n");
46                 return false;
47         }
48
49         struct ts_node *iter = root->child_list;
50         while(iter) {
51                 struct ts_node *node = iter;
52                 iter = iter->next;
53
54                 if(strcmp(node->name, "item") == 0) {
55                         SceneNode *snode;
56
57                         const char *amatch = ts_get_attr_str(node, "match_node");
58                         if(!amatch || !(snode = mscn->match_node(amatch))) {
59                                 error_log("regexp \"%s\" didn't match any nodes\n", amatch ? amatch : "");
60                                 continue;
61                         }
62
63                         Exhibit *ex;
64                         const char *atype = ts_get_attr_str(node, "type");
65                         if(!atype || !(ex = create_exhibit(atype))) {
66                                 error_log("failed to create exhibit of type: %s\n", atype);
67                                 continue;
68                         }
69
70                         ex->set_node(snode);
71                         items.push_back(ex);
72                 }
73         }
74
75         ts_free_tree(root);
76         return true;
77 }
78
79 void ExhibitManager::update(float dt)
80 {
81         int num = items.size();
82         for(int i=0; i<num; i++) {
83                 items[i]->update(dt);
84         }
85 }
86
87 static Exhibit *create_exhibit(const char *type)
88 {
89         if(strcmp(type, "static") == 0) {
90                 return new Exhibit;
91         } else if(strcmp(type, "blobs") == 0) {
92                 return new BlobExhibit;
93         }
94         error_log("unknown exhibit type: %s\n", type);
95         return 0;
96 }