Browse Source

Add texture area dragging in gui texture edit mode

master
Krisjanis Rijnieks 11 years ago
parent
commit
f2b419c82e
  1. 2
      src/ofxBaseSurface.h
  2. 5
      src/ofxSurfaceManager.h
  3. 41
      src/ofxSurfaceManagerGui.cpp
  4. 1
      src/ofxSurfaceManagerGui.h
  5. 50
      src/ofxTextureEditor.cpp
  6. 7
      src/ofxTextureEditor.h
  7. 19
      src/ofxTriangleSurface.cpp
  8. 2
      src/ofxTriangleSurface.h

2
src/ofxBaseSurface.h

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

5
src/ofxSurfaceManager.h

@ -1,11 +1,6 @@
#ifndef H_OFX_SURFACE_MANAGER
#define H_OFX_SURFACE_MANAGER
/*
Used ofxStateMachine by Neil Mendoza as example for this part of the addon.
https://github.com/neilmendoza/ofxStateMachine
*/
#include "ofxBaseSurface.h"
#include "ofxTriangleSurface.h"
#include "ofxSurfaceType.h"

41
src/ofxSurfaceManagerGui.cpp

@ -47,9 +47,12 @@ void ofxSurfaceManagerGui::draw()
surfaceManager->draw();
ofPopStyle();
// hilight selected surface
// highlight selected surface
drawSelectedSurfaceHighlight();
// hilight selected surface texture
drawSelectedSurfaceTextureHighlight();
// draw texture editing GUI on top
textureEditor.draw();
@ -72,7 +75,15 @@ void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args)
if ( guiMode == ofxGuiMode::NONE ) {
return;
} else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) {
return;
if ( surfaceManager->getSelectedSurface() != NULL ) {
// hittest texture area to see if we are hitting the texture surface
if ( surfaceManager->getSelectedSurface()->getTextureHitArea().inside(args.x, args.y) ) {
clickPosition = ofVec2f(args.x, args.y);
startDrag();
}
}
} else if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) {
bool bSurfaceSelected = false;
@ -121,8 +132,13 @@ void ofxSurfaceManagerGui::mouseDragged(ofMouseEventArgs &args)
if (bDrag) {
ofVec2f mousePosition = ofVec2f(args.x, args.y);
ofVec2f distance = mousePosition - clickPosition;
// add this distance to all vertices in surface
projectionEditor.moveSelectedSurface(distance);
if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) {
// add this distance to all vertices in surface
projectionEditor.moveSelectedSurface(distance);
} else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) {
textureEditor.moveTexCoords(distance);
}
clickPosition = mousePosition;
}
}
@ -142,6 +158,9 @@ void ofxSurfaceManagerGui::setMode(int newGuiMode)
}
guiMode = newGuiMode;
// refresh texture editor surface reference
textureEditor.setSurface(surfaceManager->getSelectedSurface());
}
void ofxSurfaceManagerGui::drawSelectedSurfaceHighlight()
@ -157,6 +176,20 @@ void ofxSurfaceManagerGui::drawSelectedSurfaceHighlight()
ofPopStyle();
}
void ofxSurfaceManagerGui::drawSelectedSurfaceTextureHighlight()
{
if ( surfaceManager->getSelectedSurface() == NULL ) return;
ofPolyline line = surfaceManager->getSelectedSurface()->getTextureHitArea();
ofPushStyle();
ofSetLineWidth(1);
ofSetColor(255, 255, 0, 255);
line.draw();
ofPopStyle();
}
void ofxSurfaceManagerGui::startDrag()
{
bDrag = true;

1
src/ofxSurfaceManagerGui.h

@ -23,6 +23,7 @@ public:
void setSurfaceManager(ofxSurfaceManager* newSurfaceManager);
void setMode(int newGuiMode);
void drawSelectedSurfaceHighlight();
void drawSelectedSurfaceTextureHighlight();
void startDrag();
void stopDrag();

50
src/ofxTextureEditor.cpp

@ -5,11 +5,6 @@ ofxTextureEditor::ofxTextureEditor()
clear();
}
ofxTextureEditor::ofxTextureEditor(ofxBaseSurface* newSurface)
{
setSurface(newSurface);
}
ofxTextureEditor::~ofxTextureEditor()
{
clear();
@ -17,17 +12,58 @@ ofxTextureEditor::~ofxTextureEditor()
void ofxTextureEditor::draw()
{
if ( surface != NULL ) {
surface->drawTexture(ofVec2f(0.0f,0.0f));
if (surface == NULL) return;
drawJoints();
}
void ofxTextureEditor::drawJoints()
{
for ( int i=0; i<joints.size(); i++ ) {
joints[i]->draw();
}
}
void ofxTextureEditor::setSurface(ofxBaseSurface* newSurface)
{
surface = newSurface;
createJoints();
}
void ofxTextureEditor::clear()
{
surface = NULL;
clearJoints();
}
void ofxTextureEditor::createJoints()
{
if ( surface == NULL ) return;
clearJoints();
vector<ofVec2f>& texCoords = surface->getTexCoords();
ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(), surface->getTexture()->getHeight());
for ( int i=0; i<texCoords.size(); i++ ) {
joints.push_back(new ofxCircleJoint());
joints.back()->position = texCoords[i] * textureSize;
}
}
void ofxTextureEditor::clearJoints()
{
while ( joints.size() ) {
delete joints.back();
joints.pop_back();
}
}
void ofxTextureEditor::moveTexCoords(ofVec2f by)
{
if ( surface == NULL ) return;
vector<ofVec2f>& texCoords = surface->getTexCoords();
ofVec2f textureSize = ofVec2f( surface->getTexture()->getWidth(), surface->getTexture()->getHeight() );
for (int i=0; i<texCoords.size(); i++) {
joints[i]->position += by;
texCoords[i] = joints[i]->position / textureSize;
}
}

7
src/ofxTextureEditor.h

@ -2,20 +2,25 @@
#define H_OFX_TEXTURE_EDITOR
#include "ofxBaseSurface.h"
#include "ofxCircleJoint.h"
class ofxTextureEditor
{
public:
ofxTextureEditor();
ofxTextureEditor(ofxBaseSurface* newSurface);
~ofxTextureEditor();
void draw();
void drawJoints();
void setSurface(ofxBaseSurface* newSurface);
void clear();
void createJoints();
void clearJoints();
void moveTexCoords(ofVec2f by);
private:
ofxBaseSurface* surface;
vector<ofxBaseJoint*> joints;
};

19
src/ofxTriangleSurface.cpp

@ -120,8 +120,27 @@ ofPolyline ofxTriangleSurface::getHitArea()
return line;
}
ofPolyline ofxTriangleSurface::getTextureHitArea()
{
ofPolyline line;
vector<ofVec2f>& texCoords = mesh.getTexCoords();
ofVec2f textureSize = ofVec2f(texture->getWidth(), texture->getHeight());
for ( int i=0; i<texCoords.size(); i++ ) {
line.addVertex( ofPoint( texCoords[i] * textureSize ) );
}
line.close();
return line;
}
vector<ofVec3f>& ofxTriangleSurface::getVertices()
{
// return only joint vertices
return mesh.getVertices();
}
vector<ofVec2f>& ofxTriangleSurface::getTexCoords()
{
return mesh.getTexCoords();
}

2
src/ofxTriangleSurface.h

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