Browse Source

Add movable joints for texture edit mode

master
Krisjanis Rijnieks 11 years ago
parent
commit
90712b37db
  1. 11
      src/ofxSurfaceManagerGui.cpp
  2. 44
      src/ofxTextureEditor.cpp
  3. 9
      src/ofxTextureEditor.h

11
src/ofxSurfaceManagerGui.cpp

@ -76,7 +76,15 @@ void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args)
return; return;
} else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { } else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) {
if ( surfaceManager->getSelectedSurface() != NULL ) { bool bSurfaceSelected = false;
ofxCircleJoint* hitJoint = textureEditor.hitTestJoints(ofVec2f(args.x, args.y));
if ( hitJoint != NULL ) {
hitJoint->startDrag();
bSurfaceSelected = true;
}
if ( surfaceManager->getSelectedSurface() != NULL && !bSurfaceSelected ) {
// hittest texture area to see if we are hitting the texture surface // hittest texture area to see if we are hitting the texture surface
if ( surfaceManager->getSelectedSurface()->getTextureHitArea().inside(args.x, args.y) ) { if ( surfaceManager->getSelectedSurface()->getTextureHitArea().inside(args.x, args.y) ) {
clickPosition = ofVec2f(args.x, args.y); clickPosition = ofVec2f(args.x, args.y);
@ -125,6 +133,7 @@ void ofxSurfaceManagerGui::mouseReleased(ofMouseEventArgs &args)
{ {
stopDrag(); stopDrag();
projectionEditor.stopDragJoints(); projectionEditor.stopDragJoints();
textureEditor.stopDragJoints();
} }
void ofxSurfaceManagerGui::mouseDragged(ofMouseEventArgs &args) void ofxSurfaceManagerGui::mouseDragged(ofMouseEventArgs &args)

44
src/ofxTextureEditor.cpp

@ -3,11 +3,38 @@
ofxTextureEditor::ofxTextureEditor() ofxTextureEditor::ofxTextureEditor()
{ {
clear(); clear();
registerAppEvents();
} }
ofxTextureEditor::~ofxTextureEditor() ofxTextureEditor::~ofxTextureEditor()
{ {
clear(); clear();
unregisterAppEvents();
}
void ofxTextureEditor::registerAppEvents()
{
ofAddListener(ofEvents().update, this, &ofxTextureEditor::update);
}
void ofxTextureEditor::unregisterAppEvents()
{
ofRemoveListener(ofEvents().update, this, &ofxTextureEditor::update);
}
void ofxTextureEditor::update(ofEventArgs &args)
{
if ( surface == NULL ) return;
// update surface if one of the joints is being dragged
ofVec2f textureSize = ofVec2f( surface->getTexture()->getWidth(), surface->getTexture()->getHeight() );
for ( int i=0; i<joints.size(); i++ ) {
if ( joints[i]->isDragged() ) {
// update vertex to new location
surface->setTexCoord(i, joints[i]->position / textureSize);
break;
}
}
} }
void ofxTextureEditor::draw() void ofxTextureEditor::draw()
@ -66,4 +93,21 @@ void ofxTextureEditor::moveTexCoords(ofVec2f by)
joints[i]->position += by; joints[i]->position += by;
texCoords[i] = joints[i]->position / textureSize; texCoords[i] = joints[i]->position / textureSize;
} }
}
void ofxTextureEditor::stopDragJoints()
{
for (int i=0; i<joints.size(); i++){
joints[i]->stopDrag();
}
}
ofxCircleJoint* ofxTextureEditor::hitTestJoints(ofVec2f pos)
{
for ( int i=0; i<joints.size(); i++ ) {
if ( joints[i]->hitTest(pos) ){
return joints[i];
}
}
return NULL;
} }

9
src/ofxTextureEditor.h

@ -1,6 +1,7 @@
#ifndef H_OFX_TEXTURE_EDITOR #ifndef H_OFX_TEXTURE_EDITOR
#define H_OFX_TEXTURE_EDITOR #define H_OFX_TEXTURE_EDITOR
#include "ofEvents.h"
#include "ofxBaseSurface.h" #include "ofxBaseSurface.h"
#include "ofxCircleJoint.h" #include "ofxCircleJoint.h"
@ -10,6 +11,10 @@ public:
ofxTextureEditor(); ofxTextureEditor();
~ofxTextureEditor(); ~ofxTextureEditor();
void registerAppEvents();
void unregisterAppEvents();
void update(ofEventArgs& args);
void draw(); void draw();
void drawJoints(); void drawJoints();
void setSurface(ofxBaseSurface* newSurface); void setSurface(ofxBaseSurface* newSurface);
@ -17,10 +22,12 @@ public:
void createJoints(); void createJoints();
void clearJoints(); void clearJoints();
void moveTexCoords(ofVec2f by); void moveTexCoords(ofVec2f by);
void stopDragJoints();
ofxCircleJoint* hitTestJoints(ofVec2f pos);
private: private:
ofxBaseSurface* surface; ofxBaseSurface* surface;
vector<ofxBaseJoint*> joints; vector<ofxCircleJoint*> joints;
}; };

Loading…
Cancel
Save