]> git.mxchange.org Git - flightgear.git/blob - utils/fgviewer/HLAViewerFederate.cxx
fgviewer: Import hla based viewer application.
[flightgear.git] / utils / fgviewer / HLAViewerFederate.cxx
1 // Copyright (C) 2009 - 2012  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #include "HLAViewerFederate.hxx"
23
24 #include <osg/Group>
25 #include <osgDB/Options>
26
27 #include "HLAEyeTrackerClass.hxx"
28 #include "HLAMPAircraftClass.hxx"
29 #include "HLAPerspectiveViewer.hxx"
30 #include "HLAPerspectiveViewerClass.hxx"
31 #include "HLASceneObjectClass.hxx"
32 #include "HLAView.hxx"
33 #include "HLAViewClass.hxx"
34 #include "Viewer.hxx"
35
36 namespace fgviewer {
37
38 HLAViewerFederate::HLAViewerFederate() :
39     _group(new osg::Group)
40 {
41 }
42
43 HLAViewerFederate::~HLAViewerFederate()
44 {
45 }
46
47 const HLAPerspectiveViewer*
48 HLAViewerFederate::getViewer() const
49 {
50     return _perspectiveViewer.get();
51 }
52
53 HLAPerspectiveViewer*
54 HLAViewerFederate::getViewer()
55 {
56     return _perspectiveViewer.get();
57 }
58
59 void
60 HLAViewerFederate::setViewer(HLAPerspectiveViewer* perspectiveViewer)
61 {
62     if (_perspectiveViewer.get() == perspectiveViewer)
63         return;
64     _perspectiveViewer = perspectiveViewer;
65 }
66
67 const HLAView*
68 HLAViewerFederate::getView() const
69 {
70     if (!_perspectiveViewer.valid())
71         return 0;
72     return _perspectiveViewer->getView();
73 }
74
75 HLAView*
76 HLAViewerFederate::getView()
77 {
78     if (!_perspectiveViewer.valid())
79         return 0;
80     return _perspectiveViewer->getView();
81 }
82
83 void
84 HLAViewerFederate::setView(HLAView* view)
85 {
86     if (!_perspectiveViewer.valid())
87         return;
88     return _perspectiveViewer->setView(view);
89 }
90
91 simgear::HLAObjectClass*
92 HLAViewerFederate::createObjectClass(const std::string& name)
93 {
94     if (name == "FGView") {
95         _viewClass = new HLAViewClass(name, this);
96         return _viewClass.get();
97     } else if (name == "FGPerspectiveViewer") {
98         _perspectiveViewerClass = new HLAPerspectiveViewerClass(name, this);
99         return _perspectiveViewerClass.get();
100     // } else if (name == "FGOrthographicViewer") {
101     //     _orthographicViewerClass = new HLAOrthograpicViewerClass(name, this);
102     //     return _orthographicViewerClass.get();
103     } else if (name == "FGEyeTracker") {
104         return new HLAEyeTrackerClass(name, this);
105     // } else if (name == "FGPerspectiveCamera") {
106     //     _perspectiveCameraClass = new HLAPerspectiveCameraClass(name, this);
107     //     return _perspectiveCameraClass.get();
108     // } else if (name == "FGOrthographicCamera") {
109     //     _orthographicCameraClass = new HLAOrthographicCameraClass(name, this);
110     //     return _orthographicCameraClass.get();
111     // } else if (name == "FGWindowDrawable") {
112     //     _windowDrawableClass = new HLAWindowDrawableClass(name, this);
113     //     return _windowDrawableClass.get();
114     // } else if (name == "FGRenderer") {
115     //     _rendererClass = new HLARendererClass(name, this);
116     //     return _rendererClass.get();
117     }
118     /// Classes for model type objects.
119     else if (name == "FGSceneObject") {
120         return new HLASceneObjectClass(name, this);
121     } else if (name == "MPAircraft") {
122         return new HLAMPAircraftClass(name, this);
123     }
124     /// Do not subscribe to anything else
125     else {
126         return 0;
127     }
128 }
129
130 bool
131 HLAViewerFederate::init()
132 {
133     if (!HLAFederate::init())
134         return false;
135     
136     if (!_perspectiveViewerClass.valid())
137         return false;
138     if (!_viewClass.valid())
139         return false;
140     
141     // Create a viewer ...
142     _perspectiveViewer = new HLAPerspectiveViewer(_perspectiveViewerClass.get(), this);
143     _perspectiveViewer->registerInstance(_perspectiveViewerClass.get());
144     // ... with a nice view
145     _defaultView = new HLAView(_viewClass.get(), this);
146     _defaultView->registerInstance(_viewClass.get());
147     _perspectiveViewer->setView(_defaultView.get());
148     
149     return true;
150 }
151
152 bool
153 HLAViewerFederate::update()
154 {
155     // if (_view.valid())
156     //     _view->updateAttributeValues(simgear::RTIData("frame"));
157     if (_perspectiveViewer.valid())
158         _perspectiveViewer->updateAttributeValues(simgear::RTIData("frame"));
159
160     // Just go ahead as far as possible.
161     // This is just fine for a viewer.
162     return HLAFederate::timeAdvanceAvailable();
163 }
164
165 bool
166 HLAViewerFederate::shutdown()
167 {
168     if (_defaultView.valid())
169         _defaultView->deleteInstance(simgear::RTIData("shutdown"));
170     _defaultView.clear();
171
172     if (_perspectiveViewer.valid())
173         _perspectiveViewer->deleteInstance(simgear::RTIData("shutdown"));
174     _perspectiveViewer.clear();
175
176     _group->removeChildren(0, _group->getNumChildren());
177
178     return HLAFederate::shutdown();
179 }
180
181 osg::Node*
182 HLAViewerFederate::getDynamicModelNode()
183 {
184     return _group.get();
185 }
186
187 void
188 HLAViewerFederate::addDynamicModel(osg::Node* node)
189 {
190     /// FIXME can do something more intelligent like a dequeue
191     /// when returning here something to identify the entry, removal is about O(1)
192     _group->addChild(node);
193 }
194
195 void
196 HLAViewerFederate::removeDynamicModel(osg::Node* node)
197 {
198     _group->removeChild(node);
199 }
200
201 osg::ref_ptr<const osgDB::Options>
202 HLAViewerFederate::getReaderWriterOptions() const
203 {
204     // Cannot use the thread safe variant of the observed_ptr methods here since the
205     // viewer is currently allocated on the stack.
206     if (!_viewer.valid())
207         return osg::ref_ptr<const osgDB::Options>();
208     return _viewer->getReaderWriterOptions();
209 }
210
211 void
212 HLAViewerFederate::attachToViewer(Viewer* viewer)
213 {
214     if (!viewer) {
215         SG_LOG(SG_NETWORK, SG_ALERT, "HLAViewerFederate::attachToViewer(): Ignoring zero viewer!");
216         return;
217     }
218     if (_viewer.valid()) {
219         SG_LOG(SG_NETWORK, SG_ALERT, "HLAViewerFederate::attachToViewer(): Not attaching a second viewer!");
220         return;
221     }
222     _viewer = viewer;
223     _viewer->insertSceneData(getDynamicModelNode());
224 }
225
226 void
227 HLAViewerFederate::insertView(HLAView* view)
228 {
229     _viewList.push_back(view);
230     /// FIXME not always
231     setView(view);
232 }
233
234 void
235 HLAViewerFederate::eraseView(HLAView* view)
236 {
237     //// FIXME store the iterator!!!!
238     for (ViewList::iterator i = _viewList.begin(); i != _viewList.end(); ++i) {
239         if (i->get() != view)
240             continue;
241         _viewList.erase(i);
242         break;
243     }
244     if (getView() == view) {
245         if (_viewList.empty()) {
246             setView(_defaultView.get());
247         } else {
248             setView(_viewList.back().get());
249         }
250     }
251 }
252
253 } // namespace fgviewer