]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/KLN89/kln89_page_int.cxx
Merge branch 'maint2' into next
[flightgear.git] / src / Instrumentation / KLN89 / kln89_page_int.cxx
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 - david.luff@nottingham.ac.uk
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_int.hxx"
29 #include "Navaids/fix.hxx"
30 #include "Navaids/navrecord.hxx"
31
32 KLN89IntPage::KLN89IntPage(KLN89* parent) 
33 : KLN89Page(parent) {
34         _nSubPages = 2;
35         _subPage = 0;
36         _name = "INT";
37         _int_id = "PORTE";
38         _last_int_id = "";
39         _fp = NULL;
40         _nearestVor = NULL;
41         _refNav = NULL;
42 }
43
44 KLN89IntPage::~KLN89IntPage() {
45 }
46
47 void KLN89IntPage::Update(double dt) {
48         bool actPage = (_kln89->_activePage->GetName() == "ACT" ? true : false);
49         bool multi;  // Not set by FindFirst...
50         bool exact = false;
51         if(_int_id.size() == 5) exact = true;
52         if(_fp == NULL) {
53                 _fp = _kln89->FindFirstIntById(_int_id, multi, exact);
54         } else if(_fp->get_ident() != _int_id) {
55                 _fp = _kln89->FindFirstIntById(_int_id, multi, exact);
56         }
57         
58         if(_fp) {
59                 _int_id = _fp->get_ident();
60                 if(_kln89->GetActiveWaypoint()) {
61                         if(_int_id == _kln89->GetActiveWaypoint()->id) {
62                                 if(!(_kln89->_waypointAlert && _kln89->_blink)) {
63                                         // Active waypoint arrow
64                                         _kln89->DrawSpecialChar(4, 2, 0, 3);
65                                 }
66                         }
67                 }
68                 if(_int_id != _last_int_id) {
69                         _nearestVor = _kln89->FindClosestVor(_fp->get_lat() * SG_DEGREES_TO_RADIANS, _fp->get_lon() * SG_DEGREES_TO_RADIANS);
70                         if(_nearestVor) {
71                                 _nvRadial = _kln89->GetMagHeadingFromTo(_nearestVor->get_lat() * SG_DEGREES_TO_RADIANS, _nearestVor->get_lon() * SG_DEGREES_TO_RADIANS,
72                                                                     _fp->get_lat() * SG_DEGREES_TO_RADIANS, _fp->get_lon() * SG_DEGREES_TO_RADIANS);
73                                 _nvDist = _kln89->GetGreatCircleDistance(_nearestVor->get_lat() * SG_DEGREES_TO_RADIANS, _nearestVor->get_lon() * SG_DEGREES_TO_RADIANS,
74                                                                      _fp->get_lat() * SG_DEGREES_TO_RADIANS, _fp->get_lon() * SG_DEGREES_TO_RADIANS);
75                                 _refNav = _nearestVor;  // TODO - check that this *always* holds - eg. when changing INT id after explicitly setting ref nav 
76                                                                         // but with no loss of focus.
77                         } else {
78                                 _refNav = NULL;
79                         }
80                         _last_int_id = _int_id;
81                 }
82                 if(_kln89->_mode != KLN89_MODE_CRSR) {
83                         if(!_entInvert) {
84                                 if(!actPage) {
85                                         _kln89->DrawText(_fp->get_ident(), 2, 1, 3);
86                                 } else {
87                                         // If it's the ACT page, The ID is shifted slightly right to make space for the waypoint index.
88                                         _kln89->DrawText(_fp->get_ident(), 2, 4, 3);
89                                         char buf[3];
90                                         int n = snprintf(buf, 3, "%i", _kln89->GetActiveWaypointIndex() + 1);
91                                         _kln89->DrawText((string)buf, 2, 3 - n, 3);
92                                         // We also draw an I to differentiate INT from USR when it's the ACT page
93                                         _kln89->DrawText("I", 2, 11, 3);
94                                 }
95                         } else {
96                                 if(!_kln89->_blink) {
97                                         _kln89->DrawText(_fp->get_ident(), 2, 1, 3, false, 99);
98                                         _kln89->DrawEnt();
99                                 }
100                         }
101                 }
102                 if(_subPage == 0) {
103                         _kln89->DrawLatitude(_fp->get_lat(), 2, 3, 2);
104                         _kln89->DrawLongitude(_fp->get_lon(), 2, 3, 1);
105                         _kln89->DrawDirDistField(_fp->get_lat() * SG_DEGREES_TO_RADIANS, _fp->get_lon() * SG_DEGREES_TO_RADIANS, 2, 0, 0, 
106                                                  _to_flag, (_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 6 ? true : false));
107                 } else {
108                         _kln89->DrawText("Ref:", 2, 1, 2);
109                         _kln89->DrawText("Rad:", 2, 1, 1);
110                         _kln89->DrawText("Dis:", 2, 1, 0);
111                         if(_refNav) {
112                                 _kln89->DrawText(_refNav->get_ident(), 2, 9, 2);        // TODO - flash and allow to change if under cursor
113                                 //_kln89->DrawHeading(_nvRadial, 2, 11, 1);
114                                 //_kln89->DrawDist(_nvDist, 2, 11, 0);
115                                 // Currently our draw heading and draw dist functions don't do as many decimal points as we want here,
116                                 // so draw it ourselves!
117                                 // Heading
118                                 char buf[10];
119                                 snprintf(buf, 6, "%.1f", _nvRadial);
120                                 string s = buf;
121                                 _kln89->DrawText(s, 2, 13 - s.size(), 1);
122                                 _kln89->DrawSpecialChar(0, 2, 13, 1);   // Degrees symbol
123                                 // Dist
124                                 double d = _nvDist;
125                                 d *= (_kln89->_distUnits == GPS_DIST_UNITS_NM ? 1.0 : SG_NM_TO_METER * 0.001);
126                                 snprintf(buf, 9, "%.1f", d);
127                                 s = buf;
128                                 s += (_kln89->_distUnits == GPS_DIST_UNITS_NM ? "nm" : "Km");
129                                 _kln89->DrawText(s, 2, 14 - s.size(), 0);
130                         }
131                 }
132         } else {
133                 // TODO - when we leave the page with invalid id and return it should
134                 // revert to showing the last valid id.  Same for vor/ndb/probably apt etc.
135                 if(_kln89->_mode != KLN89_MODE_CRSR) _kln89->DrawText(_int_id, 2, 1, 3);
136                 if(_subPage == 0) {
137                         _kln89->DrawText("- -- --.--'", 2, 3, 2);
138                         _kln89->DrawText("---- --.--'", 2, 3, 1);
139                         _kln89->DrawSpecialChar(0, 2, 7, 2);
140                         _kln89->DrawSpecialChar(0, 2, 7, 1);
141                         _kln89->DrawText(">---    ----", 2, 0, 0);
142                         _kln89->DrawSpecialChar(0, 2, 4, 0);
143                         _kln89->DrawText(_to_flag ? "To" : "Fr", 2, 5, 0);
144                         _kln89->DrawText(_kln89->_distUnits == GPS_DIST_UNITS_NM ? "nm" : "km", 2, 12, 0);
145                 }
146         }
147         
148         if(_kln89->_mode == KLN89_MODE_CRSR) {
149                 if(_uLinePos > 0 && _uLinePos < 6) {
150                         // TODO - blink as well
151                         _kln89->Underline(2, _uLinePos, 3, 1);
152                 }
153                 for(unsigned int i = 0; i < _int_id.size(); ++i) {
154                         if(_uLinePos != (i + 1)) {
155                                 _kln89->DrawChar(_int_id[i], 2, i + 1, 3);
156                         } else {
157                                 if(!_kln89->_blink) _kln89->DrawChar(_int_id[i], 2, i + 1, 3);
158                         }
159                 }
160         }
161         
162         // TODO - fix this duplication - use _id instead of _apt_id, _vor_id, _ndb_id, _int_id etc!
163         _id = _int_id;
164         
165         KLN89Page::Update(dt);
166 }
167
168 void KLN89IntPage::SetId(const string& s) {
169         _last_int_id = _int_id;
170         _save_int_id = _int_id;
171         _int_id = s;
172         _fp = NULL;
173 }
174
175 void KLN89IntPage::CrsrPressed() {
176         if(_kln89->_mode == KLN89_MODE_DISP) return;
177         if(_kln89->_obsMode) {
178                 _uLinePos = 0;
179         } else {
180                 _uLinePos = 1;
181         }
182         if(_subPage == 0) {
183                 _maxULinePos = 6;
184         } else {
185                 _maxULinePos = 6;
186         }
187 }
188
189 void KLN89IntPage::ClrPressed() {
190         if(_subPage == 0 && _uLinePos == 6) {
191                 _to_flag = !_to_flag;
192         }
193 }
194
195 void KLN89IntPage::EntPressed() {
196         if(_entInvert) {
197                 _entInvert = false;
198                 _last_int_id = _int_id;
199                 _int_id = _save_int_id;
200         }
201 }
202
203 void KLN89IntPage::Knob2Left1() {
204         if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
205                 KLN89Page::Knob2Left1();
206         } else {
207                 if(_uLinePos < 6) {
208                         // Same logic for both pages - set the ID
209                         _int_id = _int_id.substr(0, _uLinePos);
210                         // ASSERT(_uLinePos > 0);
211                         if(_uLinePos == (_int_id.size() + 1)) {
212                                 _int_id += '9';
213                         } else {
214                                 _int_id[_uLinePos - 1] = _kln89->DecChar(_int_id[_uLinePos - 1], (_uLinePos == 1 ? false : true));
215                         }
216                 } else {
217                         if(_subPage == 0) {
218                                 // NO-OP - from/to field is switched by clr button, not inner knob.
219                         } else {
220                                 // TODO - LNR type field.
221                         }
222                 }
223         }
224 }
225
226 void KLN89IntPage::Knob2Right1() {
227         if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
228                 KLN89Page::Knob2Right1();
229         } else {
230                 if(_uLinePos < 6) {
231                         // Same logic for both pages - set the ID
232                         _int_id = _int_id.substr(0, _uLinePos);
233                         // ASSERT(_uLinePos > 0);
234                         if(_uLinePos == (_int_id.size() + 1)) {
235                                 _int_id += 'A';
236                         } else {
237                                 _int_id[_uLinePos - 1] = _kln89->IncChar(_int_id[_uLinePos - 1], (_uLinePos == 1 ? false : true));
238                         }
239                 } else {
240                         if(_subPage == 0) {
241                                 // NO-OP - from/to field is switched by clr button, not inner knob.
242                         } else {
243                                 // TODO - LNR type field.
244                         }
245                 }
246         }
247 }
248