]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATCDialogOld.cxx
Fix MSVC9 build
[flightgear.git] / src / ATCDCL / ATCDialogOld.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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <simgear/compiler.h>
26
27 #include <simgear/structure/commands.hxx>
28 #include <simgear/props/props_io.hxx>
29
30 #include <Main/globals.hxx>
31 #include <GUI/gui.h>            // mkDialog
32 #include <GUI/new_gui.hxx>
33
34 #include "ATCDialogOld.hxx"
35 #include "ATC.hxx"
36 #include "ATCmgr.hxx"
37 #include "ATCutils.hxx"
38 #include <Airports/simple.hxx>
39 #include <ATC/CommStation.hxx>
40
41 #include <sstream>
42
43 using std::ostringstream;
44 using std::cerr;
45 using std::endl;
46
47 FGATCDialog *current_atcdialog;
48
49 // For the command manager - maybe eventually this should go in the built in command list
50 static bool do_ATC_dialog(const SGPropertyNode* arg) {
51         cerr << "Running ATCDCL do_ATC_dialog" << endl;
52         current_atcdialog->PopupDialog();
53         return(true);
54 }
55
56 static bool do_ATC_freq_search(const SGPropertyNode* arg) {
57         current_atcdialog->FreqDialog();
58         return(true);
59 }
60
61 ATCMenuEntry::ATCMenuEntry() {
62   stationid    = "";
63   //stationfr    = 0;
64   transmission = "";
65   menuentry    = "";
66   callback_code = 0;
67 }
68
69 ATCMenuEntry::~ATCMenuEntry() {
70 }
71
72 void atcUppercase(string &s) {
73         for(unsigned int i=0; i<s.size(); ++i) {
74                 s[i] = toupper(s[i]);
75         }
76 }
77
78 // find child whose <name>...</name> entry matches 'name'
79 static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name) {
80         SGPropertyNode* p;
81
82         for (int i = 0; i < prop->nChildren(); i++)
83                 if ((p = getNamedNode(prop->getChild(i), name)))
84                         return p;
85
86         if (!strcmp(prop->getStringValue("name"), name))
87                 return prop;
88
89         return 0;
90 }
91
92
93 FGATCDialog::FGATCDialog() {
94         _callbackPending = false;
95         _callbackTimer = 0.0;
96         _callbackWait = 0.0;
97         _callbackPtr = NULL;
98         _callbackCode = 0;
99         _gui = (NewGUI *)globals->get_subsystem("gui");
100 }
101
102 FGATCDialog::~FGATCDialog() {
103 }
104
105 void FGATCDialog::Init() {
106         // Add ATC-dialog to the command list
107         //globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
108         // Add ATC-freq-search to the command list
109         //globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
110
111         // initialize properties polled in Update()
112         //globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
113         //globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
114 }
115
116 void FGATCDialog::Update(double dt) {
117         //static SGPropertyNode_ptr airport = globals->get_props()->getNode("/sim/atc/freq-airport", true);
118         //string s = airport->getStringValue();
119         //if (!s.empty()) {
120         //      airport->setStringValue("");
121         //      FreqDisplay(s);
122         //}
123
124         //static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
125         //int n = trans_num->getIntValue();
126         //if (n >= 0) {
127         //      trans_num->setIntValue(-1);
128         //      PopupCallback(n);
129         //}
130
131         //if(_callbackPending) {
132         //      if(_callbackTimer > _callbackWait) {
133         //              _callbackPtr->ReceiveUserCallback(_callbackCode);
134         //              _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
135         //              _callbackPending = false;
136         //      } else {
137         //              _callbackTimer += dt;
138         //      }
139         //}
140 }
141
142 // Add an entry
143 void FGATCDialog::add_entry(const string& station, const string& transmission, const string& menutext, atc_type type, int code) {
144
145   ATCMenuEntry a;
146
147   a.stationid = station;
148   a.transmission = transmission;
149   a.menuentry = menutext;
150   a.callback_code = code;
151
152   (available_dialog[type])[station.c_str()].push_back(a);
153
154 }
155
156 void FGATCDialog::remove_entry( const string &station, const string &trans, atc_type type ) {
157   atcmentry_vec_type* p = &((available_dialog[type])[station]);
158   atcmentry_vec_iterator current = p->begin();
159   while(current != p->end()) {
160     if(current->transmission == trans) current = p->erase(current);
161         else ++current;
162   }
163 }
164
165 void FGATCDialog::remove_entry( const string &station, int code, atc_type type ) {
166   atcmentry_vec_type* p = &((available_dialog[type])[station]);
167   atcmentry_vec_iterator current = p->begin();
168   while(current != p->end()) {
169     if(current->callback_code == code) current = p->erase(current);
170         else ++current;
171   }
172 }
173
174 // query the database whether the transmission is already registered;
175 bool FGATCDialog::trans_reg( const string &station, const string &trans, atc_type type ) {
176   atcmentry_vec_type* p = &((available_dialog[type])[station]);
177   atcmentry_vec_iterator current = p->begin();
178   for ( ; current != p->end() ; ++current ) {
179     if ( current->transmission == trans ) return true;
180   }
181   return false;
182 }
183
184 // query the database whether the transmission is already registered;
185 bool FGATCDialog::trans_reg( const string &station, int code, atc_type type ) {
186   atcmentry_vec_type* p = &((available_dialog[type])[station]);
187   atcmentry_vec_iterator current = p->begin();
188   for ( ; current != p->end() ; ++current ) {
189     if ( current->callback_code == code ) return true;
190   }
191   return false;
192 }
193
194 // Display the ATC popup dialog box with options relevant to the users current situation.
195 void FGATCDialog::PopupDialog() {
196         const char *dialog_name = "atc-dialog";
197         SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
198         if (!dlg)
199                 return;
200
201         _gui->closeDialog(dialog_name);
202
203         SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
204         // remove all transmission buttons
205         button_group->removeChildren("button", false);
206
207         string label;
208         FGATCMgr* pAtcMgr = globals->get_ATC_mgr();
209         if (!pAtcMgr)
210         {
211             SG_LOG(SG_ATC, SG_ALERT, "ERROR! No ATC manager! Oops...");
212             return;
213         }
214         FGATC* atcptr = pAtcMgr->GetComm1ATCPointer();  // Hardwired to comm1 at the moment
215
216         if (!atcptr) {
217                 label = "Not currently tuned to any ATC service";
218                 mkDialog(label.c_str());
219                 return;
220         }
221
222         if(atcptr->GetType() == ATIS) {
223                 label = "Tuned to ATIS - no communication possible";
224                 mkDialog(label.c_str());
225                 return;
226         }
227
228         atcmentry_vec_type atcmlist = (available_dialog[atcptr->GetType()])[atcptr->get_ident()];
229         atcmentry_vec_iterator current = atcmlist.begin();
230         atcmentry_vec_iterator last = atcmlist.end();
231         
232         if(!atcmlist.size()) {
233                 label = "No transmission available";
234                 mkDialog(label.c_str());
235                 return;
236         }
237
238         const int bufsize = 32;
239         char buf[bufsize];
240         // loop over all entries in atcmentrylist
241         for (int n = 0; n < 10; ++n) {
242                 snprintf(buf, bufsize, "/sim/atc/opt[%d]", n);
243                 fgSetBool(buf, false);
244
245                 if (current == last)
246                         continue;
247
248                 // add transmission button (modified copy of <button-template>)
249                 SGPropertyNode *entry = button_group->getNode("button", n, true);
250                 copyProperties(button_group->getNode("button-template", true), entry);
251                 entry->removeChildren("enabled", true);
252                 entry->setStringValue("property", buf);
253                 entry->setIntValue("keynum", '1' + n);
254                 if (n == 0)
255                         entry->setBoolValue("default", true);
256
257                 snprintf(buf, bufsize, "%d", n + 1);
258                 string legend = string(buf) + ". " + current->menuentry;
259                 entry->setStringValue("legend", legend.c_str());
260                 entry->setIntValue("binding/value", n);
261                 current++;
262         }
263
264         _gui->showDialog(dialog_name);
265         return;
266 }
267
268 void FGATCDialog::PopupCallback(int num) {
269     FGATCMgr* pAtcMgr = globals->get_ATC_mgr();
270     if (!pAtcMgr)
271     {
272         SG_LOG(SG_ATC, SG_ALERT, "ERROR! No ATC manager! Oops...");
273         return;
274     }
275         FGATC* atcptr = pAtcMgr->GetComm1ATCPointer();  // FIXME - Hardwired to comm1 at the moment
276
277         if (!atcptr)
278                 return;
279
280         if (atcptr->GetType() == TOWER) {
281                 //cout << "TOWER " << endl;
282                 //cout << "ident is " << atcptr->get_ident() << endl;
283                 atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
284                 int size = atcmlist.size();
285                 if(size && num < size) {
286                         //cout << "Doing callback...\n";
287                         ATCMenuEntry a = atcmlist[num];
288                         atcptr->SetFreqInUse();
289                         string pilot = atcptr->GenText(a.transmission, a.callback_code);
290                         fgSetString("/sim/messages/pilot", pilot.c_str());
291                         // This is the user's speech getting displayed.
292                         _callbackPending = true;
293                         _callbackTimer = 0.0;
294                         _callbackWait = 5.0;
295                         _callbackPtr = atcptr;
296                         _callbackCode = a.callback_code;
297                 } else {
298                         //cout << "No options available...\n";
299                 }
300                 //cout << "Donded" << endl;
301         }
302 }
303
304 class AirportsWithATC : public FGAirport::AirportFilter
305 {
306 public:
307     virtual FGPositioned::Type maxType() const {
308       return FGPositioned::SEAPORT;
309     }
310   
311     virtual bool passAirport(FGAirport* aApt) const
312     {
313       return (!aApt->commStations().empty());
314     }
315 };
316
317 void FGATCDialog::FreqDialog() {
318         const char *dialog_name = "atc-freq-search";
319         SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
320         if (!dlg)
321                 return;
322
323         _gui->closeDialog(dialog_name);
324
325         SGPropertyNode_ptr button_group = getNamedNode(dlg, "quick-buttons");
326         // remove all dynamic airport/ATC buttons
327         button_group->removeChildren("button", false);
328
329
330   SGGeod geod(SGGeod::fromDegFt(fgGetDouble("/position/longitude-deg"),
331     fgGetDouble("/position/latitude-deg"), fgGetDouble("/position/altitude-ft")));
332
333     AirportsWithATC filt;
334     FGPositioned::List results = FGPositioned::findWithinRange(geod, 50.0, &filt);
335     FGPositioned::sortByRange(results, geod);
336     for (unsigned int r=0; (r<results.size()) && (r < 6); ++r) {
337       
338         SGPropertyNode *entry = button_group->getNode("button", r, true);
339                 copyProperties(button_group->getNode("button-template", true), entry);
340                 entry->removeChildren("enabled", true);
341                 entry->setStringValue("legend", results[r]->ident());
342                 entry->setStringValue("binding[0]/value", results[r]->ident());
343     }
344     
345         // (un)hide message saying no things in range
346         SGPropertyNode_ptr range_error = getNamedNode(dlg, "no-atc-in-range");
347         range_error->setBoolValue("enabled", !results.empty());
348
349         _gui->showDialog(dialog_name);
350 }
351
352 void FGATCDialog::FreqDisplay(string& ident) {
353         const char *dialog_name = "atc-freq-display";
354         SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
355         if (!dlg)
356                 return;
357
358         _gui->closeDialog(dialog_name);
359
360         SGPropertyNode_ptr freq_group = getNamedNode(dlg, "frequency-list");
361         // remove all frequency entries
362         freq_group->removeChildren("group", false);
363
364         atcUppercase(ident);
365         string label;
366
367         const FGAirport *a = fgFindAirportID(ident);
368         if (!a) {
369                 label = "Airport " + ident + " not found in database.";
370                 mkDialog(label.c_str());
371                 return;
372         }
373
374         // set title
375         label = ident + " Frequencies";
376         dlg->setStringValue("text/label", label.c_str());
377
378     const flightgear::CommStationList& comms(a->commStations());
379     if (comms.empty()) {
380         label = "No frequencies found for airport " + ident;
381                 mkDialog(label.c_str());
382                 return;
383     }
384     
385     int n = 0;
386     for (unsigned int c=0; c < comms.size(); ++c) {
387         flightgear::CommStation* comm = comms[c];
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("enabled", true);
393
394         entry->setStringValue("text[0]/label", comm->ident());
395
396                 char buf[8];
397                 snprintf(buf, 8, "%.2f", comm->freqMHz());
398                 if(buf[5] == '3') buf[5] = '2';
399                 if(buf[5] == '8') buf[5] = '7';
400                 buf[7] = '\0';
401
402                 entry->setStringValue("text[1]/label", buf);
403         ++n;
404     }
405
406         _gui->showDialog(dialog_name);
407 }
408