1 // kln89_page_*.[ch]xx - this file is one of the "pages" that
2 // are used in the KLN89 GPS unit simulation.
3 //
4 // Written by David Luff, started 2005.
5 //
6 // Copyright (C) 2005 - David C Luff - daveluff AT ntlworld.com
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 //
22 // $Id$
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "kln89_page_apt.hxx"
29
30 #include <simgear/structure/exception.hxx>
31 #include <cassert>
32 #include <cstdio>
33
34 #include <ATC/CommStation.hxx>
35 #include <Main/globals.hxx>
36 #include <Airports/runways.hxx>
37 #include <Airports/airport.hxx>
38
39 using std::string;
40
KLN89AptPage(KLN89 * parent)41 KLN89AptPage::KLN89AptPage(KLN89* parent)
42 : KLN89Page(parent) {
43 _nSubPages = 8;
44 _subPage = 0;
45 _name = "APT";
46 _apt_id = "KHWD";
47 // Make sure that _last_apt_id doesn't match at startup to force airport data to be fetched on first update.
48 _last_apt_id = "XXXX";
49 _nRwyPages = 1;
50 _curRwyPage = 0;
51 _nFreqPages = 1;
52 _curFreqPage = 0;
53 ap = NULL;
54 _iapStart = 0;
55 _iafStart = 0;
56 _fStart = 0;
57 _iaps.clear();
58 _iafDialog = false;
59 _addDialog = false;
60 _replaceDialog = false;
61 _curIap = 0;
62 _curIaf = 0;
63 }
64
~KLN89AptPage()65 KLN89AptPage::~KLN89AptPage() {
66 }
67
Update(double dt)68 void KLN89AptPage::Update(double dt) {
69 bool actPage = (_kln89->_activePage->GetName() == "ACT" ? true : false);
70 bool multi; // Not set by FindFirst...
71 bool exact = false;
72 if(_apt_id.size() == 4) exact = true;
73 // TODO - move this search out to where the button is pressed, and cache the result!
74 if(_apt_id != _last_apt_id || ap == NULL) ap = _kln89->FindFirstAptById(_apt_id, multi, exact);
75 //if(np == NULL) cout << "NULL... ";
76 //if(b == false) cout << "false...\n";
77 /*
78 if(np && b) {
79 cout << "VOR FOUND!\n";
80 } else {
81 cout << ":-(\n";
82 }
83 */
84
85 if(ap) {
86 //cout << "Valid airport found! id = " << ap->getId() << ", elev = " << ap->getElevation() << '\n';
87 if(_apt_id != _last_apt_id) {
88 UpdateAirport(ap->getId());
89 _last_apt_id = _apt_id;
90 _curFreqPage = 0;
91 _curRwyPage = 0;
92 }
93 _apt_id = ap->getId();
94 if(_kln89->GetActiveWaypoint()) {
95 if(_apt_id == _kln89->GetActiveWaypoint()->id) {
96 if(!(_kln89->_waypointAlert && _kln89->_blink)) {
97 // Active waypoint arrow
98 _kln89->DrawSpecialChar(4, 2, 0, 3);
99 }
100 }
101 }
102 if(_kln89->_mode != KLN89_MODE_CRSR) {
103 if(!(_subPage == 7 && (_iafDialog || _addDialog || _replaceDialog))) { // Don't draw the airport name when the IAP dialogs are active
104 if(!_entInvert) {
105 if(!actPage) {
106 _kln89->DrawText(ap->getId(), 2, 1, 3);
107 } else {
108 // If it's the ACT page, The ID is shifted slightly right to make space for the waypoint index.
109 _kln89->DrawText(ap->getId(), 2, 4, 3);
110 char buf[3];
111 int n = snprintf(buf, 3, "%i", _kln89->GetActiveWaypointIndex() + 1);
112 _kln89->DrawText((string)buf, 2, 3 - n, 3);
113 }
114 } else {
115 if(!_kln89->_blink) {
116 _kln89->DrawText(ap->getId(), 2, 1, 3, false, 99);
117 _kln89->DrawEnt();
118 }
119 }
120 }
121 }
122 if(_subPage == 0) {
123 // Name
124 _kln89->DrawText(ap->getName(), 2, 0, 2);
125 // Elevation
126 _kln89->DrawText(_kln89->_altUnits == GPS_ALT_UNITS_FT ? "ft" : "m", 2, 14, 3);
127 char buf[6];
128 int n = snprintf(buf, 5, "%i", (_kln89->_altUnits == GPS_ALT_UNITS_FT ? (int)(ap->getElevation()) : (int)((double)ap->getElevation() * SG_FEET_TO_METER)));
129 _kln89->DrawText((string)buf, 2, 14 - n, 3);
130 // Town
131 airport_id_str_map_iterator itr = _kln89->_airportTowns.find(_apt_id);
132 if(itr != _kln89->_airportTowns.end()) {
133 _kln89->DrawText(itr->second, 2, 0, 1);
134 }
135 // State / Province / Country
136 itr = _kln89->_airportStates.find(_apt_id);
137 if(itr != _kln89->_airportStates.end()) {
138 _kln89->DrawText(itr->second, 2, 0, 0);
139 }
140 } else if(_subPage == 1) {
141 _kln89->DrawLatitude(ap->getLatitude(), 2, 3, 2);
142 _kln89->DrawLongitude(ap->getLongitude(), 2, 3, 1);
143 _kln89->DrawDirDistField(ap->getLatitude() * SG_DEGREES_TO_RADIANS, ap->getLongitude() * SG_DEGREES_TO_RADIANS,
144 2, 0, 0, _to_flag, (_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 5 ? true : false));
145 } else if(_subPage == 2) {
146 // Try and calculate a realistic difference from UTC based on longitude
147 // Since 0 longitude is the middle of UTC, the boundaries will be at 7.5, 22.5, 37.5 etc.
148 int hrDiff = ((int)((fabs(ap->getLongitude())) + 7.5)) / 15;
149 _kln89->DrawText("UTC", 2, 0, 2);
150 if(hrDiff != 0) {
151 _kln89->DrawText(ap->getLongitude() >= 0.0 ? "+" : "-", 2, 3, 2);
152 char buf[3];
153 snprintf(buf, 3, "%02i", hrDiff);
154 _kln89->DrawText((string)buf, 2, 4, 2);
155 _kln89->DrawText("( DT)", 2, 6, 2);
156 if(ap->getLongitude() >= 0.0) {
157 hrDiff++;
158 } else {
159 hrDiff--;
160 }
161 _kln89->DrawText(ap->getLongitude() >= 0.0 ? "+" : "-", 2, 7, 2);
162 snprintf(buf, 3, "%02i", hrDiff);
163 _kln89->DrawText((string)buf, 2, 8, 2);
164 }
165 // I guess we can make a heuristic guess as to fuel availability from the runway sizes
166 // For now assume that airports with asphalt or concrete runways will have at least 100L,
167 // and that runways over 4000ft will have JET.
168 if(_aptRwys[0]->surface() <= 2) {
169 if(_aptRwys[0]->lengthFt() >= 4000) {
170 _kln89->DrawText("JET 100L", 2, 0, 1);
171 } else {
172 _kln89->DrawText("100L", 2, 0, 1);
173 }
174 }
175 if(_iaps.empty()) {
176 _kln89->DrawText("NO APR", 2, 0, 0);
177 } else {
178 // TODO - output proper differentiation of ILS and NP APR and NP APR type eg GPS(R)
179 _kln89->DrawText("NP APR", 2, 0, 0);
180 }
181 } else if(_subPage == 3) {
182 if(_nRwyPages > 1) {
183 _kln89->DrawChar('+', 1, 3, 0);
184 }
185 unsigned int i = _curRwyPage * 2;
186 string s;
187 if(i < _aptRwys.size()) {
188 // Rwy No.
189 string s = _aptRwys[i]->ident();
190 _kln89->DrawText(s, 2, 9, 3);
191 _kln89->DrawText("/", 2, 12, 3);
192 string recipIdent = _aptRwys[i]->reciprocalRunway()->ident();
193 _kln89->DrawText(recipIdent, 2, 13, 3);
194 // Length
195 s = GPSitoa(int(float(_aptRwys[i]->lengthFt()) * (_kln89->_altUnits == GPS_ALT_UNITS_FT ? 1.0 : SG_FEET_TO_METER) + 0.5));
196 _kln89->DrawText(s, 2, 5 - s.size(), 2);
197 _kln89->DrawText((_kln89->_altUnits == GPS_ALT_UNITS_FT ? "ft" : "m"), 2, 5, 2);
198 // Surface
199 // TODO - why not store these strings as an array?
200 switch(_aptRwys[i]->surface()) {
201 case 1:
202 // Asphalt - fall through
203 case 2:
204 // Concrete
205 _kln89->DrawText("HRD", 2, 9, 2);
206 break;
207 case 3:
208 case 8:
209 // Turf / Turf helipad
210 _kln89->DrawText("TRF", 2, 9, 2);
211 break;
212 case 4:
213 case 9:
214 // Dirt / Dirt helipad
215 _kln89->DrawText("DRT", 2, 9, 2);
216 break;
217 case 5:
218 // Gravel
219 _kln89->DrawText("GRV", 2, 9, 2);
220 break;
221 case 6:
222 // Asphalt helipad - fall through
223 case 7:
224 // Concrete helipad
225 _kln89->DrawText("HRD", 2, 9, 2);
226 break;
227 case 12:
228 // Lakebed
229 _kln89->DrawText("CLY", 2, 9, 2);
230 break;
231 default:
232 // erm? ...
233 _kln89->DrawText("MAT", 2, 9, 2);
234 }
235 }
236 i++;
237 if(i < _aptRwys.size()) {
238 // Rwy No.
239 string s = _aptRwys[i]->ident();
240 _kln89->DrawText(s, 2, 9, 1);
241 _kln89->DrawText("/", 2, 12, 1);
242 string recip = _aptRwys[i]->reciprocalRunway()->ident();
243 _kln89->DrawText(recip, 2, 13, 1);
244 // Length
245 s = GPSitoa(int(float(_aptRwys[i]->lengthFt()) * (_kln89->_altUnits == GPS_ALT_UNITS_FT ? 1.0 : SG_FEET_TO_METER) + 0.5));
246 _kln89->DrawText(s, 2, 5 - s.size(), 0);
247 _kln89->DrawText((_kln89->_altUnits == GPS_ALT_UNITS_FT ? "ft" : "m"), 2, 5, 0);
248 // Surface
249 // TODO - why not store these strings as an array?
250 switch(_aptRwys[i]->surface()) {
251 case 1:
252 // Asphalt - fall through
253 case 2:
254 // Concrete
255 _kln89->DrawText("HRD", 2, 9, 0);
256 break;
257 case 3:
258 case 8:
259 // Turf / Turf helipad
260 _kln89->DrawText("TRF", 2, 9, 0);
261 break;
262 case 4:
263 case 9:
264 // Dirt / Dirt helipad
265 _kln89->DrawText("DRT", 2, 9, 0);
266 break;
267 case 5:
268 // Gravel
269 _kln89->DrawText("GRV", 2, 9, 0);
270 break;
271 case 6:
272 // Asphalt helipad - fall through
273 case 7:
274 // Concrete helipad
275 _kln89->DrawText("HRD", 2, 9, 0);
276 break;
277 case 12:
278 // Lakebed
279 _kln89->DrawText("CLY", 2, 9, 0);
280 break;
281 default:
282 // erm? ...
283 _kln89->DrawText("MAT", 2, 9, 0);
284 }
285 }
286 } else if(_subPage == 4) {
287 if(_nFreqPages > 1) {
288 _kln89->DrawChar('+', 1, 3, 0);
289 }
290 unsigned int i = _curFreqPage * 3;
291 if(i < _aptFreqs.size()) {
292 _kln89->DrawText(_aptFreqs[i].service, 2, 0, 2);
293 _kln89->DrawFreq(_aptFreqs[i].freq, 2, 7, 2);
294 }
295 i++;
296 if(i < _aptFreqs.size()) {
297 _kln89->DrawText(_aptFreqs[i].service, 2, 0, 1);
298 _kln89->DrawFreq(_aptFreqs[i].freq, 2, 7, 1);
299 }
300 i++;
301 if(i < _aptFreqs.size()) {
302 _kln89->DrawText(_aptFreqs[i].service, 2, 0, 0);
303 _kln89->DrawFreq(_aptFreqs[i].freq, 2, 7, 0);
304 }
305 } else if(_subPage == 5) {
306 // TODO - user ought to be allowed to leave persistent remarks
307 _kln89->DrawText("[Remarks]", 2, 2, 2);
308 } else if(_subPage == 6) {
309 // We don't have SID/STAR database yet
310 // TODO
311 _kln89->DrawText("No SID/STAR", 2, 3, 2);
312 _kln89->DrawText("In Data Base", 2, 2, 1);
313 _kln89->DrawText("For This Airport", 2, 0, 0);
314 } else if(_subPage == 7) {
315 if(_iaps.empty()) {
316 _kln89->DrawText("IAP", 2, 11, 3);
317 _kln89->DrawText("No Approach", 2, 3, 2);
318 _kln89->DrawText("In Data Base", 2, 2, 1);
319 _kln89->DrawText("For This Airport", 2, 0, 0);
320 } else {
321 if(_iafDialog) {
322 _kln89->DrawText(_iaps[_curIap]->_ident, 2, 1, 3);
323 _kln89->DrawText(_iaps[_curIap]->_rwyStr, 2, 7, 3);
324 _kln89->DrawText(_iaps[_curIap]->_aptIdent, 2, 12, 3);
325 _kln89->DrawText("IAF", 2, 2, 2);
326 unsigned int line = 0;
327 for(unsigned int i=_iafStart; i<_approachRoutes.size(); ++i) {
328 if(line == 2) {
329 i = _approachRoutes.size() - 1;
330 }
331 // Assume that the IAF number is always single digit!
332 _kln89->DrawText(GPSitoa(i+1), 2, 6, 2-line);
333 if(!(_kln89->_mode == KLN89_MODE_CRSR && _kln89->_blink && _uLinePos == (line + 1))) {
334 _kln89->DrawText(_approachRoutes[i]->waypoints[0]->id, 2, 8, 2-line);
335 }
336 if(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (line + 1) && !(_kln89->_blink )) {
337 _kln89->Underline(2, 8, 2-line, 5);
338 }
339 ++line;
340 }
341 if(_uLinePos > 0 && !(_kln89->_blink)) {
342 _kln89->DrawEnt();
343 }
344 } else if(_addDialog) {
345 _kln89->DrawText(_iaps[_curIap]->_ident, 2, 1, 3);
346 _kln89->DrawText(_iaps[_curIap]->_rwyStr, 2, 7, 3);
347 _kln89->DrawText(_iaps[_curIap]->_aptIdent, 2, 12, 3);
348 string s = GPSitoa(_fStart + 1);
349 _kln89->DrawText(s, 2, 2-s.size(), 2);
350 s = GPSitoa(_kln89->_approachFP->waypoints.size());
351 _kln89->DrawText(s, 2, 2-s.size(), 1);
352 if(!(_uLinePos == _fStart+1 && _kln89->_blink)) {
353 _kln89->DrawText(_kln89->_approachFP->waypoints[_fStart]->id, 2, 4, 2);
354 if(_uLinePos == _fStart+1) _kln89->Underline(2, 4, 2, 6);
355 }
356 if(!(_uLinePos == _maxULinePos-1 && _kln89->_blink)) {
357 _kln89->DrawText(_kln89->_approachFP->waypoints[_kln89->_approachFP->waypoints.size()-1]->id, 2, 4, 1);
358 if(_uLinePos == _maxULinePos-1) _kln89->Underline(2, 4, 1, 6);
359 }
360 if(!(_uLinePos > _kln89->_approachFP->waypoints.size() && _kln89->_blink)) {
361 _kln89->DrawText("ADD TO FPL 0?", 2, 2, 0);
362 if(_uLinePos > _kln89->_approachFP->waypoints.size()) {
363 _kln89->Underline(2, 2, 0, 13);
364 _kln89->DrawEnt();
365 }
366 }
367 } else if(_replaceDialog) {
368 _kln89->DrawText(_iaps[_curIap]->_ident, 2, 1, 3);
369 _kln89->DrawText(_iaps[_curIap]->_rwyStr, 2, 7, 3);
370 _kln89->DrawText(_iaps[_curIap]->_aptIdent, 2, 12, 3);
371 _kln89->DrawText("Replace Existing", 2, 0, 2);
372 _kln89->DrawText("Approach", 2, 4, 1);
373 if(_uLinePos > 0 && !(_kln89->_blink)) {
374 _kln89->DrawText("APPROVE?", 2, 4, 0);
375 _kln89->Underline(2, 4, 0, 8);
376 _kln89->DrawEnt();
377 }
378 } else {
379 _kln89->DrawText("IAP", 2, 11, 3);
380 bool selApp = false;
381 if(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos > 4) {
382 selApp = true;
383 if(!_kln89->_blink) _kln89->DrawEnt();
384 }
385 // _maxULine pos should be 4 + iaps.size() at this point.
386 // Draw a maximum of 3 IAPs.
387 // If there are more than 3 IAPs for this airport, then we need to offset the start
388 // of the list if _uLinePos is pointing at the 4th or later IAP.
389 unsigned int offset = 0;
390 unsigned int index;
391 if(_uLinePos > 7) {
392 offset = _uLinePos - 7;
393 }
394 for(unsigned int i=0; i<3; ++i) {
395 index = offset + i;
396 if(index < _iaps.size()) {
397 string s = GPSitoa(index+1);
398 _kln89->DrawText(s, 2, 2 - s.size(), 2-i);
399 if(!(selApp && _uLinePos == index+5 && _kln89->_blink)) {
400 _kln89->DrawText(_iaps[index]->_ident, 2, 3, 2-i);
401 _kln89->DrawText(_iaps[index]->_rwyStr, 2, 9, 2-i);
402 }
403 if(selApp && _uLinePos == index+5 && !_kln89->_blink) {
404 _kln89->Underline(2, 3, 2-i, 9);
405 }
406 } else {
407 break;
408 }
409 }
410 }
411 }
412 }
413 } else {
414 if(_kln89->_mode != KLN89_MODE_CRSR) _kln89->DrawText(_apt_id, 2, 1, 3);
415 if(_subPage == 0) {
416 /*
417 _kln89->DrawText("----.-", 2, 9, 3);
418 _kln89->DrawText("--------------", 2, 0, 2);
419 _kln89->DrawText("- -- --.--'", 2, 3, 1);
420 _kln89->DrawText("---- --.--'", 2, 3, 0);
421 _kln89->DrawSpecialChar(0, 2, 7, 1);
422 _kln89->DrawSpecialChar(0, 2, 7, 0);
423 */
424 }
425 }
426
427 if(_kln89->_mode == KLN89_MODE_CRSR) {
428 if(!(_subPage == 7 && (_iafDialog || _addDialog || _replaceDialog))) {
429 if(_uLinePos > 0 && _uLinePos < 5) {
430 // TODO - blink as well
431 _kln89->Underline(2, _uLinePos, 3, 1);
432 }
433 for(unsigned int i = 0; i < _apt_id.size(); ++i) {
434 if(_uLinePos != (i + 1)) {
435 _kln89->DrawChar(_apt_id[i], 2, i + 1, 3);
436 } else {
437 if(!_kln89->_blink) _kln89->DrawChar(_apt_id[i], 2, i + 1, 3);
438 }
439 }
440 }
441 }
442
443 _id = _apt_id;
444
445 KLN89Page::Update(dt);
446 }
447
SetId(const string & s)448 void KLN89AptPage::SetId(const string& s) {
449 if(s != _apt_id || s != _last_apt_id) {
450 UpdateAirport(s); // If we don't do this here we break things if s is the same as the current ID since the update wouldn't get called then.
451 /*
452 DCL: Hmmm - I wrote the comment above, but I don't quite understand it!
453 I'm not quite sure why I don't simply set _apt_id here (and NOT _last_apt_id)
454 and let the logic in Update(...) handle the airport details cache update.
455 */
456 }
457 _last_apt_id = _apt_id;
458 _save_apt_id = _apt_id;
459 _apt_id = s;
460 }
461
462 // Update the cached airport details
UpdateAirport(const string & id)463 void KLN89AptPage::UpdateAirport(const string& id) {
464 // Frequencies
465 _aptFreqs.clear();
466
467 const FGAirport* apt = fgFindAirportID(id);
468 if (!apt) {
469 throw sg_exception("UpdateAirport: unknown airport id " + id);
470 }
471
472 for (unsigned int c=0; c<apt->commStations().size(); ++c) {
473 flightgear::CommStation* comm = apt->commStations()[c];
474 AptFreq aq;
475 aq.freq = comm->freqKHz();
476 switch (comm->type()) {
477 case FGPositioned::FREQ_ATIS:
478 aq.service = "ATIS*"; break;
479 case FGPositioned::FREQ_GROUND:
480 aq.service = "GRND*"; break;
481 case FGPositioned::FREQ_TOWER:
482 aq.service = "TWR *"; break;
483 case FGPositioned::FREQ_APP_DEP:
484 aq.service = "APR *"; break;
485 default:
486 continue;
487 }
488 }
489
490 _nFreqPages = (unsigned int)ceil((float(_aptFreqs.size())) / 3.0f);
491
492 // Runways
493 _aptRwys.clear();
494
495 // build local array, longest runway first
496 for (unsigned int r=0; r<apt->numRunways(); ++r) {
497 FGRunway* rwy(apt->getRunwayByIndex(r));
498 if ((r > 0) && (rwy->lengthFt() > _aptRwys.front()->lengthFt())) {
499 _aptRwys.insert(_aptRwys.begin(), rwy);
500 } else {
501 _aptRwys.push_back(rwy);
502 }
503 }
504
505 _nRwyPages = (_aptRwys.size() + 1) / 2; // 2 runways per page.
506 if(_nFreqPages < 1) _nFreqPages = 1;
507 if(_nRwyPages < 1) _nRwyPages = 1;
508
509 // Instrument approaches
510 // Only non-precision for now - TODO - handle precision approaches if necessary
511 _iaps.clear();
512 iap_map_iterator itr = _kln89->_np_iap.find(id);
513 if(itr != _kln89->_np_iap.end()) {
514 _iaps = itr->second;
515 }
516 if(_subPage == 7) {
517 if(_iafDialog || _addDialog || _replaceDialog) {
518 // Eek - major logic error if an airport details cache update occurs
519 // with one of these dialogs active.
520 // TODO - output a warning.
521 //cout << "HELP!!!!!!!!!!\n";
522 } else {
523 _maxULinePos = 4 + _iaps.size(); // We shouldn't need to check the crsr for out-of-bounds here since we only update the airport details when the airport code is changed - ie. _uLinePos <= 4!
524 }
525 }
526 }
527
CrsrPressed()528 void KLN89AptPage::CrsrPressed() {
529 if(_kln89->_mode == KLN89_MODE_DISP) {
530 if(_subPage == 7) {
531 // Pressing crsr jumps back to vanilla IAP page.
532 _iafDialog = false;
533 _addDialog = false;
534 _replaceDialog = false;
535 }
536 return;
537 }
538 if(_kln89->_obsMode) {
539 _uLinePos = 0;
540 } else {
541 _uLinePos = 1;
542 }
543 if(_subPage == 0) {
544 _maxULinePos = 32;
545 } else if(_subPage == 7) {
546 // Don't *think* we need some of this since some of it we can only get to by pressing ENT, not CRSR.
547 if(_iafDialog) {
548 _maxULinePos = _approachRoutes.size();
549 _uLinePos = 1;
550 } else if(_addDialog) {
551 _maxULinePos = 1;
552 _uLinePos = 1;
553 } else if(_replaceDialog) {
554 _maxULinePos = 1;
555 _uLinePos = 1;
556 } else {
557 _maxULinePos = 4 + _iaps.size();
558 if(_iaps.empty()) {
559 _uLinePos = 1;
560 } else {
561 _uLinePos = 5;
562 }
563 }
564 } else {
565 _maxULinePos = 5;
566 }
567 }
568
ClrPressed()569 void KLN89AptPage::ClrPressed() {
570 if(_subPage == 1 && _uLinePos == 5) {
571 _to_flag = !_to_flag;
572 } else if(_subPage == 7) {
573 // Clear backs out IAP selection one step at a time
574 if(_iafDialog) {
575 _iafDialog = false;
576 _maxULinePos = 4 + _iaps.size();
577 if(_iaps.empty()) {
578 _uLinePos = 1;
579 } else {
580 _uLinePos = 5;
581 }
582 } else if(_addDialog) {
583 _addDialog = false;
584 if(_approachRoutes.size() > 1) {
585 _iafDialog = true;
586 _maxULinePos = 1;
587 // Don't reset _curIaf since it is remembed.
588 _uLinePos = 1 + _curIaf; // TODO - make this robust to more than 3 IAF
589 } else {
590 _maxULinePos = 4 + _iaps.size();
591 if(_iaps.empty()) {
592 _uLinePos = 1;
593 } else {
594 _uLinePos = 5;
595 }
596 }
597 } else if(_replaceDialog) {
598 _replaceDialog = false;
599 _addDialog = true;
600 _maxULinePos = 1;
601 _uLinePos = 1;
602 }
603 }
604 }
605
EntPressed()606 void KLN89AptPage::EntPressed() {
607 if(_entInvert) {
608 _entInvert = false;
609 if(_kln89->_dtoReview) {
610 _kln89->DtoInitiate(_apt_id);
611 } else {
612 _last_apt_id = _apt_id;
613 _apt_id = _save_apt_id;
614 }
615 } else if(_subPage == 7 && _kln89->_mode == KLN89_MODE_CRSR && _uLinePos > 0) {
616 // We are selecting an approach
617 if(_iafDialog) {
618 if(_uLinePos > 0) {
619 // Record the IAF that was picked
620 if(_uLinePos == 3) {
621 _curIaf = _approachRoutes.size() - 1;
622 } else {
623 _curIaf = _uLinePos - 1 + _iafStart;
624 }
625 //cout << "_curIaf = " << _curIaf << '\n';
626 // TODO - delete the waypoints inside _approachFP before clearing them!!!!!!!
627 _kln89->_approachFP->waypoints.clear();
628 GPSWaypoint* wp = new GPSWaypoint;
629 *wp = *(_approachRoutes[_curIaf]->waypoints[0]); // Need to make copies here since we're going to alter ID and type sometimes
630 string iafid = wp->id;
631 _kln89->_approachFP->waypoints.push_back(wp);
632 for(unsigned int i=0; i<_IAP.size(); ++i) {
633 if(_IAP[i]->id != iafid) { // Don't duplicate waypoints that are part of the initial fix list and the approach procedure list.
634 // FIXME - allow the same waypoint to be both the IAF and the FAF in some
635 // approaches that have a procedure turn eg. KDLL
636 // Also allow MAF to be the same as IAF!
637 wp = new GPSWaypoint;
638 *wp = *_IAP[i];
639 //cout << "Adding waypoint " << wp->id << ", type is " << wp->appType << '\n';
640 //if(wp->appType == GPS_FAF) wp->id += 'f';
641 //if(wp->appType == GPS_MAP) wp->id += 'm';
642 //cout << "New id = " << wp->id << '\n';
643 _kln89->_approachFP->waypoints.push_back(wp);
644 }
645 }
646 _iafDialog = false;
647 _addDialog = true;
648 _maxULinePos = _kln89->_approachFP->waypoints.size() + 1;
649 _uLinePos = _maxULinePos;
650 }
651 } else if(_addDialog) {
652 if(_uLinePos == _maxULinePos) {
653 _addDialog = false;
654 if(_kln89->ApproachLoaded()) {
655 _replaceDialog = true;
656 _uLinePos = 1;
657 _maxULinePos = 1;
658 } else {
659 // Now load the approach into the active flightplan.
660 // As far as I can tell, the rules are this:
661 // If the airport of the approach is in the flightplan, insert it prior to this. (Not sure what happens if airport has already been passed).
662 // If the airport is not in the flightplan, append the approach to the flightplan, even if it is closer than the current active leg,
663 // in which case reorientate to flightplan might put us on the approach, but unable to activate it.
664 // However, it appears from the sim as if this can indeed happen if the user is not carefull.
665 bool added = false;
666 for(unsigned int i=0; i<_kln89->_activeFP->waypoints.size(); ++i) {
667 if(_kln89->_activeFP->waypoints[i]->id == _apt_id) {
668 _kln89->_activeFP->waypoints.insert(_kln89->_activeFP->waypoints.begin()+i, _kln89->_approachFP->waypoints.begin(), _kln89->_approachFP->waypoints.end());
669 added = true;
670 break;
671 }
672 }
673 if(!added) {
674 _kln89->_activeFP->waypoints.insert(_kln89->_activeFP->waypoints.end(), _kln89->_approachFP->waypoints.begin(), _kln89->_approachFP->waypoints.end());
675 }
676 _kln89->_approachID = _apt_id;
677 _kln89->_approachAbbrev = _iaps[_curIap]->_ident;
678 _kln89->_approachRwyStr = _iaps[_curIap]->_rwyStr;
679 _kln89->_approachLoaded = true;
680 //_kln89->_messageStack.push_back("*Press ALT To Set Baro");
681 // Actually - this message is only sent when we go into appraoch-arm mode.
682 // TODO - check the flightplan for consistency
683 _kln89->OrientateToActiveFlightPlan();
684 _kln89->_mode = KLN89_MODE_DISP;
685 _kln89->_curPage = 7;
686 _kln89->_activePage = _kln89->_pages[7]; // Do we need to clean up here at all before jumping?
687 }
688 }
689 } else if(_replaceDialog) {
690 // TODO - load the approach!
691 } else if(_uLinePos > 4) {
692 _approachRoutes.clear();
693 _IAP.clear();
694 _curIaf = 0;
695 _approachRoutes = ((FGNPIAP*)(_iaps[_uLinePos-5]))->_approachRoutes;
696 _IAP = ((FGNPIAP*)(_iaps[_uLinePos-5]))->_IAP;
697 _curIap = _uLinePos - 5; // TODO - handle the start of list ! no. 1, and the end of list not sequential!
698 _uLinePos = 1;
699 if(_approachRoutes.size() > 1) {
700 // More than 1 IAF - display the selection dialog
701 _iafDialog = true;
702 _maxULinePos = _approachRoutes.size();
703 } else {
704 // There is only 1 IAF, so load the waypoints into the approach flightplan here.
705 // TODO - there is nasty code duplication loading the approach FP between the case here where we have only one
706 // IAF and the case where we must choose the IAF from a list. Try to tidy this after it is all working properly.
707 _kln89->_approachFP->waypoints.clear();
708 GPSWaypoint* wp = new GPSWaypoint;
709 *wp = *(_approachRoutes[0]->waypoints[0]); // Need to make copies here since we're going to alter ID and type sometimes
710 string iafid = wp->id;
711 _kln89->_approachFP->waypoints.push_back(wp);
712 for(unsigned int i=0; i<_IAP.size(); ++i) {
713 if(_IAP[i]->id != iafid) { // Don't duplicate waypoints that are part of the initial fix list and the approach procedure list.
714 // FIXME - allow the same waypoint to be both the IAF and the FAF in some
715 // approaches that have a procedure turn eg. KDLL
716 // Also allow MAF to be the same as IAF!
717 wp = new GPSWaypoint;
718 *wp = *_IAP[i];
719 _kln89->_approachFP->waypoints.push_back(wp);
720 }
721 }
722 _addDialog = true;
723 _maxULinePos = 1;
724 }
725 }
726 }
727 }
728
Knob1Left1()729 void KLN89AptPage::Knob1Left1() {
730 if(_kln89->_mode == KLN89_MODE_CRSR && _subPage == 7 && _addDialog) {
731 if(_uLinePos == _maxULinePos) {
732 _uLinePos--;
733 if(_kln89->_approachFP->waypoints.size() > 1) _fStart = _kln89->_approachFP->waypoints.size() - 2;
734 } else if(_uLinePos == _maxULinePos - 1) {
735 _uLinePos--;
736 } else if(_uLinePos > 0) {
737 if(_fStart == 0) {
738 _uLinePos--;
739 } else {
740 _uLinePos--;
741 _fStart--;
742 }
743 }
744 } else {
745 KLN89Page::Knob1Left1();
746 }
747 }
748
Knob1Right1()749 void KLN89AptPage::Knob1Right1() {
750 if(_kln89->_mode == KLN89_MODE_CRSR && _subPage == 7 && _addDialog) {
751 if(_uLinePos == _maxULinePos) {
752 // no-op
753 } else if(_uLinePos == _maxULinePos - 1) {
754 _uLinePos++;
755 _fStart = 0;
756 } else if(_uLinePos > 0) {
757 if(_fStart >= _kln89->_approachFP->waypoints.size() - 2) {
758 _uLinePos++;
759 } else {
760 _uLinePos++;
761 _fStart++;
762 }
763 } else if(_uLinePos == 0) {
764 _uLinePos++;
765 _fStart = 0;
766 }
767 } else {
768 KLN89Page::Knob1Right1();
769 }
770 }
771
Knob2Left1()772 void KLN89AptPage::Knob2Left1() {
773 if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
774 if(_uLinePos == 0 && _kln89->_mode == KLN89_MODE_CRSR && _kln89->_obsMode) {
775 KLN89Page::Knob2Left1();
776 } else if(_subPage == 5) {
777 _subPage = 4;
778 _curFreqPage = _nFreqPages - 1;
779 } else if(_subPage == 4) {
780 // Freqency pages
781 if(_curFreqPage == 0) {
782 _subPage = 3;
783 _curRwyPage = _nRwyPages - 1;
784 } else {
785 _curFreqPage--;
786 }
787 } else if(_subPage == 3) {
788 if(_curRwyPage == 0) {
789 KLN89Page::Knob2Left1();
790 } else {
791 _curRwyPage--;
792 }
793 } else if(_subPage == 0) {
794 _subPage = 7;
795 // We have to set _uLinePos here even though the cursor isn't pressed, to
796 // ensure that the list displays properly.
797 if(_iaps.empty()) {
798 _uLinePos = 1;
799 } else {
800 _uLinePos = 5;
801 }
802 } else {
803 KLN89Page::Knob2Left1();
804 }
805 } else {
806 if(_uLinePos < 5 && !(_subPage == 7 && (_iafDialog || _addDialog || _replaceDialog))) {
807 // Same logic for all pages - set the ID
808 _apt_id = _apt_id.substr(0, _uLinePos);
809 // ASSERT(_uLinePos > 0);
810 if(_uLinePos == (_apt_id.size() + 1)) {
811 _apt_id += '9';
812 } else {
813 _apt_id[_uLinePos - 1] = _kln89->DecChar(_apt_id[_uLinePos - 1], (_uLinePos == 1 ? false : true));
814 }
815 } else {
816 if(_subPage == 0) {
817 // TODO - set by name
818 } else {
819 // NO-OP - to/fr is cycled by clr button
820 }
821 }
822 }
823 }
824
Knob2Right1()825 void KLN89AptPage::Knob2Right1() {
826 if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
827 if(_uLinePos == 0 && _kln89->_mode == KLN89_MODE_CRSR && _kln89->_obsMode) {
828 KLN89Page::Knob2Right1();
829 } else if(_subPage == 2) {
830 _subPage = 3;
831 _curRwyPage = 0;
832 } else if(_subPage == 3) {
833 if(_curRwyPage == _nRwyPages - 1) {
834 _subPage = 4;
835 _curFreqPage = 0;
836 } else {
837 _curRwyPage++;
838 }
839 } else if(_subPage == 4) {
840 if(_curFreqPage == _nFreqPages - 1) {
841 _subPage = 5;
842 } else {
843 _curFreqPage++;
844 }
845 } else if(_subPage == 6) {
846 _subPage = 7;
847 // We have to set _uLinePos here even though the cursor isn't pressed, to
848 // ensure that the list displays properly.
849 if(_iaps.empty()) {
850 _uLinePos = 1;
851 } else {
852 _uLinePos = 5;
853 }
854 } else {
855 KLN89Page::Knob2Right1();
856 }
857 } else {
858 if(_uLinePos < 5 && !(_subPage == 7 && (_iafDialog || _addDialog || _replaceDialog))) {
859 // Same logic for all pages - set the ID
860 _apt_id = _apt_id.substr(0, _uLinePos);
861 // ASSERT(_uLinePos > 0);
862 if(_uLinePos == (_apt_id.size() + 1)) {
863 _apt_id += 'A';
864 } else {
865 _apt_id[_uLinePos - 1] = _kln89->IncChar(_apt_id[_uLinePos - 1], (_uLinePos == 1 ? false : true));
866 }
867 } else {
868 if(_subPage == 0) {
869 // TODO - set by name
870 } else {
871 // NO-OP - to/fr is cycled by clr button
872 }
873 }
874 }
875 }
876