Browse Source

ran clang Format with google code Style

master
Felix Dubrownik 11 years ago
parent
commit
6d44d1cae7
  1. 307
      example/src/ofApp.cpp
  2. 41
      example/src/ofApp.h
  3. 126
      src/BaseJoint.cpp
  4. 89
      src/BaseJoint.h
  5. 119
      src/BaseSurface.cpp
  6. 65
      src/BaseSurface.h
  7. 85
      src/CircleJoint.cpp
  8. 31
      src/CircleJoint.h
  9. 16
      src/EditorType.h
  10. 17
      src/GuiMode.h
  11. 411
      src/ProjectionEditor.cpp
  12. 89
      src/ProjectionEditor.h
  13. 473
      src/QuadSurface.cpp
  14. 63
      src/QuadSurface.h
  15. 202
      src/SourcesEditor.cpp
  16. 61
      src/SourcesEditor.h
  17. 818
      src/SurfaceManager.cpp
  18. 73
      src/SurfaceManager.h
  19. 431
      src/SurfaceManagerGui.cpp
  20. 64
      src/SurfaceManagerGui.h
  21. 16
      src/SurfaceType.h
  22. 288
      src/TextureEditor.cpp
  23. 70
      src/TextureEditor.h
  24. 233
      src/TriangleSurface.cpp
  25. 49
      src/TriangleSurface.h
  26. 267
      src/ui/RadioList.cpp
  27. 85
      src/ui/RadioList.h

307
example/src/ofApp.cpp

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

41
example/src/ofApp.h

@ -3,26 +3,25 @@
#include "ofMain.h" #include "ofMain.h"
#include "ofxPiMapper.h" #include "ofxPiMapper.h"
class ofApp : public ofBaseApp class ofApp : public ofBaseApp {
{ public:
public: void setup();
void setup(); void update();
void update(); void draw();
void draw(); void exit();
void exit();
void keyPressed(int key); void keyPressed(int key);
void addRandomSurface(); void addRandomSurface();
void addQuadSurface(); void addQuadSurface();
void addSurface(); void addSurface();
void setFboAsTexture(); void setFboAsTexture();
ofImage image; ofImage image;
ofx::piMapper::SurfaceManager surfaceManager; ofx::piMapper::SurfaceManager surfaceManager;
ofx::piMapper::SurfaceManagerGui gui; ofx::piMapper::SurfaceManagerGui gui;
bool bShowInfo; bool bShowInfo;
ofFbo* fbo; ofFbo* fbo;
vector<ofRectangle> rects; vector<ofRectangle> rects;
vector<float> rectSpeeds; vector<float> rectSpeeds;
}; };

126
src/BaseJoint.cpp

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

89
src/BaseJoint.h

@ -2,49 +2,50 @@
#include "ofMain.h" #include "ofMain.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
class BaseJoint { class BaseJoint {
public: public:
BaseJoint(); BaseJoint();
~BaseJoint(); ~BaseJoint();
void registerMouseEvents(); void registerMouseEvents();
void unregisterMouseEvents(); void unregisterMouseEvents();
ofVec2f position; ofVec2f position;
bool enabled; bool enabled;
bool visible; bool visible;
bool selected; bool selected;
void mousePressed(ofMouseEventArgs& args); void mousePressed(ofMouseEventArgs& args);
void mouseReleased(int x, int y, int button); void mouseReleased(int x, int y, int button);
void mouseDragged(ofMouseEventArgs& args); void mouseDragged(ofMouseEventArgs& args);
void startDrag(); void startDrag();
void stopDrag(); void stopDrag();
void select(); void select();
void unselect(); void unselect();
void setClickDistance(ofVec2f newClickDistance); void setClickDistance(ofVec2f newClickDistance);
bool isDragged(); bool isDragged();
bool isSelected(); bool isSelected();
virtual void update(){}; virtual void update() {};
virtual void draw(){}; virtual void draw() {};
virtual bool hitTest(ofVec2f position){}; virtual bool hitTest(ofVec2f position) {};
protected: protected:
ofColor fillColor; ofColor fillColor;
ofColor strokeColor; ofColor strokeColor;
ofColor fillColorSelected; ofColor fillColorSelected;
ofColor strokeColorSelected; ofColor strokeColorSelected;
float strokeWidth; 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
bool bDrag; // clicked?
bool bDrag;
private:
void setDefaultColors(); private:
void setDefaultProperties(); void setDefaultColors();
}; void setDefaultProperties();
};
} }
} }

119
src/BaseSurface.cpp

@ -1,77 +1,70 @@
#include "BaseSurface.h" #include "BaseSurface.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
BaseSurface::BaseSurface() BaseSurface::BaseSurface() {
{ ofEnableNormalizedTexCoords();
ofEnableNormalizedTexCoords(); createDefaultTexture();
createDefaultTexture();
} }
void BaseSurface::createDefaultTexture() void BaseSurface::createDefaultTexture() {
{ ofPixels pixels;
ofPixels pixels; pixels.allocate(500, 500, 1);
pixels.allocate(500, 500, 1); for (int i = 0; i < pixels.size(); i++) {
for ( int i=0; i<pixels.size(); i++ ) { pixels[i] = 255;
pixels[i] = 255; }
} int squareSize = 10; // size of each test pattern square
int squareSize = 10; // size of each test pattern square bool sy = false;
bool sy = false; for (int y = 0; y < pixels.getWidth(); y += squareSize) {
for ( int y=0; y<pixels.getWidth(); y+=squareSize ) { bool sx = false;
bool sx = false; for (int x = 0; x < pixels.getHeight(); x += squareSize) {
for ( int x=0; x<pixels.getHeight(); x+=squareSize ) { for (int yi = 0; yi < squareSize; yi++) {
for ( int yi=0; yi<squareSize; yi++ ) { for (int xi = 0; xi < squareSize; xi++) {
for ( int xi=0; xi<squareSize; xi++ ){ if (sx && sy)
if ( sx && sy ) pixels[(y+yi)*pixels.getWidth()+x+xi] = 255; pixels[(y + yi) * pixels.getWidth() + x + xi] = 255;
else if ( sx && !sy ) pixels[(y+yi)*pixels.getWidth()+x+xi] = 0; else if (sx && !sy)
else if ( !sx && sy ) pixels[(y+yi)*pixels.getWidth()+x+xi] = 0; pixels[(y + yi) * pixels.getWidth() + x + xi] = 0;
else pixels[(y+yi)*pixels.getWidth()+x+xi] = 255; else if (!sx && sy)
} pixels[(y + yi) * pixels.getWidth() + x + xi] = 0;
} else
sx = !sx; pixels[(y + yi) * pixels.getWidth() + x + xi] = 255;
} }
sy = !sy; }
sx = !sx;
} }
sy = !sy;
// load pixels into texture }
defaultTexture.loadData(pixels);
// Assign default texture to texture pointer
texture = &defaultTexture;
}
void BaseSurface::drawTexture(ofVec2f position) // load pixels into texture
{ defaultTexture.loadData(pixels);
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(0.0f, texture->getHeight()));
texMesh.addTriangle(0, 2, 3);
texMesh.addTriangle(0, 1, 2);
texMesh.addTexCoord(ofVec2f(0.0f, 0.0f));
texMesh.addTexCoord(ofVec2f(1.0f, 0.0f));
texMesh.addTexCoord(ofVec2f(1.0f, 1.0f));
texMesh.addTexCoord(ofVec2f(0.0f, 1.0f));
texture->bind();
texMesh.draw();
texture->unbind();
}
void BaseSurface::setTexture(ofTexture *texturePtr) // Assign default texture to texture pointer
{ texture = &defaultTexture;
texture = texturePtr;
} }
ofTexture* BaseSurface::getTexture() void BaseSurface::drawTexture(ofVec2f position) {
{ ofMesh texMesh;
return texture; texMesh.addVertex(position);
texMesh.addVertex(position + ofVec2f(texture->getWidth(), 0.0f));
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);
texMesh.addTexCoord(ofVec2f(0.0f, 0.0f));
texMesh.addTexCoord(ofVec2f(1.0f, 0.0f));
texMesh.addTexCoord(ofVec2f(1.0f, 1.0f));
texMesh.addTexCoord(ofVec2f(0.0f, 1.0f));
texture->bind();
texMesh.draw();
texture->unbind();
} }
ofTexture* BaseSurface::getDefaultTexture() void BaseSurface::setTexture(ofTexture* texturePtr) { texture = texturePtr; }
{
return &defaultTexture; ofTexture* BaseSurface::getTexture() { return texture; }
}
}} ofTexture* BaseSurface::getDefaultTexture() { return &defaultTexture; }
}
}

65
src/BaseSurface.h

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

85
src/CircleJoint.cpp

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

31
src/CircleJoint.h

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

16
src/EditorType.h

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

17
src/GuiMode.h

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

411
src/ProjectionEditor.cpp

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

89
src/ProjectionEditor.h

@ -3,49 +3,48 @@
#include "SurfaceManager.h" #include "SurfaceManager.h"
#include "CircleJoint.h" #include "CircleJoint.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
class ProjectionEditor class ProjectionEditor {
{ public:
public: ProjectionEditor();
ProjectionEditor(); ~ProjectionEditor();
~ProjectionEditor();
void registerAppEvents();
void registerAppEvents(); void unregisterAppEvents();
void unregisterAppEvents(); void registerMouseEvents();
void registerMouseEvents(); void unregisterMouseEvents();
void unregisterMouseEvents(); void registerKeyEvents();
void registerKeyEvents(); void unregisterKeyEvents();
void unregisterKeyEvents();
void enable();
void enable(); void disable();
void disable();
void update(ofEventArgs& args);
void draw();
void mouseDragged(ofMouseEventArgs& args);
void keyPressed(ofKeyEventArgs& args);
void keyReleased(ofKeyEventArgs& args);
void gotMessage(ofMessage& msg);
void setSurfaceManager(SurfaceManager* newSurfaceManager);
void clearJoints();
void createJoints();
void updateJoints();
void unselectAllJoints();
void moveSelectedSurface(ofVec2f by);
void stopDragJoints();
void updateVertices();
void moveSelection(ofVec2f by);
void setSnapDistance(float newSnapDistance);
CircleJoint* hitTestJoints(ofVec2f pos);
private:
SurfaceManager* surfaceManager;
vector<CircleJoint*> joints;
bool bShiftKeyDown;
float fSnapDistance;
void drawJoints();
};
}} void update(ofEventArgs& args);
void draw();
void mouseDragged(ofMouseEventArgs& args);
void keyPressed(ofKeyEventArgs& args);
void keyReleased(ofKeyEventArgs& args);
void gotMessage(ofMessage& msg);
void setSurfaceManager(SurfaceManager* newSurfaceManager);
void clearJoints();
void createJoints();
void updateJoints();
void unselectAllJoints();
void moveSelectedSurface(ofVec2f by);
void stopDragJoints();
void updateVertices();
void moveSelection(ofVec2f by);
void setSnapDistance(float newSnapDistance);
CircleJoint* hitTestJoints(ofVec2f pos);
private:
SurfaceManager* surfaceManager;
vector<CircleJoint*> joints;
bool bShiftKeyDown;
float fSnapDistance;
void drawJoints();
};
}
}

