| 1 | // Copyright (C) 2009 - 2012 Mathias Froehlich - [email protected] |
| 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 "HLAView.hxx" |
| 23 | |
| 24 | #include <simgear/hla/HLAArrayDataElement.hxx> |
| 25 | #include "HLAViewClass.hxx" |
| 26 | #include "HLAViewerFederate.hxx" |
| 27 | |
| 28 | namespace fgviewer { |
| 29 | |
| 30 | HLAView::HLAView(HLAViewClass* objectClass, const SGWeakPtr<simgear::HLAFederate>& federate) : |
| 31 | HLAObjectInstance(objectClass), |
| 32 | _sceneObject(federate) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | HLAView::~HLAView() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | HLAView::createAttributeDataElements() |
| 42 | { |
| 43 | /// FIXME at some point we should not need that anymore |
| 44 | HLAObjectInstance::createAttributeDataElements(); |
| 45 | |
| 46 | assert(dynamic_cast<HLAViewClass*>(getObjectClass().get())); |
| 47 | HLAViewClass& objectClass = static_cast<HLAViewClass&>(*getObjectClass()); |
| 48 | |
| 49 | setAttributeDataElement(objectClass.getNameIndex(), _name.getDataElement()); |
| 50 | setAttributeDataElement(objectClass.getPositionIndex(), _position.getDataElement()); |
| 51 | setAttributeDataElement(objectClass.getOrientationIndex(), _orientation.getDataElement()); |
| 52 | setAttributeDataElement(objectClass.getSceneObjectNameIndex(), _sceneObject.getDataElement()); |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | HLAView::discoverInstance(const simgear::RTIData& tag) |
| 57 | { |
| 58 | HLAObjectInstance::discoverInstance(tag); |
| 59 | |
| 60 | SGSharedPtr<simgear::HLAFederate> federate = getFederate().lock(); |
| 61 | if (federate.valid()) { |
| 62 | assert(dynamic_cast<HLAViewerFederate*>(federate.get())); |
| 63 | static_cast<HLAViewerFederate*>(federate.get())->insertView(this); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | HLAView::removeInstance(const simgear::RTIData& tag) |
| 69 | { |
| 70 | HLAObjectInstance::removeInstance(tag); |
| 71 | |
| 72 | SGSharedPtr<simgear::HLAFederate> federate = getFederate().lock(); |
| 73 | if (federate.valid()) { |
| 74 | assert(dynamic_cast<HLAViewerFederate*>(federate.get())); |
| 75 | static_cast<HLAViewerFederate*>(federate.get())->eraseView(this); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | HLAView::registerInstance(simgear::HLAObjectClass* objectClass) |
| 81 | { |
| 82 | HLAObjectInstance::registerInstance(objectClass); |
| 83 | |
| 84 | SGSharedPtr<simgear::HLAFederate> federate = getFederate().lock(); |
| 85 | if (federate.valid()) { |
| 86 | assert(dynamic_cast<HLAViewerFederate*>(federate.get())); |
| 87 | static_cast<HLAViewerFederate*>(federate.get())->insertView(this); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | HLAView::deleteInstance(const simgear::RTIData& tag) |
| 93 | { |
| 94 | SGSharedPtr<simgear::HLAFederate> federate = getFederate().lock(); |
| 95 | if (federate.valid()) { |
| 96 | assert(dynamic_cast<HLAViewerFederate*>(federate.get())); |
| 97 | static_cast<HLAViewerFederate*>(federate.get())->eraseView(this); |
| 98 | } |
| 99 | |
| 100 | HLAObjectInstance::deleteInstance(tag); |
| 101 | } |
| 102 | |
| 103 | const HLASceneObject* |
| 104 | HLAView::getSceneObject() const |
| 105 | { |
| 106 | /// FIXME!!! The federate should track unresolved object references |
| 107 | _sceneObject.recheckObject(); |
| 108 | return _sceneObject.getObject(); |
| 109 | } |
| 110 | |
| 111 | HLASceneObject* |
| 112 | HLAView::getSceneObject() |
| 113 | { |
| 114 | /// FIXME!!! The federate should track unresolved object references |
| 115 | _sceneObject.recheckObject(); |
| 116 | return _sceneObject.getObject(); |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | HLAView::setSceneObject(HLASceneObject* sceneObject) |
| 121 | { |
| 122 | _sceneObject.setObject(sceneObject); |
| 123 | } |
| 124 | |
| 125 | SGLocationd |
| 126 | HLAView::getLocation(const SGTimeStamp& timeStamp) const |
| 127 | { |
| 128 | const HLASceneObject* sceneObject = getSceneObject(); |
| 129 | SGLocationd location(getPosition(), getOrientation()); |
| 130 | if (!sceneObject) |
| 131 | return location; |
| 132 | return sceneObject->getLocation(timeStamp).getAbsoluteLocation(location); |
| 133 | } |
| 134 | |
| 135 | } // namespace fgviewer |