forked from nvpro-samples/nvpro_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvpwindow.hpp
313 lines (287 loc) · 12.7 KB
/
nvpwindow.hpp
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
/*-----------------------------------------------------------------------
* Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ //--------------------------------------------------------------------
#ifndef __NVPWINDOW_H__
#define __NVPWINDOW_H__
#include "nvpsystem.hpp"
class NVPWindow
{
public:
// IMPORTANT
//
// Using and deriving of NVPWindow base-class is optional.
// However one must always make use of the NVPSystem
// That takes care of glfwInit/terminate as well.
////////////////////////////////////////////////////////////////////////
// base class
// these are taken from GLFW3 and must be kept in a matching state
enum ButtonAction
{
BUTTON_RELEASE = GLFW_RELEASE,
BUTTON_PRESS = GLFW_PRESS,
BUTTON_REPEAT = GLFW_REPEAT,
};
enum MouseButton
{
MOUSE_BUTTON_LEFT = GLFW_MOUSE_BUTTON_LEFT,
MOUSE_BUTTON_RIGHT = GLFW_MOUSE_BUTTON_RIGHT,
MOUSE_BUTTON_MIDDLE = GLFW_MOUSE_BUTTON_MIDDLE,
NUM_MOUSE_BUTTONIDX,
};
enum MouseButtonFlag
{
MOUSE_BUTTONFLAG_NONE = 0,
MOUSE_BUTTONFLAG_LEFT = (1 << MOUSE_BUTTON_LEFT),
MOUSE_BUTTONFLAG_RIGHT = (1 << MOUSE_BUTTON_RIGHT),
MOUSE_BUTTONFLAG_MIDDLE = (1 << MOUSE_BUTTON_MIDDLE)
};
enum KeyCode
{
KEY_UNKNOWN = GLFW_KEY_UNKNOWN,
KEY_SPACE = GLFW_KEY_SPACE,
KEY_APOSTROPHE = GLFW_KEY_APOSTROPHE, /* ' */
KEY_LEFT_PARENTHESIS = 40, /* ( */
KEY_RIGHT_PARENTHESIS = 41, /* ) */
KEY_ASTERISK = 42, /* * */
KEY_PLUS = 43, /* + */
KEY_COMMA = GLFW_KEY_COMMA, /* , */
KEY_MINUS = GLFW_KEY_MINUS, /* - */
KEY_PERIOD = GLFW_KEY_PERIOD, /* . */
KEY_SLASH = GLFW_KEY_SLASH, /* / */
KEY_0 = GLFW_KEY_0,
KEY_1 = GLFW_KEY_1,
KEY_2 = GLFW_KEY_2,
KEY_3 = GLFW_KEY_3,
KEY_4 = GLFW_KEY_4,
KEY_5 = GLFW_KEY_5,
KEY_6 = GLFW_KEY_6,
KEY_7 = GLFW_KEY_7,
KEY_8 = GLFW_KEY_8,
KEY_9 = GLFW_KEY_9,
KEY_COLON = 58, /* : */
KEY_SEMICOLON = GLFW_KEY_SEMICOLON, /* ; */
KEY_LESS = 60, /* < */
KEY_EQUAL = GLFW_KEY_EQUAL, /* = */
KEY_GREATER = 62, /* > */
KEY_A = GLFW_KEY_A,
KEY_B = GLFW_KEY_B,
KEY_C = GLFW_KEY_C,
KEY_D = GLFW_KEY_D,
KEY_E = GLFW_KEY_E,
KEY_F = GLFW_KEY_F,
KEY_G = GLFW_KEY_G,
KEY_H = GLFW_KEY_H,
KEY_I = GLFW_KEY_I,
KEY_J = GLFW_KEY_J,
KEY_K = GLFW_KEY_K,
KEY_L = GLFW_KEY_L,
KEY_M = GLFW_KEY_M,
KEY_N = GLFW_KEY_N,
KEY_O = GLFW_KEY_O,
KEY_P = GLFW_KEY_P,
KEY_Q = GLFW_KEY_Q,
KEY_R = GLFW_KEY_R,
KEY_S = GLFW_KEY_S,
KEY_T = GLFW_KEY_T,
KEY_U = GLFW_KEY_U,
KEY_V = GLFW_KEY_V,
KEY_W = GLFW_KEY_W,
KEY_X = GLFW_KEY_X,
KEY_Y = GLFW_KEY_Y,
KEY_Z = GLFW_KEY_Z,
KEY_LEFT_BRACKET = GLFW_KEY_LEFT_BRACKET, /* [ */
KEY_BACKSLASH = GLFW_KEY_BACKSLASH, /* \ */
KEY_RIGHT_BRACKET = GLFW_KEY_RIGHT_BRACKET, /* ] */
KEY_GRAVE_ACCENT = GLFW_KEY_GRAVE_ACCENT, /* ` */
KEY_WORLD_1 = GLFW_KEY_WORLD_1, /* non-US #1 */
KEY_WORLD_2 = GLFW_KEY_WORLD_2, /* non-US #2 */
/* Function keys */
KEY_ESCAPE = GLFW_KEY_ESCAPE,
KEY_ENTER = GLFW_KEY_ENTER,
KEY_TAB = GLFW_KEY_TAB,
KEY_BACKSPACE = GLFW_KEY_BACKSPACE,
KEY_INSERT = GLFW_KEY_INSERT,
KEY_DELETE = GLFW_KEY_DELETE,
KEY_RIGHT = GLFW_KEY_RIGHT,
KEY_LEFT = GLFW_KEY_LEFT,
KEY_DOWN = GLFW_KEY_DOWN,
KEY_UP = GLFW_KEY_UP,
KEY_PAGE_UP = GLFW_KEY_PAGE_UP,
KEY_PAGE_DOWN = GLFW_KEY_PAGE_DOWN,
KEY_HOME = GLFW_KEY_HOME,
KEY_END = GLFW_KEY_END,
KEY_CAPS_LOCK = GLFW_KEY_CAPS_LOCK,
KEY_SCROLL_LOCK = GLFW_KEY_SCROLL_LOCK,
KEY_NUM_LOCK = GLFW_KEY_NUM_LOCK,
KEY_PRINT_SCREEN = GLFW_KEY_PRINT_SCREEN,
KEY_PAUSE = GLFW_KEY_PAUSE,
KEY_F1 = GLFW_KEY_F1,
KEY_F2 = GLFW_KEY_F2,
KEY_F3 = GLFW_KEY_F3,
KEY_F4 = GLFW_KEY_F4,
KEY_F5 = GLFW_KEY_F5,
KEY_F6 = GLFW_KEY_F6,
KEY_F7 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T = GLFW_KEY_RIGHT_SHIFT,
KEY_RIGHT_CONTROL = GLFW_KEY_RIGHT_CONTROL,
KEY_RIGHT_ALT = GLFW_KEY_RIGHT_ALT,
KEY_RIGHT_SUPER = GLFW_KEY_RIGHT_SUPER,
KEY_MENU = GLFW_KEY_MENU,
KEY_LAST = GLFW_KEY_LAST,
};
enum KeyModifiers
{
KMOD_SHIFT = GLFW_MOD_SHIFT,
KMOD_CONTROL = GLFW_MOD_CONTROL,
KMOD_ALT = GLFW_MOD_ALT,
KMOD_SUPER = GLFW_MOD_SUPER,
};
//////////////////////////////////////////////////////////////////////////
GLFWwindow* m_internal = nullptr;
std::string m_windowName;
inline bool pollEvents() // returns false on exit, can do while(pollEvents()){ ... }
{
NVPSystem::pollEvents();
return !isClosing();
}
inline void waitEvents() { NVPSystem::waitEvents(); }
inline double getTime() { return NVPSystem::getTime(); }
inline std::string exePath() { return NVPSystem::exePath(); }
// Accessors
inline int getWidth() const { return m_windowSize[0]; }
inline int getHeight() const { return m_windowSize[1]; }
inline int getMouseWheel() const { return m_mouseWheel; }
inline int getKeyModifiers() const { return m_keyModifiers; }
inline int getMouseX() const { return m_mouseX; }
inline int getMouseY() const { return m_mouseY; }
void setTitle(const char* title);
void setFullScreen(bool bYes);
void setWindowPos(int x, int y);
void setWindowSize(int w, int h);
inline void setKeyModifiers(int m) { m_keyModifiers = m; }
inline void setMouse(int x, int y)
{
m_mouseX = x;
m_mouseY = y;
}
inline bool isFullScreen() const { return m_isFullScreen; }
inline bool isClosing() const { return m_isClosing || glfwWindowShouldClose(m_internal); }
inline bool isOpen() const
{
return glfwGetWindowAttrib(m_internal, GLFW_VISIBLE) == GLFW_TRUE
&& glfwGetWindowAttrib(m_internal, GLFW_ICONIFIED) == GLFW_FALSE && !isClosing();
}
virtual bool open(int posX, int posY, int width, int height, const char* title, bool requireGLContext); // creates internal window and opens it
void deinit(); // destroys internal window
void close(); // triggers closing event, still needs deinit for final cleanup
void maximize();
void restore();
void minimize();
// uses operating system specific code for sake of debugging/automated testing
void screenshot(const char* filename);
void clear(uint32_t r, uint32_t g, uint32_t b);
std::string openFileDialog(const char* title, const char* exts);
// derived windows/apps should override to handle events
virtual void onWindowClose() {}
virtual void onWindowResize(int w, int h) {}
virtual void onWindowRefresh() {}
virtual void onMouseMotion(int x, int y) {}
virtual void onMouseWheel(int delta) {}
virtual void onMouseButton(MouseButton button, ButtonAction action, int mods, int x, int y) {}
virtual void onKeyboard(KeyCode key, ButtonAction action, int mods, int x, int y) {}
virtual void onKeyboardChar(unsigned char key, int mods, int x, int y) {}
virtual void onDragDrop(int num, const char** paths) {}
// derived windows/apps should override these. Essentially used for remote-control (via sockets)
// the decoded remote paquets would invoke these methods. See shared_sources\nvsockets\socketSampleMessages.cpp
virtual void requestTiming() {
} // the app can override it to return requested timing information over sockets : use sysPostTiming() below
virtual void requestPaint() {} // the app needs to refresh once the window
virtual void requestContinuousRefresh(bool bYes) {} // the app might swith on/off the continuous rendering
virtual void requestSetArg(char token, int arg0, int arg1, int arg2, int arg3) {
} // the app receives arbitrary params from remote, free of interpretation
virtual void requestSetArg(char token, float arg0, float arg1, float arg2, float arg3) {
} // the app receives arbitrary params from remote, free of interpretation
private:
int m_mouseX;
int m_mouseY;
int m_mouseWheel;
int m_windowSize[2];
int m_keyModifiers;
bool m_isFullScreen = false;
bool m_isClosing = false;
int m_preFullScreenPos[2];
int m_preFullScreenSize[2];
static void cb_windowrefreshfun(GLFWwindow* glfwwin);
static void cb_windowsizefun(GLFWwindow* glfwwin, int w, int h);
static void cb_windowclosefun(GLFWwindow* glfwwin);
static void cb_mousebuttonfun(GLFWwindow* glfwwin, int button, int action, int mods);
static void cb_cursorposfun(GLFWwindow* glfwwin, double x, double y);
static void cb_scrollfun(GLFWwindow* glfwwin, double x, double y);
static void cb_keyfun(GLFWwindow* glfwwin, int key, int scancode, int action, int mods);
static void cb_charfun(GLFWwindow* glfwwin, unsigned int codepoint);
static void cb_dropfun(GLFWwindow* glfwwin, int count, const char** paths);
};
#endif