8 changed files with 428 additions and 339 deletions
@ -0,0 +1,31 @@ |
|||||
|
#include "MoveSurfaceVertexCommand.h" |
||||
|
|
||||
|
namespace ofx{ |
||||
|
namespace piMapper{ |
||||
|
|
||||
|
MoveSurfaceVertexCommand::MoveSurfaceVertexCommand( |
||||
|
int vertIndex, |
||||
|
BaseSurface * surface, |
||||
|
ProjectionEditor * projectionEditor){ |
||||
|
|
||||
|
_vertIndex = vertIndex; |
||||
|
_surface = surface; |
||||
|
_projectionEditor = projectionEditor; |
||||
|
} |
||||
|
|
||||
|
void MoveSurfaceVertexCommand::exec(){ |
||||
|
ofLogNotice("MoveJointCommand", "exec"); |
||||
|
_prevVertPos = _surface->getVertices()[_vertIndex]; |
||||
|
} |
||||
|
|
||||
|
void MoveSurfaceVertexCommand::undo(){ |
||||
|
ofLogNotice("MoveJointCommand", "undo"); |
||||
|
_surface->setVertex(_vertIndex, _prevVertPos); |
||||
|
_projectionEditor->updateJoints(); |
||||
|
_projectionEditor = 0; |
||||
|
_surface = 0; |
||||
|
} |
||||
|
|
||||
|
} // namespace piMapper
|
||||
|
} // namespace ofx
|
||||
|
|
@ -0,0 +1,34 @@ |
|||||
|
// MoveSurfaceVertexCommand
|
||||
|
// Provides with option to undo move surface vertex operation.
|
||||
|
// Created by Krisjanis Rijnieks 2015-05-15
|
||||
|
|
||||
|
#pragma once |
||||
|
|
||||
|
#include "BaseCommand.h" |
||||
|
#include "BaseSurface.h" |
||||
|
#include "ProjectionEditor.h" |
||||
|
#include "BaseJoint.h" |
||||
|
|
||||
|
namespace ofx{ |
||||
|
namespace piMapper{ |
||||
|
|
||||
|
class MoveSurfaceVertexCommand : public BaseUndoableCommand{ |
||||
|
|
||||
|
public: |
||||
|
MoveSurfaceVertexCommand( |
||||
|
int vertIndex, |
||||
|
BaseSurface * surface, |
||||
|
ProjectionEditor * projectionEditor); |
||||
|
void exec(); |
||||
|
void undo(); |
||||
|
|
||||
|
private: |
||||
|
int _vertIndex; |
||||
|
ofVec2f _prevVertPos; |
||||
|
BaseSurface * _surface; |
||||
|
ProjectionEditor * _projectionEditor; |
||||
|
}; |
||||
|
|
||||
|
} // namespace piMapper
|
||||
|
} // namespace ofx
|
||||
|
|
@ -1,263 +1,267 @@ |
|||||
#include "ProjectionEditor.h" |
#include "ProjectionEditor.h" |
||||
|
|
||||
namespace ofx { |
namespace ofx { |
||||
namespace piMapper { |
namespace piMapper { |
||||
ProjectionEditor::ProjectionEditor() { |
ProjectionEditor::ProjectionEditor() { |
||||
surfaceManager = NULL; |
surfaceManager = NULL; |
||||
bShiftKeyDown = false; |
bShiftKeyDown = false; |
||||
fSnapDistance = 10.0f; |
fSnapDistance = 10.0f; |
||||
enable(); |
enable(); |
||||
} |
} |
||||
|
|
||||
ProjectionEditor::~ProjectionEditor() { |
ProjectionEditor::~ProjectionEditor() { |
||||
clearJoints(); |
clearJoints(); |
||||
surfaceManager = NULL; |
surfaceManager = NULL; |
||||
disable(); |
disable(); |
||||
} |
} |
||||
|
|
||||
void ProjectionEditor::registerAppEvents() { |
void ProjectionEditor::registerAppEvents() { |
||||
ofAddListener(ofEvents().update, this, &ProjectionEditor::update); |
ofAddListener(ofEvents().update, this, &ProjectionEditor::update); |
||||
ofAddListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage); |
ofAddListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage); |
||||
} |
} |
||||
|
|
||||
void ProjectionEditor::unregisterAppEvents() { |
void ProjectionEditor::unregisterAppEvents() { |
||||
ofRemoveListener(ofEvents().update, this, &ProjectionEditor::update); |
ofRemoveListener(ofEvents().update, this, &ProjectionEditor::update); |
||||
ofRemoveListener(ofEvents().messageEvent, this, |
ofRemoveListener(ofEvents().messageEvent, this, |
||||
&ProjectionEditor::gotMessage); |
&ProjectionEditor::gotMessage); |
||||
} |
} |
||||
|
|
||||
void ProjectionEditor::registerMouseEvents() { |
void ProjectionEditor::registerMouseEvents() { |
||||
ofAddListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged); |
ofAddListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged); |
||||
} |
} |
||||
|
|
||||
void ProjectionEditor::unregisterMouseEvents() { |
void ProjectionEditor::unregisterMouseEvents() { |
||||
ofRemoveListener(ofEvents().mouseDragged, this, |
ofRemoveListener(ofEvents().mouseDragged, this, |
||||
&ProjectionEditor::mouseDragged); |
&ProjectionEditor::mouseDragged); |
||||
} |
} |
||||
|
|
||||
void ProjectionEditor::registerKeyEvents() { |
void ProjectionEditor::registerKeyEvents() { |
||||
ofAddListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); |
ofAddListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); |
||||
ofAddListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased); |
ofAddListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased); |
||||
} |
} |
||||
|
|
||||
void ProjectionEditor::unregisterKeyEvents() { |
void ProjectionEditor::unregisterKeyEvents() { |
||||
ofRemoveListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); |
ofRemoveListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); |
||||
ofRemoveListener(ofEvents().keyReleased, this, |
ofRemoveListener(ofEvents().keyReleased, this, |
||||
&ProjectionEditor::keyReleased); |
&ProjectionEditor::keyReleased); |
||||
} |
} |
||||
|
|
||||
void ProjectionEditor::enable() { |
void ProjectionEditor::enable() { |
||||
registerAppEvents(); |
registerAppEvents(); |
||||
registerMouseEvents(); |
registerMouseEvents(); |
||||
registerKeyEvents(); |
registerKeyEvents(); |
||||
} |
} |
||||
|
|
||||
void ProjectionEditor::disable() { |
void ProjectionEditor::disable() { |
||||
unregisterAppEvents(); |
unregisterAppEvents(); |
||||
unregisterMouseEvents(); |
unregisterMouseEvents(); |
||||
unregisterKeyEvents(); |
unregisterKeyEvents(); |
||||
} |
} |
||||
|
|
||||
void ProjectionEditor::update(ofEventArgs& args) { |
void ProjectionEditor::update(ofEventArgs& args) { |
||||
// update surface if one of the joints is being dragged
|
// update surface if one of the joints is being dragged
|
||||
for (int i = 0; i < joints.size(); i++) { |
for (int i = 0; i < joints.size(); i++) { |
||||
if (joints[i]->isDragged() || joints[i]->isSelected()) { |
if (joints[i]->isDragged() || joints[i]->isSelected()) { |
||||
if (surfaceManager->getSelectedSurface() != NULL) { |
if (surfaceManager->getSelectedSurface() != NULL) { |
||||
// update vertex to new location
|
// update vertex to new location
|
||||
surfaceManager->getSelectedSurface()->setVertex(i, joints[i]->position); |
surfaceManager->getSelectedSurface()->setVertex(i, joints[i]->position); |
||||
} else { |
} else { |
||||
// clear joints if there is no surface selected
|
// clear joints if there is no surface selected
|
||||
// as the remove selected surface in the surface manager
|
// as the remove selected surface in the surface manager
|
||||
// is not supposed to access joints here
|
// is not supposed to access joints here
|
||||
joints.clear(); |
joints.clear(); |
||||
} |
} |
||||
break; |
break; |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
void ProjectionEditor::draw() { |
void ProjectionEditor::draw() { |
||||
if (surfaceManager == NULL) return; |
if (surfaceManager == NULL) return; |
||||
if (surfaceManager->getSelectedSurface() == NULL) return; |
if (surfaceManager->getSelectedSurface() == NULL) return; |
||||
if (joints.size() <= 0) createJoints(); |
if (joints.size() <= 0) createJoints(); |
||||
drawJoints(); |
drawJoints(); |
||||
} |
} |
||||
|
|
||||
void ProjectionEditor::mouseDragged(ofMouseEventArgs& args) { |
void ProjectionEditor::mouseDragged(ofMouseEventArgs& args) { |
||||
ofVec2f mousePosition = ofVec2f(args.x, args.y); |
ofVec2f mousePosition = ofVec2f(args.x, args.y); |
||||
|
|
||||
// Collect all vertices of the projection surfaces
|
// Collect all vertices of the projection surfaces
|
||||
vector<ofVec3f*> allVertices; |
vector<ofVec3f*> allVertices; |
||||
for (int i = 0; i < surfaceManager->size(); i++) { |
for (int i = 0; i < surfaceManager->size(); i++) { |
||||
BaseSurface* surface = surfaceManager->getSurface(i); |
BaseSurface* surface = surfaceManager->getSurface(i); |
||||
if (surface == surfaceManager->getSelectedSurface()) { |
if (surface == surfaceManager->getSelectedSurface()) { |
||||
continue; // Don't add vertices of selected surface
|
continue; // Don't add vertices of selected surface
|
||||
} |
} |
||||
for (int j = 0; j < surface->getVertices().size(); j++) { |
for (int j = 0; j < surface->getVertices().size(); j++) { |
||||
allVertices.push_back(&surface->getVertices()[j]); |
allVertices.push_back(&surface->getVertices()[j]); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
// Snap currently dragged joint to nearest vertex
|
// Snap currently dragged joint to nearest vertex
|
||||
for (int i = 0; i < joints.size(); i++) { |
for (int i = 0; i < joints.size(); i++) { |
||||
if (joints[i]->isDragged()) { |
if (joints[i]->isDragged()) { |
||||
// Snap it!
|
// Snap it!
|
||||
for (int j = 0; j < allVertices.size(); j++) { |
for (int j = 0; j < allVertices.size(); j++) { |
||||
float distance = mousePosition.distance(*allVertices[j]); |
float distance = mousePosition.distance(*allVertices[j]); |
||||
// cout << "distance: " << distance << endl;
|
// cout << "distance: " << distance << endl;
|
||||
if (distance < fSnapDistance) { |
if (distance < fSnapDistance) { |
||||
joints[i]->position = *allVertices[j]; |
joints[i]->position = *allVertices[j]; |
||||
ofVec2f clickDistance = joints[i]->position - ofVec2f(args.x, args.y); |
ofVec2f clickDistance = joints[i]->position - ofVec2f(args.x, args.y); |
||||
joints[i]->setClickDistance(clickDistance); |
joints[i]->setClickDistance(clickDistance); |
||||
break; |
break; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ProjectionEditor::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 ProjectionEditor::keyReleased(ofKeyEventArgs& args) { |
||||
|
int key = args.key; |
||||
|
switch (key) { |
||||
|
case OF_KEY_SHIFT: |
||||
|
bShiftKeyDown = false; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ProjectionEditor::gotMessage(ofMessage& msg) { |
||||
|
if (msg.message == "surfaceSelected") { |
||||
|
// refresh gui
|
||||
|
clearJoints(); |
||||
|
createJoints(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ProjectionEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) { |
||||
|
surfaceManager = newSurfaceManager; |
||||
|
} |
||||
|
|
||||
|
void ProjectionEditor::clearJoints() { |
||||
|
while (joints.size()) { |
||||
|
delete joints.back(); |
||||
|
joints.pop_back(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ProjectionEditor::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 CircleJoint()); |
||||
|
joints.back()->position = ofVec2f(vertices[i].x, vertices[i].y); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ProjectionEditor::updateJoints() { |
||||
|
vector<ofVec3f>& vertices = |
||||
|
surfaceManager->getSelectedSurface()->getVertices(); |
||||
|
for (int i = 0; i < vertices.size(); i++) { |
||||
|
joints[i]->position = ofVec2f(vertices[i].x, vertices[i].y); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ProjectionEditor::unselectAllJoints() { |
||||
|
for (int i = 0; i < joints.size(); i++) { |
||||
|
joints[i]->unselect(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ProjectionEditor::moveSelectedSurface(ofVec2f by) { |
||||
|
if (surfaceManager == NULL) return; |
||||
|
if (surfaceManager->getSelectedSurface() == NULL) return; |
||||
|
surfaceManager->getSelectedSurface()->moveBy(by); |
||||
|
/*vector<ofVec3f>& vertices =
|
||||
|
surfaceManager->getSelectedSurface()->getVertices(); |
||||
|
for (int i=0; i<vertices.size(); i++) { |
||||
|
vertices[i] += by; |
||||
|
}*/ |
||||
|
updateJoints(); |
||||
|
} |
||||
|
|
||||
|
void ProjectionEditor::stopDragJoints() { |
||||
|
for (int i = 0; i < joints.size(); i++) { |
||||
|
joints[i]->stopDrag(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ProjectionEditor::moveSelection(ofVec2f by) { |
||||
|
// check if joints selected
|
||||
|
bool bJointSelected = false; |
||||
|
BaseJoint* 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 { |
||||
|
moveSelectedSurface(by); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ProjectionEditor::setSnapDistance(float newSnapDistance) { |
||||
|
fSnapDistance = newSnapDistance; |
||||
|
} |
||||
|
|
||||
|
CircleJoint* ProjectionEditor::hitTestJoints(ofVec2f pos) { |
||||
|
for (int i = 0; i < joints.size(); i++) { |
||||
|
if (joints[i]->hitTest(pos)) { |
||||
|
return joints[i]; |
||||
|
} |
||||
|
} |
||||
|
return NULL; |
||||
|
} |
||||
|
|
||||
|
vector<CircleJoint *> * ProjectionEditor::getJoints(){ |
||||
|
return &joints; |
||||
|
} |
||||
|
|
||||
|
void ProjectionEditor::drawJoints() { |
||||
|
for (int i = 0; i < joints.size(); i++) { |
||||
|
joints[i]->draw(); |
||||
|
} |
||||
} |
} |
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
void ProjectionEditor::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 ProjectionEditor::keyReleased(ofKeyEventArgs& args) { |
|
||||
int key = args.key; |
|
||||
switch (key) { |
|
||||
case OF_KEY_SHIFT: |
|
||||
bShiftKeyDown = false; |
|
||||
break; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
void ProjectionEditor::gotMessage(ofMessage& msg) { |
|
||||
if (msg.message == "surfaceSelected") { |
|
||||
// refresh gui
|
|
||||
clearJoints(); |
|
||||
createJoints(); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
void ProjectionEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) { |
|
||||
surfaceManager = newSurfaceManager; |
|
||||
} |
|
||||
|
|
||||
void ProjectionEditor::clearJoints() { |
|
||||
while (joints.size()) { |
|
||||
delete joints.back(); |
|
||||
joints.pop_back(); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
void ProjectionEditor::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 CircleJoint()); |
|
||||
joints.back()->position = ofVec2f(vertices[i].x, vertices[i].y); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
void ProjectionEditor::updateJoints() { |
|
||||
vector<ofVec3f>& vertices = |
|
||||
surfaceManager->getSelectedSurface()->getVertices(); |
|
||||
for (int i = 0; i < vertices.size(); i++) { |
|
||||
joints[i]->position = ofVec2f(vertices[i].x, vertices[i].y); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
void ProjectionEditor::unselectAllJoints() { |
|
||||
for (int i = 0; i < joints.size(); i++) { |
|
||||
joints[i]->unselect(); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
void ProjectionEditor::moveSelectedSurface(ofVec2f by) { |
|
||||
if (surfaceManager == NULL) return; |
|
||||
if (surfaceManager->getSelectedSurface() == NULL) return; |
|
||||
surfaceManager->getSelectedSurface()->moveBy(by); |
|
||||
/*vector<ofVec3f>& vertices =
|
|
||||
surfaceManager->getSelectedSurface()->getVertices(); |
|
||||
for (int i=0; i<vertices.size(); i++) { |
|
||||
vertices[i] += by; |
|
||||
}*/ |
|
||||
updateJoints(); |
|
||||
} |
|
||||
|
|
||||
void ProjectionEditor::stopDragJoints() { |
|
||||
for (int i = 0; i < joints.size(); i++) { |
|
||||
joints[i]->stopDrag(); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
void ProjectionEditor::moveSelection(ofVec2f by) { |
|
||||
// check if joints selected
|
|
||||
bool bJointSelected = false; |
|
||||
BaseJoint* 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 { |
|
||||
moveSelectedSurface(by); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
void ProjectionEditor::setSnapDistance(float newSnapDistance) { |
|
||||
fSnapDistance = newSnapDistance; |
|
||||
} |
|
||||
|
|
||||
CircleJoint* ProjectionEditor::hitTestJoints(ofVec2f pos) { |
|
||||
for (int i = 0; i < joints.size(); i++) { |
|
||||
if (joints[i]->hitTest(pos)) { |
|
||||
return joints[i]; |
|
||||
} |
} |
||||
} |
|
||||
return NULL; |
|
||||
} |
|
||||
|
|
||||
void ProjectionEditor::drawJoints() { |
|
||||
for (int i = 0; i < joints.size(); i++) { |
|
||||
joints[i]->draw(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
} |
Loading…
Reference in new issue