| 1 | // model-mgr.hxx - manage user-specified 3D models. |
| 2 | // Written by David Megginson, started 2002. |
| 3 | // |
| 4 | // This file is in the Public Domain, and comes with no warranty. |
| 5 | |
| 6 | #ifndef __MODELMGR_HXX |
| 7 | #define __MODELMGR_HXX 1 |
| 8 | |
| 9 | #include <vector> |
| 10 | #include <memory> |
| 11 | |
| 12 | #include <simgear/compiler.h> // for SG_USING_STD |
| 13 | #include <simgear/structure/subsystem_mgr.hxx> |
| 14 | |
| 15 | #include <Scripting/NasalModelData.hxx> |
| 16 | |
| 17 | // Don't pull in headers, since we don't need them here. |
| 18 | class SGPropertyNode; |
| 19 | class SGModelPlacement; |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * Manage a list of user-specified models. |
| 24 | */ |
| 25 | class FGModelMgr : public SGSubsystem |
| 26 | { |
| 27 | public: |
| 28 | |
| 29 | /** |
| 30 | * A dynamically-placed model using properties. |
| 31 | * |
| 32 | * The model manager uses the property nodes to update the model's |
| 33 | * position and orientation; any of the property node pointers may |
| 34 | * be set to zero to avoid update. Normally, a caller should |
| 35 | * load the model by instantiating SGModelPlacement with the path |
| 36 | * to the model or its XML wrapper, then assign any relevant |
| 37 | * property node pointers. |
| 38 | * |
| 39 | * @see SGModelPlacement |
| 40 | * @see FGModelMgr#add_instance |
| 41 | */ |
| 42 | struct Instance |
| 43 | { |
| 44 | virtual ~Instance (); |
| 45 | SGModelPlacement * model = nullptr; |
| 46 | SGPropertyNode_ptr node; |
| 47 | SGPropertyNode_ptr lon_deg_node; |
| 48 | SGPropertyNode_ptr lat_deg_node; |
| 49 | SGPropertyNode_ptr elev_ft_node; |
| 50 | SGPropertyNode_ptr roll_deg_node; |
| 51 | SGPropertyNode_ptr pitch_deg_node; |
| 52 | SGPropertyNode_ptr heading_deg_node; |
| 53 | SGPropertyNode_ptr loaded_node; |
| 54 | bool shadow = false; |
| 55 | |
| 56 | bool checkLoaded() const; |
| 57 | }; |
| 58 | |
| 59 | FGModelMgr (); |
| 60 | virtual ~FGModelMgr (); |
| 61 | |
| 62 | // Subsystem API. |
| 63 | void bind() override; |
| 64 | void init() override; |
| 65 | void shutdown() override; |
| 66 | void unbind() override; |
| 67 | void update(double dt) override; |
| 68 | |
| 69 | // Subsystem identification. |
| 70 | static const char* staticSubsystemClassId() { return "model-manager"; } |
| 71 | |
| 72 | virtual void add_model (SGPropertyNode * node); |
| 73 | |
| 74 | /** |
| 75 | * Add an instance of a dynamic model to the manager. |
| 76 | * |
| 77 | * NOTE: pointer ownership is transferred to the model manager! |
| 78 | * |
| 79 | * The caller is responsible for setting up the Instance structure |
| 80 | * as required. The model manager will continuously update the |
| 81 | * location and orientation of the model based on the current |
| 82 | * values of the properties. |
| 83 | */ |
| 84 | virtual void add_instance (Instance * instance); |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * Remove an instance of a dynamic model from the manager. |
| 89 | * |
| 90 | * NOTE: the manager will delete the instance as well. |
| 91 | */ |
| 92 | virtual void remove_instance (Instance * instance); |
| 93 | |
| 94 | |
| 95 | /** |
| 96 | * Finds an instance in the model manager from a given node path in the property tree. |
| 97 | * A possible path could be "models/model[0]" |
| 98 | * |
| 99 | * NOTE: the manager will delete the instance as well. |
| 100 | */ |
| 101 | Instance* findInstanceByNodePath(const std::string& nodePath) const; |
| 102 | |
| 103 | private: |
| 104 | /** |
| 105 | * Listener class that adds models at runtime. |
| 106 | */ |
| 107 | class Listener : public SGPropertyChangeListener |
| 108 | { |
| 109 | public: |
| 110 | Listener(FGModelMgr *mgr) : _mgr(mgr) {} |
| 111 | virtual void childAdded (SGPropertyNode * parent, SGPropertyNode * child); |
| 112 | virtual void childRemoved (SGPropertyNode * parent, SGPropertyNode * child); |
| 113 | |
| 114 | private: |
| 115 | FGModelMgr * _mgr; |
| 116 | }; |
| 117 | |
| 118 | SGPropertyNode_ptr _models; |
| 119 | std::unique_ptr<Listener> _listener; |
| 120 | |
| 121 | std::vector<Instance *> _instances; |
| 122 | }; |
| 123 | |
| 124 | #endif // __MODELMGR_HXX |