-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathos_ui.cpp
314 lines (251 loc) · 10.2 KB
/
os_ui.cpp
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
openSAM: open source SAM emulator for X Plane
Copyright (C) 2024 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 <cstdlib>
#include <cstdio>
#include "openSAM.h"
#include "plane.h"
#include "samjw.h"
#include "jwctrl.h"
#include "XPLMDisplay.h"
#include "XPStandardWidgets.h"
typedef struct _widget_ctx
{
XPWidgetID widget;
int in_vr; /* currently in vr */
int l, t, w, h; /* last geometry before bringing into vr */
} widget_ctx_t;
static widget_ctx_t ui_widget_ctx;
static XPWidgetID ui_widget, jw_btn[kMaxDoor][kNearJwLimit],
auto_btn, dock_btn, undock_btn;
static void
show_widget(widget_ctx_t *ctx)
{
if (XPIsWidgetVisible(ctx->widget))
return;
/* force window into visible area of screen
we use modern windows under the hut so UI coordinates are in boxels */
/* 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, yl, xr, yr;
XPLMGetScreenBoundsGlobal(&xl, &yr, &xr, &yl);
ctx->l = (ctx->l + ctx->w < xr) ? ctx->l : xr - ctx->w - 50;
ctx->l = (ctx->l <= xl) ? 20 : ctx->l;
ctx->t = (ctx->t - ctx->h > yl) ? ctx->t : (yr - ctx->h - 50);
ctx->t = (ctx->t >= ctx->h) ? ctx->t : (yr / 2);
log_msg("show_widget: s: (%d, %d) -> (%d, %d), w: (%d, %d) -> (%d,%d)",
xl, yl, xr, yr, ctx->l, ctx->t, ctx->l + ctx->w, ctx->t - ctx->h);
XPSetWidgetGeometry(ctx->widget, ctx->l, ctx->t, ctx->l + ctx->w, ctx->t - ctx->h);
XPShowWidget(ctx->widget);
int in_vr = XPLMGetDatai(vr_enabled_dr);
if (in_vr) {
log_msg("VR mode detected");
XPLMWindowID window = XPGetWidgetUnderlyingWindow(ctx->widget);
XPLMSetWindowPositioningMode(window, xplm_WindowVR, -1);
ctx->in_vr = 1;
} else {
if (ctx->in_vr) {
log_msg("widget now out of VR, map at (%d,%d)", ctx->l, ctx->t);
XPLMWindowID window = XPGetWidgetUnderlyingWindow(ctx->widget);
XPLMSetWindowPositioningMode(window, xplm_WindowPositionFree, -1);
/* A resize is necessary so it shows up on the main screen again */
XPSetWidgetGeometry(ctx->widget, ctx->l, ctx->t, ctx->l + ctx->w, ctx->t - ctx->h);
ctx->in_vr = 0;
}
}
}
static void
close_ui()
{
XPGetWidgetGeometry(ui_widget_ctx.widget, &ui_widget_ctx.l, &ui_widget_ctx.t, NULL, NULL);
XPHideWidget(ui_widget_ctx.widget);
}
// static
int
MyPlane::ui_widget_cb(XPWidgetMessage msg, XPWidgetID widget_id,
[[maybe_unused]]intptr_t param1, intptr_t param2)
{
if (msg == xpMessage_CloseButtonPushed) {
close_ui();
return 1;
}
unsigned n_door = my_plane->n_door_;
if (msg == xpMsg_PushButtonPressed && widget_id == dock_btn) {
log_msg("Dock pressed");
if (! my_plane->auto_mode() && my_plane->ui_unlocked_) {
my_plane->active_jws_.resize(0);
for (unsigned i = 0; i < n_door; i++) {
// check for a selected button
for (unsigned j = 0; j < my_plane->nearest_jws_.size(); j++) {
int state = (uint64_t)XPGetWidgetProperty(jw_btn[i][j], xpProperty_ButtonState, NULL);
if (state) {
log_msg("active jw for door %d is %s", i, my_plane->nearest_jws_[j].jw_->name);
my_plane->nearest_jws_[j].door_ = i;
my_plane->active_jws_.push_back(my_plane->nearest_jws_[j]);
}
}
}
}
my_plane->dock_requested_ = true;
close_ui();
return 1;
}
if (msg == xpMsg_PushButtonPressed && widget_id == undock_btn) {
log_msg("Undock pressed");
my_plane->undock_requested_ = true;
close_ui();
return 1;
}
if (msg == xpMsg_ButtonStateChanged && widget_id == auto_btn) {
bool auto_mode = (bool)(uint64_t)param2;
log_msg("auto_mode now: %d", auto_mode);
my_plane->auto_mode_set(auto_mode); // start over with new setting
return 1;
}
// one of the jw select buttons
if (msg == xpMsg_ButtonStateChanged) {
// find index of button
int idoor = -1, ijw = -1;
for (unsigned i = 0; i < n_door; i++)
for (unsigned j = 0; j < my_plane->nearest_jws_.size(); j++)
if (jw_btn[i][j] == widget_id) {
idoor = i;
ijw = j;
break;
}
if (idoor < 0 || ijw < 0) {
log_msg("invalid button selection???");
return 1;
}
int new_state = (int)(uint64_t)param2;
log_msg("button door: %d, jw: %d pressed, name: %s, new_state: %d", idoor, ijw,
my_plane->nearest_jws_[ijw].jw_->name, new_state);
// unselect all other buttons for the selected door
for (unsigned j = 0; j < my_plane->nearest_jws_.size(); j++)
if ((int)j != ijw)
XPSetWidgetProperty(jw_btn[idoor][j], xpProperty_ButtonState, 0);
// unselect selected jw for other doors
for (unsigned i = 0; i < n_door; i++)
if ((int)i != idoor)
XPSetWidgetProperty(jw_btn[i][ijw], xpProperty_ButtonState, 0);
return 1;
}
return 0;
}
void
MyPlane::update_ui(bool only_if_visible)
{
if (ui_widget == NULL || (only_if_visible && !XPIsWidgetVisible(ui_widget))) {
log_msg("update_ui: widget is not visible");
return;
}
log_msg("update_ui started");
XPSetWidgetProperty(auto_btn, xpProperty_ButtonState, auto_mode_);
// hide everything
for (unsigned i = 0; i < kMaxDoor; i++)
for (unsigned j = 0; j < kNearJwLimit; j++)
XPHideWidget(jw_btn[i][j]);
// if manual selection set label and unhide
if (ui_unlocked_ && !auto_mode_) {
for (unsigned i = 0; i < n_door_; i++)
for (unsigned j = 0; j < nearest_jws_.size(); j++) {
JwCtrl& njw = nearest_jws_[j];
XPWidgetID btn = jw_btn[i][j];
XPSetWidgetDescriptor(btn, njw.jw_->name);
XPSetWidgetProperty(btn, xpProperty_ButtonState, 0);
XPShowWidget(btn);
}
}
log_msg("update_ui finished");
}
static void
create_ui()
{
// 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);
static const int margin = 20;
static const int col_spacing = 60;
int left = xl + 50;
int top = yr - 100;
int width = 2 * margin + kMaxDoor * col_spacing;
int height = 240;
int left1;
ui_widget_ctx.l = left;
ui_widget_ctx.t = top;
ui_widget_ctx.w = width;
ui_widget_ctx.h = height;
ui_widget = XPCreateWidget(left, top, left + width, top - height,
0, "openSAM " VERSION, 1, NULL, xpWidgetClass_MainWindow);
ui_widget_ctx.widget = ui_widget;
XPSetWidgetProperty(ui_widget, xpProperty_MainWindowHasCloseBoxes, 1);
XPAddWidgetCallback(ui_widget, MyPlane::ui_widget_cb);
top -= 20;
left1 = left + 60;
XPCreateWidget(left1, top, left1 + 50, top - 20,
1, "Jetway selection", 0, ui_widget, xpWidgetClass_Caption);
top -= 30;
left1 = left + margin;
auto_btn = XPCreateWidget(left1, top, left1 + 100, top - 20, 1, "Automatic Mode", 0, ui_widget, xpWidgetClass_Button);
XPSetWidgetProperty(auto_btn, xpProperty_ButtonBehavior, xpButtonBehaviorCheckBox);
XPAddWidgetCallback(auto_btn, MyPlane::ui_widget_cb);
top -= 30;
for (int i = 0; i < kMaxDoor; i++) {
char label[50];
sprintf(label, "Door %d", i + 1);
XPCreateWidget(left1, top, left1 + 50, top - 20,
1, label, 0, ui_widget, xpWidgetClass_Caption);
left1 += col_spacing;
}
top -= 20;
for (int j = 0; j < kNearJwLimit; j++) {
left1 = left + margin;
for (int i = 0; i < kMaxDoor; i++) {
XPWidgetID btn = jw_btn[i][j] =
XPCreateWidget(left1, top, left1 + 50, top - 20, 1, "Jw", 0, ui_widget, xpWidgetClass_Button);
XPSetWidgetProperty(btn, xpProperty_ButtonBehavior, xpButtonBehaviorCheckBox);
XPAddWidgetCallback(btn, MyPlane::ui_widget_cb);
left1 += col_spacing;
}
top -= 20;
}
top -= 20;
left1 = left + margin;
dock_btn = XPCreateWidget(left1, top, left1 + 50, top - 20, 1, "Dock", 0, ui_widget, xpWidgetClass_Button);
XPSetWidgetProperty(dock_btn, xpProperty_ButtonType, xpPushButton);
XPSetWidgetProperty(dock_btn, xpProperty_ButtonBehavior, xpButtonBehaviorPushButton);
XPAddWidgetCallback(dock_btn, MyPlane::ui_widget_cb);
left1 = left + 2 * margin + 50;
undock_btn = XPCreateWidget(left1, top, left1 + 50, top - 20, 1, "Undock", 0, ui_widget, xpWidgetClass_Button);
XPSetWidgetProperty(undock_btn, xpProperty_ButtonType, xpPushButton);
XPSetWidgetProperty(undock_btn, xpProperty_ButtonBehavior, xpButtonBehaviorPushButton);
XPAddWidgetCallback(undock_btn, MyPlane::ui_widget_cb);
}
void
toggle_ui(void) {
log_msg("toggle_ui called");
if (ui_widget == NULL)
create_ui();
if (XPIsWidgetVisible(ui_widget)) {
close_ui();
return;
}
if (! my_plane->is_helicopter_) {
my_plane->update_ui(0);
show_widget(&ui_widget_ctx);
}
}