/
guirootwidget.cpp
189 lines (163 loc) · 5.4 KB
/
guirootwidget.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
/** @file guirootwidget.cpp Graphical root widget.
*
* @authors Copyright (c) 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, see:
* http://www.gnu.org/licenses</small>
*/
#include "ui/widgets/guirootwidget.h"
#include "ui/clientwindow.h"
#include "clientapp.h"
#include <de/AtlasTexture>
#include <de/GLTexture>
#include <de/GLUniform>
#include <QImage>
#include <QPainter>
using namespace de;
DENG2_PIMPL(GuiRootWidget)
{
ClientWindow *window;
QScopedPointer<AtlasTexture> atlas; ///< Shared atlas for most UI graphics/text.
GLUniform uTexAtlas;
Id solidWhiteTex;
Id roundCorners;
Id gradientFrame;
Instance(Public *i, ClientWindow *win)
: Base(i),
window(win),
atlas(0),
uTexAtlas("uTex", GLUniform::Sampler2D)
{}
~Instance()
{
// Tell all widgets to release their resource allocations. The base
// class destructor will destroy all widgets, but this class governs
// shared GL resources, so we'll ask the widgets to do this now.
self.notifyTree(NotifyArgs(&Widget::deinitialize));
}
void initAtlas()
{
if(atlas.isNull())
{
atlas.reset(AtlasTexture::newWithRowAllocator(
Atlas::BackingStore | Atlas::AllowDefragment,
GLTexture::maximumSize().min(GLTexture::Size(4096, 4096))));
uTexAtlas = *atlas;
// A set of general purpose textures:
// One solid white pixel.
Image const solidWhitePixel = Image::solidColor(Image::Color(255, 255, 255, 255),
Image::Size(1, 1));
solidWhiteTex = atlas->alloc(solidWhitePixel);
// Rounded corners.
{
QImage corners(QSize(20, 20), QImage::Format_ARGB32);
corners.fill(QColor(255, 255, 255, 0).rgba());
QPainter painter(&corners);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setBrush(Qt::NoBrush);
painter.setPen(QPen(Qt::white, 1));
painter.drawEllipse(QPoint(11, 11), 8, 8);
painter.setPen(QPen(Qt::black, 1));
painter.drawEllipse(QPoint(10, 10), 8, 8);
roundCorners = atlas->alloc(corners);
}
// Gradient frame.
{
QImage frame(QSize(12, 12), QImage::Format_ARGB32);
frame.fill(QColor(255, 255, 255, 0).rgba());
QPainter painter(&frame);
painter.setBrush(Qt::NoBrush);
painter.setPen(QColor(255, 255, 255, 32));
painter.drawRect(QRect(1, 1, 9, 9));
painter.setPen(QColor(255, 255, 255, 64));
painter.drawRect(QRect(2, 2, 7, 7));
painter.setPen(QColor(255, 255, 255, 128));
painter.drawRect(QRect(3, 3, 5, 5));
painter.setPen(QColor(255, 255, 255, 255));
painter.drawRect(QRect(4, 4, 3, 3));
gradientFrame = atlas->alloc(frame);
}
}
}
};
GuiRootWidget::GuiRootWidget(ClientWindow *window)
: d(new Instance(this, window))
{}
void GuiRootWidget::setWindow(ClientWindow *window)
{
d->window = window;
}
ClientWindow &GuiRootWidget::window()
{
DENG2_ASSERT(d->window != 0);
return *d->window;
}
AtlasTexture &GuiRootWidget::atlas()
{
d->initAtlas();
return *d->atlas;
}
GLUniform &GuiRootWidget::uAtlas()
{
return d->uTexAtlas;
}
Id GuiRootWidget::solidWhitePixel() const
{
d->initAtlas();
return d->solidWhiteTex;
}
Id GuiRootWidget::roundCorners() const
{
d->initAtlas();
return d->roundCorners;
}
Id GuiRootWidget::gradientFrame() const
{
d->initAtlas();
return d->gradientFrame;
}
GLShaderBank &GuiRootWidget::shaders()
{
return ClientApp::glShaderBank();
}
Matrix4f GuiRootWidget::projMatrix2D() const
{
RootWidget::Size const size = viewSize();
return Matrix4f::ortho(0, size.x, 0, size.y);
}
void GuiRootWidget::routeMouse(Widget *routeTo)
{
setEventRouting(QList<int>()
<< Event::MouseButton
<< Event::MouseMotion
<< Event::MousePosition
<< Event::MouseWheel, routeTo);
}
void GuiRootWidget::update()
{
if(window().canvas().isGLReady())
{
// Allow GL operations.
window().canvas().makeCurrent();
RootWidget::update();
}
}
void GuiRootWidget::drawUntil(Widget &until)
{
NotifyArgs args(&Widget::draw);
args.conditionFunc = &Widget::isVisible;
args.preNotifyFunc = &Widget::preDrawChildren;
args.postNotifyFunc = &Widget::postDrawChildren;
args.until = &until;
notifyTree(args);
}