From f5d375442aa8cbbbc3b21bdd38ee0e9fa300ed4a Mon Sep 17 00:00:00 2001 From: Tats Date: Sat, 3 May 2014 00:26:02 -0400 Subject: [PATCH] Improved the style of the control rendering. --- MM.cpp | 4 ++ MM.h | 12 ++++++ Mapper.cpp | 91 ++++++++++++++++++---------------------------- Mapper.h | 25 +++++++++---- MapperGLCanvas.cpp | 5 ++- Shape.cpp | 12 ++++-- Shape.h | 1 + Util.cpp | 89 +++++++++++++++++++++++++++++++++++++++++++++ Util.h | 8 ++++ 9 files changed, 177 insertions(+), 70 deletions(-) diff --git a/MM.cpp b/MM.cpp index 5191151..4871d82 100644 --- a/MM.cpp +++ b/MM.cpp @@ -23,4 +23,8 @@ const QColor MM::WHITE("#f6f5f5"); const QColor MM::BLUE_GRAY("#323541"); const QColor MM::DARK_GRAY("#272a36"); +const QColor MM::CONTROL_COLOR(BLUE_GRAY); +const QBrush MM::VERTEX_BACKGROUND(QColor(CONTROL_COLOR.red(), CONTROL_COLOR.green(), CONTROL_COLOR.blue(), 63)); +const QPen MM::SHAPE_STROKE(QBrush(CONTROL_COLOR), SHAPE_STROKE_WIDTH); +const QPen MM::SHAPE_INNER_STROKE(QBrush(CONTROL_COLOR), SHAPE_INNER_STROKE_WIDTH); diff --git a/MM.h b/MM.h index 22232b8..8108303 100644 --- a/MM.h +++ b/MM.h @@ -22,6 +22,8 @@ #include #include +#include +#include class MM { @@ -35,8 +37,18 @@ public: static const QColor BLUE_GRAY; static const QColor DARK_GRAY; + static const QColor CONTROL_COLOR; + static const QBrush VERTEX_BACKGROUND; + + static const qreal SHAPE_STROKE_WIDTH = 3; + static const qreal SHAPE_INNER_STROKE_WIDTH = 1; + static const QPen SHAPE_STROKE; + static const QPen SHAPE_INNER_STROKE; + // Control. static const qreal VERTEX_STICK_RADIUS = 10; + static const qreal VERTEX_SELECT_RADIUS = 20; + static const qreal VERTEX_SELECT_STROKE_WIDTH = 1; }; #endif diff --git a/Mapper.cpp b/Mapper.cpp index 7bcf2b8..7976954 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -63,23 +63,6 @@ QWidget* Mapper::getPropertiesEditor() return _propertyBrowser; } -void Mapper::drawShapeContour(QPainter* painter, const Shape& shape, int lineWidth, const QColor& color) -{ - Q_UNUSED(painter); - QColor rgbColor = color.toRgb(); - - glColor4f(rgbColor.redF(), rgbColor.greenF(), rgbColor.blueF(), 1.0f); - - glLineWidth(lineWidth); - glBegin (GL_LINE_STRIP); - for (int i = 0; i < shape.nVertices()+1; i++) - { - const QPointF& v = shape.getVertex(i % shape.nVertices()); - glVertex2f( v.x(), v.y() ); - } - glEnd(); -} - TextureMapper::TextureMapper(std::tr1::shared_ptr mapping) : Mapper(mapping) { @@ -149,7 +132,6 @@ void Mapper::_updateShapeProperty(QtProperty* shapeItem, Shape* shape) } } - ColorMapper::ColorMapper(Mapping::ptr mapping) : Mapper(mapping) { @@ -170,7 +152,8 @@ void PolygonColorMapper::draw(QPainter* painter) void PolygonColorMapper::drawControls(QPainter* painter) { - Q_UNUSED(painter); + Polygon* poly = (Polygon*)_mapping->getShape().get(); + Util::drawControlsPolygon(painter, *poly); } MeshColorMapper::MeshColorMapper(Mapping::ptr mapping) @@ -203,11 +186,7 @@ void MeshColorMapper::draw(QPainter* painter) void MeshColorMapper::drawControls(QPainter* painter) { std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(outputShape); - QVector outputQuads = outputMesh->getQuads(); - for (QVector::const_iterator it = outputQuads.begin(); it != outputQuads.end(); ++it) - { - drawShapeContour(painter, *it, 1, QColor(0, 0, 255)); - } + Util::drawControlsMesh(painter, *outputMesh); } void MeshColorMapper::setValue(QtProperty* property, const QVariant& value) @@ -233,13 +212,11 @@ EllipseColorMapper::EllipseColorMapper(Mapping::ptr mapping) void EllipseColorMapper::draw(QPainter* painter) { - painter->setRenderHint(QPainter::Antialiasing); painter->setPen(Qt::NoPen); painter->setBrush(color->getColor()); std::tr1::shared_ptr outputEllipse = std::tr1::static_pointer_cast(outputShape); qreal rotation = outputEllipse->getRotation(); - qDebug() << "Rotation: " << rotation << endl; painter->save(); // save painter state @@ -255,20 +232,8 @@ void EllipseColorMapper::draw(QPainter* painter) void EllipseColorMapper::drawControls(QPainter* painter) { - painter->setRenderHint(QPainter::Antialiasing); - painter->setPen(Qt::NoPen); - - painter->save(); // save painter state - std::tr1::shared_ptr outputEllipse = std::tr1::static_pointer_cast(outputShape); - painter->resetTransform(); - for (int i=0; i<4; i++) { - painter->setBrush(QColor(0, 0, 0)); - painter->setPen(QPen(QBrush(QColor(0, 0, 255)), 2)); - painter->drawEllipse(outputEllipse->getVertex(i), 5, 5); - } - - painter->restore(); // restore saved painter state + Util::drawControlsEllipse(painter, *outputEllipse); } void TextureMapper::updateShape(Shape* shape) @@ -385,16 +350,28 @@ void TextureMapper::drawInput(QPainter* painter) void TextureMapper::drawControls(QPainter* painter) { - drawShapeContour(painter, *outputShape, 3, QColor(0, 0, 255)); + } void TextureMapper::drawInputControls(QPainter* painter) { - drawShapeContour(painter, *inputShape, 3, QColor(0, 0, 255)); +// drawShapeContour(painter, *inputShape, 3, QColor(0, 0, 255)); +} + +void PolygonTextureMapper::drawControls(QPainter* painter) +{ + std::tr1::shared_ptr outputPoly = std::tr1::static_pointer_cast(outputShape); + Util::drawControlsPolygon(painter, *outputPoly); +} + +void PolygonTextureMapper::drawInputControls(QPainter* painter) +{ + std::tr1::shared_ptr inputPoly = std::tr1::static_pointer_cast(inputShape); + Util::drawControlsPolygon(painter, *inputPoly); } TriangleTextureMapper::TriangleTextureMapper(std::tr1::shared_ptr mapping) - : TextureMapper(mapping) + : PolygonTextureMapper(mapping) { } @@ -412,7 +389,7 @@ void TriangleTextureMapper::_doDraw(QPainter* painter) } MeshTextureMapper::MeshTextureMapper(std::tr1::shared_ptr mapping) - : TextureMapper(mapping) + : PolygonTextureMapper(mapping) { // Add mesh sub property. Mesh* mesh = (Mesh*)textureMapping->getShape().get(); @@ -447,23 +424,13 @@ void MeshTextureMapper::setValue(QtProperty* property, const QVariant& value) void MeshTextureMapper::drawControls(QPainter* painter) { std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(outputShape); - QVector outputQuads = outputMesh->getQuads(); - for (QVector::const_iterator it = outputQuads.begin(); it != outputQuads.end(); ++it) - { - drawShapeContour(painter, *it, 1, QColor(0, 0, 255)); - } + Util::drawControlsMesh(painter, *outputMesh); } void MeshTextureMapper::drawInputControls(QPainter* painter) { - Q_UNUSED(painter); - std::tr1::shared_ptr inputMesh = std::tr1::static_pointer_cast(inputShape); - QVector inputQuads = inputMesh->getQuads(); - for (QVector::const_iterator it = inputQuads.begin(); it != inputQuads.end(); ++it) - { - drawShapeContour(painter, *it, 1, QColor(0, 0, 255)); - } + Util::drawControlsMesh(painter, *inputMesh); } void MeshTextureMapper::_doDraw(QPainter* painter) @@ -490,10 +457,22 @@ void MeshTextureMapper::_doDraw(QPainter* painter) } EllipseTextureMapper::EllipseTextureMapper(std::tr1::shared_ptr mapping) -: TextureMapper(mapping) +: PolygonTextureMapper(mapping) { } +void EllipseTextureMapper::drawControls(QPainter* painter) +{ + std::tr1::shared_ptr outputEllipse = std::tr1::static_pointer_cast(outputShape); + Util::drawControlsEllipse(painter, *outputEllipse); +} + +void EllipseTextureMapper::drawInputControls(QPainter* painter) +{ + std::tr1::shared_ptr inputEllipse = std::tr1::static_pointer_cast(inputShape); + Util::drawControlsEllipse(painter, *inputEllipse); +} + void EllipseTextureMapper::_doDraw(QPainter* painter) { Q_UNUSED(painter); diff --git a/Mapper.h b/Mapper.h index 227e473..167a2bd 100644 --- a/Mapper.h +++ b/Mapper.h @@ -73,15 +73,12 @@ protected: public: virtual QWidget* getPropertiesEditor(); - virtual void draw(QPainter* painter) = 0; virtual void drawControls(QPainter* painter) = 0; virtual void drawInput(QPainter* painter) { Q_UNUSED(painter); } virtual void drawInputControls(QPainter* painter) { Q_UNUSED(painter); } - static void drawShapeContour(QPainter* painter, const Shape& shape, int lineWidth, const QColor& color); - public slots: virtual void setValue(QtProperty* property, const QVariant& value); virtual void updateShape(Shape* shape) @@ -106,7 +103,6 @@ protected: virtual void _buildShapeProperty(QtProperty* shapeItem, Shape* shape); virtual void _updateShapeProperty(QtProperty* shapeItem, Shape* shape); - }; /** @@ -199,7 +195,19 @@ protected: std::tr1::shared_ptr inputShape; }; -class TriangleTextureMapper : public TextureMapper +class PolygonTextureMapper : public TextureMapper +{ + Q_OBJECT + +public: + PolygonTextureMapper(std::tr1::shared_ptr mapping) : TextureMapper(mapping) {} + virtual ~PolygonTextureMapper() {} + + virtual void drawControls(QPainter* painter); + virtual void drawInputControls(QPainter* painter); +}; + +class TriangleTextureMapper : public PolygonTextureMapper { Q_OBJECT @@ -211,7 +219,7 @@ protected: virtual void _doDraw(QPainter* painter); }; -class MeshTextureMapper : public TextureMapper +class MeshTextureMapper : public PolygonTextureMapper { Q_OBJECT @@ -232,14 +240,15 @@ private: QtVariantProperty* _meshItem; }; -class EllipseTextureMapper : public TextureMapper { +class EllipseTextureMapper : public PolygonTextureMapper { Q_OBJECT public: EllipseTextureMapper(std::tr1::shared_ptr mapping); virtual ~EllipseTextureMapper() {} -// static void drawRotatedEllipse(float centerX, float centerY, float w, float h, float rotation, float baseRes); + virtual void drawControls(QPainter* painter); + virtual void drawInputControls(QPainter* painter); protected: virtual void _doDraw(QPainter* painter); diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index b058dba..b9fb1f9 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -62,13 +62,14 @@ void MapperGLCanvas::draw(QPainter* painter) void MapperGLCanvas::enterDraw(QPainter* painter) { - Q_UNUSED(painter); - // Clear to black. qglClearColor(Qt::black); // Clear buffer. glClear(GL_COLOR_BUFFER_BIT); + + // Antialiasing. + painter->setRenderHint(QPainter::Antialiasing); } void MapperGLCanvas::exitDraw(QPainter* painter) diff --git a/Shape.cpp b/Shape.cpp index 3dccdbb..c10f501 100644 --- a/Shape.cpp +++ b/Shape.cpp @@ -138,10 +138,14 @@ void Mesh::init(const QVector& points, int nColumns, int nRows) QPolygonF Mesh::toPolygon() const { QPolygonF polygon; - polygon.append(getVertex2d(0, 0)); - polygon.append(getVertex2d(nColumns()-1, 0)); - polygon.append(getVertex2d(nColumns()-1, nRows()-1)); - polygon.append(getVertex2d(0, nRows()-1)); + for (int i=0; i=0; i--) + polygon.append(getVertex2d(i, nRows()-1)); + for (int i=nRows()-1; i>=1; i--) + polygon.append(getVertex2d(0, i)); return polygon; } diff --git a/Shape.h b/Shape.h index eb3796f..715008f 100644 --- a/Shape.h +++ b/Shape.h @@ -177,6 +177,7 @@ public: virtual QString getType() const { return "mesh"; } + /// Returns a polygon that is formed by all the contour points of the mesh. virtual QPolygonF toPolygon() const; // Override the parent, checking to make sure the vertices are displaced correctly. diff --git a/Util.cpp b/Util.cpp index 82cd979..02bbb50 100644 --- a/Util.cpp +++ b/Util.cpp @@ -145,5 +145,94 @@ Ellipse* createEllipseForColor(int frameWidth, int frameHeight) ); } +void drawControlsVertices(QPainter* painter, const Shape& shape) +{ + for (int i=0; isetBrush(MM::VERTEX_BACKGROUND); + painter->setPen(QPen(MM::CONTROL_COLOR, strokeWidth)); + + // Draw ellipse. + painter->drawEllipse(vertex, radius, radius); + + // Draw cross. + qreal offset = sin(M_PI/4) * radius; + painter->drawLine( vertex + QPointF(offset, offset), vertex + QPointF(-offset, -offset) ); + painter->drawLine( vertex + QPointF(offset, -offset), vertex + QPointF(-offset, offset) ); +} + +void drawControlsEllipse(QPainter* painter, const Ellipse& ellipse) +{ + // Init colors and stroke. + painter->setPen(MM::SHAPE_STROKE); + painter->setBrush(Qt::NoBrush); + + // Draw ellipse contour. + qreal rotation = ellipse.getRotation(); + + painter->save(); // save painter state + + painter->resetTransform(); + const QPointF& center = ellipse.getCenter(); + painter->translate(center); + painter->rotate(rotation); + painter->drawEllipse(QPointF(0,0), ellipse.getHorizontalRadius(), ellipse.getVerticalRadius()); + + painter->restore(); // restore saved painter state + + // Draw control points. + drawControlsVertices(painter, ellipse); +} + +void drawControlsQuad(QPainter* painter, const Quad& quad) +{ + // Init colors and stroke. + painter->setPen(MM::SHAPE_STROKE); + + // Draw quad. + painter->drawPolygon(quad.toPolygon()); + + // Draw control points. + drawControlsVertices(painter, quad); +} + +void drawControlsMesh(QPainter* painter, const Mesh& mesh) +{ + // Init colors and stroke. + painter->setPen(MM::SHAPE_INNER_STROKE); + + // Draw inner quads. + QVector quads = mesh.getQuads(); + for (QVector::const_iterator it = quads.begin(); it != quads.end(); ++it) + { + painter->drawPolygon(it->toPolygon()); + } + + // Draw outer quad. + painter->setPen(MM::SHAPE_STROKE); + painter->drawPolygon(mesh.toPolygon()); + + // Draw control points. + drawControlsVertices(painter, mesh); +} + +void drawControlsPolygon(QPainter* painter, const Polygon& polygon) +{ + // Init colors and stroke. + painter->setPen(MM::SHAPE_STROKE); + + // Draw inner quads. + painter->drawPolygon(polygon.toPolygon()); + + // Draw control points. + drawControlsVertices(painter, polygon); +} + + } // end of namespace diff --git a/Util.h b/Util.h index 659484e..44b6692 100644 --- a/Util.h +++ b/Util.h @@ -26,6 +26,7 @@ #include #endif +#include "MM.h" #include "Shape.h" #include "Paint.h" @@ -55,6 +56,13 @@ Quad* createQuadForColor(int frameWidth, int frameHeight); Triangle* createTriangleForColor(int frameWidth, int frameHeight); Ellipse* createEllipseForColor(int frameWidth, int frameHeight); +void drawControlsVertices(QPainter* painter, const Shape& shape); +void drawControlsVertex(QPainter* painter, const QPointF& vertex, qreal radius = MM::VERTEX_SELECT_RADIUS, qreal strokeWidth = MM::VERTEX_SELECT_STROKE_WIDTH); +void drawControlsEllipse(QPainter* painter, const Ellipse& ellipse); +void drawControlsQuad(QPainter* painter, const Quad& quad); +void drawControlsMesh(QPainter* painter, const Mesh& mesh); +void drawControlsPolygon(QPainter* painter, const Polygon& polygon); + } // end of namespace #endif /* UTIL_H_ */