-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
basewindow.cpp
195 lines (167 loc) · 4.6 KB
/
basewindow.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
/** @file basewindow.cpp Abstract base class for application windows.
*
* @authors Copyright (c) 2014-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/BaseWindow"
#include "de/WindowTransform"
#include "de/WindowSystem"
#include "de/BaseGuiApp"
#include "de/VRConfig"
#include <de/GLBuffer>
#include <de/GLState>
namespace de {
DE_PIMPL(BaseWindow)
, DE_OBSERVES(GLWindow, Init)
, DE_OBSERVES(KeyEventSource, KeyEvent)
, DE_OBSERVES(MouseEventSource, MouseEvent)
{
WindowTransform defaultXf; ///< Used by default (doesn't apply any transformation).
WindowTransform *xf;
Impl(Public *i)
: Base(i)
, defaultXf(*i)
, xf(&defaultXf)
{
self().audienceForInit() += this;
// Listen to input.
self().eventHandler().audienceForKeyEvent() += this;
self().eventHandler().audienceForMouseEvent() += this;
}
void windowInit(GLWindow &)
{
// The framework widgets expect basic alpha blending.
GLState::current()
.setBlend(true)
.setBlendFunc(gfx::SrcAlpha, gfx::OneMinusSrcAlpha);
}
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.
if (!WindowSystem::get().processEvent(ev))
{
// Maybe the fallback handler has use for this.
self().handleFallbackEvent(ev);
}
}
void mouseEvent(MouseEvent const &event)
{
MouseEvent ev = event;
// Translate mouse coordinates for direct interaction.
if (ev.type() == Event::MousePosition ||
ev.type() == Event::MouseButton ||
ev.type() == Event::MouseWheel)
{
ev.setPos(xf->windowToLogicalCoords(event.pos()).toVec2i());
}
if (!WindowSystem::get().processEvent(ev))
{
// Maybe the fallback handler has use for this.
self().handleFallbackEvent(ev);
}
}
};
BaseWindow::BaseWindow(String const &id)
#if !defined (DE_MOBILE)
: LIBAPPFW_BASEWINDOW_SUPER(id)
, d(new Impl(this))
#else
: d(new Impl(this))
#endif
{}
void BaseWindow::setTransform(WindowTransform &xf)
{
d->xf = &xf;
}
void BaseWindow::useDefaultTransform()
{
d->xf = &d->defaultXf;
}
WindowTransform &BaseWindow::transform()
{
DE_ASSERT(d->xf != nullptr);
return *d->xf;
}
bool BaseWindow::prepareForDraw()
{
if (isGLReady())
{
#if !defined (DE_MOBILE)
// Don't run the main loop until after the paint event has been dealt with.
DE_GUI_APP->loop().pause();
#endif
return true; // Go ahead.
}
return false;
}
void BaseWindow::requestDraw()
{
update();
if (!prepareForDraw())
{
// Not right now, please.
return;
}
// Initialize Oculus Rift if needed.
auto &vr = DE_BASE_GUI_APP->vr();
if (vr.mode() == VRConfig::OculusRift)
{
if (isGLReady())
{
makeCurrent();
vr.oculusRift().init();
}
}
else
{
makeCurrent();
vr.oculusRift().deinit();
}
}
void BaseWindow::draw()
{
preDraw();
d->xf->drawTransformed();
postDraw();
}
void BaseWindow::preDraw()
{
auto &vr = DE_BASE_GUI_APP->vr();
if (vr.mode() == VRConfig::OculusRift)
{
vr.oculusRift().beginFrame();
}
}
void BaseWindow::postDraw()
{
auto &vr = DE_BASE_GUI_APP->vr();
if (vr.mode() == VRConfig::OculusRift)
{
vr.oculusRift().endFrame();
}
#if !defined (DE_MOBILE)
// The timer loop was paused when the frame was requested to be drawn.
DE_GUI_APP->loop().resume();
#endif
}
#if defined (DE_MOBILE)
String BaseWindow::configName(String const &key) const
{
return QString("window.main.%1").arg(key);
}
#endif
} // namespace de