| 1 | /* |
| 2 | * SPDX-FileName: initialstate.cxx |
| 3 | * SPDX-FileComment: setup initial state of the aircraft |
| 4 | * SPDX-FileCopyrightText: Copyright (C) 2016 James Turner <[email protected]> |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "config.h" |
| 9 | |
| 10 | #include <algorithm> |
| 11 | |
| 12 | #include <simgear/debug/logstream.hxx> |
| 13 | #include <simgear/props/props_io.hxx> |
| 14 | |
| 15 | #include <GUI/MessageBox.hxx> |
| 16 | #include <Main/fg_props.hxx> |
| 17 | |
| 18 | #include "initialstate.hxx" |
| 19 | |
| 20 | using namespace simgear; |
| 21 | |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | class NodeValue |
| 26 | { |
| 27 | public: |
| 28 | explicit NodeValue(const std::string& s) : v(s) {} |
| 29 | bool operator()(const SGPropertyNode_ptr n) const |
| 30 | { |
| 31 | return (v == n->getStringValue()); |
| 32 | } |
| 33 | |
| 34 | private: |
| 35 | std::string v; |
| 36 | }; |
| 37 | |
| 38 | SGPropertyNode_ptr nodeForState(const std::string& nm) |
| 39 | { |
| 40 | SGPropertyNode_ptr sim = fgGetNode("/sim"); |
| 41 | const PropertyList& states = sim->getChildren("state"); |
| 42 | PropertyList::const_iterator it; |
| 43 | for (it = states.begin(); it != states.end(); ++it) { |
| 44 | const PropertyList& names = (*it)->getChildren("name"); |
| 45 | if (std::find_if(names.begin(), names.end(), NodeValue(nm)) != names.end()) { |
| 46 | return *it; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return SGPropertyNode_ptr(); |
| 51 | } |
| 52 | |
| 53 | } // namespace |
| 54 | |
| 55 | namespace flightgear { |
| 56 | |
| 57 | bool isInitialStateName(const std::string& name) |
| 58 | { |
| 59 | SGPropertyNode_ptr n = nodeForState(name); |
| 60 | return n.valid(); |
| 61 | } |
| 62 | |
| 63 | void applyInitialState() |
| 64 | { |
| 65 | std::string nm = fgGetString("/sim/aircraft-state"); |
| 66 | if (nm.empty()) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | SGPropertyNode_ptr stateNode = nodeForState(nm); |
| 71 | if (!stateNode) { |
| 72 | SG_LOG(SG_AIRCRAFT, SG_WARN, "missing state node for:" << nm); |
| 73 | std::string aircraft = fgGetString("/sim/aircraft"); |
| 74 | modalMessageBox("Unknown aircraft state", |
| 75 | "The selected aircraft (" + aircraft + ") does not have a state '" + nm + "'"); |
| 76 | |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | SG_LOG(SG_AIRCRAFT, SG_INFO, "Applying aircraft state:" << nm); |
| 81 | |
| 82 | // copy all overlay properties to the tree |
| 83 | copyProperties(stateNode->getChild("overlay"), globals->get_props()); |
| 84 | } |
| 85 | |
| 86 | } // namespace flightgear |