Debian Codegraph @

src/Instrumentation/KLN89/kln89_page_apt.cxx

raw symbols SCIP
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
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
65 KLN89AptPage::~KLN89AptPage() {
66 }
67
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[12];
153 snprintf(buf, sizeof(buf), "%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, sizeof(buf), "%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]->isHardSurface()) {
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 case 20: // Light colored asphalt
203 case 21:
204 case 22:
205 case 23:
206 case 24: // Asphalt
207 case 25:
208 case 26:
209 case 27: // Darker colored asphalt
210 case 28:
211 case 29:
212 case 30:
213 case 31: // Very dark colored asphalt
214 case 32:
215 case 33:
216 case 34:
217 case 35: // Near black, ‘new’ looking asphalt
218 case 36:
219 case 37:
220 case 38:
221 case 2: //Concrete
222 case 50: // Light “new” looking concrete
223 case 51:
224 case 52:
225 case 53: // Concrete
226 case 54:
227 case 55: // Dark concrete
228 case 56:
229 case 57:
230 _kln89->DrawText("HRD", 2, 9, 2);
231 break;
232 case 3:
233 case 8:
234 // Turf / Turf helipad
235 _kln89->DrawText("TRF", 2, 9, 2);
236 break;
237 case 4:
238 case 9:
239 // Dirt / Dirt helipad
240 _kln89->DrawText("DRT", 2, 9, 2);
241 break;
242 case 5:
243 // Gravel
244 _kln89->DrawText("GRV", 2, 9, 2);
245 break;
246 case 6:
247 // Asphalt helipad - fall through
248 case 7:
249 // Concrete helipad
250 _kln89->DrawText("HRD", 2, 9, 2);
251 break;
252 case 12:
253 // Lakebed
254 _kln89->DrawText("CLY", 2, 9, 2);
255 break;
256 default:
257 // erm? ...
258 _kln89->DrawText("MAT", 2, 9, 2);
259 }
260 }
261 i++;
262 if(i < _aptRwys.size()) {
263 // Rwy No.
264 string s = _aptRwys[i]->ident();
265 _kln89->DrawText(s, 2, 9, 1);
266 _kln89->DrawText("/", 2, 12, 1);
267 string recip = _aptRwys[i]->reciprocalRunway()->ident();
268 _kln89->DrawText(recip, 2, 13, 1);
269 // Length
270 s = GPSitoa(int(float(_aptRwys[i]->lengthFt()) * (_kln89->_altUnits == GPS_ALT_UNITS_FT ? 1.0 : SG_FEET_TO_METER) + 0.5));
271 _kln89->DrawText(s, 2, 5 - s.size(), 0);
272 _kln89->DrawText((_kln89->_altUnits == GPS_ALT_UNITS_FT ? "ft" : "m"), 2, 5, 0);
273 // Surface
274 // TODO: why not store these strings as an array?
275 switch(_aptRwys[i]->surface()) {
276 case 1:
277 case 20: // Light colored asphalt
278 case 21:
279 case 22:
280 case 23:
281 case 24: // Asphalt
282 case 25:
283 case 26:
284 case 27: // Darker colored asphalt
285 case 28:
286 case 29:
287 case 30:
288 case 31: // Very dark colored asphalt
289 case 32:
290 case 33:
291 case 34:
292 case 35: // Near black, ‘new’ looking asphalt
293 case 36:
294 case 37:
295 case 38:
296 case 2: //Concrete
297 case 50: // Light “new” looking concrete
298 case 51:
299 case 52:
300 case 53: // Concrete
301 case 54:
302 case 55: // Dark concrete
303 case 56:
304 case 57:
305 _kln89->DrawText("HRD", 2, 9, 0);
306 break;
307 case 3:
308 case 8:
309 // Turf / Turf helipad
310 _kln89->DrawText("TRF", 2, 9, 0);
311 break;
312 case 4:
313 case 9:
314 // Dirt / Dirt helipad
315 _kln89->DrawText("DRT", 2, 9, 0);
316 break;
317 case 5:
318 // Gravel
319 _kln89->DrawText("GRV", 2, 9, 0);
320 break;
321 case 6:
322 // Asphalt helipad - fall through
323 case 7:
324 // Concrete helipad
325 _kln89->DrawText("HRD", 2, 9, 0);
326 break;
327 case 12:
328 // Lakebed
329 _kln89->DrawText("CLY", 2, 9, 0);
330 break;
331 default:
332 // erm? ...
333 _kln89->DrawText("MAT", 2, 9, 0);
334 }
335 }
336 } else if(_subPage == 4) {
337 if(_nFreqPages > 1) {
338 _kln89->DrawChar('+', 1, 3, 0);
339 }
340 unsigned int i = _curFreqPage * 3;
341 if(i < _aptFreqs.size()) {
342 _kln89->DrawText(_aptFreqs[i].service, 2, 0, 2);
343 _kln89->DrawFreq(_aptFreqs[i].freq, 2, 7, 2);
344 }
345 i++;
346 if(i < _aptFreqs.size()) {
347 _kln89->DrawText(_aptFreqs[i].service, 2, 0, 1);
348 _kln89->DrawFreq(_aptFreqs[i].freq, 2, 7, 1);
349 }
350 i++;
351 if(i < _aptFreqs.size()) {
352 _kln89->DrawText(_aptFreqs[i].service, 2, 0, 0);
353 _kln89->DrawFreq(_aptFreqs[i].freq, 2, 7, 0);
354 }
355 } else if(_subPage == 5) {
356 // TODO: user ought to be allowed to leave persistent remarks
357 _kln89->DrawText("[Remarks]", 2, 2, 2);
358 } else if(_subPage == 6) {
359 // We don't have SID/STAR database yet
360 // TODO
361 _kln89->DrawText("No SID/STAR", 2, 3, 2);
362 _kln89->DrawText("In Data Base", 2, 2, 1);
363 _kln89->DrawText("For This Airport", 2, 0, 0);
364 } else if(_subPage == 7) {
365 if(_iaps.empty()) {
366 _kln89->DrawText("IAP", 2, 11, 3);
367 _kln89->DrawText("No Approach", 2, 3, 2);
368 _kln89->DrawText("In Data Base", 2, 2, 1);
369 _kln89->DrawText("For This Airport", 2, 0, 0);
370 } else {
371 if(_iafDialog) {
372 _kln89->DrawText(_iaps[_curIap]->_ident, 2, 1, 3);
373 _kln89->DrawText(_iaps[_curIap]->_rwyStr, 2, 7, 3);
374 _kln89->DrawText(_iaps[_curIap]->_aptIdent, 2, 12, 3);
375 _kln89->DrawText("IAF", 2, 2, 2);
376 unsigned int line = 0;
377 for(unsigned int i=_iafStart; i<_approachRoutes.size(); ++i) {
378 if(line == 2) {
379 i = _approachRoutes.size() - 1;
380 }
381 // Assume that the IAF number is always single digit!
382 _kln89->DrawText(GPSitoa(i+1), 2, 6, 2-line);
383 if(!(_kln89->_mode == KLN89_MODE_CRSR && _kln89->_blink && _uLinePos == (line + 1))) {
384 _kln89->DrawText(_approachRoutes[i]->waypoints[0]->id, 2, 8, 2-line);
385 }
386 if(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (line + 1) && !(_kln89->_blink )) {
387 _kln89->Underline(2, 8, 2-line, 5);
388 }
389 ++line;
390 }
391 if(_uLinePos > 0 && !(_kln89->_blink)) {
392 _kln89->DrawEnt();
393 }
394 } else if(_addDialog) {
395 _kln89->DrawText(_iaps[_curIap]->_ident, 2, 1, 3);
396 _kln89->DrawText(_iaps[_curIap]->_rwyStr, 2, 7, 3);
397 _kln89->DrawText(_iaps[_curIap]->_aptIdent, 2, 12, 3);
398 string s = GPSitoa(_fStart + 1);
399 _kln89->DrawText(s, 2, 2-s.size(), 2);
400 s = GPSitoa(_kln89->_approachFP->waypoints.size());
401 _kln89->DrawText(s, 2, 2-s.size(), 1);
402 if(!(_uLinePos == _fStart+1 && _kln89->_blink)) {
403 _kln89->DrawText(_kln89->_approachFP->waypoints[_fStart]->id, 2, 4, 2);
404 if(_uLinePos == _fStart+1) _kln89->Underline(2, 4, 2, 6);
405 }
406 if(!(_uLinePos == _maxULinePos-1 && _kln89->_blink)) {
407 _kln89->DrawText(_kln89->_approachFP->waypoints[_kln89->_approachFP->waypoints.size()-1]->id, 2, 4, 1);
408 if(_uLinePos == _maxULinePos-1) _kln89->Underline(2, 4, 1, 6);
409 }
410 if(!(_uLinePos > _kln89->_approachFP->waypoints.size() && _kln89->_blink)) {
411 _kln89->DrawText("ADD TO FPL 0?", 2, 2, 0);
412 if(_uLinePos > _kln89->_approachFP->waypoints.size()) {
413 _kln89->Underline(2, 2, 0, 13);
414 _kln89->DrawEnt();
415 }
416 }
417 } else if(_replaceDialog) {
418 _kln89->DrawText(_iaps[_curIap]->_ident, 2, 1, 3);
419 _kln89->DrawText(_iaps[_curIap]->_rwyStr, 2, 7, 3);
420 _kln89->DrawText(_iaps[_curIap]->_aptIdent, 2, 12, 3);
421 _kln89->DrawText("Replace Existing", 2, 0, 2);
422 _kln89->DrawText("Approach", 2, 4, 1);
423 if(_uLinePos > 0 && !(_kln89->_blink)) {
424 _kln89->DrawText("APPROVE?", 2, 4, 0);
425 _kln89->Underline(2, 4, 0, 8);
426 _kln89->DrawEnt();
427 }
428 } else {
429 _kln89->DrawText("IAP", 2, 11, 3);
430 bool selApp = false;
431 if(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos > 4) {
432 selApp = true;
433 if(!_kln89->_blink) _kln89->DrawEnt();
434 }
435 // _maxULine pos should be 4 + iaps.size() at this point.
436 // Draw a maximum of 3 IAPs.
437 // If there are more than 3 IAPs for this airport, then we need to offset the start
438 // of the list if _uLinePos is pointing at the 4th or later IAP.
439 unsigned int offset = 0;
440 unsigned int index;
441 if(_uLinePos > 7) {
442 offset = _uLinePos - 7;
443 }
444 for(unsigned int i=0; i<3; ++i) {
445 index = offset + i;
446 if(index < _iaps.size()) {
447 string s = GPSitoa(index+1);
448 _kln89->DrawText(s, 2, 2 - s.size(), 2-i);
449 if(!(selApp && _uLinePos == index+5 && _kln89->_blink)) {
450 _kln89->DrawText(_iaps[index]->_ident, 2, 3, 2-i);
451 _kln89->DrawText(_iaps[index]->_rwyStr, 2, 9, 2-i);
452 }
453 if(selApp && _uLinePos == index+5 && !_kln89->_blink) {
454 _kln89->Underline(2, 3, 2-i, 9);
455 }
456 } else {
457 break;
458 }
459 }
460 }
461 }
462 }
463 } else {
464 if(_kln89->_mode != KLN89_MODE_CRSR) _kln89->DrawText(_apt_id, 2, 1, 3);
465 if(_subPage == 0) {
466 /*
467 _kln89->DrawText("----.-", 2, 9, 3);
468 _kln89->DrawText("--------------", 2, 0, 2);
469 _kln89->DrawText("- -- --.--'", 2, 3, 1);
470 _kln89->DrawText("---- --.--'", 2, 3, 0);
471 _kln89->DrawSpecialChar(0, 2, 7, 1);
472 _kln89->DrawSpecialChar(0, 2, 7, 0);
473 */
474 }
475 }
476
477 if(_kln89->_mode == KLN89_MODE_CRSR) {
478 if(!(_subPage == 7 && (_iafDialog || _addDialog || _replaceDialog))) {
479 if(_uLinePos > 0 && _uLinePos < 5) {
480 // TODO: blink as well
481 _kln89->Underline(2, _uLinePos, 3, 1);
482 }
483 for(unsigned int i = 0; i < _apt_id.size(); ++i) {
484 if(_uLinePos != (i + 1)) {
485 _kln89->DrawChar(_apt_id[i], 2, i + 1, 3);
486 } else {
487 if(!_kln89->_blink) _kln89->DrawChar(_apt_id[i], 2, i + 1, 3);
488 }
489 }
490 }
491 }
492
493 _id = _apt_id;
494
495 KLN89Page::Update(dt);
496 }
497
498 void KLN89AptPage::SetId(const string& s) {
499 if(s != _apt_id || s != _last_apt_id) {
500 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.
501 /*
502 DCL: Hmmm - I wrote the comment above, but I don't quite understand it!
503 I'm not quite sure why I don't simply set _apt_id here (and NOT _last_apt_id)
504 and let the logic in Update(...) handle the airport details cache update.
505 */
506 }
507 _last_apt_id = _apt_id;
508 _save_apt_id = _apt_id;
509 _apt_id = s;
510 }
511
512 // Update the cached airport details
513 void KLN89AptPage::UpdateAirport(const string& id) {
514 // Frequencies
515 _aptFreqs.clear();
516
517 const FGAirport* apt = fgFindAirportID(id);
518 if (!apt) {
519 throw sg_exception("UpdateAirport: unknown airport id " + id);
520 }
521
522 for (unsigned int c=0; c<apt->commStations().size(); ++c) {
523 flightgear::CommStation* comm = apt->commStations()[c];
524 AptFreq aq;
525 aq.freq = comm->freqKHz();
526 switch (comm->type()) {
527 case FGPositioned::FREQ_ATIS:
528 aq.service = "ATIS*"; break;
529 case FGPositioned::FREQ_GROUND:
530 aq.service = "GRND*"; break;
531 case FGPositioned::FREQ_TOWER:
532 aq.service = "TWR *"; break;
533 case FGPositioned::FREQ_APP_DEP:
534 aq.service = "APR *"; break;
535 default:
536 continue;
537 }
538 }
539
540 _nFreqPages = (unsigned int)ceil((float(_aptFreqs.size())) / 3.0f);
541
542 // Runways
543 _aptRwys.clear();
544
545 // build local array, longest runway first
546 for (unsigned int r=0; r<apt->numRunways(); ++r) {
547 FGRunway* rwy(apt->getRunwayByIndex(r));
548 if ((r > 0) && (rwy->lengthFt() > _aptRwys.front()->lengthFt())) {
549 _aptRwys.insert(_aptRwys.begin(), rwy);
550 } else {
551 _aptRwys.push_back(rwy);
552 }
553 }
554
555 _nRwyPages = (_aptRwys.size() + 1) / 2; // 2 runways per page.
556 if(_nFreqPages < 1) _nFreqPages = 1;
557 if(_nRwyPages < 1) _nRwyPages = 1;
558
559 // Instrument approaches
560 // Only non-precision for now - TODO: handle precision approaches if necessary
561 _iaps.clear();
562 iap_map_iterator itr = _kln89->_np_iap.find(id);
563 if(itr != _kln89->_np_iap.end()) {
564 _iaps = itr->second;
565 }
566 if(_subPage == 7) {
567 if(_iafDialog || _addDialog || _replaceDialog) {
568 // Eek - major logic error if an airport details cache update occurs
569 // with one of these dialogs active.
570 // TODO: output a warning.
571 //cout << "HELP!!!!!!!!!!\n";
572 } else {
573 _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!
574 }
575 }
576 }
577
578 void KLN89AptPage::CrsrPressed() {
579 if(_kln89->_mode == KLN89_MODE_DISP) {
580 if(_subPage == 7) {
581 // Pressing crsr jumps back to vanilla IAP page.
582 _iafDialog = false;
583 _addDialog = false;
584 _replaceDialog = false;
585 }
586 return;
587 }
588 if(_kln89->_obsMode) {
589 _uLinePos = 0;
590 } else {
591 _uLinePos = 1;
592 }
593 if(_subPage == 0) {
594 _maxULinePos = 32;
595 } else if(_subPage == 7) {
596 // Don't *think* we need some of this since some of it we can only get to by pressing ENT, not CRSR.
597 if(_iafDialog) {
598 _maxULinePos = _approachRoutes.size();
599 _uLinePos = 1;
600 } else if(_addDialog) {
601 _maxULinePos = 1;
602 _uLinePos = 1;
603 } else if(_replaceDialog) {
604 _maxULinePos = 1;
605 _uLinePos = 1;
606 } else {
607 _maxULinePos = 4 + _iaps.size();
608 if(_iaps.empty()) {
609 _uLinePos = 1;
610 } else {
611 _uLinePos = 5;
612 }
613 }
614 } else {
615 _maxULinePos = 5;
616 }
617 }
618
619 void KLN89AptPage::ClrPressed() {
620 if(_subPage == 1 && _uLinePos == 5) {
621 _to_flag = !_to_flag;
622 } else if(_subPage == 7) {
623 // Clear backs out IAP selection one step at a time
624 if(_iafDialog) {
625 _iafDialog = false;
626 _maxULinePos = 4 + _iaps.size();
627 if(_iaps.empty()) {
628 _uLinePos = 1;
629 } else {
630 _uLinePos = 5;
631 }
632 } else if(_addDialog) {
633 _addDialog = false;
634 if(_approachRoutes.size() > 1) {
635 _iafDialog = true;
636 _maxULinePos = 1;
637 // Don't reset _curIaf since it is remembed.
638 _uLinePos = 1 + _curIaf; // TODO: make this robust to more than 3 IAF
639 } else {
640 _maxULinePos = 4 + _iaps.size();
641 if(_iaps.empty()) {
642 _uLinePos = 1;
643 } else {
644 _uLinePos = 5;
645 }
646 }
647 } else if(_replaceDialog) {
648 _replaceDialog = false;
649 _addDialog = true;
650 _maxULinePos = 1;
651 _uLinePos = 1;
652 }
653 }
654 }
655
656 void KLN89AptPage::EntPressed() {
657 if(_entInvert) {
658 _entInvert = false;
659 if(_kln89->_dtoReview) {
660 _kln89->DtoInitiate(_apt_id);
661 } else {
662 _last_apt_id = _apt_id;
663 _apt_id = _save_apt_id;
664 }
665 } else if(_subPage == 7 && _kln89->_mode == KLN89_MODE_CRSR && _uLinePos > 0) {
666 // We are selecting an approach
667 if(_iafDialog) {
668 if(_uLinePos > 0) {
669 // Record the IAF that was picked
670 if(_uLinePos == 3) {
671 _curIaf = _approachRoutes.size() - 1;
672 } else {
673 _curIaf = _uLinePos - 1 + _iafStart;
674 }
675 //cout << "_curIaf = " << _curIaf << '\n';
676 // TODO: delete the waypoints inside _approachFP before clearing them!!!!!!!
677 _kln89->_approachFP->waypoints.clear();
678 GPSWaypoint* wp = new GPSWaypoint;
679 *wp = *(_approachRoutes[_curIaf]->waypoints[0]); // Need to make copies here since we're going to alter ID and type sometimes
680 string iafid = wp->id;
681 _kln89->_approachFP->waypoints.push_back(wp);
682 for(unsigned int i=0; i<_IAP.size(); ++i) {
683 if(_IAP[i]->id != iafid) { // Don't duplicate waypoints that are part of the initial fix list and the approach procedure list.
684 // FIXME - allow the same waypoint to be both the IAF and the FAF in some
685 // approaches that have a procedure turn eg. KDLL
686 // Also allow MAF to be the same as IAF!
687 wp = new GPSWaypoint;
688 *wp = *_IAP[i];
689 //cout << "Adding waypoint " << wp->id << ", type is " << wp->appType << '\n';
690 //if(wp->appType == GPS_FAF) wp->id += 'f';
691 //if(wp->appType == GPS_MAP) wp->id += 'm';
692 //cout << "New id = " << wp->id << '\n';
693 _kln89->_approachFP->waypoints.push_back(wp);
694 }
695 }
696 _iafDialog = false;
697 _addDialog = true;
698 _maxULinePos = _kln89->_approachFP->waypoints.size() + 1;
699 _uLinePos = _maxULinePos;
700 }
701 } else if(_addDialog) {
702 if(_uLinePos == _maxULinePos) {
703 _addDialog = false;
704 if(_kln89->ApproachLoaded()) {
705 _replaceDialog = true;
706 _uLinePos = 1;
707 _maxULinePos = 1;
708 } else {
709 // Now load the approach into the active flightplan.
710 // As far as I can tell, the rules are this:
711 // 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).
712 // If the airport is not in the flightplan, append the approach to the flightplan, even if it is closer than the current active leg,
713 // in which case reorientate to flightplan might put us on the approach, but unable to activate it.
714 // However, it appears from the sim as if this can indeed happen if the user is not carefull.
715 bool added = false;
716 for(unsigned int i=0; i<_kln89->_activeFP->waypoints.size(); ++i) {
717 if(_kln89->_activeFP->waypoints[i]->id == _apt_id) {
718 _kln89->_activeFP->waypoints.insert(_kln89->_activeFP->waypoints.begin()+i, _kln89->_approachFP->waypoints.begin(), _kln89->_approachFP->waypoints.end());
719 added = true;
720 break;
721 }
722 }
723 if(!added) {
724 _kln89->_activeFP->waypoints.insert(_kln89->_activeFP->waypoints.end(), _kln89->_approachFP->waypoints.begin(), _kln89->_approachFP->waypoints.end());
725 }
726 _kln89->_approachID = _apt_id;
727 _kln89->_approachAbbrev = _iaps[_curIap]->_ident;
728 _kln89->_approachRwyStr = _iaps[_curIap]->_rwyStr;
729 _kln89->_approachLoaded = true;
730 //_kln89->_messageStack.push_back("*Press ALT To Set Baro");
731 // Actually - this message is only sent when we go into appraoch-arm mode.
732 // TODO: check the flightplan for consistency
733 _kln89->OrientateToActiveFlightPlan();
734 _kln89->_mode = KLN89_MODE_DISP;
735 _kln89->_curPage = 7;
736 _kln89->_activePage = _kln89->_pages[7]; // Do we need to clean up here at all before jumping?
737 }
738 }
739 } else if(_replaceDialog) {
740 // TODO: load the approach!
741 } else if(_uLinePos > 4) {
742 _approachRoutes.clear();
743 _IAP.clear();
744 _curIaf = 0;
745 _approachRoutes = ((FGNPIAP*)(_iaps[_uLinePos-5]))->_approachRoutes;
746 _IAP = ((FGNPIAP*)(_iaps[_uLinePos-5]))->_IAP;
747 _curIap = _uLinePos - 5; // TODO: handle the start of list ! no. 1, and the end of list not sequential!
748 _uLinePos = 1;
749 if(_approachRoutes.size() > 1) {
750 // More than 1 IAF - display the selection dialog
751 _iafDialog = true;
752 _maxULinePos = _approachRoutes.size();
753 } else {
754 // There is only 1 IAF, so load the waypoints into the approach flightplan here.
755 // TODO: there is nasty code duplication loading the approach FP between the case here where we have only one
756 // IAF and the case where we must choose the IAF from a list. Try to tidy this after it is all working properly.
757 _kln89->_approachFP->waypoints.clear();
758 GPSWaypoint* wp = new GPSWaypoint;
759 *wp = *(_approachRoutes[0]->waypoints[0]); // Need to make copies here since we're going to alter ID and type sometimes
760 string iafid = wp->id;
761 _kln89->_approachFP->waypoints.push_back(wp);
762 for(unsigned int i=0; i<_IAP.size(); ++i) {
763 if(_IAP[i]->id != iafid) { // Don't duplicate waypoints that are part of the initial fix list and the approach procedure list.
764 // FIXME - allow the same waypoint to be both the IAF and the FAF in some
765 // approaches that have a procedure turn eg. KDLL
766 // Also allow MAF to be the same as IAF!
767 wp = new GPSWaypoint;
768 *wp = *_IAP[i];
769 _kln89->_approachFP->waypoints.push_back(wp);
770 }
771 }
772 _addDialog = true;
773 _maxULinePos = 1;
774 }
775 }
776 }
777 }
778
779 void KLN89AptPage::Knob1Left1() {
780 if(_kln89->_mode == KLN89_MODE_CRSR && _subPage == 7 && _addDialog) {
781 if(_uLinePos == _maxULinePos) {
782 _uLinePos--;
783 if(_kln89->_approachFP->waypoints.size() > 1) _fStart = _kln89->_approachFP->waypoints.size() - 2;
784 } else if(_uLinePos == _maxULinePos - 1) {
785 _uLinePos--;
786 } else if(_uLinePos > 0) {
787 if(_fStart == 0) {
788 _uLinePos--;
789 } else {
790 _uLinePos--;
791 _fStart--;
792 }
793 }
794 } else {
795 KLN89Page::Knob1Left1();
796 }
797 }
798
799 void KLN89AptPage::Knob1Right1() {
800 if(_kln89->_mode == KLN89_MODE_CRSR && _subPage == 7 && _addDialog) {
801 if(_uLinePos == _maxULinePos) {
802 // no-op
803 } else if(_uLinePos == _maxULinePos - 1) {
804 _uLinePos++;
805 _fStart = 0;
806 } else if(_uLinePos > 0) {
807 if(_fStart >= _kln89->_approachFP->waypoints.size() - 2) {
808 _uLinePos++;
809 } else {
810 _uLinePos++;
811 _fStart++;
812 }
813 } else if(_uLinePos == 0) {
814 _uLinePos++;
815 _fStart = 0;
816 }
817 } else {
818 KLN89Page::Knob1Right1();
819 }
820 }
821
822 void KLN89AptPage::Knob2Left1() {
823 if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
824 if(_uLinePos == 0 && _kln89->_mode == KLN89_MODE_CRSR && _kln89->_obsMode) {
825 KLN89Page::Knob2Left1();
826 } else if(_subPage == 5) {
827 _subPage = 4;
828 _curFreqPage = _nFreqPages - 1;
829 } else if(_subPage == 4) {
830 // Freqency pages
831 if(_curFreqPage == 0) {
832 _subPage = 3;
833 _curRwyPage = _nRwyPages - 1;
834 } else {
835 _curFreqPage--;
836 }
837 } else if(_subPage == 3) {
838 if(_curRwyPage == 0) {
839 KLN89Page::Knob2Left1();
840 } else {
841 _curRwyPage--;
842 }
843 } else if(_subPage == 0) {
844 _subPage = 7;
845 // We have to set _uLinePos here even though the cursor isn't pressed, to
846 // ensure that the list displays properly.
847 if(_iaps.empty()) {
848 _uLinePos = 1;
849 } else {
850 _uLinePos = 5;
851 }
852 } else {
853 KLN89Page::Knob2Left1();
854 }
855 } else {
856 if(_uLinePos < 5 && !(_subPage == 7 && (_iafDialog || _addDialog || _replaceDialog))) {
857 // Same logic for all pages - set the ID
858 _apt_id = _apt_id.substr(0, _uLinePos);
859 // ASSERT(_uLinePos > 0);
860 if(_uLinePos == (_apt_id.size() + 1)) {
861 _apt_id += '9';
862 } else {
863 _apt_id[_uLinePos - 1] = _kln89->DecChar(_apt_id[_uLinePos - 1], (_uLinePos == 1 ? false : true));
864 }
865 } else {
866 if(_subPage == 0) {
867 // TODO: set by name
868 } else {
869 // NO-OP - to/fr is cycled by clr button
870 }
871 }
872 }
873 }
874
875 void KLN89AptPage::Knob2Right1() {
876 if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
877 if(_uLinePos == 0 && _kln89->_mode == KLN89_MODE_CRSR && _kln89->_obsMode) {
878 KLN89Page::Knob2Right1();
879 } else if(_subPage == 2) {
880 _subPage = 3;
881 _curRwyPage = 0;
882 } else if(_subPage == 3) {
883 if(_curRwyPage == _nRwyPages - 1) {
884 _subPage = 4;
885 _curFreqPage = 0;
886 } else {
887 _curRwyPage++;
888 }
889 } else if(_subPage == 4) {
890 if(_curFreqPage == _nFreqPages - 1) {
891 _subPage = 5;
892 } else {
893 _curFreqPage++;
894 }
895 } else if(_subPage == 6) {
896 _subPage = 7;
897 // We have to set _uLinePos here even though the cursor isn't pressed, to
898 // ensure that the list displays properly.
899 if(_iaps.empty()) {
900 _uLinePos = 1;
901 } else {
902 _uLinePos = 5;
903 }
904 } else {
905 KLN89Page::Knob2Right1();
906 }
907 } else {
908 if(_uLinePos < 5 && !(_subPage == 7 && (_iafDialog || _addDialog || _replaceDialog))) {
909 // Same logic for all pages - set the ID
910 _apt_id = _apt_id.substr(0, _uLinePos);
911 // ASSERT(_uLinePos > 0);
912 if(_uLinePos == (_apt_id.size() + 1)) {
913 _apt_id += 'A';
914 } else {
915 _apt_id[_uLinePos - 1] = _kln89->IncChar(_apt_id[_uLinePos - 1], (_uLinePos == 1 ? false : true));
916 }
917 } else {
918 if(_subPage == 0) {
919 // TODO: set by name
920 } else {
921 // NO-OP - to/fr is cycled by clr button
922 }
923 }
924 }
925 }