-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen.cpp
More file actions
380 lines (307 loc) · 11.4 KB
/
screen.cpp
File metadata and controls
380 lines (307 loc) · 11.4 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
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
#include "screen.h"
#include "popup.h"
#ifdef __APPLE__
# define GLFW_INCLUDE_GLCOREARB
#endif
#ifdef EMSCRIPTEN
#define GLFW_INCLUDE_ES2
#endif
#define GLFW_INCLUDE_GLEXT
#include <GLFW/glfw3.h>
#include "nanovg.h"
/*
#if defined (__wasm__)
//#define NANOVG_GLES3_IMPLEMENTATION
#define NANOVG_GLES2_IMPLEMENTATION
#include "nanovg_gl.h"
//#define GLFW_INCLUDE_ES3
#define NANOVG_FBO_VALID 1
#include "nanovg_gl_utils.h"
#else
#define NANOVG_GL3_IMPLEMENTATION
#include "nanovg_gl.h"
#endif
*/
#ifdef EMSCRIPTEN
#define NANOVG_GLES2_IMPLEMENTATION
#include "nanovg_gl.h"
#define NANOVG_FBO_VALID 1
#include "nanovg_gl_utils.h"
#include <emscripten.h>
#include <emscripten/html5.h>
#else
#define NANOVG_GL3_IMPLEMENTATION
#include "nanovg_gl.h"
#endif
#include <cmath>
Screen *gScreen = nullptr;
Screen::Screen(float w, float h, float dpr): Item(nullptr, 0, 0, w, h), mColor(Color::white), mPixelRatio(dpr)
{
gScreen = this;
memset(mCursors, 0, sizeof(GLFWcursor *) * (int) Cursor::CursorCount);
if (!glfwInit()) {
printf("Failed to init GLFW.\n");
return;
}
#ifdef EMSCRIPTEN
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
#else
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
#endif
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
#ifdef EMSCRIPTEN
mGLFWWindow = glfwCreateWindow(w*mPixelRatio, h*mPixelRatio, "Hello World!", nullptr, nullptr);
#else
mGLFWWindow = glfwCreateWindow(w, h, "Hello World!", nullptr, nullptr);
#endif
if (!mGLFWWindow)
throw std::runtime_error("Could not create an OpenGL 3 2 context!");
glfwMakeContextCurrent(mGLFWWindow);
glfwGetFramebufferSize(mGLFWWindow, &mFBSize[0], &mFBSize[1]);
glViewport(0, 0, mFBSize[0], mFBSize[1]);
glClearColor(mColor.r/255.0f, mColor.g/255.0f, mColor.b/255.0f, 1.f);
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 *, double x, double y) {
#ifdef EMSCRIPTEN
gScreen->cursorPosCallbackEvent(x/gScreen->mPixelRatio, y/gScreen->mPixelRatio);
#else
gScreen->cursorPosCallbackEvent(x, y);
#endif
});
glfwSetMouseButtonCallback(mGLFWWindow, [](GLFWwindow *, int button, int action, int modifiers) {
gScreen->mouseButtonCallbackEvent(button, action, modifiers);
});
glfwSetKeyCallback(mGLFWWindow, [](GLFWwindow *, int key, int scancode, int action, int mods) {
gScreen->keyCallbackEvent(key, scancode, action, mods);
});
glfwSetCharCallback(mGLFWWindow, [](GLFWwindow *, unsigned int codepoint) {
gScreen->charCallbackEvent(codepoint);
});
glfwSetDropCallback(mGLFWWindow, [](GLFWwindow *, int count, const char **filenames) {
gScreen->dropCallbackEvent(count, filenames);
});
glfwSetScrollCallback(mGLFWWindow, [](GLFWwindow *, double x, double y) {
const double scrollVal = 1;
float _x = std::abs(x)>scrollVal?(x>0?-scrollVal:scrollVal):-x;
float _y = std::abs(y)>scrollVal?(y>0?-scrollVal:scrollVal):-y;
//printf("scrollCallbackEvent %f, %f %lf, %lf\n", _x, _y, x, y);
gScreen->scrollEvent(gScreen->mMousePos[0], gScreen->mMousePos[1], _x, _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 *, int width, int height) {
gScreen->resizeCallbackEvent(width, height);
});
// notify when the screen has lost focus (e.g. application switch)
glfwSetWindowFocusCallback(mGLFWWindow, [](GLFWwindow *, int focused) {
gScreen->focusEvent(focused != 0);
});
initialize(mGLFWWindow);
}
void Screen::initialize(GLFWwindow *window) {
mGLFWWindow = window;
glfwGetWindowSize(mGLFWWindow, &mSize[0], &mSize[1]);
glfwGetFramebufferSize(mGLFWWindow, &mFBSize[0], &mFBSize[1]);
/* 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);
#ifdef EMSCRIPTEN
mNVGContext = nvgCreateGLES2(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
#else
mNVGContext = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
#endif
if (mNVGContext == nullptr)
throw std::runtime_error("Could not initialize NanoVG!");
mMousePos[0] = 0;
mMousePos[1] = 0;
mMouseState = mModifiers = 0;
mDragActive = false;
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);
mPopup = add<Popup>(0, 0, 200, 400);
}
Screen::~Screen() {
for (int i=0; i < (int) Cursor::CursorCount; ++i) {
if (mCursors[i])
glfwDestroyCursor(mCursors[i]);
}
#ifdef EMSCRIPTEN
if (mNVGContext)
nvgDeleteGLES2(mNVGContext);
#else
if (mNVGContext)
nvgDeleteGL3(mNVGContext);
#endif
if (mGLFWWindow)
glfwDestroyWindow(mGLFWWindow);
}
void Screen::setBackground(Color color) {
mColor = color;
}
void Screen::setCursor(Cursor cursor) {
mCursor = cursor;
glfwSetCursor(mGLFWWindow, mCursors[(int) mCursor]);
}
void Screen::drawAll() {
glClearColor(mColor.r/255.0f, mColor.g/255.0f, mColor.b/255.0f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
drawContents();
drawWidgets();
glfwSwapBuffers(mGLFWWindow);
}
void Screen::drawWidgets() {
glfwMakeContextCurrent(mGLFWWindow);
glfwGetFramebufferSize(mGLFWWindow, &mFBSize[0], &mFBSize[1]);
glfwGetWindowSize(mGLFWWindow, &mSize[0], &mSize[1]);
glViewport(0, 0, mFBSize[0], mFBSize[1]);
//printf("mSize = (%d, %d)\n", mSize[0], mSize[1]);
//glBindSampler(0, 0);
#ifdef EMSCRIPTEN
mSize[0] = mSize[0]/mPixelRatio;
mSize[1] = mSize[1]/mPixelRatio;
#endif
nvgBeginFrame(mNVGContext, mSize[0], mSize[1], mPixelRatio);
draw(mNVGContext);
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)->focus() && (*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)->focus() && (*it)->keyboardCharacterEvent(codepoint))
return true;
}
return false;
}
bool Screen::cursorPosCallbackEvent(float x, float y) {
float p[2] = {x, y};
bool ret = false;
try {
//p[0] -= 1;
//p[1] -= 2;
if (mDragActive) {
float _x, _y;
mDragWidget->parent()->absolutePosition(&_x, &_y);
ret = mDragWidget->mouseDragEvent(p[0] - _x, p[1] - _y, p[0] - mMousePos[0], p[1] - mMousePos[1],
mMouseState, mModifiers);
}
if (!ret)
ret = mouseMotionEvent(p[0], p[1], p[0] - mMousePos[0], p[1] - mMousePos[1], mMouseState, mModifiers);
mMousePos[0] = p[0];
mMousePos[1] = p[1];
return ret;
} catch (const std::exception &e) {
std::cerr << "Caught exception in event handler: " << __FUNCTION__ << e.what() << std::endl;
return false;
}
}
bool Screen::mouseButtonCallbackEvent(int button, int action, int modifiers) {
mModifiers = modifiers;
try {
if (action != GLFW_PRESS && popup()->isVisible() && popup()->anchorItem()) {
if (!popup()->anchorItem()->containsAbs(mMousePos[0], mMousePos[1]))
popup()->hide();
}
if (action == GLFW_PRESS)
mMouseState |= 1 << button;
else
mMouseState &= ~(1 << button);
/*auto dropWidget = findItem(mMousePos[0], mMousePos[1]);
if (mDragActive && action == GLFW_RELEASE && dropWidget != mDragWidget) {
float _x, _y;
mDragWidget->parent()->absolutePosition(&_x, &_y);
mDragWidget->mouseButtonEvent(mMousePos[0] - _x, mMousePos[1] - _y, button,
false, mModifiers);
}*/
if (action == GLFW_PRESS && (button == GLFW_MOUSE_BUTTON_1 || button == GLFW_MOUSE_BUTTON_2)) {
//mDragWidget = findItem(mMousePos[0], mMousePos[1]);
//if (mDragWidget == this)
// mDragWidget = nullptr;
//mDragActive = mDragWidget != nullptr;
//if (!mDragActive)
updateFocus(nullptr);
} else {
mDragActive = false;
mDragWidget = nullptr;
}
return mouseButtonEvent(mMousePos[0], mMousePos[1], button, action == GLFW_PRESS, mModifiers);
} catch (const std::exception &) {
printf("Caught exception in event handler: %s %d\n", __FUNCTION__, action == GLFW_PRESS);
return false;
}
}
bool Screen::keyCallbackEvent(int key, int scancode, int action, int mods) {
try {
return keyboardEvent(key, scancode, action, mods);
} catch (const std::exception &e) {
std::cerr << "Caught exception in event handler: " << __FUNCTION__ << e.what() << std::endl;
return false;
}
}
bool Screen::charCallbackEvent(unsigned int codepoint) {
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::resizeCallbackEvent(int, int) {
int fbSize[2], size[2];
glfwGetFramebufferSize(mGLFWWindow, &fbSize[0], &fbSize[1]);
glfwGetWindowSize(mGLFWWindow, &size[0], &size[1]);
if ((mFBSize[0] == 0 && mFBSize[1] == 0) || (size[0] == 0 && size[1] == 0))
return false;
mFBSize[0] = fbSize[0]; mSize[0] = size[0];
mFBSize[1] = fbSize[1]; mSize[1] = size[1];
//printf("resizeCallbackEvent\n");
return false;
}
void Screen::updateFocus(Item *widget) {
for (auto w: mFocusPath) {
if (!w->focus())
continue;
w->focusEvent(false);
}
mFocusPath.clear();
while (widget) {
mFocusPath.push_back(widget);
widget = widget->parent();
}
for (auto it = mFocusPath.rbegin(); it != mFocusPath.rend(); ++it)
(*it)->focusEvent(true);
}