]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atcdialog.cxx
912f35446d017346abb498275c00e61273bb0250
[flightgear.git] / src / ATC / atcdialog.cxx
1 // atcdialog.cxx - classes to manage user interactions with AI traffic
2 // Written by Durk Talsma, started April 3, 2011
3 // Based on earlier code written by Alexander Kappes and David Luff
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 //
19 // $Id$
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <Main/fg_commands.hxx>
26 #include <Main/globals.hxx>
27
28 #include <simgear/constants.h>
29 #include <simgear/structure/commands.hxx>
30 #include <simgear/compiler.h>
31 #include <simgear/props/props.hxx>
32 #include <simgear/props/props_io.hxx>
33 #include <simgear/misc/sgstream.hxx>
34 #include <simgear/math/sg_geodesy.hxx>
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/structure/SGSharedPtr.hxx>
37
38 #include "atcdialog.hxx"
39
40 FGATCDialogNew *currentATCDialog;
41
42 /*static bool doATCDialog(const SGPropertyNode* arg) {
43         //cerr << "Running doATCDialog" << endl;
44         currentATCDialog->PopupDialog();
45         return(true);
46 }*/
47 FGATCDialogNew * FGATCDialogNew::_instance = NULL;
48
49 FGATCDialogNew::FGATCDialogNew()
50 {
51   dialogVisible = true;
52 }
53
54 FGATCDialogNew::~FGATCDialogNew()
55 {
56 }
57
58
59
60 void FGATCDialogNew::init() {
61         // Add ATC-dialog to the command list
62     globals->get_commands()->addCommand("ATC-dialog", FGATCDialogNew::popup );
63         // Add ATC-freq-search to the command list
64         //globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
65
66         // initialize properties polled in Update()
67         //globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
68         globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
69 }
70
71
72
73 // Display the ATC popup dialog box with options relevant to the users current situation.
74 // 
75 // So, the first thing we need to do is check the frequency that our pilot's nav radio
76 // is currently tuned to. The dialog should always contain an option to to tune the 
77 // to a particular frequency,
78
79 // Okay, let's see, according to my interpretation of David Luff's original code,
80 // his ATC subsystem wrote properties to a named node, called "transmission-choice"
81
82 static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name) {
83     SGPropertyNode* p;
84
85     for (int i = 0; i < prop->nChildren(); i++)
86         if ((p = getNamedNode(prop->getChild(i), name)))
87             return p;
88
89         if (!strcmp(prop->getStringValue("name"), name))
90             return prop;
91       return 0;
92 }
93
94 void FGATCDialogNew::addEntry(int nr, string txt) {
95     commands.clear();
96     commands.push_back(txt);
97     commands.push_back(string("Toggle ground network visibility"));
98 }
99
100 void FGATCDialogNew::removeEntry(int nr) {
101     commands.clear();
102 }
103
104
105
106 void FGATCDialogNew::PopupDialog() {
107     dialogVisible = !dialogVisible;
108     return;
109 }
110
111 void FGATCDialogNew::update(double dt) {
112  double onBoardRadioFreq0 =
113         fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
114     double onBoardRadioFreq1 =
115         fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
116
117     const char *dialog_name = "atc-dialog";
118     _gui = (NewGUI *)globals->get_subsystem("gui");
119     SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
120     if (!dlg)
121         return;
122
123     _gui->closeDialog(dialog_name);
124     SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
125     button_group->removeChildren("button", false);
126
127     const int bufsize = 32;
128     char buf[bufsize];
129     int commandNr = 0;
130     // loop over all entries that should fill up the dialog; use 10 items for now...
131     for (StringVecIterator i = commands.begin(); i != commands.end(); i++) {
132         snprintf(buf, bufsize, "/sim/atc/opt[%d]", commandNr);
133             fgSetBool(buf, false);
134         SGPropertyNode *entry = button_group->getNode("button", commandNr, true);
135         copyProperties(button_group->getNode("button-template", true), entry);
136         entry->removeChildren("enabled", true);
137         entry->setStringValue("property", buf);
138         entry->setIntValue("keynum", '1' + commandNr);
139         if (commandNr == 0)
140             entry->setBoolValue("default", true);
141
142         snprintf(buf, bufsize, "%d", 1 + commandNr);
143         string legend = string(buf) + (*i); //"; // + current->menuentry;
144         entry->setStringValue("legend", legend.c_str());
145         entry->setIntValue("binding/value", commandNr);
146         commandNr++;
147         //current++;
148     }
149
150     if (dialogVisible) {
151         _gui->closeDialog(dialog_name);
152     } else {
153         _gui->showDialog(dialog_name);
154     }
155     //dialogVisible = !dialogVisible;
156     return;
157     /*
158     static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
159     int n = trans_num->getIntValue();
160     if (n >= 0) {
161         trans_num->setIntValue(-1);
162            // PopupCallback(n);
163         cerr << "Selected transmission message" << n << endl;
164     } */
165 }