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