-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathwindow11.h
More file actions
233 lines (193 loc) · 7.54 KB
/
Copy pathwindow11.h
File metadata and controls
233 lines (193 loc) · 7.54 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
#pragma once
#include <memory>
#include <string>
#include <vector>
#include "XPLMDisplay.h"
#include "XPWidgetDefs.h"
#include "dataref.h"
#ifndef XPLM300
#include "XPWidgets.h"
#endif
#include "geometry.h"
#include "layout_object.h"
#include "flight_loop.h"
#define XP10_WIDGET_HEADER_HEIGHT 16
#define XP10_WIDGET_GUTTER_WIDTH 10
class LayoutObject;
class Widget11;
// This is an XPLM300 window
class Window11Base {
protected:
Rect last_frame_window_bounds;
std::shared_ptr<Window11Base> this_ref;
#ifdef XPLM300
XPLMWindowID window = nullptr;
FlightLoopCallback<void> close_detection_callback;
#else
XPWidgetID widget = nullptr;
XPWidgetID widget_background = nullptr;
XPWidgetID widget_container = nullptr;
static int window_callback(XPWidgetMessage inMessage, XPWidgetID inWidget, intptr_t inParam1, intptr_t inParam2);
static int window_background_callback(XPWidgetMessage inMessage, XPWidgetID inWidget, intptr_t inParam1, intptr_t inParam2);
static int container_callback(XPWidgetMessage inMessage, XPWidgetID inWidget, intptr_t inParam1, intptr_t inParam2);
#endif
public:
bool isVisible() const {
#ifdef XPLM300
return XPLMGetWindowIsVisible(window);
#else
return XPIsWidgetVisible(widget);
#endif
}
void show() {
#ifdef XPLM300
XPLMSetWindowIsVisible(window, 1);
#else
XPShowWidget(widget);
#endif
}
void hide() {
#ifdef XPLM300
XPLMSetWindowIsVisible(window, 0);
#else
XPHideWidget(widget);
#endif
}
void bringToFront() {
#ifdef XPLM300
XPLMBringWindowToFront(window);
#else
XPBringRootWidgetToFront(widget);
#endif
}
void closeWindow() {
this_ref.reset();
}
int getWidth() const {
return getWindowBounds().size().width;
}
int getHeight() const {
return getWindowBounds().size().height;
}
void setTitle(const std::string & title) {
#ifdef XPLM300
XPLMSetWindowTitle(window, title.c_str());
#else
XPSetWidgetDescriptor(widget, title.c_str());
#endif
}
void setWindowBounds(Rect new_bounds) {
#ifdef XPLM300
XPLMSetWindowGeometry(window, new_bounds.left, new_bounds.top, new_bounds.right, new_bounds.bottom);
#else
XPSetWidgetGeometry(widget, new_bounds.left - XP10_WIDGET_GUTTER_WIDTH, new_bounds.top + XP10_WIDGET_HEADER_HEIGHT + XP10_WIDGET_GUTTER_WIDTH, new_bounds.right + XP10_WIDGET_GUTTER_WIDTH, new_bounds.bottom - XP10_WIDGET_GUTTER_WIDTH);
XPSetWidgetGeometry(widget_background, new_bounds.left - XP10_WIDGET_GUTTER_WIDTH, new_bounds.top + XP10_WIDGET_GUTTER_WIDTH, new_bounds.right + XP10_WIDGET_GUTTER_WIDTH, new_bounds.bottom - XP10_WIDGET_GUTTER_WIDTH);
XPSetWidgetGeometry(widget_container, new_bounds.left, new_bounds.top, new_bounds.right, new_bounds.bottom);
#endif
}
Rect getWindowBounds() const {
Rect window_bounds;
#ifdef XPLM300
XPLMGetWindowGeometry(window, &window_bounds.left, &window_bounds.top, &window_bounds.right, &window_bounds.bottom);
#else
XPGetWidgetGeometry(widget_container, &window_bounds.left, &window_bounds.top, &window_bounds.right, &window_bounds.bottom);
#endif
return window_bounds;
}
void setWindowSize(int window_width, int window_height) {
Rect window_bounds = getWindowBounds();
window_bounds.right = window_bounds.left + window_width;
window_bounds.top = window_bounds.bottom + window_height;
setWindowBounds(window_bounds);
}
void setWindowSize(Size window_size) {
setWindowSize(window_size.width, window_size.height);
}
// Centered on main screen
#ifdef XPLM300
void setWindowCentered() {
int screen_left, screen_top, screen_right, screen_bottom;
XPLMGetScreenBoundsGlobal(&screen_left, &screen_top, &screen_right, &screen_bottom);
int screen_width = screen_right - screen_left;
int screen_height = screen_top - screen_bottom;
int left = screen_left + screen_width / 2 - getWidth() / 2;
int right = left + getWidth();
int bottom = screen_bottom + screen_height / 2 - getHeight() / 2;
int top = bottom + getHeight();
setWindowBounds(Rect{left, top, right, bottom});
}
#else
void setWindowCentered() {
int screen_width, screen_height;
XPLMGetScreenSize(&screen_width, &screen_height);
int left = screen_width / 2 - getWidth() / 2;
int right = screen_width / 2 + getWidth() / 2;
int top = screen_height / 2 + getHeight() / 2;
int bottom = screen_height / 2 - getHeight() / 2;
setWindowBounds(Rect{left, top, right, bottom});
}
#endif
void setWindowResizeLimits([[maybe_unused]] int min_width, [[maybe_unused]] int min_height, [[maybe_unused]] int max_width, [[maybe_unused]] int max_height) {
#ifdef XPLM300
XPLMSetWindowResizingLimits(window, min_width, min_height, max_width, max_height);
#else
/// todo: is this possible?
#endif
}
std::optional<int> getPoppedOutMonitor() const;
void setPoppedOutMonitor(int monitor_number);
static inline void draw(XPLMWindowID /* inWindowID */, void * inRefcon) {
Window11Base * pthis = static_cast<Window11Base *>(inRefcon);
pthis->draw(pthis->getWindowBounds());
}
static inline int mouseClick(XPLMWindowID /* inWindowID */, int x, int y, XPLMMouseStatus inMouse, void *inRefcon) {
Window11Base * pthis = static_cast<Window11Base *>(inRefcon);
return pthis->mouseClick(Point{x, y}, inMouse) ? 1 : 0;
}
static inline void keyPress(XPLMWindowID /* inWindowID */, char inKey, XPLMKeyFlags inFlags, char inVirtualKey, void * inRefcon, int losingFocus) {
Window11Base * pthis = static_cast<Window11Base *>(inRefcon);
pthis->keyPress(inKey, inFlags, inVirtualKey, losingFocus);
}
static inline XPLMCursorStatus handleCursor(XPLMWindowID /* inWindowID */, int x,int y, void * inRefcon) {
Window11Base * pthis = static_cast<Window11Base *>(inRefcon);
return pthis->handleCursor(Point{x, y});
}
static inline int handleMouseWheel(XPLMWindowID /* inWindowID */, int x, int y, int wheel, int clicks, void * inRefcon) {
Window11Base * pthis = static_cast<Window11Base *>(inRefcon);
return pthis->handleMouseWheel({x, y}, wheel, clicks) ? 1 : 0;
}
virtual void draw(Rect draw_bounds);
virtual bool mouseClick(Point point, XPLMMouseStatus status);
virtual void keyPress(char key, XPLMKeyFlags flags, uint8_t virtual_key, int losingFocus);
virtual XPLMCursorStatus handleCursor(Point point);
virtual bool handleMouseWheel(Point point, int wheel, int clicks);
void setKeyboardFocusToWidget(std::shared_ptr<Widget11> widget);
// Where windows implement their own key handling:
virtual bool keyPress(char /* key */, XPLMKeyFlags /* flags */, uint8_t /* virtual_key */);
#if defined(XPLM301)
protected:
Dataref<bool> currently_in_vr;
bool isSimInVR();
public:
void setVR(bool is_vr);
#endif
protected:
std::shared_ptr<LayoutObject> layout_object;
public:
Window11Base();
virtual ~Window11Base();
void setTopLevelWidget(std::shared_ptr<LayoutObject> new_layout_obj);
};
template <class WindowClass>
class Window11 : public Window11Base {
public:
template <class ... Params>
static inline std::shared_ptr<WindowClass> make(Params... params) {
std::shared_ptr<WindowClass> sp = std::make_shared<WindowClass>(params...);
sp->this_ref = sp;
return sp;
}
std::shared_ptr<WindowClass> getWindowSharedPtr() {
return std::dynamic_pointer_cast<WindowClass>(this_ref);
}
};