]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalModelData.cxx
Split huge NasalSys header apart.
[flightgear.git] / src / Scripting / NasalModelData.cxx
1
2 #include "NasalModelData.hxx"
3
4 #include <simgear/debug/logstream.hxx>
5
6 #include "NasalSys.hxx"
7 #include <Main/globals.hxx>
8
9
10 // FGNasalModelData class.  If sgLoad3DModel() is called with a pointer to
11 // such a class, then it lets modelLoaded() run the <load> script, and the
12 // destructor the <unload> script. The latter happens when the model branch
13 // is removed from the scene graph.
14
15 unsigned int FGNasalModelData::_module_id = 0;
16
17 void FGNasalModelData::load()
18 {
19     std::stringstream m;
20     m << "__model" << _module_id++;
21     _module = m.str();
22     
23     SG_LOG(SG_NASAL, SG_DEBUG, "Loading nasal module " << _module.c_str());
24     
25     const char *s = _load ? _load->getStringValue() : "";
26     FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
27     
28     naRef arg[2];
29     arg[0] = nasalSys->propNodeGhost(_root);
30     arg[1] = nasalSys->propNodeGhost(_prop);
31     nasalSys->createModule(_module.c_str(), _path.c_str(), s, strlen(s),
32                            _root, 2, arg);
33 }
34
35 void FGNasalModelData::unload()
36 {
37     if (_module.empty())
38         return;
39     
40     FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
41     if(!nasalSys) {
42         SG_LOG(SG_NASAL, SG_WARN, "Trying to run an <unload> script "
43                "without Nasal subsystem present.");
44         return;
45     }
46     
47     SG_LOG(SG_NASAL, SG_DEBUG, "Unloading nasal module " << _module.c_str());
48     
49     if (_unload)
50     {
51         const char *s = _unload->getStringValue();
52         nasalSys->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _root);
53     }
54     
55     nasalSys->deleteModule(_module.c_str());
56 }
57
58 void FGNasalModelDataProxy::modelLoaded(const std::string& path, SGPropertyNode *prop,
59                                         osg::Node *)
60 {
61     FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
62     if(!nasalSys) {
63         SG_LOG(SG_NASAL, SG_WARN, "Trying to run a <load> script "
64                "without Nasal subsystem present.");
65         return;
66     }
67     
68     if(!prop)
69         return;
70     
71     SGPropertyNode *nasal = prop->getNode("nasal");
72     if(!nasal)
73         return;
74     
75     SGPropertyNode* load   = nasal->getNode("load");
76     SGPropertyNode* unload = nasal->getNode("unload");
77     
78     if ((!load) && (!unload))
79         return;
80     
81     _data = new FGNasalModelData(_root, path, prop, load, unload);
82     
83     // register Nasal module to be created and loaded in the main thread.
84     nasalSys->registerToLoad(_data);
85 }
86
87 FGNasalModelDataProxy::~FGNasalModelDataProxy()
88 {
89     FGNasalSys* nasalSys = (FGNasalSys*) globals->get_subsystem("nasal");
90     // when necessary, register Nasal module to be destroyed/unloaded
91     // in the main thread.
92     if ((_data.valid())&&(nasalSys))
93         nasalSys->registerToUnload(_data);
94 }