Browse Source

Add undo operation with MoveSurfaceVertexCommand

master
Krisjanis Rijnieks 10 years ago
parent
commit
ce6588d296
  1. 6
      example/example.xcodeproj/project.pbxproj
  2. 2
      src/Commands/MoveSurfaceCommand.cpp
  3. 31
      src/Commands/MoveSurfaceVertexCommand.cpp
  4. 34
      src/Commands/MoveSurfaceVertexCommand.h
  5. 12
      src/Surfaces/SurfaceManagerGui.cpp
  6. 9
      src/Surfaces/SurfaceManagerGui.h
  7. 116
      src/UserInterface/ProjectionEditor.cpp
  8. 9
      src/UserInterface/ProjectionEditor.h

6
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 = "<group>"; };
39A9AAEA1B053B4200AA83BC /* SelectSurfaceCommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SelectSurfaceCommand.cpp; path = Commands/SelectSurfaceCommand.cpp; sourceTree = "<group>"; };
39A9AAEB1B053B4200AA83BC /* SelectSurfaceCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SelectSurfaceCommand.h; path = Commands/SelectSurfaceCommand.h; sourceTree = "<group>"; };
39A9AAF01B054FC300AA83BC /* MoveSurfaceVertexCommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = MoveSurfaceVertexCommand.cpp; path = Commands/MoveSurfaceVertexCommand.cpp; sourceTree = "<group>"; };
39A9AAF11B054FC300AA83BC /* MoveSurfaceVertexCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MoveSurfaceVertexCommand.h; path = Commands/MoveSurfaceVertexCommand.h; sourceTree = "<group>"; };
39C123E719EE9589005DF557 /* alphanum.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = alphanum.hpp; sourceTree = "<group>"; };
39C123EA19EE9589005DF557 /* lz4.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = lz4.c; sourceTree = "<group>"; };
39C123EB19EE9589005DF557 /* lz4.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lz4.h; sourceTree = "<group>"; };
@ -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 */,

2
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();

31
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

34
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

12
src/Surfaces/SurfaceManagerGui.cpp

