/
clientwindow.cpp
1319 lines (1105 loc) · 38 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
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @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-2014 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 "ui/clientrootwidget.h"
#include "clientapp.h"
#include <QGLFormat>
#include <QCloseEvent>
#include <de/CompositorWidget>
#include <de/Config>
#include <de/ConstantRule>
#include <de/DisplayMode>
#include <de/Drawable>
#include <de/FadeToBlackWidget>
#include <de/GLFramebuffer>
#include <de/GLState>
#include <de/NotificationAreaWidget>
#include <de/NumberValue>
#include <de/SignalAction>
#include <de/VRWindowTransform>
#include <de/concurrency.h>
#include <doomsday/console/exec.h>
#include "api_console.h"
#include "gl/sys_opengl.h"
#include "gl/gl_main.h"
#include "ui/widgets/gamewidget.h"
#include "ui/widgets/gameuiwidget.h"
#include "ui/widgets/busywidget.h"
#include "ui/widgets/taskbarwidget.h"
#include "ui/widgets/consolewidget.h"
#include "ui/widgets/privilegedlogwidget.h"
#include "ui/home/homewidget.h"
#include "ui/dialogs/coloradjustmentdialog.h"
#include "ui/dialogs/alertdialog.h"
#include "ui/inputdevice.h"
#include "CommandAction"
#include "ui/mouse_qt.h"
#include "dd_main.h"
#include "render/vr.h"
using namespace de;
static inline InputSystem &inputSys()
{
return ClientApp::inputSystem();
}
static ClientWindow *mainWindow = nullptr; // The main window, set after fully constructed.
DENG2_PIMPL(ClientWindow)
, DENG2_OBSERVES(App, StartupComplete)
, DENG2_OBSERVES(DoomsdayApp, GameChange)
, DENG2_OBSERVES(MouseEventSource, MouseStateChange)
, DENG2_OBSERVES(Canvas, FocusChange)
, DENG2_OBSERVES(Canvas, GLInit)
, DENG2_OBSERVES(Canvas, GLResize)
, DENG2_OBSERVES(Variable, Change)
{
bool needMainInit = true;
bool needRecreateCanvas = false;
bool needRootSizeUpdate = false;
Mode mode = Normal;
/// Root of the nomal UI widgets of this window.
ClientRootWidget root;
//CompositorWidget *compositor = nullptr;
GameWidget *game = nullptr;
GameUIWidget *gameUI = nullptr;
LabelWidget *nowPlaying = nullptr;
TaskBarWidget *taskBar = nullptr;
LabelWidget *taskBarBlur = nullptr; ///< Blur everything below the task bar.
NotificationAreaWidget *notifications = nullptr;
ButtonWidget *quitButton = nullptr;
AlertDialog *alerts = nullptr;
ColorAdjustmentDialog *colorAdjust = nullptr;
HomeWidget *home = nullptr;
SafeWidgetPtr<FadeToBlackWidget> fader;
BusyWidget *busy = nullptr;
GuiWidget *sidebar = nullptr;
PrivilegedLogWidget *privLog = nullptr;
LabelWidget *cursor = nullptr;
ConstantRule *cursorX;
ConstantRule *cursorY;
AnimationRule *gameWidth;
AnimationRule *gameHeight;
AnimationRule *homeDelta;
AnimationRule *quitX;
bool cursorHasBeenHidden = false;
bool isGameMini = false;
// FPS notifications.
UniqueWidgetPtr<LabelWidget> fpsCounter;
float oldFps = 0;
/// @todo Switch dynamically between VR and plain.
VRWindowTransform contentXf;
Impl(Public *i)
: Base (i)
, root (i)
, cursorX (new ConstantRule(0))
, cursorY (new ConstantRule(0))
, gameWidth (new AnimationRule(0, Animation::EaseBoth))
, gameHeight(new AnimationRule(0, Animation::EaseBoth))
, homeDelta (new AnimationRule(0, Animation::EaseBoth))
, quitX (new AnimationRule(0, Animation::EaseBoth))
, contentXf (*i)
{
self.setTransform(contentXf);
/// @todo The decision whether to receive input notifications from the
/// canvas is really a concern for the input drivers.
DoomsdayApp::app().audienceForGameChange() += this;
App::app().audienceForStartupComplete() += this;
// Listen to input.
self.canvas().audienceForMouseStateChange() += this;
foreach (String s, configVariableNames())
{
App::config(s).audienceForChange() += this;
}
}
~Impl()
{
releaseRef(cursorX);
releaseRef(cursorY);
releaseRef(gameWidth);
releaseRef(gameHeight);
releaseRef(homeDelta);
releaseRef(quitX);
if (thisPublic == mainWindow)
{
mainWindow = nullptr;
}
}
StringList configVariableNames() const
{
return StringList()
<< self.configName("fsaa")
<< self.configName("vsync");
}
void setupUI()
{
Style &style = ClientApp::windowSystem().style();
gameWidth ->set(root.viewWidth());
gameHeight->set(root.viewHeight());
game = new GameWidget;
game->rule().setInput(Rule::Left, root.viewLeft())
.setInput(Rule::Right, root.viewLeft() + *gameWidth)
.setInput(Rule::Bottom, root.viewBottom())
.setInput(Rule::Height, *gameHeight);
// Initially the widget is disabled. It will be enabled when the window
// is visible and ready to be drawn.
game->disable();
root.add(game);
gameUI = new GameUIWidget;
gameUI->rule().setRect(game->rule());
gameUI->disable();
root.add(gameUI);
auto *miniGameControls = new LabelWidget;
{
miniGameControls->set(GuiWidget::Background(style.colors().colorf("inverted.background")));
miniGameControls->rule()
.setInput(Rule::Width, root.viewWidth()/2 - style.rules().rule("unit"))
.setInput(Rule::Left, game->rule().right() + style.rules().rule("unit"))
.setInput(Rule::Top, game->rule().top());
ButtonWidget *backToGame = new ButtonWidget;
backToGame->setText("Back to Game");
backToGame->setSizePolicy(ui::Expand, ui::Expand);
backToGame->setActionFn([this] () { home->moveOffscreen(1.0); });
nowPlaying = new LabelWidget;
nowPlaying->setSizePolicy(ui::Expand, ui::Expand);
nowPlaying->margins().setLeftRight("").setBottom("dialog.gap");
nowPlaying->setMaximumTextWidth(miniGameControls->rule().width());
nowPlaying->setTextLineAlignment(ui::AlignLeft);
nowPlaying->setTextColor("inverted.text");
nowPlaying->setFont("heading");
AutoRef<Rule> combined = nowPlaying->rule().height() + backToGame->rule().height();
nowPlaying->rule()
.setMidAnchorX(miniGameControls->rule().midX())
.setInput(Rule::Top, miniGameControls->rule().midY() - combined/2);
backToGame->rule()
.setInput(Rule::Left, nowPlaying->rule().left())
.setInput(Rule::Top, nowPlaying->rule().bottom());
miniGameControls->add(nowPlaying);
miniGameControls->add(backToGame);
root.add(miniGameControls);
}
// Busy widget shows progress indicator and frozen game content.
busy = new BusyWidget;
busy->hide(); // normally hidden
busy->rule().setRect(root.viewRule());
root.add(busy);
home = new HomeWidget;
home->rule().setInput(Rule::Left, root.viewLeft())
.setInput(Rule::Right, root.viewRight())
.setInput(Rule::Top, root.viewTop())
.setInput(Rule::Bottom, root.viewBottom() + *homeDelta);
root.add(home);
// Busy progress should be visible over the Home.
busy->progress().orphan();
busy->progress().rule().setRect(game->rule());
root.add(&busy->progress());
// Common notification area.
notifications = new NotificationAreaWidget;
notifications->useDefaultPlacement(root.viewRule(), *quitX);
root.add(notifications);
// Quit shortcut.
quitButton = new ButtonWidget;
quitButton->setText(_E(b)_E(D) + tr("QUIT"));
quitButton->setFont("small");
quitButton->setSizePolicy(ui::Expand, ui::Fixed);
quitButton->setStyleImage("close.ringless", "default");
quitButton->setImageColor(style.colors().colorf("accent"));
quitButton->setTextAlignment(ui::AlignLeft);
quitButton->set(GuiWidget::Background(style.colors().colorf("background")));
quitButton->setActionFn([] () { DENG2_BASE_GUI_APP->quit(); });
quitButton->rule()
.setInput(Rule::Top, root.viewTop() + style.rules().rule("gap"))
.setInput(Rule::Left, root.viewRight() + *quitX)
.setInput(Rule::Height, style.fonts().font("default").height() +
style.rules().rule("gap") * 2);
root.add(quitButton);
// Alerts notification and popup.
alerts = new AlertDialog;
root.add(alerts);
// FPS counter for the notification area.
fpsCounter.reset(new LabelWidget);
fpsCounter->setSizePolicy(ui::Expand, ui::Expand);
fpsCounter->setAlignment(ui::AlignRight);
// Everything behind the task bar can be blurred with this widget.
taskBarBlur = new LabelWidget("taskbar-blur");
taskBarBlur->set(GuiWidget::Background(Vector4f(1, 1, 1, 1), GuiWidget::Background::Blurred));
taskBarBlur->rule().setRect(root.viewRule());
taskBarBlur->setAttribute(GuiWidget::DontDrawContent);
root.add(taskBarBlur);
// Taskbar is over almost everything else.
taskBar = new TaskBarWidget;
taskBar->rule()
.setInput(Rule::Left, root.viewLeft())
.setInput(Rule::Bottom, root.viewBottom() + taskBar->shift())
.setInput(Rule::Width, root.viewWidth());
root.add(taskBar);
miniGameControls->rule().setInput(Rule::Bottom, taskBar->rule().top());
#if 0
// The game selection's height depends on the taskbar.
AutoRef<Rule> availHeight = taskBar->rule().top() - gameSelMenu->filter().rule().height();
gameSelMenu->rule()
.setInput(Rule::AnchorY, gameSelMenu->filter().rule().height() + availHeight / 2)
.setInput(Rule::Height, OperatorRule::minimum(availHeight,
gameSelMenu->contentRule().height() + gameSelMenu->margins().height()));
#endif
// Privileged work-in-progress log.
privLog = new PrivilegedLogWidget;
privLog->rule().setRect(root.viewRule());
root.add(privLog);
// Color adjustment dialog.
colorAdjust = new ColorAdjustmentDialog;
colorAdjust->setAnchor(root.viewWidth() / 2, root.viewTop());
colorAdjust->setOpeningDirection(ui::Down);
root.add(colorAdjust);
taskBar->hide();
// Mouse cursor is used with transformed content.
cursor = new LabelWidget;
cursor->setBehavior(Widget::Unhittable);
cursor->margins().set(""); // no margins
cursor->setImage(style.images().image("window.cursor"));
cursor->setAlignment(ui::AlignTopLeft);
cursor->rule()
.setSize(Const(48), Const(48))
.setLeftTop(*cursorX, *cursorY);
cursor->hide();
root.add(cursor);
/*
#ifdef DENG2_DEBUG
auto debugPrint = [] (Widget &w)
{
int depth = 0;
for (auto *p = w.parent(); p; p = p->parent()) ++depth;
qDebug() << QByteArray(4 * depth, ' ').constData()
<< (w.parent()? w.parent()->children().indexOf(&w) : -1)
<< w.name() << "childCount:" << w.childCount();
};
root.walkInOrder(Widget::Forward, [&debugPrint] (Widget &w)
{
debugPrint(w);
return LoopContinue;
});
qDebug() << "AND BACKWARDS:";
root.children().last()->walkInOrder(Widget::Backward, [&debugPrint] (Widget &w)
{
debugPrint(w);
return LoopContinue;
});
#endif
*/
}
void appStartupCompleted()
{
taskBar->show();
// Show the tutorial if it hasn't been automatically shown yet.
if (!App::config().getb("tutorial.shown", false))
{
App::config().set("tutorial.shown", true);
LOG_NOTE("Starting tutorial (not shown before)");
QTimer::singleShot(500, taskBar, SLOT(showTutorial()));
}
else
{
taskBar->close();
}
}
void currentGameChanged(Game const &newGame)
{
minimizeGame(false);
showOrHideQuitButton();
if (!newGame.isNull())
{
nowPlaying->setText(_E(l) + tr("Now playing") + "\n" + _E(b) +
DoomsdayApp::currentGameProfile()->name());
}
// Check with Style if blurring is allowed.
taskBar->console().enableBlur(taskBar->style().isBlurringAllowed());
self.hideTaskBarBlur(); // update background blur mode
activateOculusRiftModeIfConnected();
}
void activateOculusRiftModeIfConnected()
{
if (vrCfg().oculusRift().isHMDConnected() && vrCfg().mode() != VRConfig::OculusRift)
{
LOG_NOTE("HMD connected, automatically switching to Oculus Rift mode");
Con_SetInteger("rend-vr-mode", VRConfig::OculusRift);
vrCfg().oculusRift().moveWindowToScreen(OculusRift::HMDScreen);
}
else if (!vrCfg().oculusRift().isHMDConnected() && vrCfg().mode() == VRConfig::OculusRift)
{
LOG_NOTE("HMD not connected, disabling VR mode");
Con_SetInteger("rend-vr-mode", VRConfig::Mono);
vrCfg().oculusRift().moveWindowToScreen(OculusRift::DefaultScreen);
}
}
void setMode(Mode const &newMode)
{
LOG_DEBUG("Switching to %s mode") << (newMode == Busy? "Busy" : "Normal");
// Hide and show widgets as appropriate.
switch (newMode)
{
case Busy:
game->hide();
game->disable();
gameUI->hide();
gameUI->disable();
taskBar->disable();
busy->show();
busy->enable();
break;
case Normal:
// The busy widget will hide itself after a possible transition has finished.
busy->disable();
game->show();
game->enable();
gameUI->show();
gameUI->enable();
taskBar->enable();
break;
}
mode = newMode;
}
void canvasGLInit(Canvas &)
{
Sys_GLConfigureDefaultState();
GL_Init2DState();
}
void canvasGLResized(Canvas &canvas)
{
LOG_AS("ClientWindow");
Canvas::Size size = canvas.size();
LOG_TRACE("Canvas resized to ") << size.asText();
GLState::current().setViewport(Rectangleui(0, 0, size.x, size.y));
updateRootSize();
// Show or hide the Quit button depending on the window mode.
showOrHideQuitButton();
}
void showOrHideQuitButton()
{
TimeDelta const SPAN = 0.6;
if (self.isFullScreen() && !DoomsdayApp::isGameLoaded())
{
quitX->set(-quitButton->rule().width() - Style::get().rules().rule("gap"), SPAN);
}
else
{
quitX->set(0, SPAN);
}
}
/**
* Completes the initialization of the main window. This is called only after the
* window is created and visible, so that OpenGL operations and actions on the native
* window can be performed without restrictions.
*/
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();
vrCfg().oculusRift().glPreInit();
contentXf.glInit();
}
void mouseStateChanged(MouseEventSource::State state)
{
Mouse_Trap(state == MouseEventSource::Trapped);
}
/**
* Handles an event that BaseWindow (and thus WindowSystem) didn't have use for.
*
* @param event Event to handle.
*/
bool handleFallbackEvent(Event const &ev)
{
if (MouseEvent const *mouse = ev.maybeAs<MouseEvent>())
{
// Fall back to legacy handling.
switch (ev.type())
{
case Event::MouseButton:
if (game->hitTest(ev))
{
Mouse_Qt_SubmitButton(
mouse->button() == MouseEvent::Left? IMB_LEFT :
mouse->button() == MouseEvent::Middle? IMB_MIDDLE :
mouse->button() == MouseEvent::Right? IMB_RIGHT :
mouse->button() == MouseEvent::XButton1? IMB_EXTRA1 :
mouse->button() == MouseEvent::XButton2? IMB_EXTRA2 : IMB_MAXBUTTONS,
mouse->state() == MouseEvent::Pressed);
return true;
}
break;
case Event::MouseMotion:
Mouse_Qt_SubmitMotion(IMA_POINTER, mouse->pos().x, mouse->pos().y);
return true;
case Event::MouseWheel:
if (game->hitTest(ev) && mouse->wheelMotion() == MouseEvent::Step)
{
// The old input system can only do wheel step events.
Mouse_Qt_SubmitMotion(IMA_WHEEL, mouse->wheel().x, mouse->wheel().y);
return true;
}
break;
default:
break;
}
}
return false;
}
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)
{
inputSys().forAllDevices([] (InputDevice &device)
{
device.reset();
return LoopContinue;
});
inputSys().clearEvents();
canvas.trapMouse(false);
}
else if (self.isFullScreen() && !taskBar->isOpen() && DoomsdayApp::isGameLoaded())
{
// Trap the mouse again in fullscreen mode.
canvas.trapMouse();
}
// Generate an event about this.
ddevent_t ev; de::zap(ev);
ev.device = -1;
ev.type = E_FOCUS;
ev.focus.gained = hasFocus;
ev.focus.inWindow = 1; /// @todo Ask WindowSystem for an identifier number.
inputSys().postEvent(&ev);
}
void updateFpsNotification(float fps)
{
notifications->showOrHide(*fpsCounter, self.isFPSCounterVisible());
if (!fequal(oldFps, fps))
{
fpsCounter->setText(QString("%1 " _E(l) + tr("FPS")).arg(fps, 0, 'f', 1));
oldFps = fps;
}
}
void variableValueChanged(Variable &variable, Value const &newValue)
{
if (variable.name() == "fsaa")
{
self.updateCanvasFormat();
}
else if (variable.name() == "vsync")
{
#ifdef WIN32
self.updateCanvasFormat();
DENG2_UNUSED(newValue);
#else
GL_SetVSync(newValue.isTrue());
#endif
}
}
void installSidebar(SidebarLocation location, GuiWidget *widget)
{
// Get rid of the old sidebar.
if (sidebar)
{
uninstallSidebar(location);
}
if (!widget) return;
// Maximize game to hide the "Now playing" controls.
home->moveOffscreen(1.0);
DENG2_ASSERT(sidebar == NULL);
// Attach the widget.
switch (location)
{
case RightEdge:
widget->rule()
.setInput(Rule::Top, root.viewTop())
.setInput(Rule::Right, root.viewRight())
.setInput(Rule::Bottom, taskBar->rule().top());
game->rule()
.setInput(Rule::Right, widget->rule().left());
notifications->rule()
.setInput(Rule::Right, widget->rule().left() - Style::get().rules().rule("gap"));
/*gameUI->rule()
.setInput(Rule::Right, widget->rule().left());*/
break;
}
sidebar = widget;
root.insertBefore(sidebar, *notifications);
}
void uninstallSidebar(SidebarLocation location)
{
DENG2_ASSERT(sidebar != NULL);
switch (location)
{
case RightEdge:
game->rule().setInput(Rule::Right, root.viewLeft() + *gameWidth);
notifications->useDefaultPlacement(root.viewRule(), *quitX);
break;
}
root.remove(*sidebar);
sidebar->guiDeleteLater();
sidebar = 0;
}
enum DeferredTaskResult {
Continue,
AbortFrame
};
DeferredTaskResult performDeferredTasks()
{
if (BusyMode_Active())
{
// Let's not do anything risky in busy mode.
return Continue;
}
// The canvas needs to be recreated when the GL format has changed
// (e.g., multisampling).
if (needRecreateCanvas)
{
needRecreateCanvas = false;
if (self.setDefaultGLFormat())
{
self.recreateCanvas();
// Wait until the new Canvas is ready (note: loop remains paused!).
return AbortFrame;
}
}
return Continue;
}
void updateRootSize()
{
DENG_ASSERT_IN_MAIN_THREAD();
needRootSizeUpdate = false;
Vector2ui const size = contentXf.logicalRootSize(self.canvas().size());
// Tell the widgets.
root.setViewSize(size);
}
/* void enableCompositor(bool enable)
{
DENG_ASSERT_IN_MAIN_THREAD();
if ((enable && compositor) || (!enable && !compositor))
{
return;
}
// All the children of the compositor need to be relocated.
container().remove(*gameUI);
container().remove(*home);
if (sidebar) container().remove(*sidebar);
container().remove(*notifications);
container().remove(*taskBarBlur);
container().remove(*taskBar);
container().remove(*privLog);
container().remove(*cursor);
Widget::Children additional;
// Relocate all popups to the new container (which need to stay on top).
foreach (Widget *w, container().children())
{
if (PopupWidget *pop = w->maybeAs<PopupWidget>())
{
additional.append(pop);
container().remove(*pop);
}
}
if (enable && !compositor)
{
LOG_GL_VERBOSE("Offscreen UI composition enabled");
compositor = new CompositorWidget;
compositor->rule().setRect(root.viewRule());
root.add(compositor);
}
else
{
DENG2_ASSERT(compositor != 0);
DENG2_ASSERT(!compositor->childCount());
root.remove(*compositor);
compositor->guiDeleteLater();
compositor = 0;
LOG_GL_VERBOSE("Offscreen UI composition disabled");
}
container().add(gameUI);
if (&container() == &root)
{
// Make sure the game UI doesn't show up over the busy transition.
container().moveChildBefore(gameUI, *busy);
}
container().add(home);
//container().add(gameSelMenu);
//container().add(iwadNotice);
if (sidebar) container().add(sidebar);
container().add(notifications);
container().add(taskBarBlur);
container().add(taskBar);
container().add(privLog);
// Also the other widgets.
foreach (Widget *w, additional)
{
container().add(w);
}
// Fake cursor must be on top.
container().add(cursor);
if (mode == Normal)
{
root.update();
}
}
void updateCompositor()
{
DENG_ASSERT_IN_MAIN_THREAD();
if (!compositor) return;
if (vrCfg().mode() == VRConfig::OculusRift)
{
/// @todo Adjustable compositor depth/size.
float uiDistance = 40;
float uiSize = 50;
auto const &ovr = vrCfg().oculusRift();
Vector3f const pry = ovr.headOrientation();
compositor->setCompositeProjection(
GL_GetProjectionMatrix()
* Matrix4f::rotate(radianToDegree(pry[1]), Vector3f(0, 0, -1))
* Matrix4f::rotate(radianToDegree(pry[0]), Vector3f(1, 0, 0))
* Matrix4f::rotate(radianToDegree(pry[2]), Vector3f(0, 1, 0))
* Matrix4f::translate(swizzle(ovr.headPosition() *
vrCfg().mapUnitsPerMeter(),
AxisNegX, AxisNegY, AxisZ))
* Matrix4f::scale(Vector3f(uiSize, -uiSize/ovr.aspect(), 1.f))
* Matrix4f::translate(Vector3f(-.5f, -.5f, uiDistance)));
}
else
{
// We'll simply cover the entire view.
compositor->useDefaultCompositeProjection();
}
}*/
void minimizeGame(bool mini)
{
TimeDelta const SPAN = 1.0;
if (mini && !isGameMini)
{
// Get rid of the sidebar, if it's open.
self.setSidebar(RightEdge, nullptr);
auto const &unit = Style::get().rules().rule("unit");
gameWidth ->set(root.viewWidth()/2 - unit, SPAN);
gameHeight->set(root.viewHeight()/4, SPAN);
homeDelta ->set(-*gameHeight - unit, SPAN);
isGameMini = true;
}
else if (!mini && isGameMini)
{
gameWidth ->set(root.viewWidth(), SPAN);
gameHeight->set(root.viewHeight(), SPAN);
homeDelta ->set(0, SPAN);
isGameMini = false;
}
}
void updateMouseCursor()
{
// The cursor is only needed if the content is warped.
cursor->show(!self.canvas().isMouseTrapped() && VRConfig::modeAppliesDisplacement(vrCfg().mode()));
// Show or hide the native mouse cursor.
if (cursor->isVisible())
{
if (!cursorHasBeenHidden)
{
qApp->setOverrideCursor(QCursor(Qt::BlankCursor));
cursorHasBeenHidden = true;
}
Vector2i cp = ClientApp::windowSystem().latestMousePosition();
cursorX->set(cp.x);
cursorY->set(cp.y);
}
else
{
if (cursorHasBeenHidden)
{
qApp->restoreOverrideCursor();
}
cursorHasBeenHidden = false;
}
}
void setupFadeFromBlack(TimeDelta const &span)
{
if (!fader)
{
fader.reset(new FadeToBlackWidget);
fader->rule().setRect(root.viewRule());
root.add(fader); // on top of everything else
}
fader->initFadeFromBlack(span);
fader->start();
}
void completeFade()
{
// Check if the fade is done.
if (fader)
{
fader->disposeIfDone();
}
}
};
ClientWindow::ClientWindow(String const &id)
: BaseWindow(id)
, d(new Impl(this))
{
canvas().audienceForGLResize() += d;
canvas().audienceForGLInit() += d;
#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
d->setupUI();
// The first window is the main window.
if (!mainWindow)
{
mainWindow = this;
}
}
Vector2f ClientWindow::windowContentSize() const
{
return Vector2f(d->root.viewWidth().value(), d->root.viewHeight().value());
}
ClientRootWidget &ClientWindow::root()
{
return d->root;
}
TaskBarWidget &ClientWindow::taskBar()
{
return *d->taskBar;
}
GuiWidget &ClientWindow::taskBarBlur()
{
return *d->taskBarBlur;
}
ConsoleWidget &ClientWindow::console()
{
return d->taskBar->console();
}
NotificationAreaWidget &ClientWindow::notifications()
{
return *d->notifications;
}
HomeWidget &ClientWindow::home()
{
return *d->home;
}
GameWidget &ClientWindow::game()
{
return *d->game;
}
BusyWidget &ClientWindow::busy()
{
return *d->busy;
}
AlertDialog &ClientWindow::alerts()
{
return *d->alerts;
}
bool ClientWindow::isFPSCounterVisible() const
{
return App::config().getb(configName("showFps"));
}
void ClientWindow::setMode(Mode const &mode)
{
LOG_AS("ClientWindow");
d->setMode(mode);
}