Browse Source

Refactoring: Renaming Classes, introducing namespace

master
Felix Dubrownik 11 years ago
parent
commit
66283175f1
  1. 14
      example/src/ofApp.cpp
  2. 11
      example/src/ofApp.h
  3. 49
      src/BaseJoint.cpp
  4. 90
      src/BaseJoint.h
  5. 21
      src/BaseSurface.cpp
  6. 11
      src/BaseSurface.h
  7. 19
      src/CircleJoint.cpp
  8. 15
      src/CircleJoint.h
  9. 9
      src/EditorType.h
  10. 10
      src/GuiMode.h
  11. 87
      src/ProjectionEditor.cpp
  12. 25
      src/ProjectionEditor.h
  13. 48
      src/QuadSurface.cpp
  14. 17
      src/QuadSurface.h
  15. 44
      src/SourcesEditor.cpp
  16. 23
      src/SourcesEditor.h
  17. 94
      src/SurfaceManager.cpp
  18. 35
      src/SurfaceManager.h
  19. 90
      src/SurfaceManagerGui.cpp
  20. 34
      src/SurfaceManagerGui.h
  21. 9
      src/SurfaceType.h
  22. 68
      src/TextureEditor.cpp
  23. 27
      src/TextureEditor.h
  24. 46
      src/TriangleSurface.cpp
  25. 18
      src/TriangleSurface.h
  26. 9
      src/ofxPiMapper.h
  27. 70
      src/ui/RadioList.cpp
  28. 18
      src/ui/RadioList.h

14
example/src/ofApp.cpp

@ -85,10 +85,10 @@ 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': gui.setMode(ofxGuiMode::NONE); break; case '1': gui.setMode(ofx::piMapper::GuiMode::NONE); break;
case '2': gui.setMode(ofxGuiMode::TEXTURE_MAPPING); break; case '2': gui.setMode(ofx::piMapper::GuiMode::TEXTURE_MAPPING); break;
case '3': gui.setMode(ofxGuiMode::PROJECTION_MAPPING); break; case '3': gui.setMode(ofx::piMapper::GuiMode::PROJECTION_MAPPING); break;
case '4': gui.setMode(ofxGuiMode::SOURCE_SELECTION); break; case '4': gui.setMode(ofx::piMapper::GuiMode::SOURCE_SELECTION); break;
case 'i': bShowInfo = !bShowInfo; break; case 'i': bShowInfo = !bShowInfo; break;
case 'r': addRandomSurface(); break; case 'r': addRandomSurface(); break;
case 'q': addQuadSurface(); break; case 'q': addQuadSurface(); break;
@ -103,7 +103,7 @@ void ofApp::keyPressed(int key)
void ofApp::addRandomSurface() void ofApp::addRandomSurface()
{ {
int surfaceType = ofxSurfaceType::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() ) );
@ -120,7 +120,7 @@ void ofApp::addRandomSurface()
void ofApp::addQuadSurface() void ofApp::addQuadSurface()
{ {
int surfaceType = ofxSurfaceType::QUAD_SURFACE; int surfaceType = ofx::piMapper::SurfaceType::QUAD_SURFACE;
vector<ofVec2f> vertices; vector<ofVec2f> vertices;
int border = 50; int border = 50;
@ -143,7 +143,7 @@ void ofApp::addQuadSurface()
void ofApp::addSurface() void ofApp::addSurface()
{ {
int surfaceType = ofxSurfaceType::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() ) );

11
example/src/ofApp.h

@ -1,5 +1,4 @@
#ifndef H_OF_APP #pragma once
#define H_OF_APP
#include "ofMain.h" #include "ofMain.h"
#include "ofxPiMapper.h" #include "ofxPiMapper.h"
@ -20,12 +19,10 @@ public:
void setFboAsTexture(); void setFboAsTexture();
ofImage image; ofImage image;
ofxSurfaceManager surfaceManager; ofx::piMapper::SurfaceManager surfaceManager;
ofxSurfaceManagerGui 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;
}; };
#endif

49
src/BaseJoint.cpp

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

90
src/BaseJoint.h

@ -1,48 +1,50 @@
#ifndef H_OFX_BASE_JOINT #pragma once
#define H_OFX_BASE_JOINT
#include "ofMain.h" #include "ofMain.h"
class ofxBaseJoint { namespace ofx{
public: namespace piMapper{
ofxBaseJoint();
~ofxBaseJoint();
void registerMouseEvents(); class BaseJoint {
void unregisterMouseEvents(); public:
BaseJoint();
ofVec2f position; ~BaseJoint();
bool enabled;
bool visible; void registerMouseEvents();
bool selected; void unregisterMouseEvents();
void mousePressed(ofMouseEventArgs& args); ofVec2f position;
void mouseReleased(int x, int y, int button); bool enabled;
void mouseDragged(ofMouseEventArgs& args); bool visible;
void startDrag(); bool selected;
void stopDrag();
void select(); void mousePressed(ofMouseEventArgs& args);
void unselect(); void mouseReleased(int x, int y, int button);
void setClickDistance(ofVec2f newClickDistance); void mouseDragged(ofMouseEventArgs& args);
bool isDragged(); void startDrag();
bool isSelected(); void stopDrag();
void select();
virtual void update(){}; void unselect();
virtual void draw(){}; void setClickDistance(ofVec2f newClickDistance);
virtual bool hitTest(ofVec2f position){}; bool isDragged();
bool isSelected();
protected:
ofColor fillColor; virtual void update(){};
ofColor strokeColor; virtual void draw(){};
ofColor fillColorSelected; virtual bool hitTest(ofVec2f position){};
ofColor strokeColorSelected;
float strokeWidth; protected:
ofVec2f clickDistance; // How far from the center of the joint the user has clicked? ofColor fillColor;
bool bDrag; ofColor strokeColor;
ofColor fillColorSelected;
private: ofColor strokeColorSelected;
void setDefaultColors(); float strokeWidth;
void setDefaultProperties(); ofVec2f clickDistance; // How far from the center of the joint the user has clicked?
}; bool bDrag;
#endif private:
void setDefaultColors();
void setDefaultProperties();
};
}
}

21
src/BaseSurface.cpp

@ -1,12 +1,15 @@
#include "ofxBaseSurface.h" #include "BaseSurface.h"
ofxBaseSurface::ofxBaseSurface() namespace ofx{
namespace piMapper{
BaseSurface::BaseSurface()
{ {
ofEnableNormalizedTexCoords(); ofEnableNormalizedTexCoords();
createDefaultTexture(); createDefaultTexture();
} }
void ofxBaseSurface::createDefaultTexture() void BaseSurface::createDefaultTexture()
{ {
ofPixels pixels; ofPixels pixels;
pixels.allocate(500, 500, 1); pixels.allocate(500, 500, 1);
@ -38,7 +41,7 @@ void ofxBaseSurface::createDefaultTexture()
texture = &defaultTexture; texture = &defaultTexture;
} }
void ofxBaseSurface::drawTexture(ofVec2f position) void BaseSurface::drawTexture(ofVec2f position)
{ {
ofMesh texMesh; ofMesh texMesh;
texMesh.addVertex(position); texMesh.addVertex(position);
@ -56,17 +59,19 @@ void ofxBaseSurface::drawTexture(ofVec2f position)
texture->unbind(); texture->unbind();
} }
void ofxBaseSurface::setTexture(ofTexture *texturePtr) void BaseSurface::setTexture(ofTexture *texturePtr)
{ {
texture = texturePtr; texture = texturePtr;
} }
ofTexture* ofxBaseSurface::getTexture() ofTexture* BaseSurface::getTexture()
{ {
return texture; return texture;
} }
ofTexture* ofxBaseSurface::getDefaultTexture() ofTexture* BaseSurface::getDefaultTexture()
{ {
return &defaultTexture; return &defaultTexture;
} }
}}

11
src/BaseSurface.h

