Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
} | ||
|