Browse Source

Add joints and surface deselection

master
Krisjanis Rijnieks 11 years ago
parent
commit
8ab9754ff6
  1. 1
      example/src/ofApp.cpp
  2. 1
      src/ofxBaseSurface.h
  3. 58
      src/ofxProjectionEditor.cpp
  4. 13
      src/ofxProjectionEditor.h
  5. 20
      src/ofxSurfaceManager.cpp
  6. 4
      src/ofxSurfaceManager.h
  7. 41
      src/ofxSurfaceManagerGui.cpp
  8. 3
      src/ofxSurfaceManagerGui.h
  9. 6
      src/ofxTriangleSurface.cpp
  10. 1
      src/ofxTriangleSurface.h

1
example/src/ofApp.cpp

@ -13,7 +13,6 @@ void ofApp::setup()
surfaceManager.getSurface(1)->setVertex(2, ofVec2f(300, 400));
gui.setSurfaceManager( &surfaceManager );
gui.selectSurface(1);
}
void ofApp::update()

1
src/ofxBaseSurface.h

@ -17,6 +17,7 @@ public:
virtual int getType(){};
virtual bool hitTest(ofVec2f p){};
virtual ofPolyline getHitArea(){};
virtual vector<ofVec3f>& getVertices(){};
// Draws a texture using ofMesh
void drawTexture(ofVec2f position);

58
src/ofxProjectionEditor.cpp

@ -2,15 +2,69 @@
ofxProjectionEditor::ofxProjectionEditor()
{
surfaceManager = NULL;
}
ofxProjectionEditor::~ofxProjectionEditor()
{
clearJoints();
surfaceManager = NULL;
}
void ofxProjectionEditor::draw()
{
if ( surfaceManager == NULL ) return;
cout << "ofxProjectionEditor::draw()" << endl;
if ( surfaceManager->getSelectedSurface() == NULL ) return;
if ( joints.size() <= 0 ) createJoints();
drawJoints();
}
void ofxProjectionEditor::setSurfaceManager(ofxSurfaceManager *newSurfaceManager)
{
surfaceManager = newSurfaceManager;
}
void ofxProjectionEditor::clearJoints()
{
while ( joints.size() ) {
delete joints.back();
joints.pop_back();
}
}
void ofxProjectionEditor::createJoints()
{
if ( surfaceManager == NULL ) return;
clearJoints();
if ( surfaceManager->getSelectedSurface() == NULL ) {
ofLog(OF_LOG_WARNING, "Trying to create joints while no surface selected.");
return;
}
vector<ofVec3f>& vertices = surfaceManager->getSelectedSurface()->getVertices();
for ( int i=0; i<vertices.size(); i++ ) {
joints.push_back( new ofxCircleJoint() );
joints.back()->position = ofVec2f(vertices[i].x, vertices[i].y);
}
}
bool ofxProjectionEditor::hitTestJoints(ofVec2f pos)
{
for ( int i=0; i<joints.size(); i++ ) {
if ( joints[i]->hitTest(pos) ){
return true;
}
}
return false;
}
void ofxProjectionEditor::drawJoints()
{
cout << "draw joints" << endl;
for ( int i=0; i<joints.size(); i++ ) {
joints[i]->draw();
}
}

13
src/ofxProjectionEditor.h

@ -1,6 +1,9 @@
#ifndef H_OFX_PROJECTION_EDITOR
#define H_OFX_PROJECTION_EDITOR
#include "ofxSurfaceManager.h"
#include "ofxCircleJoint.h"
class ofxProjectionEditor
{
public:
@ -8,6 +11,16 @@ public:
~ofxProjectionEditor();
void draw();
void setSurfaceManager(ofxSurfaceManager* newSurfaceManager);
void clearJoints();
void createJoints();
bool hitTestJoints(ofVec2f pos);
private:
ofxSurfaceManager* surfaceManager;
vector<ofxCircleJoint*> joints;
void drawJoints();
};
#endif

20
src/ofxSurfaceManager.cpp

