diff --git a/example/example.xcodeproj/project.pbxproj b/example/example.xcodeproj/project.pbxproj index 1f7de55..9f23536 100644 --- a/example/example.xcodeproj/project.pbxproj +++ b/example/example.xcodeproj/project.pbxproj @@ -24,6 +24,7 @@ 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 */; }; + 39A9AAEC1B053B4200AA83BC /* SelectSurfaceCommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39A9AAEA1B053B4200AA83BC /* SelectSurfaceCommand.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 */; }; @@ -153,6 +154,8 @@ 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 = ""; }; + 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 = ""; }; 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 = ""; }; @@ -648,6 +651,8 @@ 39FDD9E81AC0076200262205 /* Commands */ = { isa = PBXGroup; children = ( + 39A9AAEA1B053B4200AA83BC /* SelectSurfaceCommand.cpp */, + 39A9AAEB1B053B4200AA83BC /* SelectSurfaceCommand.h */, 39A9AAE71B0518FC00AA83BC /* MoveSurfaceCommand.cpp */, 39A9AAE81B0518FC00AA83BC /* MoveSurfaceCommand.h */, 39A9AADF1B04E78600AA83BC /* RemoveSurfaceCommand.h */, @@ -890,6 +895,7 @@ 39C1243519EE9589005DF557 /* ByteBuffer.cpp in Sources */, 39C1246B19F0AB96005DF557 /* TriangleSurface.cpp in Sources */, 39C1244419EE9589005DF557 /* PathFilterCollection.cpp in Sources */, + 39A9AAEC1B053B4200AA83BC /* SelectSurfaceCommand.cpp in Sources */, 39C1243319EE9589005DF557 /* lz4.c in Sources */, 39C1243B19EE9589005DF557 /* DeviceFilter.cpp in Sources */, 39C1243E19EE9589005DF557 /* DirectoryWatcher.cpp in Sources */, diff --git a/src/Commands/SelectSurfaceCommand.cpp b/src/Commands/SelectSurfaceCommand.cpp new file mode 100644 index 0000000..2bcba87 --- /dev/null +++ b/src/Commands/SelectSurfaceCommand.cpp @@ -0,0 +1,33 @@ +#include "SelectSurfaceCommand.h" + +namespace ofx{ + namespace piMapper{ + + SelectSurfaceCommand::SelectSurfaceCommand( + SurfaceManager * surfaceManager, + BaseSurface * surfaceToSelect, + ProjectionEditor * projectionEditor){ + + _surfaceManager = surfaceManager; + _surfaceToSelect = surfaceToSelect; + _projectionEditor = projectionEditor; + } + + void SelectSurfaceCommand::exec(){ + _prevSelectedSurface = _surfaceManager->getSelectedSurface(); + _projectionEditor->clearJoints(); + _surfaceManager->selectSurface(_surfaceToSelect); + _projectionEditor->createJoints(); + } + + void SelectSurfaceCommand::undo(){ + _projectionEditor->clearJoints(); + _surfaceManager->selectSurface(_prevSelectedSurface); + _projectionEditor->createJoints(); + _surfaceToSelect = 0; + _prevSelectedSurface = 0; + } + + } // namespace piMapper +} // namespace ofx + diff --git a/src/Commands/SelectSurfaceCommand.h b/src/Commands/SelectSurfaceCommand.h new file mode 100644 index 0000000..1bba6ef --- /dev/null +++ b/src/Commands/SelectSurfaceCommand.h @@ -0,0 +1,34 @@ +// SelectSurfaceCommand +// Provides with option to undo select surface operation. +// Created by Krisjanis Rijnieks 2015-05-14 + +#pragma once + +#include "BaseCommand.h" +#include "BaseSurface.h" +#include "SurfaceManager.h" +#include "ProjectionEditor.h" + +namespace ofx{ + namespace piMapper{ + + class SelectSurfaceCommand : public BaseUndoableCommand{ + + public: + SelectSurfaceCommand( + SurfaceManager * surfaceManager, + BaseSurface * surfaceToSelect, + ProjectionEditor * projectionEditor); + void exec(); + void undo(); + + private: + BaseSurface * _surfaceToSelect; + SurfaceManager * _surfaceManager; + BaseSurface * _prevSelectedSurface; + ProjectionEditor * _projectionEditor; + }; + + } // namespace piMapper +} // namespace ofx + diff --git a/src/Surfaces/SurfaceManager.cpp b/src/Surfaces/SurfaceManager.cpp index 3cc15aa..d56a9ac 100644 --- a/src/Surfaces/SurfaceManager.cpp +++ b/src/Surfaces/SurfaceManager.cpp @@ -370,16 +370,28 @@ void SurfaceManager::loadXmlSettings(string fileName) { mediaServer = newMediaServer; } -BaseSurface* SurfaceManager::selectSurface(int index) { - if (index >= surfaces.size()) { - throw std::runtime_error("Surface index out of bounds."); - } - - selectedSurface = surfaces[index]; + BaseSurface * SurfaceManager::selectSurface(int index) { + if (index >= surfaces.size()) { + throw std::runtime_error("Surface index out of bounds."); + } + selectedSurface = surfaces[index]; + + // notify that a new surface has been selected + ofSendMessage("surfaceSelected"); + return selectedSurface; + } - // notify that a new surface has been selected - ofSendMessage("surfaceSelected"); -} + BaseSurface * SurfaceManager::selectSurface(BaseSurface * surface){ + for (auto i = 0; i < surfaces.size(); i++) { + if (surfaces[i] == surface){ + selectedSurface = surface; + ofSendMessage("surfaceSelected"); + return selectedSurface; + } + } + deselectSurface(); + return 0; + } BaseSurface* SurfaceManager::getSelectedSurface() { return selectedSurface; diff --git a/src/Surfaces/SurfaceManager.h b/src/Surfaces/SurfaceManager.h index bffcdba..53779e9 100755 --- a/src/Surfaces/SurfaceManager.h +++ b/src/Surfaces/SurfaceManager.h @@ -42,6 +42,7 @@ class SurfaceManager { BaseSurface* getSurface(int index); int size(); BaseSurface* selectSurface(int index); + BaseSurface* selectSurface(BaseSurface * surface); BaseSurface* getSelectedSurface(); void deselectSurface(); diff --git a/src/Surfaces/SurfaceManagerGui.cpp b/src/Surfaces/SurfaceManagerGui.cpp index e307a82..981e19c 100644 --- a/src/Surfaces/SurfaceManagerGui.cpp +++ b/src/Surfaces/SurfaceManagerGui.cpp @@ -120,18 +120,19 @@ void SurfaceManagerGui::mousePressed(ofMouseEventArgs& args) { bSurfaceSelected = true; } - // attempt to select surface, loop from end to beginning - if (!bSurfaceSelected) { - for (int i = surfaceManager->size() - 1; i >= 0; i--) { - if (surfaceManager->getSurface(i)->hitTest(ofVec2f(args.x, args.y))) { - projectionEditor.clearJoints(); - surfaceManager->selectSurface(i); - projectionEditor.createJoints(); - bSurfaceSelected = true; - break; - } + // attempt to select surface, loop from end to beginning + if (!bSurfaceSelected) { + for (int i = surfaceManager->size() - 1; i >= 0; i--) { + if (surfaceManager->getSurface(i)->hitTest(ofVec2f(args.x, args.y))) { + _commandManager->exec(new SelectSurfaceCommand( + surfaceManager, + surfaceManager->getSurface(i), + &projectionEditor)); + bSurfaceSelected = true; + break; + } + } } - } if (bSurfaceSelected && hitJoint == NULL) { // if not hitting the joints, start drag only if we have a selected diff --git a/src/Surfaces/SurfaceManagerGui.h b/src/Surfaces/SurfaceManagerGui.h index 8c14063..565d05b 100644 --- a/src/Surfaces/SurfaceManagerGui.h +++ b/src/Surfaces/SurfaceManagerGui.h @@ -13,6 +13,7 @@ #include "GuiMode.h" #include "CommandManager.h" #include "MoveSurfaceCommand.h" +#include "SelectSurfaceCommand.h" namespace ofx { namespace piMapper {