From 8867edb8f298dea26f5a2e8264f29d8f515f3b6b Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Sat, 10 Oct 2015 16:39:14 +0300 Subject: [PATCH] Add AddSurfaceCmd and move surface creation key commands - from ofxPiMapper to application state machine --- example/example.xcodeproj/project.pbxproj | 6 +++ src/Application/Application.cpp | 6 +++ src/Application/Application.h | 3 ++ src/Application/ApplicationBaseState.h | 2 +- src/Application/ProjectionMappingState.cpp | 29 ++++++++++ src/Application/ProjectionMappingState.h | 3 ++ src/Commands/AddSurfaceCmd.cpp | 61 ++++++++++++++++++++++ src/Commands/AddSurfaceCmd.h | 31 +++++++++++ src/Surfaces/SurfaceManager.cpp | 8 +++ src/Surfaces/SurfaceManager.h | 2 + src/ofxPiMapper.cpp | 35 ------------- src/ofxPiMapper.h | 5 -- 12 files changed, 150 insertions(+), 41 deletions(-) create mode 100644 src/Commands/AddSurfaceCmd.cpp create mode 100644 src/Commands/AddSurfaceCmd.h diff --git a/example/example.xcodeproj/project.pbxproj b/example/example.xcodeproj/project.pbxproj index 694ebcb..88927fc 100644 --- a/example/example.xcodeproj/project.pbxproj +++ b/example/example.xcodeproj/project.pbxproj @@ -28,6 +28,7 @@ 396841781BC91F6F009F0BAE /* ProjectionMappingState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3968416F1BC91F6F009F0BAE /* ProjectionMappingState.cpp */; }; 396841791BC91F6F009F0BAE /* SourceSelectionState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 396841711BC91F6F009F0BAE /* SourceSelectionState.cpp */; }; 3968417A1BC91F6F009F0BAE /* TextureMappingState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 396841731BC91F6F009F0BAE /* TextureMappingState.cpp */; }; + 396841821BC93DDC009F0BAE /* AddSurfaceCmd.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 396841801BC93DDC009F0BAE /* AddSurfaceCmd.cpp */; }; 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 */; }; @@ -172,6 +173,8 @@ 396841721BC91F6F009F0BAE /* SourceSelectionState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SourceSelectionState.h; sourceTree = ""; }; 396841731BC91F6F009F0BAE /* TextureMappingState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TextureMappingState.cpp; sourceTree = ""; }; 396841741BC91F6F009F0BAE /* TextureMappingState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextureMappingState.h; sourceTree = ""; }; + 396841801BC93DDC009F0BAE /* AddSurfaceCmd.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = AddSurfaceCmd.cpp; path = Commands/AddSurfaceCmd.cpp; sourceTree = ""; }; + 396841811BC93DDC009F0BAE /* AddSurfaceCmd.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AddSurfaceCmd.h; path = Commands/AddSurfaceCmd.h; sourceTree = ""; }; 397EFC7B1A08E7680009286E /* ofxPiMapper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofxPiMapper.cpp; sourceTree = ""; }; 397EFC7D1A08FE720009286E /* FboSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FboSource.cpp; sourceTree = ""; }; 397EFC7E1A08FE720009286E /* FboSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FboSource.h; sourceTree = ""; }; @@ -701,6 +704,8 @@ 39FDD9EA1AC007BF00262205 /* BaseCmd.h */, 39C787BB1AC20D2400691393 /* CmdManager.h */, 39C787BC1AC2111B00691393 /* CmdManager.cpp */, + 396841811BC93DDC009F0BAE /* AddSurfaceCmd.h */, + 396841801BC93DDC009F0BAE /* AddSurfaceCmd.cpp */, 39A9AAF11B054FC300AA83BC /* MvSurfaceVertCmd.h */, 39A9AAF01B054FC300AA83BC /* MvSurfaceVertCmd.cpp */, 39A9AAEB1B053B4200AA83BC /* SelSurfaceCmd.h */, @@ -951,6 +956,7 @@ 39C1248019F187D5005DF557 /* SourcesEditor.cpp in Sources */, 391717EF1B08FFDA00F9A484 /* SetApplicationStateCmd.cpp in Sources */, 39C1243F19EE9589005DF557 /* DirectoryWatcherManager.cpp in Sources */, + 396841821BC93DDC009F0BAE /* AddSurfaceCmd.cpp in Sources */, 39C1243519EE9589005DF557 /* ByteBuffer.cpp in Sources */, 39C1246B19F0AB96005DF557 /* TriangleSurface.cpp in Sources */, 396841791BC91F6F009F0BAE /* SourceSelectionState.cpp in Sources */, diff --git a/src/Application/Application.cpp b/src/Application/Application.cpp index 58d4502..b43e936 100644 --- a/src/Application/Application.cpp +++ b/src/Application/Application.cpp @@ -20,6 +20,10 @@ namespace ofx { return _state; } + ofxPiMapper * Application::getOfxPiMapper() { + return _ofxPiMapper; + } + void Application::draw(){ _state->draw(this); } @@ -56,6 +60,8 @@ namespace ofx { &_ofxPiMapper->getGui(), GuiMode::SOURCE_SELECTION)); break; default: + // All the other keypresses are handled by the application state onKeyPressed + _state->onKeyPressed(this, args); break; } } diff --git a/src/Application/Application.h b/src/Application/Application.h index bec8459..820d29f 100644 --- a/src/Application/Application.h +++ b/src/Application/Application.h @@ -4,7 +4,9 @@ #include "ofLog.h" #include "ofxPiMapper.h" + #include "SetApplicationStateCmd.h" + #include "ApplicationBaseState.h" #include "PresentationState.h" #include "ProjectionMappingState.h" @@ -27,6 +29,7 @@ namespace ofx { ~Application(); ApplicationBaseState * getState(); + ofxPiMapper * getOfxPiMapper(); // Temporary method. void draw(); void onKeyPressed(ofKeyEventArgs & args); diff --git a/src/Application/ApplicationBaseState.h b/src/Application/ApplicationBaseState.h index c30d5f3..fd8a64e 100644 --- a/src/Application/ApplicationBaseState.h +++ b/src/Application/ApplicationBaseState.h @@ -14,7 +14,7 @@ namespace ofx { virtual void setState(Application * app, ApplicationBaseState * st); // Event handler virtual methods - virtual void keyPressed(){}; + virtual void onKeyPressed(Application * app, ofKeyEventArgs & args){}; }; } // namespace piMapper diff --git a/src/Application/ProjectionMappingState.cpp b/src/Application/ProjectionMappingState.cpp index a60e1ec..e9ca783 100644 --- a/src/Application/ProjectionMappingState.cpp +++ b/src/Application/ProjectionMappingState.cpp @@ -15,5 +15,34 @@ namespace ofx { void ProjectionMappingState::draw(Application * app) { ofDrawBitmapString("Projection Mapping State", 10, 20); } + + void ProjectionMappingState::onKeyPressed(Application * app, ofKeyEventArgs & args) { + switch (args.key) { + + case 't': + app->getOfxPiMapper()->getCmdManager().exec( + new AddSurfaceCmd( + app->getOfxPiMapper(), + SurfaceType::TRIANGLE_SURFACE) + ); + break; + + case 'q': + app->getOfxPiMapper()->getCmdManager().exec( + new AddSurfaceCmd( + app->getOfxPiMapper(), + SurfaceType::QUAD_SURFACE) + ); + break; + + case OF_KEY_BACKSPACE: + app->getOfxPiMapper()->getCmdManager().exec( + new RmSurfaceCmd(app->getOfxPiMapper())); + break; + + default: + break; + } + } } } \ No newline at end of file diff --git a/src/Application/ProjectionMappingState.h b/src/Application/ProjectionMappingState.h index 3e194b3..fd389ad 100644 --- a/src/Application/ProjectionMappingState.h +++ b/src/Application/ProjectionMappingState.h @@ -4,6 +4,8 @@ #include "ofEvents.h" #include "ofLog.h" #include "ofGraphics.h" +#include "AddSurfaceCmd.h" +#include "SurfaceType.h" namespace ofx { namespace piMapper { @@ -12,6 +14,7 @@ namespace ofx { public: static ProjectionMappingState * instance(); void draw(Application * app); + void onKeyPressed(Application * app, ofKeyEventArgs & args); private: static ProjectionMappingState * _instance; diff --git a/src/Commands/AddSurfaceCmd.cpp b/src/Commands/AddSurfaceCmd.cpp new file mode 100644 index 0000000..a806e34 --- /dev/null +++ b/src/Commands/AddSurfaceCmd.cpp @@ -0,0 +1,61 @@ +#include "AddSurfaceCmd.h" + +namespace ofx{ + namespace piMapper{ + + AddSurfaceCmd::AddSurfaceCmd(ofxPiMapper * app, int surfaceType){ + _app = app; + _surfaceType = surfaceType; + } + + void AddSurfaceCmd::exec(){ + if (_surfaceType == SurfaceType::TRIANGLE_SURFACE) { + addTriangleSurface(); + } else if (_surfaceType == SurfaceType::QUAD_SURFACE) { + addQuadSurface(); + } + } + + void AddSurfaceCmd::undo(){ + ofLogNotice("AddSurfaceCmd", "undo"); + _app->getSurfaceManager().removeSurface(); + } + + void AddSurfaceCmd::addTriangleSurface() { + int surfaceType = ofx::piMapper::SurfaceType::TRIANGLE_SURFACE; + + vector vertices; + float margin = 50.0f; + vertices.push_back(ofVec2f((float)ofGetWidth() / 2.0f, margin)); + vertices.push_back(ofVec2f((float)ofGetWidth() - margin, (float)ofGetHeight() - margin)); + vertices.push_back(ofVec2f(margin, (float)ofGetHeight() - margin)); + + vector texCoords; + texCoords.push_back(ofVec2f(0.5f, 0.0f)); + texCoords.push_back(ofVec2f(1.0f, 1.0f)); + texCoords.push_back(ofVec2f(0.0f, 1.0f)); + _app->getSurfaceManager().addSurface(surfaceType, vertices, texCoords); + } + + void AddSurfaceCmd::addQuadSurface() { + int surfaceType = ofx::piMapper::SurfaceType::QUAD_SURFACE; + + vector vertices; + float margin = 50.0f; + vertices.push_back(ofVec2f(margin, margin)); + vertices.push_back(ofVec2f((float)ofGetWidth() - margin, margin)); + vertices.push_back(ofVec2f((float)ofGetWidth() - margin, (float)ofGetHeight() - margin)); + vertices.push_back(ofVec2f(margin, (float)ofGetHeight() - margin)); + + vector texCoords; + texCoords.push_back(ofVec2f(ofVec2f(0.0f, 0.0f))); + texCoords.push_back(ofVec2f(ofVec2f(1.0f, 0.0f))); + texCoords.push_back(ofVec2f(ofVec2f(1.0f, 1.0f))); + texCoords.push_back(ofVec2f(ofVec2f(0.0f, 1.0f))); + + _app->getSurfaceManager().addSurface(surfaceType, vertices, texCoords); + } + + } // namespace piMapper +} // namespace ofx + diff --git a/src/Commands/AddSurfaceCmd.h b/src/Commands/AddSurfaceCmd.h new file mode 100644 index 0000000..61b27c2 --- /dev/null +++ b/src/Commands/AddSurfaceCmd.h @@ -0,0 +1,31 @@ +#pragma once + +#include "ofxPiMapper.h" +#include "BaseCmd.h" +#include "SurfaceType.h" +#include "BaseSurface.h" + +class ofxPiMapper; + +namespace ofx{ + namespace piMapper{ + + class AddSurfaceCmd : public BaseUndoCmd{ + + public: + AddSurfaceCmd(ofxPiMapper * app, int surfaceType); + void exec(); + void undo(); + + private: + ofxPiMapper * _app; + int _surfaceType; + + // TODO: Should use some kind of factory class here + void addTriangleSurface(); + void addQuadSurface(); + }; + + } // namespace piMapper +} // namespace ofx + diff --git a/src/Surfaces/SurfaceManager.cpp b/src/Surfaces/SurfaceManager.cpp index 1d4cfd9..b3e15cb 100644 --- a/src/Surfaces/SurfaceManager.cpp +++ b/src/Surfaces/SurfaceManager.cpp @@ -138,6 +138,14 @@ void SurfaceManager::removeSelectedSurface(){ } } +void SurfaceManager::removeSurface() { + if (!surfaces.size()) { + return; + } + delete surfaces.back(); + surfaces.pop_back(); +} + void SurfaceManager::clear() { // delete all extra allocations from the heap while (surfaces.size()) { diff --git a/src/Surfaces/SurfaceManager.h b/src/Surfaces/SurfaceManager.h index 53779e9..5e026b4 100755 --- a/src/Surfaces/SurfaceManager.h +++ b/src/Surfaces/SurfaceManager.h @@ -34,6 +34,8 @@ class SurfaceManager { void addSurface(BaseSurface * surface); void removeSelectedSurface(); + void removeSurface(); + void clear(); void saveXmlSettings(string fileName); void loadXmlSettings(string fileName); diff --git a/src/ofxPiMapper.cpp b/src/ofxPiMapper.cpp index 1ef3828..bbc8fdb 100644 --- a/src/ofxPiMapper.cpp +++ b/src/ofxPiMapper.cpp @@ -79,50 +79,15 @@ void ofxPiMapper::keyPressed(ofKeyEventArgs &args){ switch (args.key) { - /* - case '1': - if (gui.getMode() != ofx::piMapper::GuiMode::NONE) { - cmdManager.exec(new ofx::piMapper::SetGuiModeCmd(&gui, - ofx::piMapper::GuiMode::NONE)); - } - break; - case '2': - if (gui.getMode() != ofx::piMapper::GuiMode::TEXTURE_MAPPING) { - cmdManager.exec(new ofx::piMapper::SetGuiModeCmd(&gui, - ofx::piMapper::GuiMode::TEXTURE_MAPPING)); - } - break; - case '3': - if (gui.getMode() != ofx::piMapper::GuiMode::PROJECTION_MAPPING) { - cmdManager.exec(new ofx::piMapper::SetGuiModeCmd(&gui, - ofx::piMapper::GuiMode::PROJECTION_MAPPING)); - } - break; - case '4': - if (gui.getMode() != ofx::piMapper::GuiMode::SOURCE_SELECTION) { - cmdManager.exec(new ofx::piMapper::SetGuiModeCmd(&gui, - ofx::piMapper::GuiMode::SOURCE_SELECTION)); - } - break; - */ case 'i': bShowInfo = !bShowInfo; break; - case 'q': - addQuadSurface(); - break; - case 't': - addTriangleSurface(); - break; case 'f': ofToggleFullscreen(); break; case 's': surfaceManager.saveXmlSettings(PIMAPPER_USER_SURFACES_XML_FILE); break; - case OF_KEY_BACKSPACE: - cmdManager.exec(new ofx::piMapper::RmSurfaceCmd((ofxPiMapper *)this)); - break; case 'z': // Undo any undo command operation cmdManager.undo(); diff --git a/src/ofxPiMapper.h b/src/ofxPiMapper.h index 38ae474..9ac9935 100644 --- a/src/ofxPiMapper.h +++ b/src/ofxPiMapper.h @@ -35,11 +35,6 @@ class ofxPiMapper{ void keyPressed(ofKeyEventArgs& args); void addFboSource(ofx::piMapper::FboSource& fboSource); - - // Discussion: - // Maybe it makes more sense to use create prefix instead of add - // in addTriangleSurface and so on, so we get createTriangleSurface. - // TODO: Copy/move these methods to SurfaceManager (not sure) void addTriangleSurface(); void addQuadSurface();