473
src/QuadSurface.cpp

@ -1,266 +1,243 @@
#include "QuadSurface.h" #include "QuadSurface.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
QuadSurface::QuadSurface() QuadSurface::QuadSurface() {
{ cout << "QuadSurface constructor." << endl;
cout << "QuadSurface constructor." << endl; setup();
setup(); }
}
QuadSurface::~QuadSurface() { cout << "QuadSurface destructor." << endl; }
QuadSurface::~QuadSurface()
{ void QuadSurface::setup() {
cout << "QuadSurface destructor." << endl; // Create 4 points for the 2 triangles
} ofVec2f p1 = ofVec2f(0, 0);
ofVec2f p2 = ofVec2f(0, ofGetHeight());
void QuadSurface::setup() ofVec2f p3 = ofVec2f(ofGetWidth(), ofGetHeight());
{ ofVec2f p4 = ofVec2f(ofGetWidth(), 0);
// Create 4 points for the 2 triangles
ofVec2f p1 = ofVec2f(0, 0); // Create 4 point for the texture coordinates
ofVec2f p2 = ofVec2f(0, ofGetHeight()); ofVec2f t1 = ofVec2f(ofVec2f(0.0f, 0.0f));
ofVec2f p3 = ofVec2f(ofGetWidth(), ofGetHeight()); ofVec2f t2 = ofVec2f(ofVec2f(1.0f, 0.0f));
ofVec2f p4 = ofVec2f(ofGetWidth(), 0); ofVec2f t3 = ofVec2f(ofVec2f(1.0f, 1.0f));
ofVec2f t4 = ofVec2f(ofVec2f(0.0f, 1.0f));
// Create 4 point for the texture coordinates
ofVec2f t1 = ofVec2f(ofVec2f(0.0f, 0.0f));
ofVec2f t2 = ofVec2f(ofVec2f(1.0f, 0.0f));
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 );
}
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;
// Clear mesh
mesh.clear();
// Create a surface with the points
mesh.addVertex( p1 );
mesh.addVertex( p2 );
mesh.addVertex( p3 );
mesh.addVertex( p4 );
// Add 2 triangles
mesh.addTriangle(0, 2, 3);
mesh.addTriangle(0, 1, 2);
// Add texture coordinates
mesh.addTexCoord(t1);
mesh.addTexCoord(t2);
mesh.addTexCoord(t3);
mesh.addTexCoord(t4);
// Pure GL setup
// indices
quadIndices[0] = 0;
quadIndices[1] = 1;
quadIndices[2] = 2;
quadIndices[3] = 0;
quadIndices[4] = 2;
quadIndices[5] = 3;
//tex coords (those are alway 0)
quadTexCoordinates[2] = 0;
quadTexCoordinates[6] = 0;
quadTexCoordinates[10] = 0;
quadTexCoordinates[14] = 0;
calculate4dTextureCoords();
}
void QuadSurface::draw()
{
/*if(mesh.haveVertsChanged() || mesh.haveTexCoordsChanged()){
calculate4dTextureCoords();
}*/
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(4, GL_FLOAT, 0, quadTexCoordinates);
glVertexPointer(3, GL_FLOAT, 0, quadVertices);
texture->bind();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, quadIndices);
texture->unbind();
}
void QuadSurface::setVertex(int index, ofVec2f p) setup(p1, p2, p3, p4, t1, t2, t3, t4, texture);
{ }
if ( index > 3 ) {
ofLog() << "Vertex with this index does not exist: " << index << endl; void QuadSurface::setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4,
return; ofVec2f t1, ofVec2f t2, ofVec2f t3, ofVec2f t4,
} ofTexture* texturePtr) {
// Assign texture
mesh.setVertex(index, p); texture = texturePtr;
calculate4dTextureCoords();
}
void QuadSurface::setTexCoord(int index, ofVec2f t) // Clear mesh
{ mesh.clear();
if ( index > 3 ) {
ofLog() << "Texture coordinate with this index does not exist: " << index << endl;
return;
}
mesh.setTexCoord(index, t);
calculate4dTextureCoords();
}
void QuadSurface::moveBy(ofVec2f v) // Create a surface with the points
{ mesh.addVertex(p1);
vector<ofVec3f>& vertices = getVertices(); mesh.addVertex(p2);
for (int i=0; i<vertices.size(); i++) { mesh.addVertex(p3);
vertices[i] += v; mesh.addVertex(p4);
}
calculate4dTextureCoords();
}
int QuadSurface::getType() // Add 2 triangles
{ mesh.addTriangle(0, 2, 3);
return SurfaceType::QUAD_SURFACE; mesh.addTriangle(0, 1, 2);
}
bool QuadSurface::hitTest(ofVec2f p) // Add texture coordinates
{ mesh.addTexCoord(t1);
// Construct ofPolyline from vertices mesh.addTexCoord(t2);
ofPolyline line = getHitArea(); mesh.addTexCoord(t3);
mesh.addTexCoord(t4);
if ( line.inside(p.x, p.y) ) {
return true;
} else {
return false;
}
}
ofVec2f QuadSurface::getVertex(int index) // Pure GL setup
{ // indices
if ( index > 3 ) { quadIndices[0] = 0;
ofLog() << "Vertex with this index does not exist: " << index << endl; quadIndices[1] = 1;
throw std::runtime_error("Vertex index out of bounds."); quadIndices[2] = 2;
} quadIndices[3] = 0;
quadIndices[4] = 2;
ofVec3f vert = mesh.getVertex(index); quadIndices[5] = 3;
return ofVec2f(vert.x, vert.y); // tex coords (those are alway 0)
} quadTexCoordinates[2] = 0;
quadTexCoordinates[6] = 0;
quadTexCoordinates[10] = 0;
quadTexCoordinates[14] = 0;
ofVec2f QuadSurface::getTexCoord(int index) calculate4dTextureCoords();
{
if (index > 3) {
throw std::runtime_error("Texture coordinate index out of bounds.");
}
return mesh.getTexCoord(index);
} }
ofPolyline QuadSurface::getHitArea() void QuadSurface::draw() {
{ /*if(mesh.haveVertsChanged() || mesh.haveTexCoordsChanged()){
ofPolyline line; calculate4dTextureCoords();
line.addVertex( ofPoint( mesh.getVertex(0).x, mesh.getVertex(0).y ) ); }*/
line.addVertex( ofPoint( mesh.getVertex(1).x, mesh.getVertex(1).y ) ); glEnableClientState(GL_TEXTURE_COORD_ARRAY);
line.addVertex( ofPoint( mesh.getVertex(2).x, mesh.getVertex(2).y ) ); glTexCoordPointer(4, GL_FLOAT, 0, quadTexCoordinates);
line.addVertex( ofPoint( mesh.getVertex(3).x, mesh.getVertex(3).y ) ); glVertexPointer(3, GL_FLOAT, 0, quadVertices);
line.close();
return line;
}
ofPolyline QuadSurface::getTextureHitArea() texture->bind();
{ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, quadIndices);
ofPolyline line; texture->unbind();
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 ) );
}
line.close();
return line;
} }
vector<ofVec3f>& QuadSurface::getVertices() void QuadSurface::setVertex(int index, ofVec2f p) {
{ if (index > 3) {
// return only joint vertices ofLog() << "Vertex with this index does not exist: " << index << endl;
return mesh.getVertices(); return;
} }
vector<ofVec2f>& QuadSurface::getTexCoords() mesh.setVertex(index, p);
{ calculate4dTextureCoords();
return mesh.getTexCoords();
} }
void QuadSurface::calculate4dTextureCoords() void QuadSurface::setTexCoord(int index, ofVec2f t) {
{ if (index > 3) {
// Perspective Warping with OpenGL Fixed Pipeline and q coordinates ofLog() << "Texture coordinate with this index does not exist: " << index
// see: << endl;
// http://www.reedbeta.com/blog/2012/05/26/quadrilateral-interpolation-part-1/ return;
// for information on the technique }
// Pue OpenGL is used because the ofMesh sadly doesn't support ofVec4f as
// texture coordinates. mesh.setTexCoord(index, t);
// calculate intersection point calculate4dTextureCoords();
ofVec3f p0 = mesh.getVertex(0); }
ofVec3f p1 = mesh.getVertex(1);
ofVec3f p2 = mesh.getVertex(2); void QuadSurface::moveBy(ofVec2f v) {
ofVec3f p3 = mesh.getVertex(3); vector<ofVec3f>& vertices = getVertices();
for (int i = 0; i < vertices.size(); i++) {
ofVec3f t0 = mesh.getTexCoord(0); vertices[i] += v;
ofVec3f t1 = mesh.getTexCoord(1); }
ofVec3f t2 = mesh.getTexCoord(2); calculate4dTextureCoords();
ofVec3f t3 = mesh.getTexCoord(3); }
ofPoint interSect; int QuadSurface::getType() { return SurfaceType::QUAD_SURFACE; }
ofLineSegmentIntersection(ofPoint(p0.x, p0.y), ofPoint(p2.x, p2.y),
ofPoint(p1.x, p1.y), ofPoint(p3.x, p3.y), bool QuadSurface::hitTest(ofVec2f p) {
interSect); // Construct ofPolyline from vertices
ofVec3f interSecVec = ofVec3f(interSect.x, interSect.y, 0); ofPolyline line = getHitArea();
// calculate distances to intersection point if (line.inside(p.x, p.y)) {
float d0 = interSecVec.distance(p0); return true;
float d1 = interSecVec.distance(p1); } else {
float d2 = interSecVec.distance(p2); return false;
float d3 = interSecVec.distance(p3); }
}
// vertices
// top left corner ofVec2f QuadSurface::getVertex(int index) {
quadVertices[0] = p0.x; if (index > 3) {
quadVertices[1] = p0.y; ofLog() << "Vertex with this index does not exist: " << index << endl;
quadVertices[2] = 0; throw std::runtime_error("Vertex index out of bounds.");
// top right corner }
quadVertices[3] = p1.x;
quadVertices[4] = p1.y; ofVec3f vert = mesh.getVertex(index);
quadVertices[5] = 0; return ofVec2f(vert.x, vert.y);
// bottom right corner }
quadVertices[6] = p2.x;
quadVertices[7] = p2.y; ofVec2f QuadSurface::getTexCoord(int index) {
quadVertices[8] = 0; if (index > 3) {
// bottom left corner throw std::runtime_error("Texture coordinate index out of bounds.");
quadVertices[9] = p3.x; }
quadVertices[10] = p3.y;
quadVertices[11] = 0; return mesh.getTexCoord(index);
}
float q0 = (d0 + d2) / (d2);
float q1 = (d1 + d3) / (d3); ofPolyline QuadSurface::getHitArea() {
float q2 = (d2 + d0) / (d0); ofPolyline line;
float q3 = (d3 + d1) / (d1); line.addVertex(ofPoint(mesh.getVertex(0).x, mesh.getVertex(0).y));
line.addVertex(ofPoint(mesh.getVertex(1).x, mesh.getVertex(1).y));
quadTexCoordinates[0] = t0.x; line.addVertex(ofPoint(mesh.getVertex(2).x, mesh.getVertex(2).y));
quadTexCoordinates[1] = t0.y; line.addVertex(ofPoint(mesh.getVertex(3).x, mesh.getVertex(3).y));
quadTexCoordinates[3] = q0; line.close();
quadTexCoordinates[4] = t1.x * q1; return line;
quadTexCoordinates[5] = t1.y; }
quadTexCoordinates[7] = q1;
ofPolyline QuadSurface::getTextureHitArea() {
quadTexCoordinates[8] = t2.x * q2; ofPolyline line;
quadTexCoordinates[9] = t2.y * q2; vector<ofVec2f>& texCoords = mesh.getTexCoords();
quadTexCoordinates[11] = q2; ofVec2f textureSize = ofVec2f(texture->getWidth(), texture->getHeight());
for (int i = 0; i < texCoords.size(); i++) {
quadTexCoordinates[12] = t3.x; line.addVertex(ofPoint(texCoords[i] * textureSize));
quadTexCoordinates[13] = t3.y * q3; }
quadTexCoordinates[15] = q3; line.close();
} return line;
}
}} vector<ofVec3f>& QuadSurface::getVertices() {
// return only joint vertices
return mesh.getVertices();
}
vector<ofVec2f>& QuadSurface::getTexCoords() { return mesh.getTexCoords(); }
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/
// for information on the technique
// Pue OpenGL is used because the ofMesh sadly doesn't support ofVec4f as
// texture coordinates.
// calculate intersection point
ofVec3f p0 = mesh.getVertex(0);
ofVec3f p1 = mesh.getVertex(1);
ofVec3f p2 = mesh.getVertex(2);
ofVec3f p3 = mesh.getVertex(3);
ofVec3f t0 = mesh.getTexCoord(0);
ofVec3f t1 = mesh.getTexCoord(1);
ofVec3f t2 = mesh.getTexCoord(2);
ofVec3f t3 = mesh.getTexCoord(3);
ofPoint interSect;
ofLineSegmentIntersection(ofPoint(p0.x, p0.y), ofPoint(p2.x, p2.y),
ofPoint(p1.x, p1.y), ofPoint(p3.x, p3.y),
interSect);
ofVec3f interSecVec = ofVec3f(interSect.x, interSect.y, 0);
// calculate distances to intersection point
float d0 = interSecVec.distance(p0);
float d1 = interSecVec.distance(p1);
float d2 = interSecVec.distance(p2);
float d3 = interSecVec.distance(p3);
// vertices
// top left corner
quadVertices[0] = p0.x;
quadVertices[1] = p0.y;
quadVertices[2] = 0;
// top right corner
quadVertices[3] = p1.x;
quadVertices[4] = p1.y;
quadVertices[5] = 0;
// bottom right corner
quadVertices[6] = p2.x;
quadVertices[7] = p2.y;
quadVertices[8] = 0;
// bottom left corner
quadVertices[9] = p3.x;
quadVertices[10] = p3.y;
quadVertices[11] = 0;
float q0 = (d0 + d2) / (d2);
float q1 = (d1 + d3) / (d3);
float q2 = (d2 + d0) / (d0);
float q3 = (d3 + d1) / (d1);
quadTexCoordinates[0] = t0.x;
quadTexCoordinates[1] = t0.y;
quadTexCoordinates[3] = q0;
quadTexCoordinates[4] = t1.x * q1;
quadTexCoordinates[5] = t1.y;
quadTexCoordinates[7] = q1;
quadTexCoordinates[8] = t2.x * q2;
quadTexCoordinates[9] = t2.y * q2;
quadTexCoordinates[11] = q2;
quadTexCoordinates[12] = t3.x;
quadTexCoordinates[13] = t3.y * q3;
quadTexCoordinates[15] = q3;
}
}
}