@ -30,6 +30,25 @@ void ofxSurfaceManager::addSurface(int surfaceType)
}
}
ofxBaseSurface* ofxSurfaceManager::selectSurface(int index)
{
if ( index >= surfaces.size() ) {
throw std::runtime_error("Surface index out of bounds.");
}
selectedSurface = surfaces[index];
}
ofxBaseSurface* ofxSurfaceManager::getSelectedSurface()
{
return selectedSurface;
}
void ofxSurfaceManager::deselectSurface()
{
selectedSurface = NULL;
}
ofxBaseSurface* ofxSurfaceManager::getSurface(int index)
{
if ( index >= surfaces.size() ) {
@ -44,3 +63,4 @@ int ofxSurfaceManager::size()
{
return surfaces.size();
}

4
src/ofxSurfaceManager.h

@ -22,9 +22,13 @@ public:
void addSurface(int surfaceType);
ofxBaseSurface* getSurface(int index);
int size();
ofxBaseSurface* selectSurface(int index);
ofxBaseSurface* getSelectedSurface();
void deselectSurface();
private:
vector<ofxBaseSurface*> surfaces;
ofxBaseSurface* selectedSurface;
};
#endif

41
src/ofxSurfaceManagerGui.cpp

@ -4,7 +4,6 @@ ofxSurfaceManagerGui::ofxSurfaceManagerGui()
{
surfaceManager = NULL;
guiMode = ofxGuiMode::NONE;
selectedSurface = NULL;
registerMouseEvents();
}
@ -33,8 +32,8 @@ void ofxSurfaceManagerGui::draw()
} else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) {
// draw the texture of the selected surface
if ( selectedSurface != NULL ) {
selectedSurface->drawTexture( ofVec2f(0,0) );
if ( surfaceManager->getSelectedSurface() != NULL ) {
surfaceManager->getSelectedSurface()->drawTexture( ofVec2f(0,0) );
}
// draw surfaces with opacity
@ -71,18 +70,34 @@ void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args)
} else if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) {
// attempt to select surface, loop from end to beginning
bool bSurfaceSelected = false;
for ( int i=surfaceManager->size()-1; i>=0; i-- ) {
if ( surfaceManager->getSurface(i)->hitTest( ofVec2f(args.x, args.y) ) ) {
selectSurface(i);
projectionEditor.clearJoints();
surfaceManager->selectSurface(i);
projectionEditor.createJoints();
bSurfaceSelected = true;
break;
}
}
// Check if hitting one of the joints as that also couts as hit when surface is selected
if ( projectionEditor.hitTestJoints(ofVec2f(args.x, args.y)) ) {
bSurfaceSelected = true;
}
if ( !bSurfaceSelected ) {
// unselect if no surface selected
projectionEditor.clearJoints();
surfaceManager->deselectSurface();
}
}
}
void ofxSurfaceManagerGui::setSurfaceManager(ofxSurfaceManager* newSurfaceManager)
{
surfaceManager = newSurfaceManager;
projectionEditor.setSurfaceManager( surfaceManager );
}
void ofxSurfaceManagerGui::setMode(int newGuiMode)
@ -96,25 +111,11 @@ void ofxSurfaceManagerGui::setMode(int newGuiMode)
guiMode = newGuiMode;
}
ofxBaseSurface* ofxSurfaceManagerGui::selectSurface(int index)
{
if ( index >= surfaceManager->size() ) {
throw std::runtime_error("Surface index out of bounds.");
}
selectedSurface = surfaceManager->getSurface(index);
}
void ofxSurfaceManagerGui::deselectSurface()
{
selectedSurface = NULL;
}
void ofxSurfaceManagerGui::drawSelectedSurfaceHighlight()
{
if ( selectedSurface == NULL ) return;
if ( surfaceManager->getSelectedSurface() == NULL ) return;
ofPolyline line = selectedSurface->getHitArea();
ofPolyline line = surfaceManager->getSelectedSurface()->getHitArea();
ofPushStyle();
ofSetLineWidth(1);

3
src/ofxSurfaceManagerGui.h

@ -20,8 +20,6 @@ public:
void mousePressed(ofMouseEventArgs& args);
void setSurfaceManager(ofxSurfaceManager* newSurfaceManager);
void setMode(int newGuiMode);
ofxBaseSurface* selectSurface(int index);
void deselectSurface();
void drawSelectedSurfaceHighlight();
private:
@ -29,7 +27,6 @@ private:
ofxTextureEditor textureEditor;
ofxProjectionEditor projectionEditor;
int guiMode;
ofxBaseSurface* selectedSurface;
};
#endif

6
src/ofxTriangleSurface.cpp

@ -118,4 +118,10 @@ ofPolyline ofxTriangleSurface::getHitArea()
line.close();
return line;
}
vector<ofVec3f>& ofxTriangleSurface::getVertices()
{
// return only joint vertices
return mesh.getVertices();
}

1
src/ofxTriangleSurface.h

@ -22,6 +22,7 @@ public:
ofVec2f getVertex(int index);
ofVec2f getTexCoord(int index);
ofPolyline getHitArea();
vector<ofVec3f>& getVertices();
};
#endif
Loading…
Cancel
Save