]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCDialog.cxx
Replace hard-coded dialogs with NewGUI/XML dialogs, where some widgets
[flightgear.git] / src / ATC / ATCDialog.cxx
1 // ATCDialog.cxx - Functions and classes to handle the pop-up ATC dialog
2 //
3 // Written by Alexander Kappes and David Luff, started February 2003.
4 //
5 // Copyright (C) 2003  Alexander Kappes and David Luff
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 #include <simgear/compiler.h>
22
23 #include <simgear/structure/commands.hxx>
24
25 #include <Main/globals.hxx>
26 #include <GUI/gui.h>            // mkDialog
27 #include <GUI/new_gui.hxx>
28
29 #include "ATCDialog.hxx"
30 #include "ATC.hxx"
31 #include "ATCmgr.hxx"
32 #include "ATCdisplay.hxx"
33 #include "commlist.hxx"
34 #include "ATCutils.hxx"
35 #include <Airports/simple.hxx>
36
37 #include <sstream>
38 #include <map>
39
40 SG_USING_STD(ostringstream);
41 SG_USING_STD(map);
42
43 FGATCDialog *current_atcdialog;
44
45 // For the command manager - maybe eventually this should go in the built in command list
46 static bool do_ATC_dialog(const SGPropertyNode* arg) {
47         current_atcdialog->PopupDialog();
48         return(true);
49 }
50
51 static bool do_ATC_freq_search(const SGPropertyNode* arg) {
52         current_atcdialog->FreqDialog();
53         return(true);
54 }
55
56 ATCMenuEntry::ATCMenuEntry() {
57   stationid    = "";
58   //stationfr    = 0;
59   transmission = "";
60   menuentry    = "";
61   callback_code = 0;
62 }
63
64 ATCMenuEntry::~ATCMenuEntry() {
65 }
66
67 static void atcUppercase(string &s) {
68         for(unsigned int i=0; i<s.size(); ++i) {
69                 s[i] = toupper(s[i]);
70         }
71 }
72
73 // find child whose <name>...</name> entry matches 'name'
74 static SGPropertyNode_ptr namedNode(const vector<SGPropertyNode_ptr> &children, const char *name) {
75         SGPropertyNode_ptr ret;
76         for (int i = 0; i < children.size(); i++)
77                 if (!strcmp(children[i]->getStringValue("name"), name)) {
78                         ret = children[i];
79                         break;
80                 }
81         return ret;
82 }
83
84
85 FGATCDialog::FGATCDialog() {
86         _callbackPending = false;
87         _callbackTimer = 0.0;
88         _callbackWait = 0.0;
89         _callbackPtr = NULL;
90         _callbackCode = 0;
91         _gui = (NewGUI *)globals->get_subsystem("gui");
92 }
93
94 FGATCDialog::~FGATCDialog() {
95 }
96
97 void FGATCDialog::Init() {
98         // Add ATC-dialog to the command list
99         globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
100         // Add ATC-freq-search to the command list
101         globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
102
103         // initialize properties polled in Update()
104         globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
105         globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
106 }
107
108 void FGATCDialog::Update(double dt) {
109         static SGPropertyNode_ptr airport = globals->get_props()->getNode("/sim/atc/freq-airport", true);
110         string s = airport->getStringValue();
111         if (!s.empty()) {
112                 airport->setStringValue("");
113                 FreqDisplay(s);
114         }
115
116         static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
117         int n = trans_num->getIntValue();
118         if (n >= 0) {
119                 trans_num->setIntValue(-1);
120                 PopupCallback(n);
121         }
122
123         if(_callbackPending) {
124                 if(_callbackTimer > _callbackWait) {
125                         _callbackPtr->ReceiveUserCallback(_callbackCode);
126                         _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
127                         _callbackPending = false;
128                 } else {
129                         _callbackTimer += dt;
130                 }
131         }
132 }
133
134 // Add an entry
135 void FGATCDialog::add_entry(const string& station, const string& transmission, const string& menutext, atc_type type, int code) {
136
137   ATCMenuEntry a;
138
139   a.stationid = station;
140   a.transmission = transmission;
141   a.menuentry = menutext;
142   a.callback_code = code;
143
144   (available_dialog[type])[station.c_str()].push_back(a);
145
146 }
147
148 void FGATCDialog::remove_entry( const string &station, const string &trans, atc_type type ) {
149   atcmentry_vec_type* p = &((available_dialog[type])[station]);
150   atcmentry_vec_iterator current = p->begin();
151   while(current != p->end()) {
152     if(current->transmission == trans) current = p->erase(current);
153         else ++current;
154   }
155 }
156
157 void FGATCDialog::remove_entry( const string &station, int code, atc_type type ) {
158   atcmentry_vec_type* p = &((available_dialog[type])[station]);
159   atcmentry_vec_iterator current = p->begin();
160   while(current != p->end()) {
161     if(current->callback_code == code) current = p->erase(current);
162         else ++current;
163   }
164 }
165
166 // query the database whether the transmission is already registered;
167 bool FGATCDialog::trans_reg( const string &station, const string &trans, atc_type type ) {
168   atcmentry_vec_type* p = &((available_dialog[type])[station]);
169   atcmentry_vec_iterator current = p->begin();
170   for ( ; current != p->end() ; ++current ) {
171     if ( current->transmission == trans ) return true;
172   }
173   return false;
174 }
175
176 // query the database whether the transmission is already registered;
177 bool FGATCDialog::trans_reg( const string &station, int code, atc_type type ) {
178   atcmentry_vec_type* p = &((available_dialog[type])[station]);
179   atcmentry_vec_iterator current = p->begin();
180   for ( ; current != p->end() ; ++current ) {
181     if ( current->callback_code == code ) return true;
182   }
183   return false;
184 }
185
186 // Display the ATC popup dialog box with options relevant to the users current situation.
187 void FGATCDialog::PopupDialog() {
188         const char *dialog_name = "atc-dialog";
189         SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
190         if (!dlg)
191                 return;
192
193         _gui->closeDialog(dialog_name);
194
195         SGPropertyNode_ptr button_group = namedNode(dlg->getChildren("group"), "transmission-choice");
196         // remove all transmission buttons
197         button_group->removeChildren("button", false);
198
199         string label;
200         FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer();   // Hardwired to comm1 at the moment
201
202         if (!atcptr) {
203                 label = "Not currently tuned to any ATC service";
204                 mkDialog(label.c_str());
205                 return;
206         }
207
208         if(atcptr->GetType() == ATIS) {
209                 label = "Tuned to ATIS - no communication possible";
210                 mkDialog(label.c_str());
211                 return;
212         }
213
214         atcmentry_vec_type atcmlist = (available_dialog[atcptr->GetType()])[atcptr->get_ident()];
215         atcmentry_vec_iterator current = atcmlist.begin();
216         atcmentry_vec_iterator last = atcmlist.end();
217         
218         if(!atcmlist.size()) {
219                 label = "No transmission available";
220                 mkDialog(label.c_str());
221                 return;
222         }
223
224         const int bufsize = 32;
225         char buf[bufsize];
226         // loop over all entries in atcmentrylist
227         for (int n = 0; n < 10; ++n) {
228                 snprintf(buf, bufsize, "/sim/atc/opt[%d]", n);
229                 fgSetBool(buf, false);
230
231                 if (current == last)
232                         continue;
233
234                 // add transmission button (modified copy of <button-template>)
235                 SGPropertyNode *entry = button_group->getNode("button", n, true);
236                 copyProperties(button_group->getNode("button-template", true), entry);
237                 entry->removeChildren("hide", false);
238                 entry->setStringValue("property", buf);
239                 if (n == 0)
240                         entry->setBoolValue("default", true);
241
242                 snprintf(buf, bufsize, "%d", n + 1);
243                 string legend = string(buf) + ". " + current->menuentry;
244                 entry->setStringValue("legend", legend.c_str());
245                 entry->setIntValue("binding/value", n);
246                 current++;
247         }
248
249         _gui->showDialog(dialog_name);
250         return;
251 }
252
253 void FGATCDialog::PopupCallback(int num) {
254         FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer();   // FIXME - Hardwired to comm1 at the moment
255
256         if (!atcptr)
257                 return;
258
259         if (atcptr->GetType() == TOWER) {
260                 //cout << "TOWER " << endl;
261                 //cout << "ident is " << atcptr->get_ident() << endl;
262                 atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
263                 if(atcmlist.size()) {
264                         //cout << "Doing callback...\n";
265                         ATCMenuEntry a = atcmlist[num];
266                         atcptr->SetFreqInUse();
267                         // This is the user's speech getting displayed.
268                         globals->get_ATC_display()->RegisterSingleMessage(atcptr->GenText(a.transmission, a.callback_code));
269                         _callbackPending = true;
270                         _callbackTimer = 0.0;
271                         _callbackWait = 5.0;
272                         _callbackPtr = atcptr;
273                         _callbackCode = a.callback_code;
274                 } else {
275                         //cout << "No options available...\n";
276                 }
277                 //cout << "Donded" << endl;
278         }
279 }
280
281 // map() key data type (removes duplicates and sorts by distance)
282 struct atcdata {
283         atcdata() {}
284         atcdata(const string i, const string n, const double d) {
285                 id = i, name = n, distance = d;
286         }
287         bool operator<(const atcdata& a) const {
288                 return a.id != id && a.distance > distance;
289         }
290         bool operator==(const atcdata& a) const {
291                 return a.distance == distance && a.id == id;
292         }
293         string id;
294         string name;
295         double distance;
296 };
297
298 void FGATCDialog::FreqDialog() {
299         const char *dialog_name = "atc-freq-search";
300         SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
301         if (!dlg)
302                 return;
303
304         _gui->closeDialog(dialog_name);
305
306         SGPropertyNode_ptr button_group = namedNode(dlg->getChildren("group"), "quick-buttons");
307         // remove all dynamic ATC buttons
308         button_group->removeChildren("button", false);
309
310         // Find the ATC stations within a reasonable range (about 40 miles?)
311         comm_list_type atc_stations;
312         comm_list_iterator atc_stat_itr;
313         
314         double lon = fgGetDouble("/position/longitude-deg");
315         double lat = fgGetDouble("/position/latitude-deg");
316         double elev = fgGetDouble("/position/altitude-ft");
317         Point3D aircraft = sgGeodToCart(Point3D(lon * SGD_DEGREES_TO_RADIANS,
318                 lat * SGD_DEGREES_TO_RADIANS, elev));
319
320         // search stations in range
321         int num_stat = current_commlist->FindByPos(lon, lat, elev, 40.0, &atc_stations);
322         if (num_stat != 0) {
323                 map<atcdata, bool> uniq;
324                 // fill map (sorts by distance)
325                 comm_list_iterator itr = atc_stations.begin();
326                 for (; itr != atc_stations.end(); ++itr) {
327                         Point3D station = Point3D(itr->x, itr->y, itr->z);
328                         double distance = aircraft.distance3Dsquared(station);
329                         uniq[atcdata(itr->ident, itr->name, distance)] = true;
330                 }
331                 // create button per map entry (modified copy of <button-template>)
332                 map<atcdata, bool>::iterator uit = uniq.begin();
333                 for (int n = 0; uit != uniq.end() && n < 6; ++uit, ++n) { // max 6 buttons
334                         SGPropertyNode *entry = button_group->getNode("button", n, true);
335                         copyProperties(button_group->getNode("button-template", true), entry);
336                         entry->removeChildren("hide", false);
337                         entry->setStringValue("legend", uit->first.id.c_str());
338                         entry->setStringValue("binding[0]/value", uit->first.id.c_str());
339                 }
340         }
341
342         // (un)hide message saying no things in range
343         SGPropertyNode_ptr range_error = namedNode(dlg->getChildren("text"), "no-atc-in-range");
344         range_error->setBoolValue("hide", num_stat);
345
346         _gui->showDialog(dialog_name);
347 }
348
349 void FGATCDialog::FreqDisplay(string& ident) {
350         const char *dialog_name = "atc-freq-display";
351         SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
352         if (!dlg)
353                 return;
354
355         _gui->closeDialog(dialog_name);
356
357         SGPropertyNode_ptr freq_group = namedNode(dlg->getChildren("group"), "frequency-list");
358         // remove all frequency entries
359         freq_group->removeChildren("group", false);
360
361         atcUppercase(ident);
362         string label;
363
364         FGAirport a;
365         if (!dclFindAirportID(ident, &a)) {
366                 label = "Airport " + ident + " not found in database.";
367                 mkDialog(label.c_str());
368                 return;
369         }
370
371         // set title
372         label = ident + " Frequencies";
373         dlg->setStringValue("text/label", label.c_str());
374
375         int n = 0;      // Number of ATC frequencies at this airport
376
377         comm_list_type stations;
378         int found = current_commlist->FindByPos(a.getLongitude(), a.getLatitude(), a.getElevation(), 20.0, &stations);
379         if(found) {
380                 ostringstream ostr;
381                 comm_list_iterator itr = stations.begin();
382                 for (n = 0; itr != stations.end(); ++itr) {
383                         if(itr->ident != ident)
384                                 continue;
385
386                         if(itr->type == INVALID)
387                                 continue;
388
389                         // add frequency line (modified copy of <group-template>)
390                         SGPropertyNode *entry = freq_group->getNode("group", n, true);
391                         copyProperties(freq_group->getNode("group-template", true), entry);
392                         entry->removeChildren("hide", false);
393
394                         ostr << itr->type;
395                         entry->setStringValue("text[0]/label", ostr.str().c_str());
396
397                         char buf[8];
398                         snprintf(buf, 8, "%.2f", (itr->freq / 100.0));  // Convert from KHz to MHz
399                         if(buf[5] == '3') buf[5] = '2';
400                         if(buf[5] == '8') buf[5] = '7';
401                         buf[7] = '\0';
402
403                         entry->setStringValue("text[1]/label", buf);
404
405                         ostr.seekp(0);
406                         n++;
407                 }
408         }
409         if(n == 0) {
410                 label = "No frequencies found for airport " + ident;
411                 mkDialog(label.c_str());
412                 return;
413         }
414
415         _gui->showDialog(dialog_name);
416 }
417