Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Client|UI: Added DocumentWidget for larger blocks of text
While LabelWidget is good for small static UI texts, DocumentWidget
is designed to handle longer texts that typically are only partially
visible.
  • Loading branch information
skyjake committed Jul 29, 2013
1 parent ce445e2 commit 47e1bf6
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doomsday/client/client.pro
Expand Up @@ -336,6 +336,7 @@ DENG_HEADERS += \
include/ui/widgets/buttonwidget.h \
include/ui/widgets/consolecommandwidget.h \
include/ui/widgets/consolewidget.h \
include/ui/widgets/documentwidget.h \
include/ui/widgets/gameselectionwidget.h \
include/ui/widgets/gltextcomposer.h \
include/ui/widgets/guirootwidget.h \
Expand Down Expand Up @@ -628,6 +629,7 @@ SOURCES += \
src/ui/widgets/buttonwidget.cpp \
src/ui/widgets/consolecommandwidget.cpp \
src/ui/widgets/consolewidget.cpp \
src/ui/widgets/documentwidget.cpp \
src/ui/widgets/gameselectionwidget.cpp \
src/ui/widgets/gltextcomposer.cpp \
src/ui/widgets/guirootwidget.cpp \
Expand Down
79 changes: 79 additions & 0 deletions doomsday/client/include/ui/widgets/documentwidget.h
@@ -0,0 +1,79 @@
/** @file documentwidget.h Widget for displaying large amounts of text.
*
* @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>
*/

#ifndef DENG_CLIENT_DOCUMENTWIDGET_H
#define DENG_CLIENT_DOCUMENTWIDGET_H

#include "scrollareawidget.h"
#include "alignment.h"

/**
* Widget for displaying large amounts of text.
*
* Based on ScrollAreaWidget for scrolling. Only the visible portion of the
* source text is kept ready for drawing -- the length of the source document
* is thus unlimited. Only vertical scrolling is supported at the moment.
*
* DocumentWidget can be configured to expand horizontally, or it can use a
* certain determined fixed width (see DocumentWidget::setWidthPolicy()).
*
* The assumption is that the source document is largely static so that once
* prepared, the GL resources can be reused as many times as possible.
*/
class DocumentWidget : public ScrollAreaWidget
{
public:
DocumentWidget(de::String const &name = "");

/**
* Sets the policy for managing the widget's width.
* - ui::Fixed means that the widget's width must be defined externally,
* and the width is also used as the content's width.
* - ui::Expand means that the widget's width automatically expands
* to fit the entire content, but the maximum line width is never
* exceeded.
*
* The default policy is ui::Expand.
*
* @param policy Size policy for the widget's width.
*/
void setWidthPolicy(ui::SizePolicy policy);

/**
* Sets the maximum line width when using ui::Expand as the width policy.
*
* @param maxWidth Maximum width of a text line.
*/
void setMaximumLineWidth(int maxWidth);

// Events.
void viewResized();
void update();
void drawContent();
bool handleEvent(de::Event const &event);

protected:
void glInit();
void glDeinit();
void glMakeGeometry(DefaultVertexBuf::Builder &verts);

private:
DENG2_PRIVATE(d)
};

#endif // DENG_CLIENT_DOCUMENTWIDGET_H
112 changes: 112 additions & 0 deletions doomsday/client/src/ui/widgets/documentwidget.cpp
@@ -0,0 +1,112 @@
/** @file documentwidget.cpp Widget for displaying large amounts of text.
*
* @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/documentwidget.h"
#include "ui/widgets/guirootwidget.h"

#include <de/Drawable>

using namespace de;

DENG2_PIMPL(DocumentWidget)
{
typedef DefaultVertexBuf VertexBuf;

Drawable drawable;
GLUniform uMvpMatrix;

Instance(Public *i)
: Base(i),
uMvpMatrix("uMvpMatrix", GLUniform::Mat4)
{

}

void glInit()
{
drawable.addBuffer(new VertexBuf);
}

void glDeinit()
{
drawable.clear();
}

void updateGeometry()
{
Rectanglei pos;
if(!self.hasChangedPlace(pos) && !self.geometryRequested())
{
// No need to change anything.
return;
}

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

// Geometry is now up to date.
self.requestGeometry(false);
}

void draw()
{
updateGeometry();
}
};

DocumentWidget::DocumentWidget(String const &name) : d(new Instance(this))
{}

void DocumentWidget::viewResized()
{
d->uMvpMatrix = root().projMatrix2D();
}

void DocumentWidget::update()
{
ScrollAreaWidget::update();
}

void DocumentWidget::drawContent()
{
d->draw();
}

bool DocumentWidget::handleEvent(Event const &event)
{
return ScrollAreaWidget::handleEvent(event);
}

void DocumentWidget::glInit()
{
d->glInit();
}

void DocumentWidget::glDeinit()
{
d->glDeinit();
}

void DocumentWidget::glMakeGeometry(DefaultVertexBuf::Builder &verts)
{
ScrollAreaWidget::glMakeGeometry(verts);

// Geometry from the text composer.
}

0 comments on commit 47e1bf6

Please sign in to comment.