From c9a591a4aeddc3d475e7e9dbb2edc3465909760d Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Thu, 14 May 2015 21:19:46 +0300 Subject: [PATCH] Add undo feature with MoveSurfaceCommand --- example/example.xcodeproj/project.pbxproj | 6 +++++ src/Commands/MoveSurfaceCommand.cpp | 31 +++++++++++++++++++++++ src/Commands/MoveSurfaceCommand.h | 31 +++++++++++++++++++++++ src/Surfaces/SurfaceManagerGui.cpp | 11 ++++++++ src/Surfaces/SurfaceManagerGui.h | 6 +++++ src/ofxPiMapper.cpp | 1 + 6 files changed, 86 insertions(+) create mode 100644 src/Commands/MoveSurfaceCommand.cpp create mode 100644 src/Commands/MoveSurfaceCommand.h diff --git a/example/example.xcodeproj/project.pbxproj b/example/example.xcodeproj/project.pbxproj index b523371..1f7de55 100644 --- a/example/example.xcodeproj/project.pbxproj +++ b/example/example.xcodeproj/project.pbxproj @@ -23,6 +23,7 @@ 397EFC7F1A08FE720009286E /* FboSource.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 397EFC7D1A08FE720009286E /* FboSource.cpp */; }; 397EFC821A09047C0009286E /* CustomSource.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 397EFC801A09047C0009286E /* CustomSource.cpp */; }; 39A9AAE01B04E78600AA83BC /* RemoveSurfaceCommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39A9AADE1B04E78600AA83BC /* RemoveSurfaceCommand.cpp */; }; + 39A9AAE91B0518FC00AA83BC /* MoveSurfaceCommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39A9AAE71B0518FC00AA83BC /* MoveSurfaceCommand.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 */; }; @@ -150,6 +151,8 @@ 397EFC811A09047C0009286E /* CustomSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CustomSource.h; sourceTree = ""; }; 39A9AADE1B04E78600AA83BC /* RemoveSurfaceCommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = RemoveSurfaceCommand.cpp; path = Commands/RemoveSurfaceCommand.cpp; sourceTree = ""; }; 39A9AADF1B04E78600AA83BC /* RemoveSurfaceCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RemoveSurfaceCommand.h; path = Commands/RemoveSurfaceCommand.h; sourceTree = ""; }; + 39A9AAE71B0518FC00AA83BC /* MoveSurfaceCommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = MoveSurfaceCommand.cpp; path = Commands/MoveSurfaceCommand.cpp; sourceTree = ""; }; + 39A9AAE81B0518FC00AA83BC /* MoveSurfaceCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MoveSurfaceCommand.h; path = Commands/MoveSurfaceCommand.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 = ""; }; @@ -645,6 +648,8 @@ 39FDD9E81AC0076200262205 /* Commands */ = { isa = PBXGroup; children = ( + 39A9AAE71B0518FC00AA83BC /* MoveSurfaceCommand.cpp */, + 39A9AAE81B0518FC00AA83BC /* MoveSurfaceCommand.h */, 39A9AADF1B04E78600AA83BC /* RemoveSurfaceCommand.h */, 39A9AADE1B04E78600AA83BC /* RemoveSurfaceCommand.cpp */, 39FDD9EA1AC007BF00262205 /* BaseCommand.h */, @@ -855,6 +860,7 @@ E4B69E200A3A1BDC003C02F2 /* main.cpp in Sources */, 39C1244719EE9589005DF557 /* RegexPathFilter.cpp in Sources */, 39C787C11AC2BBAE00691393 /* TestUndoCommand.cpp in Sources */, + 39A9AAE91B0518FC00AA83BC /* MoveSurfaceCommand.cpp in Sources */, 39C1247C19F187D5005DF557 /* BaseJoint.cpp in Sources */, 39FDD9EE1AC00EAE00262205 /* TestCommand.cpp in Sources */, 39C1245219EE95DD005DF557 /* MediaServer.cpp in Sources */, diff --git a/src/Commands/MoveSurfaceCommand.cpp b/src/Commands/MoveSurfaceCommand.cpp new file mode 100644 index 0000000..cb1779e --- /dev/null +++ b/src/Commands/MoveSurfaceCommand.cpp @@ -0,0 +1,31 @@ +#include "MoveSurfaceCommand.h" + +namespace ofx{ + namespace piMapper{ + + MoveSurfaceCommand::MoveSurfaceCommand( + BaseSurface * surface, + ProjectionEditor * projectionEditor){ + + _surface = surface; + _projectionEditor = projectionEditor; + } + + void MoveSurfaceCommand::exec(){ + // Store surface location by copying vertices + _previousVertices = _surface->getVertices(); + } + + void MoveSurfaceCommand::undo(){ + //for (auto i = 0; i < _previousVertices.size(); i++) { + //_surface->setVertex(i, _previousVertices[i]); + //} + _surface->moveBy(_previousVertices[0] - _surface->getVertices()[0]); + _projectionEditor->updateJoints(); + _previousVertices.clear(); + _surface = 0; + } + + } // namespace piMapper +} // namespace ofx + diff --git a/src/Commands/MoveSurfaceCommand.h b/src/Commands/MoveSurfaceCommand.h new file mode 100644 index 0000000..38a7cd3 --- /dev/null +++ b/src/Commands/MoveSurfaceCommand.h @@ -0,0 +1,31 @@ +// MoveSurfaceCommand +// Provides with option to undo move surface operation. +// Created by Krisjanis Rijnieks 2015-05-14 + +#pragma once + +#include "BaseCommand.h" +#include "BaseSurface.h" +#include "ProjectionEditor.h" + +namespace ofx{ + namespace piMapper{ + + class MoveSurfaceCommand : public BaseUndoableCommand{ + + public: + MoveSurfaceCommand( + BaseSurface * surface, + ProjectionEditor * projectionEditor); + void exec(); + void undo(); + + private: + BaseSurface * _surface; + ProjectionEditor * _projectionEditor; + vector _previousVertices; + }; + + } // namespace piMapper +} // namespace ofx + diff --git a/src/Surfaces/SurfaceManagerGui.cpp b/src/Surfaces/SurfaceManagerGui.cpp index dee1584..e307a82 100644 --- a/src/Surfaces/SurfaceManagerGui.cpp +++ b/src/Surfaces/SurfaceManagerGui.cpp @@ -8,11 +8,13 @@ SurfaceManagerGui::SurfaceManagerGui() { bDrag = false; registerMouseEvents(); ofHideCursor(); + _commandManager = 0; } SurfaceManagerGui::~SurfaceManagerGui() { unregisterMouseEvents(); surfaceManager = NULL; + _commandManager = 0; } void SurfaceManagerGui::registerMouseEvents() { @@ -136,6 +138,11 @@ void SurfaceManagerGui::mousePressed(ofMouseEventArgs& args) { // surface clickPosition = ofVec2f(args.x, args.y); startDrag(); + + _commandManager->exec( + new MoveSurfaceCommand( + surfaceManager->getSelectedSurface(), + &projectionEditor)); } if (!bSurfaceSelected) { @@ -181,6 +188,10 @@ void SurfaceManagerGui::setSurfaceManager(SurfaceManager* newSurfaceManager) { sourcesEditor.setMediaServer(mediaServer); } + void SurfaceManagerGui::setCommandManager(CommandManager * commandManager){ + _commandManager = commandManager; + } + void SurfaceManagerGui::setMode(int newGuiMode) { if (newGuiMode != GuiMode::NONE && newGuiMode != GuiMode::TEXTURE_MAPPING && newGuiMode != GuiMode::PROJECTION_MAPPING && diff --git a/src/Surfaces/SurfaceManagerGui.h b/src/Surfaces/SurfaceManagerGui.h index 7e998a8..8c14063 100644 --- a/src/Surfaces/SurfaceManagerGui.h +++ b/src/Surfaces/SurfaceManagerGui.h @@ -11,6 +11,8 @@ #include "ProjectionEditor.h" #include "SourcesEditor.h" #include "GuiMode.h" +#include "CommandManager.h" +#include "MoveSurfaceCommand.h" namespace ofx { namespace piMapper { @@ -26,8 +28,11 @@ class SurfaceManagerGui { 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(); @@ -43,6 +48,7 @@ class SurfaceManagerGui { int guiMode; bool bDrag; ofVec2f clickPosition; + CommandManager * _commandManager; }; } } \ No newline at end of file diff --git a/src/ofxPiMapper.cpp b/src/ofxPiMapper.cpp index c5b3784..e259a41 100644 --- a/src/ofxPiMapper.cpp +++ b/src/ofxPiMapper.cpp @@ -14,6 +14,7 @@ void ofxPiMapper::setup(){ // Assign media server to other pi mapper components surfaceManager.setMediaServer(&mediaServer); gui.setMediaServer(&mediaServer); + gui.setCommandManager(&commandManager); // Check if we have user surfaces defined, if not - load default if (ofFile::doesFileExist(PIMAPPER_USER_SURFACES_XML_FILE)){