From 5a999f19836820dae9f3758620341dcfda56cbdb Mon Sep 17 00:00:00 2001 From: Tats Date: Sat, 30 Nov 2013 19:19:29 -0500 Subject: [PATCH] - Put all mappings and paints in a single MappingManager class - Basic compiling / running program (no quads for now) --- DestinationGLCanvas.cpp | 115 ++++++++++++++-------------- DestinationGLCanvas.h | 5 +- MainWindow.cpp | 51 +++++++++++-- MainWindow.h | 37 ++++++++- Mapper.h | 58 ++------------ MapperGLCanvas.cpp | 162 +++++++++++++++++++++------------------- MapperGLCanvas.h | 13 ++-- Mapping.h | 85 +++++++++++++++++++++ MappingManager.cpp | 78 +++++++++++++++++++ MappingManager.h | 58 ++++++++++++++ Shape.h | 2 + SourceGLCanvas.cpp | 82 +++++++++++--------- SourceGLCanvas.h | 2 +- libremapping.pro | 4 +- main.cpp | 3 +- 15 files changed, 513 insertions(+), 242 deletions(-) create mode 100644 Mapping.h create mode 100644 MappingManager.cpp create mode 100644 MappingManager.h diff --git a/DestinationGLCanvas.cpp b/DestinationGLCanvas.cpp index bcca1fe..f823457 100644 --- a/DestinationGLCanvas.cpp +++ b/DestinationGLCanvas.cpp @@ -18,78 +18,79 @@ */ #include "DestinationGLCanvas.h" +#include "MainWindow.h" DestinationGLCanvas::DestinationGLCanvas(QWidget* parent, const QGLWidget * shareWidget) : MapperGLCanvas(parent, shareWidget) { } -Quad& DestinationGLCanvas::getQuad() -{ - std::tr1::shared_ptr textureMapping = std::tr1::static_pointer_cast(Common::currentMapping); - Q_CHECK_PTR(textureMapping); - - std::tr1::shared_ptr quad = std::tr1::static_pointer_cast(Common::currentMapping->getShape()); - Q_CHECK_PTR(quad); - - return (*quad); -} +//Quad& DestinationGLCanvas::getQuad() +//{ +// std::tr1::shared_ptr textureMapping = std::tr1::static_pointer_cast(Common::currentMapping); +// Q_CHECK_PTR(textureMapping); +// +// std::tr1::shared_ptr quad = std::tr1::static_pointer_cast(Common::currentMapping->getShape()); +// Q_CHECK_PTR(quad); +// +// return (*quad); +//} void DestinationGLCanvas::doDraw() { - // No sources = nothing to do. - if (Common::nImages() == 0) - return; +// // No sources = nothing to do. +// if (Common::nImages() == 0) +// return; +// +// // TODO: Ceci est un hack necessaire car tout est en fonction de la width/height de la texture. +// // Il faut changer ca. +// std::tr1::shared_ptr textureMapping = std::tr1::static_pointer_cast(Common::currentMapping); +// Q_CHECK_PTR(textureMapping); +// +// std::tr1::shared_ptr texture = std::tr1::static_pointer_cast(textureMapping->getPaint()); +// Q_CHECK_PTR(texture); +// +// for (int i=0; i < Common::nImages(); i++) +// { +// std::tr1::shared_ptr tex = std::tr1::static_pointer_cast(Common::mappings[i]->getPaint()); +// Q_CHECK_PTR(tex); +// +// // FIXME: maybe the texture id is actually 0 and it's ok, no? +// // we should use a boolean is_texture_loaded, or so +// if (tex->getTextureId() == 0) +// { +// tex->loadTexture(); +// } +// } - // TODO: Ceci est un hack necessaire car tout est en fonction de la width/height de la texture. - // Il faut changer ca. - std::tr1::shared_ptr textureMapping = std::tr1::static_pointer_cast(Common::currentMapping); - Q_CHECK_PTR(textureMapping); - - std::tr1::shared_ptr texture = std::tr1::static_pointer_cast(textureMapping->getPaint()); - Q_CHECK_PTR(texture); - - for (int i=0; i < Common::nImages(); i++) - { - std::tr1::shared_ptr tex = std::tr1::static_pointer_cast(Common::mappings[i]->getPaint()); - Q_CHECK_PTR(tex); - - // FIXME: maybe the texture id is actually 0 and it's ok, no? - // we should use a boolean is_texture_loaded, or so - if (tex->getTextureId() == 0) - { - tex->loadTexture(); - } - } - - // Now, draw - // DRAW THE TEXTURE glPushMatrix(); - for (int i=0; i < Common::nImages(); i++) + MappingManager& mappingManager = MainWindow::getInstance().getMappingManager(); + for (int i=0; idraw(); + QuadTextureMapper mapper((TextureMapping*)mappingManager.getMapping(i).get()); + mapper.draw(); } - - // Draw the quad. - Quad& quad = getQuad(); - - glColor4f(1.0, 0.0, 0.0, 1.0); - - // Destination quad. - // Source quad. - glLineWidth(5); - glBegin (GL_LINE_STRIP); - { - for (int i=0; i<5; i++) - { - glVertex2f( - quad.getVertex(i % 4).x, - quad.getVertex(i % 4).y); - } - } - glEnd (); +// +// // Draw the quad. +// Quad& quad = getQuad(); +// +// glColor4f(1.0, 0.0, 0.0, 1.0); +// +// // Destination quad. +// // Source quad. +// glLineWidth(5); +// glBegin (GL_LINE_STRIP); +// { +// for (int i=0; i<5; i++) +// { +// glVertex2f( +// quad.getVertex(i % 4).x, +// quad.getVertex(i % 4).y); +// } +// } +// glEnd (); glPopMatrix(); } diff --git a/DestinationGLCanvas.h b/DestinationGLCanvas.h index 6d2fef1..727096e 100644 --- a/DestinationGLCanvas.h +++ b/DestinationGLCanvas.h @@ -21,6 +21,9 @@ #define DESTINATIONGLCANVAS_H_ #include "MapperGLCanvas.h" +#include "MappingManager.h" + +#include "Mapper.h" class DestinationGLCanvas: public MapperGLCanvas { @@ -30,7 +33,7 @@ public: DestinationGLCanvas(QWidget* parent = 0, const QGLWidget * shareWidget = 0); // virtual ~DestinationGLCanvas(); - virtual Quad& getQuad(); +// virtual Quad& getQuad(); private: virtual void doDraw(); diff --git a/MainWindow.cpp b/MainWindow.cpp index b4e8dd2..6eea4b3 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -21,6 +21,9 @@ MainWindow::MainWindow() { + mappingManager = new MappingManager; + currentPaintId = -1; + createLayout(); createActions(); @@ -35,6 +38,17 @@ MainWindow::MainWindow() setCurrentFile(""); } +MainWindow& MainWindow::getInstance() +{ + static MainWindow instance; + return instance; +} + +MainWindow::~MainWindow() +{ + delete mappingManager; +} + void MainWindow::handleSourceItemSelectionChanged() { std::cout << "selection changed" << std::endl; @@ -121,6 +135,11 @@ void MainWindow::import() } } +void MainWindow::addQuad() +{ + qDebug() << "add quad" << endl; +} + void MainWindow::about() { QMessageBox::about(this, tr("About Libremapping"), @@ -150,14 +169,18 @@ void MainWindow::createLayout() sourceList->setSelectionMode(QAbstractItemView::SingleSelection); sourceList->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); + shapeList = new QListWidget; + shapeList->setSelectionMode(QAbstractItemView::SingleSelection); + shapeList->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); + sourceCanvas = new SourceGLCanvas; destinationCanvas = new DestinationGLCanvas(0, sourceCanvas); - connect(sourceCanvas, SIGNAL(quadChanged()), destinationCanvas, - SLOT(updateCanvas())); + connect(sourceCanvas, SIGNAL(quadChanged()), + destinationCanvas, SLOT(updateCanvas())); - connect(destinationCanvas, SIGNAL(quadSwitched()), sourceCanvas, - SLOT(updateCanvas())); + connect(destinationCanvas, SIGNAL(imageChanged()), + sourceCanvas, SLOT(updateCanvas())); sourceCanvas->setFocusPolicy(Qt::ClickFocus); destinationCanvas->setFocusPolicy(Qt::ClickFocus); @@ -174,7 +197,11 @@ void MainWindow::createLayout() canvasSplitter->addWidget(sourceCanvas); canvasSplitter->addWidget(destinationCanvas); - mainSplitter->addWidget(sourceList); + sourceSplitter = new QSplitter(Qt::Vertical); + sourceSplitter->addWidget(sourceList); + sourceSplitter->addWidget(shapeList); + + mainSplitter->addWidget(sourceSplitter); mainSplitter->addWidget(canvasSplitter); mainSplitter->setStretchFactor(1, 1); // Upon resizing window, give the extra stretch expansion to canvasSplitter. @@ -268,6 +295,11 @@ void MainWindow::createActions() aboutAction = new QAction(tr("&About"), this); aboutAction->setStatusTip(tr("Show the application's About box")); connect(aboutAction, SIGNAL(triggered()), this, SLOT(about())); + + addQuadAction = new QAction(tr("&Add quad"), this); + addQuadAction->setIcon(QIcon(":/images/draw-rectangle-2.png")); + addQuadAction->setStatusTip(tr("Add quad")); + connect(addQuadAction, SIGNAL(triggered()), this, SLOT(addQuad())); } void MainWindow::createMenus() @@ -326,6 +358,8 @@ void MainWindow::createToolBars() fileToolBar->addAction(newAction); fileToolBar->addAction(openAction); fileToolBar->addAction(saveAction); + fileToolBar->addSeparator(); + fileToolBar->addAction(addQuadAction); // editToolBar = addToolBar(tr("&Edit")); // editToolBar->addAction(cutAction); @@ -447,10 +481,13 @@ bool MainWindow::importFile(const QString &fileName) } QApplication::setOverrideCursor(Qt::WaitCursor); - Common::addImage(fileName, sourceCanvas->width(), sourceCanvas->height()); + // Add image to model. + int imageId = mappingManager->addImage(fileName, sourceCanvas->width(), sourceCanvas->height()); + + // Add image to sourceList widget. QListWidgetItem* item = new QListWidgetItem(strippedName(fileName)); - item->setData(Qt::UserRole, Common::nImages()-1); + item->setData(Qt::UserRole, imageId); // TODO: could possibly be replaced by a Paint pointer item->setIcon(QIcon(fileName)); item->setSizeHint(QSize(item->sizeHint().width(), MainWindow::SOURCE_LIST_ITEM_HEIGHT)); sourceList->addItem(item); diff --git a/MainWindow.h b/MainWindow.h index 6bfcb44..5748937 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -22,17 +22,25 @@ #include -#include "Common.h" - -#include "DestinationGLCanvas.h" #include "SourceGLCanvas.h" +#include "DestinationGLCanvas.h" + +#include "MappingManager.h" + +#define LIBREMAPPING_VERSION "0.1" class MainWindow: public QMainWindow { Q_OBJECT public: + ~MainWindow(); + static MainWindow& getInstance(); + +private: MainWindow(); + MainWindow(MainWindow const&); + void operator=(MainWindow const&); protected: // Events. @@ -47,7 +55,9 @@ private slots: void import(); void about(); void updateStatusBar(); + void handleSourceItemSelectionChanged(); + void addQuad(); void windowModified(); @@ -95,14 +105,35 @@ private: // QAction *deleteAction; QAction *aboutAction; + QAction *addQuadAction; + QListWidget* sourceList; + QListWidget* shapeList; + SourceGLCanvas* sourceCanvas; DestinationGLCanvas* destinationCanvas; QSplitter* mainSplitter; + QSplitter* sourceSplitter; QSplitter* canvasSplitter; +private: + // Model. + MappingManager* mappingManager; + + // View. + int currentPaintId; + public: + MappingManager& getMappingManager() { return *mappingManager; } + int getCurrentPaintId() const { return currentPaintId; } + void setPaint(int id) + { + currentPaintId = id; + } + +public: + // Constants. static const int DEFAULT_WIDTH = 1600; static const int DEFAULT_HEIGHT = 800; static const int SOURCE_LIST_ITEM_HEIGHT = 40; diff --git a/Mapper.h b/Mapper.h index f2ad4f8..8eaa9e9 100644 --- a/Mapper.h +++ b/Mapper.h @@ -23,67 +23,19 @@ #define MAPPER_H_ #include + #include #include + #include #include + #include "Shape.h" #include "Paint.h" +#include "Mapping.h" + #include "Util.h" -/** - * One object in the scene that is a shape with some paint on it. - * - * A Mapping is an area of the rendering window that is drawn with - * either some texture, or any special effect that might animate a - * polygon or a line. - */ -class Mapping -{ -protected: - Paint::ptr _paint; - Shape::ptr _shape; - -public: - typedef std::tr1::shared_ptr ptr; - Mapping(Paint* paint, Shape* shape) - : _paint(paint), _shape(shape) - {} - - virtual void build() { - _paint->build(); - _shape->build(); - } - -public: - Paint::ptr getPaint() const { return _paint; } - Shape::ptr getShape() const { return _shape; } -}; - -/** - * Object whose paint is an image texture. - */ -class TextureMapping : public Mapping -{ -private: - Shape::ptr _inputShape; - -public: - TextureMapping(Paint* paint, - Shape* shape, - Shape* inputShape) - : Mapping(paint, shape), - _inputShape(inputShape) - {} - - virtual void build() { - Mapping::build(); - _inputShape->build(); - } -public: - Shape::ptr getInputShape() const { return _inputShape; } -}; - /** * A way to draw on some kind of shape. * diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index 546576a..0d9f04f 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -19,6 +19,8 @@ #include "MapperGLCanvas.h" +#include "MainWindow.h" + MapperGLCanvas::MapperGLCanvas(QWidget* parent, const QGLWidget * shareWidget) : QGLWidget(parent, shareWidget), _mousepressed(false) { @@ -95,26 +97,26 @@ void MapperGLCanvas::enterDraw() void MapperGLCanvas::mousePressEvent(QMouseEvent* event) { - int i, dist, maxdist, mindist; - int xmouse = event->x(); - int ymouse = event->y(); - maxdist = mindist = 50; - if (event->buttons() & Qt::LeftButton) - { - // std::cout << "Left Mouse key pressed" << std::endl; - for (i = 0; i < 4; i++) - { - Quad& quad = getQuad(); - Point p = quad.getVertex(i); - dist = abs(xmouse - p.x) + abs(ymouse - p.y); - if (dist < maxdist && dist < mindist) - { - quad.setActiveVertex(i); - mindist = dist; - } - } - _mousepressed = true; - } +// int i, dist, maxdist, mindist; +// int xmouse = event->x(); +// int ymouse = event->y(); +// maxdist = mindist = 50; +// if (event->buttons() & Qt::LeftButton) +// { +// // std::cout << "Left Mouse key pressed" << std::endl; +// for (i = 0; i < 4; i++) +// { +// Quad& quad = getQuad(); +// Point p = quad.getVertex(i); +// dist = abs(xmouse - p.x) + abs(ymouse - p.y); +// if (dist < maxdist && dist < mindist) +// { +// quad.setActiveVertex(i); +// mindist = dist; +// } +// } +// _mousepressed = true; +// } } void MapperGLCanvas::mouseReleaseEvent(QMouseEvent* event) @@ -125,63 +127,63 @@ void MapperGLCanvas::mouseReleaseEvent(QMouseEvent* event) void MapperGLCanvas::mouseMoveEvent(QMouseEvent* event) { - - if (_mousepressed) - { - // std::cout << "Move event " << std::endl; - Quad& quad = getQuad(); - Point p = quad.getVertex(quad.getActiveVertex()); - p.x = event->x(); - p.y = event->y(); - - quad.setVertex(quad.getActiveVertex(), p); - update(); - emit quadChanged(); - } +// +// if (_mousepressed) +// { +// // std::cout << "Move event " << std::endl; +// Quad& quad = getQuad(); +// Point p = quad.getVertex(quad.getActiveVertex()); +// p.x = event->x(); +// p.y = event->y(); +// +// quad.setVertex(quad.getActiveVertex(), p); +// update(); +// emit quadChanged(); +// } } void MapperGLCanvas::keyPressEvent(QKeyEvent* event) { - std::cout << "Key pressed" << std::endl; - int xMove = 0; - int yMove = 0; - switch (event->key()) { - case Qt::Key_Tab: - if (event->modifiers() & Qt::ControlModifier) - switchImage( (Common::getCurrentSourceId() + 1) % Common::nImages()); - else - { - Quad& quad = getQuad(); - quad.setActiveVertex((quad.getActiveVertex() + 1) % 4); - } - break; - case Qt::Key_Up: - yMove = -1; - break; - case Qt::Key_Down: - yMove = +1; - break; - case Qt::Key_Left: - xMove = -1; - break; - case Qt::Key_Right: - xMove = +1; - break; - default: - std::cerr << "Unhandled key" << std::endl; - QWidget::keyPressEvent(event); - break; - } - - Quad& quad = getQuad(); - Point p = quad.getVertex(quad.getActiveVertex()); - p.x += xMove; - p.y += yMove; - quad.setVertex(quad.getActiveVertex(), p); - - update(); - - emit quadChanged(); +// std::cout << "Key pressed" << std::endl; +// int xMove = 0; +// int yMove = 0; +// switch (event->key()) { +// case Qt::Key_Tab: +// if (event->modifiers() & Qt::ControlModifier) +// switchImage( (Common::getCurrentSourceId() + 1) % Common::nImages()); +// else +// { +// Quad& quad = getQuad(); +// quad.setActiveVertex((quad.getActiveVertex() + 1) % 4); +// } +// break; +// case Qt::Key_Up: +// yMove = -1; +// break; +// case Qt::Key_Down: +// yMove = +1; +// break; +// case Qt::Key_Left: +// xMove = -1; +// break; +// case Qt::Key_Right: +// xMove = +1; +// break; +// default: +// std::cerr << "Unhandled key" << std::endl; +// QWidget::keyPressEvent(event); +// break; +// } +// +// Quad& quad = getQuad(); +// Point p = quad.getVertex(quad.getActiveVertex()); +// p.x += xMove; +// p.y += yMove; +// quad.setVertex(quad.getActiveVertex(), p); +// +// update(); +// +// emit quadChanged(); } void MapperGLCanvas::paintEvent(QPaintEvent* /* event */) @@ -192,10 +194,20 @@ void MapperGLCanvas::paintEvent(QPaintEvent* /* event */) void MapperGLCanvas::switchImage(int imageId) { - Common::switchImage(imageId); - emit quadSwitched(); + MainWindow::getInstance().setPaint(imageId); + emit imageChanged(); } +//QSize MapperGLCanvas::sizeHint() const +//{ +// return QSize( 640, 480 ); +//} +// +//QSize MapperGLCanvas::minimumSizeHint() const +//{ +// return QSize( 320, 240 ); +//} + void MapperGLCanvas::exitDraw() { glFlush(); diff --git a/MapperGLCanvas.h b/MapperGLCanvas.h index cb5cddc..2912e3c 100644 --- a/MapperGLCanvas.h +++ b/MapperGLCanvas.h @@ -26,9 +26,11 @@ #include -#include "Common.h" +//#include "Common.h" #include "Shape.h" +//#include "MainWindow.h" + /** * Qt GUI widget that draws a mapper, which is a shape with some paint. */ @@ -37,12 +39,12 @@ class MapperGLCanvas: public QGLWidget Q_OBJECT public: - MapperGLCanvas(QWidget* parent = 0, const QGLWidget * shareWidget = 0); + MapperGLCanvas(QWidget* parent = 0, const QGLWidget* shareWidget = 0); virtual ~MapperGLCanvas() {} - virtual Quad& getQuad() = 0; - void switchImage(int imageId); +// QSize sizeHint() const; +// QSize minimumSizeHint() const; protected: void initializeGL(); @@ -55,7 +57,6 @@ protected: void mouseReleaseEvent(QMouseEvent* event); void paintEvent(QPaintEvent* event); - private: void draw(); void enterDraw(); @@ -65,7 +66,7 @@ private: signals: void quadChanged(); - void quadSwitched(); + void imageChanged(); public slots: void updateCanvas(); diff --git a/Mapping.h b/Mapping.h new file mode 100644 index 0000000..9f12fdf --- /dev/null +++ b/Mapping.h @@ -0,0 +1,85 @@ +/* + * Mapping.h + * + * (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com + * + * 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 3 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 . + */ + + +#ifndef MAPPING_H_ +#define MAPPING_H_ + +#include +#include + +#include "Shape.h" +#include "Paint.h" + +/** + * One object in the scene that is a shape with some paint on it. + * + * A Mapping is an area of the rendering window that is drawn with + * either some texture, or any special effect that might animate a + * polygon or a line. + */ +class Mapping +{ +protected: + Paint::ptr _paint; + Shape::ptr _shape; + +public: + typedef std::tr1::shared_ptr ptr; + Mapping(Paint* paint, Shape* shape) + : _paint(paint), _shape(shape) + {} + virtual ~Mapping() {} + + virtual void build() { + _paint->build(); + _shape->build(); + } + +public: + Paint::ptr getPaint() const { return _paint; } + Shape::ptr getShape() const { return _shape; } +}; + +/** + * Object whose paint is an image texture. In the case of a texture mapping we require + * an additional input shape to specify the area on the image where we pick the pixels. + */ +class TextureMapping : public Mapping +{ +private: + Shape::ptr _inputShape; + +public: + TextureMapping(Paint* paint, + Shape* shape, + Shape* inputShape) + : Mapping(paint, shape), + _inputShape(inputShape) + {} + + virtual void build() { + Mapping::build(); + _inputShape->build(); + } +public: + Shape::ptr getInputShape() const { return _inputShape; } +}; + +#endif /* MAPPING_H_ */ diff --git a/MappingManager.cpp b/MappingManager.cpp new file mode 100644 index 0000000..ecdf570 --- /dev/null +++ b/MappingManager.cpp @@ -0,0 +1,78 @@ +/* + * MappingManager.cpp + * + * (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com + * + * 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 3 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 . + */ + +#include "MappingManager.h" + +MappingManager::MappingManager() +{ + // TODO Auto-generated constructor stub + +} + +std::vector MappingManager::getPaintMappings(const Paint::ptr paint) const +{ + std::vector paintMappings; + for (std::vector::const_iterator it = mappings.begin(); it != mappings.end(); ++it) + { + if ((*it)->getPaint() == paint) + paintMappings.push_back(*it); + } + return paintMappings; +} + +std::vector MappingManager::getPaintMappings(int i) const +{ + return getPaintMappings( paints[i] ); +} + +int MappingManager::addPaint(Paint::ptr paint) +{ + paints.push_back(paint); + return paints.size()-1; +} + +int MappingManager::addImage(const QString imagePath, int frameWidth, int frameHeight) +{ + Image* img = new Image(imagePath); + + img->setPosition( (frameWidth - img->getWidth() ) / 2.0f, + (frameHeight - img->getHeight()) / 2.0f ); + + return addPaint(Paint::ptr(img)); +} + +//bool MappingManager::removePaint(Paint::ptr paint) +//{ +// +//} + +int MappingManager::addMapping(Mapping::ptr mapping) +{ + if (std::find(paints.begin(), paints.end(), mapping->getPaint()) != paints.end()) + { + mappings.push_back(mapping); + return mappings.size()-1; + } + else + return (-1); +} + +//bool MappingManager::removeMapping(Mapping::ptr mapping) +//{ +//} diff --git a/MappingManager.h b/MappingManager.h new file mode 100644 index 0000000..387ef3f --- /dev/null +++ b/MappingManager.h @@ -0,0 +1,58 @@ +/* + * MappingManager.h + * + * (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com + * + * 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 3 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 . + */ + +#ifndef MAPPINGMANAGER_H_ +#define MAPPINGMANAGER_H_ + +#include "Paint.h" +#include "Mapping.h" + +/** + * This is a container class for all the paints and mappings. It is on the model + * side. + */ +class MappingManager +{ +public: + MappingManager(); + +private: + // Model. + std::vector paints; + std::vector mappings; + +public: + /// Returns the list of mappings associated with given paint. + std::vector getPaintMappings(const Paint::ptr paint) const; + std::vector getPaintMappings(int id) const; + + int addPaint(Paint::ptr paint); +// bool removePaint(Paint::ptr paint); + int nPaints() const { return paints.size(); } + Paint::ptr getPaint(int i) { return paints[i]; } + + int addImage(const QString imagePath, int frameWidth, int frameHeight); + + int addMapping(Mapping::ptr mapping); +// bool removeMapping(Mapping::ptr mapping); + int nMappings() const { return mappings.size(); } + Mapping::ptr getMapping(int i) { return mappings[i]; } +}; + +#endif /* MAPPINGMANAGER_H_ */ diff --git a/Shape.h b/Shape.h index f4d4a35..331635b 100644 --- a/Shape.h +++ b/Shape.h @@ -51,6 +51,8 @@ public: virtual void build() {} + int nVertices() const { return vertices.size(); } + const Point& getVertex(int i) { return vertices[i]; diff --git a/SourceGLCanvas.cpp b/SourceGLCanvas.cpp index 6e16474..3fcc1e7 100644 --- a/SourceGLCanvas.cpp +++ b/SourceGLCanvas.cpp @@ -19,43 +19,44 @@ #include "SourceGLCanvas.h" +#include "MainWindow.h" + + SourceGLCanvas::SourceGLCanvas(QWidget* parent) : MapperGLCanvas(parent) { } -Quad& SourceGLCanvas::getQuad() -{ - std::tr1::shared_ptr textureMapping = std::tr1::static_pointer_cast(Common::currentMapping); - Q_CHECK_PTR(textureMapping); - - std::tr1::shared_ptr inputQuad = std::tr1::static_pointer_cast(textureMapping->getInputShape()); - Q_CHECK_PTR(inputQuad); - - return (*inputQuad); -} +//Quad& SourceGLCanvas::getQuad() +//{ +// std::tr1::shared_ptr textureMapping = std::tr1::static_pointer_cast(Common::currentMapping); +// Q_CHECK_PTR(textureMapping); +// +// std::tr1::shared_ptr inputQuad = std::tr1::static_pointer_cast(textureMapping->getInputShape()); +// Q_CHECK_PTR(inputQuad); +// +// return (*inputQuad); +//} void SourceGLCanvas::doDraw() { - // No sources = nothing to do. - if (Common::nImages() == 0) + int paintId = MainWindow::getInstance().getCurrentPaintId(); + + if (paintId < 0) return; - // TODO: Ceci est un hack necessaire car tout est en fonction de la width/height de la texture. - // Il faut changer ca. - std::tr1::shared_ptr textureMapping = std::tr1::static_pointer_cast(Common::currentMapping); - Q_CHECK_PTR(textureMapping); - - std::tr1::shared_ptr texture = std::tr1::static_pointer_cast(textureMapping->getPaint()); + // Retrieve paint (as texture) and draw it. + Paint::ptr paint = MainWindow::getInstance().getMappingManager().getPaint(paintId); + Q_CHECK_PTR(paint); + std::tr1::shared_ptr texture = std::tr1::static_pointer_cast(paint); Q_CHECK_PTR(texture); - std::cout << width() << " " << height() << std::endl; + // Load texture if needed. if (texture->getTextureId() == 0) { texture->loadTexture(); } - // Now, draw - // DRAW THE TEXTURE + // Draw the texture. glPushMatrix(); // Enable blending mode (for alphas). @@ -86,7 +87,7 @@ void SourceGLCanvas::doDraw() // float textureHalfWidth = (float) texture->getWidth() / 2.0f; // float textureHalfHeight = (float) texture->getHeight() / 2.0f; - //printf("SRC: %f %f %f %f\n", centerX, centerY, textureHalfWidth, textureHalfHeight); + glColor4f (1.0f, 1.0f, 1.0f, 1.0f); // FIXME: Does this draw the quad counterclockwise? glBegin (GL_QUADS); @@ -107,24 +108,35 @@ void SourceGLCanvas::doDraw() glDisable(GL_TEXTURE_2D); - // Draw the quad. - Quad& quad = getQuad(); + // Retrieve all mappings associated to paint. + std::vector mappings = MainWindow::getInstance().getMappingManager().getPaintMappings(paintId); - glColor4f(1.0f, 0.0f, 0.0f, 1.0f); - - // Source quad. - glLineWidth(5); - glBegin (GL_LINE_STRIP); + for (std::vector::iterator it = mappings.begin(); it != mappings.end(); ++it) { - for (int i = 0; i < 5; i++) + // TODO: Ceci est un hack necessaire car tout est en fonction de la width/height de la texture. + // Il faut changer ca. + std::tr1::shared_ptr textureMapping = std::tr1::static_pointer_cast(*it); + Q_CHECK_PTR(textureMapping); + + std::tr1::shared_ptr inputShape = std::tr1::static_pointer_cast(textureMapping->getInputShape()); + Q_CHECK_PTR(inputShape); + + glColor4f(1.0f, 0.0f, 0.0f, 1.0f); + + // Source shape. + glLineWidth(5); + glBegin (GL_LINE_STRIP); { - glVertex2f( - quad.getVertex(i % 4).x, - quad.getVertex(i % 4).y - ); + for (int i = 0; i < inputShape->nVertices()+1; i++) + { + glVertex2f( + inputShape->getVertex(i % inputShape->nVertices()).x, + inputShape->getVertex(i % inputShape->nVertices()).y + ); + } } + glEnd (); } - glEnd (); glPopMatrix(); } diff --git a/SourceGLCanvas.h b/SourceGLCanvas.h index 8981892..186d382 100644 --- a/SourceGLCanvas.h +++ b/SourceGLCanvas.h @@ -35,7 +35,7 @@ public: SourceGLCanvas(QWidget* parent = 0); // virtual ~SourceGLCanvas() {} - virtual Quad& getQuad(); +// virtual Quad& getQuad(); private: virtual void doDraw(); diff --git a/libremapping.pro b/libremapping.pro index c97fdc0..7355232 100644 --- a/libremapping.pro +++ b/libremapping.pro @@ -1,7 +1,7 @@ CONFIG += qt debug TEMPLATE = app -HEADERS = MainWindow.h Common.h Util.h SourceGLCanvas.h DestinationGLCanvas.h MapperGLCanvas.h Mapper.h Shape.h Paint.h -SOURCES = main.cpp MainWindow.cpp Common.cpp Util.cpp Mapper.cpp SourceGLCanvas.cpp DestinationGLCanvas.cpp MapperGLCanvas.cpp +HEADERS = MainWindow.h Util.h MapperGLCanvas.h SourceGLCanvas.h DestinationGLCanvas.h Mapper.h Mapping.h Shape.h Paint.h MappingManager.h +SOURCES = main.cpp MainWindow.cpp Util.cpp Mapper.cpp MapperGLCanvas.cpp SourceGLCanvas.cpp DestinationGLCanvas.cpp MappingManager.cpp QT += gui opengl LIBS += -lglut -lGLU RESOURCES = libremapping.qrc diff --git a/main.cpp b/main.cpp index b5f2517..6820146 100644 --- a/main.cpp +++ b/main.cpp @@ -18,8 +18,7 @@ int main(int argc, char *argv[]) return 1; } - MainWindow window; - window.show(); + MainWindow::getInstance().show(); return app.exec(); }