Browse Source

Add projection surface joint movement

master
Krisjanis Rijnieks 11 years ago
parent
commit
4c93ea241f
  1. 33
      src/ofxBaseJoint.cpp
  2. 9
      src/ofxBaseJoint.h
  3. 56
      src/ofxProjectionEditor.cpp
  4. 11
      src/ofxProjectionEditor.h
  5. 384
      src/ofxSurfaceGui.cpp
  6. 71
      src/ofxSurfaceGui.h
  7. 18
      src/ofxSurfaceManagerGui.cpp

33
src/ofxBaseJoint.cpp

@ -4,18 +4,31 @@ ofxBaseJoint::ofxBaseJoint()
{
setDefaultColors();
setDefaultProperties();
registerMouseEvents();
}
ofxBaseJoint::~ofxBaseJoint()
{
unregisterMouseEvents();
}
void ofxBaseJoint::registerMouseEvents()
{
ofAddListener(ofEvents().mousePressed, this, &ofxBaseJoint::mousePressed);
ofAddListener(ofEvents().mouseDragged, this, &ofxBaseJoint::mouseDragged);
}
void ofxBaseJoint::unregisterMouseEvents()
{
ofRemoveListener(ofEvents().mousePressed, this, &ofxBaseJoint::mousePressed);
ofRemoveListener(ofEvents().mouseDragged, this, &ofxBaseJoint::mouseDragged);
}
void ofxBaseJoint::mousePressed(int x, int y, int button)
void ofxBaseJoint::mousePressed(ofMouseEventArgs& args)
{
if ( hitTest(ofVec2f(x, y)) ) {
if ( hitTest(ofVec2f(args.x, args.y)) ) {
//selected = true;
clickDistance = position - ofVec2f(x, y);
clickDistance = position - ofVec2f(args.x, args.y);
//startDrag();
}
}
@ -25,25 +38,25 @@ void ofxBaseJoint::mouseReleased(int x, int y, int button)
stopDrag();
}
void ofxBaseJoint::mouseDragged(int x, int y, int button)
void ofxBaseJoint::mouseDragged(ofMouseEventArgs& args)
{
if ( !dragging ) return;
position = ofVec2f(x, y) + clickDistance;
if ( !bDrag ) return;
position = ofVec2f(args.x, args.y) + clickDistance;
}
void ofxBaseJoint::startDrag()
{
dragging = true;
bDrag = true;
}
void ofxBaseJoint::stopDrag()
{
dragging = false;
bDrag = false;
}
bool ofxBaseJoint::isDragged()
{
return dragging;
return bDrag;
}
void ofxBaseJoint::setDefaultColors()
@ -60,7 +73,7 @@ void ofxBaseJoint::setDefaultProperties()
visible = true;
position = ofVec2f(20.0f, 20.0f);
clickDistance = ofVec2f(0.0f, 0.0f);
dragging = false;
bDrag = false;
selected = false;
strokeWidth = 1.5f;
}

9
src/ofxBaseJoint.h

@ -8,14 +8,17 @@ public:
ofxBaseJoint();
~ofxBaseJoint();
void registerMouseEvents();
void unregisterMouseEvents();
ofVec2f position;
bool enabled;
bool visible;
bool selected;
void mousePressed(int x, int y, int button);
void mousePressed(ofMouseEventArgs& args);
void mouseReleased(int x, int y, int button);
void mouseDragged(int x, int y, int button);
void mouseDragged(ofMouseEventArgs& args);
void startDrag();
void stopDrag();
bool isDragged();
@ -31,7 +34,7 @@ protected:
ofColor strokeColorSelected;
float strokeWidth;
ofVec2f clickDistance; // How far from the center of the joint the user has clicked?
bool dragging;
bool bDrag;
private:
void setDefaultColors();

56
src/ofxProjectionEditor.cpp

