mirror of
https://github.com/mapmapteam/mapmap.git
synced 2026-06-16 12:33:19 +02:00
Added basic support for color ellipses
This commit is contained in:
@@ -80,6 +80,7 @@ void MainWindow::handlePaintItemSelectionChanged()
|
||||
// Enable creation of mappings when a paint is selected.
|
||||
addQuadAction->setEnabled(true);
|
||||
addTriangleAction->setEnabled(true);
|
||||
addEllipseAction->setEnabled(true);
|
||||
|
||||
// Update canvases.
|
||||
updateAll();
|
||||
@@ -281,6 +282,41 @@ void MainWindow::addTriangle()
|
||||
addMappingItem(mappingId);
|
||||
}
|
||||
|
||||
void MainWindow::addEllipse()
|
||||
{
|
||||
// A paint must be selected to add a mapping.
|
||||
if (getCurrentPaintId() == NULL_UID)
|
||||
return;
|
||||
|
||||
// Create default quad.
|
||||
|
||||
// Retrieve current paint (as texture).
|
||||
Paint::ptr paint = MainWindow::getInstance().getMappingManager().getPaint(getCurrentPaintId());
|
||||
Q_CHECK_PTR(paint);
|
||||
|
||||
// Create input and output quads.
|
||||
Mapping* mappingPtr;
|
||||
if (paint->getType() == "color")
|
||||
{
|
||||
Shape::ptr outputEllipse = Shape::ptr(Util::createEllipseForColor(sourceCanvas->width(), sourceCanvas->height()));
|
||||
mappingPtr = new ColorMapping(paint, outputEllipse);
|
||||
}
|
||||
else
|
||||
{
|
||||
// std::tr1::shared_ptr<Texture> texture = std::tr1::static_pointer_cast<Texture>(paint);
|
||||
// Q_CHECK_PTR(texture);
|
||||
//
|
||||
// Shape::ptr outputEllipse = Shape::ptr(Util::createEllipseForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height()));
|
||||
// Shape::ptr inputEllipse = Shape::ptr(Util::createEllipseForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height()));
|
||||
// mappingPtr = new TextureMapping(paint, inputEllipse, outputEllipse);
|
||||
}
|
||||
|
||||
// Create mapping.
|
||||
Mapping::ptr mapping(mappingPtr);
|
||||
uint mappingId = mappingManager->addMapping(mapping);
|
||||
addMappingItem(mappingId);
|
||||
}
|
||||
|
||||
void MainWindow::about()
|
||||
{
|
||||
QMessageBox::about(this, tr("About LibreMapping"),
|
||||
@@ -527,6 +563,34 @@ uid MainWindow::createTriangleColorMapping(uid mappingId,
|
||||
}
|
||||
}
|
||||
|
||||
uid MainWindow::createEllipseColorMapping(uid mappingId,
|
||||
uid paintId,
|
||||
const QList<QPointF> &dst)
|
||||
{
|
||||
// Cannot create element with already existing id or element for which no paint exists.
|
||||
if (Mapping::getUidAllocator().exists(mappingId) ||
|
||||
!Paint::getUidAllocator().exists(paintId) ||
|
||||
paintId == NULL_UID)
|
||||
return NULL_UID;
|
||||
|
||||
else
|
||||
{
|
||||
Paint::ptr paint = mappingManager->getPaintById(paintId);
|
||||
Q_ASSERT(dst.size() == 4);
|
||||
|
||||
Shape::ptr outputEllipse(new Ellipse(dst[0], dst[1], dst[2], dst[3]));
|
||||
|
||||
// Add it to the manager.
|
||||
Mapping::ptr mapping(new ColorMapping(paint, outputEllipse, mappingId));
|
||||
uid id = mappingManager->addMapping(mapping);
|
||||
|
||||
// Add it to the GUI.
|
||||
addMappingItem(mappingId);
|
||||
|
||||
// Return the id.
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::windowModified()
|
||||
{
|
||||
@@ -700,6 +764,12 @@ void MainWindow::createActions()
|
||||
addTriangleAction->setStatusTip(tr("Add triangle"));
|
||||
connect(addTriangleAction, SIGNAL(triggered()), this, SLOT(addTriangle()));
|
||||
addTriangleAction->setEnabled(false);
|
||||
|
||||
addEllipseAction = new QAction(tr("Add &Ellipse"), this);
|
||||
addEllipseAction->setIcon(QIcon(":/images/draw-ellipse-2.png"));
|
||||
addEllipseAction->setStatusTip(tr("Add ellipse"));
|
||||
connect(addEllipseAction, SIGNAL(triggered()), this, SLOT(addEllipse()));
|
||||
addEllipseAction->setEnabled(false);
|
||||
}
|
||||
|
||||
void MainWindow::createMenus()
|
||||
@@ -765,6 +835,7 @@ void MainWindow::createToolBars()
|
||||
fileToolBar->addSeparator();
|
||||
fileToolBar->addAction(addQuadAction);
|
||||
fileToolBar->addAction(addTriangleAction);
|
||||
fileToolBar->addAction(addEllipseAction);
|
||||
|
||||
// editToolBar = addToolBar(tr("&Edit"));
|
||||
// editToolBar->addAction(cutAction);
|
||||
@@ -1009,6 +1080,15 @@ void MainWindow::addMappingItem(uint mappingId)
|
||||
else
|
||||
mapper = Mapper::ptr(new MeshTextureMapper(textureMapping));
|
||||
}
|
||||
else if (shapeType == "ellipse")
|
||||
{
|
||||
label = QString("Ellipse %1").arg(mappingId);
|
||||
icon = QIcon(":/images/draw-ellipse-2.png");
|
||||
if (paintType == "color")
|
||||
mapper = Mapper::ptr(new EllipseColorMapper(mapping));
|
||||
else
|
||||
mapper = Mapper::ptr(new MeshTextureMapper(textureMapping));
|
||||
}
|
||||
else
|
||||
{
|
||||
label = QString("Polygon %1").arg(mappingId);
|
||||
@@ -1161,6 +1241,8 @@ void MainWindow::applyOscCommand(QVariantList & command)
|
||||
addMesh();
|
||||
else if (path == "/add/triangle")
|
||||
addTriangle();
|
||||
else if (path == "/add/ellipse")
|
||||
addEllipse();
|
||||
else if (path == "/project/save")
|
||||
save();
|
||||
else if (path == "/project/open")
|
||||
|
||||
@@ -76,6 +76,7 @@ private slots:
|
||||
void handleMappingIndexesMoved();
|
||||
void addMesh();
|
||||
void addTriangle();
|
||||
void addEllipse();
|
||||
|
||||
void windowModified();
|
||||
void pollOscInterface();
|
||||
@@ -128,6 +129,13 @@ public slots:
|
||||
uid paintId,
|
||||
const QList<QPointF> &dst);
|
||||
|
||||
/**
|
||||
* Creates a color ellipse.
|
||||
*/
|
||||
uid createEllipseColorMapping(uid mappingId,
|
||||
uid paintId,
|
||||
const QList<QPointF> &dst);
|
||||
|
||||
private:
|
||||
// Methods.
|
||||
void createLayout();
|
||||
@@ -184,6 +192,7 @@ private:
|
||||
|
||||
QAction *addQuadAction;
|
||||
QAction *addTriangleAction;
|
||||
QAction *addEllipseAction;
|
||||
|
||||
QListWidget* paintList;
|
||||
QListWidget* mappingList;
|
||||
|
||||
+46
@@ -226,6 +226,52 @@ void MeshColorMapper::setValue(QtProperty* property, const QVariant& value)
|
||||
ColorMapper::setValue(property, value);
|
||||
}
|
||||
|
||||
EllipseColorMapper::EllipseColorMapper(Mapping::ptr mapping)
|
||||
: ColorMapper(mapping) {
|
||||
}
|
||||
|
||||
void EllipseColorMapper::draw(QPainter* painter)
|
||||
{
|
||||
painter->setRenderHint(QPainter::Antialiasing);
|
||||
painter->setPen(Qt::NoPen);
|
||||
painter->setBrush(color->getColor());
|
||||
|
||||
std::tr1::shared_ptr<Ellipse> outputEllipse = std::tr1::static_pointer_cast<Ellipse>(outputShape);
|
||||
qreal rotation = outputEllipse->getRotation();
|
||||
qDebug() << "Rotation: " << rotation << endl;
|
||||
QRect rect = outputEllipse->getBoundingRect();
|
||||
painter->save(); // save painter state
|
||||
painter->resetTransform();
|
||||
painter->setBrush(color->getColor());
|
||||
// painter->translate()
|
||||
//painter->translate(0, rect.height()/2);
|
||||
QPointF p0 = outputEllipse->getVertex(0)->toPoint();
|
||||
QPointF center = outputEllipse->getCenter();
|
||||
float rx = rect.width() / 2;
|
||||
float ry = rect.height() / 2;
|
||||
painter->translate(center);
|
||||
painter->rotate(rotation);
|
||||
painter->drawEllipse(QPointF(0,0), rx, ry);
|
||||
// painter->translate(p0.x() + rect.width()/2, p0.y() + rect.height() / 2);
|
||||
//painter->drawEllipse(rect);
|
||||
|
||||
painter->resetTransform();
|
||||
for (int i=0; i<4; i++) {
|
||||
if (i==0)
|
||||
painter->setBrush(QColor("#333333"));
|
||||
else if (i==1)
|
||||
painter->setBrush(QColor("#777777"));
|
||||
else if (i==2)
|
||||
painter->setBrush(QColor("#aaaaaa"));
|
||||
else
|
||||
painter->setBrush(QColor("#ffffff"));
|
||||
|
||||
painter->drawEllipse(outputEllipse->getVertex(i)->toPoint(), 5, 5);
|
||||
painter->drawStaticText(outputEllipse->getVertex(i)->toPoint(), QString(i));
|
||||
}
|
||||
|
||||
painter->restore(); // restore saved painter state
|
||||
}
|
||||
|
||||
void TextureMapper::updateShape(Shape* shape)
|
||||
{
|
||||
|
||||
@@ -140,6 +140,17 @@ private:
|
||||
QtVariantProperty* _meshItem;
|
||||
};
|
||||
|
||||
class EllipseColorMapper : public ColorMapper {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
EllipseColorMapper(Mapping::ptr mapping);
|
||||
virtual ~EllipseColorMapper() {}
|
||||
|
||||
virtual void draw(QPainter* painter);
|
||||
// virtual void drawControls(QPainter* painter);
|
||||
};
|
||||
|
||||
/**
|
||||
* Draws a texture.
|
||||
*/
|
||||
|
||||
@@ -26,6 +26,8 @@
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include <tr1/memory>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
@@ -49,6 +51,10 @@ public:
|
||||
Q_INVOKABLE Point(qreal x, qreal y): QPointF(x,y) {}
|
||||
Q_INVOKABLE Point(): QPointF(0,0) {}
|
||||
|
||||
static qreal dist(const QPointF& p1, const QPointF& p2){
|
||||
return pow(p2.x() - p1.x(),2) + pow(p2.y() - p1.y(),2);
|
||||
}
|
||||
|
||||
signals:
|
||||
void xChanged();
|
||||
void yChanged();
|
||||
@@ -242,5 +248,47 @@ protected:
|
||||
void _reorderVertices();
|
||||
};
|
||||
|
||||
class Ellipse : public Shape {
|
||||
public:
|
||||
Ellipse() {}
|
||||
Ellipse(QPointF p1, QPointF p2, QPointF p3, QPointF p4)
|
||||
{
|
||||
_addVertex(p1);
|
||||
_addVertex(p2);
|
||||
_addVertex(p3);
|
||||
_addVertex(p4);
|
||||
}
|
||||
virtual ~Ellipse() {}
|
||||
|
||||
virtual QString getType() const { return "ellipse"; }
|
||||
|
||||
qreal getRotation() const {
|
||||
QPointF hAxis = getHorizontalAxis();
|
||||
return atan2(hAxis.y(), hAxis.x()) * 180.0 / M_PI;
|
||||
}
|
||||
|
||||
QRect getBoundingRect() const {
|
||||
return QRect(0, getVerticalAxis().manhattanLength(), getHorizontalAxis().manhattanLength(), getVerticalAxis().manhattanLength());
|
||||
}
|
||||
|
||||
QPointF getCenter() const {
|
||||
return getVertex(0)->toPoint() + (getHorizontalAxis() / 2);
|
||||
}
|
||||
|
||||
QPointF getHorizontalAxis() const {
|
||||
return getVertex(2)->toPoint() - getVertex(0)->toPoint();
|
||||
}
|
||||
|
||||
QPointF getVerticalAxis() const {
|
||||
return getVertex(1)->toPoint() - getVertex(3)->toPoint();
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void _vertexChanged(int i, Point* p=NULL) {
|
||||
// Get horizontal and vertical axis length.
|
||||
qreal hAxisLength = Point::dist(getVertex(0)->toPoint(), getVertex(2)->toPoint());
|
||||
qreal vAxisLength = Point::dist(getVertex(1)->toPoint(), getVertex(3)->toPoint());
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* SHAPE_H_ */
|
||||
|
||||
@@ -76,6 +76,22 @@ Triangle* createTriangleForTexture(Texture* texture, int frameWidth, int frameHe
|
||||
);
|
||||
}
|
||||
|
||||
Ellipse* createEllipseForTexture(Texture* texture, int frameWidth,
|
||||
int frameHeight)
|
||||
{
|
||||
qreal halfWidth = texture->getWidth() / 2;
|
||||
qreal halfHeight = texture->getHeight() / 2;
|
||||
|
||||
return new Ellipse(
|
||||
Point(texture->getX(), texture->getY() + halfHeight),
|
||||
Point(texture->getX() + halfWidth, texture->getY()),
|
||||
Point(texture->getX() + texture->getWidth(), texture->getY() + halfHeight),
|
||||
Point(texture->getX() + halfWidth, texture->getY() + texture->getHeight())
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Quad* createQuadForColor(int frameWidth, int frameHeight)
|
||||
{
|
||||
return new Quad(
|
||||
@@ -95,5 +111,15 @@ Triangle* createTriangleForColor(int frameWidth, int frameHeight)
|
||||
);
|
||||
}
|
||||
|
||||
Ellipse* createEllipseForColor(int frameWidth, int frameHeight)
|
||||
{
|
||||
return new Ellipse(
|
||||
Point(frameWidth / 4, frameHeight / 2),
|
||||
Point(frameWidth / 2, frameHeight / 4),
|
||||
Point(frameWidth * 3 / 4, frameHeight / 2),
|
||||
Point(frameWidth / 2, frameHeight * 3 / 4)
|
||||
);
|
||||
}
|
||||
|
||||
} // end of namespace
|
||||
|
||||
|
||||
@@ -41,9 +41,11 @@ int map_int(int value, int istart, int istop, int ostart, int ostop);
|
||||
|
||||
Mesh* createMeshForTexture(Texture* texture, int frameWidth, int frameHeight);
|
||||
Triangle* createTriangleForTexture(Texture* texture, int frameWidth, int frameHeight);
|
||||
Ellipse* createEllipseForTexture(Texture* texture, int frameWidth, int frameHeight);
|
||||
|
||||
Quad* createQuadForColor(int frameWidth, int frameHeight);
|
||||
Triangle* createTriangleForColor(int frameWidth, int frameHeight);
|
||||
Ellipse* createEllipseForColor(int frameWidth, int frameHeight);
|
||||
|
||||
} // end of namespace
|
||||
|
||||
|
||||
Reference in New Issue
Block a user