63
src/QuadSurface.h

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

202
src/SourcesEditor.cpp

@ -1,139 +1,121 @@
#include "SourcesEditor.h" #include "SourcesEditor.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
SourcesEditor::SourcesEditor() SourcesEditor::SourcesEditor() {
{ defImgDir = DEFAULT_IMAGES_DIR;
defImgDir = DEFAULT_IMAGES_DIR; registerAppEvents();
registerAppEvents();
} }
SourcesEditor::~SourcesEditor() SourcesEditor::~SourcesEditor() {
{ unregisterAppEvents();
unregisterAppEvents(); delete gui;
delete gui; while (images.size()) {
while ( images.size() ) { delete images.back();
delete images.back(); images.pop_back();
images.pop_back(); }
}
} }
void SourcesEditor::registerAppEvents() void SourcesEditor::registerAppEvents() {
{ ofAddListener(ofEvents().setup, this, &SourcesEditor::setup);
ofAddListener(ofEvents().setup, this, &SourcesEditor::setup);
} }
void SourcesEditor::unregisterAppEvents() void SourcesEditor::unregisterAppEvents() {
{ ofRemoveListener(ofEvents().setup, this, &SourcesEditor::setup);
ofRemoveListener(ofEvents().setup, this, &SourcesEditor::setup);
} }
void SourcesEditor::setup(ofEventArgs& args) void SourcesEditor::setup(ofEventArgs& args) {
{ gui = new RadioList();
gui = new RadioList();
// read directory contents
ofDirectory imgDir;
imgDir.listDir(defImgDir);
imgDir.sort();
vector<string> vnames;
for(int i = 0; i < (int)imgDir.size(); i++){
//images[i].loadImage(imgDir.getPath(i));
vnames.push_back(imgDir.getName(i));
}
gui->setup("Images", vnames);
gui->setPosition(20, 20);
ofAddListener(gui->radioSelectedEvent, this, &SourcesEditor::guiEvent);
}
void SourcesEditor::draw() // read directory contents
{ ofDirectory imgDir;
// Don't draw if there is no source selected imgDir.listDir(defImgDir);
if ( surfaceManager->getSelectedSurface() == NULL ) { imgDir.sort();
return;
} vector<string> vnames;
gui->draw(); for (int i = 0; i < (int)imgDir.size(); i++) {
// images[i].loadImage(imgDir.getPath(i));
vnames.push_back(imgDir.getName(i));
}
gui->setup("Images", vnames);
gui->setPosition(20, 20);
ofAddListener(gui->radioSelectedEvent, this, &SourcesEditor::guiEvent);
} }
void SourcesEditor::loadImage( string name, string path ) void SourcesEditor::draw() {
{ // Don't draw if there is no source selected
images.push_back(new ofImage()); if (surfaceManager->getSelectedSurface() == NULL) {
images.back()->loadImage(path); return;
}
imageNames.push_back(name);
gui->draw();
ofSendMessage("imageLoaded");
} }
void SourcesEditor::disable() void SourcesEditor::loadImage(string name, string path) {
{ images.push_back(new ofImage());
gui->disable(); images.back()->loadImage(path);
imageNames.push_back(name);
ofSendMessage("imageLoaded");
} }
void SourcesEditor::enable() void SourcesEditor::disable() { gui->disable(); }
{
// Don't enable if there is no surface selected void SourcesEditor::enable() {
if ( surfaceManager->getSelectedSurface() == NULL ) { // Don't enable if there is no surface selected
cout << "No surface selected. Not enable()ing source list." << endl; if (surfaceManager->getSelectedSurface() == NULL) {
return; cout << "No surface selected. Not enable()ing source list." << endl;
} return;
}
gui->enable();
gui->enable();
} }
void SourcesEditor::setSurfaceManager(SurfaceManager *newSurfaceManager) void SourcesEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) {
{ surfaceManager = newSurfaceManager;
surfaceManager = newSurfaceManager;
} }
void SourcesEditor::selectImageSourceRadioButton(string name) void SourcesEditor::selectImageSourceRadioButton(string name) {
{ if (name == "none") {
if (name == "none") { gui->unselectAll();
gui->unselectAll(); return;
} else {
int i;
for (i = 0; i < gui->size(); i++) {
if (gui->getItemName(i) == name) {
gui->selectItem(i);
return; return;
} else { }
int i;
for (i = 0; i < gui->size(); i++) {
if (gui->getItemName(i) == name) {
gui->selectItem(i);
return;
}
}
} }
}
} }
int SourcesEditor::getLoadedTexCount() int SourcesEditor::getLoadedTexCount() { return images.size(); }
{
return images.size();
}
ofTexture* SourcesEditor::getTexture(int index) ofTexture* SourcesEditor::getTexture(int index) {
{ if (index >= images.size()) {
if (index >= images.size()){ throw std::runtime_error("Texture index out of bounds.");
throw std::runtime_error("Texture index out of bounds."); }
}
return &images[index]->getTextureReference();
}
void SourcesEditor::guiEvent(string &imageName) return &images[index]->getTextureReference();
{
string name = imageName;
if ( surfaceManager->getSelectedSurface() == NULL ) {
return;
}
stringstream ss;
ss << defImgDir << name;
cout << "attempt to load image: " << ss.str() << endl;
ofTexture* texture = surfaceManager->loadImageSource(name, ss.str());
surfaceManager->getSelectedSurface()->setTexture(texture);
surfaceManager->manageMemory();
} }
}} void SourcesEditor::guiEvent(string& imageName) {
string name = imageName;
if (surfaceManager->getSelectedSurface() == NULL) {
return;
}
stringstream ss;
ss << defImgDir << name;
cout << "attempt to load image: " << ss.str() << endl;
ofTexture* texture = surfaceManager->loadImageSource(name, ss.str());
surfaceManager->getSelectedSurface()->setTexture(texture);
surfaceManager->manageMemory();
}
}
}

