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