Skip to content

Commit

Permalink
libgui|VertexBuilder: Added makeQuad() for constructing 2D rectangles
Browse files Browse the repository at this point in the history
Cleaned up the Client's widgets' geometry building by using makeQuad().
  • Loading branch information
skyjake committed May 24, 2013
1 parent 1ce21e8 commit 41ecd01
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 65 deletions.
12 changes: 1 addition & 11 deletions doomsday/client/src/ui/widgets/gltextcomposer.cpp
Expand Up @@ -211,11 +211,6 @@ void GLTextComposer::makeVertices(Vertices &triStrip,
Vector2ui const size = d->atlas->imageRect(d->lines[i].id).size();
Rectanglef const uv = d->atlas->imageRectf(d->lines[i].id);

Vertex v;
VertexBuf::Builder quad;

v.rgba = Vector4f(1, 1, 1, 1); // should be a param

Vector2f linePos = p + Vector2f(d->wraps->lineIndent(i), 0);

// Align the line.
Expand All @@ -228,12 +223,7 @@ void GLTextComposer::makeVertices(Vertices &triStrip,
linePos.x += (int(rect.width()) - int(size.x)) / 2;
}

v.pos = linePos; v.texCoord = uv.topLeft; quad << v;
v.pos = linePos + Vector2f(size.x, 0); v.texCoord = uv.topRight(); quad << v;
v.pos = linePos + Vector2f(0, size.y); v.texCoord = uv.bottomLeft(); quad << v;
v.pos = linePos + Vector2f(size.x, size.y); v.texCoord = uv.bottomRight; quad << v;

triStrip += quad;
triStrip.makeQuad(Rectanglef::fromSize(linePos, size), Vector4f(1, 1, 1, 1), uv);
}
p.y += d->font->lineSpacing().value();
}
Expand Down
37 changes: 10 additions & 27 deletions doomsday/client/src/ui/widgets/lineeditwidget.cpp
Expand Up @@ -159,16 +159,9 @@ DENG2_OBSERVES(Atlas, Reposition)

Vector4f bgColor = self.style().colors().colorf("background");

// The background.
VertexBuf::Builder verts;
VertexBuf::Type v;

v.rgba = bgColor;
v.texCoord = atlas().imageRectf(bgTex).middle();

v.pos = pos.topLeft; verts << v;
v.pos = pos.topRight(); verts << v;
v.pos = pos.bottomLeft(); verts << v;
v.pos = pos.bottomRight; verts << v;
verts.makeQuad(pos, bgColor, atlas().imageRectf(bgTex).middle());

// Text lines.
Rectanglei const contentRect = pos.shrunk(margin);
Expand All @@ -192,35 +185,25 @@ DENG2_OBSERVES(Atlas, Reposition)
Vector2i start = wraps.charTopLeftInPixels(i, i == startPos.y? startPos.x : span.start) + offset;
Vector2i end = wraps.charTopLeftInPixels(i, i == endPos.y? endPos.x : span.end) + offset;

VertexBuf::Builder quad;
v.rgba = Vector4f(1, 1, 1, 1);
v.texCoord = atlas().imageRectf(bgTex).middle();

v.pos = start; quad << v;
v.pos = end; quad << v;
v.pos = start + Vector2i(0, 1); quad << v;
v.pos = end + Vector2i(0, 1); quad << v;

verts += quad;
verts.makeQuad(Rectanglef(start, end + Vector2i(0, 1)),
Vector4f(1, 1, 1, 1),
atlas().imageRectf(bgTex).middle());
}
}

drawable.buffer<VertexBuf>(ID_BUF_TEXT)
.setVertices(gl::TriangleStrip, verts, gl::Static);

// Cursor.
verts.clear();
v.rgba = Vector4f(1, 1, 1, 1);
v.texCoord = atlas().imageRectf(bgTex).middle();

Vector2i const cursorPos = self.lineCursorPos();
Vector2f const cp = wraps.charTopLeftInPixels(cursorPos.y, cursorPos.x) +
contentRect.topLeft;

v.pos = cp + Vector2f(-1, 0); verts << v;
v.pos = cp + Vector2f(1, 0); verts << v;
v.pos = cp + Vector2f(-1, font->height().value()); verts << v;
v.pos = cp + Vector2f(1, font->height().value()); verts << v;
verts.clear();
verts.makeQuad(Rectanglef(cp + Vector2f(-1, 0),
cp + Vector2f(1, font->height().value())),
Vector4f(1, 1, 1, 1),
atlas().imageRectf(bgTex).middle());