61
src/SourcesEditor.h

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

818
src/SurfaceManager.cpp

@ -1,183 +1,168 @@
#include "SurfaceManager.h" #include "SurfaceManager.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
SurfaceManager::SurfaceManager() SurfaceManager::SurfaceManager() {}
{
SurfaceManager::~SurfaceManager() { clear(); }
void SurfaceManager::draw() {
for (int i = 0; i < surfaces.size(); i++) {
surfaces[i]->draw();
}
} }
SurfaceManager::~SurfaceManager() void SurfaceManager::addSurface(int surfaceType) {
{ if (surfaceType == SurfaceType::TRIANGLE_SURFACE) {
clear(); 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::draw() void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr) {
{ if (surfaceType == SurfaceType::TRIANGLE_SURFACE) {
for ( int i=0; i<surfaces.size(); i++ ) { surfaces.push_back(new TriangleSurface());
surfaces[i]->draw(); surfaces.back()->setTexture(texturePtr);
} } else if (surfaceType == SurfaceType::QUAD_SURFACE) {
surfaces.push_back(new QuadSurface());
surfaces.back()->setTexture(texturePtr);
} else {
throw std::runtime_error("Attempt to add non-existing surface type.");
}
} }
void SurfaceManager::addSurface(int surfaceType) void SurfaceManager::addSurface(int surfaceType, vector<ofVec2f> vertices,
{ vector<ofVec2f> texCoords) {
if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) { if (surfaceType == SurfaceType::TRIANGLE_SURFACE) {
surfaces.push_back( new TriangleSurface() ); if (vertices.size() < 3) {
} throw std::runtime_error(
else if (surfaceType == SurfaceType::QUAD_SURFACE ) { "There must be 3 vertices for a triangle surface.");
surfaces.push_back( new QuadSurface() ); } else if (texCoords.size() < 3) {
throw std::runtime_error(
"There must be 3 texture coordinates for a triangle surface.");
} }
else {
throw std::runtime_error("Attempt to add non-existing surface type."); surfaces.push_back(new TriangleSurface());
for (int i = 0; i < 3; i++) {
surfaces.back()->setVertex(i, vertices[i]);
surfaces.back()->setTexCoord(i, texCoords[i]);
} }
}
void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr) } else if (surfaceType == SurfaceType::QUAD_SURFACE) {
{ if (vertices.size() < 4) {
if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) { throw std::runtime_error("There must be 4 vertices for a quad surface.");
surfaces.push_back( new TriangleSurface() ); } else if (texCoords.size() < 4) {
surfaces.back()->setTexture(texturePtr); throw std::runtime_error(
} "There must be 4 texture coordinates for a quad surface.");
else if (surfaceType == SurfaceType::QUAD_SURFACE ) {
surfaces.push_back( new QuadSurface() );
surfaces.back()->setTexture(texturePtr);
} }
else {
throw std::runtime_error("Attempt to add non-existing surface type."); surfaces.push_back(new QuadSurface());
for (int i = 0; i < 4; i++) {
surfaces.back()->setVertex(i, vertices[i]);
surfaces.back()->setTexCoord(i, texCoords[i]);
} }
} else {
throw std::runtime_error("Attempt to add non-existing surface type.");
}
} }
void SurfaceManager::addSurface(int surfaceType, vector<ofVec2f> vertices, vector<ofVec2f> texCoords) void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr,
{ vector<ofVec2f> vertices,
if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) { vector<ofVec2f> texCoords) {
if (surfaceType == SurfaceType::TRIANGLE_SURFACE) {
if ( vertices.size() < 3 ) { if (vertices.size() < 3) {
throw std::runtime_error("There must be 3 vertices for a triangle surface."); throw std::runtime_error(
} else if (texCoords.size() < 3) { "There must be 3 vertices for a triangle surface.");
throw std::runtime_error("There must be 3 texture coordinates for a triangle surface."); } else if (texCoords.size() < 3) {
} throw std::runtime_error(
"Thre must be 3 texture coordinates for a triangle surface.");
surfaces.push_back( new TriangleSurface() );
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 ) {
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.");
}
surfaces.push_back( new QuadSurface() );
for ( int i=0; i<4; i++ ) {
surfaces.back()->setVertex(i, vertices[i]);
surfaces.back()->setTexCoord(i, texCoords[i]);
}
}
else {
throw std::runtime_error("Attempt to add non-existing surface type.");
} }
} surfaces.push_back(new TriangleSurface());
surfaces.back()->setTexture(texturePtr);
void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vector<ofVec2f> vertices, vector<ofVec2f> texCoords) for (int i = 0; i < 3; i++) {
{ surfaces.back()->setVertex(i, vertices[i]);
if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) { surfaces.back()->setTexCoord(i, texCoords[i]);
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.");
}
surfaces.push_back( new TriangleSurface() );
surfaces.back()->setTexture(texturePtr);
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 ) {
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.");
}
surfaces.push_back( new QuadSurface() );
surfaces.back()->setTexture(texturePtr);
for ( int i=0; i<4; i++ ) {
surfaces.back()->setVertex(i, vertices[i]);
surfaces.back()->setTexCoord(i, texCoords[i]);
}
} }
else {
throw std::runtime_error("Attempt to add non-existing surface type."); } 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.");
} }
}
void SurfaceManager::removeSelectedSurface() surfaces.push_back(new QuadSurface());
{ surfaces.back()->setTexture(texturePtr);
if ( selectedSurface == NULL ) return;
for (int i = 0; i < 4; i++) {
for ( int i=0; i<surfaces.size(); i++ ) { surfaces.back()->setVertex(i, vertices[i]);
if ( surfaces[i] == selectedSurface ) { surfaces.back()->setTexCoord(i, texCoords[i]);
delete surfaces[i];
surfaces.erase(surfaces.begin()+i);
selectedSurface = NULL;
break;
}
} }
} else {
throw std::runtime_error("Attempt to add non-existing surface type.");
}
} }
void SurfaceManager::manageMemory() void SurfaceManager::removeSelectedSurface() {
{ if (selectedSurface == NULL) return;
// 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 < surfaces.size(); i++) {
bool bAssigned = false; if (surfaces[i] == selectedSurface) {
delete surfaces[i];
for ( int j=0; j<surfaces.size(); j++ ) { surfaces.erase(surfaces.begin() + i);
if ( surfaces[j]->getTexture() == &loadedImageSources[i]->getTextureReference() ) { selectedSurface = NULL;
bAssigned = true; break;
break;
}
}
if ( !bAssigned ) {
// purge the image source from memory
delete loadedImageSources[i];
loadedImageSources.erase(loadedImageSources.begin()+i);
cout << "Deleting image source: " << loadedImageSourceNames[i] << endl;
loadedImageSourceNames.erase(loadedImageSourceNames.begin()+i);
i--;
}
} }
}
} }
void SurfaceManager::clear() void SurfaceManager::manageMemory() {
{ // check if each of the sources is assigned to a surface or not
// delete all extra allocations from the heap for (int i = 0; i < loadedImageSources.size(); i++) {
while ( surfaces.size() ) { bool bAssigned = false;
delete surfaces.back();
surfaces.pop_back(); for (int j = 0; j < surfaces.size(); j++) {
} if (surfaces[j]->getTexture() ==
&loadedImageSources[i]->getTextureReference()) {
while ( loadedImageSources.size() ) { bAssigned = true;
delete loadedImageSources.back(); break;
loadedImageSources.pop_back(); }
} }
while ( loadedImageSourceNames.size() ) { if (!bAssigned) {
loadedImageSourceNames.pop_back(); // purge the image source from memory
delete loadedImageSources[i];
loadedImageSources.erase(loadedImageSources.begin() + i);
cout << "Deleting image source: " << loadedImageSourceNames[i] << endl;
loadedImageSourceNames.erase(loadedImageSourceNames.begin() + i);
i--;
} }
}
}
void SurfaceManager::clear() {
// delete all extra allocations from the heap
while (surfaces.size()) {
delete surfaces.back();
surfaces.pop_back();
}
while (loadedImageSources.size()) {
delete loadedImageSources.back();
loadedImageSources.pop_back();
}
while (loadedImageSourceNames.size()) {
loadedImageSourceNames.pop_back();
}
} }
// String getTypeString(SurfaceType e) // String getTypeString(SurfaceType e)
@ -190,299 +175,288 @@ void SurfaceManager::clear()
// } // }
// } // }
void SurfaceManager::saveXmlSettings(string fileName) void SurfaceManager::saveXmlSettings(string fileName) {
{ xmlSettings.clear();
xmlSettings.clear();
// save surfaces
// save surfaces xmlSettings.addTag("surfaces");
xmlSettings.addTag("surfaces"); xmlSettings.pushTag("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);
xmlSettings.addTag("surface"); BaseSurface* surface = surfaces[i];
xmlSettings.pushTag("surface", i);
BaseSurface* surface = surfaces[i]; xmlSettings.addTag("vertices");
xmlSettings.pushTag("vertices");
xmlSettings.addTag("vertices"); vector<ofVec3f>* vertices = &surface->getVertices();
xmlSettings.pushTag("vertices"); for (int j = 0; j < vertices->size(); j++) {
vector<ofVec3f>* vertices = &surface->getVertices(); xmlSettings.addTag("vertex");
for ( int j=0; j<vertices->size(); j++ ) { xmlSettings.pushTag("vertex", j);
xmlSettings.addTag("vertex"); ofVec3f* vertex = &(*vertices)[j];
xmlSettings.pushTag("vertex", j); xmlSettings.addValue("x", vertex->x);
ofVec3f* vertex = &(*vertices)[j]; xmlSettings.addValue("y", vertex->y);
xmlSettings.addValue("x", vertex->x);
xmlSettings.addValue("y", vertex->y); // we don't need z as it will be 0 anyways
// we don't need z as it will be 0 anyways xmlSettings.popTag(); // vertex
xmlSettings.popTag(); // vertex
}
xmlSettings.popTag(); // vertices
xmlSettings.addTag("texCoords");
xmlSettings.pushTag("texCoords");
vector<ofVec2f>* texCoords = &surface->getTexCoords();
for ( int j=0; j<texCoords->size(); j++ ) {
xmlSettings.addTag("texCoord");
xmlSettings.pushTag("texCoord", j);
ofVec2f* texCoord = &(*texCoords)[j];
xmlSettings.addValue("x", texCoord->x);
xmlSettings.addValue("y", texCoord->y);
xmlSettings.popTag(); // texCoord
}
xmlSettings.popTag(); // texCoords
xmlSettings.addTag("source");
xmlSettings.pushTag("source");
xmlSettings.addValue("source-type", "image");
xmlSettings.addValue("source-name", getSurfaceSourceName(surface));
//xmlSettings.addValue("source-path", "/root/etc/image.jpg");
xmlSettings.popTag(); // source
// xmlSettings.addTag("type");
// xmlSettings.pushTag("type");
// // surfaceType == SurfaceType::TRIANGLE_SURFACE
// SurfaceType surfaceType = &surface->getType();
// xmlSettings.addValue("surface-type", surfaceType);
// xmlSettings.popTag(); // type
xmlSettings.popTag(); // surface
} }
xmlSettings.popTag(); // surfaces xmlSettings.popTag(); // vertices
xmlSettings.save(fileName); xmlSettings.addTag("texCoords");
} xmlSettings.pushTag("texCoords");
vector<ofVec2f>* texCoords = &surface->getTexCoords();
for (int j = 0; j < texCoords->size(); j++) {
xmlSettings.addTag("texCoord");
xmlSettings.pushTag("texCoord", j);
ofVec2f* texCoord = &(*texCoords)[j];
xmlSettings.addValue("x", texCoord->x);
xmlSettings.addValue("y", texCoord->y);
xmlSettings.popTag(); // texCoord
}
xmlSettings.popTag(); // texCoords
void SurfaceManager::loadXmlSettings(string fileName) xmlSettings.addTag("source");
{ xmlSettings.pushTag("source");
xmlSettings.addValue("source-type", "image");
xmlSettings.addValue("source-name", getSurfaceSourceName(surface));
// xmlSettings.addValue("source-path", "/root/etc/image.jpg");
xmlSettings.popTag(); // source
if (!xmlSettings.loadFile(fileName)){ // xmlSettings.addTag("type");
ofLog(OF_LOG_WARNING, "Could not load XML settings."); // xmlSettings.pushTag("type");
return; // // surfaceType == SurfaceType::TRIANGLE_SURFACE
// SurfaceType surfaceType = &surface->getType();
// xmlSettings.addValue("surface-type", surfaceType);
// xmlSettings.popTag(); // type
xmlSettings.popTag(); // surface
}
xmlSettings.popTag(); // surfaces
xmlSettings.save(fileName);
}
void SurfaceManager::loadXmlSettings(string fileName) {
if (!xmlSettings.loadFile(fileName)) {
ofLog(OF_LOG_WARNING, "Could not load XML settings.");
return;
}
if (!xmlSettings.tagExists("surfaces")) {
ofLog(OF_LOG_WARNING, "XML settings is empty or has wrong markup.");
return;
}
xmlSettings.pushTag("surfaces");
int numSurfaces = xmlSettings.getNumTags("surface");
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") {
stringstream ss;
ss << "sources/images/" << sourceName; // TODO: reuse constants here
sourceTexture = loadImageSource(sourceName, ss.str());
} }
xmlSettings.popTag(); // source
if (!xmlSettings.tagExists("surfaces")){
ofLog(OF_LOG_WARNING, "XML settings is empty or has wrong markup."); // // attempt to load surface type
return; // ofLog(OF_LOG_WARNING, "Attempt to load surface type.");
// xmlSettings.pushTag("type");
// 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) {
ofLog(OF_LOG_NOTICE, "create Triangle");
xmlSettings.pushTag("vertex", 0);
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)));
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 2);
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 100.0f)));
xmlSettings.popTag();
xmlSettings.popTag(); // vertices
xmlSettings.pushTag("texCoords");
vector<ofVec2f> texCoords;
xmlSettings.pushTag("texCoord", 0);
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)));
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 2);
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);
} else {
addSurface(SurfaceType::TRIANGLE_SURFACE, vertices, texCoords);
}
} }
// it's a quad ?
xmlSettings.pushTag("surfaces"); else if (vertexCount == 4)
// if (surface-type == QUAD_SURFACE)
int numSurfaces = xmlSettings.getNumTags("surface"); {
for ( int i=0; i<numSurfaces; i++ ) { xmlSettings.pushTag("vertex", 0);
xmlSettings.pushTag("surface", i); vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
// attempt to load surface source xmlSettings.getValue("y", 0.0f)));
xmlSettings.pushTag("source"); xmlSettings.popTag();
string sourceType = xmlSettings.getValue("source-type", "image");
string sourceName = xmlSettings.getValue("source-name", "none"); xmlSettings.pushTag("vertex", 1);
ofTexture* sourceTexture = NULL; vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f),
if ( sourceName != "none" ) { xmlSettings.getValue("y", 0.0f)));
stringstream ss; xmlSettings.popTag();
ss << "sources/images/" << sourceName; // TODO: reuse constants here
sourceTexture = loadImageSource(sourceName, ss.str()); xmlSettings.pushTag("vertex", 2);
} vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f),
xmlSettings.popTag(); // source xmlSettings.getValue("y", 100.0f)));
xmlSettings.popTag();
// // attempt to load surface type
// ofLog(OF_LOG_WARNING, "Attempt to load surface type."); xmlSettings.pushTag("vertex", 3);
// xmlSettings.pushTag("type"); vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
// string surfaceType = xmlSettings.getValue("surface-type", "TRIANGLE_SURFACE"); xmlSettings.getValue("y", 100.0f)));
// xmlSettings.popTag(); // type xmlSettings.popTag();
xmlSettings.popTag(); // vertices
xmlSettings.pushTag("vertices");
vector<ofVec2f> vertices; xmlSettings.pushTag("texCoords");
int vertexCount = xmlSettings.getNumTags("vertex"); vector<ofVec2f> texCoords;
xmlSettings.pushTag("texCoord", 0);
//it's a triangle ? texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
if (vertexCount == 3) xmlSettings.getValue("y", 0.0f)));
{ xmlSettings.popTag();
ofLog(OF_LOG_NOTICE, "create Triangle");
xmlSettings.pushTag("vertex", 0); xmlSettings.pushTag("texCoord", 1);
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) ); texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f),
xmlSettings.popTag(); 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) ) ); xmlSettings.pushTag("texCoord", 2);
xmlSettings.popTag(); texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f),
xmlSettings.getValue("y", 1.0f)));
xmlSettings.pushTag("vertex", 2); xmlSettings.popTag();
vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 100.0f) ) );
xmlSettings.popTag(); xmlSettings.pushTag("texCoord", 3);
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.popTag(); // vertices xmlSettings.getValue("y", 1.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("texCoords");
xmlSettings.popTag(); // texCoords
vector<ofVec2f> texCoords;
// now we have variables sourceName and sourceTexture
xmlSettings.pushTag("texCoord", 0); // by checking those we can use one or another addSurface method
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) ); if (sourceName != "none" && sourceTexture != NULL) {
xmlSettings.popTag(); addSurface(SurfaceType::QUAD_SURFACE, sourceTexture, vertices,
texCoords);
xmlSettings.pushTag("texCoord", 1); } else {
texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 0.0f) ) ); addSurface(SurfaceType::QUAD_SURFACE, vertices, texCoords);
xmlSettings.popTag(); }
xmlSettings.pushTag("texCoord", 2);
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);
} else {
addSurface(SurfaceType::TRIANGLE_SURFACE, vertices, texCoords);
}
}
// it's a quad ?
else if (vertexCount == 4)
// if (surface-type == QUAD_SURFACE)
{
xmlSettings.pushTag("vertex", 0);
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) ) );
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 2);
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) ) );
xmlSettings.popTag();
xmlSettings.popTag(); // vertices
xmlSettings.pushTag("texCoords");
vector<ofVec2f> texCoords;
xmlSettings.pushTag("texCoord", 0);
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) ) );
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 2);
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) ) );
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);
} else {
addSurface(SurfaceType::QUAD_SURFACE, vertices, texCoords);
}
}
xmlSettings.popTag(); // surface
} }
xmlSettings.popTag(); // surfaces
}
BaseSurface* SurfaceManager::selectSurface(int index) xmlSettings.popTag(); // surface
{ }
if ( index >= surfaces.size() ) {
throw std::runtime_error("Surface index out of bounds.");
}
selectedSurface = surfaces[index];
// notify that a new surface has been selected
ofSendMessage("surfaceSelected");
}
BaseSurface* SurfaceManager::getSelectedSurface() xmlSettings.popTag(); // surfaces
{
return selectedSurface;
} }
void SurfaceManager::deselectSurface() BaseSurface* SurfaceManager::selectSurface(int index) {
{ if (index >= surfaces.size()) {
selectedSurface = NULL; throw std::runtime_error("Surface index out of bounds.");
} }
ofTexture* SurfaceManager::loadImageSource(string name, string path) selectedSurface = surfaces[index];
{
// check if it is loaded // notify that a new surface has been selected
for ( int i=0; i<loadedImageSourceNames.size(); i++ ) { ofSendMessage("surfaceSelected");
if ( loadedImageSourceNames[i] == name ) {
// this image is already loaded
return &loadedImageSources[i]->getTextureReference();
}
}
// not loaded - load
ofImage* image = new ofImage();
if ( !image->loadImage(path) ){
return NULL;
}
loadedImageSources.push_back(image);
loadedImageSourceNames.push_back(name);
return &image->getTextureReference();
} }
string SurfaceManager::getSelectedSurfaceSourceName() BaseSurface* SurfaceManager::getSelectedSurface() { return selectedSurface; }
{
if ( selectedSurface == NULL ) { void SurfaceManager::deselectSurface() { selectedSurface = NULL; }
return "none";
ofTexture* SurfaceManager::loadImageSource(string name, string path) {
// check if it is loaded
for (int i = 0; i < loadedImageSourceNames.size(); i++) {
if (loadedImageSourceNames[i] == name) {
// this image is already loaded
return &loadedImageSources[i]->getTextureReference();
} }
}
return getSurfaceSourceName( selectedSurface );
// not loaded - load
ofImage* image = new ofImage();
if (!image->loadImage(path)) {
return NULL;
}
loadedImageSources.push_back(image);
loadedImageSourceNames.push_back(name);
return &image->getTextureReference();
} }
string SurfaceManager::getSurfaceSourceName(BaseSurface *surface) string SurfaceManager::getSelectedSurfaceSourceName() {
{ if (selectedSurface == NULL) {
ofTexture* tex = surface->getTexture();
for ( int i=0; i<loadedImageSources.size(); i++ ) {
if (tex == &loadedImageSources[i]->getTextureReference()) {
return loadedImageSourceNames[i];
}
}
return "none"; return "none";
}
return getSurfaceSourceName(selectedSurface);
} }
BaseSurface* SurfaceManager::getSurface(int index) string SurfaceManager::getSurfaceSourceName(BaseSurface* surface) {
{ ofTexture* tex = surface->getTexture();
if ( index >= surfaces.size() ) { for (int i = 0; i < loadedImageSources.size(); i++) {
throw std::runtime_error("Surface index out of bounds."); if (tex == &loadedImageSources[i]->getTextureReference()) {
return NULL; return loadedImageSourceNames[i];
} }
}
return surfaces[index];
}
int SurfaceManager::size() return "none";
{
return surfaces.size();
} }
}} BaseSurface* SurfaceManager::getSurface(int index) {
if (index >= surfaces.size()) {
throw std::runtime_error("Surface index out of bounds.");
return NULL;
}
return surfaces[index];
}
int SurfaceManager::size() { return surfaces.size(); }
}
}

