From bd693642849404237b015546b6926754d76ffcca Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Thu, 14 May 2015 17:47:22 +0300 Subject: [PATCH] Add undoable RemoveSurfaceCommand --- example/example.xcodeproj/project.pbxproj | 6 +++++ src/Commands/RemoveSurfaceCommand.cpp | 28 +++++++++++++++++++++ src/Commands/RemoveSurfaceCommand.h | 30 +++++++++++++++++++++++ src/Surfaces/SurfaceManager.cpp | 29 +++++++++++++--------- src/Surfaces/SurfaceManager.h | 6 +++++ src/ofxPiMapper.cpp | 3 ++- src/ofxPiMapper.h | 1 + 7 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 src/Commands/RemoveSurfaceCommand.cpp create mode 100644 src/Commands/RemoveSurfaceCommand.h diff --git a/example/example.xcodeproj/project.pbxproj b/example/example.xcodeproj/project.pbxproj index 111e8f4..b523371 100644 --- a/example/example.xcodeproj/project.pbxproj +++ b/example/example.xcodeproj/project.pbxproj @@ -22,6 +22,7 @@ 397EFC7C1A08E7680009286E /* ofxPiMapper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 397EFC7B1A08E7680009286E /* ofxPiMapper.cpp */; }; 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 */; }; 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 */; }; @@ -147,6 +148,8 @@ 397EFC7E1A08FE720009286E /* FboSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FboSource.h; sourceTree = ""; }; 397EFC801A09047C0009286E /* CustomSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CustomSource.cpp; sourceTree = ""; }; 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 = ""; }; 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 = ""; }; @@ -642,6 +645,8 @@ 39FDD9E81AC0076200262205 /* Commands */ = { isa = PBXGroup; children = ( + 39A9AADF1B04E78600AA83BC /* RemoveSurfaceCommand.h */, + 39A9AADE1B04E78600AA83BC /* RemoveSurfaceCommand.cpp */, 39FDD9EA1AC007BF00262205 /* BaseCommand.h */, 39C787BB1AC20D2400691393 /* CommandManager.h */, 39C787BC1AC2111B00691393 /* CommandManager.cpp */, @@ -856,6 +861,7 @@ 39C1243D19EE9589005DF557 /* DirectoryUtils.cpp in Sources */, 39264843192224F90008A7F5 /* tinyxmlparser.cpp in Sources */, 3933D5D419BB87BD000ACA55 /* ofxButton.cpp in Sources */, + 39A9AAE01B04E78600AA83BC /* RemoveSurfaceCommand.cpp in Sources */, 39C1244519EE9589005DF557 /* RecursiveDirectoryIterator.cpp in Sources */, 39C1243919EE9589005DF557 /* COBSEncoding.cpp in Sources */, 39C1244919EE9589005DF557 /* snappy-sinksource.cc in Sources */, diff --git a/src/Commands/RemoveSurfaceCommand.cpp b/src/Commands/RemoveSurfaceCommand.cpp new file mode 100644 index 0000000..35b5c13 --- /dev/null +++ b/src/Commands/RemoveSurfaceCommand.cpp @@ -0,0 +1,28 @@ +#include "RemoveSurfaceCommand.h" + +namespace ofx{ + namespace piMapper{ + + RemoveSurfaceCommand::RemoveSurfaceCommand(ofxPiMapper * app){ + _app = app; + _surface = 0; + } + + void RemoveSurfaceCommand::exec(){ + // Store the surface, this implies that the surfaceManager's + // removeSelectedSurface does not destroy the surface. + _surface = _app->surfaceManager.getSelectedSurface(); + _app->surfaceManager.removeSelectedSurface(); + } + + void RemoveSurfaceCommand::undo(){ + if (_surface == 0) { + ofLogError("RemoveSurfaceCommand", "No surface stored"); + } + _app->surfaceManager.addSurface(_surface); + _surface = 0; + } + + } // namespace piMapper +} // namespace ofx + diff --git a/src/Commands/RemoveSurfaceCommand.h b/src/Commands/RemoveSurfaceCommand.h new file mode 100644 index 0000000..4e10077 --- /dev/null +++ b/src/Commands/RemoveSurfaceCommand.h @@ -0,0 +1,30 @@ +// RemoveSurfaceCommand +// Provides with option to undo remove surface operation. +// Created by Krisjanis Rijnieks 2015-05-14 + +#pragma once + +#include "ofxPiMapper.h" +#include "BaseCommand.h" +#include "BaseSurface.h" + +class ofxPiMapper; + +namespace ofx{ + namespace piMapper{ + + class RemoveSurfaceCommand : public BaseUndoableCommand{ + + public: + RemoveSurfaceCommand(ofxPiMapper * app); + void exec(); + void undo(); + + private: + ofxPiMapper * _app; + BaseSurface * _surface; + }; + + } // namespace piMapper +} // namespace ofx + diff --git a/src/Surfaces/SurfaceManager.cpp b/src/Surfaces/SurfaceManager.cpp index 493a949..3cc15aa 100644 --- a/src/Surfaces/SurfaceManager.cpp +++ b/src/Surfaces/SurfaceManager.cpp @@ -117,18 +117,25 @@ void SurfaceManager::addSurface(int surfaceType, BaseSource* newSource, } } -void SurfaceManager::removeSelectedSurface() { - if (selectedSurface == NULL) { - return; - } - for (int i = 0; i < surfaces.size(); i++) { - if (surfaces[i] == selectedSurface) { - delete surfaces[i]; - surfaces.erase(surfaces.begin() + i); - selectedSurface = NULL; - break; +// Add existing surface +void SurfaceManager::addSurface(BaseSurface * surface){ + surfaces.push_back(surface); +} + +void SurfaceManager::removeSelectedSurface(){ + if (selectedSurface == NULL){ + return; + } + for (int i = 0; i < surfaces.size(); i++){ + if (surfaces[i] == selectedSurface){ + // Do not delete pointer as we are storing the + // surface in the RemoveSurfaceCommand. + //delete surfaces[i]; + surfaces.erase(surfaces.begin() + i); + selectedSurface = NULL; + break; + } } - } } void SurfaceManager::clear() { diff --git a/src/Surfaces/SurfaceManager.h b/src/Surfaces/SurfaceManager.h index 2e6eb6f..bffcdba 100755 --- a/src/Surfaces/SurfaceManager.h +++ b/src/Surfaces/SurfaceManager.h @@ -21,12 +21,18 @@ class SurfaceManager { ~SurfaceManager(); void draw(); + + // TODO: These should be renamed to createSurface void addSurface(int surfaceType); void addSurface(int surfaceType, BaseSource* newSource); void addSurface(int surfaceType, vector vertices, vector texCoords); void addSurface(int surfaceType, BaseSource* newSource, vector vertices, vector texCoords); + + // Except this, as it adds existing surface + void addSurface(BaseSurface * surface); + void removeSelectedSurface(); void clear(); void saveXmlSettings(string fileName); diff --git a/src/ofxPiMapper.cpp b/src/ofxPiMapper.cpp index fe9d013..c5b3784 100644 --- a/src/ofxPiMapper.cpp +++ b/src/ofxPiMapper.cpp @@ -100,7 +100,8 @@ void ofxPiMapper::keyPressed(ofKeyEventArgs &args){ surfaceManager.saveXmlSettings(PIMAPPER_USER_SURFACES_XML_FILE); break; case OF_KEY_BACKSPACE: - surfaceManager.removeSelectedSurface(); + //surfaceManager.removeSelectedSurface(); + commandManager.exec(new ofx::piMapper::RemoveSurfaceCommand((ofxPiMapper *)this)); break; // TODO: Remove the following case when Command test done. case '9': diff --git a/src/ofxPiMapper.h b/src/ofxPiMapper.h index b7f8908..d45b394 100644 --- a/src/ofxPiMapper.h +++ b/src/ofxPiMapper.h @@ -16,6 +16,7 @@ #include "TestCommand.h" // TODO: Remove this line when done testing #include "TestUndoCommand.h" #include "CommandManager.h" +#include "RemoveSurfaceCommand.h" #define PIMAPPER_DEF_SURFACES_XML_FILE "defaultSurfaces.xml" #define PIMAPPER_USER_SURFACES_XML_FILE "surfaces.xml"