@ -3,23 +3,63 @@
ofxProjectionEditor::ofxProjectionEditor()
{
surfaceManager = NULL;
registerAppEvents();
registerMouseEvents();
}
ofxProjectionEditor::~ofxProjectionEditor()
{
clearJoints();
surfaceManager = NULL;
unregisterAppEvents();
unregisterMouseEvents();
}
void ofxProjectionEditor::registerAppEvents()
{
ofAddListener(ofEvents().update, this, &ofxProjectionEditor::update);
}
void ofxProjectionEditor::unregisterAppEvents()
{
ofRemoveListener(ofEvents().update, this, &ofxProjectionEditor::update);
}
void ofxProjectionEditor::registerMouseEvents()
{
ofAddListener(ofEvents().mouseDragged, this, &ofxProjectionEditor::mouseDragged);
}
void ofxProjectionEditor::unregisterMouseEvents()
{
ofRemoveListener(ofEvents().mouseDragged, this, &ofxProjectionEditor::mouseDragged);
}
void ofxProjectionEditor::update(ofEventArgs &args)
{
// update surface if one of the joints is being dragged
for ( int i=0; i<joints.size(); i++ ) {
if ( joints[i]->isDragged() ) {
// update vertex to new location
surfaceManager->getSelectedSurface()->setVertex(i, joints[i]->position);
break;
}
}
}
void ofxProjectionEditor::draw()
{
if ( surfaceManager == NULL ) return;
cout << "ofxProjectionEditor::draw()" << endl;
if ( surfaceManager->getSelectedSurface() == NULL ) return;
if ( joints.size() <= 0 ) createJoints();
drawJoints();
}
void ofxProjectionEditor::mouseDragged(ofMouseEventArgs &args)
{
//
}
void ofxProjectionEditor::setSurfaceManager(ofxSurfaceManager *newSurfaceManager)
{
surfaceManager = newSurfaceManager;
@ -70,19 +110,25 @@ void ofxProjectionEditor::moveSelectedSurface(ofVec2f by)
updateJoints();
}
bool ofxProjectionEditor::hitTestJoints(ofVec2f pos)
void ofxProjectionEditor::stopDragJoints()
{
for (int i=0; i<joints.size(); i++){
joints[i]->stopDrag();
}
}
ofxCircleJoint* ofxProjectionEditor::hitTestJoints(ofVec2f pos)
{
for ( int i=0; i<joints.size(); i++ ) {
if ( joints[i]->hitTest(pos) ){
return true;
return joints[i];
}
}
return false;
return NULL;
}
void ofxProjectionEditor::drawJoints()
{
cout << "draw joints" << endl;
for ( int i=0; i<joints.size(); i++ ) {
joints[i]->draw();
}

11
src/ofxProjectionEditor.h

@ -10,13 +10,22 @@ public:
ofxProjectionEditor();
~ofxProjectionEditor();
void registerAppEvents();
void unregisterAppEvents();
void registerMouseEvents();
void unregisterMouseEvents();
void update(ofEventArgs& args);
void draw();
void mouseDragged(ofMouseEventArgs& args);
void setSurfaceManager(ofxSurfaceManager* newSurfaceManager);
void clearJoints();
void createJoints();
void updateJoints();
void moveSelectedSurface(ofVec2f by);
bool hitTestJoints(ofVec2f pos);
void stopDragJoints();
void updateVertices();
ofxCircleJoint* hitTestJoints(ofVec2f pos);
private:
ofxSurfaceManager* surfaceManager;

384
src/ofxSurfaceGui.cpp

@ -1,384 +0,0 @@
#include "ofxSurfaceGui.h"
ofxSurfaceGui::ofxSurfaceGui()
{
surface = NULL;
mode = NONE;
bProjectionMappingJointSelected = false;
bTextureMappingJointSelected = false;
bTextureDragging = false;
bProjectionDragging = false;
bSelected = false;
}
ofxSurfaceGui::~ofxSurfaceGui()
{
}
void ofxSurfaceGui::setup(ofxTriangleSurface& surfaceForGui)
{
surface = &surfaceForGui;
addNumProjectionMappingJoints(3);
addNumTextureMappingJoints(3);
}
void ofxSurfaceGui::update()
{
if (surface == NULL) return;
if (mode == NONE) return;
if (mode == PROJECTION_MAPPING) {
for ( int i=0; i<projectionMappingJoints.size(); i++ ) {
projectionMappingJoints[i].update();
}
} else if (mode == TEXTURE_MAPPING) {
for ( int i=0; i<textureMappingJoints.size(); i++ ) {
textureMappingJoints[i].update();
}
}
}
void ofxSurfaceGui::draw()
{
if (surface == NULL) return;
if (mode == NONE) return;
// This has to be on bottom, so is drawn first
if (bSelected && mode == TEXTURE_MAPPING) {
ofPolyline line;
for ( int i=0; i<textureMappingJoints.size(); i++ ) {
line.addVertex( ofPoint(textureMappingJoints[i].position.x,
textureMappingJoints[i].position.y) );
}
line.close();
line.draw();
for ( int i=0; i<textureMappingJoints.size(); i++ ) {
textureMappingJoints[i].draw();
}
}
// Draw line around projection surface always when selected
if ( bSelected ) {
ofPolyline line;
for ( int i=0; i<projectionMappingJoints.size(); i++ ) {
line.addVertex( ofPoint(projectionMappingJoints[i].position.x,
projectionMappingJoints[i].position.y) );
}
line.close();
line.draw();
}
// Draw projection surface joints
if (bSelected && mode == PROJECTION_MAPPING) {
for ( int i=0; i<projectionMappingJoints.size(); i++ ) {
projectionMappingJoints[i].draw();
}
}
}
void ofxSurfaceGui::mousePressed(int x, int y, int button)
{
if (surface == NULL) return;
if (mode == NONE) return;
if (!bSelected) return;
if (mode == PROJECTION_MAPPING) {
bProjectionMappingJointSelected = false;
for ( int i=0; i<projectionMappingJoints.size(); i++ ) {
projectionMappingJoints[i].selected = false;
projectionMappingJoints[i].mousePressed(x, y, button);
if ( !isProjectionMappingJointSelected() && projectionMappingJoints[i].hitTest(ofVec2f(x, y)) ) {
projectionMappingJoints[i].selected = true;
projectionMappingJoints[i].startDrag();
bProjectionMappingJointSelected = true;
}
}
if ( !bProjectionMappingJointSelected ) {
// Check if we are hitting projection hitarea
if ( projectionHitarea.inside(x, y) ) {
clickPosition = ofVec2f(x, y);
stopDrag();
dragProjectionArea();
}
}
} else if (mode == TEXTURE_MAPPING) {
bTextureMappingJointSelected = false;
for ( int i=0; i<textureMappingJoints.size(); i++ ) {
textureMappingJoints[i].selected = false;
textureMappingJoints[i].mousePressed(x, y, button);
if ( !isTextureMappingJointSelected() && textureMappingJoints[i].hitTest(ofVec2f(x, y)) ) {
textureMappingJoints[i].selected = true;
textureMappingJoints[i].startDrag();
bTextureMappingJointSelected = true;
}
}
if ( !bTextureMappingJointSelected ) {
// Check if we are hitting the texture mapping hitarea
if ( textureHitarea.inside(x, y) ) {
clickPosition = ofVec2f(x, y);
stopDrag();
dragTextureArea();
}
}
}
}
void ofxSurfaceGui::mouseReleased(int x, int y, int button)
{
if (surface == NULL) return;
stopDrag();
if (mode == NONE) return;
if (mode == PROJECTION_MAPPING) {
for ( int i=0; i<projectionMappingJoints.size(); i++ ) {
projectionMappingJoints[i].mouseReleased(x, y, button);
}
} else if (mode == TEXTURE_MAPPING) {
for ( int i=0; i<textureMappingJoints.size(); i++ ) {
textureMappingJoints[i].mouseReleased(x, y, button);
}
}
}
void ofxSurfaceGui::mouseDragged(int x, int y, int button)
{
if (surface == NULL) return;
if (mode == NONE) return;
if (!bSelected) return;
if (mode == PROJECTION_MAPPING) {
if ( bProjectionDragging ) {
ofVec2f curPos = ofVec2f(x, y);
ofVec2f dist = curPos - clickPosition;
for ( int i=0; i<projectionMappingJoints.size(); i++ ) {
projectionMappingJoints[i].position += dist;
surface->setVertex(i, projectionMappingJoints[i].position);
}
updateProjectionHitarea();
clickPosition = curPos;
} else {
for ( int i=0; i<projectionMappingJoints.size(); i++ ) {
projectionMappingJoints[i].mouseDragged(x, y, button);
if ( projectionMappingJoints[i].isDragged() ) {
surface->setVertex(i, projectionMappingJoints[i].position);
updateProjectionHitarea();
}
} // for
} // if ( bProjectionDragging
} else if (mode == TEXTURE_MAPPING) {
ofVec2f textureSize = ofVec2f( surface->getTexture()->getWidth(), surface->getTexture()->getHeight() );
if ( bTextureDragging ) {
ofVec2f curPos = ofVec2f(x, y);
ofVec2f dist = curPos - clickPosition;
for ( int i=0; i<textureMappingJoints.size(); i++ ) {
textureMappingJoints[i].position += dist;
surface->setTexCoord(i, textureMappingJoints[i].position/textureSize);
}
updateTextureHitarea();
clickPosition = curPos;
} else {
for ( int i=0; i<textureMappingJoints.size(); i++ ) {
textureMappingJoints[i].mouseDragged(x, y, button);
if ( textureMappingJoints[i].isDragged() ) {
surface->setTexCoord(i, textureMappingJoints[i].position/textureSize);
updateTextureHitarea();
}
} // for
} // if ( bTextureDragging
} // if (mode
}
void ofxSurfaceGui::setMode(ofxSurfaceGui::editMode newMode)
{
if (mode != NONE &&
mode != PROJECTION_MAPPING &&
mode != TEXTURE_MAPPING) {
throw std::runtime_error("Trying to set invalid mode.");
};
mode = newMode;
}
void ofxSurfaceGui::select()
{
bSelected = true;
}
void ofxSurfaceGui::unselect()
{
bSelected = false;
}
void ofxSurfaceGui::updateTextureHitarea()
{
textureHitarea.clear();
for ( int i=0; i<textureMappingJoints.size(); i++ ) {
textureHitarea.addVertex( ofPoint(textureMappingJoints[i].position.x,
textureMappingJoints[i].position.y) );
}
}
void ofxSurfaceGui::updateProjectionHitarea()
{
projectionHitarea.clear();
for ( int i=0; i<projectionMappingJoints.size(); i++ ) {
projectionHitarea.addVertex( ofPoint(projectionMappingJoints[i].position.x,
projectionMappingJoints[i].position.y) );
}
}
void ofxSurfaceGui::updateHitarea()
{
updateProjectionHitarea();
updateTextureHitarea();
}
void ofxSurfaceGui::updateJoints()
{
// move joints to their positions
for ( int i=0; i<projectionMappingJoints.size(); i++ ) {
projectionMappingJoints[i].position = surface->getVertex(i);
}
updateProjectionHitarea();
ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(), surface->getTexture()->getHeight());
for ( int i=0; i<textureMappingJoints.size(); i++ ) {
textureMappingJoints[i].position = surface->getTexCoord(i) * textureSize;
}
updateTextureHitarea();
}
bool ofxSurfaceGui::hitTest(float x, float y)
{
if (mode == PROJECTION_MAPPING){
return hitTestProjectionArea(x, y);
} else if (mode == TEXTURE_MAPPING) {
return hitTestTextureArea(x, y);
}
}
bool ofxSurfaceGui::hitTestTextureArea(float x, float y)
{
if ( textureAreaExists() ) {
if ( textureHitarea.inside(x, y) ) {
return true;
} else {
// are we hitting texture mapping joints?
for ( int i=0; i<textureMappingJoints.size(); i++ ) {
if ( textureMappingJoints[i].hitTest(ofVec2f(x, y)) ) {
return true;
}
}
return false;
}
} else {
return false;
} // textureAreaExists()
}
bool ofxSurfaceGui::hitTestProjectionArea(float x, float y)
{
if ( projectionAreaExists() ) {
if ( projectionHitarea.inside(x, y) ) {
return true;
} else {
// hitting one of the projection mappting joints also counts,
// so let's check that
for ( int i=0; i<projectionMappingJoints.size(); i++ ) {
if ( projectionMappingJoints[i].hitTest(ofVec2f(x, y)) ) {
return true;
}
}
return false;
}
} else {
return false;
} // projectionAreaExists()
}
bool ofxSurfaceGui::isSelected()
{
return bSelected;
}
ofxSurfaceGui::editMode ofxSurfaceGui::getMode()
{
return mode;
}
void ofxSurfaceGui::addProjectionMappingJoint()
{
projectionMappingJoints.push_back(ofxCircleJoint());
projectionMappingJoints.back().position = surface->getVertex(projectionMappingJoints.size()-1);
updateProjectionHitarea();
}
void ofxSurfaceGui::addNumProjectionMappingJoints(int num)
{
for ( int i=0; i<num; i++ ) {
addProjectionMappingJoint();
}
}
void ofxSurfaceGui::addTextureMappingJoint()
{
textureMappingJoints.push_back(ofxCircleJoint());
ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(), surface->getTexture()->getHeight());
textureMappingJoints.back().position = surface->getTexCoord(textureMappingJoints.size()-1) * textureSize;
updateTextureHitarea();
}
void ofxSurfaceGui::addNumTextureMappingJoints(int num)
{
for ( int i=0; i<num; i++ ) {
addTextureMappingJoint();
}
}
void ofxSurfaceGui::dragTextureArea()
{
bTextureDragging = true;
}
void ofxSurfaceGui::dragProjectionArea()
{
bProjectionDragging = true;
}
void ofxSurfaceGui::stopDrag()
{
bTextureDragging = false;
bProjectionDragging = false;
}
bool ofxSurfaceGui::isProjectionMappingJointSelected()
{
return bProjectionMappingJointSelected;
}
bool ofxSurfaceGui::isTextureMappingJointSelected()
{
return bTextureMappingJointSelected;
}
bool ofxSurfaceGui::projectionAreaExists()
{
if ( projectionHitarea.size() > 2 ) {
return true;
} else {
return false;
}
}
bool ofxSurfaceGui::textureAreaExists()
{
if ( textureHitarea.size() > 2 ) {
return true;
} else {
return false;
}
}

