1 // ATCDialog.cxx - Functions and classes to handle the pop-up ATC dialog
3 // Written by Alexander Kappes and David Luff, started February 2003.
5 // Copyright (C) 2003 Alexander Kappes and David Luff
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.
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.
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.
25 #include <simgear/compiler.h>
27 #include <simgear/structure/commands.hxx>
28 #include <simgear/props/props_io.hxx>
30 #include <Main/globals.hxx>
31 #include <GUI/gui.h> // mkDialog
32 #include <GUI/new_gui.hxx>
33 #include <Main/fg_props.hxx>
35 #include "ATCDialogOld.hxx"
38 #include "ATCutils.hxx"
39 #include <Airports/simple.hxx>
40 #include <ATC/CommStation.hxx>
44 using std::ostringstream;
48 FGATCDialog *current_atcdialog;
50 // For the command manager - maybe eventually this should go in the built in command list
52 static bool do_ATC_dialog(const SGPropertyNode* arg) {
53 cerr << "Running ATCDCL do_ATC_dialog" << endl;
54 current_atcdialog->PopupDialog();
58 static bool do_ATC_freq_search(const SGPropertyNode* arg) {
59 current_atcdialog->FreqDialog();
64 ATCMenuEntry::ATCMenuEntry() {
72 ATCMenuEntry::~ATCMenuEntry() {
75 void atcUppercase(string &s) {
76 for(unsigned int i=0; i<s.size(); ++i) {
81 // find child whose <name>...</name> entry matches 'name'
82 static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name) {
85 for (int i = 0; i < prop->nChildren(); i++)
86 if ((p = getNamedNode(prop->getChild(i), name)))
89 if (!strcmp(prop->getStringValue("name"), name))
96 FGATCDialog::FGATCDialog() {
97 _callbackPending = false;
102 _gui = (NewGUI *)globals->get_subsystem("gui");
105 FGATCDialog::~FGATCDialog() {
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);
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);
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();
123 // airport->setStringValue("");
127 //static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
128 //int n = trans_num->getIntValue();
130 // trans_num->setIntValue(-1);
134 //if(_callbackPending) {
135 // if(_callbackTimer > _callbackWait) {
136 // _callbackPtr->ReceiveUserCallback(_callbackCode);
137 // _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
138 // _callbackPending = false;
140 // _callbackTimer += dt;
146 void FGATCDialog::add_entry(const string& station, const string& transmission, const string& menutext, atc_type type, int code) {
150 a.stationid = station;
151 a.transmission = transmission;
152 a.menuentry = menutext;
153 a.callback_code = code;
155 (available_dialog[type])[station.c_str()].push_back(a);
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);
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);
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;
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;
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);
204 _gui->closeDialog(dialog_name);
206 SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
207 // remove all transmission buttons
208 button_group->removeChildren("button", false);
211 FGATCMgr* pAtcMgr = globals->get_ATC_mgr();
214 SG_LOG(SG_ATC, SG_ALERT, "ERROR! No ATC manager! Oops...");
217 FGATC* atcptr = pAtcMgr->GetComm1ATCPointer(); // Hardwired to comm1 at the moment
220 label = "Not currently tuned to any ATC service";
221 mkDialog(label.c_str());
225 if(atcptr->GetType() == ATIS) {
226 label = "Tuned to ATIS - no communication possible";
227 mkDialog(label.c_str());
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();
235 if(!atcmlist.size()) {
236 label = "No transmission available";
237 mkDialog(label.c_str());
241 const int bufsize = 32;
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);
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);
258 entry->setBoolValue("default", true);
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);
267 _gui->showDialog(dialog_name);
271 void FGATCDialog::PopupCallback(int num) {
272 FGATCMgr* pAtcMgr = globals->get_ATC_mgr();
275 SG_LOG(SG_ATC, SG_ALERT, "ERROR! No ATC manager! Oops...");
278 FGATC* atcptr = pAtcMgr->GetComm1ATCPointer(); // FIXME - Hardwired to comm1 at the moment
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;
298 _callbackPtr = atcptr;
299 _callbackCode = a.callback_code;
301 //cout << "No options available...\n";
303 //cout << "Donded" << endl;