73
src/SurfaceManager.h

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

431
src/SurfaceManagerGui.cpp

@ -1,258 +1,243 @@
#include "SurfaceManagerGui.h" #include "SurfaceManagerGui.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
SurfaceManagerGui::SurfaceManagerGui() SurfaceManagerGui::SurfaceManagerGui() {
{ surfaceManager = NULL;
surfaceManager = NULL; guiMode = GuiMode::NONE;
guiMode = GuiMode::NONE; bDrag = false;
bDrag = false; registerMouseEvents();
registerMouseEvents(); ofHideCursor();
ofHideCursor();
} }
SurfaceManagerGui::~SurfaceManagerGui() SurfaceManagerGui::~SurfaceManagerGui() {
{ unregisterMouseEvents();
unregisterMouseEvents(); surfaceManager = NULL;
surfaceManager = NULL;
} }
void SurfaceManagerGui::registerMouseEvents() void SurfaceManagerGui::registerMouseEvents() {
{ ofAddListener(ofEvents().mousePressed, this,
ofAddListener(ofEvents().mousePressed, this, &SurfaceManagerGui::mousePressed); &SurfaceManagerGui::mousePressed);
ofAddListener(ofEvents().mouseReleased, this, &SurfaceManagerGui::mouseReleased); ofAddListener(ofEvents().mouseReleased, this,
ofAddListener(ofEvents().mouseDragged, this, &SurfaceManagerGui::mouseDragged); &SurfaceManagerGui::mouseReleased);
ofAddListener(ofEvents().mouseDragged, this,
&SurfaceManagerGui::mouseDragged);
} }
void SurfaceManagerGui::unregisterMouseEvents() void SurfaceManagerGui::unregisterMouseEvents() {
{ ofRemoveListener(ofEvents().mousePressed, this,
ofRemoveListener(ofEvents().mousePressed, this, &SurfaceManagerGui::mousePressed); &SurfaceManagerGui::mousePressed);
ofRemoveListener(ofEvents().mouseReleased, this, &SurfaceManagerGui::mouseReleased); ofRemoveListener(ofEvents().mouseReleased, this,
ofRemoveListener(ofEvents().mouseDragged, this, &SurfaceManagerGui::mouseDragged); &SurfaceManagerGui::mouseReleased);
ofRemoveListener(ofEvents().mouseDragged, this,
&SurfaceManagerGui::mouseDragged);
} }
void SurfaceManagerGui::draw() void SurfaceManagerGui::draw() {
{ if (surfaceManager == NULL) return;
if ( surfaceManager == NULL ) return;
if ( guiMode == GuiMode::NONE ) {
surfaceManager->draw();
} else if ( guiMode == GuiMode::TEXTURE_MAPPING ) {
// draw the texture of the selected surface
if ( surfaceManager->getSelectedSurface() != NULL ) {
surfaceManager->getSelectedSurface()->drawTexture( ofVec2f(0,0) );
}
// draw surfaces with opacity
ofPushStyle();
ofSetColor(255, 255, 255, 200);
surfaceManager->draw();
ofPopStyle();
// highlight selected surface
drawSelectedSurfaceHighlight();
// hilight selected surface texture
drawSelectedSurfaceTextureHighlight();
// draw texture editing GUI on top
textureEditor.draw();
} else if ( guiMode == GuiMode::PROJECTION_MAPPING ) {
// draw projection surfaces first
surfaceManager->draw();
// highlight selected surface
drawSelectedSurfaceHighlight();
// draw projection mapping editing gui
projectionEditor.draw();
} else if ( guiMode == GuiMode::SOURCE_SELECTION ) {
// draw projection surfaces first
surfaceManager->draw();
// highlight selected surface
drawSelectedSurfaceHighlight();
sourcesEditor.draw();
}
}
void SurfaceManagerGui::mousePressed(ofMouseEventArgs &args) if (guiMode == GuiMode::NONE) {
{ surfaceManager->draw();
if ( guiMode == GuiMode::NONE ) { } else if (guiMode == GuiMode::TEXTURE_MAPPING) {
return; // draw the texture of the selected surface
} else if ( guiMode == GuiMode::TEXTURE_MAPPING ) { if (surfaceManager->getSelectedSurface() != NULL) {
surfaceManager->getSelectedSurface()->drawTexture(ofVec2f(0, 0));
bool bSurfaceSelected = false;
CircleJoint* hitJoint = textureEditor.hitTestJoints(ofVec2f(args.x, args.y));
if ( hitJoint != NULL ) {
textureEditor.unselectAllJoints();
hitJoint->select();
hitJoint->startDrag();
bSurfaceSelected = true;
} else {
textureEditor.unselectAllJoints();
}
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) ) {
clickPosition = ofVec2f(args.x, args.y);
startDrag();
}
}
} else if ( guiMode == GuiMode::PROJECTION_MAPPING ) {
bool bSurfaceSelected = false;
CircleJoint* hitJoint = projectionEditor.hitTestJoints(ofVec2f(args.x, args.y));
if ( hitJoint != NULL ) {
projectionEditor.unselectAllJoints();
hitJoint->select();
hitJoint->startDrag();
bSurfaceSelected = true;
}
// 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) ) ) {
projectionEditor.clearJoints();
surfaceManager->selectSurface(i);
projectionEditor.createJoints();
bSurfaceSelected = true;
break;
}
}
}
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 ) {
// unselect if no surface selected
projectionEditor.clearJoints();
surfaceManager->deselectSurface();
}
} else if ( guiMode == GuiMode::SOURCE_SELECTION ) {
} }
}
void SurfaceManagerGui::mouseReleased(ofMouseEventArgs &args) // draw surfaces with opacity
{ ofPushStyle();
stopDrag(); ofSetColor(255, 255, 255, 200);
projectionEditor.stopDragJoints(); surfaceManager->draw();
textureEditor.stopDragJoints(); ofPopStyle();
// highlight selected surface
drawSelectedSurfaceHighlight();
// hilight selected surface texture
drawSelectedSurfaceTextureHighlight();
// draw texture editing GUI on top
textureEditor.draw();
} else if (guiMode == GuiMode::PROJECTION_MAPPING) {
// draw projection surfaces first
surfaceManager->draw();
// highlight selected surface
drawSelectedSurfaceHighlight();
// draw projection mapping editing gui
projectionEditor.draw();
} else if (guiMode == GuiMode::SOURCE_SELECTION) {
// draw projection surfaces first
surfaceManager->draw();
// highlight selected surface
drawSelectedSurfaceHighlight();
sourcesEditor.draw();
}
} }
void SurfaceManagerGui::mouseDragged(ofMouseEventArgs &args) void SurfaceManagerGui::mousePressed(ofMouseEventArgs& args) {
{ if (guiMode == GuiMode::NONE) {
if (bDrag) { return;
ofVec2f mousePosition = ofVec2f(args.x, args.y); } else if (guiMode == GuiMode::TEXTURE_MAPPING) {
ofVec2f distance = mousePosition - clickPosition; bool bSurfaceSelected = false;
if ( guiMode == GuiMode::PROJECTION_MAPPING ) { CircleJoint* hitJoint =
// add this distance to all vertices in surface textureEditor.hitTestJoints(ofVec2f(args.x, args.y));
projectionEditor.moveSelectedSurface(distance); if (hitJoint != NULL) {
} else if ( guiMode == GuiMode::TEXTURE_MAPPING ) { textureEditor.unselectAllJoints();
textureEditor.moveTexCoords(distance); hitJoint->select();
} hitJoint->startDrag();
clickPosition = mousePosition; bSurfaceSelected = true;
} else {
textureEditor.unselectAllJoints();
} }
}
void SurfaceManagerGui::setSurfaceManager(SurfaceManager* newSurfaceManager) if (surfaceManager->getSelectedSurface() != NULL && !bSurfaceSelected) {
{ // hittest texture area to see if we are hitting the texture surface
surfaceManager = newSurfaceManager; if (surfaceManager->getSelectedSurface()->getTextureHitArea().inside(
projectionEditor.setSurfaceManager( surfaceManager ); args.x, args.y)) {
sourcesEditor.setSurfaceManager( surfaceManager ); clickPosition = ofVec2f(args.x, args.y);
} startDrag();
}
}
} else if (guiMode == GuiMode::PROJECTION_MAPPING) {
bool bSurfaceSelected = false;
void SurfaceManagerGui::setMode(int newGuiMode) CircleJoint* hitJoint =
{ projectionEditor.hitTestJoints(ofVec2f(args.x, args.y));
if (newGuiMode != GuiMode::NONE && if (hitJoint != NULL) {
newGuiMode != GuiMode::TEXTURE_MAPPING && projectionEditor.unselectAllJoints();
newGuiMode != GuiMode::PROJECTION_MAPPING && hitJoint->select();
newGuiMode != GuiMode::SOURCE_SELECTION) { hitJoint->startDrag();
throw std::runtime_error("Trying to set invalid mode."); bSurfaceSelected = true;
} }
if ( newGuiMode == GuiMode::NONE ) { // attempt to select surface, loop from end to beginning
ofHideCursor(); if (!bSurfaceSelected) {
} else { for (int i = surfaceManager->size() - 1; i >= 0; i--) {
ofShowCursor(); if (surfaceManager->getSurface(i)->hitTest(ofVec2f(args.x, args.y))) {
projectionEditor.clearJoints();
surfaceManager->selectSurface(i);
projectionEditor.createJoints();
bSurfaceSelected = true;
break;
}
}
} }
guiMode = newGuiMode; if (bSurfaceSelected && hitJoint == NULL) {
// if not hitting the joints, start drag only if we have a selected
if ( guiMode == GuiMode::SOURCE_SELECTION ) { // surface
sourcesEditor.enable(); clickPosition = ofVec2f(args.x, args.y);
string sourceName = surfaceManager->getSelectedSurfaceSourceName(); startDrag();
sourcesEditor.selectImageSourceRadioButton(sourceName);
} else {
sourcesEditor.disable();
} }
if ( guiMode == GuiMode::TEXTURE_MAPPING ) { if (!bSurfaceSelected) {
textureEditor.enable(); // unselect if no surface selected
// refresh texture editor surface reference projectionEditor.clearJoints();
textureEditor.setSurface(surfaceManager->getSelectedSurface()); surfaceManager->deselectSurface();
} else {
textureEditor.disable();
} }
} else if (guiMode == GuiMode::SOURCE_SELECTION) {
}
}
void SurfaceManagerGui::mouseReleased(ofMouseEventArgs& args) {
stopDrag();
projectionEditor.stopDragJoints();
textureEditor.stopDragJoints();
}
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) {
projectionEditor.enable(); // add this distance to all vertices in surface
} else { projectionEditor.moveSelectedSurface(distance);
projectionEditor.disable(); } else if (guiMode == GuiMode::TEXTURE_MAPPING) {
textureEditor.moveTexCoords(distance);
} }
clickPosition = mousePosition;
}
} }
void SurfaceManagerGui::drawSelectedSurfaceHighlight() void SurfaceManagerGui::setSurfaceManager(SurfaceManager* newSurfaceManager) {
{ surfaceManager = newSurfaceManager;
if ( surfaceManager->getSelectedSurface() == NULL ) return; projectionEditor.setSurfaceManager(surfaceManager);
sourcesEditor.setSurfaceManager(surfaceManager);
ofPolyline line = surfaceManager->getSelectedSurface()->getHitArea();
ofPushStyle();
ofSetLineWidth(1);
ofSetColor(255, 255, 255, 255);
line.draw();
ofPopStyle();
} }
void SurfaceManagerGui::drawSelectedSurfaceTextureHighlight() void SurfaceManagerGui::setMode(int newGuiMode) {
{ if (newGuiMode != GuiMode::NONE && newGuiMode != GuiMode::TEXTURE_MAPPING &&
if ( surfaceManager->getSelectedSurface() == NULL ) return; newGuiMode != GuiMode::PROJECTION_MAPPING &&
newGuiMode != GuiMode::SOURCE_SELECTION) {
ofPolyline line = surfaceManager->getSelectedSurface()->getTextureHitArea(); throw std::runtime_error("Trying to set invalid mode.");
}
ofPushStyle();
ofSetLineWidth(1);
ofSetColor(255, 255, 0, 255);
line.draw();
ofPopStyle();
if (newGuiMode == GuiMode::NONE) {
ofHideCursor();
} else {
ofShowCursor();
}
guiMode = newGuiMode;
if (guiMode == GuiMode::SOURCE_SELECTION) {
sourcesEditor.enable();
string sourceName = surfaceManager->getSelectedSurfaceSourceName();
sourcesEditor.selectImageSourceRadioButton(sourceName);
} else {
sourcesEditor.disable();
}
if (guiMode == GuiMode::TEXTURE_MAPPING) {
textureEditor.enable();
// refresh texture editor surface reference
textureEditor.setSurface(surfaceManager->getSelectedSurface());
} else {
textureEditor.disable();
}
if (guiMode == GuiMode::PROJECTION_MAPPING) {
projectionEditor.enable();
} else {
projectionEditor.disable();
}
} }
void SurfaceManagerGui::startDrag() void SurfaceManagerGui::drawSelectedSurfaceHighlight() {
{ if (surfaceManager->getSelectedSurface() == NULL) return;
bDrag = true;
ofPolyline line = surfaceManager->getSelectedSurface()->getHitArea();
ofPushStyle();
ofSetLineWidth(1);
ofSetColor(255, 255, 255, 255);
line.draw();
ofPopStyle();
} }
void SurfaceManagerGui::stopDrag() void SurfaceManagerGui::drawSelectedSurfaceTextureHighlight() {
{ if (surfaceManager->getSelectedSurface() == NULL) return;
bDrag = false;
ofPolyline line = surfaceManager->getSelectedSurface()->getTextureHitArea();
ofPushStyle();
ofSetLineWidth(1);
ofSetColor(255, 255, 0, 255);
line.draw();
ofPopStyle();
} }
}} void SurfaceManagerGui::startDrag() { bDrag = true; }
void SurfaceManagerGui::stopDrag() { bDrag = false; }
}
}

