-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
canvas.cpp
428 lines (353 loc) · 10.7 KB
/
canvas.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
/** @file canvas.cpp OpenGL drawing surface (QWidget).
*
* @authors Copyright (c) 2012-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program 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 General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "de/Canvas"
#include "de/CanvasWindow"
#include "de/GLState"
#include "de/gui/opengl.h"
#include <de/App>
#include <de/Log>
#include <QApplication>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QShowEvent>
#include <QResizeEvent>
#include <QPaintEvent>
#include <QImage>
#include <QCursor>
#include <QTimer>
#include <QTime>
#include <QDebug>
namespace de {
static const int MOUSE_WHEEL_CONTINUOUS_THRESHOLD_MS = 100;
DENG2_PIMPL(Canvas)
{
GLTarget target;
CanvasWindow *parent;
bool readyNotified;
Size currentSize;
bool mouseDisabled;
bool mouseGrabbed;
#ifdef WIN32
bool altIsDown;
#endif
QPoint prevMousePos;
QTime prevWheelAt;
int wheelDir[2];
Instance(Public *i, CanvasWindow *parentWindow)
: Base(i),
parent(parentWindow),
readyNotified(false),
mouseDisabled(false),
mouseGrabbed(false)
{
wheelDir[0] = wheelDir[1] = 0;
#ifdef WIN32
altIsDown = false;
#endif
mouseDisabled = App::commandLine().has("-nomouse");
}
void grabMouse()
{
if(!self.isVisible() || mouseDisabled) return;
LOG_DEBUG("grabbing mouse (already grabbed? %b)") << mouseGrabbed;
if(!mouseGrabbed)
{
mouseGrabbed = true;
DENG2_FOR_PUBLIC_AUDIENCE(MouseStateChange, i)
{
i->mouseStateChanged(Trapped);
}
}
}
void ungrabMouse()
{
if(!self.isVisible() || mouseDisabled) return;
LOG_DEBUG("ungrabbing mouse (presently grabbed? %b)") << mouseGrabbed;
if(mouseGrabbed)
{
// Tell the mouse driver that the mouse is untrapped.
mouseGrabbed = false;
DENG2_FOR_PUBLIC_AUDIENCE(MouseStateChange, i)
{
i->mouseStateChanged(Untrapped);
}
}
}
static int nativeCode(QKeyEvent const *ev)
{
#if defined(UNIX) && !defined(MACOSX)
return ev->nativeScanCode();
#else
return ev->nativeVirtualKey();
#endif
}
void handleKeyEvent(QKeyEvent *ev)
{
//LOG_AS("Canvas");
ev->accept();
if(ev->isAutoRepeat()) return; // Ignore repeats, we do our own.
/*
qDebug() << "Canvas: key press" << ev->key() << QString("0x%1").arg(ev->key(), 0, 16)
<< "text:" << ev->text()
<< "native:" << ev->nativeVirtualKey()
<< "scancode:" << ev->nativeScanCode();
*/
#ifdef WIN32
// We must track the state of the alt key ourselves as the OS grabs the up event...
if(ev->key() == Qt::Key_Alt)
{
if(ev->type() == QEvent::KeyPress)
{
if(altIsDown) return; // Ignore repeat down events(!)?
altIsDown = true;
}
else if(ev->type() == QEvent::KeyRelease)
{
if(!altIsDown)
{
LOG_DEBUG("Ignoring repeat alt up.");
return; // Ignore repeat up events.
}
altIsDown = false;
//LOG_DEBUG("Alt is up.");
}
}
#endif
DENG2_FOR_PUBLIC_AUDIENCE(KeyEvent, i)
{
i->keyEvent(KeyEvent(ev->type() == QEvent::KeyPress? KeyEvent::Pressed :
KeyEvent::Released,
ev->key(),
KeyEvent::ddKeyFromQt(ev->key(), ev->nativeVirtualKey(), ev->nativeScanCode()),
nativeCode(ev),
ev->text()));
}
}
};
Canvas::Canvas(CanvasWindow* parent, QGLWidget* shared)
: QGLWidget(parent, shared), d(new Instance(this, parent))
{
LOG_AS("Canvas");
LOG_DEBUG("swap interval: ") << format().swapInterval();
LOG_DEBUG("multisample: %b") << format().sampleBuffers();
// We will be doing buffer swaps manually (for timing purposes).
setAutoBufferSwap(false);
setFocusPolicy(Qt::StrongFocus);
}
void Canvas::setParent(CanvasWindow *parent)
{
d->parent = parent;
}
QImage Canvas::grabImage(QSize const &outputSize)
{
// We will be grabbing the visible, latest complete frame.
glReadBuffer(GL_FRONT);
QImage grabbed = grabFrameBuffer(); // no alpha
glReadBuffer(GL_BACK);
if(outputSize.isValid())
{
grabbed = grabbed.scaled(outputSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}
return grabbed;
}
GLuint Canvas::grabAsTexture(QSize const &outputSize)
{
return bindTexture(grabImage(outputSize), GL_TEXTURE_2D, GL_RGB,
QGLContext::LinearFilteringBindOption);
}
Canvas::Size Canvas::size() const
{
return d->currentSize;
}
void Canvas::trapMouse(bool trap)
{
if(d->mouseDisabled) return;
if(trap)
{
d->grabMouse();
}
else
{
d->ungrabMouse();
}
}
bool Canvas::isMouseTrapped() const
{
return d->mouseGrabbed;
}
void Canvas::copyAudiencesFrom(Canvas const &other)
{
audienceForGLReady = other.audienceForGLReady;
audienceForGLInit = other.audienceForGLInit;
audienceForGLResize = other.audienceForGLResize;
audienceForGLDraw = other.audienceForGLDraw;
audienceForFocusChange = other.audienceForFocusChange;
audienceForKeyEvent = other.audienceForKeyEvent;
audienceForMouseStateChange = other.audienceForMouseStateChange;
audienceForMouseAxisEvent = other.audienceForMouseAxisEvent;
audienceForMouseButtonEvent = other.audienceForMouseButtonEvent;
}
GLTarget &Canvas::renderTarget() const
{
return d->target;
}
void Canvas::initializeGL()
{
LOG_AS("Canvas");
LOG_DEBUG("Notifying GL init (during paint)");
DENG2_FOR_AUDIENCE(GLInit, i) i->canvasGLInit(*this);
}
void Canvas::resizeGL(int w, int h)
{
Size newSize(max(0, w), max(0, h));
// Only react if this is actually a resize.
if(d->currentSize != newSize)
{
d->currentSize = newSize;
DENG2_FOR_AUDIENCE(GLResize, i) i->canvasGLResized(*this);
}
}
void Canvas::showEvent(QShowEvent* ev)
{
LOG_AS("Canvas");
QGLWidget::showEvent(ev);
// The first time the window is shown, run the initialization callback. On
// some platforms, OpenGL is not fully ready to be used before the window
// actually appears on screen.
if(isVisible() && !d->readyNotified)
{
LOG_DEBUG("Received first show event, scheduling GL ready notification");
#ifdef WIN32
makeCurrent();
getAllOpenGLEntryPoints();
doneCurrent();
#endif
QTimer::singleShot(1, this, SLOT(notifyReady()));
}
}
void Canvas::notifyReady()
{
if(d->readyNotified) return;
d->readyNotified = true;
LOG_AS("Canvas");
LOG_DEBUG("Notifying GL ready");
DENG2_FOR_AUDIENCE(GLReady, i) i->canvasGLReady(*this);
// This Canvas instance might have been destroyed now.
}
void Canvas::paintGL()
{
// Make sure any changes to the state stack become effective.
GLState::top().apply();
DENG2_FOR_AUDIENCE(GLDraw, i) i->canvasGLDraw(*this);
}
void Canvas::focusInEvent(QFocusEvent*)
{
LOG_AS("Canvas");
LOG_INFO("Gained focus.");
DENG2_FOR_AUDIENCE(FocusChange, i) i->canvasFocusChanged(*this, true);
}
void Canvas::focusOutEvent(QFocusEvent*)
{
LOG_AS("Canvas");
LOG_INFO("Lost focus.");
// Automatically ungrab the mouse if focus is lost.
d->ungrabMouse();
DENG2_FOR_AUDIENCE(FocusChange, i) i->canvasFocusChanged(*this, false);
}
void Canvas::keyPressEvent(QKeyEvent *ev)
{
d->handleKeyEvent(ev);
}
void Canvas::keyReleaseEvent(QKeyEvent *ev)
{
d->handleKeyEvent(ev);
}
static MouseEventSource::Button translateButton(Qt::MouseButton btn)
{
if(btn == Qt::LeftButton) return MouseEventSource::Left;
#ifdef DENG2_QT_4_7_OR_NEWER
if(btn == Qt::MiddleButton) return MouseEventSource::Middle;
#else
if(btn == Qt::MidButton) return MouseEventSource::Middle;
#endif
if(btn == Qt::RightButton) return MouseEventSource::Right;
if(btn == Qt::XButton1) return MouseEventSource::XButton1;
if(btn == Qt::XButton2) return MouseEventSource::XButton2;
return MouseEventSource::Unknown;
}
void Canvas::mousePressEvent(QMouseEvent *ev)
{
if(!d->mouseGrabbed)
{
// The mouse will be grabbed when the button is released.
ev->ignore();
return;
}
ev->accept();
DENG2_FOR_AUDIENCE(MouseButtonEvent, i)
{
i->mouseButtonEvent(translateButton(ev->button()), MouseEventSource::Pressed);
}
}
void Canvas::mouseReleaseEvent(QMouseEvent* ev)
{
if(d->mouseDisabled)
{
ev->ignore();
return;
}
ev->accept();
if(!d->mouseGrabbed)
{
// Start grabbing after a click.
trapMouse();
return;
}
DENG2_FOR_AUDIENCE(MouseButtonEvent, i)
{
i->mouseButtonEvent(translateButton(ev->button()), MouseEventSource::Released);
}
}
void Canvas::wheelEvent(QWheelEvent *ev)
{
if(d->mouseDisabled)
{
ev->ignore();
return;
}
ev->accept();
bool continuousMovement = (d->prevWheelAt.elapsed() < MOUSE_WHEEL_CONTINUOUS_THRESHOLD_MS);
int axis = (ev->orientation() == Qt::Horizontal? 0 : 1);
int dir = (ev->delta() < 0? -1 : 1);
if(!continuousMovement || d->wheelDir[axis] != dir)
{
d->wheelDir[axis] = dir;
//qDebug() << "Canvas: signal wheel axis" << axis << "dir" << dir;
DENG2_FOR_AUDIENCE(MouseAxisEvent, i)
{
i->mouseAxisEvent(MouseEventSource::Wheel,
axis == 0? Vector2i(dir, 0) :
axis == 1? Vector2i(0, dir) :
Vector2i());
}
}
d->prevWheelAt.start();
}
} // namespace de