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