64
src/SurfaceManagerGui.h

@ -12,37 +12,35 @@
#include "SourcesEditor.h" #include "SourcesEditor.h"
#include "GuiMode.h" #include "GuiMode.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
class SurfaceManagerGui class SurfaceManagerGui {
{ public:
public: SurfaceManagerGui();
SurfaceManagerGui(); ~SurfaceManagerGui();
~SurfaceManagerGui();
void registerMouseEvents();
void registerMouseEvents(); void unregisterMouseEvents();
void unregisterMouseEvents();
void draw();
void mousePressed(ofMouseEventArgs& args);
void mouseReleased(ofMouseEventArgs& args);
void mouseDragged(ofMouseEventArgs& args);
void setSurfaceManager(SurfaceManager* newSurfaceManager);
void setMode(int newGuiMode);
void drawSelectedSurfaceHighlight();
void drawSelectedSurfaceTextureHighlight();
void startDrag();
void stopDrag();
private:
SurfaceManager* surfaceManager;
TextureEditor textureEditor;
ProjectionEditor projectionEditor;
SourcesEditor sourcesEditor;
int guiMode;
bool bDrag;
ofVec2f clickPosition;
};
}} void draw();
void mousePressed(ofMouseEventArgs& args);
void mouseReleased(ofMouseEventArgs& args);
void mouseDragged(ofMouseEventArgs& args);
void setSurfaceManager(SurfaceManager* newSurfaceManager);
void setMode(int newGuiMode);
void drawSelectedSurfaceHighlight();
void drawSelectedSurfaceTextureHighlight();
void startDrag();
void stopDrag();
private:
SurfaceManager* surfaceManager;
TextureEditor textureEditor;
ProjectionEditor projectionEditor;
SourcesEditor sourcesEditor;
int guiMode;
bool bDrag;
ofVec2f clickPosition;
};
}
}

