Browse Source

ran clang Format with google code Style

master
Felix Dubrownik 11 years ago
parent
commit
6d44d1cae7
  1. 139
      example/src/ofApp.cpp
  2. 5
      example/src/ofApp.h
  3. 80
      src/BaseJoint.cpp
  4. 17
      src/BaseJoint.h
  5. 55
      src/BaseSurface.cpp
  6. 37
      src/BaseSurface.h
  7. 37
      src/CircleJoint.cpp
  8. 15
      src/CircleJoint.h
  9. 16
      src/EditorType.h
  10. 17
      src/GuiMode.h
  11. 195
      src/ProjectionEditor.cpp
  12. 15
      src/ProjectionEditor.h
  13. 107
      src/QuadSurface.cpp
  14. 25
      src/QuadSurface.h
  15. 68
      src/SourcesEditor.cpp
  16. 19
      src/SourcesEditor.h
  17. 296
      src/SurfaceManager.cpp
  18. 25
      src/SurfaceManager.h
  19. 147
      src/SurfaceManagerGui.cpp
  20. 16
      src/SurfaceManagerGui.h
  21. 16
      src/SurfaceType.h
  22. 144
      src/TextureEditor.cpp
  23. 16
      src/TextureEditor.h
  24. 93
      src/TriangleSurface.cpp
  25. 21
      src/TriangleSurface.h
  26. 101
      src/ui/RadioList.cpp
  27. 21
      src/ui/RadioList.h

139
example/src/ofApp.cpp