71
src/ofxSurfaceGui.h

@ -1,71 +0,0 @@
#ifndef H_OFX_SURFACE_GUI
#define H_OFX_SURFACE_GUI
#include "ofMain.h"
#include "ofxTriangleSurface.h"
#include "ofxCircleJoint.h"
class ofxSurfaceGui
{
public:
ofxSurfaceGui();
~ofxSurfaceGui();
enum editMode {
NONE,
TEXTURE_MAPPING,
PROJECTION_MAPPING
};
void setup(ofxTriangleSurface& surfaceForGui);
void update();
void draw();
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseDragged(int x, int y, int button);
void setMode(editMode newMode);
void select();
void unselect();
void updateTextureHitarea();
void updateProjectionHitarea();
void updateHitarea(); // update both
void updateJoints();
bool hitTest(float x, float y);
bool hitTestTextureArea(float x, float y);
bool hitTestProjectionArea(float x, float y);
bool isSelected();
editMode getMode();
private:
editMode mode;
ofxTriangleSurface* surface;
ofPolyline textureHitarea;
ofPolyline projectionHitarea;
ofVec2f clickPosition;
vector<ofxCircleJoint> projectionMappingJoints;
vector<ofxCircleJoint> textureMappingJoints;
bool bTextureMappingJointSelected;
bool bProjectionMappingJointSelected;
bool bTextureDragging;
bool bProjectionDragging;
bool bSelected;
bool isProjectionMappingJointSelected();
bool isTextureMappingJointSelected();
bool projectionAreaExists();
bool textureAreaExists();
void addProjectionMappingJoint();
void addNumProjectionMappingJoints(int num);
void addTextureMappingJoint();
void addNumTextureMappingJoints(int num);
void dragTextureArea();
void dragProjectionArea();
void stopDrag();
};
#endif

