1 // Copyright (C) 2009 - 2012 Mathias Froehlich - Mathias.Froehlich@web.de
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.
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.
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.
22 #include "HLAViewerFederate.hxx"
25 #include <osgDB/Options>
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"
38 HLAViewerFederate::HLAViewerFederate() :
39 _group(new osg::Group)
43 HLAViewerFederate::~HLAViewerFederate()
47 const HLAPerspectiveViewer*
48 HLAViewerFederate::getViewer() const
50 return _perspectiveViewer.get();
54 HLAViewerFederate::getViewer()
56 return _perspectiveViewer.get();
60 HLAViewerFederate::setViewer(HLAPerspectiveViewer* perspectiveViewer)
62 if (_perspectiveViewer.get() == perspectiveViewer)
64 _perspectiveViewer = perspectiveViewer;
68 HLAViewerFederate::getView() const
70 if (!_perspectiveViewer.valid())
72 return _perspectiveViewer->getView();
76 HLAViewerFederate::getView()
78 if (!_perspectiveViewer.valid())
80 return _perspectiveViewer->getView();
84 HLAViewerFederate::setView(HLAView* view)
86 if (!_perspectiveViewer.valid())
88 return _perspectiveViewer->setView(view);
91 simgear::HLAObjectClass*
92 HLAViewerFederate::createObjectClass(const std::string& name)
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();
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);
124 /// Do not subscribe to anything else
131 HLAViewerFederate::init()
133 if (!HLAFederate::init())
136 if (!_perspectiveViewerClass.valid())
138 if (!_viewClass.valid())
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());
153 HLAViewerFederate::update()
155 // if (_view.valid())
156 // _view->updateAttributeValues(simgear::RTIData("frame"));
157 if (_perspectiveViewer.valid())
158 _perspectiveViewer->updateAttributeValues(simgear::RTIData("frame"));
160 // Just go ahead as far as possible.
161 // This is just fine for a viewer.
162 return HLAFederate::timeAdvanceAvailable();
166 HLAViewerFederate::shutdown()
168 if (_defaultView.valid())
169 _defaultView->deleteInstance(simgear::RTIData("shutdown"));
170 _defaultView.clear();
172 if (_perspectiveViewer.valid())
173 _perspectiveViewer->deleteInstance(simgear::RTIData("shutdown"));
174 _perspectiveViewer.clear();
176 _group->removeChildren(0, _group->getNumChildren());
178 return HLAFederate::shutdown();
182 HLAViewerFederate::getDynamicModelNode()
188 HLAViewerFederate::addDynamicModel(osg::Node* node)
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);
196 HLAViewerFederate::removeDynamicModel(osg::Node* node)
198 _group->removeChild(node);
201 osg::ref_ptr<const osgDB::Options>
202 HLAViewerFederate::getReaderWriterOptions() const
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();
212 HLAViewerFederate::attachToViewer(Viewer* viewer)
215 SG_LOG(SG_NETWORK, SG_ALERT, "HLAViewerFederate::attachToViewer(): Ignoring zero viewer!");
218 if (_viewer.valid()) {
219 SG_LOG(SG_NETWORK, SG_ALERT, "HLAViewerFederate::attachToViewer(): Not attaching a second viewer!");
223 _viewer->insertSceneData(getDynamicModelNode());
227 HLAViewerFederate::insertView(HLAView* view)
229 _viewList.push_back(view);
235 HLAViewerFederate::eraseView(HLAView* view)
237 //// FIXME store the iterator!!!!
238 for (ViewList::iterator i = _viewList.begin(); i != _viewList.end(); ++i) {
239 if (i->get() != view)
244 if (getView() == view) {
245 if (_viewList.empty()) {
246 setView(_defaultView.get());
248 setView(_viewList.back().get());
253 } // namespace fgviewer