@ -1,15 +1,16 @@
#ifndef H_OFX_BASE_SURFACE #pragma once
#define H_OFX_BASE_SURFACE
#include "ofMain.h" #include "ofMain.h"
#include <string> #include <string>
using namespace std; using namespace std;
class ofxBaseSurface namespace ofx{
namespace piMapper{
class BaseSurface
{ {
public: public:
ofxBaseSurface(); 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){};
@ -37,4 +38,4 @@ protected:
void createDefaultTexture(); void createDefaultTexture();
}; };
#endif }}

19
src/CircleJoint.cpp

@ -1,16 +1,19 @@
#include "ofxCircleJoint.h" #include "CircleJoint.h"
ofxCircleJoint::ofxCircleJoint() namespace ofx{
namespace piMapper{
CircleJoint::CircleJoint()
{ {
setDefaultProperties(); setDefaultProperties();
} }
void ofxCircleJoint::update() void CircleJoint::update()
{ {
if (!enabled) return; if (!enabled) return;
} }
void ofxCircleJoint::draw() void CircleJoint::draw()
{ {
if (!visible) return; if (!visible) return;
if (!enabled) return; if (!enabled) return;
@ -38,14 +41,16 @@ void ofxCircleJoint::draw()
ofPopStyle(); ofPopStyle();
} }
void ofxCircleJoint::setDefaultProperties() void CircleJoint::setDefaultProperties()
{ {
radius = 10.0f; radius = 10.0f;
} }
bool ofxCircleJoint::hitTest(ofVec2f pos) bool CircleJoint::hitTest(ofVec2f pos)
{ {
float distance = position.distance(pos); float distance = position.distance(pos);
if ( distance < radius ) return true; if ( distance < radius ) return true;
else return false; else return false;
} }
}}

15
src/CircleJoint.h

@ -1,13 +1,15 @@
#ifndef H_OFX_CIRCLE_JOINT #pragma once
#define H_OFX_CIRCLE_JOINT
#include "ofMain.h" #include "ofMain.h"
#include "ofxBaseJoint.h" #include "BaseJoint.h"
class ofxCircleJoint : public ofxBaseJoint
namespace ofx{
namespace piMapper{
class CircleJoint : public BaseJoint
{ {
public: public:
ofxCircleJoint(); CircleJoint();
void update(); void update();
void draw(); void draw();
@ -18,5 +20,4 @@ private:
void setDefaultProperties(); void setDefaultProperties();
}; };
}}
#endif

9
src/EditorType.h

@ -1,7 +1,8 @@
#ifndef H_OFX_EDITOR_TYPE #pragma once
#define H_OFX_EDITOR_TYPE
struct ofxEditorType namespace ofx{
namespace piMapper{
struct EditorType
{ {
enum { enum {
TEXTURE, TEXTURE,
@ -9,4 +10,4 @@ struct ofxEditorType
}; };
}; };
#endif }}

10
src/GuiMode.h

@ -1,7 +1,8 @@
#ifndef H_OFX_GUI_MODE #pragma once
#define H_OFX_GUI_MODE
struct ofxGuiMode namespace ofx{
namespace piMapper{
struct GuiMode
{ {
enum { enum {
NONE, NONE,
@ -10,5 +11,4 @@ struct ofxGuiMode
SOURCE_SELECTION SOURCE_SELECTION
}; };
}; };
}}
#endif

87
src/ProjectionEditor.cpp

@ -1,6 +1,8 @@
#include "ofxProjectionEditor.h" #include "ProjectionEditor.h"
ofxProjectionEditor::ofxProjectionEditor() namespace ofx{
namespace piMapper{
ProjectionEditor::ProjectionEditor()
{ {
surfaceManager = NULL; surfaceManager = NULL;
bShiftKeyDown = false; bShiftKeyDown = false;
@ -8,62 +10,62 @@ ofxProjectionEditor::ofxProjectionEditor()
enable(); enable();
} }
ofxProjectionEditor::~ofxProjectionEditor() ProjectionEditor::~ProjectionEditor()
{ {
clearJoints(); clearJoints();
surfaceManager = NULL; surfaceManager = NULL;
disable(); disable();
} }
void ofxProjectionEditor::registerAppEvents() void ProjectionEditor::registerAppEvents()
{ {
ofAddListener(ofEvents().update, this, &ofxProjectionEditor::update); ofAddListener(ofEvents().update, this, &ProjectionEditor::update);
ofAddListener(ofEvents().messageEvent, this, &ofxProjectionEditor::gotMessage); ofAddListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage);
} }
void ofxProjectionEditor::unregisterAppEvents() void ProjectionEditor::unregisterAppEvents()
{ {
ofRemoveListener(ofEvents().update, this, &ofxProjectionEditor::update); ofRemoveListener(ofEvents().update, this, &ProjectionEditor::update);
ofRemoveListener(ofEvents().messageEvent, this, &ofxProjectionEditor::gotMessage); ofRemoveListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage);
} }
void ofxProjectionEditor::registerMouseEvents() void ProjectionEditor::registerMouseEvents()
{ {
ofAddListener(ofEvents().mouseDragged, this, &ofxProjectionEditor::mouseDragged); ofAddListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged);
} }
void ofxProjectionEditor::unregisterMouseEvents() void ProjectionEditor::unregisterMouseEvents()
{ {
ofRemoveListener(ofEvents().mouseDragged, this, &ofxProjectionEditor::mouseDragged); ofRemoveListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged);
} }
void ofxProjectionEditor::registerKeyEvents() void ProjectionEditor::registerKeyEvents()
{ {
ofAddListener(ofEvents().keyPressed, this, &ofxProjectionEditor::keyPressed); ofAddListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed);
ofAddListener(ofEvents().keyReleased, this, &ofxProjectionEditor::keyReleased); ofAddListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased);
} }
void ofxProjectionEditor::unregisterKeyEvents() void ProjectionEditor::unregisterKeyEvents()
{ {
ofRemoveListener(ofEvents().keyPressed, this, &ofxProjectionEditor::keyPressed); ofRemoveListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed);
ofRemoveListener(ofEvents().keyReleased, this, &ofxProjectionEditor::keyReleased); ofRemoveListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased);
} }
void ofxProjectionEditor::enable() void ProjectionEditor::enable()
{ {
registerAppEvents(); registerAppEvents();
registerMouseEvents(); registerMouseEvents();
registerKeyEvents(); registerKeyEvents();
} }
void ofxProjectionEditor::disable() void ProjectionEditor::disable()
{ {
unregisterAppEvents(); unregisterAppEvents();
unregisterMouseEvents(); unregisterMouseEvents();
unregisterKeyEvents(); unregisterKeyEvents();
} }
void ofxProjectionEditor::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++ ) {
@ -83,7 +85,7 @@ void ofxProjectionEditor::update(ofEventArgs &args)
} }
} }
void ofxProjectionEditor::draw() void ProjectionEditor::draw()
{ {
if ( surfaceManager == NULL ) return; if ( surfaceManager == NULL ) return;
if ( surfaceManager->getSelectedSurface() == NULL ) return; if ( surfaceManager->getSelectedSurface() == NULL ) return;
@ -91,14 +93,14 @@ void ofxProjectionEditor::draw()
drawJoints(); drawJoints();
} }
void ofxProjectionEditor::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++ ) {
ofxBaseSurface* 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
} }
@ -125,7 +127,7 @@ void ofxProjectionEditor::mouseDragged(ofMouseEventArgs &args)
} }
} }
void ofxProjectionEditor::keyPressed(ofKeyEventArgs &args) void ProjectionEditor::keyPressed(ofKeyEventArgs &args)
{ {
int key = args.key; int key = args.key;
float moveStep; float moveStep;
@ -142,7 +144,7 @@ void ofxProjectionEditor::keyPressed(ofKeyEventArgs &args)
} }
} }
void ofxProjectionEditor::keyReleased(ofKeyEventArgs &args) void ProjectionEditor::keyReleased(ofKeyEventArgs &args)
{ {
int key = args.key; int key = args.key;
switch (key) { switch (key) {
@ -150,7 +152,7 @@ void ofxProjectionEditor::keyReleased(ofKeyEventArgs &args)
} }
} }
void ofxProjectionEditor::gotMessage(ofMessage& msg) void ProjectionEditor::gotMessage(ofMessage& msg)
{ {
if (msg.message == "surfaceSelected") { if (msg.message == "surfaceSelected") {
// refresh gui // refresh gui
@ -159,12 +161,12 @@ void ofxProjectionEditor::gotMessage(ofMessage& msg)
} }
} }
void ofxProjectionEditor::setSurfaceManager(ofxSurfaceManager *newSurfaceManager) void ProjectionEditor::setSurfaceManager(SurfaceManager *newSurfaceManager)
{ {
surfaceManager = newSurfaceManager; surfaceManager = newSurfaceManager;
} }
void ofxProjectionEditor::clearJoints() void ProjectionEditor::clearJoints()
{ {
while ( joints.size() ) { while ( joints.size() ) {
delete joints.back(); delete joints.back();
@ -172,7 +174,7 @@ void ofxProjectionEditor::clearJoints()
} }
} }
void ofxProjectionEditor::createJoints() void ProjectionEditor::createJoints()
{ {
if ( surfaceManager == NULL ) return; if ( surfaceManager == NULL ) return;
clearJoints(); clearJoints();
@ -185,12 +187,12 @@ void ofxProjectionEditor::createJoints()
vector<ofVec3f>& vertices = surfaceManager->getSelectedSurface()->getVertices(); vector<ofVec3f>& vertices = surfaceManager->getSelectedSurface()->getVertices();
for ( int i=0; i<vertices.size(); i++ ) { for ( int i=0; i<vertices.size(); i++ ) {
joints.push_back( new ofxCircleJoint() ); 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 ofxProjectionEditor::updateJoints() void ProjectionEditor::updateJoints()
{ {
vector<ofVec3f>& vertices = surfaceManager->getSelectedSurface()->getVertices(); vector<ofVec3f>& vertices = surfaceManager->getSelectedSurface()->getVertices();
for ( int i=0; i<vertices.size(); i++ ) { for ( int i=0; i<vertices.size(); i++ ) {
@ -198,14 +200,14 @@ void ofxProjectionEditor::updateJoints()
} }
} }
void ofxProjectionEditor::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 ofxProjectionEditor::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;
@ -217,18 +219,18 @@ void ofxProjectionEditor::moveSelectedSurface(ofVec2f by)
updateJoints(); updateJoints();
} }
void ofxProjectionEditor::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 ofxProjectionEditor::moveSelection(ofVec2f by) void ProjectionEditor::moveSelection(ofVec2f by)
{ {
// check if joints selected // check if joints selected
bool bJointSelected = false; bool bJointSelected = false;
ofxBaseJoint* 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;
@ -244,12 +246,12 @@ void ofxProjectionEditor::moveSelection(ofVec2f by)
} }
} }
void ofxProjectionEditor::setSnapDistance(float newSnapDistance) void ProjectionEditor::setSnapDistance(float newSnapDistance)
{ {
fSnapDistance = newSnapDistance; fSnapDistance = newSnapDistance;
} }
ofxCircleJoint* ofxProjectionEditor::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) ){
@ -259,9 +261,10 @@ ofxCircleJoint* ofxProjectionEditor::hitTestJoints(ofVec2f pos)
return NULL; return NULL;
} }
void ofxProjectionEditor::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();
} }
} }
}}

25
src/ProjectionEditor.h