@ -87,7 +87,6 @@ namespace ofx {
return;
} else if (guiMode == GuiMode::TEXTURE_MAPPING) {
bool bSurfaceSelected = false;
CircleJoint* hitJoint =
textureEditor.hitTestJoints(ofVec2f(args.x, args.y));
if (hitJoint != NULL) {
@ -117,6 +116,17 @@ namespace ofx {
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;
}

9
src/Surfaces/SurfaceManagerGui.h

@ -14,10 +14,11 @@
#include "CommandManager.h"
#include "MoveSurfaceCommand.h"
#include "SelectSurfaceCommand.h"
#include "MoveSurfaceVertexCommand.h"
namespace ofx {
namespace piMapper {
class SurfaceManagerGui {
namespace piMapper {
class SurfaceManagerGui {
public:
SurfaceManagerGui();
~SurfaceManagerGui();
@ -50,6 +51,6 @@ class SurfaceManagerGui {
bool bDrag;
ofVec2f clickPosition;
CommandManager * _commandManager;
};
}
};
}
}

116
src/UserInterface/ProjectionEditor.cpp

@ -1,64 +1,64 @@
#include "ProjectionEditor.h"
namespace ofx {
namespace piMapper {
ProjectionEditor::ProjectionEditor() {
namespace piMapper {
ProjectionEditor::ProjectionEditor() {
surfaceManager = NULL;
bShiftKeyDown = false;
fSnapDistance = 10.0f;
enable();
}
}
ProjectionEditor::~ProjectionEditor() {
ProjectionEditor::~ProjectionEditor() {
clearJoints();
surfaceManager = NULL;
disable();
}
}
void ProjectionEditor::registerAppEvents() {
void ProjectionEditor::registerAppEvents() {
ofAddListener(ofEvents().update, this, &ProjectionEditor::update);
ofAddListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage);
}
}
void ProjectionEditor::unregisterAppEvents() {
void ProjectionEditor::unregisterAppEvents() {
ofRemoveListener(ofEvents().update, this, &ProjectionEditor::update);
ofRemoveListener(ofEvents().messageEvent, this,
&ProjectionEditor::gotMessage);
}
}
void ProjectionEditor::registerMouseEvents() {
void ProjectionEditor::registerMouseEvents() {
ofAddListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged);
}
}
void ProjectionEditor::unregisterMouseEvents() {
void ProjectionEditor::unregisterMouseEvents() {
ofRemoveListener(ofEvents().mouseDragged, this,
&ProjectionEditor::mouseDragged);
}
}
void ProjectionEditor::registerKeyEvents() {
void ProjectionEditor::registerKeyEvents() {
ofAddListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed);
ofAddListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased);
}
}
void ProjectionEditor::unregisterKeyEvents() {
void ProjectionEditor::unregisterKeyEvents() {
ofRemoveListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed);
ofRemoveListener(ofEvents().keyReleased, this,
&ProjectionEditor::keyReleased);
}
}
void ProjectionEditor::enable() {
void ProjectionEditor::enable() {
registerAppEvents();
registerMouseEvents();
registerKeyEvents();
}
}
void ProjectionEditor::disable() {
void ProjectionEditor::disable() {
unregisterAppEvents();
unregisterMouseEvents();
unregisterKeyEvents();
}
}
void ProjectionEditor::update(ofEventArgs& args) {
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()) {
@ -74,16 +74,16 @@ void ProjectionEditor::update(ofEventArgs& args) {
break;
}
}
}
}
void ProjectionEditor::draw() {
void ProjectionEditor::draw() {
if (surfaceManager == NULL) return;
if (surfaceManager->getSelectedSurface() == NULL) return;
if (joints.size() <= 0) createJoints();
drawJoints();
}
}
void ProjectionEditor::mouseDragged(ofMouseEventArgs& args) {
void ProjectionEditor::mouseDragged(ofMouseEventArgs& args) {
ofVec2f mousePosition = ofVec2f(args.x, args.y);
// Collect all vertices of the projection surfaces
@ -114,9 +114,9 @@ void ProjectionEditor::mouseDragged(ofMouseEventArgs& args) {
}
}
}
}
}
void ProjectionEditor::keyPressed(ofKeyEventArgs& args) {
void ProjectionEditor::keyPressed(ofKeyEventArgs& args) {
int key = args.key;
float moveStep;
@ -142,37 +142,37 @@ void ProjectionEditor::keyPressed(ofKeyEventArgs& args) {
bShiftKeyDown = true;
break;
}
}
}
void ProjectionEditor::keyReleased(ofKeyEventArgs& args) {
void ProjectionEditor::keyReleased(ofKeyEventArgs& args) {
int key = args.key;
switch (key) {
case OF_KEY_SHIFT:
bShiftKeyDown = false;
break;
}
}
}
void ProjectionEditor::gotMessage(ofMessage& msg) {
void ProjectionEditor::gotMessage(ofMessage& msg) {
if (msg.message == "surfaceSelected") {
// refresh gui
clearJoints();
createJoints();
}
}
}
void ProjectionEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) {
void ProjectionEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) {
surfaceManager = newSurfaceManager;
}
}
void ProjectionEditor::clearJoints() {
void ProjectionEditor::clearJoints() {
while (joints.size()) {
delete joints.back();
joints.pop_back();
}
}
}
void ProjectionEditor::createJoints() {
void ProjectionEditor::createJoints() {
if (surfaceManager == NULL) return;
clearJoints();
@ -188,23 +188,23 @@ void ProjectionEditor::createJoints() {
joints.push_back(new CircleJoint());
joints.back()->position = ofVec2f(vertices[i].x, vertices[i].y);
}
}
}
void ProjectionEditor::updateJoints() {
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() {
void ProjectionEditor::unselectAllJoints() {
for (int i = 0; i < joints.size(); i++) {
joints[i]->unselect();
}
}
}
void ProjectionEditor::moveSelectedSurface(ofVec2f by) {
void ProjectionEditor::moveSelectedSurface(ofVec2f by) {
if (surfaceManager == NULL) return;
if (surfaceManager->getSelectedSurface() == NULL) return;
surfaceManager->getSelectedSurface()->moveBy(by);
@ -214,15 +214,15 @@ void ProjectionEditor::moveSelectedSurface(ofVec2f by) {
vertices[i] += by;
}*/
updateJoints();
}
}
void ProjectionEditor::stopDragJoints() {
void ProjectionEditor::stopDragJoints() {
for (int i = 0; i < joints.size(); i++) {
joints[i]->stopDrag();
}
}
}
void ProjectionEditor::moveSelection(ofVec2f by) {
void ProjectionEditor::moveSelection(ofVec2f by) {
// check if joints selected
bool bJointSelected = false;
BaseJoint* selectedJoint;
@ -239,25 +239,29 @@ void ProjectionEditor::moveSelection(ofVec2f by) {
} else {
moveSelectedSurface(by);
}
}
}
void ProjectionEditor::setSnapDistance(float newSnapDistance) {
void ProjectionEditor::setSnapDistance(float newSnapDistance) {
fSnapDistance = newSnapDistance;
}
}
CircleJoint* ProjectionEditor::hitTestJoints(ofVec2f pos) {
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() {
void ProjectionEditor::drawJoints() {
for (int i = 0; i < joints.size(); i++) {
joints[i]->draw();
}
}
}
}
}
}

9
src/UserInterface/ProjectionEditor.h

@ -4,8 +4,8 @@
#include "CircleJoint.h"
namespace ofx {
namespace piMapper {
class ProjectionEditor {
namespace piMapper {
class ProjectionEditor {
public:
ProjectionEditor();
~ProjectionEditor();
@ -37,6 +37,7 @@ class ProjectionEditor {
void moveSelection(ofVec2f by);
void setSnapDistance(float newSnapDistance);
CircleJoint* hitTestJoints(ofVec2f pos);
vector<CircleJoint *> * getJoints();
private:
SurfaceManager* surfaceManager;
@ -45,6 +46,6 @@ class ProjectionEditor {
float fSnapDistance;
void drawJoints();
};
}
};
}
}
Loading…
Cancel
Save