FlightGear next
initialstate.cxx
Go to the documentation of this file.
1/*
2 * SPDX-FileName: initialstate.cxx
3 * SPDX-FileComment: setup initial state of the aircraft
4 * SPDX-FileCopyrightText: Copyright (C) 2016 James Turner <zakalawe@mac.com>
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
20using namespace simgear;
21
22
23namespace {
24
25class NodeValue
26{
27public:
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
34private:
35 std::string v;
36};
37
38SGPropertyNode_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
55namespace flightgear {
56
57bool isInitialStateName(const std::string& name)
58{
59 SGPropertyNode_ptr n = nodeForState(name);
60 return n.valid();
61}
62
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
std::string fgGetString(const char *name, const char *defaultValue)
Get a string value for a property.
Definition fg_props.cxx:556
FGGlobals * globals
Definition globals.cxx:142
FlightPlan.hxx - defines a full flight-plan object, including departure, cruise, arrival information ...
Definition Addon.cxx:53
MessageBoxResult modalMessageBox(const std::string &caption, const std::string &msg, const std::string &moreText)
void applyInitialState()
const char * name
bool isInitialStateName(const std::string &name)
is the supplied name a defined initial-state, or alias of one
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
Definition proptest.cpp:27