18
src/ofxSurfaceManagerGui.cpp

@ -74,8 +74,17 @@ void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args)
} else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) {
return;
} else if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) {
// attempt to select surface, loop from end to beginning
bool bSurfaceSelected = false;
ofxCircleJoint* hitJoint = projectionEditor.hitTestJoints(ofVec2f(args.x, args.y));
if ( hitJoint != NULL ) {
hitJoint->startDrag();
bSurfaceSelected = true;
}
// attempt to select surface, loop from end to beginning
if ( !bSurfaceSelected ){
for ( int i=surfaceManager->size()-1; i>=0; i-- ) {
if ( surfaceManager->getSurface(i)->hitTest( ofVec2f(args.x, args.y) ) ) {
projectionEditor.clearJoints();
@ -85,11 +94,9 @@ void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args)
break;
}
}
}
// Check if hitting one of the joints as that also counts as hit when surface is selected
if ( projectionEditor.hitTestJoints(ofVec2f(args.x, args.y)) ) {
bSurfaceSelected = true;
} else if ( bSurfaceSelected ) {
if ( bSurfaceSelected && hitJoint == NULL ) {
// if not hitting the joints, start drag only if we have a selected surface
clickPosition = ofVec2f(args.x, args.y);
startDrag();
@ -106,6 +113,7 @@ void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args)
void ofxSurfaceManagerGui::mouseReleased(ofMouseEventArgs &args)
{
stopDrag();
projectionEditor.stopDragJoints();
}
void ofxSurfaceManagerGui::mouseDragged(ofMouseEventArgs &args)

Loading…
Cancel
Save