]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atcdialog.cxx
autopilot: Introduce virtual dtor.
[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 "atcdialog.hxx"
26
27 #include <boost/foreach.hpp>
28
29 #include <simgear/constants.h>
30 #include <simgear/structure/commands.hxx>
31 #include <simgear/compiler.h>
32 #include <simgear/props/props.hxx>
33 #include <simgear/props/props_io.hxx>
34 #include <simgear/misc/sgstream.hxx>
35 #include <simgear/math/sg_geodesy.hxx>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/structure/SGSharedPtr.hxx>
38
39 #include <Main/fg_commands.hxx>
40 #include <Main/globals.hxx>
41 #include <Main/fg_props.hxx>
42 #include <GUI/gui.h>            // mkDialog
43 #include <GUI/new_gui.hxx>
44 #include <Airports/simple.hxx>
45 #include <ATC/CommStation.hxx>
46
47
48 static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name)
49 {
50   SGPropertyNode* p;
51   
52   for (int i = 0; i < prop->nChildren(); i++)
53     if ((p = getNamedNode(prop->getChild(i), name)))
54       return p;
55   
56   if (!strcmp(prop->getStringValue("name"), name))
57     return prop;
58   return 0;
59 }
60
61 static void atcUppercase(string &s) {
62         for(unsigned int i=0; i<s.size(); ++i) {
63                 s[i] = toupper(s[i]);
64         }
65 }
66
67 class AirportsWithATC : public FGAirport::AirportFilter
68 {
69 public:
70   virtual FGPositioned::Type maxType() const {
71     return FGPositioned::SEAPORT;
72   }
73   
74   virtual bool passAirport(FGAirport* aApt) const
75   {
76     return (!aApt->commStations().empty());
77   }
78 };
79
80 void FGATCDialogNew::frequencySearch()
81 {
82         const char *dialog_name = "atc-freq-search";
83         SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
84         if (!dlg)
85                 return;
86   
87         _gui->closeDialog(dialog_name);
88   
89         SGPropertyNode_ptr button_group = getNamedNode(dlg, "quick-buttons");
90         // remove all dynamic airport/ATC buttons
91         button_group->removeChildren("button", false);
92   
93   AirportsWithATC filt;
94   FGPositioned::List results = FGPositioned::findWithinRange(globals->get_aircraft_position(), 50.0, &filt);
95   FGPositioned::sortByRange(results, globals->get_aircraft_position());
96   for (unsigned int r=0; (r<results.size()) && (r < 6); ++r) {
97     
98     SGPropertyNode *entry = button_group->getNode("button", r, true);
99                 copyProperties(button_group->getNode("button-template", true), entry);
100                 entry->removeChildren("enabled", true);
101                 entry->setStringValue("legend", results[r]->ident());
102                 entry->setStringValue("binding[0]/icao", results[r]->ident());
103   }
104   
105         // (un)hide message saying no things in range
106         SGPropertyNode_ptr range_error = getNamedNode(dlg, "no-atc-in-range");
107         range_error->setBoolValue("visible", !results.empty());
108   
109         _gui->showDialog(dialog_name);
110 }
111
112 void FGATCDialogNew::frequencyDisplay(const std::string& ident)
113 {
114         const char *dialog_name = "atc-freq-display";
115         SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
116         if (!dlg)
117                 return;
118   
119         _gui->closeDialog(dialog_name);
120   
121         SGPropertyNode_ptr freq_group = getNamedNode(dlg, "frequency-list");
122         // remove all frequency entries
123         freq_group->removeChildren("group", false);
124   
125   std::string uident(ident);
126         atcUppercase(uident);
127         string label;
128   
129         const FGAirport *a = fgFindAirportID(uident);
130         if (!a) {
131                 label = "Airport " + ident + " not found in database.";
132                 mkDialog(label.c_str());
133                 return;
134         }
135   
136         // set title
137         label = uident + " Frequencies";
138         dlg->setStringValue("text/label", label.c_str());
139   
140   const flightgear::CommStationList& comms(a->commStations());
141   if (comms.empty()) {
142     label = "No frequencies found for airport " + uident;
143                 mkDialog(label.c_str());
144                 return;
145   }
146   
147   int n = 0;
148   for (unsigned int c=0; c < comms.size(); ++c) {
149     flightgear::CommStation* comm = comms[c];
150     
151     // add frequency line (modified copy of <group-template>)
152                 SGPropertyNode *entry = freq_group->getNode("group", n, true);
153                 copyProperties(freq_group->getNode("group-template", true), entry);
154                 entry->removeChildren("enabled", true);
155     
156     entry->setStringValue("text[0]/label", comm->ident());
157     
158                 char buf[8];
159                 snprintf(buf, 8, "%.2f", comm->freqMHz());
160                 if(buf[5] == '3') buf[5] = '2';
161                 if(buf[5] == '8') buf[5] = '7';
162                 buf[7] = '\0';
163     
164                 entry->setStringValue("text[1]/label", buf);
165     ++n;
166   }
167   
168         _gui->showDialog(dialog_name);
169 }
170
171 static bool doFrequencySearch( const SGPropertyNode* )
172 {
173   FGATCDialogNew::instance()->frequencySearch();
174   return true;
175 }
176
177 static bool doFrequencyDisplay( const SGPropertyNode* args )
178 {
179   std::string icao = args->getStringValue("icao");
180   if (icao.empty()) {
181     icao = fgGetString("/sim/atc/freq-airport");
182   }
183   
184   FGATCDialogNew::instance()->frequencyDisplay(icao);
185   return true;
186 }
187
188 FGATCDialogNew * FGATCDialogNew::_instance = NULL;
189
190 FGATCDialogNew::FGATCDialogNew()
191 {
192   dialogVisible = true;
193 }
194
195 FGATCDialogNew::~FGATCDialogNew()
196 {
197 }
198
199
200
201 void FGATCDialogNew::init() {
202         // Add ATC-dialog to the command list
203     globals->get_commands()->addCommand("ATC-dialog", FGATCDialogNew::popup );
204         // Add ATC-freq-search to the command list
205         globals->get_commands()->addCommand("ATC-freq-search", doFrequencySearch);
206   globals->get_commands()->addCommand("ATC-freq-display", doFrequencyDisplay);
207   
208         // initialize properties polled in Update()
209         //globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
210         globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
211 }
212
213
214
215 // Display the ATC popup dialog box with options relevant to the users current situation.
216 // 
217 // So, the first thing we need to do is check the frequency that our pilot's nav radio
218 // is currently tuned to. The dialog should always contain an option to to tune the 
219 // to a particular frequency,
220
221 // Okay, let's see, according to my interpretation of David Luff's original code,
222 // his ATC subsystem wrote properties to a named node, called "transmission-choice"
223
224
225 void FGATCDialogNew::addEntry(int nr, const std::string& txt) {
226     commands.clear();
227     commands.push_back(txt);
228     commands.push_back(string("Toggle ground network visibility"));
229 }
230
231 void FGATCDialogNew::removeEntry(int nr) {
232     commands.clear();
233 }
234
235
236
237 void FGATCDialogNew::PopupDialog() {
238     dialogVisible = !dialogVisible;
239     return;
240 }
241
242 void FGATCDialogNew::update(double dt) {
243 //  double onBoardRadioFreq0 =
244 //      fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
245 //  double onBoardRadioFreq1 =
246 //      fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
247
248     const char *dialog_name = "atc-dialog";
249     _gui = (NewGUI *)globals->get_subsystem("gui");
250     SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
251     if (!dlg)
252         return;
253
254     _gui->closeDialog(dialog_name);
255     SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
256     button_group->removeChildren("button", false);
257
258     const int bufsize = 32;
259     char buf[bufsize];
260     int commandNr = 0;
261     // loop over all entries that should fill up the dialog; use 10 items for now...
262     BOOST_FOREACH(const std::string& i, commands) {
263         snprintf(buf, bufsize, "/sim/atc/opt[%d]", commandNr);
264             fgSetBool(buf, false);
265         SGPropertyNode *entry = button_group->getNode("button", commandNr, true);
266         copyProperties(button_group->getNode("button-template", true), entry);
267         entry->removeChildren("enabled", true);
268         entry->setStringValue("property", buf);
269         entry->setIntValue("keynum", '1' + commandNr);
270         if (commandNr == 0)
271             entry->setBoolValue("default", true);
272
273         snprintf(buf, bufsize, "%d", 1 + commandNr);
274         string legend = string(buf) + i; //"; // + current->menuentry;
275         entry->setStringValue("legend", legend.c_str());
276         entry->setIntValue("binding/value", commandNr);
277         commandNr++;
278         //current++;
279     }
280
281     if (dialogVisible) {
282         _gui->closeDialog(dialog_name);
283     } else {
284         _gui->showDialog(dialog_name);
285     }
286     //dialogVisible = !dialogVisible;
287     return;
288     /*
289     static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
290     int n = trans_num->getIntValue();
291     if (n >= 0) {
292         trans_num->setIntValue(-1);
293            // PopupCallback(n);
294         cerr << "Selected transmission message" << n << endl;
295     } */
296 }