/
clientwindow.cpp
475 lines (393 loc) · 13.7 KB
/
clientwindow.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
/** @file clientwindow.cpp Top-level window with UI widgets.
* @ingroup base
*
* @todo Platform-specific behavior should be encapsulated in subclasses, e.g.,
* MacWindowBehavior. This would make the code easier to follow and more adaptable
* to the quirks of each platform.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2008 Jamie Jones <jamie_jones_au@yahoo.com.au>
*
* @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 "ui/clientwindow.h"
#include "clientapp.h"
#include <de/DisplayMode>
#include <de/NumberValue>
#include <QGLFormat>
#include <QCloseEvent>
#include "gl/sys_opengl.h"
#include "gl/gl_main.h"
#include "ui/widgets/legacywidget.h"
#include "ui/widgets/busywidget.h"
#include "ui/widgets/taskbarwidget.h"
#include "ui/widgets/lineeditwidget.h"
#include "ui/widgets/consolecommandwidget.h"
#include "ui/widgets/logwidget.h"
#include "ui/mouse_qt.h"
#include "dd_main.h"
#include "con_main.h"
using namespace de;
static String const LEGACY_WIDGET_NAME = "legacy";
DENG2_PIMPL(ClientWindow),
DENG2_OBSERVES(KeyEventSource, KeyEvent),
DENG2_OBSERVES(MouseEventSource, MouseStateChange),
#ifndef WIN32
DENG2_OBSERVES(MouseEventSource, MouseAxisEvent),
DENG2_OBSERVES(MouseEventSource, MouseButtonEvent),
#endif
DENG2_OBSERVES(Canvas, FocusChange)
{
bool needMainInit;
bool needRecreateCanvas;
Mode mode;
/// Root of the nomal UI widgets of this window.
GuiRootWidget root;
GuiRootWidget busyRoot;
Instance(Public *i)
: Base(i),
needMainInit(true),
needRecreateCanvas(false),
mode(Normal),
root(thisPublic),
busyRoot(thisPublic)
{
LegacyWidget *legacy = new LegacyWidget(LEGACY_WIDGET_NAME);
legacy->rule()
.setLeftTop (root.viewLeft(), root.viewTop())
.setRightBottom(root.viewRight(), root.viewBottom());
root.add(legacy);
/*
TaskBarWidget *taskBar = new TaskBarWidget;
taskBar->rule()
.setInput(Rule::Left, root.viewLeft())
.setInput(Rule::Bottom, root.viewBottom())
.setInput(Rule::Width, root.viewWidth());
root.add(taskBar);*/
ConsoleCommandWidget *test = new ConsoleCommandWidget;
test->rule()
.setInput(Rule::Left, root.viewLeft())
.setInput(Rule::Bottom, root.viewBottom())
.setInput(Rule::Width, root.viewWidth());
root.add(test);
root.setFocus(test);
LogWidget *log = new LogWidget;
log->rule()
.setInput(Rule::Left, root.viewLeft())
.setInput(Rule::Bottom, test->rule().top())
.setInput(Rule::Top, root.viewTop() + 200);
root.add(log);
// Initially the widget is disabled. It will be enabled when the window
// is visible and ready to be drawn.
legacy->disable();
// For busy mode we have an entirely different widget tree.
BusyWidget *busy = new BusyWidget;
busy->rule()
.setLeftTop (busyRoot.viewLeft(), busyRoot.viewTop())
.setRightBottom(busyRoot.viewRight(), busyRoot.viewBottom());
busyRoot.add(busy);
/// @todo The decision whether to receive input notifications from the
/// canvas is really a concern for the input drivers.
// Listen to input.
self.canvas().audienceForKeyEvent += this;
self.canvas().audienceForMouseStateChange += this;
#ifndef WIN32 // On Windows, DirectInput bypasses the mouse input from Canvas.
self.canvas().audienceForMouseAxisEvent += this;
self.canvas().audienceForMouseButtonEvent += this;
#endif
}
~Instance()
{
self.canvas().audienceForFocusChange -= this;
self.canvas().audienceForMouseStateChange -= this;
self.canvas().audienceForKeyEvent -= this;
}
void setMode(Mode const &newMode)
{
LOG_VERBOSE("Switching to %s mode") << (newMode == Busy? "Busy" : "Normal");
mode = newMode;
}
void finishMainWindowInit()
{
#ifdef MACOSX
if(self.isFullScreen())
{
// The window must be manually raised above the shielding window put up by
// the fullscreen display capture.
DisplayMode_Native_Raise(self.nativeHandle());
}
#endif
self.raise();
self.activateWindow();
// Automatically grab the mouse from the get-go if in fullscreen mode.
if(Mouse_IsPresent() && self.isFullScreen())
{
self.canvas().trapMouse();
}
self.canvas().audienceForFocusChange += this;
#ifdef WIN32
if(self.isFullScreen())
{
// It would seem we must manually give our canvas focus. Bug in Qt?
self.canvas().setFocus();
}
#endif
self.canvas().makeCurrent();
DD_FinishInitializationAfterWindowReady();
}
void keyEvent(KeyEvent const &ev)
{
/// @todo Input drivers should observe the notification instead, input
/// subsystem passes it to window system. -jk
// Pass the event onto the window system.
ClientApp::windowSystem().processEvent(ev);
}
void mouseStateChanged(MouseEventSource::State state)
{
Mouse_Trap(state == MouseEventSource::Trapped);
}
#ifndef WIN32
void mouseAxisEvent(MouseEventSource::Axis axis, Vector2i const &value)
{
switch(axis)
{
case MouseEventSource::Motion:
Mouse_Qt_SubmitMotion(IMA_POINTER, value.x, value.y);
break;
case MouseEventSource::Wheel:
Mouse_Qt_SubmitMotion(IMA_WHEEL, value.x, value.y);
break;
default:
break;
}
}
void mouseButtonEvent(MouseEventSource::Button button, MouseEventSource::ButtonState state)
{
Mouse_Qt_SubmitButton(
button == MouseEventSource::Left? IMB_LEFT :
button == MouseEventSource::Middle? IMB_MIDDLE :
button == MouseEventSource::Right? IMB_RIGHT :
button == MouseEventSource::XButton1? IMB_EXTRA1 :
button == MouseEventSource::XButton2? IMB_EXTRA2 : IMB_MAXBUTTONS,
state == MouseEventSource::Pressed);
}
#endif // !WIN32
void canvasFocusChanged(Canvas &canvas, bool hasFocus)
{
LOG_DEBUG("canvasFocusChanged focus:%b fullscreen:%b hidden:%b minimized:%b")
<< hasFocus << self.isFullScreen() << self.isHidden() << self.isMinimized();
if(!hasFocus)
{
DD_ClearEvents();
I_ResetAllDevices();
canvas.trapMouse(false);
}
else if(self.isFullScreen())
{
// Trap the mouse again in fullscreen mode.
canvas.trapMouse();
}
// Generate an event about this.
ddevent_t ev;
ev.type = E_FOCUS;
ev.focus.gained = hasFocus;
ev.focus.inWindow = 1; /// @todo Ask WindowSystem for an identifier number.
DD_PostEvent(&ev);
}
};
ClientWindow::ClientWindow(String const &id)
: PersistentCanvasWindow(id), d(new Instance(this))
{
canvas().audienceForGLResize += this;
canvas().audienceForGLInit += this;
#ifdef WIN32
// Set an icon for the window.
Path iconPath = DENG2_APP->nativeBasePath() / "data\\graphics\\doomsday.ico";
LOG_DEBUG("Window icon: ") << NativePath(iconPath).pretty();
setWindowIcon(QIcon(iconPath));
#endif
}
GuiRootWidget &ClientWindow::root()
{
return d->mode == Busy? d->busyRoot : d->root;
}
void ClientWindow::setMode(Mode const &mode)
{
LOG_AS("ClientWindow");
d->setMode(mode);
}
void ClientWindow::closeEvent(QCloseEvent *ev)
{
LOG_DEBUG("Window is about to close, executing 'quit'.");
/// @todo autosave and quit?
Con_Execute(CMDS_DDAY, "quit", true, false);
// We are not authorizing immediate closing of the window;
// engine shutdown will take care of it later.
ev->ignore(); // don't close
}
void ClientWindow::canvasGLReady(Canvas &canvas)
{
// Update the capability flags.
GL_state.features.multisample = canvas.format().sampleBuffers();
LOG_DEBUG("GL feature: Multisampling: %b") << GL_state.features.multisample;
PersistentCanvasWindow::canvasGLReady(canvas);
// Now that the Canvas is ready for drawing we can enable the LegacyWidget.
d->root.find(LEGACY_WIDGET_NAME)->enable();
LOG_DEBUG("LegacyWidget enabled");
if(d->needMainInit)
{
d->needMainInit = false;
d->finishMainWindowInit();
}
}
void ClientWindow::canvasGLInit(Canvas &)
{
Sys_GLConfigureDefaultState();
GL_Init2DState();
}
void ClientWindow::canvasGLDraw(Canvas &canvas)
{
// All of this occurs during the Canvas paintGL event.
ClientApp::app().preFrame(); /// @todo what about multiwindow?
DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
root().draw();
// Finish GL drawing and swap it on to the screen. Blocks until buffers
// swapped.
GL_DoUpdate();
ClientApp::app().postFrame(); /// @todo what about multiwindow?
PersistentCanvasWindow::canvasGLDraw(canvas);
}
void ClientWindow::canvasGLResized(Canvas &canvas)
{
LOG_AS("ClientWindow");
Canvas::Size size = canvas.size();
LOG_DEBUG("Canvas resized to ") << size.asText();
// Tell the widgets.
d->root.setViewSize(size);
d->busyRoot.setViewSize(size);
}
bool ClientWindow::setDefaultGLFormat() // static
{
LOG_AS("DefaultGLFormat");
// Configure the GL settings for all subsequently created canvases.
QGLFormat fmt;
fmt.setDepthBufferSize(16);
fmt.setStencilBufferSize(8);
fmt.setDoubleBuffer(true);
if(CommandLine_Exists("-novsync") || !Con_GetByte("vid-vsync"))
{
fmt.setSwapInterval(0); // vsync off
LOG_DEBUG("vsync off");
}
else
{
fmt.setSwapInterval(1);
LOG_DEBUG("vsync on");
}
// The value of the "vid-fsaa" variable is written to this settings
// key when the value of the variable changes.
bool configured = de::App::config().getb("window.fsaa");
if(CommandLine_Exists("-nofsaa") || !configured)
{
fmt.setSampleBuffers(false);
LOG_DEBUG("multisampling off");
}
else
{
fmt.setSampleBuffers(true); // multisampling on (default: highest available)
//fmt.setSamples(4);
LOG_DEBUG("multisampling on (max)");
}
if(fmt != QGLFormat::defaultFormat())
{
LOG_DEBUG("Applying new format...");
QGLFormat::setDefaultFormat(fmt);
return true;
}
else
{
LOG_DEBUG("New format is the same as before.");
return false;
}
}
void ClientWindow::draw()
{
// Don't run the main loop until after the paint event has been dealt with.
ClientApp::app().loop().pause();
// The canvas needs to be recreated when the GL format has changed
// (e.g., multisampling).
if(d->needRecreateCanvas)
{
d->needRecreateCanvas = false;
if(setDefaultGLFormat())
{
recreateCanvas();
// Wait until the new Canvas is ready (note: loop remains paused!).
return;
}
}
if(shouldRepaintManually())
{
DENG_ASSERT_GL_CONTEXT_ACTIVE();
// Perform the drawing manually right away.
canvas().updateGL();
}
else
{
// Request update at the earliest convenience.
canvas().update();
}
}
bool ClientWindow::shouldRepaintManually() const
{
// When the mouse is not trapped, allow the system to regulate window
// updates (e.g., for window manipulation).
if(isFullScreen()) return true;
return !Mouse_IsPresent() || canvas().isMouseTrapped();
}
void ClientWindow::grab(image_t &img, bool halfSized) const
{
DENG_ASSERT_IN_MAIN_THREAD();
QSize outputSize = (halfSized? QSize(width()/2, height()/2) : QSize());
QImage grabbed = canvas().grabImage(outputSize);
Image_Init(&img);
img.size.width = grabbed.width();
img.size.height = grabbed.height();
img.pixelSize = grabbed.depth()/8;
img.pixels = (uint8_t *) malloc(grabbed.byteCount());
memcpy(img.pixels, grabbed.constBits(), grabbed.byteCount());
LOG_DEBUG("Canvas: grabbed %i x %i, byteCount:%i depth:%i format:%i")
<< grabbed.width() << grabbed.height()
<< grabbed.byteCount() << grabbed.depth() << grabbed.format();
DENG_ASSERT(img.pixelSize != 0);
}
void ClientWindow::updateCanvasFormat()
{
d->needRecreateCanvas = true;
// Save the relevant format settings.
App::config().set("window.fsaa", Con_GetByte("vid-fsaa") != 0);
}
ClientWindow &ClientWindow::main()
{
return static_cast<ClientWindow &>(PersistentCanvasWindow::main());
}
#if defined(UNIX) && !defined(MACOSX)
void GL_AssertContextActive()
{
DENG_ASSERT(QGLContext::currentContext() != 0);
}
#endif