@ -1,14 +1,15 @@
#ifndef H_OFX_PROJECTION_EDITOR #pragma once
#define H_OFX_PROJECTION_EDITOR
#include "ofxSurfaceManager.h" #include "SurfaceManager.h"
#include "ofxCircleJoint.h" #include "CircleJoint.h"
class ofxProjectionEditor namespace ofx{
namespace piMapper{
class ProjectionEditor
{ {
public: public:
ofxProjectionEditor(); ProjectionEditor();
~ofxProjectionEditor(); ~ProjectionEditor();
void registerAppEvents(); void registerAppEvents();
void unregisterAppEvents(); void unregisterAppEvents();
@ -26,7 +27,7 @@ public:
void keyPressed(ofKeyEventArgs& args); void keyPressed(ofKeyEventArgs& args);
void keyReleased(ofKeyEventArgs& args); void keyReleased(ofKeyEventArgs& args);
void gotMessage(ofMessage& msg); void gotMessage(ofMessage& msg);
void setSurfaceManager(ofxSurfaceManager* newSurfaceManager); void setSurfaceManager(SurfaceManager* newSurfaceManager);
void clearJoints(); void clearJoints();
void createJoints(); void createJoints();
void updateJoints(); void updateJoints();
@ -36,15 +37,15 @@ public:
void updateVertices(); void updateVertices();
void moveSelection(ofVec2f by); void moveSelection(ofVec2f by);
void setSnapDistance(float newSnapDistance); void setSnapDistance(float newSnapDistance);
ofxCircleJoint* hitTestJoints(ofVec2f pos); CircleJoint* hitTestJoints(ofVec2f pos);
private: private:
ofxSurfaceManager* surfaceManager; SurfaceManager* surfaceManager;
vector<ofxCircleJoint*> joints; vector<CircleJoint*> joints;
bool bShiftKeyDown; bool bShiftKeyDown;
float fSnapDistance; float fSnapDistance;
void drawJoints(); void drawJoints();
}; };
#endif }}

48
src/QuadSurface.cpp

@ -1,17 +1,19 @@
#include "ofxQuadSurface.h" #include "QuadSurface.h"
ofxQuadSurface::ofxQuadSurface() namespace ofx{
namespace piMapper{
QuadSurface::QuadSurface()
{ {
cout << "ofxQuadSurface constructor." << endl; cout << "QuadSurface constructor." << endl;
setup(); setup();
} }
ofxQuadSurface::~ofxQuadSurface() QuadSurface::~QuadSurface()
{ {
cout << "ofxQuadSurface destructor." << endl; cout << "QuadSurface destructor." << endl;
} }
void ofxQuadSurface::setup() void QuadSurface::setup()
{ {
// Create 4 points for the 2 triangles // Create 4 points for the 2 triangles
ofVec2f p1 = ofVec2f(0, 0); ofVec2f p1 = ofVec2f(0, 0);
@ -28,7 +30,7 @@ void ofxQuadSurface::setup()
setup( p1, p2, p3, p4, t1, t2, t3, t4, texture ); setup( p1, p2, p3, p4, t1, t2, t3, t4, texture );
} }
void ofxQuadSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4, void QuadSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4,
ofVec2f t1, ofVec2f t2, ofVec2f t3, ofVec2f t4, ofTexture* texturePtr ) ofVec2f t1, ofVec2f t2, ofVec2f t3, ofVec2f t4, ofTexture* texturePtr )
{ {
// Assign texture // Assign texture
@ -70,7 +72,7 @@ void ofxQuadSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4,
calculate4dTextureCoords(); calculate4dTextureCoords();
} }
void ofxQuadSurface::draw() void QuadSurface::draw()
{ {
/*if(mesh.haveVertsChanged() || mesh.haveTexCoordsChanged()){ /*if(mesh.haveVertsChanged() || mesh.haveTexCoordsChanged()){
calculate4dTextureCoords(); calculate4dTextureCoords();
@ -84,7 +86,7 @@ void ofxQuadSurface::draw()
texture->unbind(); texture->unbind();
} }
void ofxQuadSurface::setVertex(int index, ofVec2f p) void QuadSurface::setVertex(int index, ofVec2f p)
{ {
if ( index > 3 ) { if ( index > 3 ) {
ofLog() << "Vertex with this index does not exist: " << index << endl; ofLog() << "Vertex with this index does not exist: " << index << endl;
@ -95,7 +97,7 @@ void ofxQuadSurface::setVertex(int index, ofVec2f p)
calculate4dTextureCoords(); calculate4dTextureCoords();
} }
void ofxQuadSurface::setTexCoord(int index, ofVec2f t) void QuadSurface::setTexCoord(int index, ofVec2f t)
{ {
if ( index > 3 ) { if ( index > 3 ) {
ofLog() << "Texture coordinate with this index does not exist: " << index << endl; ofLog() << "Texture coordinate with this index does not exist: " << index << endl;
@ -106,7 +108,7 @@ void ofxQuadSurface::setTexCoord(int index, ofVec2f t)
calculate4dTextureCoords(); calculate4dTextureCoords();
} }
void ofxQuadSurface::moveBy(ofVec2f v) void QuadSurface::moveBy(ofVec2f v)
{ {
vector<ofVec3f>& vertices = getVertices(); vector<ofVec3f>& vertices = getVertices();
for (int i=0; i<vertices.size(); i++) { for (int i=0; i<vertices.size(); i++) {
@ -115,12 +117,12 @@ void ofxQuadSurface::moveBy(ofVec2f v)
calculate4dTextureCoords(); calculate4dTextureCoords();
} }
int ofxQuadSurface::getType() int QuadSurface::getType()
{ {
return ofxSurfaceType::QUAD_SURFACE; return SurfaceType::QUAD_SURFACE;
} }
bool ofxQuadSurface::hitTest(ofVec2f p) bool QuadSurface::hitTest(ofVec2f p)
{ {
// Construct ofPolyline from vertices // Construct ofPolyline from vertices
ofPolyline line = getHitArea(); ofPolyline line = getHitArea();
@ -132,7 +134,7 @@ bool ofxQuadSurface::hitTest(ofVec2f p)
} }
} }
ofVec2f ofxQuadSurface::getVertex(int index) ofVec2f QuadSurface::getVertex(int index)
{ {
if ( index > 3 ) { if ( index > 3 ) {
ofLog() << "Vertex with this index does not exist: " << index << endl; ofLog() << "Vertex with this index does not exist: " << index << endl;
@ -143,7 +145,7 @@ ofVec2f ofxQuadSurface::getVertex(int index)
return ofVec2f(vert.x, vert.y); return ofVec2f(vert.x, vert.y);
} }
ofVec2f ofxQuadSurface::getTexCoord(int index) ofVec2f QuadSurface::getTexCoord(int index)
{ {
if (index > 3) { if (index > 3) {
throw std::runtime_error("Texture coordinate index out of bounds."); throw std::runtime_error("Texture coordinate index out of bounds.");
@ -152,7 +154,7 @@ ofVec2f ofxQuadSurface::getTexCoord(int index)
return mesh.getTexCoord(index); return mesh.getTexCoord(index);
} }
ofPolyline ofxQuadSurface::getHitArea() ofPolyline QuadSurface::getHitArea()
{ {
ofPolyline line; ofPolyline line;
line.addVertex( ofPoint( mesh.getVertex(0).x, mesh.getVertex(0).y ) ); line.addVertex( ofPoint( mesh.getVertex(0).x, mesh.getVertex(0).y ) );
@ -164,7 +166,7 @@ ofPolyline ofxQuadSurface::getHitArea()
return line; return line;
} }
ofPolyline ofxQuadSurface::getTextureHitArea() ofPolyline QuadSurface::getTextureHitArea()
{ {
ofPolyline line; ofPolyline line;
vector<ofVec2f>& texCoords = mesh.getTexCoords(); vector<ofVec2f>& texCoords = mesh.getTexCoords();
@ -177,19 +179,19 @@ ofPolyline ofxQuadSurface::getTextureHitArea()
return line; return line;
} }
vector<ofVec3f>& ofxQuadSurface::getVertices() vector<ofVec3f>& QuadSurface::getVertices()
{ {
// return only joint vertices // return only joint vertices
return mesh.getVertices(); return mesh.getVertices();
} }
vector<ofVec2f>& ofxQuadSurface::getTexCoords() vector<ofVec2f>& QuadSurface::getTexCoords()
{ {
return mesh.getTexCoords(); return mesh.getTexCoords();
} }
void ofxQuadSurface::calculate4dTextureCoords() void QuadSurface::calculate4dTextureCoords()
{ {
// Perspective Warping with OpenGL Fixed Pipeline and q coordinates // Perspective Warping with OpenGL Fixed Pipeline and q coordinates
// see: // see:
@ -259,4 +261,6 @@ void ofxQuadSurface::calculate4dTextureCoords()
quadTexCoordinates[13] = t3.y * q3; quadTexCoordinates[13] = t3.y * q3;
quadTexCoordinates[15] = q3; quadTexCoordinates[15] = q3;
} }
}}

17
src/QuadSurface.h

@ -1,15 +1,16 @@
#ifndef H_OFX_QUAD_SURFACE #pragma once
#define H_OFX_QUAD_SURFACE
#include "ofMain.h" #include "ofMain.h"
#include "ofxBaseSurface.h" #include "BaseSurface.h"
#include "ofxSurfaceType.h" #include "SurfaceType.h"
class ofxQuadSurface : public ofxBaseSurface namespace ofx{
namespace piMapper{
class QuadSurface : public BaseSurface
{ {
public: public:
ofxQuadSurface(); QuadSurface();
~ofxQuadSurface(); ~QuadSurface();
void setup(); void setup();
@ -37,4 +38,4 @@ private:
GLfloat quadTexCoordinates[16]; GLfloat quadTexCoordinates[16];
}; };
#endif }}

44
src/SourcesEditor.cpp

@ -1,12 +1,14 @@
#include "ofxSourcesEditor.h" #include "SourcesEditor.h"
ofxSourcesEditor::ofxSourcesEditor() namespace ofx{
namespace piMapper{
SourcesEditor::SourcesEditor()
{ {
defImgDir = DEFAULT_IMAGES_DIR; defImgDir = DEFAULT_IMAGES_DIR;
registerAppEvents(); registerAppEvents();
} }
ofxSourcesEditor::~ofxSourcesEditor() SourcesEditor::~SourcesEditor()
{ {
unregisterAppEvents(); unregisterAppEvents();
delete gui; delete gui;
@ -16,19 +18,19 @@ ofxSourcesEditor::~ofxSourcesEditor()
} }
} }
void ofxSourcesEditor::registerAppEvents() void SourcesEditor::registerAppEvents()
{ {
ofAddListener(ofEvents().setup, this, &ofxSourcesEditor::setup); ofAddListener(ofEvents().setup, this, &SourcesEditor::setup);
} }
void ofxSourcesEditor::unregisterAppEvents() void SourcesEditor::unregisterAppEvents()
{ {
ofRemoveListener(ofEvents().setup, this, &ofxSourcesEditor::setup); ofRemoveListener(ofEvents().setup, this, &SourcesEditor::setup);
} }
void ofxSourcesEditor::setup(ofEventArgs& args) void SourcesEditor::setup(ofEventArgs& args)
{ {
gui = new ofxRadioList(); gui = new RadioList();
// read directory contents // read directory contents
ofDirectory imgDir; ofDirectory imgDir;
@ -44,10 +46,10 @@ void ofxSourcesEditor::setup(ofEventArgs& args)
gui->setup("Images", vnames); gui->setup("Images", vnames);
gui->setPosition(20, 20); gui->setPosition(20, 20);
ofAddListener(gui->radioSelectedEvent, this, &ofxSourcesEditor::guiEvent); ofAddListener(gui->radioSelectedEvent, this, &SourcesEditor::guiEvent);
} }
void ofxSourcesEditor::draw() void SourcesEditor::draw()
{ {
// Don't draw if there is no source selected // Don't draw if there is no source selected
if ( surfaceManager->getSelectedSurface() == NULL ) { if ( surfaceManager->getSelectedSurface() == NULL ) {
@ -57,7 +59,7 @@ void ofxSourcesEditor::draw()
gui->draw(); gui->draw();
} }
void ofxSourcesEditor::loadImage( string name, string path ) void SourcesEditor::loadImage( string name, string path )
{ {
images.push_back(new ofImage()); images.push_back(new ofImage());
images.back()->loadImage(path); images.back()->loadImage(path);
@ -67,12 +69,12 @@ void ofxSourcesEditor::loadImage( string name, string path )
ofSendMessage("imageLoaded"); ofSendMessage("imageLoaded");
} }
void ofxSourcesEditor::disable() void SourcesEditor::disable()
{ {
gui->disable(); gui->disable();
} }
void ofxSourcesEditor::enable() void SourcesEditor::enable()
{ {
// Don't enable if there is no surface selected // Don't enable if there is no surface selected
if ( surfaceManager->getSelectedSurface() == NULL ) { if ( surfaceManager->getSelectedSurface() == NULL ) {
@ -83,12 +85,12 @@ void ofxSourcesEditor::enable()
gui->enable(); gui->enable();
} }
void ofxSourcesEditor::setSurfaceManager(ofxSurfaceManager *newSurfaceManager) void SourcesEditor::setSurfaceManager(SurfaceManager *newSurfaceManager)
{ {
surfaceManager = newSurfaceManager; surfaceManager = newSurfaceManager;
} }
void ofxSourcesEditor::selectImageSourceRadioButton(string name) void SourcesEditor::selectImageSourceRadioButton(string name)
{ {
if (name == "none") { if (name == "none") {
gui->unselectAll(); gui->unselectAll();
@ -104,12 +106,12 @@ void ofxSourcesEditor::selectImageSourceRadioButton(string name)
} }
} }
int ofxSourcesEditor::getLoadedTexCount() int SourcesEditor::getLoadedTexCount()
{ {
return images.size(); return images.size();
} }
ofTexture* ofxSourcesEditor::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.");
@ -118,7 +120,7 @@ ofTexture* ofxSourcesEditor::getTexture(int index)
return &images[index]->getTextureReference(); return &images[index]->getTextureReference();
} }
void ofxSourcesEditor::guiEvent(string &imageName) void SourcesEditor::guiEvent(string &imageName)
{ {
string name = imageName; string name = imageName;
@ -132,4 +134,6 @@ void ofxSourcesEditor::guiEvent(string &imageName)
ofTexture* texture = surfaceManager->loadImageSource(name, ss.str()); ofTexture* texture = surfaceManager->loadImageSource(name, ss.str());
surfaceManager->getSelectedSurface()->setTexture(texture); surfaceManager->getSelectedSurface()->setTexture(texture);
surfaceManager->manageMemory(); surfaceManager->manageMemory();
} }
}}

23
src/SourcesEditor.h

@ -1,18 +1,19 @@
#ifndef H_OFX_SOURCES_EDITOR #pragma once
#define H_OFX_SOURCES_EDITOR
#include "ofGraphics.h" #include "ofGraphics.h"
#include "ofEvents.h" #include "ofEvents.h"
#include "ofxSurfaceManager.h" #include "SurfaceManager.h"
#include "ofxRadioList.h" #include "RadioList.h"
#define DEFAULT_IMAGES_DIR "sources/images/"; #define DEFAULT_IMAGES_DIR "sources/images/";
class ofxSourcesEditor namespace ofx{
namespace piMapper{
class SourcesEditor
{ {
public: public:
ofxSourcesEditor(); SourcesEditor();
~ofxSourcesEditor(); ~SourcesEditor();
void registerAppEvents(); void registerAppEvents();
void unregisterAppEvents(); void unregisterAppEvents();
@ -22,19 +23,19 @@ public:
void loadImage( string name, string path ); void loadImage( string name, string path );
void disable(); void disable();
void enable(); void enable();
void setSurfaceManager(ofxSurfaceManager* newSurfaceManager); void setSurfaceManager(SurfaceManager* newSurfaceManager);
void selectImageSourceRadioButton(string name); void selectImageSourceRadioButton(string name);
int getLoadedTexCount(); int getLoadedTexCount();
ofTexture* getTexture(int index); ofTexture* getTexture(int index);
private: private:
ofxSurfaceManager* surfaceManager; SurfaceManager* surfaceManager;
ofxRadioList* gui; RadioList* gui;
string defImgDir; string defImgDir;
void guiEvent(string &imageName); void guiEvent(string &imageName);
vector<ofImage*> images; vector<ofImage*> images;
vector<string> imageNames; vector<string> imageNames;
}; };
#endif }}

94
src/SurfaceManager.cpp

@ -1,43 +1,45 @@
#include "ofxSurfaceManager.h" #include "SurfaceManager.h"
ofxSurfaceManager::ofxSurfaceManager() namespace ofx{
namespace piMapper{
SurfaceManager::SurfaceManager()
{ {
} }
ofxSurfaceManager::~ofxSurfaceManager() SurfaceManager::~SurfaceManager()
{ {
clear(); clear();
} }
void ofxSurfaceManager::draw() void SurfaceManager::draw()
{ {
for ( int i=0; i<surfaces.size(); i++ ) { for ( int i=0; i<surfaces.size(); i++ ) {
surfaces[i]->draw(); surfaces[i]->draw();
} }
} }
void ofxSurfaceManager::addSurface(int surfaceType) void SurfaceManager::addSurface(int surfaceType)
{ {
if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) { if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) {
surfaces.push_back( new ofxTriangleSurface() ); surfaces.push_back( new TriangleSurface() );
} }
else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) { else if (surfaceType == SurfaceType::QUAD_SURFACE ) {
surfaces.push_back( new ofxQuadSurface() ); surfaces.push_back( new QuadSurface() );
} }
else { else {
throw std::runtime_error("Attempt to add non-existing surface type."); throw std::runtime_error("Attempt to add non-existing surface type.");
} }
} }
void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr) void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr)
{ {
if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) { if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) {
surfaces.push_back( new ofxTriangleSurface() ); surfaces.push_back( new TriangleSurface() );
surfaces.back()->setTexture(texturePtr); surfaces.back()->setTexture(texturePtr);
} }
else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) { else if (surfaceType == SurfaceType::QUAD_SURFACE ) {
surfaces.push_back( new ofxQuadSurface() ); surfaces.push_back( new QuadSurface() );
surfaces.back()->setTexture(texturePtr); surfaces.back()->setTexture(texturePtr);
} }
else { else {
@ -45,9 +47,9 @@ void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr)
} }
} }
void ofxSurfaceManager::addSurface(int surfaceType, vector<ofVec2f> vertices, vector<ofVec2f> texCoords) void SurfaceManager::addSurface(int surfaceType, vector<ofVec2f> vertices, vector<ofVec2f> texCoords)
{ {
if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) { 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("There must be 3 vertices for a triangle surface.");
@ -55,7 +57,7 @@ void ofxSurfaceManager::addSurface(int surfaceType, vector<ofVec2f> vertices, ve
throw std::runtime_error("There must be 3 texture coordinates for a triangle surface."); throw std::runtime_error("There must be 3 texture coordinates for a triangle surface.");
} }
surfaces.push_back( new ofxTriangleSurface() ); surfaces.push_back( new TriangleSurface() );
for ( int i=0; i<3; i++ ) { for ( int i=0; i<3; i++ ) {
surfaces.back()->setVertex(i, vertices[i]); surfaces.back()->setVertex(i, vertices[i]);
@ -63,14 +65,14 @@ void ofxSurfaceManager::addSurface(int surfaceType, vector<ofVec2f> vertices, ve
} }
} }
else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) { else if (surfaceType == SurfaceType::QUAD_SURFACE ) {
if ( vertices.size() < 4 ) { if ( vertices.size() < 4 ) {
throw std::runtime_error("There must be 4 vertices for a quad surface."); throw std::runtime_error("There must be 4 vertices for a quad surface.");
} else if (texCoords.size() < 4) { } else if (texCoords.size() < 4) {
throw std::runtime_error("There must be 4 texture coordinates for a quad surface."); throw std::runtime_error("There must be 4 texture coordinates for a quad surface.");
} }
surfaces.push_back( new ofxQuadSurface() ); surfaces.push_back( new QuadSurface() );
for ( int i=0; i<4; i++ ) { for ( int i=0; i<4; i++ ) {
surfaces.back()->setVertex(i, vertices[i]); surfaces.back()->setVertex(i, vertices[i]);
@ -83,9 +85,9 @@ void ofxSurfaceManager::addSurface(int surfaceType, vector<ofVec2f> vertices, ve
} }
void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vector<ofVec2f> vertices, vector<ofVec2f> texCoords) void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vector<ofVec2f> vertices, vector<ofVec2f> texCoords)
{ {
if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) { 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("There must be 3 vertices for a triangle surface.");
@ -93,7 +95,7 @@ void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vecto
throw std::runtime_error("Thre must be 3 texture coordinates for a triangle surface."); throw std::runtime_error("Thre must be 3 texture coordinates for a triangle surface.");
} }
surfaces.push_back( new ofxTriangleSurface() ); surfaces.push_back( new TriangleSurface() );
surfaces.back()->setTexture(texturePtr); surfaces.back()->setTexture(texturePtr);
for ( int i=0; i<3; i++ ) { for ( int i=0; i<3; i++ ) {
@ -102,14 +104,14 @@ void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vecto
} }
} }
else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) { else if (surfaceType == SurfaceType::QUAD_SURFACE ) {
if ( vertices.size() < 4 ) { if ( vertices.size() < 4 ) {
throw std::runtime_error("There must be 4 vertices for a quad surface."); throw std::runtime_error("There must be 4 vertices for a quad surface.");
} else if (texCoords.size() < 4) { } else if (texCoords.size() < 4) {
throw std::runtime_error("Thre must be 4 texture coordinates for a quad surface."); throw std::runtime_error("Thre must be 4 texture coordinates for a quad surface.");
} }
surfaces.push_back( new ofxQuadSurface() ); surfaces.push_back( new QuadSurface() );
surfaces.back()->setTexture(texturePtr); surfaces.back()->setTexture(texturePtr);
for ( int i=0; i<4; i++ ) { for ( int i=0; i<4; i++ ) {
@ -122,7 +124,7 @@ void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vecto
} }
} }
void ofxSurfaceManager::removeSelectedSurface() void SurfaceManager::removeSelectedSurface()
{ {
if ( selectedSurface == NULL ) return; if ( selectedSurface == NULL ) return;
@ -136,7 +138,7 @@ void ofxSurfaceManager::removeSelectedSurface()
} }
} }
void ofxSurfaceManager::manageMemory() void SurfaceManager::manageMemory()
{ {
// check if each of the sources is assigned to a surface or not // check if each of the sources is assigned to a surface or not
for ( int i=0; i<loadedImageSources.size(); i++ ) { for ( int i=0; i<loadedImageSources.size(); i++ ) {
@ -160,7 +162,7 @@ void ofxSurfaceManager::manageMemory()
} }
} }
void ofxSurfaceManager::clear() void SurfaceManager::clear()
{ {
// delete all extra allocations from the heap // delete all extra allocations from the heap
while ( surfaces.size() ) { while ( surfaces.size() ) {
@ -178,7 +180,7 @@ void ofxSurfaceManager::clear()
} }
} }
// String getTypeString(ofxSurfaceType e) // String getTypeString(SurfaceType e)
// { // {
// switch e // switch e
// { // {
@ -188,7 +190,7 @@ void ofxSurfaceManager::clear()
// } // }
// } // }
void ofxSurfaceManager::saveXmlSettings(string fileName) void SurfaceManager::saveXmlSettings(string fileName)
{ {
xmlSettings.clear(); xmlSettings.clear();
@ -199,7 +201,7 @@ void ofxSurfaceManager::saveXmlSettings(string fileName)
xmlSettings.addTag("surface"); xmlSettings.addTag("surface");
xmlSettings.pushTag("surface", i); xmlSettings.pushTag("surface", i);
ofxBaseSurface* surface = surfaces[i]; BaseSurface* surface = surfaces[i];
xmlSettings.addTag("vertices"); xmlSettings.addTag("vertices");
xmlSettings.pushTag("vertices"); xmlSettings.pushTag("vertices");
@ -241,8 +243,8 @@ void ofxSurfaceManager::saveXmlSettings(string fileName)
// xmlSettings.addTag("type"); // xmlSettings.addTag("type");
// xmlSettings.pushTag("type"); // xmlSettings.pushTag("type");
// // surfaceType == ofxSurfaceType::TRIANGLE_SURFACE // // surfaceType == SurfaceType::TRIANGLE_SURFACE
// ofxSurfaceType surfaceType = &surface->getType(); // SurfaceType surfaceType = &surface->getType();
// xmlSettings.addValue("surface-type", surfaceType); // xmlSettings.addValue("surface-type", surfaceType);
// xmlSettings.popTag(); // type // xmlSettings.popTag(); // type
@ -253,7 +255,7 @@ void ofxSurfaceManager::saveXmlSettings(string fileName)
xmlSettings.save(fileName); xmlSettings.save(fileName);
} }
void ofxSurfaceManager::loadXmlSettings(string fileName) void SurfaceManager::loadXmlSettings(string fileName)
{ {
@ -337,9 +339,9 @@ void ofxSurfaceManager::loadXmlSettings(string fileName)
// now we have variables sourceName and sourceTexture // now we have variables sourceName and sourceTexture
// by checking those we can use one or another addSurface method // by checking those we can use one or another addSurface method
if ( sourceName != "none" && sourceTexture != NULL ) { if ( sourceName != "none" && sourceTexture != NULL ) {
addSurface(ofxSurfaceType::TRIANGLE_SURFACE, sourceTexture, vertices, texCoords); addSurface(SurfaceType::TRIANGLE_SURFACE, sourceTexture, vertices, texCoords);
} else { } else {
addSurface(ofxSurfaceType::TRIANGLE_SURFACE, vertices, texCoords); addSurface(SurfaceType::TRIANGLE_SURFACE, vertices, texCoords);
} }
} }
// it's a quad ? // it's a quad ?
@ -390,9 +392,9 @@ void ofxSurfaceManager::loadXmlSettings(string fileName)
// now we have variables sourceName and sourceTexture // now we have variables sourceName and sourceTexture
// by checking those we can use one or another addSurface method // by checking those we can use one or another addSurface method
if ( sourceName != "none" && sourceTexture != NULL ) { if ( sourceName != "none" && sourceTexture != NULL ) {
addSurface(ofxSurfaceType::QUAD_SURFACE, sourceTexture, vertices, texCoords); addSurface(SurfaceType::QUAD_SURFACE, sourceTexture, vertices, texCoords);
} else { } else {
addSurface(ofxSurfaceType::QUAD_SURFACE, vertices, texCoords); addSurface(SurfaceType::QUAD_SURFACE, vertices, texCoords);
} }
@ -404,7 +406,7 @@ void ofxSurfaceManager::loadXmlSettings(string fileName)
xmlSettings.popTag(); // surfaces xmlSettings.popTag(); // surfaces
} }
ofxBaseSurface* ofxSurfaceManager::selectSurface(int index) BaseSurface* SurfaceManager::selectSurface(int index)
{ {
if ( index >= surfaces.size() ) { if ( index >= surfaces.size() ) {
throw std::runtime_error("Surface index out of bounds."); throw std::runtime_error("Surface index out of bounds.");
@ -416,17 +418,17 @@ ofxBaseSurface* ofxSurfaceManager::selectSurface(int index)
ofSendMessage("surfaceSelected"); ofSendMessage("surfaceSelected");
} }
ofxBaseSurface* ofxSurfaceManager::getSelectedSurface() BaseSurface* SurfaceManager::getSelectedSurface()
{ {
return selectedSurface; return selectedSurface;
} }
void ofxSurfaceManager::deselectSurface() void SurfaceManager::deselectSurface()
{ {
selectedSurface = NULL; selectedSurface = NULL;
} }
ofTexture* ofxSurfaceManager::loadImageSource(string name, string path) ofTexture* SurfaceManager::loadImageSource(string name, string path)
{ {
// check if it is loaded // check if it is loaded
for ( int i=0; i<loadedImageSourceNames.size(); i++ ) { for ( int i=0; i<loadedImageSourceNames.size(); i++ ) {
@ -446,7 +448,7 @@ ofTexture* ofxSurfaceManager::loadImageSource(string name, string path)
return &image->getTextureReference(); return &image->getTextureReference();
} }
string ofxSurfaceManager::getSelectedSurfaceSourceName() string SurfaceManager::getSelectedSurfaceSourceName()
{ {
if ( selectedSurface == NULL ) { if ( selectedSurface == NULL ) {
return "none"; return "none";
@ -455,7 +457,7 @@ string ofxSurfaceManager::getSelectedSurfaceSourceName()
return getSurfaceSourceName( selectedSurface ); return getSurfaceSourceName( selectedSurface );
} }
string ofxSurfaceManager::getSurfaceSourceName(ofxBaseSurface *surface) string SurfaceManager::getSurfaceSourceName(BaseSurface *surface)
{ {
ofTexture* tex = surface->getTexture(); ofTexture* tex = surface->getTexture();
for ( int i=0; i<loadedImageSources.size(); i++ ) { for ( int i=0; i<loadedImageSources.size(); i++ ) {
@ -467,7 +469,7 @@ string ofxSurfaceManager::getSurfaceSourceName(ofxBaseSurface *surface)
return "none"; return "none";
} }
ofxBaseSurface* ofxSurfaceManager::getSurface(int index) BaseSurface* SurfaceManager::getSurface(int index)
{ {
if ( index >= surfaces.size() ) { if ( index >= surfaces.size() ) {
throw std::runtime_error("Surface index out of bounds."); throw std::runtime_error("Surface index out of bounds.");
@ -477,10 +479,10 @@ ofxBaseSurface* ofxSurfaceManager::getSurface(int index)
return surfaces[index]; return surfaces[index];
} }
int ofxSurfaceManager::size() int SurfaceManager::size()
{ {
return surfaces.size(); return surfaces.size();
} }
}}

35
src/SurfaceManager.h

@ -1,20 +1,23 @@
#ifndef H_OFX_SURFACE_MANAGER #pragma once
#define H_OFX_SURFACE_MANAGER
#include "BaseSurface.h"
#include "TriangleSurface.h"
#include "QuadSurface.h"
#include "SurfaceType.h"
#include "ofxBaseSurface.h"
#include "ofxTriangleSurface.h"
#include "ofxQuadSurface.h"
#include "ofxSurfaceType.h"
#include "ofEvents.h" #include "ofEvents.h"
#include "ofxXmlSettings.h" #include "ofxXmlSettings.h"
using namespace std; using namespace std;
class ofxSurfaceManager
namespace ofx{
namespace piMapper{
class SurfaceManager
{ {
public: public:
ofxSurfaceManager(); SurfaceManager();
~ofxSurfaceManager(); ~SurfaceManager();
void draw(); void draw();
void addSurface(int surfaceType); void addSurface(int surfaceType);
@ -27,22 +30,22 @@ public:
void saveXmlSettings(string fileName); void saveXmlSettings(string fileName);
void loadXmlSettings(string fileName); void loadXmlSettings(string fileName);
ofxBaseSurface* getSurface(int index); BaseSurface* getSurface(int index);
int size(); int size();
ofxBaseSurface* selectSurface(int index); BaseSurface* selectSurface(int index);
ofxBaseSurface* getSelectedSurface(); BaseSurface* getSelectedSurface();
void deselectSurface(); void deselectSurface();
ofTexture* loadImageSource(string name, string path); ofTexture* loadImageSource(string name, string path);
string getSelectedSurfaceSourceName(); string getSelectedSurfaceSourceName();
string getSurfaceSourceName( ofxBaseSurface* surface ); string getSurfaceSourceName( BaseSurface* surface );
private: private:
vector<ofxBaseSurface*> surfaces; vector<BaseSurface*> surfaces;
ofxBaseSurface* selectedSurface; BaseSurface* selectedSurface;
vector<string> loadedImageSourceNames; vector<string> loadedImageSourceNames;
vector<ofImage*> loadedImageSources; vector<ofImage*> loadedImageSources;
ofxXmlSettings xmlSettings; ofxXmlSettings xmlSettings;
}; };
#endif }}

90
src/SurfaceManagerGui.cpp

@ -1,41 +1,43 @@
#include "ofxSurfaceManagerGui.h" #include "SurfaceManagerGui.h"
ofxSurfaceManagerGui::ofxSurfaceManagerGui() namespace ofx{
namespace piMapper{
SurfaceManagerGui::SurfaceManagerGui()
{ {
surfaceManager = NULL; surfaceManager = NULL;
guiMode = ofxGuiMode::NONE; guiMode = GuiMode::NONE;
bDrag = false; bDrag = false;
registerMouseEvents(); registerMouseEvents();
ofHideCursor(); ofHideCursor();
} }
ofxSurfaceManagerGui::~ofxSurfaceManagerGui() SurfaceManagerGui::~SurfaceManagerGui()
{ {
unregisterMouseEvents(); unregisterMouseEvents();
surfaceManager = NULL; surfaceManager = NULL;
} }
void ofxSurfaceManagerGui::registerMouseEvents() void SurfaceManagerGui::registerMouseEvents()
{ {
ofAddListener(ofEvents().mousePressed, this, &ofxSurfaceManagerGui::mousePressed); ofAddListener(ofEvents().mousePressed, this, &SurfaceManagerGui::mousePressed);
ofAddListener(ofEvents().mouseReleased, this, &ofxSurfaceManagerGui::mouseReleased); ofAddListener(ofEvents().mouseReleased, this, &SurfaceManagerGui::mouseReleased);
ofAddListener(ofEvents().mouseDragged, this, &ofxSurfaceManagerGui::mouseDragged); ofAddListener(ofEvents().mouseDragged, this, &SurfaceManagerGui::mouseDragged);
} }
void ofxSurfaceManagerGui::unregisterMouseEvents() void SurfaceManagerGui::unregisterMouseEvents()
{ {
ofRemoveListener(ofEvents().mousePressed, this, &ofxSurfaceManagerGui::mousePressed); ofRemoveListener(ofEvents().mousePressed, this, &SurfaceManagerGui::mousePressed);
ofRemoveListener(ofEvents().mouseReleased, this, &ofxSurfaceManagerGui::mouseReleased); ofRemoveListener(ofEvents().mouseReleased, this, &SurfaceManagerGui::mouseReleased);
ofRemoveListener(ofEvents().mouseDragged, this, &ofxSurfaceManagerGui::mouseDragged); ofRemoveListener(ofEvents().mouseDragged, this, &SurfaceManagerGui::mouseDragged);
} }
void ofxSurfaceManagerGui::draw() void SurfaceManagerGui::draw()
{ {
if ( surfaceManager == NULL ) return; if ( surfaceManager == NULL ) return;
if ( guiMode == ofxGuiMode::NONE ) { if ( guiMode == GuiMode::NONE ) {
surfaceManager->draw(); surfaceManager->draw();
} else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { } else if ( guiMode == GuiMode::TEXTURE_MAPPING ) {
// draw the texture of the selected surface // draw the texture of the selected surface
if ( surfaceManager->getSelectedSurface() != NULL ) { if ( surfaceManager->getSelectedSurface() != NULL ) {
@ -57,7 +59,7 @@ void ofxSurfaceManagerGui::draw()
// draw texture editing GUI on top // draw texture editing GUI on top
textureEditor.draw(); textureEditor.draw();
} else if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) { } else if ( guiMode == GuiMode::PROJECTION_MAPPING ) {
// draw projection surfaces first // draw projection surfaces first
surfaceManager->draw(); surfaceManager->draw();
@ -68,7 +70,7 @@ void ofxSurfaceManagerGui::draw()
// draw projection mapping editing gui // draw projection mapping editing gui
projectionEditor.draw(); projectionEditor.draw();
} else if ( guiMode == ofxGuiMode::SOURCE_SELECTION ) { } else if ( guiMode == GuiMode::SOURCE_SELECTION ) {
// draw projection surfaces first // draw projection surfaces first
surfaceManager->draw(); surfaceManager->draw();
@ -79,15 +81,15 @@ void ofxSurfaceManagerGui::draw()
} }
} }
void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args) void SurfaceManagerGui::mousePressed(ofMouseEventArgs &args)
{ {
if ( guiMode == ofxGuiMode::NONE ) { if ( guiMode == GuiMode::NONE ) {
return; return;
} else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { } else if ( guiMode == GuiMode::TEXTURE_MAPPING ) {
bool bSurfaceSelected = false; bool bSurfaceSelected = false;
ofxCircleJoint* hitJoint = textureEditor.hitTestJoints(ofVec2f(args.x, args.y)); CircleJoint* hitJoint = textureEditor.hitTestJoints(ofVec2f(args.x, args.y));
if ( hitJoint != NULL ) { if ( hitJoint != NULL ) {
textureEditor.unselectAllJoints(); textureEditor.unselectAllJoints();
hitJoint->select(); hitJoint->select();
@ -105,11 +107,11 @@ void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args)
} }
} }
} else if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) { } else if ( guiMode == GuiMode::PROJECTION_MAPPING ) {
bool bSurfaceSelected = false; bool bSurfaceSelected = false;
ofxCircleJoint* hitJoint = projectionEditor.hitTestJoints(ofVec2f(args.x, args.y)); CircleJoint* hitJoint = projectionEditor.hitTestJoints(ofVec2f(args.x, args.y));
if ( hitJoint != NULL ) { if ( hitJoint != NULL ) {
projectionEditor.unselectAllJoints(); projectionEditor.unselectAllJoints();
hitJoint->select(); hitJoint->select();
@ -141,51 +143,51 @@ void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args)
projectionEditor.clearJoints(); projectionEditor.clearJoints();
surfaceManager->deselectSurface(); surfaceManager->deselectSurface();
} }
} else if ( guiMode == ofxGuiMode::SOURCE_SELECTION ) { } else if ( guiMode == GuiMode::SOURCE_SELECTION ) {
} }
} }
void ofxSurfaceManagerGui::mouseReleased(ofMouseEventArgs &args) void SurfaceManagerGui::mouseReleased(ofMouseEventArgs &args)
{ {
stopDrag(); stopDrag();
projectionEditor.stopDragJoints(); projectionEditor.stopDragJoints();
textureEditor.stopDragJoints(); textureEditor.stopDragJoints();
} }
void ofxSurfaceManagerGui::mouseDragged(ofMouseEventArgs &args) void SurfaceManagerGui::mouseDragged(ofMouseEventArgs &args)
{ {
if (bDrag) { if (bDrag) {
ofVec2f mousePosition = ofVec2f(args.x, args.y); ofVec2f mousePosition = ofVec2f(args.x, args.y);
ofVec2f distance = mousePosition - clickPosition; ofVec2f distance = mousePosition - clickPosition;
if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) { if ( guiMode == GuiMode::PROJECTION_MAPPING ) {
// add this distance to all vertices in surface // add this distance to all vertices in surface
projectionEditor.moveSelectedSurface(distance); projectionEditor.moveSelectedSurface(distance);
} else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { } else if ( guiMode == GuiMode::TEXTURE_MAPPING ) {
textureEditor.moveTexCoords(distance); textureEditor.moveTexCoords(distance);
} }
clickPosition = mousePosition; clickPosition = mousePosition;
} }
} }
void ofxSurfaceManagerGui::setSurfaceManager(ofxSurfaceManager* newSurfaceManager) void SurfaceManagerGui::setSurfaceManager(SurfaceManager* newSurfaceManager)
{ {
surfaceManager = newSurfaceManager; surfaceManager = newSurfaceManager;
projectionEditor.setSurfaceManager( surfaceManager ); projectionEditor.setSurfaceManager( surfaceManager );
sourcesEditor.setSurfaceManager( surfaceManager ); sourcesEditor.setSurfaceManager( surfaceManager );
} }
void ofxSurfaceManagerGui::setMode(int newGuiMode) void SurfaceManagerGui::setMode(int newGuiMode)
{ {
if (newGuiMode != ofxGuiMode::NONE && if (newGuiMode != GuiMode::NONE &&
newGuiMode != ofxGuiMode::TEXTURE_MAPPING && newGuiMode != GuiMode::TEXTURE_MAPPING &&
newGuiMode != ofxGuiMode::PROJECTION_MAPPING && newGuiMode != GuiMode::PROJECTION_MAPPING &&
newGuiMode != ofxGuiMode::SOURCE_SELECTION) { newGuiMode != GuiMode::SOURCE_SELECTION) {
throw std::runtime_error("Trying to set invalid mode."); throw std::runtime_error("Trying to set invalid mode.");
} }
if ( newGuiMode == ofxGuiMode::NONE ) { if ( newGuiMode == GuiMode::NONE ) {
ofHideCursor(); ofHideCursor();
} else { } else {
ofShowCursor(); ofShowCursor();
@ -193,7 +195,7 @@ void ofxSurfaceManagerGui::setMode(int newGuiMode)
guiMode = newGuiMode; guiMode = newGuiMode;
if ( guiMode == ofxGuiMode::SOURCE_SELECTION ) { if ( guiMode == GuiMode::SOURCE_SELECTION ) {
sourcesEditor.enable(); sourcesEditor.enable();
string sourceName = surfaceManager->getSelectedSurfaceSourceName(); string sourceName = surfaceManager->getSelectedSurfaceSourceName();
sourcesEditor.selectImageSourceRadioButton(sourceName); sourcesEditor.selectImageSourceRadioButton(sourceName);
@ -201,7 +203,7 @@ void ofxSurfaceManagerGui::setMode(int newGuiMode)
sourcesEditor.disable(); sourcesEditor.disable();
} }
if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { if ( guiMode == GuiMode::TEXTURE_MAPPING ) {
textureEditor.enable(); textureEditor.enable();
// refresh texture editor surface reference // refresh texture editor surface reference
textureEditor.setSurface(surfaceManager->getSelectedSurface()); textureEditor.setSurface(surfaceManager->getSelectedSurface());
@ -209,14 +211,14 @@ void ofxSurfaceManagerGui::setMode(int newGuiMode)
textureEditor.disable(); textureEditor.disable();
} }
if (guiMode == ofxGuiMode::PROJECTION_MAPPING) { if (guiMode == GuiMode::PROJECTION_MAPPING) {
projectionEditor.enable(); projectionEditor.enable();
} else { } else {
projectionEditor.disable(); projectionEditor.disable();
} }
} }
void ofxSurfaceManagerGui::drawSelectedSurfaceHighlight() void SurfaceManagerGui::drawSelectedSurfaceHighlight()
{ {
if ( surfaceManager->getSelectedSurface() == NULL ) return; if ( surfaceManager->getSelectedSurface() == NULL ) return;
@ -229,7 +231,7 @@ void ofxSurfaceManagerGui::drawSelectedSurfaceHighlight()
ofPopStyle(); ofPopStyle();
} }
void ofxSurfaceManagerGui::drawSelectedSurfaceTextureHighlight() void SurfaceManagerGui::drawSelectedSurfaceTextureHighlight()
{ {
if ( surfaceManager->getSelectedSurface() == NULL ) return; if ( surfaceManager->getSelectedSurface() == NULL ) return;
@ -243,12 +245,14 @@ void ofxSurfaceManagerGui::drawSelectedSurfaceTextureHighlight()
} }
void ofxSurfaceManagerGui::startDrag() void SurfaceManagerGui::startDrag()
{ {
bDrag = true; bDrag = true;
} }
void ofxSurfaceManagerGui::stopDrag() void SurfaceManagerGui::stopDrag()
{ {
bDrag = false; bDrag = false;
} }
}}

34
src/SurfaceManagerGui.h

@ -1,22 +1,24 @@
#ifndef H_OFX_SURFACE_MANAGER_GUI #pragma once
#define H_OFX_SURFACE_MANAGER_GUI
// I'm starting to think, maybe we should use ofxStateMachine here. // I'm starting to think, maybe we should use ofxStateMachine here.
// Would make sense. TODO later. // Would make sense. TODO later.
#include "ofEvents.h" #include "ofEvents.h"
#include "ofxSurfaceManager.h"
#include "ofxTextureEditor.h"
#include "ofxProjectionEditor.h"
#include "ofxSourcesEditor.h"
#include "ofxGuiMode.h"
#include "ofGraphics.h" #include "ofGraphics.h"
class ofxSurfaceManagerGui #include "SurfaceManager.h"
#include "TextureEditor.h"
#include "ProjectionEditor.h"
#include "SourcesEditor.h"
#include "GuiMode.h"
namespace ofx{
namespace piMapper{
class SurfaceManagerGui
{ {
public: public:
ofxSurfaceManagerGui(); SurfaceManagerGui();
~ofxSurfaceManagerGui(); ~SurfaceManagerGui();
void registerMouseEvents(); void registerMouseEvents();
void unregisterMouseEvents(); void unregisterMouseEvents();
@ -25,7 +27,7 @@ public:
void mousePressed(ofMouseEventArgs& args); void mousePressed(ofMouseEventArgs& args);
void mouseReleased(ofMouseEventArgs& args); void mouseReleased(ofMouseEventArgs& args);
void mouseDragged(ofMouseEventArgs& args); void mouseDragged(ofMouseEventArgs& args);
void setSurfaceManager(ofxSurfaceManager* newSurfaceManager); void setSurfaceManager(SurfaceManager* newSurfaceManager);
void setMode(int newGuiMode); void setMode(int newGuiMode);
void drawSelectedSurfaceHighlight(); void drawSelectedSurfaceHighlight();
void drawSelectedSurfaceTextureHighlight(); void drawSelectedSurfaceTextureHighlight();
@ -33,14 +35,14 @@ public:
void stopDrag(); void stopDrag();
private: private:
ofxSurfaceManager* surfaceManager; SurfaceManager* surfaceManager;
ofxTextureEditor textureEditor; TextureEditor textureEditor;
ofxProjectionEditor projectionEditor; ProjectionEditor projectionEditor;
ofxSourcesEditor sourcesEditor; SourcesEditor sourcesEditor;
int guiMode; int guiMode;
bool bDrag; bool bDrag;
ofVec2f clickPosition; ofVec2f clickPosition;
}; };
#endif }}

9
src/SurfaceType.h

@ -1,7 +1,8 @@
#ifndef H_OFX_SURFACE_TYPE #pragma once
#define H_OFX_SURFACE_TYPE
struct ofxSurfaceType namespace ofx{
namespace piMapper{
struct SurfaceType
{ {
enum { enum {
TRIANGLE_SURFACE, TRIANGLE_SURFACE,
@ -9,4 +10,4 @@ struct ofxSurfaceType
}; };
}; };
#endif }}

68
src/TextureEditor.cpp

@ -1,53 +1,55 @@
#include "ofxTextureEditor.h" #include "TextureEditor.h"
ofxTextureEditor::ofxTextureEditor() namespace ofx{
namespace piMapper{
TextureEditor::TextureEditor()
{ {
clear(); clear();
enable(); enable();
} }
ofxTextureEditor::~ofxTextureEditor() TextureEditor::~TextureEditor()
{ {
clear(); clear();
disable(); disable();
} }
void ofxTextureEditor::registerAppEvents() void TextureEditor::registerAppEvents()
{ {
ofAddListener(ofEvents().update, this, &ofxTextureEditor::update); ofAddListener(ofEvents().update, this, &TextureEditor::update);
} }
void ofxTextureEditor::unregisterAppEvents() void TextureEditor::unregisterAppEvents()
{ {
ofRemoveListener(ofEvents().update, this, &ofxTextureEditor::update); ofRemoveListener(ofEvents().update, this, &TextureEditor::update);
} }
void ofxTextureEditor::registerKeyEvents() void TextureEditor::registerKeyEvents()
{ {
ofAddListener(ofEvents().keyPressed, this, &ofxTextureEditor::keyPressed); ofAddListener(ofEvents().keyPressed, this, &TextureEditor::keyPressed);
ofAddListener(ofEvents().keyReleased, this, &ofxTextureEditor::keyReleased); ofAddListener(ofEvents().keyReleased, this, &TextureEditor::keyReleased);
} }
void ofxTextureEditor::unregisterKeyEvents() void TextureEditor::unregisterKeyEvents()
{ {
ofRemoveListener(ofEvents().keyPressed, this, &ofxTextureEditor::keyPressed); ofRemoveListener(ofEvents().keyPressed, this, &TextureEditor::keyPressed);
ofRemoveListener(ofEvents().keyReleased, this, &ofxTextureEditor::keyReleased); ofRemoveListener(ofEvents().keyReleased, this, &TextureEditor::keyReleased);
} }
void ofxTextureEditor::enable() void TextureEditor::enable()
{ {
registerAppEvents(); registerAppEvents();
registerKeyEvents(); registerKeyEvents();
bShiftKeyDown = false; bShiftKeyDown = false;
} }
void ofxTextureEditor::disable() void TextureEditor::disable()
{ {
unregisterAppEvents(); unregisterAppEvents();
unregisterKeyEvents(); unregisterKeyEvents();
} }
void ofxTextureEditor::update(ofEventArgs &args) void TextureEditor::update(ofEventArgs &args)
{ {
if ( surface == NULL ) return; if ( surface == NULL ) return;
@ -62,7 +64,7 @@ void ofxTextureEditor::update(ofEventArgs &args)
} }
} }
void ofxTextureEditor::keyPressed(ofKeyEventArgs &args) void TextureEditor::keyPressed(ofKeyEventArgs &args)
{ {
int key = args.key; int key = args.key;
float moveStep; float moveStep;
@ -79,7 +81,7 @@ void ofxTextureEditor::keyPressed(ofKeyEventArgs &args)
} }
} }
void ofxTextureEditor::keyReleased(ofKeyEventArgs &args) void TextureEditor::keyReleased(ofKeyEventArgs &args)
{ {
int key = args.key; int key = args.key;
switch (key) { switch (key) {
@ -88,33 +90,33 @@ void ofxTextureEditor::keyReleased(ofKeyEventArgs &args)
} }
void ofxTextureEditor::draw() void TextureEditor::draw()
{ {
if (surface == NULL) return; if (surface == NULL) return;
drawJoints(); drawJoints();
} }
void ofxTextureEditor::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 ofxTextureEditor::setSurface(ofxBaseSurface* newSurface) void TextureEditor::setSurface(BaseSurface* newSurface)
{ {
surface = newSurface; surface = newSurface;
createJoints(); createJoints();
} }
void ofxTextureEditor::clear() void TextureEditor::clear()
{ {
surface = NULL; surface = NULL;
clearJoints(); clearJoints();
} }
void ofxTextureEditor::createJoints() void TextureEditor::createJoints()
{ {
if ( surface == NULL ) return; if ( surface == NULL ) return;
clearJoints(); clearJoints();
@ -122,12 +124,12 @@ void ofxTextureEditor::createJoints()
ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(), surface->getTexture()->getHeight()); ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(), surface->getTexture()->getHeight());
for ( int i=0; i<texCoords.size(); i++ ) { for ( int i=0; i<texCoords.size(); i++ ) {
joints.push_back(new ofxCircleJoint()); joints.push_back(new CircleJoint());
joints.back()->position = texCoords[i] * textureSize; joints.back()->position = texCoords[i] * textureSize;
} }
} }
void ofxTextureEditor::clearJoints() void TextureEditor::clearJoints()
{ {
while ( joints.size() ) { while ( joints.size() ) {
delete joints.back(); delete joints.back();
@ -135,14 +137,14 @@ void ofxTextureEditor::clearJoints()
} }
} }
void ofxTextureEditor::unselectAllJoints() void TextureEditor::unselectAllJoints()
{ {
for ( int i=0; i<joints.size(); i++ ) { for ( int i=0; i<joints.size(); i++ ) {
joints[i]->unselect(); joints[i]->unselect();
} }
} }
void ofxTextureEditor::moveTexCoords(ofVec2f by) void TextureEditor::moveTexCoords(ofVec2f by)
{ {
if ( surface == NULL ) return; if ( surface == NULL ) return;
vector<ofVec2f>& texCoords = surface->getTexCoords(); vector<ofVec2f>& texCoords = surface->getTexCoords();
@ -153,18 +155,18 @@ void ofxTextureEditor::moveTexCoords(ofVec2f by)
} }
} }
void ofxTextureEditor::stopDragJoints() void TextureEditor::stopDragJoints()
{ {
for (int i=0; i<joints.size(); i++){ for (int i=0; i<joints.size(); i++){
joints[i]->stopDrag(); joints[i]->stopDrag();
} }
} }
void ofxTextureEditor::moveSelection(ofVec2f by) void TextureEditor::moveSelection(ofVec2f by)
{ {
// check if joints selected // check if joints selected
bool bJointSelected = false; bool bJointSelected = false;
ofxBaseJoint* 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;
@ -180,7 +182,7 @@ void ofxTextureEditor::moveSelection(ofVec2f by)
} }
} }
ofxCircleJoint* ofxTextureEditor::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) ){
@ -188,4 +190,6 @@ ofxCircleJoint* ofxTextureEditor::hitTestJoints(ofVec2f pos)
} }
} }
return NULL; return NULL;
} }
}}

27
src/TextureEditor.h

@ -1,15 +1,18 @@
#ifndef H_OFX_TEXTURE_EDITOR #pragma once
#define H_OFX_TEXTURE_EDITOR
#include "ofEvents.h" #include "ofEvents.h"
#include "ofxBaseSurface.h"
#include "ofxCircleJoint.h"
class ofxTextureEditor #include "BaseSurface.h"
#include "CircleJoint.h"
namespace ofx{
namespace piMapper{
class TextureEditor
{ {
public: public:
ofxTextureEditor(); TextureEditor();
~ofxTextureEditor(); ~TextureEditor();
void registerAppEvents(); void registerAppEvents();
void unregisterAppEvents(); void unregisterAppEvents();
@ -23,7 +26,7 @@ public:
void keyReleased(ofKeyEventArgs& args); void keyReleased(ofKeyEventArgs& args);
void draw(); void draw();
void drawJoints(); void drawJoints();
void setSurface(ofxBaseSurface* newSurface); void setSurface(BaseSurface* newSurface);
void clear(); void clear();
void createJoints(); void createJoints();
void clearJoints(); void clearJoints();
@ -31,12 +34,12 @@ public:
void moveTexCoords(ofVec2f by); void moveTexCoords(ofVec2f by);
void stopDragJoints(); void stopDragJoints();
void moveSelection(ofVec2f by); void moveSelection(ofVec2f by);
ofxCircleJoint* hitTestJoints(ofVec2f pos); CircleJoint* hitTestJoints(ofVec2f pos);
private: private:
ofxBaseSurface* surface; BaseSurface* surface;
vector<ofxCircleJoint*> joints; vector<CircleJoint*> joints;
bool bShiftKeyDown; bool bShiftKeyDown;
}; };
#endif }}

46
src/TriangleSurface.cpp

@ -1,17 +1,19 @@
#include "ofxTriangleSurface.h" #include "TriangleSurface.h"
ofxTriangleSurface::ofxTriangleSurface() namespace ofx{
namespace piMapper{
TriangleSurface::TriangleSurface()
{ {
cout << "ofxTriangleSurface constructor." << endl; cout << "TriangleSurface constructor." << endl;
setup(); setup();
} }
ofxTriangleSurface::~ofxTriangleSurface() TriangleSurface::~TriangleSurface()
{ {
cout << "ofxTriangleSurface destructor." << endl; cout << "TriangleSurface destructor." << endl;
} }
void ofxTriangleSurface::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);
@ -26,7 +28,7 @@ void ofxTriangleSurface::setup()
setup( p1, p2, p3, t1, t2, t3, texture ); setup( p1, p2, p3, t1, t2, t3, texture );
} }
void ofxTriangleSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr ) void TriangleSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr )
{ {
// Assign texture // Assign texture
texture = texturePtr; texture = texturePtr;
@ -45,14 +47,14 @@ void ofxTriangleSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1,
mesh.addTexCoord(t3); mesh.addTexCoord(t3);
} }
void ofxTriangleSurface::draw() void TriangleSurface::draw()
{ {
texture->bind(); texture->bind();
mesh.draw(); mesh.draw();
texture->unbind(); texture->unbind();
} }
void ofxTriangleSurface::setVertex(int index, ofVec2f p) void TriangleSurface::setVertex(int index, ofVec2f p)
{ {
if ( index > 2 ) { if ( index > 2 ) {
ofLog() << "Vertex with this index does not exist: " << index << endl; ofLog() << "Vertex with this index does not exist: " << index << endl;
@ -62,7 +64,7 @@ void ofxTriangleSurface::setVertex(int index, ofVec2f p)
mesh.setVertex(index, p); mesh.setVertex(index, p);
} }
void ofxTriangleSurface::setTexCoord(int index, ofVec2f t) void TriangleSurface::setTexCoord(int index, ofVec2f t)
{ {
if ( index > 2 ) { if ( index > 2 ) {
ofLog() << "Texture coordinate with this index does not exist: " << index << endl; ofLog() << "Texture coordinate with this index does not exist: " << index << endl;
@ -72,7 +74,7 @@ void ofxTriangleSurface::setTexCoord(int index, ofVec2f t)
mesh.setTexCoord(index, t); mesh.setTexCoord(index, t);
} }
void ofxTriangleSurface::moveBy(ofVec2f v) void TriangleSurface::moveBy(ofVec2f v)
{ {
vector<ofVec3f>& vertices = getVertices(); vector<ofVec3f>& vertices = getVertices();
for (int i=0; i<vertices.size(); i++) { for (int i=0; i<vertices.size(); i++) {
@ -80,12 +82,12 @@ void ofxTriangleSurface::moveBy(ofVec2f v)
} }
} }
int ofxTriangleSurface::getType() int TriangleSurface::getType()
{ {
return ofxSurfaceType::TRIANGLE_SURFACE; return SurfaceType::TRIANGLE_SURFACE;
} }
bool ofxTriangleSurface::hitTest(ofVec2f p) bool TriangleSurface::hitTest(ofVec2f p)
{ {
// Construct ofPolyline from vertices // Construct ofPolyline from vertices
ofPolyline line = getHitArea(); ofPolyline line = getHitArea();
@ -97,7 +99,7 @@ bool ofxTriangleSurface::hitTest(ofVec2f p)
} }
} }
ofVec2f ofxTriangleSurface::getVertex(int index) ofVec2f TriangleSurface::getVertex(int index)
{ {
if ( index > 2 ) { if ( index > 2 ) {
ofLog() << "Vertex with this index does not exist: " << index << endl; ofLog() << "Vertex with this index does not exist: " << index << endl;
@ -108,7 +110,7 @@ ofVec2f ofxTriangleSurface::getVertex(int index)
return ofVec2f(vert.x, vert.y); return ofVec2f(vert.x, vert.y);
} }
ofVec2f ofxTriangleSurface::getTexCoord(int index) ofVec2f TriangleSurface::getTexCoord(int index)
{ {
if (index > 2) { if (index > 2) {
throw std::runtime_error("Texture coordinate index out of bounds."); throw std::runtime_error("Texture coordinate index out of bounds.");
@ -117,7 +119,7 @@ ofVec2f ofxTriangleSurface::getTexCoord(int index)
return mesh.getTexCoord(index); return mesh.getTexCoord(index);
} }
ofPolyline ofxTriangleSurface::getHitArea() ofPolyline TriangleSurface::getHitArea()
{ {
ofPolyline line; ofPolyline line;
line.addVertex( ofPoint( mesh.getVertex(0).x, mesh.getVertex(0).y ) ); line.addVertex( ofPoint( mesh.getVertex(0).x, mesh.getVertex(0).y ) );
@ -128,7 +130,7 @@ ofPolyline ofxTriangleSurface::getHitArea()
return line; return line;
} }
ofPolyline ofxTriangleSurface::getTextureHitArea() ofPolyline TriangleSurface::getTextureHitArea()
{ {
ofPolyline line; ofPolyline line;
vector<ofVec2f>& texCoords = mesh.getTexCoords(); vector<ofVec2f>& texCoords = mesh.getTexCoords();
@ -141,14 +143,16 @@ ofPolyline ofxTriangleSurface::getTextureHitArea()
return line; return line;
} }
vector<ofVec3f>& ofxTriangleSurface::getVertices() vector<ofVec3f>& TriangleSurface::getVertices()
{ {
// return only joint vertices // return only joint vertices
return mesh.getVertices(); return mesh.getVertices();
} }
vector<ofVec2f>& ofxTriangleSurface::getTexCoords() vector<ofVec2f>& TriangleSurface::getTexCoords()
{ {
return mesh.getTexCoords(); return mesh.getTexCoords();
} }
}}

18
src/TriangleSurface.h

@ -1,15 +1,17 @@
#ifndef H_OFX_TRIANGLE_SURFACE #pragma once
#define H_OFX_TRIANGLE_SURFACE
#include "ofMain.h" #include "ofMain.h"
#include "ofxBaseSurface.h" #include "BaseSurface.h"
#include "ofxSurfaceType.h" #include "SurfaceType.h"
class ofxTriangleSurface : public ofxBaseSurface
namespace ofx{
namespace piMapper{
class TriangleSurface : public BaseSurface
{ {
public: public:
ofxTriangleSurface(); TriangleSurface();
~ofxTriangleSurface(); ~TriangleSurface();
void setup(); void setup();
void setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr ); void setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr );
@ -28,4 +30,4 @@ public:
vector<ofVec2f>& getTexCoords(); vector<ofVec2f>& getTexCoords();
}; };
#endif }}

9
src/ofxPiMapper.h

@ -1,7 +1,4 @@
#ifndef H_OFX_PI_MAPPER #pragma once
#define H_OFX_PI_MAPPER
#include "ofxSurfaceManager.h" #include "SurfaceManager.h"
#include "ofxSurfaceManagerGui.h" #include "SurfaceManagerGui.h"
#endif

70
src/ui/RadioList.cpp

@ -1,29 +1,31 @@
#include "ofxRadioList.h" #include "RadioList.h"
ofxRadioList::ofxRadioList() namespace ofx{
namespace piMapper{
RadioList::RadioList()
{ {
storedTitle = ""; storedTitle = "";
storedSelectedItem = 0; storedSelectedItem = 0;
} }
ofxRadioList::ofxRadioList(vector<string> &labels) RadioList::RadioList(vector<string> &labels)
{ {
ofxRadioList(); RadioList();
setup(labels); setup(labels);
} }
ofxRadioList::ofxRadioList(string title, vector<string> &labels) RadioList::RadioList(string title, vector<string> &labels)
{ {
ofxRadioList(); RadioList();
setup(title, labels); setup(title, labels);
} }
ofxRadioList::~ofxRadioList() RadioList::~RadioList()
{ {
clear(); clear();
} }
void ofxRadioList::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;
@ -34,14 +36,14 @@ void ofxRadioList::setup(vector<string> &labels)
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, &ofxRadioList::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 ofxRadioList::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;
@ -49,28 +51,28 @@ void ofxRadioList::setup(string title, vector<string> &labels)
setup(labels); setup(labels);
} }
void ofxRadioList::draw() void RadioList::draw()
{ {
guiGroup.draw(); guiGroup.draw();
} }
void ofxRadioList::setTitle(string title) void RadioList::setTitle(string title)
{ {
storedTitle = title; storedTitle = title;
guiGroup.setName(title); guiGroup.setName(title);
} }
void ofxRadioList::setPosition(ofPoint p) void RadioList::setPosition(ofPoint p)
{ {
guiGroup.setPosition(p); guiGroup.setPosition(p);
} }
void ofxRadioList::setPosition(float x, float y) void RadioList::setPosition(float x, float y)
{ {
guiGroup.setPosition(x, y); guiGroup.setPosition(x, y);
} }
void ofxRadioList::selectItem(int index) void RadioList::selectItem(int index)
{ {
if (index >= guiGroup.getNumControls()) { if (index >= guiGroup.getNumControls()) {
return; return;
@ -79,16 +81,16 @@ void ofxRadioList::selectItem(int index)
unselectAll(); unselectAll();
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(index)); ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(index));
toggle->removeListener(this, &ofxRadioList::onToggleClicked); toggle->removeListener(this, &RadioList::onToggleClicked);
*toggle = true; // Select the specific radio button *toggle = true; // Select the specific radio button
toggle->addListener(this, &ofxRadioList::onToggleClicked); toggle->addListener(this, &RadioList::onToggleClicked);
string name = toggle->getName(); string name = toggle->getName();
ofNotifyEvent(radioSelectedEvent, name, this); ofNotifyEvent(radioSelectedEvent, name, this);
storedSelectedItem = index; storedSelectedItem = index;
} }
void ofxRadioList::enable() void RadioList::enable()
{ {
if (guiGroup.getNumControls() >= 0) { if (guiGroup.getNumControls() >= 0) {
clear(); clear();
@ -99,63 +101,63 @@ void ofxRadioList::enable()
// Select the stored selected item without throwing an event // Select the stored selected item without throwing an event
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(storedSelectedItem)); ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(storedSelectedItem));
toggle->removeListener(this, &ofxRadioList::onToggleClicked); toggle->removeListener(this, &RadioList::onToggleClicked);
*toggle = true; *toggle = true;
toggle->addListener(this, &ofxRadioList::onToggleClicked); toggle->addListener(this, &RadioList::onToggleClicked);
cout << "num items after enable: " << guiGroup.getNumControls() << endl; cout << "num items after enable: " << guiGroup.getNumControls() << endl;
} }
void ofxRadioList::disable() void RadioList::disable()
{ {
// Just remove everything // Just remove everything
clear(); clear();
} }
void ofxRadioList::clear() void RadioList::clear()
{ {
int i; int i;
for (i = 0; i < guiGroup.getNumControls(); i++) { for (i = 0; i < guiGroup.getNumControls(); i++) {
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i)); ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i));
toggle->removeListener(this, &ofxRadioList::onToggleClicked); toggle->removeListener(this, &RadioList::onToggleClicked);
delete toggle; delete toggle;
} }
guiGroup.clear(); guiGroup.clear();
} }
void ofxRadioList::unselectAll() void RadioList::unselectAll()
{ {
int i; int i;
for (i = 0; i < guiGroup.getNumControls(); i++) { for (i = 0; i < guiGroup.getNumControls(); i++) {
ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i)); ofxToggle* toggle = static_cast<ofxToggle*>(guiGroup.getControl(i));
ofParameter<bool>* paramPtr = static_cast<ofParameter<bool>*>(&toggle->getParameter()); ofParameter<bool>* paramPtr = static_cast<ofParameter<bool>*>(&toggle->getParameter());
toggle->removeListener(this, &ofxRadioList::onToggleClicked); toggle->removeListener(this, &RadioList::onToggleClicked);
*toggle = false; *toggle = false;
toggle->addListener(this, &ofxRadioList::onToggleClicked); toggle->addListener(this, &RadioList::onToggleClicked);
} }
} }
ofPoint ofxRadioList::getPosition() ofPoint RadioList::getPosition()
{ {
return guiGroup.getPosition(); return guiGroup.getPosition();
} }
float ofxRadioList::getWidth() float RadioList::getWidth()
{ {
return guiGroup.getWidth(); return guiGroup.getWidth();
} }
float ofxRadioList::getHeight() float RadioList::getHeight()
{ {
return guiGroup.getHeight(); return guiGroup.getHeight();
} }
string ofxRadioList::getTitle() string RadioList::getTitle()
{ {
return guiGroup.getName(); return guiGroup.getName();
} }
string ofxRadioList::getItemName(int index) string RadioList::getItemName(int index)
{ {
if (index >= guiGroup.getNumControls()) { if (index >= guiGroup.getNumControls()) {
return ""; return "";
@ -165,12 +167,12 @@ string ofxRadioList::getItemName(int index)
return toggle->getName(); return toggle->getName();
} }
int ofxRadioList::size() int RadioList::size()
{ {
return guiGroup.getNumControls(); return guiGroup.getNumControls();
} }
void ofxRadioList::onToggleClicked(bool &toggleValue) void RadioList::onToggleClicked(bool &toggleValue)
{ {
unselectAll(); unselectAll();
@ -186,3 +188,5 @@ void ofxRadioList::onToggleClicked(bool &toggleValue)
} }
} }
} }
}}

18
src/ui/RadioList.h

@ -5,13 +5,15 @@
#include "ofxToggle.h" #include "ofxToggle.h"
#include "ofxLabel.h" #include "ofxLabel.h"
class ofxRadioList namespace ofx{
namespace piMapper{
class RadioList
{ {
public: public:
ofxRadioList(); RadioList();
ofxRadioList(vector<string> &labels); RadioList(vector<string> &labels);
ofxRadioList(string title, vector<string> &labels); RadioList(string title, vector<string> &labels);
~ofxRadioList(); ~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);
@ -32,7 +34,7 @@ public:
int size(); int size();
// This event notifies about a toggle being selected and passes it's name to the listeners. // This event notifies about a toggle being selected and passes it's name to the listeners.
// Use ofAddListener(ofxRadioListInstance.radioSelectedEvent, listenerClassPtr, &listenerClass::listenerMethod) // Use ofAddListener(RadioListInstance.radioSelectedEvent, listenerClassPtr, &listenerClass::listenerMethod)
// to listen to this. Listner method void listenerMethod(string & radioName) // to listen to this. Listner method void listenerMethod(string & radioName)
ofEvent<string> radioSelectedEvent; ofEvent<string> radioSelectedEvent;
@ -43,4 +45,6 @@ private:
int storedSelectedItem; int storedSelectedItem;
void onToggleClicked(bool &toggleValue); void onToggleClicked(bool &toggleValue);
}; };
}}
Loading…
Cancel
Save