/
busywidget.cpp
227 lines (188 loc) · 5.54 KB
/
busywidget.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
/** @file busywidget.cpp
*
* @authors Copyright (c) 2013-2014 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, see:
* http://www.gnu.org/licenses</small>
*/
#include "de_platform.h"
#include "ui/widgets/busywidget.h"
#include "ui/busyvisual.h"
#include "ui/ui_main.h"
#include "ui/clientwindow.h"
#include "gl/gl_main.h"
#include "render/r_main.h"
#include "busymode.h"
#include "sys_system.h"
#include <de/concurrency.h>
#include <de/Drawable>
#include <de/GLFramebuffer>
#include <de/GuiRootWidget>
#include <de/ProgressWidget>
using namespace de;
DENG_GUI_PIMPL(BusyWidget)
{
typedef DefaultVertexBuf VertexBuf;
ProgressWidget *progress;
Time frameDrawnAt;
GLFramebuffer transitionFrame;
Drawable drawable;
GLUniform uTex { "uTex", GLUniform::Sampler2D };
GLUniform uMvpMatrix { "uMvpMatrix", GLUniform::Mat4 };
Instance(Public *i) : Base(i)
{
progress = new ProgressWidget;
progress->setAlignment(ui::AlignCenter, LabelWidget::AlignOnlyByImage);
progress->setRange(Rangei(0, 200));
progress->setImageScale(.2f);
progress->rule().setRect(self.rule());
self.add(progress);
}
void glInit()
{
//transitionFrame.setColorFormat(Image::RGB_888);
VertexBuf *buf = new VertexBuf;
VertexBuf::Builder verts;
verts.makeQuad(Rectanglef(0, 0, 1, 1), Vector4f(1, 1, 1, 1), Rectanglef(0, 0, 1, 1));
buf->setVertices(gl::TriangleStrip, verts, gl::Static);
drawable.addBuffer(buf);
shaders().build(drawable.program(), "generic.textured.color")
<< uMvpMatrix << uTex;
}
void glDeinit()
{
drawable.clear();
transitionFrame.glDeinit();
}
bool haveTransitionFrame() const
{
return transitionFrame.isReady();
}
};
BusyWidget::BusyWidget(String const &name)
: GuiWidget(name), d(new Instance(this))
{
requestGeometry(false);
}
ProgressWidget &BusyWidget::progress()
{
return *d->progress;
}
void BusyWidget::viewResized()
{
GuiWidget::viewResized();
}
void BusyWidget::update()
{
GuiWidget::update();
if(BusyMode_Active())
{
BusyMode_Loop();
}
}
void BusyWidget::drawContent()
{
if(!BusyMode_Active())
{
d->progress->hide();
if(Con_TransitionInProgress())
{
GLState::push()
.setViewport(Rectangleui::fromSize(GLState::current().target().size()))
.apply();
Con_DrawTransition();
GLState::pop().apply();
}
else
{
// Time to hide the busy widget, the transition has ended (or
// was never started).
hide();
releaseTransitionFrame();
}
return;
}
if(d->haveTransitionFrame())
{
GLState::current().apply();
glDisable(GL_ALPHA_TEST); /// @todo get rid of these
glDisable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
// Draw the texture.
Rectanglei pos = rule().recti();
d->uMvpMatrix = Matrix4f::scale(Vector3f(1, -1, 1)) *
root().projMatrix2D() *
Matrix4f::scaleThenTranslate(pos.size(), pos.topLeft);
d->drawable.draw();
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}
}
bool BusyWidget::handleEvent(Event const &)
{
// Eat events and ignore them.
return true;
}
void BusyWidget::renderTransitionFrame()
{
LOG_AS("BusyWidget");
if(d->haveTransitionFrame())
{
// We already have a valid frame, no need to render again.
LOGDEV_GL_VERBOSE("Skipping rendering of transition frame (got one already)");
return;
}
// We'll have an up-to-date frame after this.
d->frameDrawnAt = Time();
DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
Rectanglei grabRect = Rectanglei::fromSize(root().window().canvas().size());
LOGDEV_GL_VERBOSE("Rendering transition frame, size ") << grabRect.size().asText();
d->transitionFrame.resize(grabRect.size());
if(!d->transitionFrame.isReady())
{
d->transitionFrame.glInit();
}
GLState::push()
.setTarget(d->transitionFrame.target())
.setViewport(Rectangleui::fromSize(d->transitionFrame.size()))
.apply();
root().window().as<ClientWindow>().drawGameContent();
GLState::pop().apply();
d->uTex = d->transitionFrame.colorTexture();
}
void BusyWidget::releaseTransitionFrame()
{
if(d->haveTransitionFrame())
{
LOGDEV_GL_VERBOSE("Releasing transition frame");
d->transitionFrame.glDeinit();
}
}
GLTexture const *BusyWidget::transitionFrame() const
{
if(d->haveTransitionFrame())
{
return &d->transitionFrame.colorTexture();
}
return 0;
}
void BusyWidget::glInit()
{
d->glInit();
}
void BusyWidget::glDeinit()
{
d->glDeinit();
}