]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/KLN89/kln89_page_vor.cxx
James Turner: Improved runway management code:
[flightgear.git] / src / Instrumentation / KLN89 / kln89_page_vor.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_vor.hxx"
29
30 KLN89VorPage::KLN89VorPage(KLN89* parent) 
31 : KLN89Page(parent) {
32         _nSubPages = 2;
33         _subPage = 0;
34         _name = "VOR";
35         _vor_id = "OSI";        // TODO - check a property for an initial value to allow user-override.
36         np = NULL;
37 }
38
39 KLN89VorPage::~KLN89VorPage() {
40 }
41
42 void KLN89VorPage::Update(double dt) {
43         bool actPage = (_kln89->_activePage->GetName() == "ACT" ? true : false);
44         bool multi;  // Not set by FindFirst...
45         bool exact = false;
46         if(_vor_id.size() == 3) exact = true;
47         if(np == NULL) {
48                 np = _kln89->FindFirstVorById(_vor_id, multi, exact);
49         } else if(np->get_ident() != _vor_id) {
50                 np = _kln89->FindFirstVorById(_vor_id, multi, exact);
51         }
52         //if(np == NULL) cout << "NULL... ";
53         //if(b == false) cout << "false...\n";
54         /*
55         if(np && b) {
56                 cout << "VOR FOUND!\n";
57         } else {
58                 cout << ":-(\n";
59         }
60         */
61         if(np) {
62                 //cout << np->id << '\n';
63                 _vor_id = np->get_ident();
64                 if(_kln89->GetActiveWaypoint()) {
65                         if(_vor_id == _kln89->GetActiveWaypoint()->id) {
66                                 if(!(_kln89->_waypointAlert && _kln89->_blink)) {
67                                         // Active waypoint arrow
68                                         _kln89->DrawSpecialChar(4, 2, 0, 3);
69                                 }
70                         }
71                 }
72                 if(_kln89->_mode != KLN89_MODE_CRSR) {
73                         if(!_entInvert) {
74                                 if(!actPage) {
75                                         _kln89->DrawText(np->get_ident(), 2, 1, 3);
76                                 } else {
77                                         // If it's the ACT page, The ID is shifted slightly right to make space for the waypoint index.
78                                         _kln89->DrawText(np->get_ident(), 2, 4, 3);
79                                         char buf[3];
80                                         int n = snprintf(buf, 3, "%i", _kln89->GetActiveWaypointIndex() + 1);
81                                         _kln89->DrawText((string)buf, 2, 3 - n, 3);
82                                 }
83                         } else {
84                                 if(!_kln89->_blink) {
85                                         _kln89->DrawText(np->get_ident(), 2, 1, 3, false, 99);
86                                         _kln89->DrawEnt();
87                                 }
88                         }
89                 }
90                 if(_subPage == 0) {
91                         //// TODO - will almost certainly have to process freq below for FG
92                         _kln89->DrawFreq(np->get_freq(), 2, 9, 3);
93                         // TODO - trim VOR-DME from the name, convert to uppercase, abbreviate, etc
94                         _kln89->DrawText(np->get_name(), 2, 0, 2);
95                         //cout << np->lat << "... ";
96                         _kln89->DrawLatitude(np->get_lat(), 2, 3, 1);
97                         _kln89->DrawLongitude(np->get_lon(), 2, 3, 0);
98                 } else {
99                         _kln89->DrawText("Mag Var", 2, 0, 2);
100                         ////float mvf = np->magvar * SG_RADIANS_TO_DEGREES;
101                         //// TODO FIXME BELOW
102                         float mvf = 0.0;
103                         _kln89->DrawChar((mvf <= 0 ? 'E' : 'W'), 2, 9, 2);
104                         int mvi = (int)(fabs(mvf) + 0.5);
105                         string mvs = GPSitoa(mvi);
106                         _kln89->DrawText(mvs, 2, 13 - mvs.size(), 2);
107                         _kln89->DrawSpecialChar(0, 2, 13, 2);
108                         _kln89->DrawDirDistField(np->get_lat() * SG_DEGREES_TO_RADIANS, np->get_lon() * SG_DEGREES_TO_RADIANS, 2, 0, 0, 
109                                                  _to_flag, (_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 4 ? true : false));
110                 }
111         } else {
112                 if(_kln89->_mode != KLN89_MODE_CRSR) _kln89->DrawText(_vor_id, 2, 1, 3);
113                 if(_subPage == 0) {
114                         _kln89->DrawText("---.--", 2, 9, 3);
115                         _kln89->DrawText("--------------", 2, 0, 2);
116                         _kln89->DrawText("- -- --.--'", 2, 3, 1);
117                         _kln89->DrawText("---- --.--'", 2, 3, 0);
118                         _kln89->DrawSpecialChar(0, 2, 7, 1);
119                         _kln89->DrawSpecialChar(0, 2, 7, 0);
120                 }
121         }
122         
123         if(_kln89->_mode == KLN89_MODE_CRSR) {
124                 if(_uLinePos > 0 && _uLinePos < 4) {
125                         // TODO - blink as well
126                         _kln89->Underline(2, _uLinePos, 3, 1);
127                 }
128                 for(unsigned int i = 0; i < _vor_id.size(); ++i) {
129                         if(_uLinePos != (i + 1)) {
130                                 _kln89->DrawChar(_vor_id[i], 2, i + 1, 3);
131                         } else {
132                                 if(!_kln89->_blink) _kln89->DrawChar(_vor_id[i], 2, i + 1, 3);
133                         }
134                 }
135         }
136         
137         _id = _vor_id;
138                         
139         KLN89Page::Update(dt);
140 }
141
142 void KLN89VorPage::SetId(const string& s) {
143         _last_vor_id = _vor_id;
144         _save_vor_id = _vor_id;
145         _vor_id = s;
146         np = NULL;
147 }
148
149 void KLN89VorPage::CrsrPressed() {
150         if(_kln89->_mode == KLN89_MODE_DISP) return;
151         if(_kln89->_obsMode) {
152                 _uLinePos = 0;
153         } else {
154                 _uLinePos = 1;
155         }
156         if(_subPage == 0) {
157                 _maxULinePos = 17;
158         } else {
159                 _maxULinePos = 4;
160         }
161 }
162
163 void KLN89VorPage::ClrPressed() {
164         if(_subPage == 1 && _uLinePos == 4) {
165                 _to_flag = !_to_flag;
166         }
167 }
168
169 void KLN89VorPage::EntPressed() {
170         if(_entInvert) {
171                 _entInvert = false;
172                 _last_vor_id = _vor_id;
173                 _vor_id = _save_vor_id;
174         }
175 }
176
177 void KLN89VorPage::Knob2Left1() {
178         if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
179                 KLN89Page::Knob2Left1();
180         } else {
181                 if(_uLinePos < 4) {
182                         // Same logic for both pages - set the ID
183                         _vor_id = _vor_id.substr(0, _uLinePos);
184                         // ASSERT(_uLinePos > 0);
185                         if(_uLinePos == (_vor_id.size() + 1)) {
186                                 _vor_id += '9';
187                         } else {
188                                 _vor_id[_uLinePos - 1] = _kln89->DecChar(_vor_id[_uLinePos - 1], (_uLinePos == 1 ? false : true));
189                         }
190                 } else {
191                         if(_subPage == 0) {
192                                 // set by name
193                         } else {
194                                 // NO-OP - from/to field is switched by clr button, not inner knob.
195                         }
196                 }
197         }
198 }
199
200 void KLN89VorPage::Knob2Right1() {
201         if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) {
202                 KLN89Page::Knob2Right1();
203         } else {
204                 if(_uLinePos < 4) {
205                         // Same logic for both pages - set the ID
206                         _vor_id = _vor_id.substr(0, _uLinePos);
207                         // ASSERT(_uLinePos > 0);
208                         if(_uLinePos == (_vor_id.size() + 1)) {
209                                 _vor_id += 'A';
210                         } else {
211                                 _vor_id[_uLinePos - 1] = _kln89->IncChar(_vor_id[_uLinePos - 1], (_uLinePos == 1 ? false : true));
212                         }
213                 } else {
214                         if(_subPage == 0) {
215                                 // set by name
216                         } else {
217                                 // NO-OP - from/to field is switched by clr button, not inner knob.
218                         }
219                 }
220         }
221 }