From ce6588d2960244d5ff9018ffb64f0598cf1a2f32 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Fri, 15 May 2015 01:32:33 +0300 Subject: [PATCH] Add undo operation with MoveSurfaceVertexCommand --- example/example.xcodeproj/project.pbxproj | 6 + src/Commands/MoveSurfaceCommand.cpp | 2 + src/Commands/MoveSurfaceVertexCommand.cpp | 31 ++ src/Commands/MoveSurfaceVertexCommand.h | 34 ++ src/Surfaces/SurfaceManagerGui.cpp | 16 +- src/Surfaces/SurfaceManagerGui.h | 73 +-- src/UserInterface/ProjectionEditor.cpp | 518 +++++++++++----------- src/UserInterface/ProjectionEditor.h | 87 ++-- 8 files changed, 428 insertions(+), 339 deletions(-) create mode 100644 src/Commands/MoveSurfaceVertexCommand.cpp create mode 100644 src/Commands/MoveSurfaceVertexCommand.h diff --git a/example/example.xcodeproj/project.pbxproj b/example/example.xcodeproj/project.pbxproj index 9f23536..4d7c1a7 100644 --- a/example/example.xcodeproj/project.pbxproj +++ b/example/example.xcodeproj/project.pbxproj @@ -25,6 +25,7 @@ 39A9AAE01B04E78600AA83BC /* RemoveSurfaceCommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39A9AADE1B04E78600AA83BC /* RemoveSurfaceCommand.cpp */; }; 39A9AAE91B0518FC00AA83BC /* MoveSurfaceCommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39A9AAE71B0518FC00AA83BC /* MoveSurfaceCommand.cpp */; }; 39A9AAEC1B053B4200AA83BC /* SelectSurfaceCommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39A9AAEA1B053B4200AA83BC /* SelectSurfaceCommand.cpp */; }; + 39A9AAF21B054FC300AA83BC /* MoveSurfaceVertexCommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39A9AAF01B054FC300AA83BC /* MoveSurfaceVertexCommand.cpp */; }; 39C1243319EE9589005DF557 /* lz4.c in Sources */ = {isa = PBXBuildFile; fileRef = 39C123EA19EE9589005DF557 /* lz4.c */; }; 39C1243419EE9589005DF557 /* Base64Encoding.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39C1241219EE9589005DF557 /* Base64Encoding.cpp */; }; 39C1243519EE9589005DF557 /* ByteBuffer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39C1241319EE9589005DF557 /* ByteBuffer.cpp */; }; @@ -156,6 +157,8 @@ 39A9AAE81B0518FC00AA83BC /* MoveSurfaceCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MoveSurfaceCommand.h; path = Commands/MoveSurfaceCommand.h; sourceTree = ""; }; 39A9AAEA1B053B4200AA83BC /* SelectSurfaceCommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SelectSurfaceCommand.cpp; path = Commands/SelectSurfaceCommand.cpp; sourceTree = ""; }; 39A9AAEB1B053B4200AA83BC /* SelectSurfaceCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SelectSurfaceCommand.h; path = Commands/SelectSurfaceCommand.h; sourceTree = ""; }; + 39A9AAF01B054FC300AA83BC /* MoveSurfaceVertexCommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = MoveSurfaceVertexCommand.cpp; path = Commands/MoveSurfaceVertexCommand.cpp; sourceTree = ""; }; + 39A9AAF11B054FC300AA83BC /* MoveSurfaceVertexCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MoveSurfaceVertexCommand.h; path = Commands/MoveSurfaceVertexCommand.h; sourceTree = ""; }; 39C123E719EE9589005DF557 /* alphanum.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = alphanum.hpp; sourceTree = ""; }; 39C123EA19EE9589005DF557 /* lz4.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = lz4.c; sourceTree = ""; }; 39C123EB19EE9589005DF557 /* lz4.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lz4.h; sourceTree = ""; }; @@ -651,6 +654,8 @@ 39FDD9E81AC0076200262205 /* Commands */ = { isa = PBXGroup; children = ( + 39A9AAF01B054FC300AA83BC /* MoveSurfaceVertexCommand.cpp */, + 39A9AAF11B054FC300AA83BC /* MoveSurfaceVertexCommand.h */, 39A9AAEA1B053B4200AA83BC /* SelectSurfaceCommand.cpp */, 39A9AAEB1B053B4200AA83BC /* SelectSurfaceCommand.h */, 39A9AAE71B0518FC00AA83BC /* MoveSurfaceCommand.cpp */, @@ -877,6 +882,7 @@ 39C1243919EE9589005DF557 /* COBSEncoding.cpp in Sources */, 39C1244919EE9589005DF557 /* snappy-sinksource.cc in Sources */, 39C1248819F1EB75005DF557 /* SurfaceManager.cpp in Sources */, + 39A9AAF21B054FC300AA83BC /* MoveSurfaceVertexCommand.cpp in Sources */, 39C1244319EE9589005DF557 /* LinkFilter.cpp in Sources */, 39C1245919F086A9005DF557 /* BaseSource.cpp in Sources */, 39C1244019EE9589005DF557 /* FileExtensionFilter.cpp in Sources */, diff --git a/src/Commands/MoveSurfaceCommand.cpp b/src/Commands/MoveSurfaceCommand.cpp index afafe04..8a53549 100644 --- a/src/Commands/MoveSurfaceCommand.cpp +++ b/src/Commands/MoveSurfaceCommand.cpp @@ -12,11 +12,13 @@ namespace ofx{ } void MoveSurfaceCommand::exec(){ + ofLogNotice("MoveSurfaceCommand", "exec"); _previousVertices = _surface->getVertices(); _surface->setMoved(false); } void MoveSurfaceCommand::undo(){ + ofLogNotice("MoveSurfaceCommand", "undo"); _surface->moveBy(_previousVertices[0] - _surface->getVertices()[0]); _projectionEditor->updateJoints(); _previousVertices.clear(); diff --git a/src/Commands/MoveSurfaceVertexCommand.cpp b/src/Commands/MoveSurfaceVertexCommand.cpp new file mode 100644 index 0000000..de48986 --- /dev/null +++ b/src/Commands/MoveSurfaceVertexCommand.cpp @@ -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 + diff --git a/src/Commands/MoveSurfaceVertexCommand.h b/src/Commands/MoveSurfaceVertexCommand.h new file mode 100644 index 0000000..204e255 --- /dev/null +++ b/src/Commands/MoveSurfaceVertexCommand.h @@ -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 + diff --git a/src/Surfaces/SurfaceManagerGui.cpp b/src/Surfaces/SurfaceManagerGui.cpp index 473f44f..677cd53 100644 --- a/src/Surfaces/SurfaceManagerGui.cpp +++ b/src/Surfaces/SurfaceManagerGui.cpp @@ -87,9 +87,8 @@ namespace ofx { return; } else if (guiMode == GuiMode::TEXTURE_MAPPING) { bool bSurfaceSelected = false; - CircleJoint* hitJoint = - textureEditor.hitTestJoints(ofVec2f(args.x, args.y)); + textureEditor.hitTestJoints(ofVec2f(args.x, args.y)); if (hitJoint != NULL) { textureEditor.unselectAllJoints(); hitJoint->select(); @@ -112,11 +111,22 @@ namespace ofx { bool bSurfaceSelected = false; CircleJoint* hitJoint = - projectionEditor.hitTestJoints(ofVec2f(args.x, args.y)); + projectionEditor.hitTestJoints(ofVec2f(args.x, args.y)); if (hitJoint != NULL) { projectionEditor.unselectAllJoints(); hitJoint->select(); hitJoint->startDrag(); + int jointVertIndex = 0; + for (auto i = 0; i < projectionEditor.getJoints()->size(); i++) { + if ((*projectionEditor.getJoints())[i] == hitJoint) { + jointVertIndex = i; + break; + } + } + _commandManager->exec(new MoveSurfaceVertexCommand( + jointVertIndex, + surfaceManager->getSelectedSurface(), + &projectionEditor)); bSurfaceSelected = true; } diff --git a/src/Surfaces/SurfaceManagerGui.h b/src/Surfaces/SurfaceManagerGui.h index 565d05b..758a2d8 100644 --- a/src/Surfaces/SurfaceManagerGui.h +++ b/src/Surfaces/SurfaceManagerGui.h @@ -14,42 +14,43 @@ #include "CommandManager.h" #include "MoveSurfaceCommand.h" #include "SelectSurfaceCommand.h" +#include "MoveSurfaceVertexCommand.h" namespace ofx { -namespace piMapper { -class SurfaceManagerGui { - public: - SurfaceManagerGui(); - ~SurfaceManagerGui(); - - void registerMouseEvents(); - void unregisterMouseEvents(); - - void draw(); - void mousePressed(ofMouseEventArgs& args); - void mouseReleased(ofMouseEventArgs& args); - void mouseDragged(ofMouseEventArgs& args); - - void setSurfaceManager(SurfaceManager* newSurfaceManager); - void setMediaServer(MediaServer* newMediaServer); - void setCommandManager(CommandManager * commandManager); - - void setMode(int newGuiMode); - void drawSelectedSurfaceHighlight(); - void drawSelectedSurfaceTextureHighlight(); - void startDrag(); - void stopDrag(); - - private: - SurfaceManager* surfaceManager; - MediaServer* mediaServer; - TextureEditor textureEditor; - ProjectionEditor projectionEditor; - SourcesEditor sourcesEditor; - int guiMode; - bool bDrag; - ofVec2f clickPosition; - CommandManager * _commandManager; -}; -} + namespace piMapper { + class SurfaceManagerGui { + public: + SurfaceManagerGui(); + ~SurfaceManagerGui(); + + void registerMouseEvents(); + void unregisterMouseEvents(); + + void draw(); + void mousePressed(ofMouseEventArgs& args); + void mouseReleased(ofMouseEventArgs& args); + void mouseDragged(ofMouseEventArgs& args); + + void setSurfaceManager(SurfaceManager* newSurfaceManager); + void setMediaServer(MediaServer* newMediaServer); + void setCommandManager(CommandManager * commandManager); + + void setMode(int newGuiMode); + void drawSelectedSurfaceHighlight(); + void drawSelectedSurfaceTextureHighlight(); + void startDrag(); + void stopDrag(); + + private: + SurfaceManager* surfaceManager; + MediaServer* mediaServer; + TextureEditor textureEditor; + ProjectionEditor projectionEditor; + SourcesEditor sourcesEditor; + int guiMode; + bool bDrag; + ofVec2f clickPosition; + CommandManager * _commandManager; + }; + } } \ No newline at end of file diff --git a/src/UserInterface/ProjectionEditor.cpp b/src/UserInterface/ProjectionEditor.cpp index 1851cd2..b8647d3 100644 --- a/src/UserInterface/ProjectionEditor.cpp +++ b/src/UserInterface/ProjectionEditor.cpp @@ -1,263 +1,267 @@ #include "ProjectionEditor.h" namespace ofx { -namespace piMapper { -ProjectionEditor::ProjectionEditor() { - surfaceManager = NULL; - bShiftKeyDown = false; - fSnapDistance = 10.0f; - enable(); -} - -ProjectionEditor::~ProjectionEditor() { - clearJoints(); - surfaceManager = NULL; - disable(); -} - -void ProjectionEditor::registerAppEvents() { - ofAddListener(ofEvents().update, this, &ProjectionEditor::update); - ofAddListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage); -} - -void ProjectionEditor::unregisterAppEvents() { - ofRemoveListener(ofEvents().update, this, &ProjectionEditor::update); - ofRemoveListener(ofEvents().messageEvent, this, - &ProjectionEditor::gotMessage); -} - -void ProjectionEditor::registerMouseEvents() { - ofAddListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged); -} - -void ProjectionEditor::unregisterMouseEvents() { - ofRemoveListener(ofEvents().mouseDragged, this, - &ProjectionEditor::mouseDragged); -} - -void ProjectionEditor::registerKeyEvents() { - ofAddListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); - ofAddListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased); -} - -void ProjectionEditor::unregisterKeyEvents() { - ofRemoveListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); - ofRemoveListener(ofEvents().keyReleased, this, - &ProjectionEditor::keyReleased); -} - -void ProjectionEditor::enable() { - registerAppEvents(); - registerMouseEvents(); - registerKeyEvents(); -} - -void ProjectionEditor::disable() { - unregisterAppEvents(); - unregisterMouseEvents(); - unregisterKeyEvents(); -} - -void ProjectionEditor::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() || joints[i]->isSelected()) { - if (surfaceManager->getSelectedSurface() != NULL) { - // update vertex to new location - surfaceManager->getSelectedSurface()->setVertex(i, joints[i]->position); - } else { - // clear joints if there is no surface selected - // as the remove selected surface in the surface manager - // is not supposed to access joints here - joints.clear(); - } - break; - } - } -} - -void ProjectionEditor::draw() { - if (surfaceManager == NULL) return; - if (surfaceManager->getSelectedSurface() == NULL) return; - if (joints.size() <= 0) createJoints(); - drawJoints(); -} - -void ProjectionEditor::mouseDragged(ofMouseEventArgs& args) { - ofVec2f mousePosition = ofVec2f(args.x, args.y); - - // Collect all vertices of the projection surfaces - vector allVertices; - for (int i = 0; i < surfaceManager->size(); i++) { - BaseSurface* surface = surfaceManager->getSurface(i); - if (surface == surfaceManager->getSelectedSurface()) { - continue; // Don't add vertices of selected surface - } - for (int j = 0; j < surface->getVertices().size(); j++) { - allVertices.push_back(&surface->getVertices()[j]); - } - } - - // Snap currently dragged joint to nearest vertex - for (int i = 0; i < joints.size(); i++) { - if (joints[i]->isDragged()) { - // Snap it! - for (int j = 0; j < allVertices.size(); j++) { - float distance = mousePosition.distance(*allVertices[j]); - // cout << "distance: " << distance << endl; - if (distance < fSnapDistance) { - joints[i]->position = *allVertices[j]; - ofVec2f clickDistance = joints[i]->position - ofVec2f(args.x, args.y); - joints[i]->setClickDistance(clickDistance); - break; + namespace piMapper { + ProjectionEditor::ProjectionEditor() { + surfaceManager = NULL; + bShiftKeyDown = false; + fSnapDistance = 10.0f; + enable(); + } + + ProjectionEditor::~ProjectionEditor() { + clearJoints(); + surfaceManager = NULL; + disable(); + } + + void ProjectionEditor::registerAppEvents() { + ofAddListener(ofEvents().update, this, &ProjectionEditor::update); + ofAddListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage); + } + + void ProjectionEditor::unregisterAppEvents() { + ofRemoveListener(ofEvents().update, this, &ProjectionEditor::update); + ofRemoveListener(ofEvents().messageEvent, this, + &ProjectionEditor::gotMessage); + } + + void ProjectionEditor::registerMouseEvents() { + ofAddListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged); + } + + void ProjectionEditor::unregisterMouseEvents() { + ofRemoveListener(ofEvents().mouseDragged, this, + &ProjectionEditor::mouseDragged); + } + + void ProjectionEditor::registerKeyEvents() { + ofAddListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); + ofAddListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased); + } + + void ProjectionEditor::unregisterKeyEvents() { + ofRemoveListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); + ofRemoveListener(ofEvents().keyReleased, this, + &ProjectionEditor::keyReleased); + } + + void ProjectionEditor::enable() { + registerAppEvents(); + registerMouseEvents(); + registerKeyEvents(); + } + + void ProjectionEditor::disable() { + unregisterAppEvents(); + unregisterMouseEvents(); + unregisterKeyEvents(); + } + + void ProjectionEditor::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() || joints[i]->isSelected()) { + if (surfaceManager->getSelectedSurface() != NULL) { + // update vertex to new location + surfaceManager->getSelectedSurface()->setVertex(i, joints[i]->position); + } else { + // clear joints if there is no surface selected + // as the remove selected surface in the surface manager + // is not supposed to access joints here + joints.clear(); + } + break; + } + } + } + + void ProjectionEditor::draw() { + if (surfaceManager == NULL) return; + if (surfaceManager->getSelectedSurface() == NULL) return; + if (joints.size() <= 0) createJoints(); + drawJoints(); + } + + void ProjectionEditor::mouseDragged(ofMouseEventArgs& args) { + ofVec2f mousePosition = ofVec2f(args.x, args.y); + + // Collect all vertices of the projection surfaces + vector allVertices; + for (int i = 0; i < surfaceManager->size(); i++) { + BaseSurface* surface = surfaceManager->getSurface(i); + if (surface == surfaceManager->getSelectedSurface()) { + continue; // Don't add vertices of selected surface + } + for (int j = 0; j < surface->getVertices().size(); j++) { + allVertices.push_back(&surface->getVertices()[j]); + } + } + + // Snap currently dragged joint to nearest vertex + for (int i = 0; i < joints.size(); i++) { + if (joints[i]->isDragged()) { + // Snap it! + for (int j = 0; j < allVertices.size(); j++) { + float distance = mousePosition.distance(*allVertices[j]); + // cout << "distance: " << distance << endl; + if (distance < fSnapDistance) { + joints[i]->position = *allVertices[j]; + ofVec2f clickDistance = joints[i]->position - ofVec2f(args.x, args.y); + joints[i]->setClickDistance(clickDistance); + 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& 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& 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& vertices = + surfaceManager->getSelectedSurface()->getVertices(); + for (int i=0; istopDrag(); + } + } + + 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 * 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& 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& 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& vertices = - surfaceManager->getSelectedSurface()->getVertices(); - for (int i=0; istopDrag(); - } -} - -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(); - } -} -} } \ No newline at end of file diff --git a/src/UserInterface/ProjectionEditor.h b/src/UserInterface/ProjectionEditor.h index 50b8582..ec59cef 100755 --- a/src/UserInterface/ProjectionEditor.h +++ b/src/UserInterface/ProjectionEditor.h @@ -4,47 +4,48 @@ #include "CircleJoint.h" namespace ofx { -namespace piMapper { -class ProjectionEditor { - public: - ProjectionEditor(); - ~ProjectionEditor(); - - void registerAppEvents(); - void unregisterAppEvents(); - void registerMouseEvents(); - void unregisterMouseEvents(); - void registerKeyEvents(); - void unregisterKeyEvents(); - - void enable(); - void disable(); - - void update(ofEventArgs& args); - void draw(); - void mouseDragged(ofMouseEventArgs& args); - void keyPressed(ofKeyEventArgs& args); - void keyReleased(ofKeyEventArgs& args); - void gotMessage(ofMessage& msg); - void setSurfaceManager(SurfaceManager* newSurfaceManager); - void clearJoints(); - void createJoints(); - void updateJoints(); - void unselectAllJoints(); - void moveSelectedSurface(ofVec2f by); - void stopDragJoints(); - void updateVertices(); - void moveSelection(ofVec2f by); - void setSnapDistance(float newSnapDistance); - CircleJoint* hitTestJoints(ofVec2f pos); - - private: - SurfaceManager* surfaceManager; - vector joints; - bool bShiftKeyDown; - float fSnapDistance; - - void drawJoints(); -}; -} + namespace piMapper { + class ProjectionEditor { + public: + ProjectionEditor(); + ~ProjectionEditor(); + + void registerAppEvents(); + void unregisterAppEvents(); + void registerMouseEvents(); + void unregisterMouseEvents(); + void registerKeyEvents(); + void unregisterKeyEvents(); + + void enable(); + void disable(); + + void update(ofEventArgs& args); + void draw(); + void mouseDragged(ofMouseEventArgs& args); + void keyPressed(ofKeyEventArgs& args); + void keyReleased(ofKeyEventArgs& args); + void gotMessage(ofMessage& msg); + void setSurfaceManager(SurfaceManager* newSurfaceManager); + void clearJoints(); + void createJoints(); + void updateJoints(); + void unselectAllJoints(); + void moveSelectedSurface(ofVec2f by); + void stopDragJoints(); + void updateVertices(); + void moveSelection(ofVec2f by); + void setSnapDistance(float newSnapDistance); + CircleJoint* hitTestJoints(ofVec2f pos); + vector * getJoints(); + + private: + SurfaceManager* surfaceManager; + vector joints; + bool bShiftKeyDown; + float fSnapDistance; + + void drawJoints(); + }; + } } \ No newline at end of file