16
src/SurfaceType.h

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

288
src/TextureEditor.cpp

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

70
src/TextureEditor.h

@ -5,41 +5,39 @@
#include "BaseSurface.h" #include "BaseSurface.h"
#include "CircleJoint.h" #include "CircleJoint.h"
namespace ofx {
namespace piMapper {
class TextureEditor {
public:
TextureEditor();
~TextureEditor();
namespace ofx{ void registerAppEvents();
namespace piMapper{ void unregisterAppEvents();
class TextureEditor void registerKeyEvents();
{ void unregisterKeyEvents();
public: void enable();
TextureEditor(); void disable();
~TextureEditor();
void update(ofEventArgs& args);
void registerAppEvents(); void keyPressed(ofKeyEventArgs& args);
void unregisterAppEvents(); void keyReleased(ofKeyEventArgs& args);
void registerKeyEvents(); void draw();
void unregisterKeyEvents(); void drawJoints();
void enable(); void setSurface(BaseSurface* newSurface);
void disable(); void clear();
void createJoints();
void update(ofEventArgs& args); void clearJoints();
void keyPressed(ofKeyEventArgs& args); void unselectAllJoints();
void keyReleased(ofKeyEventArgs& args); void moveTexCoords(ofVec2f by);
void draw(); void stopDragJoints();
void drawJoints(); void moveSelection(ofVec2f by);
void setSurface(BaseSurface* newSurface); CircleJoint* hitTestJoints(ofVec2f pos);
void clear();
void createJoints();
void clearJoints();
void unselectAllJoints();
void moveTexCoords(ofVec2f by);
void stopDragJoints();
void moveSelection(ofVec2f by);
CircleJoint* hitTestJoints(ofVec2f pos);
private:
BaseSurface* surface;
vector<CircleJoint*> joints;
bool bShiftKeyDown;
};
}} private:
BaseSurface* surface;
vector<CircleJoint*> joints;
bool bShiftKeyDown;
};
}
}

233
src/TriangleSurface.cpp

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

49
src/TriangleSurface.h

