]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATCDialogOld.cxx
Goodbye automake.
[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         FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer();   // Hardwired to comm1 at the moment
209
210         if (!atcptr) {
211                 label = "Not currently tuned to any ATC service";
212                 mkDialog(label.c_str());
213                 return;
214         }
215
216         if(atcptr->GetType() == ATIS) {
217                 label = "Tuned to ATIS - no communication possible";
218                 mkDialog(label.c_str());
219                 return;
220         }
221
222         atcmentry_vec_type atcmlist = (available_dialog[atcptr->GetType()])[atcptr->get_ident()];
223         atcmentry_vec_iterator current = atcmlist.begin();
224         atcmentry_vec_iterator last = atcmlist.end();
225         
226         if(!atcmlist.size()) {
227                 label = "No transmission available";
228                 mkDialog(label.c_str());
229                 return;
230         }
231
232         const int bufsize = 32;
233         char buf[bufsize];
234         // loop over all entries in atcmentrylist
235         for (int n = 0; n < 10; ++n) {
236                 snprintf(buf, bufsize, "/sim/atc/opt[%d]", n);
237                 fgSetBool(buf, false);
238
239                 if (current == last)
240                         continue;
241
242                 // add transmission button (modified copy of <button-template>)
243                 SGPropertyNode *entry = button_group->getNode("button", n, true);
244                 copyProperties(button_group->getNode("button-template", true), entry);
245                 entry->removeChildren("enabled", true);
246                 entry->setStringValue("property", buf);
247                 entry->setIntValue("keynum", '1' + n);
248                 if (n == 0)
249                         entry->setBoolValue("default", true);
250
251                 snprintf(buf, bufsize, "%d", n + 1);
252                 string legend = string(buf) + ". " + current->menuentry;
253                 entry->setStringValue("legend", legend.c_str());
254                 entry->setIntValue("binding/value", n);
255                 current++;
256         }
257
258         _gui->showDialog(dialog_name);
259         return;
260 }
261
262 void FGATCDialog::PopupCallback(int num) {
263         FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer();   // FIXME - Hardwired to comm1 at the moment
264
265         if (!atcptr)
266                 return;
267
268         if (atcptr->GetType() == TOWER) {
269                 //cout << "TOWER " << endl;
270                 //cout << "ident is " << atcptr->get_ident() << endl;
271                 atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
272                 int size = atcmlist.size();
273                 if(size && num < size) {
274                         //cout << "Doing callback...\n";
275                         ATCMenuEntry a = atcmlist[num];
276                         atcptr->SetFreqInUse();
277                         string pilot = atcptr->GenText(a.transmission, a.callback_code);
278                         fgSetString("/sim/messages/pilot", pilot.c_str());
279                         // This is the user's speech getting displayed.
280                         _callbackPending = true;
281                         _callbackTimer = 0.0;
282                         _callbackWait = 5.0;
283                         _callbackPtr = atcptr;
284                         _callbackCode = a.callback_code;
285                 } else {
286                         //cout << "No options available...\n";
287                 }
288                 //cout << "Donded" << endl;
289         }
290 }
291
292 class AirportsWithATC : public FGAirport::AirportFilter
293 {
294 public:
295     virtual FGPositioned::Type maxType() const {
296       return FGPositioned::SEAPORT;
297     }
298   
299     virtual bool passAirport(FGAirport* aApt) const
300     {
301       return (!aApt->commStations().empty());
302     }
303 };
304
305 void FGATCDialog::FreqDialog() {
306         const char *dialog_name = "atc-freq-search";
307         SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
308         if (!dlg)
309                 return;
310
311         _gui->closeDialog(dialog_name);
312
313         SGPropertyNode_ptr button_group = getNamedNode(dlg, "quick-buttons");
314         // remove all dynamic airport/ATC buttons
315         button_group->removeChildren("button", false);
316
317
318   SGGeod geod(SGGeod::fromDegFt(fgGetDouble("/position/longitude-deg"),
319     fgGetDouble("/position/latitude-deg"), fgGetDouble("/position/altitude-ft")));
320
321     AirportsWithATC filt;
322     FGPositioned::List results = FGPositioned::findWithinRange(geod, 50.0, &filt);
323     FGPositioned::sortByRange(results, geod);
324     for (unsigned int r=0; (r<results.size()) && (r < 6); ++r) {
325       
326         SGPropertyNode *entry = button_group->getNode("button", r, true);
327                 copyProperties(button_group->getNode("button-template", true), entry);
328                 entry->removeChildren("enabled", true);
329                 entry->setStringValue("legend", results[r]->ident());
330                 entry->setStringValue("binding[0]/value", results[r]->ident());
331     }
332     
333         // (un)hide message saying no things in range
334         SGPropertyNode_ptr range_error = getNamedNode(dlg, "no-atc-in-range");
335         range_error->setBoolValue("enabled", !results.empty());
336
337         _gui->showDialog(dialog_name);
338 }
339
340 void FGATCDialog::FreqDisplay(string& ident) {
341         const char *dialog_name = "atc-freq-display";
342         SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
343         if (!dlg)
344                 return;
345
346         _gui->closeDialog(dialog_name);
347
348         SGPropertyNode_ptr freq_group = getNamedNode(dlg, "frequency-list");
349         // remove all frequency entries
350         freq_group->removeChildren("group", false);
351
352         atcUppercase(ident);
353         string label;
354
355         const FGAirport *a = fgFindAirportID(ident);
356         if (!a) {
357                 label = "Airport " + ident + " not found in database.";
358                 mkDialog(label.c_str());
359                 return;
360         }
361
362         // set title
363         label = ident + " Frequencies";
364         dlg->setStringValue("text/label", label.c_str());
365
366     const flightgear::CommStationList& comms(a->commStations());
367     if (comms.empty()) {
368         label = "No frequencies found for airport " + ident;
369                 mkDialog(label.c_str());
370                 return;
371     }
372     
373     int n = 0;
374     for (unsigned int c=0; c < comms.size(); ++c) {
375         flightgear::CommStation* comm = comms[c];
376         
377         // add frequency line (modified copy of <group-template>)
378                 SGPropertyNode *entry = freq_group->getNode("group", n, true);
379                 copyProperties(freq_group->getNode("group-template", true), entry);
380                 entry->removeChildren("enabled", true);
381
382         entry->setStringValue("text[0]/label", comm->ident());
383
384                 char buf[8];
385                 snprintf(buf, 8, "%.2f", comm->freqMHz());
386                 if(buf[5] == '3') buf[5] = '2';
387                 if(buf[5] == '8') buf[5] = '7';
388                 buf[7] = '\0';
389
390                 entry->setStringValue("text[1]/label", buf);
391         ++n;
392     }
393
394         _gui->showDialog(dialog_name);
395 }
396