forked from danilw/nanogui-GLES-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.cpp
713 lines (605 loc) · 23.1 KB
/
screen.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/*
src/screen.cpp -- Top-level widget and interface between NanoGUI and GLFW
A significant redesign of this code was contributed by Christian Schueller.
NanoGUI was developed by Wenzel Jakob <[email protected]>.
The widget drawing code is based on the NanoVG demo application
by Mikko Mononen.
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE.txt file.
*/
#include <nanogui/screen.h>
#include <nanogui/theme.h>
#include <nanogui/opengl.h>
#include <nanogui/window.h>
#include <nanogui/popup.h>
#include <map>
#include <iostream>
#if defined(_WIN32)
# define NOMINMAX
# undef APIENTRY
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# define GLFW_EXPOSE_NATIVE_WGL
# define GLFW_EXPOSE_NATIVE_WIN32
# include <GLFW/glfw3native.h>
#endif
/* Allow enforcing the GL2 implementation of NanoVG */
//#define NANOVG_GL3_IMPLEMENTATION
#include <nanovg_gl.h>
NAMESPACE_BEGIN(nanogui)
std::map<GLFWwindow *, Screen *> __nanogui_screens;
#if defined(NANOGUI_GLAD)
static bool gladInitialized = false;
#endif
/* Calculate pixel ratio for hi-dpi devices. */
static float get_pixel_ratio(GLFWwindow *window) {
#if defined(_WIN32)
HWND hWnd = glfwGetWin32Window(window);
HMONITOR monitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
/* The following function only exists on Windows 8.1+, but we don't want to make that a dependency */
static HRESULT (WINAPI *GetDpiForMonitor_)(HMONITOR, UINT, UINT*, UINT*) = nullptr;
static bool GetDpiForMonitor_tried = false;
if (!GetDpiForMonitor_tried) {
auto shcore = LoadLibrary(TEXT("shcore"));
if (shcore)
GetDpiForMonitor_ = (decltype(GetDpiForMonitor_)) GetProcAddress(shcore, "GetDpiForMonitor");
GetDpiForMonitor_tried = true;
}
if (GetDpiForMonitor_) {
uint32_t dpiX, dpiY;
if (GetDpiForMonitor_(monitor, 0 /* effective DPI */, &dpiX, &dpiY) == S_OK)
return dpiX / 96.0;
}
return 1.f;
#elif defined(__linux__)
(void) window;
float ratio = 1.0f;
FILE *fp;
/* Try to read the pixel ratio from KDEs config */
auto currentDesktop = std::getenv("XDG_CURRENT_DESKTOP");
if (currentDesktop && currentDesktop == std::string("KDE")) {
fp = popen("kreadconfig5 --group KScreen --key ScaleFactor", "r");
if (!fp)
return 1;
if (fscanf(fp, "%f", &ratio) != 1)
return 1;
} else {
/* Try to read the pixel ratio from GTK */
fp = popen("gsettings get org.gnome.desktop.interface scaling-factor", "r");
if (!fp)
return 1;
int ratioInt = 1;
if (fscanf(fp, "uint32 %i", &ratioInt) != 1)
return 1;
ratio = ratioInt;
}
if (pclose(fp) != 0)
return 1;
return ratio >= 1 ? ratio : 1;
#else
Vector2i fbSize, size;
glfwGetFramebufferSize(window, &fbSize[0], &fbSize[1]);
glfwGetWindowSize(window, &size[0], &size[1]);
return (float)fbSize[0] / (float)size[0];
#endif
}
Screen::Screen()
: Widget(nullptr), mGLFWWindow(nullptr), mNVGContext(nullptr),
mCursor(Cursor::Arrow), mBackground(0.3f, 0.3f, 0.32f, 1.f),
mShutdownGLFWOnDestruct(false), mFullscreen(false) {
memset(mCursors, 0, sizeof(GLFWcursor *) * (int) Cursor::CursorCount);
}
Screen::Screen(const Vector2i &size, const std::string &caption, bool resizable,
bool fullscreen, int colorBits, int alphaBits, int depthBits,
int stencilBits, int nSamples,
unsigned int glMajor, unsigned int glMinor,double scrollValx)
: Widget(nullptr), mGLFWWindow(nullptr), mNVGContext(nullptr),
mCursor(Cursor::Arrow), mBackground(0.3f, 0.3f, 0.32f, 1.f), mCaption(caption),
mShutdownGLFWOnDestruct(false), mFullscreen(fullscreen) {
memset(mCursors, 0, sizeof(GLFWcursor *) * (int) Cursor::CursorCount);
scrollVal=scrollValx;
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, glMajor);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, glMinor);
glfwWindowHint(GLFW_SAMPLES, nSamples);
glfwWindowHint(GLFW_RED_BITS, colorBits);
glfwWindowHint(GLFW_GREEN_BITS, colorBits);
glfwWindowHint(GLFW_BLUE_BITS, colorBits);
glfwWindowHint(GLFW_ALPHA_BITS, alphaBits);
glfwWindowHint(GLFW_STENCIL_BITS, stencilBits);
glfwWindowHint(GLFW_DEPTH_BITS, depthBits);
glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
glfwWindowHint(GLFW_RESIZABLE, resizable ? GL_TRUE : GL_FALSE);
if (fullscreen) {
GLFWmonitor *monitor = glfwGetPrimaryMonitor();
const GLFWvidmode *mode = glfwGetVideoMode(monitor);
mGLFWWindow = glfwCreateWindow(mode->width, mode->height,
caption.c_str(), monitor, nullptr);
} else {
mGLFWWindow = glfwCreateWindow(size.x(), size.y(),
caption.c_str(), nullptr, nullptr);
}
if (!mGLFWWindow)
throw std::runtime_error("Could not create an OpenGL " +
std::to_string(glMajor) + "." +
std::to_string(glMinor) + " context!");
glfwMakeContextCurrent(mGLFWWindow);
#if defined(NANOGUI_GLAD)
if (!gladInitialized) {
gladInitialized = true;
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
throw std::runtime_error("Could not initialize GLAD!");
glGetError(); // pull and ignore unhandled errors like GL_INVALID_ENUM
}
#endif
glfwGetFramebufferSize(mGLFWWindow, &mFBSize[0], &mFBSize[1]);
glViewport(0, 0, mFBSize[0], mFBSize[1]);
glClearColor(mBackground[0], mBackground[1], mBackground[2], mBackground[3]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glfwSwapInterval(0);
glfwSwapBuffers(mGLFWWindow);
#if defined(__APPLE__)
/* Poll for events once before starting a potentially
lengthy loading process. This is needed to be
classified as "interactive" by other software such
as iTerm2 */
glfwPollEvents();
#endif
/* Propagate GLFW events to the appropriate Screen instance */
glfwSetCursorPosCallback(mGLFWWindow,
[](GLFWwindow *w, double x, double y) {
auto it = __nanogui_screens.find(w);
if (it == __nanogui_screens.end())
return;
Screen *s = it->second;
if (!s->mProcessEvents)
return;
s->cursorPosCallbackEvent(x, y);
}
);
glfwSetMouseButtonCallback(mGLFWWindow,
[](GLFWwindow *w, int button, int action, int modifiers) {
auto it = __nanogui_screens.find(w);
if (it == __nanogui_screens.end())
return;
Screen *s = it->second;
if (!s->mProcessEvents)
return;
s->mouseButtonCallbackEvent(button, action, modifiers);
}
);
glfwSetKeyCallback(mGLFWWindow,
[](GLFWwindow *w, int key, int scancode, int action, int mods) {
auto it = __nanogui_screens.find(w);
if (it == __nanogui_screens.end())
return;
Screen *s = it->second;
if (!s->mProcessEvents)
return;
s->keyCallbackEvent(key, scancode, action, mods);
}
);
glfwSetCharCallback(mGLFWWindow,
[](GLFWwindow *w, unsigned int codepoint) {
auto it = __nanogui_screens.find(w);
if (it == __nanogui_screens.end())
return;
Screen *s = it->second;
if (!s->mProcessEvents)
return;
s->charCallbackEvent(codepoint);
}
);
glfwSetDropCallback(mGLFWWindow,
[](GLFWwindow *w, int count, const char **filenames) {
auto it = __nanogui_screens.find(w);
if (it == __nanogui_screens.end())
return;
Screen *s = it->second;
if (!s->mProcessEvents)
return;
s->dropCallbackEvent(count, filenames);
}
);
glfwSetScrollCallback(mGLFWWindow,
[](GLFWwindow *w, double x, double y) {
auto it = __nanogui_screens.find(w);
if (it == __nanogui_screens.end())
return;
Screen *s = it->second;
if (!s->mProcessEvents)
return;
s->scrollCallbackEvent(abs(x)>s->scrollVal?(x>0?-s->scrollVal:s->scrollVal):-x, abs(y)>s->scrollVal?(y>0?-s->scrollVal:s->scrollVal):-y);
}
);
/* React to framebuffer size events -- includes window
size events and also catches things like dragging
a window from a Retina-capable screen to a normal
screen on Mac OS X */
glfwSetFramebufferSizeCallback(mGLFWWindow,
[](GLFWwindow *w, int width, int height) {
auto it = __nanogui_screens.find(w);
if (it == __nanogui_screens.end())
return;
Screen *s = it->second;
if (!s->mProcessEvents)
return;
s->resizeCallbackEvent(width, height);
}
);
// notify when the screen has lost focus (e.g. application switch)
glfwSetWindowFocusCallback(mGLFWWindow,
[](GLFWwindow *w, int focused) {
auto it = __nanogui_screens.find(w);
if (it == __nanogui_screens.end())
return;
Screen *s = it->second;
// focused: 0 when false, 1 when true
s->focusEvent(focused != 0);
}
);
initialize(mGLFWWindow, true);
}
void Screen::initialize(GLFWwindow *window, bool shutdownGLFWOnDestruct) {
mGLFWWindow = window;
mShutdownGLFWOnDestruct = shutdownGLFWOnDestruct;
glfwGetWindowSize(mGLFWWindow, &mSize[0], &mSize[1]);
glfwGetFramebufferSize(mGLFWWindow, &mFBSize[0], &mFBSize[1]);
mPixelRatio = get_pixel_ratio(window);
#if defined(_WIN32) || defined(__linux__)
if (mPixelRatio != 1 && !mFullscreen)
glfwSetWindowSize(window, mSize.x() * mPixelRatio, mSize.y() * mPixelRatio);
#endif
#if defined(NANOGUI_GLAD)
if (!gladInitialized) {
gladInitialized = true;
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
throw std::runtime_error("Could not initialize GLAD!");
glGetError(); // pull and ignore unhandled errors like GL_INVALID_ENUM
}
#endif
/* Detect framebuffer properties and set up compatible NanoVG context */
GLint nStencilBits = 0, nSamples = 0;
glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER,
GL_STENCIL, GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE, &nStencilBits);
glGetIntegerv(GL_SAMPLES, &nSamples);
int flags = 0;
if (nStencilBits >= 8)
flags |= NVG_STENCIL_STROKES;
if (nSamples <= 1)
flags |= NVG_ANTIALIAS;
#if !defined(NDEBUG)
flags |= NVG_DEBUG;
#endif
mNVGContext = nvgCreateGLES3(flags);
if (mNVGContext == nullptr)
throw std::runtime_error("Could not initialize NanoVG!");
mVisible = glfwGetWindowAttrib(window, GLFW_VISIBLE) != 0;
setTheme(new Theme(mNVGContext));
mMousePos = Vector2i::Zero();
mMouseState = mModifiers = 0;
mDragActive = false;
mLastInteraction = glfwGetTime();
mProcessEvents = true;
__nanogui_screens[mGLFWWindow] = this;
for (int i=0; i < (int) Cursor::CursorCount; ++i)
mCursors[i] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR + i);
/// Fixes retina display-related font rendering issue (#185)
nvgBeginFrame(mNVGContext, mSize[0], mSize[1], mPixelRatio);
nvgEndFrame(mNVGContext);
}
Screen::~Screen() {
__nanogui_screens.erase(mGLFWWindow);
for (int i=0; i < (int) Cursor::CursorCount; ++i) {
if (mCursors[i])
glfwDestroyCursor(mCursors[i]);
}
if (mNVGContext)
nvgDeleteGLES3(mNVGContext);
if (mGLFWWindow && mShutdownGLFWOnDestruct)
glfwDestroyWindow(mGLFWWindow);
}
void Screen::setVisible(bool visible) {
if (mVisible != visible) {
mVisible = visible;
if (visible)
glfwShowWindow(mGLFWWindow);
else
glfwHideWindow(mGLFWWindow);
}
}
void Screen::setCaption(const std::string &caption) {
if (caption != mCaption) {
glfwSetWindowTitle(mGLFWWindow, caption.c_str());
mCaption = caption;
}
}
void Screen::setSize(const Vector2i &size) {
Widget::setSize(size);
#if defined(_WIN32) || defined(__linux__)
glfwSetWindowSize(mGLFWWindow, size.x() * mPixelRatio, size.y() * mPixelRatio);
#else
glfwSetWindowSize(mGLFWWindow, size.x(), size.y());
#endif
}
void Screen::drawAll() {
glClearColor(mBackground[0], mBackground[1], mBackground[2], mBackground[3]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
drawContents();
drawWidgets();
glfwSwapBuffers(mGLFWWindow);
}
void Screen::drawWidgets() {
if (!mVisible)
return;
glfwMakeContextCurrent(mGLFWWindow);
glfwGetFramebufferSize(mGLFWWindow, &mFBSize[0], &mFBSize[1]);
glfwGetWindowSize(mGLFWWindow, &mSize[0], &mSize[1]);
#if defined(_WIN32) || defined(__linux__)
mSize = (mSize.cast<float>() / mPixelRatio).cast<int>();
mFBSize = (mSize.cast<float>() * mPixelRatio).cast<int>();
#else
/* Recompute pixel ratio on OSX */
if (mSize[0])
mPixelRatio = (float) mFBSize[0] / (float) mSize[0];
#endif
glViewport(0, 0, mFBSize[0], mFBSize[1]);
glBindSampler(0, 0);
nvgBeginFrame(mNVGContext, mSize[0], mSize[1], mPixelRatio);
draw(mNVGContext);
double elapsed = glfwGetTime() - mLastInteraction;
if (elapsed > 0.5f) {
/* Draw tooltips */
const Widget *widget = findWidget(mMousePos);
if (widget && !widget->tooltip().empty()) {
int tooltipWidth = 150;
float bounds[4];
nvgFontFace(mNVGContext, "sans");
nvgFontSize(mNVGContext, 15.0f);
nvgTextAlign(mNVGContext, NVG_ALIGN_LEFT | NVG_ALIGN_TOP);
nvgTextLineHeight(mNVGContext, 1.1f);
Vector2i pos = widget->absolutePosition() +
Vector2i(widget->width() / 2, widget->height() + 10);
nvgTextBounds(mNVGContext, pos.x(), pos.y(),
widget->tooltip().c_str(), nullptr, bounds);
int h = (bounds[2] - bounds[0]) / 2;
if (h > tooltipWidth / 2) {
nvgTextAlign(mNVGContext, NVG_ALIGN_CENTER | NVG_ALIGN_TOP);
nvgTextBoxBounds(mNVGContext, pos.x(), pos.y(), tooltipWidth,
widget->tooltip().c_str(), nullptr, bounds);
h = (bounds[2] - bounds[0]) / 2;
}
nvgGlobalAlpha(mNVGContext,
std::min(1.0, 2 * (elapsed - 0.5f)) * 0.8);
nvgBeginPath(mNVGContext);
nvgFillColor(mNVGContext, Color(0, 255));
nvgRoundedRect(mNVGContext, bounds[0] - 4 - h, bounds[1] - 4,
(int) (bounds[2] - bounds[0]) + 8,
(int) (bounds[3] - bounds[1]) + 8, 3);
int px = (int) ((bounds[2] + bounds[0]) / 2) - h;
nvgMoveTo(mNVGContext, px, bounds[1] - 10);
nvgLineTo(mNVGContext, px + 7, bounds[1] + 1);
nvgLineTo(mNVGContext, px - 7, bounds[1] + 1);
nvgFill(mNVGContext);
nvgFillColor(mNVGContext, Color(255, 255));
nvgFontBlur(mNVGContext, 0.0f);
nvgTextBox(mNVGContext, pos.x() - h, pos.y(), tooltipWidth,
widget->tooltip().c_str(), nullptr);
}
}
nvgEndFrame(mNVGContext);
}
bool Screen::keyboardEvent(int key, int scancode, int action, int modifiers) {
if (mFocusPath.size() > 0) {
for (auto it = mFocusPath.rbegin() + 1; it != mFocusPath.rend(); ++it)
if ((*it)->focused() && (*it)->keyboardEvent(key, scancode, action, modifiers))
return true;
}
return false;
}
bool Screen::keyboardCharacterEvent(unsigned int codepoint) {
if (mFocusPath.size() > 0) {
for (auto it = mFocusPath.rbegin() + 1; it != mFocusPath.rend(); ++it)
if ((*it)->focused() && (*it)->keyboardCharacterEvent(codepoint))
return true;
}
return false;
}
bool Screen::resizeEvent(const Vector2i& size) {
if (mResizeCallback) {
mResizeCallback(size);
return true;
}
return false;
}
bool Screen::cursorPosCallbackEvent(double x, double y) {
Vector2i p((int) x, (int) y);
#if defined(_WIN32) || defined(__linux__)
p = (p.cast<float>() / mPixelRatio).cast<int>();
#endif
bool ret = false;
mLastInteraction = glfwGetTime();
try {
p -= Vector2i(1, 2);
if (!mDragActive) {
Widget *widget = findWidget(p);
if (widget != nullptr && widget->cursor() != mCursor) {
mCursor = widget->cursor();
glfwSetCursor(mGLFWWindow, mCursors[(int) mCursor]);
}
} else {
ret = mDragWidget->mouseDragEvent(
p - mDragWidget->parent()->absolutePosition(), p - mMousePos,
mMouseState, mModifiers);
}
if (!ret)
ret = mouseMotionEvent(p, p - mMousePos, mMouseState, mModifiers);
mMousePos = p;
return ret;
} catch (const std::exception &e) {
std::cerr << "Caught exception in event handler: " << e.what() << std::endl;
return false;
}
}
bool Screen::mouseButtonCallbackEvent(int button, int action, int modifiers) {
mModifiers = modifiers;
mLastInteraction = glfwGetTime();
try {
if (mFocusPath.size() > 1) {
const Window *window =
dynamic_cast<Window *>(mFocusPath[mFocusPath.size() - 2]);
if (window && window->modal()) {
if (!window->contains(mMousePos))
return false;
}
}
if (action == GLFW_PRESS)
mMouseState |= 1 << button;
else
mMouseState &= ~(1 << button);
auto dropWidget = findWidget(mMousePos);
if (mDragActive && action == GLFW_RELEASE &&
dropWidget != mDragWidget)
mDragWidget->mouseButtonEvent(
mMousePos - mDragWidget->parent()->absolutePosition(), button,
false, mModifiers);
if (dropWidget != nullptr && dropWidget->cursor() != mCursor) {
mCursor = dropWidget->cursor();
glfwSetCursor(mGLFWWindow, mCursors[(int) mCursor]);
}
if (action == GLFW_PRESS && (button == GLFW_MOUSE_BUTTON_1 || button == GLFW_MOUSE_BUTTON_2)) {
mDragWidget = findWidget(mMousePos);
if (mDragWidget == this)
mDragWidget = nullptr;
mDragActive = mDragWidget != nullptr;
if (!mDragActive)
updateFocus(nullptr);
} else {
mDragActive = false;
mDragWidget = nullptr;
}
return mouseButtonEvent(mMousePos, button, action == GLFW_PRESS,
mModifiers);
} catch (const std::exception &e) {
std::cerr << "Caught exception in event handler: " << e.what() << std::endl;
return false;
}
}
bool Screen::keyCallbackEvent(int key, int scancode, int action, int mods) {
mLastInteraction = glfwGetTime();
try {
return keyboardEvent(key, scancode, action, mods);
} catch (const std::exception &e) {
std::cerr << "Caught exception in event handler: " << e.what() << std::endl;
return false;
}
}
bool Screen::charCallbackEvent(unsigned int codepoint) {
mLastInteraction = glfwGetTime();
try {
return keyboardCharacterEvent(codepoint);
} catch (const std::exception &e) {
std::cerr << "Caught exception in event handler: " << e.what()
<< std::endl;
return false;
}
}
bool Screen::dropCallbackEvent(int count, const char **filenames) {
std::vector<std::string> arg(count);
for (int i = 0; i < count; ++i)
arg[i] = filenames[i];
return dropEvent(arg);
}
bool Screen::scrollCallbackEvent(double x, double y) {
mLastInteraction = glfwGetTime();
try {
if (mFocusPath.size() > 1) {
const Window *window =
dynamic_cast<Window *>(mFocusPath[mFocusPath.size() - 2]);
if (window && window->modal()) {
if (!window->contains(mMousePos))
return false;
}
}
return scrollEvent(mMousePos, Vector2f(x, y));
} catch (const std::exception &e) {
std::cerr << "Caught exception in event handler: " << e.what()
<< std::endl;
return false;
}
}
bool Screen::resizeCallbackEvent(int, int) {
Vector2i fbSize, size;
glfwGetFramebufferSize(mGLFWWindow, &fbSize[0], &fbSize[1]);
glfwGetWindowSize(mGLFWWindow, &size[0], &size[1]);
#if defined(_WIN32) || defined(__linux__)
size = (size.cast<float>() / mPixelRatio).cast<int>();
#endif
if (mFBSize == Vector2i(0, 0) || size == Vector2i(0, 0))
return false;
mFBSize = fbSize; mSize = size;
mLastInteraction = glfwGetTime();
try {
return resizeEvent(mSize);
} catch (const std::exception &e) {
std::cerr << "Caught exception in event handler: " << e.what()
<< std::endl;
return false;
}
}
void Screen::updateFocus(Widget *widget) {
for (auto w: mFocusPath) {
if (!w->focused())
continue;
w->focusEvent(false);
}
mFocusPath.clear();
Widget *window = nullptr;
while (widget) {
mFocusPath.push_back(widget);
if (dynamic_cast<Window *>(widget))
window = widget;
widget = widget->parent();
}
for (auto it = mFocusPath.rbegin(); it != mFocusPath.rend(); ++it)
(*it)->focusEvent(true);
if (window)
moveWindowToFront((Window *) window);
}
void Screen::disposeWindow(Window *window) {
if (std::find(mFocusPath.begin(), mFocusPath.end(), window) != mFocusPath.end())
mFocusPath.clear();
if (mDragWidget == window)
mDragWidget = nullptr;
removeChild(window);
}
void Screen::centerWindow(Window *window) {
if (window->size() == Vector2i::Zero()) {
window->setSize(window->preferredSize(mNVGContext));
window->performLayout(mNVGContext);
}
window->setPosition((mSize - window->size()) / 2);
}
void Screen::moveWindowToFront(Window *window) {
mChildren.erase(std::remove(mChildren.begin(), mChildren.end(), window), mChildren.end());
mChildren.push_back(window);
/* Brute force topological sort (no problem for a few windows..) */
bool changed = false;
do {
size_t baseIndex = 0;
for (size_t index = 0; index < mChildren.size(); ++index)
if (mChildren[index] == window)
baseIndex = index;
changed = false;
for (size_t index = 0; index < mChildren.size(); ++index) {
Popup *pw = dynamic_cast<Popup *>(mChildren[index]);
if (pw && pw->parentWindow() == window && index < baseIndex) {
moveWindowToFront(pw);
changed = true;
break;
}
}
} while (changed);
}
NAMESPACE_END(nanogui)