drawable.buffer<VertexBuf>(ID_BUF_CURSOR)
.setVertices(gl::TriangleStrip, verts, gl::Static);
Expand Down
38 changes: 11 additions & 27 deletions doomsday/client/src/ui/widgets/logwidget.cpp
Expand Up @@ -448,21 +448,13 @@ DENG2_PIMPL(LogWidget), public Font::RichFormat::IStyle
float const indPos = float(visibleOffset) / float(maxScroll);
float const avail = contentSize.y - indHeight;
for(int i = 0; i < indHeight; ++i)
{
VertexBuf::Builder quad;
VertexBuf::Type v;
v.rgba = Vector4f(1, 1, 1, scrollOpacity) * accentColor / 255.f;
v.texCoord = entryAtlas->imageRectf(scrollTex).middle();

Rectanglef indRect(Vector2f(contentSize.x + margin - 2*scrollBarWidth, avail - indPos * avail + indHeight),
Vector2f(contentSize.x + margin - scrollBarWidth, avail - indPos * avail));

v.pos = indRect.topLeft; quad << v;
v.pos = indRect.topRight(); quad << v;
v.pos = indRect.bottomLeft(); quad << v;
v.pos = indRect.bottomRight; quad << v;

verts += quad;
{
verts.makeQuad(Rectanglef(Vector2f(contentSize.x + margin - 2*scrollBarWidth,
avail - indPos * avail + indHeight),
Vector2f(contentSize.x + margin - scrollBarWidth,
avail - indPos * avail)),
Vector4f(1, 1, 1, scrollOpacity) * accentColor / 255.f,
entryAtlas->imageRectf(scrollTex).middle());
}
}

Expand All @@ -479,18 +471,10 @@ DENG2_PIMPL(LogWidget), public Font::RichFormat::IStyle
if(self.checkPlace(pos) || !bgBuf->isReady())
{
// Update the background quad.
VertexBuf::Vertices verts;

VertexBuf::Type v;
v.rgba = Vector4f(0, 0, 0, .5f);
v.texCoord = self.root().atlas().imageRectf(bgTex).middle();

v.pos = pos.topLeft; verts << v;
v.pos = pos.topRight(); verts << v;
v.pos = pos.bottomLeft(); verts << v;
v.pos = pos.bottomRight; verts << v;

bgBuf->setVertices(gl::TriangleStrip, verts, gl::Static);
bgBuf->setVertices(gl::TriangleStrip,
VertexBuf::Builder().makeQuad(pos, Vector4f(0, 0, 0, .666f),
self.root().atlas().imageRectf(bgTex).middle()),
gl::Static);
}

updateGeometry();
Expand Down
5 changes: 5 additions & 0 deletions doomsday/libdeng2/include/de/core/rectangle.h
Expand Up @@ -60,6 +60,11 @@ class Rectangle
return RectangleType(qr.left(), qr.top(), qr.width(), qr.height());
}

// Automatic conversion to a Rectanglef.
operator Rectangle<Vector2f, Vector2f> () const {
return Rectangle<Vector2f, Vector2f>(topLeft.x, topLeft.y, width(), height());
}

SizeType width() const { return abs(bottomRight.x - topLeft.x); }
SizeType height() const { return abs(bottomRight.y - topLeft.y); }
Size size() const { return Size(width(), height()); }
Expand Down
23 changes: 23 additions & 0 deletions doomsday/libgui/include/de/gui/vertexbuilder.h
Expand Up @@ -20,6 +20,8 @@
#define LIBGUI_VERTEXBUILDER_H

#include <QVector>
#include <de/Vector>
#include <de/Rectangle>

namespace de {

Expand All @@ -38,6 +40,27 @@ struct VertexBuilder
Vertices v(*this);
return v += other;
}
Vertices &makeQuad(Rectanglef const &rect, Vector4f const &color, Vector2f const &uv) {
Vertices quad;
VertexType v;
v.rgba = color;
v.texCoord = uv;
v.pos = rect.topLeft; quad << v;
v.pos = rect.topRight(); quad << v;
v.pos = rect.bottomLeft(); quad << v;
v.pos = rect.bottomRight; quad << v;
return *this += quad;
}
Vertices &makeQuad(Rectanglef const &rect, Vector4f const &color, Rectanglef const &uv) {
Vertices quad;
VertexType v;
v.rgba = color;
v.pos = rect.topLeft; v.texCoord = uv.topLeft; quad << v;
v.pos = rect.topRight(); v.texCoord = uv.topRight(); quad << v;
v.pos = rect.bottomLeft(); v.texCoord = uv.bottomLeft(); quad << v;
v.pos = rect.bottomRight; v.texCoord = uv.bottomRight; quad << v;
return *this += quad;
}
};

static void concatenate(Vertices const &stripSequence, Vertices &destStrip)
Expand Down

0 comments on commit 41ecd01

Please sign in to comment.