@ -4,30 +4,29 @@
#include "BaseSurface.h" #include "BaseSurface.h"
#include "SurfaceType.h" #include "SurfaceType.h"
namespace ofx {
namespace piMapper {
class TriangleSurface : public BaseSurface {
public:
TriangleSurface();
~TriangleSurface();
namespace ofx{ void setup();
namespace piMapper{ void setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2,
class TriangleSurface : public BaseSurface ofVec2f t3, ofTexture* texturePtr);
{ void draw();
public: void setVertex(int index, ofVec2f p);
TriangleSurface(); void setTexCoord(int index, ofVec2f t);
~TriangleSurface(); void moveBy(ofVec2f v);
void setup();
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 moveBy(ofVec2f v);
int getType();
bool hitTest(ofVec2f p);
ofVec2f getVertex(int index);
ofVec2f getTexCoord(int index);
ofPolyline getHitArea();
ofPolyline getTextureHitArea();
vector<ofVec3f>& getVertices();
vector<ofVec2f>& getTexCoords();
};
}} int getType();
bool hitTest(ofVec2f p);
ofVec2f getVertex(int index);
ofVec2f getTexCoord(int index);
ofPolyline getHitArea();
ofPolyline getTextureHitArea();
vector<ofVec3f>& getVertices();
vector<ofVec2f>& getTexCoords();
};
}
}

267
src/ui/RadioList.cpp

@ -1,192 +1,155 @@
#include "RadioList.h" #include "RadioList.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
RadioList::RadioList() RadioList::RadioList() {
{ storedTitle = "";
storedTitle = ""; storedSelectedItem = 0;
storedSelectedItem = 0;
} }
RadioList::RadioList(vector<string> &labels) RadioList::RadioList(vector<string>& labels) {
{ RadioList();
RadioList(); setup(labels);
setup(labels);
} }
RadioList::RadioList(string title, vector<string> &labels) RadioList::RadioList(string title, vector<string>& labels) {
{ RadioList();
RadioList(); setup(title, labels);
setup(title, labels);
} }
RadioList::~RadioList() RadioList::~RadioList() { clear(); }
{
clear();
}
void RadioList::setup(vector<string> &labels) void RadioList::setup(vector<string>& labels) {
{ // Copy incomming labels for later use
// Copy incomming labels for later use storedLabels = labels;
storedLabels = labels;
// Create toggles with labels from the labels arg
// Create toggles with labels from the labels arg int i;
int i; for (i = 0; i < labels.size(); i++) {
for (i = 0; i < labels.size(); i++) { ofxToggle* toggle = new ofxToggle();
ofxToggle* toggle = new ofxToggle(); toggle->setup(false);
toggle->setup(false); toggle->setName(labels[i]);
toggle->setName(labels[i]); toggle->addListener(this, &RadioList::onToggleClicked);
toggle->addListener(this, &RadioList::onToggleClicked); guiGroup.add(toggle);
guiGroup.add(toggle); }
}
cout << "num items: " << guiGroup.getNumControls() << endl;
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
// Store title for later use storedTitle = title;
storedTitle = title; guiGroup.setName(title);
guiGroup.setName(title); setup(labels);
setup(labels);
} }
void RadioList::draw() void RadioList::draw() { guiGroup.draw(); }
{
guiGroup.draw(); void RadioList::setTitle(string title) {
storedTitle = title;
guiGroup.setName(title);
} }
void RadioList::setTitle(string title) void RadioList::setPosition(ofPoint p) { guiGroup.setPosition(p); }
{
storedTitle = title; void RadioList::setPosition(float x, float y) { guiGroup.setPosition(x, y); }
guiGroup.setName(title);
void RadioList::selectItem(int index) {
if (index >= guiGroup.getNumControls()) {
return;
}
unselectAll();
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(index));
toggle->removeListener(this, &RadioList::onToggleClicked);
*toggle = true; // Select the specific radio button
toggle->addListener(this, &RadioList::onToggleClicked);
string name = toggle->getName();
ofNotifyEvent(radioSelectedEvent, name, this);
storedSelectedItem = index;
} }
void RadioList::setPosition(ofPoint p) void RadioList::enable() {
{ if (guiGroup.getNumControls() >= 0) {
guiGroup.setPosition(p); clear();
}
// Rebuild everyting
setup(storedTitle, storedLabels);
// Select the stored selected item without throwing an event
ofxToggle* toggle =
static_cast<ofxToggle*>(guiGroup.getControl(storedSelectedItem));
toggle->removeListener(this, &RadioList::onToggleClicked);
*toggle = true;
toggle->addListener(this, &RadioList::onToggleClicked);
cout << "num items after enable: " << guiGroup.getNumControls() << endl;
} }
void RadioList::setPosition(float x, float y) void RadioList::disable() {
{ // Just remove everything
guiGroup.setPosition(x, y); clear();
} }
void RadioList::selectItem(int index) void RadioList::clear() {
{ int i;
if (index >= guiGroup.getNumControls()) { for (i = 0; i < guiGroup.getNumControls(); i++) {
return; ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i));
}
unselectAll();
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(index));
toggle->removeListener(this, &RadioList::onToggleClicked); toggle->removeListener(this, &RadioList::onToggleClicked);
*toggle = true; // Select the specific radio button delete toggle;
toggle->addListener(this, &RadioList::onToggleClicked); }
string name = toggle->getName(); guiGroup.clear();
ofNotifyEvent(radioSelectedEvent, name, this);
storedSelectedItem = index;
} }
void RadioList::enable() void RadioList::unselectAll() {
{ int i;
if (guiGroup.getNumControls() >= 0) { for (i = 0; i < guiGroup.getNumControls(); i++) {
clear(); ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i));
} ofParameter<bool>* paramPtr =
static_cast<ofParameter<bool>*>(&toggle->getParameter());
// Rebuild everyting
setup(storedTitle, storedLabels);
// Select the stored selected item without throwing an event
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(storedSelectedItem));
toggle->removeListener(this, &RadioList::onToggleClicked); toggle->removeListener(this, &RadioList::onToggleClicked);
*toggle = true; *toggle = false;
toggle->addListener(this, &RadioList::onToggleClicked); toggle->addListener(this, &RadioList::onToggleClicked);
}
cout << "num items after enable: " << guiGroup.getNumControls() << endl;
} }
void RadioList::disable() ofPoint RadioList::getPosition() { return guiGroup.getPosition(); }
{
// Just remove everything
clear();
}
void RadioList::clear() float RadioList::getWidth() { return guiGroup.getWidth(); }
{
int i;
for (i = 0; i < guiGroup.getNumControls(); i++) {
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i));
toggle->removeListener(this, &RadioList::onToggleClicked);
delete toggle;
}
guiGroup.clear();
}
void RadioList::unselectAll() float RadioList::getHeight() { return guiGroup.getHeight(); }
{
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());
toggle->removeListener(this, &RadioList::onToggleClicked);
*toggle = false;
toggle->addListener(this, &RadioList::onToggleClicked);
}
}
ofPoint RadioList::getPosition() string RadioList::getTitle() { return guiGroup.getName(); }
{
return guiGroup.getPosition();
}
float RadioList::getWidth() string RadioList::getItemName(int index) {
{ if (index >= guiGroup.getNumControls()) {
return guiGroup.getWidth(); return "";
} }
float RadioList::getHeight() ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(index));
{ return toggle->getName();
return guiGroup.getHeight();
} }
string RadioList::getTitle() int RadioList::size() { return guiGroup.getNumControls(); }
{
return guiGroup.getName();
}
string RadioList::getItemName(int index) void RadioList::onToggleClicked(bool& toggleValue) {
{ unselectAll();
if (index >= guiGroup.getNumControls()) {
return "";
}
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(index));
return toggle->getName();
}
int RadioList::size() // Search for the actual toggle triggering the event
{ int i;
return guiGroup.getNumControls(); for (i = 0; i < guiGroup.getNumControls(); i++) {
} ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i));
ofParameter<bool>* paramPtr =
static_cast<ofParameter<bool>*>(&toggle->getParameter());
void RadioList::onToggleClicked(bool &toggleValue) if (&(paramPtr->get()) == &toggleValue) {
{ selectItem(i);
unselectAll(); break;
// 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());
if (&(paramPtr->get()) == &toggleValue) {
selectItem(i);
break;
}
} }
}
}
}
} }
}}

85
src/ui/RadioList.h

@ -5,46 +5,47 @@
#include "ofxToggle.h" #include "ofxToggle.h"
#include "ofxLabel.h" #include "ofxLabel.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
class RadioList class RadioList {
{ public:
public: RadioList();
RadioList(); RadioList(vector<string> &labels);
RadioList(vector<string> &labels); RadioList(string title, vector<string> &labels);
RadioList(string title, vector<string> &labels); ~RadioList();
~RadioList();
void setup(vector<string> &labels);
void setup(vector<string> &labels); void setup(string title, vector<string> &labels);
void setup(string title, vector<string> &labels); void draw();
void draw(); void setTitle(string title);
void setTitle(string title); void setPosition(ofPoint p);
void setPosition(ofPoint p); void setPosition(float x, float y);
void setPosition(float x, float y); void selectItem(int index);
void selectItem(int index); void enable();
void enable(); void disable();
void disable(); void clear();
void clear(); void unselectAll();
void unselectAll(); ofPoint getPosition();
ofPoint getPosition(); float getWidth();
float getWidth(); float getHeight();
float getHeight(); string getTitle();
string getTitle(); string getItemName(int index);
string getItemName(int index); int size();
int size();
// This event notifies about a toggle being selected and passes it's name to
// This event notifies about a toggle being selected and passes it's name to the listeners. // the listeners.
// Use ofAddListener(RadioListInstance.radioSelectedEvent, listenerClassPtr, &listenerClass::listenerMethod) // Use ofAddListener(RadioListInstance.radioSelectedEvent, listenerClassPtr,
// to listen to this. Listner method void listenerMethod(string & radioName) // &listenerClass::listenerMethod)
ofEvent<string> radioSelectedEvent; // to listen to this. Listner method void listenerMethod(string & radioName)
ofEvent<string> radioSelectedEvent;
private:
vector<string> storedLabels;
string storedTitle;
ofxGuiGroup guiGroup;
int storedSelectedItem;
void onToggleClicked(bool &toggleValue);
};
}} private:
vector<string> storedLabels;
string storedTitle;
ofxGuiGroup guiGroup;
int storedSelectedItem;
void onToggleClicked(bool &toggleValue);
};
}
}
Loading…
Cancel
Save