]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATCDialogOld.cxx
Bugfixes and some finetuning:
[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 #include <Main/fg_props.hxx>
34
35 #include "ATCDialogOld.hxx"
36 #include "ATC.hxx"
37 #include "ATCmgr.hxx"
38 #include "ATCutils.hxx"
39 #include <Airports/simple.hxx>
40 #include <ATC/CommStation.hxx>
41
42 #include <sstream>
43
44 using std::ostringstream;
45 using std::cerr;
46 using std::endl;
47
48 FGATCDialog *current_atcdialog;
49
50 // For the command manager - maybe eventually this should go in the built in command list
51 #if 0
52 static bool do_ATC_dialog(const SGPropertyNode* arg) {
53         cerr << "Running ATCDCL do_ATC_dialog" << endl;
54         current_atcdialog->PopupDialog();
55         return(true);
56 }
57
58 static bool do_ATC_freq_search(const SGPropertyNode* arg) {
59         current_atcdialog->FreqDialog();
60         return(true);
61 }
62 #endif
63
64 ATCMenuEntry::ATCMenuEntry() {
65   stationid    = "";
66   //stationfr    = 0;
67   transmission = "";
68   menuentry    = "";
69   callback_code = 0;
70 }
71
72 ATCMenuEntry::~ATCMenuEntry() {
73 }
74
75 void atcUppercase(string &s) {
76         for(unsigned int i=0; i<s.size(); ++i) {
77                 s[i] = toupper(s[i]);
78         }
79 }
80
81 // find child whose <name>...</name> entry matches 'name'
82 static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name) {
83         SGPropertyNode* p;
84
85         for (int i = 0; i < prop->nChildren(); i++)
86                 if ((p = getNamedNode(prop->getChild(i), name)))
87                         return p;
88
89         if (!strcmp(prop->getStringValue("name"), name))
90                 return prop;
91
92         return 0;
93 }
94
95
96 FGATCDialog::FGATCDialog() {
97         _callbackPending = false;
98         _callbackTimer = 0.0;
99         _callbackWait = 0.0;
100         _callbackPtr = NULL;
101         _callbackCode = 0;
102         _gui = (NewGUI *)globals->get_subsystem("gui");
103 }
104
105 FGATCDialog::~FGATCDialog() {
106 }
107
108 void FGATCDialog::Init() {
109         // Add ATC-dialog to the command list
110         //globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
111         // Add ATC-freq-search to the command list
112         //globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
113
114         // initialize properties polled in Update()
115         //globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
116         //globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
117 }
118
119 void FGATCDialog::Update(double dt) {
120         //static SGPropertyNode_ptr airport = globals->get_props()->getNode("/sim/atc/freq-airport", true);
121         //string s = airport->getStringValue();
122         //if (!s.empty()) {
123         //      airport->setStringValue("");
124         //      FreqDisplay(s);
125         //}
126
127         //static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
128         //int n = trans_num->getIntValue();
129         //if (n >= 0) {
130         //      trans_num->setIntValue(-1);
131         //      PopupCallback(n);
132         //}
133
134         //if(_callbackPending) {
135         //      if(_callbackTimer > _callbackWait) {
136         //              _callbackPtr->ReceiveUserCallback(_callbackCode);
137         //              _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
138         //              _callbackPending = false;
139         //      } else {
140         //              _callbackTimer += dt;
141         //      }
142         //}
143 }
144
145 // Add an entry
146 void FGATCDialog::add_entry(const string& station, const string& transmission, const string& menutext, atc_type type, int code) {
147
148   ATCMenuEntry a;
149
150   a.stationid = station;
151   a.transmission = transmission;
152   a.menuentry = menutext;
153   a.callback_code = code;
154
155   (available_dialog[type])[station.c_str()].push_back(a);
156
157 }
158
159 void FGATCDialog::remove_entry( const string &station, const string &trans, atc_type type ) {
160   atcmentry_vec_type* p = &((available_dialog[type])[station]);
161   atcmentry_vec_iterator current = p->begin();
162   while(current != p->end()) {
163     if(current->transmission == trans) current = p->erase(current);
164         else ++current;
165   }
166 }
167
168 void FGATCDialog::remove_entry( const string &station, int code, atc_type type ) {
169   atcmentry_vec_type* p = &((available_dialog[type])[station]);
170   atcmentry_vec_iterator current = p->begin();
171   while(current != p->end()) {
172     if(current->callback_code == code) current = p->erase(current);
173         else ++current;
174   }
175 }
176
177 // query the database whether the transmission is already registered;
178 bool FGATCDialog::trans_reg( const string &station, const string &trans, atc_type type ) {
179   atcmentry_vec_type* p = &((available_dialog[type])[station]);
180   atcmentry_vec_iterator current = p->begin();
181   for ( ; current != p->end() ; ++current ) {
182     if ( current->transmission == trans ) return true;
183   }
184   return false;
185 }
186
187 // query the database whether the transmission is already registered;
188 bool FGATCDialog::trans_reg( const string &station, int code, atc_type type ) {
189   atcmentry_vec_type* p = &((available_dialog[type])[station]);
190   atcmentry_vec_iterator current = p->begin();
191   for ( ; current != p->end() ; ++current ) {
192     if ( current->callback_code == code ) return true;
193   }
194   return false;
195 }
196
197 // Display the ATC popup dialog box with options relevant to the users current situation.
198 void FGATCDialog::PopupDialog() {
199         const char *dialog_name = "atc-dialog";
200         SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
201         if (!dlg)
202                 return;
203
204         _gui->closeDialog(dialog_name);
205
206         SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
207         // remove all transmission buttons
208         button_group->removeChildren("button", false);
209
210         string label;
211         FGATCMgr* pAtcMgr = globals->get_ATC_mgr();
212         if (!pAtcMgr)
213         {
214             SG_LOG(SG_ATC, SG_ALERT, "ERROR! No ATC manager! Oops...");
215             return;
216         }
217         FGATC* atcptr = pAtcMgr->GetComm1ATCPointer();  // Hardwired to comm1 at the moment
218
219         if (!atcptr) {
220                 label = "Not currently tuned to any ATC service";
221                 mkDialog(label.c_str());
222                 return;
223         }
224
225         if(atcptr->GetType() == ATIS) {
226                 label = "Tuned to ATIS - no communication possible";
227                 mkDialog(label.c_str());
228                 return;
229         }
230
231         atcmentry_vec_type atcmlist = (available_dialog[atcptr->GetType()])[atcptr->get_ident()];
232         atcmentry_vec_iterator current = atcmlist.begin();
233         atcmentry_vec_iterator last = atcmlist.end();
234         
235         if(!atcmlist.size()) {
236                 label = "No transmission available";
237                 mkDialog(label.c_str());
238                 return;
239         }
240
241         const int bufsize = 32;
242         char buf[bufsize];
243         // loop over all entries in atcmentrylist
244         for (int n = 0; n < 10; ++n) {
245                 snprintf(buf, bufsize, "/sim/atc/opt[%d]", n);
246                 fgSetBool(buf, false);
247
248                 if (current == last)
249                         continue;
250
251                 // add transmission button (modified copy of <button-template>)
252                 SGPropertyNode *entry = button_group->getNode("button", n, true);
253                 copyProperties(button_group->getNode("button-template", true), entry);
254                 entry->removeChildren("enabled", true);
255                 entry->setStringValue("property", buf);
256                 entry->setIntValue("keynum", '1' + n);
257                 if (n == 0)
258                         entry->setBoolValue("default", true);
259
260                 snprintf(buf, bufsize, "%d", n + 1);
261                 string legend = string(buf) + ". " + current->menuentry;
262                 entry->setStringValue("legend", legend.c_str());
263                 entry->setIntValue("binding/value", n);
264                 current++;
265         }
266
267         _gui->showDialog(dialog_name);
268         return;
269 }
270
271 void FGATCDialog::PopupCallback(int num) {
272     FGATCMgr* pAtcMgr = globals->get_ATC_mgr();
273     if (!pAtcMgr)
274     {
275         SG_LOG(SG_ATC, SG_ALERT, "ERROR! No ATC manager! Oops...");
276         return;
277     }
278         FGATC* atcptr = pAtcMgr->GetComm1ATCPointer();  // FIXME - Hardwired to comm1 at the moment
279
280         if (!atcptr)
281                 return;
282
283         if (atcptr->GetType() == TOWER) {
284                 //cout << "TOWER " << endl;
285                 //cout << "ident is " << atcptr->get_ident() << endl;
286                 atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
287                 int size = atcmlist.size();
288                 if(size && num < size) {
289                         //cout << "Doing callback...\n";
290                         ATCMenuEntry a = atcmlist[num];
291                         atcptr->SetFreqInUse();
292                         string pilot = atcptr->GenText(a.transmission, a.callback_code);
293                         fgSetString("/sim/messages/pilot", pilot.c_str());
294                         // This is the user's speech getting displayed.
295                         _callbackPending = true;
296                         _callbackTimer = 0.0;
297                         _callbackWait = 5.0;
298                         _callbackPtr = atcptr;
299                         _callbackCode = a.callback_code;
300                 } else {
301                         //cout << "No options available...\n";
302                 }
303                 //cout << "Donded" << endl;
304         }
305 }
306
307
308