1 // kln89_page_*.[ch]xx - this file is one of the "pages" that
2 // are used in the KLN89 GPS unit simulation.
4 // Written by David Luff, started 2005.
6 // Copyright (C) 2005 - David C Luff - david.luff@nottingham.ac.uk
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.
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.
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.
28 #include "kln89_page_fpl.hxx"
35 KLN89FplPage::KLN89FplPage(KLN89* parent)
55 KLN89FplPage::~KLN89FplPage() {
58 void KLN89FplPage::Update(double dt) {
61 // NOTE - we need to draw the active leg arrow outside of this block to avoid the _delFP check.
62 // TODO - we really ought to merge the page 0 and other pages drawing code with a couple of lines of extra logic.
63 if(_subPage == 0 && !_delFP) { // Note that in the _delFP case, the active flightplan gets a header, and hence the same geometry as the other fps, so we draw it there.
65 // NOTE THAT FOR THE ACTIVE FLIGHT PLAN, TOP POSITION IS STILL 4 in the underline position scheme, to make
66 // copy and paste easier!!!!
68 // ---------------------------------- Copy the active FlightPlan and insert approach header and fence if required ---------------
69 // For synatical convienience
70 //vector<GPSWaypoint*> waylist = _kln89->_flightPlans[_subPage]->waypoints;
71 // Copy every waypoint for now.
72 // This is inefficient, but allows us to insert dummy waypoints to represent the header and fence
73 // in our *local copy* of the flightplan, if an approach is loaded. There must be a better way to do this!!!!
74 //cout << "AA" << endl;
75 vector<GPSWaypoint> waylist;
76 for(unsigned int i=0; i<_kln89->_flightPlans[_subPage]->waypoints.size(); ++i) {
77 waylist.push_back(*_kln89->_flightPlans[_subPage]->waypoints[i]);
79 //cout << "BB" << endl;
82 if(_kln89->_approachLoaded) {
85 wp.type = GPS_WP_VIRT;
87 //cout << "CC" << endl;
88 for(unsigned int i=0; i<waylist.size(); ++i) {
89 // Insert the hdr immediately before the IAF
90 if(waylist[i].appType == GPS_IAF) {
91 waylist.insert(waylist.begin()+i, wp);
92 // Need to insert empty string into the params to keep them in sync
93 _params.insert(_params.begin()+i-1, "");
98 //cout << "DD" << endl;
100 wp.type = GPS_WP_VIRT;
101 wp.appType = GPS_FENCE;
102 for(unsigned int i=0; i<waylist.size(); ++i) {
103 // Insert the fence between the MAF and the MAP
104 if(waylist[i].appType == GPS_MAHP) {
105 waylist.insert(waylist.begin()+i, wp);
106 // Need to insert empty string into the params to keep them in sync
107 _params.insert(_params.begin()+i-1, "");
114 // Now make up a vector of waypoint numbers, since they aren't aligned with list position anymore
118 for(unsigned int i=0; i<waylist.size(); ++i) {
119 if(waylist[i].appType != GPS_HDR && waylist[i].appType != GPS_FENCE) {
120 numlist.push_back(num);
123 numlist.push_back(-1);
128 for(unsigned int i=0; i<waylist.size(); ++i) {
129 //cout << i + 1 - hfcount << ": ID= " << waylist[i].id;
130 if(waylist[i].appType == GPS_HDR) {
134 if(waylist[i].appType == GPS_FENCE) {
140 //----------------------------------------- end active FP copy ------------------------------------------------
142 // Recalculate which waypoint is displayed at the top of the list if required (generally if this page has lost focus).
143 int idx = _parent->GetActiveWaypointIndex();
145 if(waylist.size() <= 1) {
147 } else if(waylist.size() <= 4) {
150 // Make the active waypoint the second WP displayed
156 //cout << "HeaderPos = " << _hdrPos << ", fencePos = " << _fencePos << ", _fplPos = " << _fplPos << ", active waypoint index = " << _parent->GetActiveWaypointIndex() << '\n';
157 if(_hdrPos >= 0 && idx >= _hdrPos) {
159 if(_fencePos >= 0 && (idx + 1) >= _fencePos) {
163 _resetFplPos0 = false;
166 // Increment the active waypoint position if required due hdr and fence here not above so it gets called every frame
167 if(_hdrPos >= 0 && idx >= _hdrPos) {
169 if(_fencePos >= 0 && idx >= _fencePos) {
174 // Draw the leg arrow etc
175 int diff = idx - (int)_fplPos;
178 // No active waypoint
179 } else if(diff < 0) {
180 // Off screen to the top
181 } else if(diff > 2) {
186 // Only the head is blinked during waypoint alerting
187 if(!(_kln89->_waypointAlert && _kln89->_blink)) {
188 _kln89->DrawSpecialChar(4, 2, 0, 3-drawPos);
190 // If the active waypoint is immediately after an approach header then we need to do an extra-long tail leg
191 if(_hdrPos >= 0 && idx == _hdrPos + 1) {
192 if(drawPos > 0 && !_kln89->_dto) _kln89->DrawLongLegTail(3-drawPos);
194 if(drawPos > 0 && !_kln89->_dto) _kln89->DrawLegTail(3-drawPos);
197 //cout << "Top pos is " << _fplPos0 << ' ';
199 if(_kln89->_mode == KLN89_MODE_CRSR) {
201 _kln89->Underline(2, 13, 3, 3);
202 } else if(_uLinePos >= 4) {
205 _kln89->Underline(2, 5, 3 - (_uLinePos - 4), 4);
206 } else if(_wLinePos == 4) {
207 _kln89->Underline(2, 4, 3 - (_uLinePos - 4), 4);
209 _kln89->Underline(2, 4, 3 - (_uLinePos - 4), _wLinePos);
210 _kln89->Underline(2, 5 + _wLinePos, 3 - (_uLinePos - 4), 4 - _wLinePos);
212 if(!_kln89->_blink) {
213 //_kln89->DrawText(_entWp->id, 2, 4, 2 - (_uLinePos - 4), false, _wLinePos);
217 _kln89->Underline(2, 4, 3 - (_uLinePos - 4), 5);
221 // ----------------------------------
222 //cout << "A1" << endl;
224 // Sanity check the top position - remember that we can have an extra blank one at the bottom even if CRSR is off if crsr is switched on then off
225 if((int)_fplPos > ((int)waylist.size()) - 3) _fplPos = (((int)waylist.size()) - 3 < 0 ? 0 : waylist.size() - 3);
226 unsigned int last_pos;
227 if(waylist.empty()) {
230 last_pos = ((int)_fplPos == ((int)waylist.size()) - 3 ? waylist.size() : waylist.size() - 1);
232 //cout << "Initialising last_pos, last_pos = " << last_pos << '\n';
233 //cout << "B1" << endl;
234 if(waylist.size() < 4) last_pos = waylist.size();
236 // Don't draw the cyclic field header if the top waypoint is the approach header
237 // Not sure if this also applies to the fence - don't think so but TODO - check!
238 if(waylist[_fplPos].appType != GPS_HDR) {
239 _kln89->DrawChar('>', 2, 12, 3);
240 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 3 && _kln89->_blink)) {
246 for(unsigned int i=0; i<4; ++i) {
247 //cout << "F1... " << i << endl;
248 // Don't draw the waypoint number for the header or fence
249 //cout << "_fplPos0 = " << _fplPos0 << ", waylist size is " << waylist.size() << '\n';
250 //cout << "F1, i = " << i << ", way id = " << waylist[_fplPos0+i].id << '\n';
251 if((waylist[_fplPos+i].appType == GPS_HDR || waylist[_fplPos+i].appType == GPS_FENCE)
252 && i != 3) { // By definition, the header and fence lines can't be displayed on the last line hence the unconditional !i==3 is safe.
256 int n = (i < 3 ? _fplPos + i + 1 : last_pos + 1);
257 if(_kln89->_approachLoaded) {
259 if(n > _fencePos) --n;
261 string s = GPSitoa(n);
262 // Don't draw the colon for waypoints that are part of the published approach
263 if(waylist[_fplPos+i].appType == GPS_APP_NONE) {
266 if(!(_delWp && _uLinePos == i+4)) _kln89->DrawText(s, 2, 4 - (s[s.size()-1] == ':' ? s.size() : s.size()+1), 3 - i);
268 //cout << "F1 done!\n";
270 if(_delWp && _uLinePos == i+4) {
271 if(!_kln89->_blink) {
272 _kln89->DrawText("Del", 2, 0, 3-i);
273 _kln89->DrawChar('?', 2, 10, 3-i);
274 _kln89->Underline(2, 0, 3-i, 11);
277 } else if(_kln89->_mode == KLN89_MODE_CRSR && _bEntWp && _uLinePos == i+4) {
278 if(!_kln89->_blink) {
279 if(_wLinePos >= _entWp->id.size()) {
280 _kln89->DrawText(_entWp->id, 2, 4, 3-i);
281 _kln89->DrawChar(' ', 2, 4+_wLinePos, 3-i, false, true);
283 _kln89->DrawText(_entWp->id.substr(0, _wLinePos), 2, 4, 3-i);
284 _kln89->DrawChar(_entWp->id[_wLinePos], 2, 4+_wLinePos, 3-i, false, true);
285 _kln89->DrawText(_entWp->id.substr(_wLinePos+1, _entWp->id.size()-_wLinePos-1), 2, 5+_wLinePos, 3-i);
290 //cout << "F2" << endl;
292 //cout << "F2a" << endl;
293 if(i == 3 || _fplPos + i == waylist.size()) {
294 //cout << "F2a1" << endl;
295 //cout << "_uLinePos = " << _uLinePos << ", i = " << i << ", waylist.size() = " << waylist.size() << endl;
296 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (i + 4) && _kln89->_blink)) {
297 //cout << "Drawing underline..." << endl;
298 _kln89->DrawText(last_pos < waylist.size() ? waylist[last_pos].GetAprId() : "_____", 2, 4, 3-i);
299 //cout << "2" << endl;
301 //cout << "3" << endl;
302 //cout << "last_pos = " << last_pos << endl;
303 if(last_pos > 0 && last_pos < waylist.size() && i > 0) {
304 //cout << "4" << endl;
306 if(_actFpMode == 0) {
307 string s = _params[last_pos - 1];
308 _kln89->DrawText(s, 2, 16-s.size(), 3-i);
309 } else if(_actFpMode == 3) {
310 string s = _params[last_pos - 1];
311 _kln89->DrawText(s, 2, 15-s.size(), 3-i);
312 _kln89->DrawSpecialChar(0, 2, 15, 3-i);
314 //cout << "5" << endl;
316 //cout << "6" << endl;
319 //cout << "F2a2" << endl;
320 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (i + 4) && _kln89->_blink)) {
321 if(waylist[_fplPos+i].appType == GPS_HDR) {
323 _kln89->DrawText("DELETE APPR?", 2, 1, 3-i);
324 } else if(_changeAppr) {
325 _kln89->DrawText("CHANGE APPR?", 2, 1, 3-i);
327 _kln89->DrawText(_kln89->_approachAbbrev, 2, 1, 3-i);
328 _kln89->DrawText(_kln89->_approachRwyStr, 2, 7, 3-i);
329 _kln89->DrawText(_kln89->_approachID, 2, 12, 3-i);
331 } else if(waylist[_fplPos+i].appType == GPS_FENCE) {
332 _kln89->DrawText("*NO WPT SEQ", 2, 0, 3-i);
334 _kln89->DrawText(waylist[_fplPos+i].GetAprId(), 2, 4, 3-i);
338 //cout << "F2b" << endl;
341 //cout << "i > 0 param draw...\n";
342 if(_actFpMode == 0) {
343 string s = _params[_fplPos + i - 1];
344 _kln89->DrawText(s, 2, 16-s.size(), 3-i);
345 } else if(_actFpMode == 3) {
346 string s = _params[_fplPos + i - 1];
347 _kln89->DrawText(s, 2, 15-s.size(), 3-i);
348 _kln89->DrawSpecialChar(0, 2, 15, 3-i);
351 //cout << "F2c" << endl;
353 //cout << "F3" << endl;
355 //cout << "GGGGGG" << endl;
356 } else { // Not active flightplan
357 //cout << "Top pos is " << _fplPos << ' ';
358 // For synatical convienience
359 //int nWp = (_subPage == 0 && !_delFP ? 4 : 3); // number of waypoints to display
360 vector<GPSWaypoint*> waylist = _kln89->_flightPlans[_subPage]->waypoints;
361 if(waylist.empty()) {
362 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 1 && _kln89->_blink)) {
363 _kln89->DrawText(_delFP ? "Delete FPL?" : "Copy FPL 0?", 2, 0, 3);
366 if(!(_kln89->_mode == KLN89_MODE_CRSR && (_uLinePos == 1 || _uLinePos == 2) && _kln89->_blink)) {
367 _kln89->DrawText(_delFP ? "Delete FPL?" : "Use?", 2, 0, 3);
369 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 2 && _kln89->_blink)) {
370 if(!_delFP) _kln89->DrawText("Inverted?", 2, 5, 3);
374 // ----------------------------------
375 if(_kln89->_mode == KLN89_MODE_CRSR) {
377 if(!_kln89->_blink) {
378 _kln89->Underline(2, 0, 3, (waylist.empty() || _delFP ? 11 : 4)); // This underline is blinked
381 } else if(_uLinePos == 2) {
382 // assert(!waylist.empty());
383 if(!_kln89->_blink) {
384 _kln89->Underline(2, 0, 3, 14); // This underline is blinked
387 } else if(_uLinePos == 3) {
388 _kln89->Underline(2, 13, 2, 3);
389 } else if(_uLinePos >= 4) {
392 _kln89->Underline(2, 5, 2 - (_uLinePos - 4), 4);
393 } else if(_wLinePos == 4) {
394 _kln89->Underline(2, 4, 2 - (_uLinePos - 4), 4);
396 _kln89->Underline(2, 4, 2 - (_uLinePos - 4), _wLinePos);
397 _kln89->Underline(2, 5 + _wLinePos, 2 - (_uLinePos - 4), 4 - _wLinePos);
399 if(!_kln89->_blink) {
400 //_kln89->DrawText(_entWp->id, 2, 4, 2 - (_uLinePos - 4), false, _wLinePos);
404 if(!_delWp) _kln89->Underline(2, 4, 2 - (_uLinePos - 4), 5);
408 // ----------------------------------
410 _kln89->DrawChar('>', 2, 12, 2);
411 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 3 && _kln89->_blink)) DrawFpMode(2);
412 // Sanity check the top position - remember that we can have an extra blank one at the bottom even if CRSR is off if crsr is switched on then off
413 if((int)_fplPos > ((int)waylist.size()) - 2) _fplPos = (((int)waylist.size()) - 2 < 0 ? 0 : waylist.size() - 2);
414 unsigned int last_pos;
415 if(waylist.empty()) {
418 last_pos = ((int)_fplPos == ((int)waylist.size()) - 2 ? waylist.size() : waylist.size() - 1);
420 if(waylist.size() < 3) last_pos = waylist.size();
421 for(unsigned int i=0; i<3; ++i) {
422 string s = GPSitoa(i < 2 ? _fplPos + i + 1 : last_pos + 1);
424 if(!(_delWp && _uLinePos == i+4)) _kln89->DrawText(s, 2, 4 - s.size(), 2 - i);
426 if(_delWp && _uLinePos == i+4) {
427 if(!_kln89->_blink) {
428 _kln89->DrawText("Del", 2, 0, 2-i);
429 _kln89->DrawChar('?', 2, 10, 2-i);
430 _kln89->Underline(2, 0, 2-i, 11);
433 } else if(_kln89->_mode == KLN89_MODE_CRSR && _bEntWp && _uLinePos == i+4) {
434 if(!_kln89->_blink) {
435 if(_wLinePos >= _entWp->id.size()) {
436 _kln89->DrawText(_entWp->id, 2, 4, 2-i);
437 _kln89->DrawChar(' ', 2, 4+_wLinePos, 2-i, false, true);
439 _kln89->DrawText(_entWp->id.substr(0, _wLinePos), 2, 4, 2-i);
440 _kln89->DrawChar(_entWp->id[_wLinePos], 2, 4+_wLinePos, 2-i, false, true);
441 _kln89->DrawText(_entWp->id.substr(_wLinePos+1, _entWp->id.size()-_wLinePos-1), 2, 5+_wLinePos, 2-i);
447 if(i == 2 || _fplPos + i == waylist.size()) {
448 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (i + 4) && _kln89->_blink)) {
449 _kln89->DrawText(last_pos < waylist.size() ? waylist[last_pos]->id : "_____", 2, 4, 2-i);
451 if(last_pos > 0 && last_pos < waylist.size() && i > 0) {
454 string s = _params[last_pos - 1];
455 _kln89->DrawText(s, 2, 16-s.size(), 2-i);
460 if(!(_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == (i + 4) && _kln89->_blink)) {
461 _kln89->DrawText(waylist[_fplPos+i]->id, 2, 4, 2-i);
466 string s = _params[_fplPos + i - 1];
467 _kln89->DrawText(s, 2, 16-s.size(), 2-i);
475 KLN89Page::Update(dt);
478 void KLN89FplPage::DrawFpMode(int ypos) {
481 if(_actFpMode == 1) {
483 } else if(_actFpMode == 2) {
484 s = "UTC"; // TODO - alter depending on chosen timezone
485 } else if(_actFpMode == 3) {
486 s = (_kln89->_obsMode ? "OBS" : "Dtk");
493 _kln89->DrawText(s, 2, 13, ypos);
496 // Bit of an ipsy-dipsy function this one - calc the required parameters for the displayed flightplan.
497 void KLN89FplPage::Calc() {
499 GPSFlightPlan* fp = _kln89->_flightPlans[_subPage];
500 vector<GPSWaypoint*> wv = fp->waypoints;
502 // Active FP - parameters are only displayed for the active waypoint onwards for the active plan,
503 // and distance is cumulative from the user position.
504 if(0 == _actFpMode) {
506 double cum_tot = 0.0;
508 //cum_tot += _kln89->GetHorizontalSeparation(_kln89->_gpsLat, _kln89->_gpsLon, wv[0]->lat, wv[0]->lon) * SG_METER_TO_NM;
509 cum_tot += _kln89->GetGreatCircleDistance(_kln89->_gpsLat, _kln89->_gpsLon, wv[0]->lat, wv[0]->lon);
511 for(unsigned int i=1; i<wv.size(); ++i) {
512 //cum_tot += _kln89->GetHorizontalSeparation(wv[i-1]->lat, wv[i-1]->lon, wv[i]->lat, wv[i]->lon) * SG_METER_TO_NM; // TODO - add units switch!
513 cum_tot += _kln89->GetGreatCircleDistance(wv[i-1]->lat, wv[i-1]->lon, wv[i]->lat, wv[i]->lon); // TODO - add units switch!
514 int n = (int)(cum_tot + 0.5);
515 _params.push_back(GPSitoa(n));
517 } else if(1 == _actFpMode) {
518 } else if(2 == _actFpMode) {
521 for(int i=1; i<wv.size(); ++i) {
522 double dtk = _kln89->GetMagHeadingFromTo(wv[i-1]->lat, wv[i-1]->lon, wv[i]->lat, wv[i]->lon);
523 int n = (int)(dtk + 0.5);
524 _params.push_back(GPSitoa(n));
531 double cum_tot = 0.0;
532 for(int i=1; i<wv.size(); ++i) {
533 //cum_tot += _kln89->GetHorizontalSeparation(wv[i-1]->lat, wv[i-1]->lon, wv[i]->lat, wv[i]->lon) * SG_METER_TO_NM; // TODO - add units switch!
534 cum_tot += _kln89->GetGreatCircleDistance(wv[i-1]->lat, wv[i-1]->lon, wv[i]->lat, wv[i]->lon); // TODO - add units switch!
535 int n = (int)(cum_tot + 0.5);
536 _params.push_back(GPSitoa(n));
543 void KLN89FplPage::CrsrPressed() {
546 _kln89->_mode = KLN89_MODE_DISP;
551 if(_kln89->_mode == KLN89_MODE_DISP) {
554 for(unsigned int i = 0; i < _kln89->_flightPlans[_subPage]->waypoints.size(); ++i) {
555 if(_kln89->_flightPlans[_subPage]->waypoints[i] == _entWp) {
556 _kln89->_flightPlans[_subPage]->waypoints.erase(_kln89->_flightPlans[_subPage]->waypoints.begin() + i);
565 if(_kln89->_obsMode) {
568 if(_kln89->_flightPlans[_subPage]->IsEmpty()) {
571 _uLinePos = (_subPage == 0 ? 3 : 1);
577 void KLN89FplPage::ClrPressed() {
579 _kln89->_mode = KLN89_MODE_DISP;
581 } else if(_delAppr) {
582 _kln89->_mode = KLN89_MODE_DISP;
585 if(KLN89_MODE_CRSR == _kln89->_mode) {
586 // TODO - see if we need to delete a waypoint
589 _kln89->_mode = KLN89_MODE_DISP;
592 // First check that we're not trying to delete an approach waypoint. Note that we can delete the approach by deleting the header though.
593 // Check for approach waypoints or header/fences in flightplan 0
594 int n = _fplPos + _uLinePos - 4;
596 bool fencePos = false;
597 //cout << "_fplPos = " << _fplPos << ", _uLinePos = " << _uLinePos << ", n = " << n << ", _hdrPos = " << _hdrPos << ", _fencePos = " << _fencePos << '\n';
599 //cout << "HEADER POS\n";
603 //cout << "FENCE POS\n";
606 if(_hdrPos >= 0 && n > _hdrPos) --n;
607 if(_fencePos >= 0 && n >= _fencePos) --n; // This one needs to be >= since n is already decremented by 1 in the line above!
608 //cout << "New n = " << n << '\n';
612 } else if(fencePos) {
615 } else if(n >= _kln89->_flightPlans[_subPage]->waypoints.size()) {
616 // no-op - off the end of the list on the entry field
617 } else if(_kln89->_flightPlans[_subPage]->waypoints[n]->appType == GPS_APP_NONE) {
619 _kln89->_mode = KLN89_MODE_CRSR;
622 ShowScratchpadMessage("Invald", " Del ");
625 } else if(_uLinePos == 3) {
628 if(_actFpMode > 3) _actFpMode = 0;
631 if(_fpMode > 1) _fpMode = 0;
637 _kln89->_mode = KLN89_MODE_CRSR;
642 void KLN89FplPage::CleanUp() {
643 // TODO - possibly need to clean up _delWp here as well, since it goes off if dto and then ent are pressed.
646 for(unsigned int i = 0; i < _kln89->_flightPlans[_subPage]->waypoints.size(); ++i) {
647 if(_kln89->_flightPlans[_subPage]->waypoints[i] == _entWp) {
648 _kln89->_flightPlans[_subPage]->waypoints.erase(_kln89->_flightPlans[_subPage]->waypoints.begin() + i);
654 KLN89Page::CleanUp();
657 void KLN89FplPage::LooseFocus() {
659 _resetFplPos0 = true;
663 _scratchpadMsg = false;
666 void KLN89FplPage::EntPressed() {
668 _parent->ClearFlightPlan(_subPage);
671 int pos = _uLinePos - 4 + _fplPos;
672 // Sanity check - the calculated wp position should never be off the end of the waypoint list.
673 if(pos > _kln89->_flightPlans[_subPage]->waypoints.size() - 1) {
674 cout << "ERROR - _uLinePos too big in KLN89FplPage::EntPressed!\n";
677 _kln89->_flightPlans[_subPage]->waypoints.erase(_kln89->_flightPlans[_subPage]->waypoints.begin() + pos);
679 // Do we need to re-calc _fplPos here?
680 } else if(_bEntExp) {
683 _entWp = NULL; // DON'T delete it! - it's been pushed onto the waypoint list at this point.
685 _kln89->_cleanUpPage = -1;
687 // TODO - in actual fact the previously underlined waypoint stays in the same position and underlined
688 // in some or possibly all circumstances - need to check this out and match it, but not too important
692 // TODO - should be able to get rid of this switch I think and use the enum values.
693 switch(_entWp->type) {
695 _kln89->_activePage = _kln89->_pages[0];
696 _kln89->_curPage = 0;
697 ((KLN89Page*)_kln89->_pages[0])->SetEntInvert(true);
700 _kln89->_activePage = _kln89->_pages[1];
701 _kln89->_curPage = 1;
702 ((KLN89Page*)_kln89->_pages[1])->SetEntInvert(true);
705 _kln89->_activePage = _kln89->_pages[2];
706 _kln89->_curPage = 2;
707 ((KLN89Page*)_kln89->_pages[2])->SetEntInvert(true);
710 _kln89->_activePage = _kln89->_pages[3];
711 _kln89->_curPage = 3;
712 ((KLN89Page*)_kln89->_pages[3])->SetEntInvert(true);
715 _kln89->_activePage = _kln89->_pages[4];
716 _kln89->_curPage = 4;
717 ((KLN89Page*)_kln89->_pages[4])->SetEntInvert(true);
720 cout << "Error - unknown waypoint type found in KLN89::FplPage::EntPressed()\n";
722 _kln89->_activePage->SetId(_entWp->id);
723 _kln89->_entJump = 7;
724 _kln89->_cleanUpPage = 7;
725 _kln89->_entRestoreCrsr = true;
726 _kln89->_mode = KLN89_MODE_DISP;
729 } else if(_uLinePos == 1) {
730 if(_kln89->_flightPlans[_subPage]->IsEmpty()) {
732 for(unsigned int i=0; i<_kln89->_flightPlans[0]->waypoints.size(); ++i) {
733 GPSWaypoint* wp = new GPSWaypoint;
734 *wp = *(_kln89->_flightPlans[0]->waypoints[i]);
735 _kln89->_flightPlans[_subPage]->waypoints.push_back(wp);
739 _parent->ClearFlightPlan(0);
740 for(unsigned int i=0; i<_kln89->_flightPlans[_subPage]->waypoints.size(); ++i) {
741 GPSWaypoint* wp = new GPSWaypoint;
742 *wp = *(_kln89->_flightPlans[_subPage]->waypoints[i]);
743 _kln89->_flightPlans[0]->waypoints.push_back(wp);
745 _kln89->OrientateToActiveFlightPlan();
748 _parent->CrsrPressed();
749 } else if(_uLinePos == 2) {
750 if(_kln89->_flightPlans[_subPage]->IsEmpty()) {
754 _parent->ClearFlightPlan(0);
755 for(unsigned int i=0; i<_kln89->_flightPlans[_subPage]->waypoints.size(); ++i) {
756 GPSWaypoint* wp = new GPSWaypoint;
757 *wp = *(_kln89->_flightPlans[_subPage]->waypoints[i]);
758 // FIXME - very inefficient - use a reverse iterator on the source array and push_back instead!!!!!!!!
759 _kln89->_flightPlans[0]->waypoints.insert(_kln89->_flightPlans[0]->waypoints.begin(), wp);
761 _kln89->OrientateToActiveFlightPlan();
763 _parent->CrsrPressed();
768 void KLN89FplPage::Knob1Left1() {
776 if(_kln89->_mode == KLN89_MODE_CRSR) {
778 if(_wLinePos > 0) _wLinePos--;
780 // _uLinePos with empty/not-empty plan: 1 = Copy FPL 0? / Use?, 2 = unused if empty / Invert?, 3 = >Dis/Dtk field, 4+ = Waypoint 1+
783 } else if(_uLinePos == 1 || _uLinePos == 2) {
785 } else if(_uLinePos == 3) {
787 } else if(_uLinePos == 4) {
788 if(_kln89->_flightPlans[_subPage]->IsEmpty()) {
789 _uLinePos = (_subPage == 0 ? 0 : 1);
790 } else if(_fplPos == 0) {
791 _uLinePos = (_subPage == 0 ? 0 : 2);
795 } else if(_uLinePos == 5) {
801 if(_subPage == 0 && _uLinePos > 3) {
802 int ix = _fplPos + (_uLinePos - 4);
803 if(_fencePos >= 0 && ix >= _fencePos) ix--;
804 if(_hdrPos >= 0 && ix >= _hdrPos) ix--;
805 if(ix >= _kln89->_activeFP->waypoints.size()) {
808 _fp0SelWpId = _kln89->_activeFP->waypoints[ix]->id;
812 //cout << "Not page 0, or not in waypoints, clearing id!\n";
818 void KLN89FplPage::Knob1Right1() {
826 if(_kln89->_mode == KLN89_MODE_CRSR) {
828 if(_wLinePos < 4) _wLinePos++;
830 // _uLinePos with empty/not-empty plan:
831 // 1 = Copy FPL 0? / Use?, 2 = unused if empty / Invert?, 3 = >Dis/Dtk field, 4+ = Waypoint 1+
833 _uLinePos = (_subPage == 0 ? 4 : 1);
834 } else if(_uLinePos == 1) {
835 _uLinePos = (_kln89->_flightPlans[_subPage]->IsEmpty() ? 4 : 2);
836 } else if(_uLinePos == 2) {
838 } else if(_uLinePos == 3) {
839 if(!_kln89->_flightPlans[_subPage]->IsEmpty()) _uLinePos = 5;
840 } else if(_uLinePos == 4) {
842 } else if((_subPage == 0 && _uLinePos == 6) || (_subPage > 0 && _uLinePos == 5)) {
843 // Urrggh - complicated!
845 // 1: We're on the entry field at the end of the list, and can't move any more.
846 // 2: We're on the last or second-last field, and move to the last position
847 // 3: We're on a field before the second-last one, and don't move, but change the list-head position
848 // And 4: _subPage 0 can be complicated by the presence of header/fence lines in an approach.
851 if(_hdrPos >= 0) hfcount++;
852 if(_fencePos >= 0) hfcount++;
854 if(_kln89->_flightPlans[_subPage]->waypoints.size() == 1 || _fplPos == _kln89->_flightPlans[_subPage]->waypoints.size() + hfcount - 1) {
856 } else if(_fplPos >= _kln89->_flightPlans[_subPage]->waypoints.size() + hfcount - (_subPage == 0 ? 4 : 3)) {
861 } else if(_uLinePos == 5) {
862 // Must be _subPage 0
865 // Must be the last line - either _uLinePos 6 or 7 depending on _subPage
866 int thresh = (_subPage == 0 ? 3 : 2);
867 if(_kln89->_flightPlans[_subPage]->waypoints.size() == thresh || _fplPos == _kln89->_flightPlans[_subPage]->waypoints.size() - thresh) {
874 if(_subPage == 0 && _uLinePos > 3) {
875 int ix = _fplPos + (_uLinePos - 4);
876 if(_fencePos >= 0 && ix >= _fencePos) ix--;
877 if(_hdrPos >= 0 && ix >= _hdrPos) ix--;
878 if(ix >= _kln89->_activeFP->waypoints.size()) {
881 _fp0SelWpId = _kln89->_activeFP->waypoints[ix]->id;
885 //cout << "Not page 0, or not in waypoints, clearing id!\n";
891 void KLN89FplPage::Knob2Left1() {
898 if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
899 if(_kln89->_mode != KLN89_MODE_CRSR) _resetFplPos0 = true;
900 KLN89Page::Knob2Left1();
903 // Check for approach waypoints or header/fences in flightplan 0
904 int n = _fplPos + _uLinePos - 4;
906 bool fencePos = false;
908 //cout << "_fplPos = " << _fplPos << ", _uLinePos = " << _uLinePos << ", n = " << n << ", _hdrPos = " << _hdrPos << ", _fencePos = " << _fencePos << '\n';
910 //cout << "HEADER POS\n";
914 //cout << "FENCE POS\n";
917 if(_hdrPos >= 0 && n > _hdrPos) --n;
918 if(_fencePos >= 0 && n >= _fencePos) --n; // This one needs to be >= since n is already decremented by 1 in the line above!
919 //cout << "New n = " << n << '\n';
921 if(n < _kln89->_flightPlans[_subPage]->waypoints.size()) {
922 if(_kln89->_flightPlans[_subPage]->waypoints[n]->appType != GPS_APP_NONE) {
928 // TODO - not sure what we actually do in this condition
930 } else if(fencePos) {
933 ShowScratchpadMessage("Invald", " Add ");
935 if((_wLinePos + 1) > _entWpStr.size()) {
938 _entWpStr[_wLinePos] = _kln89->DecChar(_entWpStr[_wLinePos], (_wLinePos == 0 ? false : true));
941 _fp0SelWpId.clear(); // Waypoints don't become the DTO default whilst being entered.
943 GPSWaypoint* wp = _kln89->FindFirstById(_entWpStr.substr(0, _wLinePos+1));
948 *_entWp = *wp; // copy
952 if(_fplPos + (_uLinePos - 4) >= _kln89->_flightPlans[_subPage]->waypoints.size()) {
953 _kln89->_flightPlans[_subPage]->waypoints.push_back(_entWp);
955 _kln89->_flightPlans[_subPage]->waypoints.insert(_kln89->_flightPlans[_subPage]->waypoints.begin()+(_fplPos + (_uLinePos - 4)), _entWp);
964 void KLN89FplPage::Knob2Right1() {
971 if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
972 if(_kln89->_mode != KLN89_MODE_CRSR) _resetFplPos0 = true;
973 KLN89Page::Knob2Right1();
976 // Check for approach waypoints or header/fences in flightplan 0
977 int n = _fplPos + _uLinePos - 4;
979 bool fencePos = false;
981 //cout << "_fplPos = " << _fplPos << ", _uLinePos = " << _uLinePos << ", n = " << n << ", _hdrPos = " << _hdrPos << ", _fencePos = " << _fencePos << '\n';
983 //cout << "HEADER POS\n";
987 //cout << "FENCE POS\n";
990 if(_hdrPos >= 0 && n > _hdrPos) --n;
991 if(_fencePos >= 0 && n >= _fencePos) --n; // This one needs to be >= since n is already decremented by 1 in the line above!
992 //cout << "New n = " << n << '\n';
994 if(n < _kln89->_flightPlans[_subPage]->waypoints.size()) {
995 if(_kln89->_flightPlans[_subPage]->waypoints[n]->appType != GPS_APP_NONE) {
1001 // TODO - not sure what we actually do in this condition
1003 } else if(fencePos) {
1006 ShowScratchpadMessage("Invald", " Add ");
1008 if((_wLinePos + 1) > _entWpStr.size()) {
1011 _entWpStr[_wLinePos] = _kln89->DecChar(_entWpStr[_wLinePos], (_wLinePos == 0 ? false : true));
1014 _fp0SelWpId.clear(); // Waypoints don't become the DTO default whilst being entered.
1016 GPSWaypoint* wp = _kln89->FindFirstById(_entWpStr.substr(0, _wLinePos+1));
1021 *_entWp = *wp; // copy
1025 if(_fplPos + (_uLinePos - 4) >= _kln89->_flightPlans[_subPage]->waypoints.size()) {
1026 _kln89->_flightPlans[_subPage]->waypoints.push_back(_entWp);
1028 _kln89->_flightPlans[_subPage]->waypoints.insert(_kln89->_flightPlans[_subPage]->waypoints.begin()+(_fplPos + (_uLinePos - 4)), _entWp);