-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathadgs_ui.cpp
More file actions
289 lines (241 loc) · 10.3 KB
/
adgs_ui.cpp
File metadata and controls
289 lines (241 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//
// AutoDGS: Show Marshaller or VDGS at default airports
//
// Copyright (C) 2006-2013 Jonathan Harris
// Copyright (C) 2023, 2025 Holger Teutsch
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
// USA
//
#include <cstring>
#include <cassert>
#include "autodgs.h"
#include "airport.h"
#include "plane.h"
#include "XPWidgets.h"
#include "XPStandardWidgets.h"
#include "XPListBox.h"
#include "widget_ctx.h"
#include "version.h"
static WidgetCtx ui_widget_ctx;
static XPWidgetID ui_widget, list_box, status_line, marshaller_label, vdgs_label, marshaller_btn, vdgs_btn,
activate_btn, move_btn;
// current status of ui
static std::string ui_arpt_icao;
static int ui_selected_stand;
static inline bool IsActive() {
return (arpt && arpt->state() >= Airport::ARRIVAL);
}
static void ShowTypeButtons() {
XPShowWidget(vdgs_label);
XPShowWidget(marshaller_label);
XPShowWidget(vdgs_btn);
XPShowWidget(marshaller_btn);
XPShowWidget(move_btn);
XPHideWidget(activate_btn);
}
static void HideTypeButtons() {
XPHideWidget(vdgs_label);
XPHideWidget(marshaller_label);
XPHideWidget(vdgs_btn);
XPHideWidget(marshaller_btn);
XPHideWidget(move_btn);
}
static int WidgetCb(XPWidgetMessage msg, XPWidgetID widget_id, intptr_t param1, intptr_t param2) {
if (msg == xpMessage_CloseButtonPushed) {
ui_widget_ctx.Hide();
return 1;
}
if (msg == xpMsg_PushButtonPressed) {
if (widget_id == activate_btn) {
if (!(plane.BeaconOn() && on_ground))
XPSetWidgetDescriptor(status_line, "Beacon off or not on ground");
else
Activate();
} else if (widget_id == move_btn && IsActive())
arpt->DgsMoveCloser();
return 1;
}
if (msg == xpMessage_ListBoxItemSelected) {
ui_selected_stand = XPGetWidgetProperty(list_box, xpProperty_ListBoxCurrentItem, NULL);
assert(ui_selected_stand >= 0); // be paranoid
ui_selected_stand--; // 0 is "Automatic"
if (ui_selected_stand < 0) {
HideTypeButtons();
std::string txt = "Automatic @ " + (ui_arpt_icao.empty() ? "unknown" : ui_arpt_icao);
XPSetWidgetDescriptor(status_line, txt.c_str());
if (IsActive())
arpt->SetSelectedStand(-1); // unselect
return 1;
}
char ss_name[100];
ss_name[99] = '\0';
XPGetWidgetDescriptor(list_box, ss_name, sizeof(ss_name) - 1);
std::string txt{ss_name + 2}; // skip type, blank
LogMsg("selected ramp is '%s'", txt.c_str());
txt += " @ " + (ui_arpt_icao.empty() ? "unknown" : ui_arpt_icao);
XPSetWidgetDescriptor(status_line, txt.c_str());
ShowTypeButtons();
if (IsActive()) { // be paranoid, otherwise we should never be here
arpt->SetSelectedStand(ui_selected_stand);
int dgs_type = arpt->GetDgsType();
XPSetWidgetProperty(marshaller_btn, xpProperty_ButtonState, 0);
XPSetWidgetProperty(vdgs_btn, xpProperty_ButtonState, 0);
if (dgs_type == kMarshaller)
XPSetWidgetProperty(marshaller_btn, xpProperty_ButtonState, 1);
else
XPSetWidgetProperty(vdgs_btn, xpProperty_ButtonState, 1);
}
return 1;
}
if (msg != xpMsg_ButtonStateChanged)
return 0;
// From here on the radio buttons only
// radio buttons get this message only when they are clicked to "selected"
int dgs_type = -1;
if (widget_id == marshaller_btn) {
dgs_type = kMarshaller;
XPSetWidgetProperty(vdgs_btn, xpProperty_ButtonState, 0);
} else if (widget_id == vdgs_btn) {
dgs_type = kVDGS;
XPSetWidgetProperty(marshaller_btn, xpProperty_ButtonState, 0);
}
assert(dgs_type != -1);
if (IsActive()) {
arpt->SetDgsType(dgs_type);
int selected_stand = XPGetWidgetProperty(list_box, xpProperty_ListBoxCurrentItem, NULL);
selected_stand--; // 0 is "Automatic"
if (selected_stand >= 0) {
auto [dgs_type, name] = arpt->GetStand(selected_stand);
char entry[100];
snprintf(entry, sizeof(entry), "%c %s", (dgs_type == kMarshaller ? 'M' : 'V'), name.c_str());
// currently there is no replace op
XPSetWidgetProperty(list_box, xpProperty_ListBoxDeleteItem, 1);
XPSetWidgetDescriptor(list_box, entry);
XPSetWidgetProperty(list_box, xpProperty_ListBoxInsertItem, 1);
}
}
return 1;
}
void UpdateUI(bool only_if_visible) {
if (ui_widget == NULL || (only_if_visible && !XPIsWidgetVisible(ui_widget))) {
LogMsg("update_ui: widget is not visible");
return;
}
LogMsg("update_ui started");
if (!IsActive()) {
ui_selected_stand = -1;
XPSetWidgetProperty(list_box, xpProperty_ListBoxClear, 1);
ui_arpt_icao.clear();
HideTypeButtons();
XPShowWidget(activate_btn);
XPSetWidgetDescriptor(status_line, "");
return;
}
// active and newly loaded
if (arpt->name() != ui_arpt_icao) {
LogMsg("airport changed to %s", arpt->name().c_str());
ui_arpt_icao = arpt->name();
XPHideWidget(activate_btn);
LogMsg("load ramps");
ui_selected_stand = arpt->selected_stand(); // preselected via OFP or -1
XPSetWidgetDescriptor(list_box, "Automatic");
XPSetWidgetProperty(list_box, xpProperty_ListBoxAddItemsWithClear, 1);
for (int i = 0; i < arpt->nstands(); i++) {
auto [dgs_type, name] = arpt->GetStand(i);
char entry[100];
snprintf(entry, sizeof(entry), "%c %s", (dgs_type == kMarshaller ? 'M' : 'V'), name.c_str());
XPSetWidgetDescriptor(list_box, entry);
XPSetWidgetProperty(list_box, xpProperty_ListBoxAddItem, 1);
}
}
// that's cheap so we do it always
if (ui_selected_stand == -1) {
XPHideWidget(activate_btn);
HideTypeButtons();
std::string txt = "Automatic @ " + ui_arpt_icao;
XPSetWidgetDescriptor(status_line, txt.c_str());
} else if (IsActive()) {
ShowTypeButtons();
auto [dgs_type, name] = arpt->GetStand(ui_selected_stand);
XPSetWidgetProperty(marshaller_btn, xpProperty_ButtonState, (dgs_type == kMarshaller ? 1 : 0));
XPSetWidgetProperty(vdgs_btn, xpProperty_ButtonState, (dgs_type == kVDGS ? 1 : 0));
std::string txt = name + " @ " + ui_arpt_icao;
XPSetWidgetDescriptor(status_line, txt.c_str());
}
}
static void CreateUI() {
// Note that (0,0) is the top left while for widgets it's bottom left
// so we pass the y* arguments that the outcome is in widget coordinates
int xl, yr;
XPLMGetScreenBoundsGlobal(&xl, &yr, NULL, NULL);
int left = xl + 50;
int top = yr - 100;
int width = 250;
int height = 450;
int lb_height = 350;
ui_widget = XPCreateWidget(left, top, left + width, top - height, 0, "AutoDGS " VERSION_SHORT, 1, NULL,
xpWidgetClass_MainWindow);
ui_widget_ctx.Set(ui_widget, left, top, width, height);
XPSetWidgetProperty(ui_widget, xpProperty_MainWindowHasCloseBoxes, 1);
XPAddWidgetCallback(ui_widget, WidgetCb);
left += 5;
int left1 = left + 60;
top -= 20;
int top_btn = top - 20;
int left_btn = left + (width - 60) / 2;
// "Activate" button
activate_btn = XPCreateWidget(left_btn, top_btn, left_btn + 60, top_btn - 20, 1, "Activate", 0, ui_widget,
xpWidgetClass_Button);
XPSetWidgetProperty(activate_btn, xpProperty_ButtonType, xpPushButton);
XPSetWidgetProperty(activate_btn, xpProperty_ButtonBehavior, xpButtonBehaviorPushButton);
XPAddWidgetCallback(activate_btn, WidgetCb);
// "Move closer" button
left_btn = left1 + 60;
move_btn = XPCreateWidget(left_btn, top_btn, left_btn + 80, top_btn - 20, 1, "Move closer", 0, ui_widget,
xpWidgetClass_Button);
XPSetWidgetProperty(move_btn, xpProperty_ButtonType, xpPushButton);
XPSetWidgetProperty(move_btn, xpProperty_ButtonBehavior, xpButtonBehaviorPushButton);
XPAddWidgetCallback(move_btn, WidgetCb);
// Type radio buttons
marshaller_label =
XPCreateWidget(left, top, left + 50, top - 20, 1, "Marshaller", 0, ui_widget, xpWidgetClass_Caption);
marshaller_btn = XPCreateWidget(left1, top, left1 + 20, top - 20, 1, "", 0, ui_widget, xpWidgetClass_Button);
XPSetWidgetProperty(marshaller_btn, xpProperty_ButtonType, xpRadioButton);
XPSetWidgetProperty(marshaller_btn, xpProperty_ButtonBehavior, xpButtonBehaviorRadioButton);
XPAddWidgetCallback(marshaller_btn, WidgetCb);
XPSetWidgetProperty(marshaller_btn, xpProperty_ButtonState, 1);
top -= 20;
vdgs_label = XPCreateWidget(left, top, left + 50, top - 20, 1, "VDGS", 0, ui_widget, xpWidgetClass_Caption);
vdgs_btn = XPCreateWidget(left1, top, left1 + 20, top - 20, 1, "", 0, ui_widget, xpWidgetClass_Button);
XPSetWidgetProperty(vdgs_btn, xpProperty_ButtonType, xpRadioButton);
XPSetWidgetProperty(vdgs_btn, xpProperty_ButtonBehavior, xpButtonBehaviorRadioButton);
XPAddWidgetCallback(vdgs_btn, WidgetCb);
top -= 20;
status_line = XPCreateWidget(left, top, left + width - 30, top - 20, 1, "", 0, ui_widget, xpWidgetClass_Caption);
top -= 30;
list_box = XPCreateListBox(left, top, left + width - 10, top - lb_height, 1, "Automatic", ui_widget);
}
void ToggleUI(void) {
LogMsg("toggle_ui called");
if (ui_widget == NULL)
CreateUI();
if (XPIsWidgetVisible(ui_widget)) {
ui_widget_ctx.Hide();
return;
}
UpdateUI(false);
ui_widget_ctx.Show();
}