]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atcdialog.cxx
Merge branch 'next' into durk-atc
[flightgear.git] / src / ATC / atcdialog.cxx
1 // atcdialog.cxx - classes to manage user interactions with AI traffic
2 // Written by Durk Talsma, started April 3, 2011
3 // Based on earlier code written by Alexander Kappes and David Luff
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 //
19 // $Id$
20
21 #include <Main/fg_commands.hxx>
22 #include <Main/globals.hxx>
23
24 #include <simgear/constants.h>
25 #include <simgear/structure/commands.hxx>
26 #include <simgear/compiler.h>
27 #include <simgear/props/props.hxx>
28 #include <simgear/props/props_io.hxx>
29 #include <simgear/misc/sgstream.hxx>
30 #include <simgear/math/sg_geodesy.hxx>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/structure/SGSharedPtr.hxx>
33
34 #include "atcdialog.hxx"
35
36 FGATCDialogNew *currentATCDialog;
37
38 static bool doATCDialog(const SGPropertyNode* arg) {
39         cerr << "Running doATCDialog" << endl;
40         currentATCDialog->PopupDialog();
41         return(true);
42 }
43
44 FGATCDialogNew::FGATCDialogNew()
45 {
46   dialogVisible = false;
47 }
48
49 FGATCDialogNew::~FGATCDialogNew()
50 {
51 }
52
53
54
55 void FGATCDialogNew::init() {
56         // Add ATC-dialog to the command list
57         globals->get_commands()->addCommand("ATC-dialog", doATCDialog);
58         // Add ATC-freq-search to the command list
59         //globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
60
61         // initialize properties polled in Update()
62         //globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
63         globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
64 }
65
66
67
68 // Display the ATC popup dialog box with options relevant to the users current situation.
69 // 
70 // So, the first thing we need to do is check the frequency that our pilot's nav radio
71 // is currently tuned to. The dialog should always contain an option to to tune the 
72 // to a particular frequency,
73
74 // Okay, let's see, according to my interpretation of David Luff's original code,
75 // his ATC subsystem wrote properties to a named node, called "transmission-choice"
76
77 static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name) {
78     SGPropertyNode* p;
79
80     for (int i = 0; i < prop->nChildren(); i++)
81         if ((p = getNamedNode(prop->getChild(i), name)))
82             return p;
83
84         if (!strcmp(prop->getStringValue("name"), name))
85             return prop;
86       return 0;
87 }
88
89 void FGATCDialogNew::addEntry(int nr, string txt) {
90     commands.clear();
91     commands.push_back(txt);
92 }
93
94 void FGATCDialogNew::removeEntry(int nr) {
95     commands.clear();
96 }
97
98
99
100 void FGATCDialogNew::PopupDialog() {
101     /*double onBoardRadioFreq0 =
102         fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
103     double onBoardRadioFreq1 =
104         fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
105
106     const char *dialog_name = "atc-dialog";
107     _gui = (NewGUI *)globals->get_subsystem("gui");
108     SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
109     if (!dlg)
110         return;
111
112     _gui->closeDialog(dialog_name);
113     SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
114     button_group->removeChildren("button", false);
115
116     const int bufsize = 32;
117     char buf[bufsize];
118     int commandNr = 0;
119     // loop over all entries that should fill up the dialog; use 10 items for now...
120     for (StringVecIterator i = commands.begin(); i != commands.end(); i++) {
121         snprintf(buf, bufsize, "/sim/atc/opt[%d]", commandNr);
122             fgSetBool(buf, false);
123         SGPropertyNode *entry = button_group->getNode("button", commandNr, true);
124         copyProperties(button_group->getNode("button-template", true), entry);
125         entry->removeChildren("enabled", true);
126         entry->setStringValue("property", buf);
127         entry->setIntValue("keynum", '1' + commandNr);
128         if (commandNr == 0)
129             entry->setBoolValue("default", true);
130
131         snprintf(buf, bufsize, "%d", 1 + commandNr);
132         string legend = string(buf) + (*i); //"; // + current->menuentry;
133         entry->setStringValue("legend", legend.c_str());
134         entry->setIntValue("binding/value", commandNr);
135         commandNr++;
136         //current++;
137     }
138 */
139     //if (dialogVisible) {
140     //    _gui->closeDialog(dialog_name);
141     //} else {
142     //    _gui->showDialog(dialog_name);
143     //}
144     dialogVisible = !dialogVisible;
145     return;
146 }
147
148 void FGATCDialogNew::update(double dt) {
149  double onBoardRadioFreq0 =
150         fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
151     double onBoardRadioFreq1 =
152         fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
153
154     const char *dialog_name = "atc-dialog";
155     _gui = (NewGUI *)globals->get_subsystem("gui");
156     SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
157     if (!dlg)
158         return;
159
160     _gui->closeDialog(dialog_name);
161     SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
162     button_group->removeChildren("button", false);
163
164     const int bufsize = 32;
165     char buf[bufsize];
166     int commandNr = 0;
167     // loop over all entries that should fill up the dialog; use 10 items for now...
168     for (StringVecIterator i = commands.begin(); i != commands.end(); i++) {
169         snprintf(buf, bufsize, "/sim/atc/opt[%d]", commandNr);
170             fgSetBool(buf, false);
171         SGPropertyNode *entry = button_group->getNode("button", commandNr, true);
172         copyProperties(button_group->getNode("button-template", true), entry);
173         entry->removeChildren("enabled", true);
174         entry->setStringValue("property", buf);
175         entry->setIntValue("keynum", '1' + commandNr);
176         if (commandNr == 0)
177             entry->setBoolValue("default", true);
178
179         snprintf(buf, bufsize, "%d", 1 + commandNr);
180         string legend = string(buf) + (*i); //"; // + current->menuentry;
181         entry->setStringValue("legend", legend.c_str());
182         entry->setIntValue("binding/value", commandNr);
183         commandNr++;
184         //current++;
185     }
186
187     if (dialogVisible) {
188         _gui->closeDialog(dialog_name);
189     } else {
190         _gui->showDialog(dialog_name);
191     }
192     //dialogVisible = !dialogVisible;
193     return;
194     /*
195     static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
196     int n = trans_num->getIntValue();
197     if (n >= 0) {
198         trans_num->setIntValue(-1);
199            // PopupCallback(n);
200         cerr << "Selected transmission message" << n << endl;
201     } */
202 }