Skip to content

Commit

Permalink
Refactor: Rectangle<> separate size type, Rectanglei uses unsigned size
Browse files Browse the repository at this point in the history
Coherently using unsigned ints for sizes when dealing with integer
vectors and rectangles.

Sizes can never be negative.
  • Loading branch information
skyjake committed Apr 24, 2013
1 parent 7261dfe commit 70511b7
Show file tree
Hide file tree
Showing 19 changed files with 94 additions and 68 deletions.
2 changes: 1 addition & 1 deletion doomsday/client/src/ui/clientwindow.cpp
Expand Up @@ -306,7 +306,7 @@ void ClientWindow::canvasGLResized(Canvas &canvas)
{
LOG_AS("ClientWindow");

Vector2i size = canvas.size();
Canvas::Size size = canvas.size();
LOG_DEBUG("Canvas resized to ") << size.asText();

// Tell the widgets.
Expand Down
1 change: 1 addition & 0 deletions doomsday/libdeng2/include/de/data/numbervalue.h
Expand Up @@ -56,6 +56,7 @@ class DENG2_PUBLIC NumberValue : public Value
NumberValue(Number initialValue = 0, SemanticHints semantic = Generic);
NumberValue(dsize initialSize);
NumberValue(dint initialInteger);
NumberValue(duint initialUnsignedInteger);
NumberValue(bool initialBoolean);

/**
Expand Down
37 changes: 20 additions & 17 deletions doomsday/libdeng2/include/de/rectangle.h
Expand Up @@ -34,41 +34,44 @@ namespace de {
*
* @ingroup math
*/
template <typename VectorType>
template <typename CornerVectorType, typename SizeVectorType>
class Rectangle
{
public:
typedef VectorType Corner;
typedef Rectangle<CornerVectorType, SizeVectorType> RectangleType;
typedef CornerVectorType Corner;
typedef SizeVectorType Size;
typedef typename Corner::ValueType Type;
typedef VectorType Size;
typedef typename Size::ValueType SizeType;

public:
Rectangle() {}
Rectangle(Type left, Type top, Type width, Type height)
Rectangle(Type left, Type top, SizeType width, SizeType height)
: topLeft(left, top), bottomRight(left + width, top + height) {}
Rectangle(Corner const &tl, Corner const &br) : topLeft(tl), bottomRight(br) {}
Type width() const { return abs(bottomRight.x - topLeft.x); }
Type height() const { return abs(bottomRight.y - topLeft.y); }
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()); }
void moveTopLeft(VectorType const &point) {
void moveTopLeft(CornerVectorType const &point) {
Size s = size();
topLeft = point;
bottomRight = point + s;
bottomRight.x = point.x + s.x;
bottomRight.y = point.y + s.y;
}
void setWidth(Type w) { bottomRight.x = topLeft.x + w; }
void setHeight(Type h) { bottomRight.y = topLeft.y + h; }
void setSize(Vector2<Type> const &s) { setWidth(s.x); setHeight(s.y); }
void setWidth(SizeType w) { bottomRight.x = topLeft.x + w; }
void setHeight(SizeType h) { bottomRight.y = topLeft.y + h; }
void setSize(SizeVectorType const &s) { setWidth(s.x); setHeight(s.y); }
void include(Corner const &point) {
topLeft = topLeft.min(point);
bottomRight = bottomRight.max(point);
}
Rectangle<VectorType> adjusted(VectorType const &tl, VectorType const &br) const {
return Rectangle<VectorType>(topLeft + tl, bottomRight + br);
RectangleType adjusted(CornerVectorType const &tl, CornerVectorType const &br) const {
return RectangleType(topLeft + tl, bottomRight + br);
}
bool contains(Corner const &point) const {
return point >= topLeft && point <= bottomRight;
}
bool operator == (Rectangle<VectorType> const &other) const {
bool operator == (RectangleType const &other) const {
return topLeft == other.topLeft && bottomRight == other.bottomRight;
}
String asText() const {
Expand Down Expand Up @@ -107,9 +110,9 @@ class Rectangle
};

// Common types.
typedef Rectangle<Vector2i> Rectanglei;
typedef Rectangle<Vector2ui> Rectangleui;
typedef Rectangle<Vector2f> Rectanglef;
typedef Rectangle<Vector2i, Vector2ui> Rectanglei;
typedef Rectangle<Vector2ui, Vector2ui> Rectangleui;
typedef Rectangle<Vector2f, Vector2f> Rectanglef;

} // namespace de

Expand Down
7 changes: 5 additions & 2 deletions doomsday/libdeng2/include/de/widgets/rootwidget.h
Expand Up @@ -40,10 +40,13 @@ class Rule;
*/
class DENG2_PUBLIC RootWidget : public Widget
{
public:
typedef Vector2ui Size;

public:
RootWidget();

Vector2i viewSize() const;
Size viewSize() const;

Rule const &viewLeft() const;
Rule const &viewRight() const;
Expand All @@ -57,7 +60,7 @@ class DENG2_PUBLIC RootWidget : public Widget
*
* @param viewSize View size.
*/
virtual void setViewSize(Vector2i const &viewSize);
virtual void setViewSize(Size const &viewSize);

/**
* Sets the focus widget. It is the first widget to be offered input
Expand Down
4 changes: 4 additions & 0 deletions doomsday/libdeng2/src/data/numbervalue.cpp
Expand Up @@ -38,6 +38,10 @@ NumberValue::NumberValue(dint initialInteger)
: _value(initialInteger), _semantic(Generic)
{}

NumberValue::NumberValue(duint initialUnsignedInteger)
: _value(initialUnsignedInteger), _semantic(Generic)
{}

NumberValue::NumberValue(bool initialBoolean)
: _value(initialBoolean? True : False), _semantic(Boolean)
{}
Expand Down
10 changes: 5 additions & 5 deletions doomsday/libdeng2/src/widgets/rootwidget.cpp
Expand Up @@ -41,17 +41,17 @@ DENG2_PIMPL_NOREF(RootWidget)
delete viewRect;
}

Vector2i viewSize() const
Size viewSize() const
{
return Vector2i(de::floor(viewRect->right().value()),
de::floor(viewRect->bottom().value()));
return Size(de::max(0, de::floor(viewRect->right().value())),
de::max(0, de::floor(viewRect->bottom().value())));
}
};

RootWidget::RootWidget() : Widget(), d(new Instance)
{}

Vector2i RootWidget::viewSize() const
RootWidget::Size RootWidget::viewSize() const
{
return d->viewSize();
}
Expand Down Expand Up @@ -86,7 +86,7 @@ Rule const &RootWidget::viewHeight() const
return d->viewRect->bottom();
}

void RootWidget::setViewSize(Vector2i const &size)
void RootWidget::setViewSize(Size const &size)
{
d->viewRect->setInput(Rule::Right, Const(size.x));
d->viewRect->setInput(Rule::Bottom, Const(size.y));
Expand Down
13 changes: 12 additions & 1 deletion doomsday/libgui/include/de/gui/canvas.h
Expand Up @@ -22,10 +22,12 @@

#include <de/Observers>
#include <de/libdeng2.h>
#include <de/Vector>
#include <QGLWidget>

#include "../KeyEventSource"
#include "../MouseEventSource"
#include "../GLTarget"

namespace de {

Expand All @@ -46,6 +48,8 @@ class LIBGUI_PUBLIC Canvas : public QGLWidget, public KeyEventSource, public Mou
Q_OBJECT

public:
typedef Vector2ui Size;

/**
* Notified when the canvas is ready for GL operations. The OpenGL context
* and drawing surface are not ready to be used before that. The
Expand Down Expand Up @@ -111,7 +115,7 @@ class LIBGUI_PUBLIC Canvas : public QGLWidget, public KeyEventSource, public Mou
/**
* Returns the size of the canvas in pixels.
*/
Vector2i size() const;
Size size() const;

/**
* When the mouse is trapped, all mouse input is grabbed, the mouse cursor
Expand All @@ -134,6 +138,13 @@ class LIBGUI_PUBLIC Canvas : public QGLWidget, public KeyEventSource, public Mou
*/
void copyAudiencesFrom(Canvas const &other);

/**
* Returns a render target that renders to this canvas.
*
* @return GL render target.
*/
GLTarget &renderTarget() const;

protected:
void initializeGL();
void resizeGL(int w, int h);
Expand Down
4 changes: 2 additions & 2 deletions doomsday/libgui/include/de/gui/canvaswindow.h
Expand Up @@ -47,7 +47,7 @@ class LIBGUI_PUBLIC CanvasWindow : public QMainWindow,
Q_OBJECT

public:
typedef Vector2i Size;
typedef Vector2ui Size;

public:
CanvasWindow();
Expand All @@ -64,7 +64,7 @@ class LIBGUI_PUBLIC CanvasWindow : public QMainWindow,
*/
inline Vector2i pos() const { return Vector2i(x(), y()); }

inline Vector2i size() const { return Vector2i(width(), height()); }
inline Size size() const { return Size(de::max(0, width()), de::max(0, height())); }

/**
* Determines the current width of window's Canvas in pixels.
Expand Down
7 changes: 4 additions & 3 deletions doomsday/libgui/src/canvas.cpp
Expand Up @@ -43,9 +43,10 @@ static const int MOUSE_WHEEL_CONTINUOUS_THRESHOLD_MS = 100;

DENG2_PIMPL(Canvas)
{
GLTarget target;
CanvasWindow *parent;
bool readyNotified;
Vector2i currentSize;
Size currentSize;
bool mouseDisabled;
bool mouseGrabbed;
#ifdef WIN32
Expand Down Expand Up @@ -199,7 +200,7 @@ GLuint Canvas::grabAsTexture(QSize const &outputSize)
QGLContext::LinearFilteringBindOption);
}

Vector2i Canvas::size() const
Canvas::Size Canvas::size() const
{
return d->currentSize;
}
Expand Down Expand Up @@ -248,7 +249,7 @@ void Canvas::initializeGL()

void Canvas::resizeGL(int w, int h)
{
Vector2i newSize(w, h);
Size newSize(max(0, w), max(0, h));

// Only react if this is actually a resize.
if(d->currentSize != newSize)
Expand Down
6 changes: 3 additions & 3 deletions doomsday/libgui/src/persistentcanvaswindow.cpp
Expand Up @@ -50,7 +50,7 @@ static QRect desktopRect()
return QApplication::desktop()->screenGeometry();
}

static QRect centeredQRect(Vector2i size)
static QRect centeredQRect(Vector2ui const &size)
{
QSize screenSize = desktopRect().size();

Expand All @@ -63,7 +63,7 @@ static QRect centeredQRect(Vector2i size)
QSize(size.x, size.y));
}

static Rectanglei centeredRect(Vector2i size)
static Rectanglei centeredRect(Vector2ui const &size)
{
QRect rect = centeredQRect(size);
return Rectanglei(rect.left(), rect.top(), rect.width(), rect.height());
Expand Down Expand Up @@ -217,7 +217,7 @@ DENG2_PIMPL(PersistentCanvasWindow)
ArrayValue &fs = config.geta(configName("fullSize"));
if(fs.size() >= 2)
{
fullSize = Vector2i(fs.at(0).asNumber(), fs.at(1).asNumber());
fullSize = Size(fs.at(0).asNumber(), fs.at(1).asNumber());
}

colorDepthBits = config.geti(configName("colorDepth"));
Expand Down
10 changes: 5 additions & 5 deletions doomsday/libshell/include/de/shell/textcanvas.h
Expand Up @@ -52,7 +52,7 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(Alignment)
class LIBSHELL_PUBLIC TextCanvas
{
public:
typedef Vector2i Size;
typedef Vector2ui Size;
typedef Vector2i Coord;

struct Char
Expand Down Expand Up @@ -164,9 +164,9 @@ class LIBSHELL_PUBLIC TextCanvas

void fill(Rectanglei const &rect, Char const &ch);

void put(Vector2i const &pos, Char const &ch);
void put(Coord const &pos, Char const &ch);

void drawText(Vector2i const &pos, String const &text,
void drawText(Coord const &pos, String const &text,
Char::Attribs const &attribs = Char::DefaultAttributes,
int richOffset = 0);

Expand All @@ -180,7 +180,7 @@ class LIBSHELL_PUBLIC TextCanvas
* @param attribs Character attributes.
* @param lineAlignment Alignment for lines.
*/
void drawWrappedText(Vector2i const &pos, String const &text, LineWrapping const &wraps,
void drawWrappedText(Coord const &pos, String const &text, LineWrapping const &wraps,
Char::Attribs const &attribs = Char::DefaultAttributes,
Alignment lineAlignment = AlignLeft);

Expand Down Expand Up @@ -212,7 +212,7 @@ class LIBSHELL_PUBLIC TextCanvas
*
* @param pos Position.
*/
virtual void setCursorPosition(Vector2i const &pos);
virtual void setCursorPosition(Coord const &pos);

private:
DENG2_PRIVATE(d)
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libshell/include/de/shell/textrootwidget.h
Expand Up @@ -58,7 +58,7 @@ class LIBSHELL_PUBLIC TextRootWidget : public RootWidget
*
* @param viewSize New size.
*/
void setViewSize(Vector2i const &viewSize);
void setViewSize(Size const &viewSize);

TextWidget *focus() const;

Expand Down
2 changes: 1 addition & 1 deletion doomsday/libshell/src/lineeditwidget.cpp
Expand Up @@ -81,7 +81,7 @@ DENG2_PIMPL(LineEditWidget)
*/
void updateWrapsAndHeight()
{
wraps.wrapTextToWidth(text, de::max(1, self.rule().recti().width() - prompt.size() - 1));
wraps.wrapTextToWidth(text, de::max(1, int(self.rule().recti().width()) - int(prompt.size()) - 1));
height->set(wraps.height());
}

Expand Down
4 changes: 2 additions & 2 deletions doomsday/libshell/src/logwidget.cpp
Expand Up @@ -94,7 +94,7 @@ DENG2_PIMPL(LogWidget)
{
Sink sink;
MonospaceLogSinkFormatter formatter;
int cacheWidth;
unsigned int cacheWidth;
QList<TextCanvas *> cache; ///< Indices match entry indices in sink.
int maxEntries;
int visibleOffset;
Expand Down Expand Up @@ -240,7 +240,7 @@ void LogWidget::draw()
LogEntry const &entry = d->sink.entry(idx);
QList<String> lines = d->formatter.logEntryToTextLines(entry);

TextCanvas *buf = new TextCanvas(Vector2i(pos.width(), lines.size()));
TextCanvas *buf = new TextCanvas(Vector2ui(pos.width(), lines.size()));
d->cache.append(buf);

TextCanvas::Char::Attribs attribs = (entry.flags() & LogEntry::Remote?
Expand Down

0 comments on commit 70511b7

Please sign in to comment.