@ -1,39 +1,38 @@
#include "ofApp.h"
void ofApp::setup()
{
void ofApp::setup() {
bShowInfo = false;
// check if the surfaces.xml file is there
// if not - load defaultSurfaces.xml
if ( ofFile::doesFileExist("surfaces.xml") ) {
if (ofFile::doesFileExist("surfaces.xml")) {
surfaceManager.loadXmlSettings("surfaces.xml");
} else {
surfaceManager.loadXmlSettings("defaultSurfaces.xml");
}
// Pass the surface manager to the mapper graphical user interface
gui.setSurfaceManager( &surfaceManager );
gui.setSurfaceManager(&surfaceManager);
// Create FBO
fbo = new ofFbo();
fbo->allocate( 500, 500 );
fbo->allocate(500, 500);
setFboAsTexture();
// Genereate rects
int numRects = 20; // change this to add more or less rects
for ( int i=0; i<numRects; i++ ) {
rects.push_back( ofRectangle(0, ofRandom(fbo->getHeight()), fbo->getWidth(), ofRandom(20)) );
rectSpeeds.push_back( (1.0f + ofRandom(5)) );
for (int i = 0; i < numRects; i++) {
rects.push_back(ofRectangle(0, ofRandom(fbo->getHeight()), fbo->getWidth(),
ofRandom(20)));
rectSpeeds.push_back((1.0f + ofRandom(5)));
}
}
void ofApp::update()
{
void ofApp::update() {
// Move rects
for ( int i=0; i<rects.size(); i++ ) {
for (int i = 0; i < rects.size(); i++) {
rects[i].y += rectSpeeds[i];
if ( rects[i].y > fbo->getHeight() ) {
if (rects[i].y > fbo->getHeight()) {
rects[i].y = -rects[i].getHeight();
}
}
@ -43,18 +42,17 @@ void ofApp::update()
ofClear(0);
ofBackground(0);
ofSetColor(255);
for ( int i=0; i<rects.size(); i++ ) {
ofRect( rects[i] );
for (int i = 0; i < rects.size(); i++) {
ofRect(rects[i]);
}
fbo->end();
}
void ofApp::draw()
{
void ofApp::draw() {
// Draw the piMapper GUI
gui.draw();
if ( bShowInfo ) {
if (bShowInfo) {
// Draw instructions
stringstream ss;
ss << "There are 4 modes:\n\n";
@ -62,7 +60,8 @@ void ofApp::draw()
ss << " 2. Texture mapping mode\n";
ss << " 3. Projection mapping mode\n";
ss << " 4. Source selection mode\n\n";
ss << "You can switch between the modes by using <1>, <2>, <3> and <4> keys on the keyboard.\n\n";
ss << "You can switch between the modes by using <1>, <2>, <3> and <4> "
"keys on the keyboard.\n\n";
ss << "Press <r> or <n> to add random or normal surface.\n";
ss << "Press <q> to add a new quad surface.\n";
ss << "Press <s> to save the composition.\n";
@ -70,56 +69,78 @@ void ofApp::draw()
ss << "Press <a> to reassign the fbo texture to the first surface\n";
ss << "Hit <i> to hide this message.";
ofDrawBitmapStringHighlight(ss.str(), 10, 20, ofColor(0,0,0,100), ofColor(255,255,255,200));
ofDrawBitmapStringHighlight(ss.str(), 10, 20, ofColor(0, 0, 0, 100),
ofColor(255, 255, 255, 200));
}
}
void ofApp::exit()
{
void ofApp::exit() {
// Clear FBO from mem
delete fbo;
}
void ofApp::keyPressed(int key)
{
void ofApp::keyPressed(int key) {
cout << "Key pressed: " << static_cast<char>(key) << endl;
switch (key) {
case '1': gui.setMode(ofx::piMapper::GuiMode::NONE); break;
case '2': gui.setMode(ofx::piMapper::GuiMode::TEXTURE_MAPPING); break;
case '3': gui.setMode(ofx::piMapper::GuiMode::PROJECTION_MAPPING); break;
case '4': gui.setMode(ofx::piMapper::GuiMode::SOURCE_SELECTION); break;
case 'i': bShowInfo = !bShowInfo; break;
case 'r': addRandomSurface(); break;
case 'q': addQuadSurface(); break;
case 'n': addSurface(); break;
case 'f': ofToggleFullscreen(); break;
case 's': surfaceManager.saveXmlSettings("surfaces.xml"); break;
case 'a': setFboAsTexture(); break;
case OF_KEY_BACKSPACE: surfaceManager.removeSelectedSurface(); break;
default: break;
case '1':
gui.setMode(ofx::piMapper::GuiMode::NONE);
break;
case '2':
gui.setMode(ofx::piMapper::GuiMode::TEXTURE_MAPPING);
break;
case '3':
gui.setMode(ofx::piMapper::GuiMode::PROJECTION_MAPPING);
break;
case '4':
gui.setMode(ofx::piMapper::GuiMode::SOURCE_SELECTION);
break;
case 'i':
bShowInfo = !bShowInfo;
break;
case 'r':
addRandomSurface();
break;
case 'q':
addQuadSurface();
break;
case 'n':
addSurface();
break;
case 'f':
ofToggleFullscreen();
break;
case 's':
surfaceManager.saveXmlSettings("surfaces.xml");
break;
case 'a':
setFboAsTexture();
break;
case OF_KEY_BACKSPACE:
surfaceManager.removeSelectedSurface();
break;
default:
break;
}
}
void ofApp::addRandomSurface()
{
void ofApp::addRandomSurface() {
int surfaceType = ofx::piMapper::SurfaceType::TRIANGLE_SURFACE;
vector<ofVec2f> vertices;
vertices.push_back( ofVec2f( ofRandomWidth(), ofRandomHeight() ) );
vertices.push_back( ofVec2f( ofRandomWidth(), ofRandomHeight() ) );
vertices.push_back( ofVec2f( ofRandomWidth(), ofRandomHeight() ) );
vertices.push_back(ofVec2f(ofRandomWidth(), ofRandomHeight()));
vertices.push_back(ofVec2f(ofRandomWidth(), ofRandomHeight()));
vertices.push_back(ofVec2f(ofRandomWidth(), ofRandomHeight()));
vector<ofVec2f> texCoords;
texCoords.push_back( ofVec2f( ofRandomuf(), ofRandomuf() ) );
texCoords.push_back( ofVec2f( ofRandomuf(), ofRandomuf() ) );
texCoords.push_back( ofVec2f( ofRandomuf(), ofRandomuf() ) );
texCoords.push_back(ofVec2f(ofRandomuf(), ofRandomuf()));
texCoords.push_back(ofVec2f(ofRandomuf(), ofRandomuf()));
texCoords.push_back(ofVec2f(ofRandomuf(), ofRandomuf()));
surfaceManager.addSurface(surfaceType, vertices, texCoords);
// select this surface right away
surfaceManager.selectSurface(surfaceManager.size()-1);
surfaceManager.selectSurface(surfaceManager.size() - 1);
}
void ofApp::addQuadSurface()
{
void ofApp::addQuadSurface() {
int surfaceType = ofx::piMapper::SurfaceType::QUAD_SURFACE;
vector<ofVec2f> vertices;
@ -138,27 +159,25 @@ void ofApp::addQuadSurface()
surfaceManager.addSurface(surfaceType, vertices, texCoords);
// select this surface right away
surfaceManager.selectSurface(surfaceManager.size()-1);
surfaceManager.selectSurface(surfaceManager.size() - 1);
}
void ofApp::addSurface()
{
void ofApp::addSurface() {
int surfaceType = ofx::piMapper::SurfaceType::TRIANGLE_SURFACE;
vector<ofVec2f> vertices;
vertices.push_back( ofVec2f( (float)ofGetWidth()/2.0f, 0.0f ) );
vertices.push_back( ofVec2f( (float)ofGetWidth(), (float)ofGetHeight() ) );
vertices.push_back( ofVec2f( 0.0f, (float)ofGetHeight() ) );
vertices.push_back(ofVec2f((float)ofGetWidth() / 2.0f, 0.0f));
vertices.push_back(ofVec2f((float)ofGetWidth(), (float)ofGetHeight()));
vertices.push_back(ofVec2f(0.0f, (float)ofGetHeight()));
vector<ofVec2f> texCoords;
texCoords.push_back( ofVec2f( 0.5f, 0.0f ) );
texCoords.push_back( ofVec2f( 1.0f, 1.0f ) );
texCoords.push_back( ofVec2f( 0.0f, 1.0f ) );
texCoords.push_back(ofVec2f(0.5f, 0.0f));
texCoords.push_back(ofVec2f(1.0f, 1.0f));
texCoords.push_back(ofVec2f(0.0f, 1.0f));
surfaceManager.addSurface(surfaceType, vertices, texCoords);
// select this surface right away
surfaceManager.selectSurface(surfaceManager.size()-1);
surfaceManager.selectSurface(surfaceManager.size() - 1);
}
void ofApp::setFboAsTexture()
{
surfaceManager.getSurface(0)->setTexture( &fbo->getTextureReference() );
void ofApp::setFboAsTexture() {
surfaceManager.getSurface(0)->setTexture(&fbo->getTextureReference());
}

5
example/src/ofApp.h

@ -3,9 +3,8 @@
#include "ofMain.h"
#include "ofxPiMapper.h"
class ofApp : public ofBaseApp
{
public:
class ofApp : public ofBaseApp {
public:
void setup();
void update();
void draw();

80
src/BaseJoint.cpp

@ -1,97 +1,65 @@
#include "BaseJoint.h"
namespace ofx{
namespace piMapper{
namespace ofx {
namespace piMapper {
BaseJoint::BaseJoint()
{
BaseJoint::BaseJoint() {
setDefaultColors();
setDefaultProperties();
registerMouseEvents();
}
BaseJoint::~BaseJoint()
{
unregisterMouseEvents();
}
BaseJoint::~BaseJoint() { unregisterMouseEvents(); }
void BaseJoint::registerMouseEvents()
{
void BaseJoint::registerMouseEvents() {
ofAddListener(ofEvents().mousePressed, this, &BaseJoint::mousePressed);
ofAddListener(ofEvents().mouseDragged, this, &BaseJoint::mouseDragged);
}
void BaseJoint::unregisterMouseEvents()
{
void BaseJoint::unregisterMouseEvents() {
ofRemoveListener(ofEvents().mousePressed, this, &BaseJoint::mousePressed);
ofRemoveListener(ofEvents().mouseDragged, this, &BaseJoint::mouseDragged);
}
void BaseJoint::mousePressed(ofMouseEventArgs& args)
{
if ( hitTest(ofVec2f(args.x, args.y)) ) {
//selected = true;
void BaseJoint::mousePressed(ofMouseEventArgs& args) {
if (hitTest(ofVec2f(args.x, args.y))) {
// selected = true;
clickDistance = position - ofVec2f(args.x, args.y);
//startDrag();
// startDrag();
}
}
void BaseJoint::mouseReleased(int x, int y, int button)
{
stopDrag();
}
void BaseJoint::mouseReleased(int x, int y, int button) { stopDrag(); }
void BaseJoint::mouseDragged(ofMouseEventArgs& args)
{
if ( !bDrag ) return;
void BaseJoint::mouseDragged(ofMouseEventArgs& args) {
if (!bDrag) return;
position = ofVec2f(args.x, args.y) + clickDistance;
}
void BaseJoint::startDrag()
{
bDrag = true;
}
void BaseJoint::startDrag() { bDrag = true; }
void BaseJoint::stopDrag()
{
bDrag = false;
}
void BaseJoint::stopDrag() { bDrag = false; }
void BaseJoint::select()
{
selected = true;
}
void BaseJoint::select() { selected = true; }
void BaseJoint::unselect()
{
selected = false;
}
void BaseJoint::unselect() { selected = false; }
void BaseJoint::setClickDistance(ofVec2f newClickDistance)
{
void BaseJoint::setClickDistance(ofVec2f newClickDistance) {
clickDistance = newClickDistance;
}
bool BaseJoint::isDragged()
{
return bDrag;
}
bool BaseJoint::isDragged() { return bDrag; }
bool BaseJoint::isSelected()
{
return selected;
}
bool BaseJoint::isSelected() { return selected; }
void BaseJoint::setDefaultColors()
{
void BaseJoint::setDefaultColors() {
fillColor = ofColor(0, 255, 255, 0);
strokeColor = ofColor(255, 255, 255);
fillColorSelected = ofColor(255, 255, 0, 0);
strokeColorSelected = ofColor(255, 0, 0);
}
void BaseJoint::setDefaultProperties()
{
void BaseJoint::setDefaultProperties() {
enabled = true;
visible = true;
position = ofVec2f(20.0f, 20.0f);
@ -100,5 +68,5 @@ void BaseJoint::setDefaultProperties()
selected = false;
strokeWidth = 1.5f;
}
}}
}
}

17
src/BaseJoint.h

@ -2,10 +2,10 @@
#include "ofMain.h"
namespace ofx{
namespace piMapper{
namespace ofx {
namespace piMapper {
class BaseJoint {
class BaseJoint {
public:
BaseJoint();
~BaseJoint();
@ -29,9 +29,9 @@ namespace piMapper{
bool isDragged();
bool isSelected();
virtual void update(){};
virtual void draw(){};
virtual bool hitTest(ofVec2f position){};
virtual void update() {};
virtual void draw() {};
virtual bool hitTest(ofVec2f position) {};
protected:
ofColor fillColor;
@ -39,12 +39,13 @@ namespace piMapper{
ofColor fillColorSelected;
ofColor strokeColorSelected;
float strokeWidth;
ofVec2f clickDistance; // How far from the center of the joint the user has clicked?
ofVec2f clickDistance; // How far from the center of the joint the user has
// clicked?
bool bDrag;
private:
void setDefaultColors();
void setDefaultProperties();
};
};
}
}

55
src/BaseSurface.cpp

@ -1,32 +1,34 @@
#include "BaseSurface.h"
namespace ofx{
namespace piMapper{
namespace ofx {
namespace piMapper {
BaseSurface::BaseSurface()
{
BaseSurface::BaseSurface() {
ofEnableNormalizedTexCoords();
createDefaultTexture();
}
void BaseSurface::createDefaultTexture()
{
void BaseSurface::createDefaultTexture() {
ofPixels pixels;
pixels.allocate(500, 500, 1);
for ( int i=0; i<pixels.size(); i++ ) {
for (int i = 0; i < pixels.size(); i++) {
pixels[i] = 255;
}
int squareSize = 10; // size of each test pattern square
bool sy = false;
for ( int y=0; y<pixels.getWidth(); y+=squareSize ) {
for (int y = 0; y < pixels.getWidth(); y += squareSize) {
bool sx = false;
for ( int x=0; x<pixels.getHeight(); x+=squareSize ) {
for ( int yi=0; yi<squareSize; yi++ ) {
for ( int xi=0; xi<squareSize; xi++ ){
if ( sx && sy ) pixels[(y+yi)*pixels.getWidth()+x+xi] = 255;
else if ( sx && !sy ) pixels[(y+yi)*pixels.getWidth()+x+xi] = 0;
else if ( !sx && sy ) pixels[(y+yi)*pixels.getWidth()+x+xi] = 0;
else pixels[(y+yi)*pixels.getWidth()+x+xi] = 255;
for (int x = 0; x < pixels.getHeight(); x += squareSize) {
for (int yi = 0; yi < squareSize; yi++) {
for (int xi = 0; xi < squareSize; xi++) {
if (sx && sy)
pixels[(y + yi) * pixels.getWidth() + x + xi] = 255;
else if (sx && !sy)
pixels[(y + yi) * pixels.getWidth() + x + xi] = 0;
else if (!sx && sy)
pixels[(y + yi) * pixels.getWidth() + x + xi] = 0;
else
pixels[(y + yi) * pixels.getWidth() + x + xi] = 255;
}
}
sx = !sx;
@ -41,12 +43,12 @@ void BaseSurface::createDefaultTexture()
texture = &defaultTexture;
}
void BaseSurface::drawTexture(ofVec2f position)
{
void BaseSurface::drawTexture(ofVec2f position) {
ofMesh texMesh;
texMesh.addVertex(position);
texMesh.addVertex(position + ofVec2f(texture->getWidth(), 0.0f));
texMesh.addVertex(position + ofVec2f(texture->getWidth(), texture->getHeight()));
texMesh.addVertex(position +
ofVec2f(texture->getWidth(), texture->getHeight()));
texMesh.addVertex(position + ofVec2f(0.0f, texture->getHeight()));
texMesh.addTriangle(0, 2, 3);
texMesh.addTriangle(0, 1, 2);
@ -59,19 +61,10 @@ void BaseSurface::drawTexture(ofVec2f position)
texture->unbind();
}
void BaseSurface::setTexture(ofTexture *texturePtr)
{
texture = texturePtr;
}
void BaseSurface::setTexture(ofTexture* texturePtr) { texture = texturePtr; }
ofTexture* BaseSurface::getTexture()
{
return texture;
}
ofTexture* BaseSurface::getTexture() { return texture; }
ofTexture* BaseSurface::getDefaultTexture()
{
return &defaultTexture;
ofTexture* BaseSurface::getDefaultTexture() { return &defaultTexture; }
}
}
}}

37
src/BaseSurface.h

@ -5,23 +5,22 @@
using namespace std;
namespace ofx{
namespace piMapper{
class BaseSurface
{
public:
namespace ofx {
namespace piMapper {
class BaseSurface {
public:
BaseSurface();
virtual void setup(){};
virtual void draw(){};
virtual void setVertex(int index, ofVec2f p){};
virtual void setTexCoord(int index, ofVec2f t){};
virtual void moveBy(ofVec2f v){};
virtual int getType(){};
virtual bool hitTest(ofVec2f p){};
virtual ofPolyline getHitArea(){};
virtual ofPolyline getTextureHitArea(){};
virtual vector<ofVec3f>& getVertices(){};
virtual vector<ofVec2f>& getTexCoords(){};
virtual void setup() {};
virtual void draw() {};
virtual void setVertex(int index, ofVec2f p) {};
virtual void setTexCoord(int index, ofVec2f t) {};
virtual void moveBy(ofVec2f v) {};
virtual int getType() {};
virtual bool hitTest(ofVec2f p) {};
virtual ofPolyline getHitArea() {};
virtual ofPolyline getTextureHitArea() {};
virtual vector<ofVec3f>& getVertices() {};
virtual vector<ofVec2f>& getTexCoords() {};
// Draws a texture using ofMesh
void drawTexture(ofVec2f position);
@ -30,12 +29,12 @@ public:
ofTexture* getTexture();
ofTexture* getDefaultTexture();
protected:
protected:
ofMesh mesh;
ofTexture* texture;
ofTexture defaultTexture;
void createDefaultTexture();
};
}}
}
}

37
src/CircleJoint.cpp

@ -1,27 +1,22 @@
#include "CircleJoint.h"
namespace ofx{
namespace piMapper{
namespace ofx {
namespace piMapper {
CircleJoint::CircleJoint()
{
setDefaultProperties();
}
CircleJoint::CircleJoint() { setDefaultProperties(); }
void CircleJoint::update()
{
void CircleJoint::update() {
if (!enabled) return;
}
void CircleJoint::draw()
{
void CircleJoint::draw() {
if (!visible) return;
if (!enabled) return;
ofPushStyle();
ofFill();
if ( selected ) {
if (selected) {
ofSetColor(fillColorSelected);
} else {
ofSetColor(fillColor);
@ -30,7 +25,7 @@ void CircleJoint::draw()
ofCircle(position.x, position.y, radius);
ofNoFill();
if ( selected ) {
if (selected) {
ofSetColor(strokeColorSelected);
} else {
ofSetColor(strokeColor);
@ -41,16 +36,14 @@ void CircleJoint::draw()
ofPopStyle();
}
void CircleJoint::setDefaultProperties()
{
radius = 10.0f;
}
void CircleJoint::setDefaultProperties() { radius = 10.0f; }
bool CircleJoint::hitTest(ofVec2f pos)
{
bool CircleJoint::hitTest(ofVec2f pos) {
float distance = position.distance(pos);
if ( distance < radius ) return true;
else return false;
if (distance < radius)
return true;
else
return false;
}
}
}
}}

15
src/CircleJoint.h

@ -3,21 +3,20 @@
#include "ofMain.h"
#include "BaseJoint.h"
namespace ofx{
namespace piMapper{
class CircleJoint : public BaseJoint
{
public:
namespace ofx {
namespace piMapper {
class CircleJoint : public BaseJoint {
public:
CircleJoint();
void update();
void draw();
bool hitTest(ofVec2f position);
private:
private:
float radius;
void setDefaultProperties();
};
}}
}
}

16
src/EditorType.h

@ -1,13 +1,9 @@
#pragma once
namespace ofx{
namespace piMapper{
struct EditorType
{
enum {
TEXTURE,
PROJECTION
};
namespace ofx {
namespace piMapper {
struct EditorType {
enum { TEXTURE, PROJECTION };
};
}}
}
}

17
src/GuiMode.h

@ -1,14 +1,9 @@
#pragma once
namespace ofx{
namespace piMapper{
struct GuiMode
{
enum {
NONE,
TEXTURE_MAPPING,
PROJECTION_MAPPING,
SOURCE_SELECTION
};
namespace ofx {
namespace piMapper {
struct GuiMode {
enum { NONE, TEXTURE_MAPPING, PROJECTION_MAPPING, SOURCE_SELECTION };
};
}}
}
}

195
src/ProjectionEditor.cpp

@ -1,77 +1,68 @@
#include "ProjectionEditor.h"
namespace ofx{
namespace piMapper{
ProjectionEditor::ProjectionEditor()
{
namespace ofx {
namespace piMapper {
ProjectionEditor::ProjectionEditor() {
surfaceManager = NULL;
bShiftKeyDown = false;
fSnapDistance = 10.0f;
enable();
}
ProjectionEditor::~ProjectionEditor()
{
ProjectionEditor::~ProjectionEditor() {
clearJoints();
surfaceManager = NULL;
disable();
}
void ProjectionEditor::registerAppEvents()
{
void ProjectionEditor::registerAppEvents() {
ofAddListener(ofEvents().update, this, &ProjectionEditor::update);
ofAddListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage);
}
void ProjectionEditor::unregisterAppEvents()
{
void ProjectionEditor::unregisterAppEvents() {
ofRemoveListener(ofEvents().update, this, &ProjectionEditor::update);
ofRemoveListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage);
ofRemoveListener(ofEvents().messageEvent, this,
&ProjectionEditor::gotMessage);
}
void ProjectionEditor::registerMouseEvents()
{
void ProjectionEditor::registerMouseEvents() {
ofAddListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged);
}
void ProjectionEditor::unregisterMouseEvents()
{
ofRemoveListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged);
void ProjectionEditor::unregisterMouseEvents() {
ofRemoveListener(ofEvents().mouseDragged, this,
&ProjectionEditor::mouseDragged);
}
void ProjectionEditor::registerKeyEvents()
{
void ProjectionEditor::registerKeyEvents() {
ofAddListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed);
ofAddListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased);
}
void ProjectionEditor::unregisterKeyEvents()
{
void ProjectionEditor::unregisterKeyEvents() {
ofRemoveListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed);
ofRemoveListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased);
ofRemoveListener(ofEvents().keyReleased, this,
&ProjectionEditor::keyReleased);
}
void ProjectionEditor::enable()
{
void ProjectionEditor::enable() {
registerAppEvents();
registerMouseEvents();
registerKeyEvents();
}
void ProjectionEditor::disable()
{
void ProjectionEditor::disable() {
unregisterAppEvents();
unregisterMouseEvents();
unregisterKeyEvents();
}
void ProjectionEditor::update(ofEventArgs &args)
{
void ProjectionEditor::update(ofEventArgs& args) {
// update surface if one of the joints is being dragged
for ( int i=0; i<joints.size(); i++ ) {
if ( joints[i]->isDragged() || joints[i]->isSelected() ) {
if ( surfaceManager->getSelectedSurface() != NULL ) {
for (int i = 0; i < joints.size(); i++) {
if (joints[i]->isDragged() || joints[i]->isSelected()) {
if (surfaceManager->getSelectedSurface() != NULL) {
// update vertex to new location
surfaceManager->getSelectedSurface()->setVertex(i, joints[i]->position);
} else {
@ -85,38 +76,36 @@ void ProjectionEditor::update(ofEventArgs &args)
}
}
void ProjectionEditor::draw()
{
if ( surfaceManager == NULL ) return;
if ( surfaceManager->getSelectedSurface() == NULL ) return;
if ( joints.size() <= 0 ) createJoints();
void ProjectionEditor::draw() {
if (surfaceManager == NULL) return;
if (surfaceManager->getSelectedSurface() == NULL) return;
if (joints.size() <= 0) createJoints();
drawJoints();
}
void ProjectionEditor::mouseDragged(ofMouseEventArgs &args)
{
void ProjectionEditor::mouseDragged(ofMouseEventArgs& args) {
ofVec2f mousePosition = ofVec2f(args.x, args.y);
// Collect all vertices of the projection surfaces
vector<ofVec3f*> allVertices;
for ( int i=0; i<surfaceManager->size(); i++ ) {
for (int i = 0; i < surfaceManager->size(); i++) {
BaseSurface* surface = surfaceManager->getSurface(i);
if ( surface == surfaceManager->getSelectedSurface() ) {
if (surface == surfaceManager->getSelectedSurface()) {
continue; // Don't add vertices of selected surface
}
for ( int j=0; j<surface->getVertices().size(); j++ ) {
for (int j = 0; j < surface->getVertices().size(); j++) {
allVertices.push_back(&surface->getVertices()[j]);
}
}
// Snap currently dragged joint to nearest vertex
for ( int i=0; i<joints.size(); i++ ) {
if ( joints[i]->isDragged() ) {
for (int i = 0; i < joints.size(); i++) {
if (joints[i]->isDragged()) {
// Snap it!
for ( int j=0; j<allVertices.size(); j++ ) {
for (int j = 0; j < allVertices.size(); j++) {
float distance = mousePosition.distance(*allVertices[j]);
//cout << "distance: " << distance << endl;
if ( distance < fSnapDistance ) {
// cout << "distance: " << distance << endl;
if (distance < fSnapDistance) {
joints[i]->position = *allVertices[j];
ofVec2f clickDistance = joints[i]->position - ofVec2f(args.x, args.y);
joints[i]->setClickDistance(clickDistance);
@ -127,33 +116,44 @@ void ProjectionEditor::mouseDragged(ofMouseEventArgs &args)
}
}
void ProjectionEditor::keyPressed(ofKeyEventArgs &args)
{
void ProjectionEditor::keyPressed(ofKeyEventArgs& args) {
int key = args.key;
float moveStep;
if (bShiftKeyDown) moveStep = 10.0f;
else moveStep = 0.5f;
if (bShiftKeyDown)
moveStep = 10.0f;
else
moveStep = 0.5f;
switch (key) {
case OF_KEY_LEFT: moveSelection(ofVec2f(-moveStep,0.0f)); break;
case OF_KEY_RIGHT: moveSelection(ofVec2f(moveStep,0.0f)); break;
case OF_KEY_UP: moveSelection(ofVec2f(0.0f,-moveStep)); break;
case OF_KEY_DOWN: moveSelection(ofVec2f(0.0f,moveStep)); break;
case OF_KEY_SHIFT: bShiftKeyDown = true; break;
case OF_KEY_LEFT:
moveSelection(ofVec2f(-moveStep, 0.0f));
break;
case OF_KEY_RIGHT:
moveSelection(ofVec2f(moveStep, 0.0f));
break;
case OF_KEY_UP:
moveSelection(ofVec2f(0.0f, -moveStep));
break;
case OF_KEY_DOWN:
moveSelection(ofVec2f(0.0f, moveStep));
break;
case OF_KEY_SHIFT:
bShiftKeyDown = true;
break;
}
}
void ProjectionEditor::keyReleased(ofKeyEventArgs &args)
{
void ProjectionEditor::keyReleased(ofKeyEventArgs& args) {
int key = args.key;
switch (key) {
case OF_KEY_SHIFT: bShiftKeyDown = false; break;
case OF_KEY_SHIFT:
bShiftKeyDown = false;
break;
}
}
void ProjectionEditor::gotMessage(ofMessage& msg)
{
void ProjectionEditor::gotMessage(ofMessage& msg) {
if (msg.message == "surfaceSelected") {
// refresh gui
clearJoints();
@ -161,77 +161,72 @@ void ProjectionEditor::gotMessage(ofMessage& msg)
}
}
void ProjectionEditor::setSurfaceManager(SurfaceManager *newSurfaceManager)
{
void ProjectionEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) {
surfaceManager = newSurfaceManager;
}
void ProjectionEditor::clearJoints()
{
while ( joints.size() ) {
void ProjectionEditor::clearJoints() {
while (joints.size()) {
delete joints.back();
joints.pop_back();
}
}
void ProjectionEditor::createJoints()
{
if ( surfaceManager == NULL ) return;
void ProjectionEditor::createJoints() {
if (surfaceManager == NULL) return;
clearJoints();
if ( surfaceManager->getSelectedSurface() == NULL ) {
if (surfaceManager->getSelectedSurface() == NULL) {
ofLog(OF_LOG_WARNING, "Trying to create joints while no surface selected.");
return;
}
vector<ofVec3f>& vertices = surfaceManager->getSelectedSurface()->getVertices();
vector<ofVec3f>& vertices =
surfaceManager->getSelectedSurface()->getVertices();
for ( int i=0; i<vertices.size(); i++ ) {
joints.push_back( new CircleJoint() );
for (int i = 0; i < vertices.size(); i++) {
joints.push_back(new CircleJoint());
joints.back()->position = ofVec2f(vertices[i].x, vertices[i].y);
}
}
void ProjectionEditor::updateJoints()
{
vector<ofVec3f>& vertices = surfaceManager->getSelectedSurface()->getVertices();
for ( int i=0; i<vertices.size(); i++ ) {
void ProjectionEditor::updateJoints() {
vector<ofVec3f>& vertices =
surfaceManager->getSelectedSurface()->getVertices();
for (int i = 0; i < vertices.size(); i++) {
joints[i]->position = ofVec2f(vertices[i].x, vertices[i].y);
}
}
void ProjectionEditor::unselectAllJoints()
{
for ( int i=0; i<joints.size(); i++ ) {
void ProjectionEditor::unselectAllJoints() {
for (int i = 0; i < joints.size(); i++) {
joints[i]->unselect();
}
}
void ProjectionEditor::moveSelectedSurface(ofVec2f by)
{
if ( surfaceManager == NULL ) return;
if ( surfaceManager->getSelectedSurface() == NULL ) return;
void ProjectionEditor::moveSelectedSurface(ofVec2f by) {
if (surfaceManager == NULL) return;
if (surfaceManager->getSelectedSurface() == NULL) return;
surfaceManager->getSelectedSurface()->moveBy(by);
/*vector<ofVec3f>& vertices = surfaceManager->getSelectedSurface()->getVertices();
/*vector<ofVec3f>& vertices =
surfaceManager->getSelectedSurface()->getVertices();
for (int i=0; i<vertices.size(); i++) {
vertices[i] += by;
}*/
updateJoints();
}
void ProjectionEditor::stopDragJoints()
{
for (int i=0; i<joints.size(); i++){
void ProjectionEditor::stopDragJoints() {
for (int i = 0; i < joints.size(); i++) {
joints[i]->stopDrag();
}
}
void ProjectionEditor::moveSelection(ofVec2f by)
{
void ProjectionEditor::moveSelection(ofVec2f by) {
// check if joints selected
bool bJointSelected = false;
BaseJoint* selectedJoint;
for ( int i=0; i<joints.size(); i++ ) {
for (int i = 0; i < joints.size(); i++) {
if (joints[i]->isSelected()) {
bJointSelected = true;
selectedJoint = joints[i];
@ -239,32 +234,30 @@ void ProjectionEditor::moveSelection(ofVec2f by)
}
}
if ( bJointSelected ) {
if (bJointSelected) {
selectedJoint->position += by;
} else {
moveSelectedSurface(by);
}
}
void ProjectionEditor::setSnapDistance(float newSnapDistance)
{
void ProjectionEditor::setSnapDistance(float newSnapDistance) {
fSnapDistance = newSnapDistance;
}
CircleJoint* ProjectionEditor::hitTestJoints(ofVec2f pos)
{
for ( int i=0; i<joints.size(); i++ ) {
if ( joints[i]->hitTest(pos) ){
CircleJoint* ProjectionEditor::hitTestJoints(ofVec2f pos) {
for (int i = 0; i < joints.size(); i++) {
if (joints[i]->hitTest(pos)) {
return joints[i];
}
}
return NULL;
}
void ProjectionEditor::drawJoints()
{
for ( int i=0; i<joints.size(); i++ ) {
void ProjectionEditor::drawJoints() {
for (int i = 0; i < joints.size(); i++) {
joints[i]->draw();
}
}
}}
}
}

15
src/ProjectionEditor.h

@ -3,11 +3,10 @@
#include "SurfaceManager.h"
#include "CircleJoint.h"
namespace ofx{
namespace piMapper{
class ProjectionEditor
{
public:
namespace ofx {
namespace piMapper {
class ProjectionEditor {
public:
ProjectionEditor();
~ProjectionEditor();
@ -39,7 +38,7 @@ public:
void setSnapDistance(float newSnapDistance);
CircleJoint* hitTestJoints(ofVec2f pos);
private:
private:
SurfaceManager* surfaceManager;
vector<CircleJoint*> joints;
bool bShiftKeyDown;
@ -47,5 +46,5 @@ private:
void drawJoints();
};
}}
}
}

107
src/QuadSurface.cpp

@ -1,20 +1,15 @@
#include "QuadSurface.h"
namespace ofx{
namespace piMapper{
QuadSurface::QuadSurface()
{
namespace ofx {
namespace piMapper {
QuadSurface::QuadSurface() {
cout << "QuadSurface constructor." << endl;
setup();
}
QuadSurface::~QuadSurface()
{
cout << "QuadSurface destructor." << endl;
}
QuadSurface::~QuadSurface() { cout << "QuadSurface destructor." << endl; }
void QuadSurface::setup()
{
void QuadSurface::setup() {
// Create 4 points for the 2 triangles
ofVec2f p1 = ofVec2f(0, 0);
ofVec2f p2 = ofVec2f(0, ofGetHeight());
@ -27,12 +22,12 @@ void QuadSurface::setup()
ofVec2f t3 = ofVec2f(ofVec2f(1.0f, 1.0f));
ofVec2f t4 = ofVec2f(ofVec2f(0.0f, 1.0f));
setup( p1, p2, p3, p4, t1, t2, t3, t4, texture );
setup(p1, p2, p3, p4, t1, t2, t3, t4, texture);
}
void QuadSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4,
ofVec2f t1, ofVec2f t2, ofVec2f t3, ofVec2f t4, ofTexture* texturePtr )
{
void QuadSurface::setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4,
ofVec2f t1, ofVec2f t2, ofVec2f t3, ofVec2f t4,
ofTexture* texturePtr) {
// Assign texture
texture = texturePtr;
@ -40,10 +35,10 @@ void QuadSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4,
mesh.clear();
// Create a surface with the points
mesh.addVertex( p1 );
mesh.addVertex( p2 );
mesh.addVertex( p3 );
mesh.addVertex( p4 );
mesh.addVertex(p1);
mesh.addVertex(p2);
mesh.addVertex(p3);
mesh.addVertex(p4);
// Add 2 triangles
mesh.addTriangle(0, 2, 3);
@ -63,7 +58,7 @@ void QuadSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4,
quadIndices[3] = 0;
quadIndices[4] = 2;
quadIndices[5] = 3;
//tex coords (those are alway 0)
// tex coords (those are alway 0)
quadTexCoordinates[2] = 0;
quadTexCoordinates[6] = 0;
quadTexCoordinates[10] = 0;
@ -72,8 +67,7 @@ void QuadSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4,
calculate4dTextureCoords();
}
void QuadSurface::draw()
{
void QuadSurface::draw() {
/*if(mesh.haveVertsChanged() || mesh.haveTexCoordsChanged()){
calculate4dTextureCoords();
}*/
@ -86,9 +80,8 @@ void QuadSurface::draw()
texture->unbind();
}
void QuadSurface::setVertex(int index, ofVec2f p)
{
if ( index > 3 ) {
void QuadSurface::setVertex(int index, ofVec2f p) {
if (index > 3) {
ofLog() << "Vertex with this index does not exist: " << index << endl;
return;
}
@ -97,10 +90,10 @@ void QuadSurface::setVertex(int index, ofVec2f p)
calculate4dTextureCoords();
}
void QuadSurface::setTexCoord(int index, ofVec2f t)
{
if ( index > 3 ) {
ofLog() << "Texture coordinate with this index does not exist: " << index << endl;
void QuadSurface::setTexCoord(int index, ofVec2f t) {
if (index > 3) {
ofLog() << "Texture coordinate with this index does not exist: " << index
<< endl;
return;
}
@ -108,35 +101,29 @@ void QuadSurface::setTexCoord(int index, ofVec2f t)
calculate4dTextureCoords();
}
void QuadSurface::moveBy(ofVec2f v)
{
void QuadSurface::moveBy(ofVec2f v) {
vector<ofVec3f>& vertices = getVertices();
for (int i=0; i<vertices.size(); i++) {
for (int i = 0; i < vertices.size(); i++) {
vertices[i] += v;
}
calculate4dTextureCoords();
}
int QuadSurface::getType()
{
return SurfaceType::QUAD_SURFACE;
}
int QuadSurface::getType() { return SurfaceType::QUAD_SURFACE; }
bool QuadSurface::hitTest(ofVec2f p)
{
bool QuadSurface::hitTest(ofVec2f p) {
// Construct ofPolyline from vertices
ofPolyline line = getHitArea();
if ( line.inside(p.x, p.y) ) {
if (line.inside(p.x, p.y)) {
return true;
} else {
return false;
}
}
ofVec2f QuadSurface::getVertex(int index)
{
if ( index > 3 ) {
ofVec2f QuadSurface::getVertex(int index) {
if (index > 3) {
ofLog() << "Vertex with this index does not exist: " << index << endl;
throw std::runtime_error("Vertex index out of bounds.");
}
@ -145,8 +132,7 @@ ofVec2f QuadSurface::getVertex(int index)
return ofVec2f(vert.x, vert.y);
}
ofVec2f QuadSurface::getTexCoord(int index)
{
ofVec2f QuadSurface::getTexCoord(int index) {
if (index > 3) {
throw std::runtime_error("Texture coordinate index out of bounds.");
}
@ -154,45 +140,37 @@ ofVec2f QuadSurface::getTexCoord(int index)
return mesh.getTexCoord(index);
}
ofPolyline QuadSurface::getHitArea()
{
ofPolyline QuadSurface::getHitArea() {
ofPolyline line;
line.addVertex( ofPoint( mesh.getVertex(0).x, mesh.getVertex(0).y ) );
line.addVertex( ofPoint( mesh.getVertex(1).x, mesh.getVertex(1).y ) );
line.addVertex( ofPoint( mesh.getVertex(2).x, mesh.getVertex(2).y ) );
line.addVertex( ofPoint( mesh.getVertex(3).x, mesh.getVertex(3).y ) );
line.addVertex(ofPoint(mesh.getVertex(0).x, mesh.getVertex(0).y));
line.addVertex(ofPoint(mesh.getVertex(1).x, mesh.getVertex(1).y));
line.addVertex(ofPoint(mesh.getVertex(2).x, mesh.getVertex(2).y));
line.addVertex(ofPoint(mesh.getVertex(3).x, mesh.getVertex(3).y));
line.close();
return line;
}
ofPolyline QuadSurface::getTextureHitArea()
{
ofPolyline QuadSurface::getTextureHitArea() {
ofPolyline line;
vector<ofVec2f>& texCoords = mesh.getTexCoords();
ofVec2f textureSize = ofVec2f(texture->getWidth(), texture->getHeight());
for ( int i=0; i<texCoords.size(); i++ ) {
line.addVertex( ofPoint( texCoords[i] * textureSize ) );
for (int i = 0; i < texCoords.size(); i++) {
line.addVertex(ofPoint(texCoords[i] * textureSize));
}
line.close();
return line;
}
vector<ofVec3f>& QuadSurface::getVertices()
{
vector<ofVec3f>& QuadSurface::getVertices() {
// return only joint vertices
return mesh.getVertices();
}
vector<ofVec2f>& QuadSurface::getTexCoords()
{
vector<ofVec2f>& QuadSurface::getTexCoords() { return mesh.getTexCoords(); }
return mesh.getTexCoords();
}
void QuadSurface::calculate4dTextureCoords()
{
void QuadSurface::calculate4dTextureCoords() {
// Perspective Warping with OpenGL Fixed Pipeline and q coordinates
// see:
// http://www.reedbeta.com/blog/2012/05/26/quadrilateral-interpolation-part-1/
@ -260,7 +238,6 @@ void QuadSurface::calculate4dTextureCoords()
quadTexCoordinates[12] = t3.x;
quadTexCoordinates[13] = t3.y * q3;
quadTexCoordinates[15] = q3;
}
}}
}
}

25
src/QuadSurface.h

@ -4,23 +4,21 @@
#include "BaseSurface.h"
#include "SurfaceType.h"
namespace ofx{
namespace piMapper{
class QuadSurface : public BaseSurface
{
public:
namespace ofx {
namespace piMapper {
class QuadSurface : public BaseSurface {
public:
QuadSurface();
~QuadSurface();
void setup();
void setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4,
ofVec2f t1, ofVec2f t2, ofVec2f t3, ofVec2f t4,
ofTexture* texturePtr );
void setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4, ofVec2f t1,
ofVec2f t2, ofVec2f t3, ofVec2f t4, ofTexture* texturePtr);
void draw();
void setVertex( int index, ofVec2f p );
void setTexCoord( int index, ofVec2f t );
void setVertex(int index, ofVec2f p);
void setTexCoord(int index, ofVec2f t);
void moveBy(ofVec2f v);
int getType();
@ -31,11 +29,12 @@ public:
ofPolyline getTextureHitArea();
vector<ofVec3f>& getVertices();
vector<ofVec2f>& getTexCoords();
private:
private:
void calculate4dTextureCoords();
GLfloat quadVertices[12];
GLubyte quadIndices[6];
GLfloat quadTexCoordinates[16];
};
}}
}
}

68
src/SourcesEditor.cpp

@ -1,35 +1,30 @@
#include "SourcesEditor.h"
namespace ofx{
namespace piMapper{
SourcesEditor::SourcesEditor()
{
namespace ofx {
namespace piMapper {
SourcesEditor::SourcesEditor() {
defImgDir = DEFAULT_IMAGES_DIR;
registerAppEvents();
}
SourcesEditor::~SourcesEditor()
{
SourcesEditor::~SourcesEditor() {
unregisterAppEvents();
delete gui;
while ( images.size() ) {
while (images.size()) {
delete images.back();
images.pop_back();
}
}
void SourcesEditor::registerAppEvents()
{
void SourcesEditor::registerAppEvents() {
ofAddListener(ofEvents().setup, this, &SourcesEditor::setup);
}
void SourcesEditor::unregisterAppEvents()
{
void SourcesEditor::unregisterAppEvents() {
ofRemoveListener(ofEvents().setup, this, &SourcesEditor::setup);
}
void SourcesEditor::setup(ofEventArgs& args)
{
void SourcesEditor::setup(ofEventArgs& args) {
gui = new RadioList();
// read directory contents
@ -39,8 +34,8 @@ void SourcesEditor::setup(ofEventArgs& args)
vector<string> vnames;
for(int i = 0; i < (int)imgDir.size(); i++){
//images[i].loadImage(imgDir.getPath(i));
for (int i = 0; i < (int)imgDir.size(); i++) {
// images[i].loadImage(imgDir.getPath(i));
vnames.push_back(imgDir.getName(i));
}
@ -49,18 +44,16 @@ void SourcesEditor::setup(ofEventArgs& args)
ofAddListener(gui->radioSelectedEvent, this, &SourcesEditor::guiEvent);
}
void SourcesEditor::draw()
{
void SourcesEditor::draw() {
// Don't draw if there is no source selected
if ( surfaceManager->getSelectedSurface() == NULL ) {
if (surfaceManager->getSelectedSurface() == NULL) {
return;
}
gui->draw();
}
void SourcesEditor::loadImage( string name, string path )
{
void SourcesEditor::loadImage(string name, string path) {
images.push_back(new ofImage());
images.back()->loadImage(path);
@ -69,15 +62,11 @@ void SourcesEditor::loadImage( string name, string path )
ofSendMessage("imageLoaded");
}
void SourcesEditor::disable()
{
gui->disable();
}
void SourcesEditor::disable() { gui->disable(); }
void SourcesEditor::enable()
{
void SourcesEditor::enable() {
// Don't enable if there is no surface selected
if ( surfaceManager->getSelectedSurface() == NULL ) {
if (surfaceManager->getSelectedSurface() == NULL) {
cout << "No surface selected. Not enable()ing source list." << endl;
return;
}
@ -85,13 +74,11 @@ void SourcesEditor::enable()
gui->enable();
}
void SourcesEditor::setSurfaceManager(SurfaceManager *newSurfaceManager)
{
void SourcesEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) {
surfaceManager = newSurfaceManager;
}
void SourcesEditor::selectImageSourceRadioButton(string name)
{
void SourcesEditor::selectImageSourceRadioButton(string name) {
if (name == "none") {
gui->unselectAll();
return;
@ -106,25 +93,20 @@ void SourcesEditor::selectImageSourceRadioButton(string name)
}
}
int SourcesEditor::getLoadedTexCount()
{
return images.size();
}
int SourcesEditor::getLoadedTexCount() { return images.size(); }
ofTexture* SourcesEditor::getTexture(int index)
{
if (index >= images.size()){
ofTexture* SourcesEditor::getTexture(int index) {
if (index >= images.size()) {
throw std::runtime_error("Texture index out of bounds.");
}
return &images[index]->getTextureReference();
}
void SourcesEditor::guiEvent(string &imageName)
{
void SourcesEditor::guiEvent(string& imageName) {
string name = imageName;
if ( surfaceManager->getSelectedSurface() == NULL ) {
if (surfaceManager->getSelectedSurface() == NULL) {
return;
}
@ -135,5 +117,5 @@ void SourcesEditor::guiEvent(string &imageName)
surfaceManager->getSelectedSurface()->setTexture(texture);
surfaceManager->manageMemory();
}
}}
}
}

19
src/SourcesEditor.h

@ -7,11 +7,10 @@
#define DEFAULT_IMAGES_DIR "sources/images/";
namespace ofx{
namespace piMapper{
class SourcesEditor
{
public:
namespace ofx {
namespace piMapper {
class SourcesEditor {
public:
SourcesEditor();
~SourcesEditor();
@ -20,7 +19,7 @@ public:
void setup(ofEventArgs& args);
void draw();
void loadImage( string name, string path );
void loadImage(string name, string path);
void disable();
void enable();
void setSurfaceManager(SurfaceManager* newSurfaceManager);
@ -29,13 +28,13 @@ public:
int getLoadedTexCount();
ofTexture* getTexture(int index);
private:
private:
SurfaceManager* surfaceManager;
RadioList* gui;
string defImgDir;
void guiEvent(string &imageName);
void guiEvent(string& imageName);
vector<ofImage*> images;
vector<string> imageNames;
};
}}
}
}

296
src/SurfaceManager.cpp

@ -1,181 +1,166 @@
#include "SurfaceManager.h"
namespace ofx{
namespace piMapper{
SurfaceManager::SurfaceManager()
{
namespace ofx {
namespace piMapper {
SurfaceManager::SurfaceManager() {}
}
SurfaceManager::~SurfaceManager()
{
clear();
}
SurfaceManager::~SurfaceManager() { clear(); }
void SurfaceManager::draw()
{
for ( int i=0; i<surfaces.size(); i++ ) {
void SurfaceManager::draw() {
for (int i = 0; i < surfaces.size(); i++) {
surfaces[i]->draw();
}
}
void SurfaceManager::addSurface(int surfaceType)
{
if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) {
surfaces.push_back( new TriangleSurface() );
}
else if (surfaceType == SurfaceType::QUAD_SURFACE ) {
surfaces.push_back( new QuadSurface() );
}
else {
void SurfaceManager::addSurface(int surfaceType) {
if (surfaceType == SurfaceType::TRIANGLE_SURFACE) {
surfaces.push_back(new TriangleSurface());
} else if (surfaceType == SurfaceType::QUAD_SURFACE) {
surfaces.push_back(new QuadSurface());
} else {
throw std::runtime_error("Attempt to add non-existing surface type.");
}
}
void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr)
{
if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) {
surfaces.push_back( new TriangleSurface() );
void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr) {
if (surfaceType == SurfaceType::TRIANGLE_SURFACE) {
surfaces.push_back(new TriangleSurface());
surfaces.back()->setTexture(texturePtr);
}
else if (surfaceType == SurfaceType::QUAD_SURFACE ) {
surfaces.push_back( new QuadSurface() );
} else if (surfaceType == SurfaceType::QUAD_SURFACE) {
surfaces.push_back(new QuadSurface());
surfaces.back()->setTexture(texturePtr);
}
else {
} else {
throw std::runtime_error("Attempt to add non-existing surface type.");
}
}
void SurfaceManager::addSurface(int surfaceType, vector<ofVec2f> vertices, vector<ofVec2f> texCoords)
{
if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) {
if ( vertices.size() < 3 ) {
throw std::runtime_error("There must be 3 vertices for a triangle surface.");
void SurfaceManager::addSurface(int surfaceType, vector<ofVec2f> vertices,
vector<ofVec2f> texCoords) {
if (surfaceType == SurfaceType::TRIANGLE_SURFACE) {
if (vertices.size() < 3) {
throw std::runtime_error(
"There must be 3 vertices for a triangle surface.");
} else if (texCoords.size() < 3) {
throw std::runtime_error("There must be 3 texture coordinates for a triangle surface.");
throw std::runtime_error(
"There must be 3 texture coordinates for a triangle surface.");
}
surfaces.push_back( new TriangleSurface() );
surfaces.push_back(new TriangleSurface());
for ( int i=0; i<3; i++ ) {
for (int i = 0; i < 3; i++) {
surfaces.back()->setVertex(i, vertices[i]);
surfaces.back()->setTexCoord(i, texCoords[i]);
}
}
else if (surfaceType == SurfaceType::QUAD_SURFACE ) {
if ( vertices.size() < 4 ) {
} else if (surfaceType == SurfaceType::QUAD_SURFACE) {
if (vertices.size() < 4) {
throw std::runtime_error("There must be 4 vertices for a quad surface.");
} else if (texCoords.size() < 4) {
throw std::runtime_error("There must be 4 texture coordinates for a quad surface.");
throw std::runtime_error(
"There must be 4 texture coordinates for a quad surface.");
}
surfaces.push_back( new QuadSurface() );
surfaces.push_back(new QuadSurface());
for ( int i=0; i<4; i++ ) {
for (int i = 0; i < 4; i++) {
surfaces.back()->setVertex(i, vertices[i]);
surfaces.back()->setTexCoord(i, texCoords[i]);
}
}
else {
} else {
throw std::runtime_error("Attempt to add non-existing surface type.");
}
}
void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vector<ofVec2f> vertices, vector<ofVec2f> texCoords)
{
if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) {
if ( vertices.size() < 3 ) {
throw std::runtime_error("There must be 3 vertices for a triangle surface.");
void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr,
vector<ofVec2f> vertices,
vector<ofVec2f> texCoords) {
if (surfaceType == SurfaceType::TRIANGLE_SURFACE) {
if (vertices.size() < 3) {
throw std::runtime_error(
"There must be 3 vertices for a triangle surface.");
} else if (texCoords.size() < 3) {
throw std::runtime_error("Thre must be 3 texture coordinates for a triangle surface.");
throw std::runtime_error(
"Thre must be 3 texture coordinates for a triangle surface.");
}
surfaces.push_back( new TriangleSurface() );
surfaces.push_back(new TriangleSurface());
surfaces.back()->setTexture(texturePtr);
for ( int i=0; i<3; i++ ) {
for (int i = 0; i < 3; i++) {
surfaces.back()->setVertex(i, vertices[i]);
surfaces.back()->setTexCoord(i, texCoords[i]);
}
}
else if (surfaceType == SurfaceType::QUAD_SURFACE ) {
if ( vertices.size() < 4 ) {
} else if (surfaceType == SurfaceType::QUAD_SURFACE) {
if (vertices.size() < 4) {
throw std::runtime_error("There must be 4 vertices for a quad surface.");
} else if (texCoords.size() < 4) {
throw std::runtime_error("Thre must be 4 texture coordinates for a quad surface.");
throw std::runtime_error(
"Thre must be 4 texture coordinates for a quad surface.");
}
surfaces.push_back( new QuadSurface() );
surfaces.push_back(new QuadSurface());
surfaces.back()->setTexture(texturePtr);
for ( int i=0; i<4; i++ ) {
for (int i = 0; i < 4; i++) {
surfaces.back()->setVertex(i, vertices[i]);
surfaces.back()->setTexCoord(i, texCoords[i]);
}
}
else {
} else {
throw std::runtime_error("Attempt to add non-existing surface type.");
}
}
void SurfaceManager::removeSelectedSurface()
{
if ( selectedSurface == NULL ) return;
void SurfaceManager::removeSelectedSurface() {
if (selectedSurface == NULL) return;
for ( int i=0; i<surfaces.size(); i++ ) {
if ( surfaces[i] == selectedSurface ) {
for (int i = 0; i < surfaces.size(); i++) {
if (surfaces[i] == selectedSurface) {
delete surfaces[i];
surfaces.erase(surfaces.begin()+i);
surfaces.erase(surfaces.begin() + i);
selectedSurface = NULL;
break;
}
}
}
void SurfaceManager::manageMemory()
{
void SurfaceManager::manageMemory() {
// check if each of the sources is assigned to a surface or not
for ( int i=0; i<loadedImageSources.size(); i++ ) {
for (int i = 0; i < loadedImageSources.size(); i++) {
bool bAssigned = false;
for ( int j=0; j<surfaces.size(); j++ ) {
if ( surfaces[j]->getTexture() == &loadedImageSources[i]->getTextureReference() ) {
for (int j = 0; j < surfaces.size(); j++) {
if (surfaces[j]->getTexture() ==
&loadedImageSources[i]->getTextureReference()) {
bAssigned = true;
break;
}
}
if ( !bAssigned ) {
if (!bAssigned) {
// purge the image source from memory
delete loadedImageSources[i];
loadedImageSources.erase(loadedImageSources.begin()+i);
loadedImageSources.erase(loadedImageSources.begin() + i);
cout << "Deleting image source: " << loadedImageSourceNames[i] << endl;
loadedImageSourceNames.erase(loadedImageSourceNames.begin()+i);
loadedImageSourceNames.erase(loadedImageSourceNames.begin() + i);
i--;
}
}
}
void SurfaceManager::clear()
{
void SurfaceManager::clear() {
// delete all extra allocations from the heap
while ( surfaces.size() ) {
while (surfaces.size()) {
delete surfaces.back();
surfaces.pop_back();
}
while ( loadedImageSources.size() ) {
while (loadedImageSources.size()) {
delete loadedImageSources.back();
loadedImageSources.pop_back();
}
while ( loadedImageSourceNames.size() ) {
while (loadedImageSourceNames.size()) {
loadedImageSourceNames.pop_back();
}
}
@ -190,15 +175,13 @@ void SurfaceManager::clear()
// }
// }
void SurfaceManager::saveXmlSettings(string fileName)
{
void SurfaceManager::saveXmlSettings(string fileName) {
xmlSettings.clear();
// save surfaces
xmlSettings.addTag("surfaces");
xmlSettings.pushTag("surfaces");
for ( int i=0; i<surfaces.size(); i++ ) {
for (int i = 0; i < surfaces.size(); i++) {
xmlSettings.addTag("surface");
xmlSettings.pushTag("surface", i);
BaseSurface* surface = surfaces[i];
@ -206,7 +189,7 @@ void SurfaceManager::saveXmlSettings(string fileName)
xmlSettings.addTag("vertices");
xmlSettings.pushTag("vertices");
vector<ofVec3f>* vertices = &surface->getVertices();
for ( int j=0; j<vertices->size(); j++ ) {
for (int j = 0; j < vertices->size(); j++) {
xmlSettings.addTag("vertex");
xmlSettings.pushTag("vertex", j);
ofVec3f* vertex = &(*vertices)[j];
@ -222,7 +205,7 @@ void SurfaceManager::saveXmlSettings(string fileName)
xmlSettings.addTag("texCoords");
xmlSettings.pushTag("texCoords");
vector<ofVec2f>* texCoords = &surface->getTexCoords();
for ( int j=0; j<texCoords->size(); j++ ) {
for (int j = 0; j < texCoords->size(); j++) {
xmlSettings.addTag("texCoord");
xmlSettings.pushTag("texCoord", j);
ofVec2f* texCoord = &(*texCoords)[j];
@ -237,10 +220,9 @@ void SurfaceManager::saveXmlSettings(string fileName)
xmlSettings.addValue("source-type", "image");
xmlSettings.addValue("source-name", getSurfaceSourceName(surface));
//xmlSettings.addValue("source-path", "/root/etc/image.jpg");
// xmlSettings.addValue("source-path", "/root/etc/image.jpg");
xmlSettings.popTag(); // source
// xmlSettings.addTag("type");
// xmlSettings.pushTag("type");
// // surfaceType == SurfaceType::TRIANGLE_SURFACE
@ -255,16 +237,13 @@ void SurfaceManager::saveXmlSettings(string fileName)
xmlSettings.save(fileName);
}
void SurfaceManager::loadXmlSettings(string fileName)
{
if (!xmlSettings.loadFile(fileName)){
void SurfaceManager::loadXmlSettings(string fileName) {
if (!xmlSettings.loadFile(fileName)) {
ofLog(OF_LOG_WARNING, "Could not load XML settings.");
return;
}
if (!xmlSettings.tagExists("surfaces")){
if (!xmlSettings.tagExists("surfaces")) {
ofLog(OF_LOG_WARNING, "XML settings is empty or has wrong markup.");
return;
}
@ -272,14 +251,14 @@ void SurfaceManager::loadXmlSettings(string fileName)
xmlSettings.pushTag("surfaces");
int numSurfaces = xmlSettings.getNumTags("surface");
for ( int i=0; i<numSurfaces; i++ ) {
for (int i = 0; i < numSurfaces; i++) {
xmlSettings.pushTag("surface", i);
// attempt to load surface source
xmlSettings.pushTag("source");
string sourceType = xmlSettings.getValue("source-type", "image");
string sourceName = xmlSettings.getValue("source-name", "none");
ofTexture* sourceTexture = NULL;
if ( sourceName != "none" ) {
if (sourceName != "none") {
stringstream ss;
ss << "sources/images/" << sourceName; // TODO: reuse constants here
sourceTexture = loadImageSource(sourceName, ss.str());
@ -289,30 +268,31 @@ void SurfaceManager::loadXmlSettings(string fileName)
// // attempt to load surface type
// ofLog(OF_LOG_WARNING, "Attempt to load surface type.");
// xmlSettings.pushTag("type");
// string surfaceType = xmlSettings.getValue("surface-type", "TRIANGLE_SURFACE");
// string surfaceType = xmlSettings.getValue("surface-type",
// "TRIANGLE_SURFACE");
// xmlSettings.popTag(); // type
xmlSettings.pushTag("vertices");
vector<ofVec2f> vertices;
int vertexCount = xmlSettings.getNumTags("vertex");
//it's a triangle ?
if (vertexCount == 3)
{
// it's a triangle ?
if (vertexCount == 3) {
ofLog(OF_LOG_NOTICE, "create Triangle");
xmlSettings.pushTag("vertex", 0);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) );
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 1);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 0.0f) ) );
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 2);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 100.0f) ) );
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 100.0f)));
xmlSettings.popTag();
xmlSettings.popTag(); // vertices
@ -322,24 +302,27 @@ void SurfaceManager::loadXmlSettings(string fileName)
vector<ofVec2f> texCoords;
xmlSettings.pushTag("texCoord", 0);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) );
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 1);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 0.0f) ) );
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 2);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 1.0f) ) );
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 1.0f)));
xmlSettings.popTag();
xmlSettings.popTag(); // texCoords
// now we have variables sourceName and sourceTexture
// by checking those we can use one or another addSurface method
if ( sourceName != "none" && sourceTexture != NULL ) {
addSurface(SurfaceType::TRIANGLE_SURFACE, sourceTexture, vertices, texCoords);
if (sourceName != "none" && sourceTexture != NULL) {
addSurface(SurfaceType::TRIANGLE_SURFACE, sourceTexture, vertices,
texCoords);
} else {
addSurface(SurfaceType::TRIANGLE_SURFACE, vertices, texCoords);
}
@ -349,19 +332,23 @@ void SurfaceManager::loadXmlSettings(string fileName)
// if (surface-type == QUAD_SURFACE)
{
xmlSettings.pushTag("vertex", 0);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) );
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 1);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 0.0f) ) );
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 2);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 100.0f) ) );
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f),
xmlSettings.getValue("y", 100.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 3);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 100.0f) ) );
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 100.0f)));
xmlSettings.popTag();
xmlSettings.popTag(); // vertices
@ -371,33 +358,35 @@ void SurfaceManager::loadXmlSettings(string fileName)
vector<ofVec2f> texCoords;
xmlSettings.pushTag("texCoord", 0);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) );
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 1);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 0.0f) ) );
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 2);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 1.0f) ) );
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f),
xmlSettings.getValue("y", 1.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 3);
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 1.0f) ) );
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 1.0f)));
xmlSettings.popTag();
xmlSettings.popTag(); // texCoords
// now we have variables sourceName and sourceTexture
// by checking those we can use one or another addSurface method
if ( sourceName != "none" && sourceTexture != NULL ) {
addSurface(SurfaceType::QUAD_SURFACE, sourceTexture, vertices, texCoords);
if (sourceName != "none" && sourceTexture != NULL) {
addSurface(SurfaceType::QUAD_SURFACE, sourceTexture, vertices,
texCoords);
} else {
addSurface(SurfaceType::QUAD_SURFACE, vertices, texCoords);
}
}
xmlSettings.popTag(); // surface
@ -406,9 +395,8 @@ void SurfaceManager::loadXmlSettings(string fileName)
xmlSettings.popTag(); // surfaces
}
BaseSurface* SurfaceManager::selectSurface(int index)
{
if ( index >= surfaces.size() ) {
BaseSurface* SurfaceManager::selectSurface(int index) {
if (index >= surfaces.size()) {
throw std::runtime_error("Surface index out of bounds.");
}
@ -418,21 +406,14 @@ BaseSurface* SurfaceManager::selectSurface(int index)
ofSendMessage("surfaceSelected");
}
BaseSurface* SurfaceManager::getSelectedSurface()
{
return selectedSurface;
}
BaseSurface* SurfaceManager::getSelectedSurface() { return selectedSurface; }
void SurfaceManager::deselectSurface()
{
selectedSurface = NULL;
}
void SurfaceManager::deselectSurface() { selectedSurface = NULL; }
ofTexture* SurfaceManager::loadImageSource(string name, string path)
{
ofTexture* SurfaceManager::loadImageSource(string name, string path) {
// check if it is loaded
for ( int i=0; i<loadedImageSourceNames.size(); i++ ) {
if ( loadedImageSourceNames[i] == name ) {
for (int i = 0; i < loadedImageSourceNames.size(); i++) {
if (loadedImageSourceNames[i] == name) {
// this image is already loaded
return &loadedImageSources[i]->getTextureReference();
}
@ -440,7 +421,7 @@ ofTexture* SurfaceManager::loadImageSource(string name, string path)
// not loaded - load
ofImage* image = new ofImage();
if ( !image->loadImage(path) ){
if (!image->loadImage(path)) {
return NULL;
}
loadedImageSources.push_back(image);
@ -448,19 +429,17 @@ ofTexture* SurfaceManager::loadImageSource(string name, string path)
return &image->getTextureReference();
}
string SurfaceManager::getSelectedSurfaceSourceName()
{
if ( selectedSurface == NULL ) {
string SurfaceManager::getSelectedSurfaceSourceName() {
if (selectedSurface == NULL) {
return "none";
}
return getSurfaceSourceName( selectedSurface );
return getSurfaceSourceName(selectedSurface);
}
string SurfaceManager::getSurfaceSourceName(BaseSurface *surface)
{
string SurfaceManager::getSurfaceSourceName(BaseSurface* surface) {
ofTexture* tex = surface->getTexture();
for ( int i=0; i<loadedImageSources.size(); i++ ) {
for (int i = 0; i < loadedImageSources.size(); i++) {
if (tex == &loadedImageSources[i]->getTextureReference()) {
return loadedImageSourceNames[i];
}
@ -469,9 +448,8 @@ string SurfaceManager::getSurfaceSourceName(BaseSurface *surface)
return "none";
}
BaseSurface* SurfaceManager::getSurface(int index)
{
if ( index >= surfaces.size() ) {
BaseSurface* SurfaceManager::getSurface(int index) {
if (index >= surfaces.size()) {
throw std::runtime_error("Surface index out of bounds.");
return NULL;
}
@ -479,10 +457,6 @@ BaseSurface* SurfaceManager::getSurface(int index)
return surfaces[index];
}
int SurfaceManager::size()
{
return surfaces.size();
int SurfaceManager::size() { return surfaces.size(); }
}
}
}}

25
src/SurfaceManager.h

@ -10,20 +10,20 @@
using namespace std;
namespace ofx{
namespace piMapper{
class SurfaceManager
{
public:
namespace ofx {
namespace piMapper {
class SurfaceManager {
public:
SurfaceManager();
~SurfaceManager();
void draw();
void addSurface(int surfaceType);
void addSurface(int surfaceType, ofTexture* texturePtr);
void addSurface(int surfaceType, vector<ofVec2f> vertices, vector<ofVec2f> texCoords);
void addSurface(int surfaceType, ofTexture* texturePtr, vector<ofVec2f> vertices, vector<ofVec2f> texCoords);
void addSurface(int surfaceType, vector<ofVec2f> vertices,
vector<ofVec2f> texCoords);
void addSurface(int surfaceType, ofTexture* texturePtr,
vector<ofVec2f> vertices, vector<ofVec2f> texCoords);
void removeSelectedSurface();
void manageMemory(); // deletes unasigned sources
void clear();
@ -37,15 +37,14 @@ public:
void deselectSurface();
ofTexture* loadImageSource(string name, string path);
string getSelectedSurfaceSourceName();
string getSurfaceSourceName( BaseSurface* surface );
string getSurfaceSourceName(BaseSurface* surface);
private:
private:
vector<BaseSurface*> surfaces;
BaseSurface* selectedSurface;
vector<string> loadedImageSourceNames;
vector<ofImage*> loadedImageSources;
ofxXmlSettings xmlSettings;
};
}}
}
}

147
src/SurfaceManagerGui.cpp

@ -1,9 +1,8 @@
#include "SurfaceManagerGui.h"
namespace ofx{
namespace piMapper{
SurfaceManagerGui::SurfaceManagerGui()
{
namespace ofx {
namespace piMapper {
SurfaceManagerGui::SurfaceManagerGui() {
surfaceManager = NULL;
guiMode = GuiMode::NONE;
bDrag = false;
@ -11,37 +10,38 @@ SurfaceManagerGui::SurfaceManagerGui()
ofHideCursor();
}
SurfaceManagerGui::~SurfaceManagerGui()
{
SurfaceManagerGui::~SurfaceManagerGui() {
unregisterMouseEvents();
surfaceManager = NULL;
}
void SurfaceManagerGui::registerMouseEvents()
{
ofAddListener(ofEvents().mousePressed, this, &SurfaceManagerGui::mousePressed);
ofAddListener(ofEvents().mouseReleased, this, &SurfaceManagerGui::mouseReleased);
ofAddListener(ofEvents().mouseDragged, this, &SurfaceManagerGui::mouseDragged);
void SurfaceManagerGui::registerMouseEvents() {
ofAddListener(ofEvents().mousePressed, this,
&SurfaceManagerGui::mousePressed);
ofAddListener(ofEvents().mouseReleased, this,
&SurfaceManagerGui::mouseReleased);
ofAddListener(ofEvents().mouseDragged, this,
&SurfaceManagerGui::mouseDragged);
}
void SurfaceManagerGui::unregisterMouseEvents()
{
ofRemoveListener(ofEvents().mousePressed, this, &SurfaceManagerGui::mousePressed);
ofRemoveListener(ofEvents().mouseReleased, this, &SurfaceManagerGui::mouseReleased);
ofRemoveListener(ofEvents().mouseDragged, this, &SurfaceManagerGui::mouseDragged);
void SurfaceManagerGui::unregisterMouseEvents() {
ofRemoveListener(ofEvents().mousePressed, this,
&SurfaceManagerGui::mousePressed);
ofRemoveListener(ofEvents().mouseReleased, this,
&SurfaceManagerGui::mouseReleased);
ofRemoveListener(ofEvents().mouseDragged, this,
&SurfaceManagerGui::mouseDragged);
}
void SurfaceManagerGui::draw()
{
if ( surfaceManager == NULL ) return;
void SurfaceManagerGui::draw() {
if (surfaceManager == NULL) return;
if ( guiMode == GuiMode::NONE ) {
if (guiMode == GuiMode::NONE) {
surfaceManager->draw();
} else if ( guiMode == GuiMode::TEXTURE_MAPPING ) {
} else if (guiMode == GuiMode::TEXTURE_MAPPING) {
// draw the texture of the selected surface
if ( surfaceManager->getSelectedSurface() != NULL ) {
surfaceManager->getSelectedSurface()->drawTexture( ofVec2f(0,0) );
if (surfaceManager->getSelectedSurface() != NULL) {
surfaceManager->getSelectedSurface()->drawTexture(ofVec2f(0, 0));
}
// draw surfaces with opacity
@ -59,8 +59,7 @@ void SurfaceManagerGui::draw()
// draw texture editing GUI on top
textureEditor.draw();
} else if ( guiMode == GuiMode::PROJECTION_MAPPING ) {
} else if (guiMode == GuiMode::PROJECTION_MAPPING) {
// draw projection surfaces first
surfaceManager->draw();
@ -70,7 +69,7 @@ void SurfaceManagerGui::draw()
// draw projection mapping editing gui
projectionEditor.draw();
} else if ( guiMode == GuiMode::SOURCE_SELECTION ) {
} else if (guiMode == GuiMode::SOURCE_SELECTION) {
// draw projection surfaces first
surfaceManager->draw();
@ -81,16 +80,15 @@ void SurfaceManagerGui::draw()
}
}
void SurfaceManagerGui::mousePressed(ofMouseEventArgs &args)
{
if ( guiMode == GuiMode::NONE ) {
void SurfaceManagerGui::mousePressed(ofMouseEventArgs& args) {
if (guiMode == GuiMode::NONE) {
return;
} else if ( guiMode == GuiMode::TEXTURE_MAPPING ) {
} else if (guiMode == GuiMode::TEXTURE_MAPPING) {
bool bSurfaceSelected = false;
CircleJoint* hitJoint = textureEditor.hitTestJoints(ofVec2f(args.x, args.y));
if ( hitJoint != NULL ) {
CircleJoint* hitJoint =
textureEditor.hitTestJoints(ofVec2f(args.x, args.y));
if (hitJoint != NULL) {
textureEditor.unselectAllJoints();
hitJoint->select();
hitJoint->startDrag();
@ -99,20 +97,21 @@ void SurfaceManagerGui::mousePressed(ofMouseEventArgs &args)
textureEditor.unselectAllJoints();
}
if ( surfaceManager->getSelectedSurface() != NULL && !bSurfaceSelected ) {
if (surfaceManager->getSelectedSurface() != NULL && !bSurfaceSelected) {
// hittest texture area to see if we are hitting the texture surface
if ( surfaceManager->getSelectedSurface()->getTextureHitArea().inside(args.x, args.y) ) {
if (surfaceManager->getSelectedSurface()->getTextureHitArea().inside(
args.x, args.y)) {
clickPosition = ofVec2f(args.x, args.y);
startDrag();
}
}
} else if ( guiMode == GuiMode::PROJECTION_MAPPING ) {
} else if (guiMode == GuiMode::PROJECTION_MAPPING) {
bool bSurfaceSelected = false;
CircleJoint* hitJoint = projectionEditor.hitTestJoints(ofVec2f(args.x, args.y));
if ( hitJoint != NULL ) {
CircleJoint* hitJoint =
projectionEditor.hitTestJoints(ofVec2f(args.x, args.y));
if (hitJoint != NULL) {
projectionEditor.unselectAllJoints();
hitJoint->select();
hitJoint->startDrag();
@ -120,9 +119,9 @@ void SurfaceManagerGui::mousePressed(ofMouseEventArgs &args)
}
// attempt to select surface, loop from end to beginning
if ( !bSurfaceSelected ){
for ( int i=surfaceManager->size()-1; i>=0; i-- ) {
if ( surfaceManager->getSurface(i)->hitTest( ofVec2f(args.x, args.y) ) ) {
if (!bSurfaceSelected) {
for (int i = surfaceManager->size() - 1; i >= 0; i--) {
if (surfaceManager->getSurface(i)->hitTest(ofVec2f(args.x, args.y))) {
projectionEditor.clearJoints();
surfaceManager->selectSurface(i);
projectionEditor.createJoints();
@ -132,62 +131,57 @@ void SurfaceManagerGui::mousePressed(ofMouseEventArgs &args)
}
}
if ( bSurfaceSelected && hitJoint == NULL ) {
// if not hitting the joints, start drag only if we have a selected surface
if (bSurfaceSelected && hitJoint == NULL) {
// if not hitting the joints, start drag only if we have a selected
// surface
clickPosition = ofVec2f(args.x, args.y);
startDrag();
}
if ( !bSurfaceSelected ) {
if (!bSurfaceSelected) {
// unselect if no surface selected
projectionEditor.clearJoints();
surfaceManager->deselectSurface();
}
} else if ( guiMode == GuiMode::SOURCE_SELECTION ) {
} else if (guiMode == GuiMode::SOURCE_SELECTION) {
}
}
void SurfaceManagerGui::mouseReleased(ofMouseEventArgs &args)
{
void SurfaceManagerGui::mouseReleased(ofMouseEventArgs& args) {
stopDrag();
projectionEditor.stopDragJoints();
textureEditor.stopDragJoints();
}
void SurfaceManagerGui::mouseDragged(ofMouseEventArgs &args)
{
void SurfaceManagerGui::mouseDragged(ofMouseEventArgs& args) {
if (bDrag) {
ofVec2f mousePosition = ofVec2f(args.x, args.y);
ofVec2f distance = mousePosition - clickPosition;
if ( guiMode == GuiMode::PROJECTION_MAPPING ) {
if (guiMode == GuiMode::PROJECTION_MAPPING) {
// add this distance to all vertices in surface
projectionEditor.moveSelectedSurface(distance);
} else if ( guiMode == GuiMode::TEXTURE_MAPPING ) {
} else if (guiMode == GuiMode::TEXTURE_MAPPING) {
textureEditor.moveTexCoords(distance);
}
clickPosition = mousePosition;
}
}
void SurfaceManagerGui::setSurfaceManager(SurfaceManager* newSurfaceManager)
{
void SurfaceManagerGui::setSurfaceManager(SurfaceManager* newSurfaceManager) {
surfaceManager = newSurfaceManager;
projectionEditor.setSurfaceManager( surfaceManager );
sourcesEditor.setSurfaceManager( surfaceManager );
projectionEditor.setSurfaceManager(surfaceManager);
sourcesEditor.setSurfaceManager(surfaceManager);
}
void SurfaceManagerGui::setMode(int newGuiMode)
{
if (newGuiMode != GuiMode::NONE &&
newGuiMode != GuiMode::TEXTURE_MAPPING &&
void SurfaceManagerGui::setMode(int newGuiMode) {
if (newGuiMode != GuiMode::NONE && newGuiMode != GuiMode::TEXTURE_MAPPING &&
newGuiMode != GuiMode::PROJECTION_MAPPING &&
newGuiMode != GuiMode::SOURCE_SELECTION) {
throw std::runtime_error("Trying to set invalid mode.");
}
if ( newGuiMode == GuiMode::NONE ) {
if (newGuiMode == GuiMode::NONE) {
ofHideCursor();
} else {
ofShowCursor();
@ -195,7 +189,7 @@ void SurfaceManagerGui::setMode(int newGuiMode)
guiMode = newGuiMode;
if ( guiMode == GuiMode::SOURCE_SELECTION ) {
if (guiMode == GuiMode::SOURCE_SELECTION) {
sourcesEditor.enable();
string sourceName = surfaceManager->getSelectedSurfaceSourceName();
sourcesEditor.selectImageSourceRadioButton(sourceName);
@ -203,7 +197,7 @@ void SurfaceManagerGui::setMode(int newGuiMode)
sourcesEditor.disable();
}
if ( guiMode == GuiMode::TEXTURE_MAPPING ) {
if (guiMode == GuiMode::TEXTURE_MAPPING) {
textureEditor.enable();
// refresh texture editor surface reference
textureEditor.setSurface(surfaceManager->getSelectedSurface());
@ -218,9 +212,8 @@ void SurfaceManagerGui::setMode(int newGuiMode)
}
}
void SurfaceManagerGui::drawSelectedSurfaceHighlight()
{
if ( surfaceManager->getSelectedSurface() == NULL ) return;
void SurfaceManagerGui::drawSelectedSurfaceHighlight() {
if (surfaceManager->getSelectedSurface() == NULL) return;
ofPolyline line = surfaceManager->getSelectedSurface()->getHitArea();
@ -231,9 +224,8 @@ void SurfaceManagerGui::drawSelectedSurfaceHighlight()
ofPopStyle();
}
void SurfaceManagerGui::drawSelectedSurfaceTextureHighlight()
{
if ( surfaceManager->getSelectedSurface() == NULL ) return;
void SurfaceManagerGui::drawSelectedSurfaceTextureHighlight() {
if (surfaceManager->getSelectedSurface() == NULL) return;
ofPolyline line = surfaceManager->getSelectedSurface()->getTextureHitArea();
@ -242,17 +234,10 @@ void SurfaceManagerGui::drawSelectedSurfaceTextureHighlight()
ofSetColor(255, 255, 0, 255);
line.draw();
ofPopStyle();
}
void SurfaceManagerGui::startDrag()
{
bDrag = true;
}
void SurfaceManagerGui::startDrag() { bDrag = true; }
void SurfaceManagerGui::stopDrag()
{
bDrag = false;
void SurfaceManagerGui::stopDrag() { bDrag = false; }
}
}
}}

16
src/SurfaceManagerGui.h

@ -12,11 +12,10 @@
#include "SourcesEditor.h"
#include "GuiMode.h"
namespace ofx{
namespace piMapper{
class SurfaceManagerGui
{
public:
namespace ofx {
namespace piMapper {
class SurfaceManagerGui {
public:
SurfaceManagerGui();
~SurfaceManagerGui();
@ -34,7 +33,7 @@ public:
void startDrag();
void stopDrag();
private:
private:
SurfaceManager* surfaceManager;
TextureEditor textureEditor;
ProjectionEditor projectionEditor;
@ -42,7 +41,6 @@ private:
int guiMode;
bool bDrag;
ofVec2f clickPosition;
};
}}
}
}

16
src/SurfaceType.h

@ -1,13 +1,9 @@
#pragma once
namespace ofx{
namespace piMapper{
struct SurfaceType
{
enum {
TRIANGLE_SURFACE,
QUAD_SURFACE
};
namespace ofx {
namespace piMapper {
struct SurfaceType {
enum { TRIANGLE_SURFACE, QUAD_SURFACE };
};
}}
}
}

144
src/TextureEditor.cpp

@ -1,62 +1,54 @@
#include "TextureEditor.h"
namespace ofx{
namespace piMapper{
TextureEditor::TextureEditor()
{
namespace ofx {
namespace piMapper {
TextureEditor::TextureEditor() {
clear();
enable();
}
TextureEditor::~TextureEditor()
{
TextureEditor::~TextureEditor() {
clear();
disable();
}
void TextureEditor::registerAppEvents()
{
void TextureEditor::registerAppEvents() {
ofAddListener(ofEvents().update, this, &TextureEditor::update);
}
void TextureEditor::unregisterAppEvents()
{
void TextureEditor::unregisterAppEvents() {
ofRemoveListener(ofEvents().update, this, &TextureEditor::update);
}
void TextureEditor::registerKeyEvents()
{
void TextureEditor::registerKeyEvents() {
ofAddListener(ofEvents().keyPressed, this, &TextureEditor::keyPressed);
ofAddListener(ofEvents().keyReleased, this, &TextureEditor::keyReleased);
}
void TextureEditor::unregisterKeyEvents()
{
void TextureEditor::unregisterKeyEvents() {
ofRemoveListener(ofEvents().keyPressed, this, &TextureEditor::keyPressed);
ofRemoveListener(ofEvents().keyReleased, this, &TextureEditor::keyReleased);
}
void TextureEditor::enable()
{
void TextureEditor::enable() {
registerAppEvents();
registerKeyEvents();
bShiftKeyDown = false;
}
void TextureEditor::disable()
{
void TextureEditor::disable() {
unregisterAppEvents();
unregisterKeyEvents();
}
void TextureEditor::update(ofEventArgs &args)
{
if ( surface == NULL ) return;
void TextureEditor::update(ofEventArgs& args) {
if (surface == NULL) return;
// update surface if one of the joints is being dragged
ofVec2f textureSize = ofVec2f( surface->getTexture()->getWidth(), surface->getTexture()->getHeight() );
for ( int i=0; i<joints.size(); i++ ) {
if ( joints[i]->isDragged() || joints[i]->isSelected() ) {
ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(),
surface->getTexture()->getHeight());
for (int i = 0; i < joints.size(); i++) {
if (joints[i]->isDragged() || joints[i]->isSelected()) {
// update vertex to new location
surface->setTexCoord(i, joints[i]->position / textureSize);
break;
@ -64,110 +56,113 @@ void TextureEditor::update(ofEventArgs &args)
}
}
void TextureEditor::keyPressed(ofKeyEventArgs &args)
{
void TextureEditor::keyPressed(ofKeyEventArgs& args) {
int key = args.key;
float moveStep;
if (bShiftKeyDown) moveStep = 10.0f;
else moveStep = 0.5f;
if (bShiftKeyDown)
moveStep = 10.0f;
else
moveStep = 0.5f;
switch (key) {
case OF_KEY_LEFT: moveSelection(ofVec2f(-moveStep,0.0f)); break;
case OF_KEY_RIGHT: moveSelection(ofVec2f(moveStep,0.0f)); break;
case OF_KEY_UP: moveSelection(ofVec2f(0.0f,-moveStep)); break;
case OF_KEY_DOWN: moveSelection(ofVec2f(0.0f,moveStep)); break;
case OF_KEY_SHIFT: bShiftKeyDown = true; break;
case OF_KEY_LEFT:
moveSelection(ofVec2f(-moveStep, 0.0f));
break;
case OF_KEY_RIGHT:
moveSelection(ofVec2f(moveStep, 0.0f));
break;
case OF_KEY_UP:
moveSelection(ofVec2f(0.0f, -moveStep));
break;
case OF_KEY_DOWN:
moveSelection(ofVec2f(0.0f, moveStep));
break;
case OF_KEY_SHIFT:
bShiftKeyDown = true;
break;
}
}
void TextureEditor::keyReleased(ofKeyEventArgs &args)
{
void TextureEditor::keyReleased(ofKeyEventArgs& args) {
int key = args.key;
switch (key) {
case OF_KEY_SHIFT: bShiftKeyDown = false; break;
case OF_KEY_SHIFT:
bShiftKeyDown = false;
break;
}
}
void TextureEditor::draw()
{
void TextureEditor::draw() {
if (surface == NULL) return;
drawJoints();
}
void TextureEditor::drawJoints()
{
for ( int i=0; i<joints.size(); i++ ) {
void TextureEditor::drawJoints() {
for (int i = 0; i < joints.size(); i++) {
joints[i]->draw();
}
}
void TextureEditor::setSurface(BaseSurface* newSurface)
{
void TextureEditor::setSurface(BaseSurface* newSurface) {
surface = newSurface;
createJoints();
}
void TextureEditor::clear()
{
void TextureEditor::clear() {
surface = NULL;
clearJoints();
}
void TextureEditor::createJoints()
{
if ( surface == NULL ) return;
void TextureEditor::createJoints() {
if (surface == NULL) return;
clearJoints();
vector<ofVec2f>& texCoords = surface->getTexCoords();
ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(), surface->getTexture()->getHeight());
ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(),
surface->getTexture()->getHeight());
for ( int i=0; i<texCoords.size(); i++ ) {
for (int i = 0; i < texCoords.size(); i++) {
joints.push_back(new CircleJoint());
joints.back()->position = texCoords[i] * textureSize;
}
}
void TextureEditor::clearJoints()
{
while ( joints.size() ) {
void TextureEditor::clearJoints() {
while (joints.size()) {
delete joints.back();
joints.pop_back();
}
}
void TextureEditor::unselectAllJoints()
{
for ( int i=0; i<joints.size(); i++ ) {
void TextureEditor::unselectAllJoints() {
for (int i = 0; i < joints.size(); i++) {
joints[i]->unselect();
}
}
void TextureEditor::moveTexCoords(ofVec2f by)
{
if ( surface == NULL ) return;
void TextureEditor::moveTexCoords(ofVec2f by) {
if (surface == NULL) return;
vector<ofVec2f>& texCoords = surface->getTexCoords();
ofVec2f textureSize = ofVec2f( surface->getTexture()->getWidth(), surface->getTexture()->getHeight() );
for (int i=0; i<texCoords.size(); i++) {
ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(),
surface->getTexture()->getHeight());
for (int i = 0; i < texCoords.size(); i++) {
joints[i]->position += by;
texCoords[i] = joints[i]->position / textureSize;
}
}
void TextureEditor::stopDragJoints()
{
for (int i=0; i<joints.size(); i++){
void TextureEditor::stopDragJoints() {
for (int i = 0; i < joints.size(); i++) {
joints[i]->stopDrag();
}
}
void TextureEditor::moveSelection(ofVec2f by)
{
void TextureEditor::moveSelection(ofVec2f by) {
// check if joints selected
bool bJointSelected = false;
BaseJoint* selectedJoint;
for ( int i=0; i<joints.size(); i++ ) {
for (int i = 0; i < joints.size(); i++) {
if (joints[i]->isSelected()) {
bJointSelected = true;
selectedJoint = joints[i];
@ -175,21 +170,20 @@ void TextureEditor::moveSelection(ofVec2f by)
}
}
if ( bJointSelected ) {
if (bJointSelected) {
selectedJoint->position += by;
} else {
moveTexCoords(by);
}
}
CircleJoint* TextureEditor::hitTestJoints(ofVec2f pos)
{
for ( int i=0; i<joints.size(); i++ ) {
if ( joints[i]->hitTest(pos) ){
CircleJoint* TextureEditor::hitTestJoints(ofVec2f pos) {
for (int i = 0; i < joints.size(); i++) {
if (joints[i]->hitTest(pos)) {
return joints[i];
}
}
return NULL;
}
}}
}
}

16
src/TextureEditor.h

@ -5,12 +5,10 @@
#include "BaseSurface.h"
#include "CircleJoint.h"
namespace ofx{
namespace piMapper{
class TextureEditor
{
public:
namespace ofx {
namespace piMapper {
class TextureEditor {
public:
TextureEditor();
~TextureEditor();
@ -36,10 +34,10 @@ public:
void moveSelection(ofVec2f by);
CircleJoint* hitTestJoints(ofVec2f pos);
private:
private:
BaseSurface* surface;
vector<CircleJoint*> joints;
bool bShiftKeyDown;
};
}}
}
}

93
src/TriangleSurface.cpp

@ -1,22 +1,19 @@
#include "TriangleSurface.h"
namespace ofx{
namespace piMapper{
TriangleSurface::TriangleSurface()
{
namespace ofx {
namespace piMapper {
TriangleSurface::TriangleSurface() {
cout << "TriangleSurface constructor." << endl;
setup();
}
TriangleSurface::~TriangleSurface()
{
TriangleSurface::~TriangleSurface() {
cout << "TriangleSurface destructor." << endl;
}
void TriangleSurface::setup()
{
void TriangleSurface::setup() {
// Create 3 points for the triangle
ofVec2f p1 = ofVec2f(ofGetWidth()/2.0f, 0);
ofVec2f p1 = ofVec2f(ofGetWidth() / 2.0f, 0);
ofVec2f p2 = ofVec2f(ofVec2f(0, ofGetHeight()));
ofVec2f p3 = ofVec2f(ofGetWidth(), ofGetHeight());
@ -25,11 +22,11 @@ void TriangleSurface::setup()
ofVec2f t2 = ofVec2f(0, 1.0f);
ofVec2f t3 = ofVec2f(1, 1.0f);
setup( p1, p2, p3, t1, t2, t3, texture );
setup(p1, p2, p3, t1, t2, t3, texture);
}
void TriangleSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr )
{
void TriangleSurface::setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1,
ofVec2f t2, ofVec2f t3, ofTexture* texturePtr) {
// Assign texture
texture = texturePtr;
@ -37,9 +34,9 @@ void TriangleSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofV
mesh.clear();
// Create a surface with the points
mesh.addVertex( p1 );
mesh.addVertex( p2 );
mesh.addVertex( p3 );
mesh.addVertex(p1);
mesh.addVertex(p2);
mesh.addVertex(p3);
// Add texture coordinates
mesh.addTexCoord(t1);
@ -47,16 +44,14 @@ void TriangleSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofV
mesh.addTexCoord(t3);
}
void TriangleSurface::draw()
{
void TriangleSurface::draw() {
texture->bind();
mesh.draw();
texture->unbind();
}
void TriangleSurface::setVertex(int index, ofVec2f p)
{
if ( index > 2 ) {
void TriangleSurface::setVertex(int index, ofVec2f p) {
if (index > 2) {
ofLog() << "Vertex with this index does not exist: " << index << endl;
return;
}
@ -64,44 +59,38 @@ void TriangleSurface::setVertex(int index, ofVec2f p)
mesh.setVertex(index, p);
}
void TriangleSurface::setTexCoord(int index, ofVec2f t)
{
if ( index > 2 ) {
ofLog() << "Texture coordinate with this index does not exist: " << index << endl;
void TriangleSurface::setTexCoord(int index, ofVec2f t) {
if (index > 2) {
ofLog() << "Texture coordinate with this index does not exist: " << index
<< endl;
return;
}
mesh.setTexCoord(index, t);
}
void TriangleSurface::moveBy(ofVec2f v)
{
void TriangleSurface::moveBy(ofVec2f v) {
vector<ofVec3f>& vertices = getVertices();
for (int i=0; i<vertices.size(); i++) {
for (int i = 0; i < vertices.size(); i++) {
vertices[i] += v;
}
}
int TriangleSurface::getType()
{
return SurfaceType::TRIANGLE_SURFACE;
}
int TriangleSurface::getType() { return SurfaceType::TRIANGLE_SURFACE; }
bool TriangleSurface::hitTest(ofVec2f p)
{
bool TriangleSurface::hitTest(ofVec2f p) {
// Construct ofPolyline from vertices
ofPolyline line = getHitArea();
if ( line.inside(p.x, p.y) ) {
if (line.inside(p.x, p.y)) {
return true;
} else {
return false;
}
}
ofVec2f TriangleSurface::getVertex(int index)
{
if ( index > 2 ) {
ofVec2f TriangleSurface::getVertex(int index) {
if (index > 2) {
ofLog() << "Vertex with this index does not exist: " << index << endl;
throw std::runtime_error("Vertex index out of bounds.");
}
@ -110,8 +99,7 @@ ofVec2f TriangleSurface::getVertex(int index)
return ofVec2f(vert.x, vert.y);
}
ofVec2f TriangleSurface::getTexCoord(int index)
{
ofVec2f TriangleSurface::getTexCoord(int index) {
if (index > 2) {
throw std::runtime_error("Texture coordinate index out of bounds.");
}
@ -119,40 +107,33 @@ ofVec2f TriangleSurface::getTexCoord(int index)
return mesh.getTexCoord(index);
}
ofPolyline TriangleSurface::getHitArea()
{
ofPolyline TriangleSurface::getHitArea() {
ofPolyline line;
line.addVertex( ofPoint( mesh.getVertex(0).x, mesh.getVertex(0).y ) );
line.addVertex( ofPoint( mesh.getVertex(1).x, mesh.getVertex(1).y ) );
line.addVertex( ofPoint( mesh.getVertex(2).x, mesh.getVertex(2).y ) );
line.addVertex(ofPoint(mesh.getVertex(0).x, mesh.getVertex(0).y));
line.addVertex(ofPoint(mesh.getVertex(1).x, mesh.getVertex(1).y));
line.addVertex(ofPoint(mesh.getVertex(2).x, mesh.getVertex(2).y));
line.close();
return line;
}
ofPolyline TriangleSurface::getTextureHitArea()
{
ofPolyline TriangleSurface::getTextureHitArea() {
ofPolyline line;
vector<ofVec2f>& texCoords = mesh.getTexCoords();
ofVec2f textureSize = ofVec2f(texture->getWidth(), texture->getHeight());
for ( int i=0; i<texCoords.size(); i++ ) {
line.addVertex( ofPoint( texCoords[i] * textureSize ) );
for (int i = 0; i < texCoords.size(); i++) {
line.addVertex(ofPoint(texCoords[i] * textureSize));
}
line.close();
return line;
}
vector<ofVec3f>& TriangleSurface::getVertices()
{
vector<ofVec3f>& TriangleSurface::getVertices() {
// return only joint vertices
return mesh.getVertices();
}
vector<ofVec2f>& TriangleSurface::getTexCoords()
{
return mesh.getTexCoords();
vector<ofVec2f>& TriangleSurface::getTexCoords() { return mesh.getTexCoords(); }
}
}
}}

21
src/TriangleSurface.h

@ -4,20 +4,19 @@
#include "BaseSurface.h"
#include "SurfaceType.h"
namespace ofx{
namespace piMapper{
class TriangleSurface : public BaseSurface
{
public:
namespace ofx {
namespace piMapper {
class TriangleSurface : public BaseSurface {
public:
TriangleSurface();
~TriangleSurface();
void setup();
void setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr );
void setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2,
ofVec2f t3, ofTexture* texturePtr);
void draw();
void setVertex( int index, ofVec2f p );
void setTexCoord( int index, ofVec2f t );
void setVertex(int index, ofVec2f p);
void setTexCoord(int index, ofVec2f t);
void moveBy(ofVec2f v);
int getType();
@ -29,5 +28,5 @@ public:
vector<ofVec3f>& getVertices();
vector<ofVec2f>& getTexCoords();
};
}}
}
}

101
src/ui/RadioList.cpp

@ -1,32 +1,25 @@
#include "RadioList.h"
namespace ofx{
namespace piMapper{
RadioList::RadioList()
{
namespace ofx {
namespace piMapper {
RadioList::RadioList() {
storedTitle = "";
storedSelectedItem = 0;
}
RadioList::RadioList(vector<string> &labels)
{
RadioList::RadioList(vector<string>& labels) {
RadioList();
setup(labels);
}
RadioList::RadioList(string title, vector<string> &labels)
{
RadioList::RadioList(string title, vector<string>& labels) {
RadioList();
setup(title, labels);
}
RadioList::~RadioList()
{
clear();
}
RadioList::~RadioList() { clear(); }
void RadioList::setup(vector<string> &labels)
{
void RadioList::setup(vector<string>& labels) {
// Copy incomming labels for later use
storedLabels = labels;
@ -43,37 +36,25 @@ void RadioList::setup(vector<string> &labels)
cout << "num items: " << guiGroup.getNumControls() << endl;
}
void RadioList::setup(string title, vector<string> &labels)
{
void RadioList::setup(string title, vector<string>& labels) {
// Store title for later use
storedTitle = title;
guiGroup.setName(title);
setup(labels);
}
void RadioList::draw()
{
guiGroup.draw();
}
void RadioList::draw() { guiGroup.draw(); }
void RadioList::setTitle(string title)
{
void RadioList::setTitle(string title) {
storedTitle = title;
guiGroup.setName(title);
}
void RadioList::setPosition(ofPoint p)
{
guiGroup.setPosition(p);
}
void RadioList::setPosition(ofPoint p) { guiGroup.setPosition(p); }
void RadioList::setPosition(float x, float y)
{
guiGroup.setPosition(x, y);
}
void RadioList::setPosition(float x, float y) { guiGroup.setPosition(x, y); }
void RadioList::selectItem(int index)
{
void RadioList::selectItem(int index) {
if (index >= guiGroup.getNumControls()) {
return;
}
@ -90,8 +71,7 @@ void RadioList::selectItem(int index)
storedSelectedItem = index;
}
void RadioList::enable()
{
void RadioList::enable() {
if (guiGroup.getNumControls() >= 0) {
clear();
}
@ -100,7 +80,8 @@ void RadioList::enable()
setup(storedTitle, storedLabels);
// Select the stored selected item without throwing an event
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(storedSelectedItem));
ofxToggle* toggle =
static_cast<ofxToggle*>(guiGroup.getControl(storedSelectedItem));
toggle->removeListener(this, &RadioList::onToggleClicked);
*toggle = true;
toggle->addListener(this, &RadioList::onToggleClicked);
@ -108,14 +89,12 @@ void RadioList::enable()
cout << "num items after enable: " << guiGroup.getNumControls() << endl;
}
void RadioList::disable()
{
void RadioList::disable() {
// Just remove everything
clear();
}
void RadioList::clear()
{
void RadioList::clear() {
int i;
for (i = 0; i < guiGroup.getNumControls(); i++) {
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i));
@ -125,40 +104,27 @@ void RadioList::clear()
guiGroup.clear();
}
void RadioList::unselectAll()
{
void RadioList::unselectAll() {
int i;
for (i = 0; i < guiGroup.getNumControls(); i++) {
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i));
ofParameter<bool>* paramPtr = static_cast<ofParameter<bool>*>(&toggle->getParameter());
ofParameter<bool>* paramPtr =
static_cast<ofParameter<bool>*>(&toggle->getParameter());
toggle->removeListener(this, &RadioList::onToggleClicked);
*toggle = false;
toggle->addListener(this, &RadioList::onToggleClicked);
}
}
ofPoint RadioList::getPosition()
{
return guiGroup.getPosition();
}
ofPoint RadioList::getPosition() { return guiGroup.getPosition(); }
float RadioList::getWidth()
{
return guiGroup.getWidth();
}
float RadioList::getWidth() { return guiGroup.getWidth(); }
float RadioList::getHeight()
{
return guiGroup.getHeight();
}
float RadioList::getHeight() { return guiGroup.getHeight(); }
string RadioList::getTitle()
{
return guiGroup.getName();
}
string RadioList::getTitle() { return guiGroup.getName(); }
string RadioList::getItemName(int index)
{
string RadioList::getItemName(int index) {
if (index >= guiGroup.getNumControls()) {
return "";
}
@ -167,20 +133,17 @@ string RadioList::getItemName(int index)
return toggle->getName();
}
int RadioList::size()
{
return guiGroup.getNumControls();
}
int RadioList::size() { return guiGroup.getNumControls(); }
void RadioList::onToggleClicked(bool &toggleValue)
{
void RadioList::onToggleClicked(bool& toggleValue) {
unselectAll();
// Search for the actual toggle triggering the event
int i;
for (i = 0; i < guiGroup.getNumControls(); i++) {
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i));
ofParameter<bool>* paramPtr = static_cast<ofParameter<bool>*>(&toggle->getParameter());
ofParameter<bool>* paramPtr =
static_cast<ofParameter<bool>*>(&toggle->getParameter());
if (&(paramPtr->get()) == &toggleValue) {
selectItem(i);
@ -188,5 +151,5 @@ void RadioList::onToggleClicked(bool &toggleValue)
}
}
}
}}
}
}

21
src/ui/RadioList.h

@ -5,11 +5,10 @@
#include "ofxToggle.h"
#include "ofxLabel.h"
namespace ofx{
namespace piMapper{
class RadioList
{
public:
namespace ofx {
namespace piMapper {
class RadioList {
public:
RadioList();
RadioList(vector<string> &labels);
RadioList(string title, vector<string> &labels);
@ -33,12 +32,14 @@ public:
string getItemName(int index);
int size();
// This event notifies about a toggle being selected and passes it's name to the listeners.
// Use ofAddListener(RadioListInstance.radioSelectedEvent, listenerClassPtr, &listenerClass::listenerMethod)
// This event notifies about a toggle being selected and passes it's name to
// the listeners.
// Use ofAddListener(RadioListInstance.radioSelectedEvent, listenerClassPtr,
// &listenerClass::listenerMethod)
// to listen to this. Listner method void listenerMethod(string & radioName)
ofEvent<string> radioSelectedEvent;
private:
private:
vector<string> storedLabels;
string storedTitle;
ofxGuiGroup guiGroup;
@ -46,5 +47,5 @@ private:
void onToggleClicked(bool &toggleValue);
};
}}
}
}
Loading…
Cancel
Save