|
|
@ -3,13 +3,13 @@ |
|
|
|
ofxTextureEditor::ofxTextureEditor() |
|
|
|
{ |
|
|
|
clear(); |
|
|
|
registerAppEvents(); |
|
|
|
enable(); |
|
|
|
} |
|
|
|
|
|
|
|
ofxTextureEditor::~ofxTextureEditor() |
|
|
|
{ |
|
|
|
clear(); |
|
|
|
unregisterAppEvents(); |
|
|
|
disable(); |
|
|
|
} |
|
|
|
|
|
|
|
void ofxTextureEditor::registerAppEvents() |
|
|
@ -22,6 +22,31 @@ void ofxTextureEditor::unregisterAppEvents() |
|
|
|
ofRemoveListener(ofEvents().update, this, &ofxTextureEditor::update); |
|
|
|
} |
|
|
|
|
|
|
|
void ofxTextureEditor::registerKeyEvents() |
|
|
|
{ |
|
|
|
ofAddListener(ofEvents().keyPressed, this, &ofxTextureEditor::keyPressed); |
|
|
|
ofAddListener(ofEvents().keyReleased, this, &ofxTextureEditor::keyReleased); |
|
|
|
} |
|
|
|
|
|
|
|
void ofxTextureEditor::unregisterKeyEvents() |
|
|
|
{ |
|
|
|
ofRemoveListener(ofEvents().keyPressed, this, &ofxTextureEditor::keyPressed); |
|
|
|
ofRemoveListener(ofEvents().keyReleased, this, &ofxTextureEditor::keyReleased); |
|
|
|
} |
|
|
|
|
|
|
|
void ofxTextureEditor::enable() |
|
|
|
{ |
|
|
|
registerAppEvents(); |
|
|
|
registerKeyEvents(); |
|
|
|
bShiftKeyDown = false; |
|
|
|
} |
|
|
|
|
|
|
|
void ofxTextureEditor::disable() |
|
|
|
{ |
|
|
|
unregisterAppEvents(); |
|
|
|
unregisterKeyEvents(); |
|
|
|
} |
|
|
|
|
|
|
|
void ofxTextureEditor::update(ofEventArgs &args) |
|
|
|
{ |
|
|
|
if ( surface == NULL ) return; |
|
|
@ -29,7 +54,7 @@ void ofxTextureEditor::update(ofEventArgs &args) |
|
|
|
// 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() ) { |
|
|
|
if ( joints[i]->isDragged() || joints[i]->isSelected() ) { |
|
|
|
// update vertex to new location
|
|
|
|
surface->setTexCoord(i, joints[i]->position / textureSize); |
|
|
|
break; |
|
|
@ -37,6 +62,32 @@ void ofxTextureEditor::update(ofEventArgs &args) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void ofxTextureEditor::keyPressed(ofKeyEventArgs &args) |
|
|
|
{ |
|
|
|
int key = args.key; |
|
|
|
float moveStep; |
|
|
|
|
|
|
|
if (bShiftKeyDown) moveStep = 10.0f; |
|
|
|
else moveStep = 0.5f; |
|
|
|
|
|
|
|
switch (key) { |
|
|
|
case OF_KEY_LEFT: moveSelection(ofVec2f(-moveStep,0.0f)); break; |
|
|
|
case OF_KEY_RIGHT: moveSelection(ofVec2f(moveStep,0.0f)); break; |
|
|
|
case OF_KEY_UP: moveSelection(ofVec2f(0.0f,-moveStep)); break; |
|
|
|
case OF_KEY_DOWN: moveSelection(ofVec2f(0.0f,moveStep)); break; |
|
|
|
case OF_KEY_SHIFT: bShiftKeyDown = true; break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void ofxTextureEditor::keyReleased(ofKeyEventArgs &args) |
|
|
|
{ |
|
|
|
int key = args.key; |
|
|
|
switch (key) { |
|
|
|
case OF_KEY_SHIFT: bShiftKeyDown = false; break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ofxTextureEditor::draw() |
|
|
|
{ |
|
|
|
if (surface == NULL) return; |
|
|
@ -109,6 +160,26 @@ void ofxTextureEditor::stopDragJoints() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void ofxTextureEditor::moveSelection(ofVec2f by) |
|
|
|
{ |
|
|
|
// check if joints selected
|
|
|
|
bool bJointSelected = false; |
|
|
|
ofxBaseJoint* selectedJoint; |
|
|
|
for ( int i=0; i<joints.size(); i++ ) { |
|
|
|
if (joints[i]->isSelected()) { |
|
|
|
bJointSelected = true; |
|
|
|
selectedJoint = joints[i]; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if ( bJointSelected ) { |
|
|
|
selectedJoint->position += by; |
|
|
|
} else { |
|
|
|
moveTexCoords(by); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
ofxCircleJoint* ofxTextureEditor::hitTestJoints(ofVec2f pos) |
|
|
|
{ |
|
|
|
for ( int i=0; i<joints.size(); i++ ) { |
|
|
|