2 #include "NasalModelData.hxx"
3 #include "NasalSys.hxx"
4 #include <Main/globals.hxx>
6 #include <simgear/debug/logstream.hxx>
8 #include <boost/bind.hpp>
11 #include <cstring> // for strlen
13 // FGNasalModelData class. If sgLoad3DModel() is called with a pointer to
14 // such a class, then it lets modelLoaded() run the <load> script, and the
15 // destructor the <unload> script. The latter happens when the model branch
16 // is removed from the scene graph.
18 unsigned int FGNasalModelData::_max_module_id = 0;
19 FGNasalModelDataList FGNasalModelData::_loaded_models;
21 //------------------------------------------------------------------------------
22 FGNasalModelData::FGNasalModelData( SGPropertyNode *root,
23 const std::string& path,
26 SGPropertyNode* unload,
29 _root(root), _prop(prop),
30 _load(load), _unload(unload),
32 _module_id( _max_module_id++ )
34 _loaded_models.push_back(this);
37 //------------------------------------------------------------------------------
38 FGNasalModelData::~FGNasalModelData()
40 _loaded_models.remove(this);
43 //------------------------------------------------------------------------------
44 void FGNasalModelData::load()
47 m << "__model" << _module_id;
50 SG_LOG(SG_NASAL, SG_DEBUG, "Loading nasal module " << _module.c_str());
52 const char *s = _load ? _load->getStringValue() : "";
53 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
55 // Add _module_id to script local hash to allow placing canvasses on objects
57 nasalSys->getGlobals().createHash(_module).set("_module_id", _module_id);
60 arg[0] = nasalSys->propNodeGhost(_root);
61 arg[1] = nasalSys->propNodeGhost(_prop);
62 nasalSys->createModule(_module.c_str(), _path.c_str(), s, strlen(s),
66 //------------------------------------------------------------------------------
67 void FGNasalModelData::unload()
72 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
74 SG_LOG(SG_NASAL, SG_WARN, "Trying to run an <unload> script "
75 "without Nasal subsystem present.");
79 SG_LOG(SG_NASAL, SG_DEBUG, "Unloading nasal module " << _module.c_str());
83 const char *s = _unload->getStringValue();
84 nasalSys->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _root);
87 nasalSys->deleteModule(_module.c_str());
90 //------------------------------------------------------------------------------
91 osg::Node* FGNasalModelData::getNode()
96 //------------------------------------------------------------------------------
97 FGNasalModelData* FGNasalModelData::getByModuleId(unsigned int id)
99 FGNasalModelDataList::iterator it = std::find_if
101 _loaded_models.begin(),
102 _loaded_models.end(),
103 boost::bind(&FGNasalModelData::_module_id, _1) == id
106 if( it != _loaded_models.end() )
112 //------------------------------------------------------------------------------
113 FGNasalModelDataProxy::~FGNasalModelDataProxy()
115 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
116 // when necessary, register Nasal module to be destroyed/unloaded
117 // in the main thread.
118 if ((_data.valid())&&(nasalSys))
119 nasalSys->registerToUnload(_data);
122 //------------------------------------------------------------------------------
123 void FGNasalModelDataProxy::modelLoaded( const std::string& path,
124 SGPropertyNode *prop,
127 FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
129 SG_LOG(SG_NASAL, SG_WARN, "Trying to run a <load> script "
130 "without Nasal subsystem present.");
137 SGPropertyNode *nasal = prop->getNode("nasal");
141 SGPropertyNode* load = nasal->getNode("load");
142 SGPropertyNode* unload = nasal->getNode("unload");
144 if ((!load) && (!unload))
147 _data = new FGNasalModelData(_root, path, prop, load, unload, branch);
149 // register Nasal module to be created and loaded in the main thread.
150 nasalSys->registerToLoad(_data);