From 0d6106b22a1f0e246c2921198581fc612e56d9fe Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Sat, 23 Jan 2016 23:41:55 +0100 Subject: [PATCH] Move all essential application parts to `Application` class --- src/Application/Application.cpp | 61 +++++++++++++++------- src/Application/Application.h | 27 +++++++--- src/Application/PresentationState.cpp | 4 +- src/Application/ProjectionMappingState.cpp | 24 +++++---- src/Application/ProjectionMappingState.h | 1 + src/Application/SourceSelectionState.cpp | 4 +- src/Application/TextureMappingState.cpp | 4 +- src/Commands/AddSurfaceCmd.cpp | 8 +-- src/Commands/AddSurfaceCmd.h | 10 ++-- src/Commands/RmSurfaceCmd.cpp | 12 ++--- src/Commands/RmSurfaceCmd.h | 6 +-- src/Commands/SetApplicationStateCmd.h | 1 + src/ofxPiMapper.cpp | 61 ++-------------------- src/ofxPiMapper.h | 22 -------- 14 files changed, 107 insertions(+), 138 deletions(-) diff --git a/src/Application/Application.cpp b/src/Application/Application.cpp index a24c973..30ae9e1 100644 --- a/src/Application/Application.cpp +++ b/src/Application/Application.cpp @@ -4,28 +4,37 @@ namespace ofx { namespace piMapper { -Application::Application(ofxPiMapper * opm){ - _ofxPiMapper = opm; +Application::Application(){ + _surfaceManager.setMediaServer(&_mediaServer); + _gui.setMediaServer(&_mediaServer); + _gui.setCmdManager(&_cmdManager); + _gui.setSurfaceManager(&_surfaceManager); + setState(PresentationState::instance()); ofAddListener(ofEvents().keyPressed, this, &Application::onKeyPressed); } Application::~Application(){ - _ofxPiMapper = 0; setState(0); ofRemoveListener(ofEvents().keyPressed, this, &Application::onKeyPressed); } -ApplicationBaseState * Application::getState(){ - return _state; +void Application::setup(){ + if(!loadXmlSettings(PIMAPPER_USER_SURFACES_XML_FILE)){ + ofLogWarning("Application::setup()") << "Failed to load user settings" << endl; + if(!loadXmlSettings(PIMAPPER_DEF_SURFACES_XML_FILE)){ + ofLogWarning("Application::setup()") << "Failed to load default settings" << endl; + } + } } -ofxPiMapper * Application::getOfxPiMapper(){ - return _ofxPiMapper; +ApplicationBaseState * Application::getState(){ + return _state; } void Application::draw(){ _state->draw(this); + _info.draw(); } // Here we handle application state changes only @@ -36,31 +45,31 @@ void Application::onKeyPressed(ofKeyEventArgs & args){ switch(args.key){ case '1': - _ofxPiMapper->getCmdManager()->exec( + _cmdManager.exec( new ofx::piMapper::SetApplicationStateCmd( this, PresentationState::instance(), - _ofxPiMapper->getGui(), GuiMode::NONE)); + &_gui, GuiMode::NONE)); break; case '2': - _ofxPiMapper->getCmdManager()->exec( + _cmdManager.exec( new ofx::piMapper::SetApplicationStateCmd( this, TextureMappingState::instance(), - _ofxPiMapper->getGui(), GuiMode::TEXTURE_MAPPING)); + &_gui, GuiMode::TEXTURE_MAPPING)); break; case '3': - _ofxPiMapper->getCmdManager()->exec( + _cmdManager.exec( new ofx::piMapper::SetApplicationStateCmd( this, ProjectionMappingState::instance(), - _ofxPiMapper->getGui(), GuiMode::PROJECTION_MAPPING)); + &_gui, GuiMode::PROJECTION_MAPPING)); break; case '4': - _ofxPiMapper->getCmdManager()->exec( + _cmdManager.exec( new ofx::piMapper::SetApplicationStateCmd( this, SourceSelectionState::instance(), - _ofxPiMapper->getGui(), GuiMode::SOURCE_SELECTION)); + &_gui, GuiMode::SOURCE_SELECTION)); break; case 'f': @@ -68,16 +77,16 @@ void Application::onKeyPressed(ofKeyEventArgs & args){ break; case 'i': - _ofxPiMapper->getInfo()->toggle(); + _info.toggle(); break; case 's': - _ofxPiMapper->getSurfaceManager()->saveXmlSettings( + _surfaceManager.saveXmlSettings( PIMAPPER_USER_SURFACES_XML_FILE); break; case 'z': - _ofxPiMapper->getCmdManager()->undo(); + _cmdManager.undo(); break; default: @@ -87,9 +96,25 @@ void Application::onKeyPressed(ofKeyEventArgs & args){ } } +void Application::addFboSource(FboSource & fboSource){ + _mediaServer.addFboSource(fboSource); +} + void Application::setState(ApplicationBaseState * st){ _state = st; } +bool Application::loadXmlSettings(string fileName){ + if(!ofFile::doesFileExist(fileName)){ + ofLogError("Application::loadXmlSettings()") << fileName << " does not exist"; + return false; + } + if(!_surfaceManager.loadXmlSettings(fileName)){ + ofLogError("Application::loadXmlSettings()") << "Failed to load " << fileName << endl; + return false; + } + return true; +} + } // namespace piMapper } // namespace ofx diff --git a/src/Application/Application.h b/src/Application/Application.h index af40dd3..3b8a5d7 100644 --- a/src/Application/Application.h +++ b/src/Application/Application.h @@ -3,8 +3,6 @@ #include "ofEvents.h" #include "ofLog.h" -#include "ofxPiMapper.h" - #include "SetApplicationStateCmd.h" #include "ApplicationBaseState.h" @@ -12,11 +10,16 @@ #include "ProjectionMappingState.h" #include "TextureMappingState.h" #include "SourceSelectionState.h" +#include "FboSource.h" +#include "Info.h" + +#include "SurfaceManagerGui.h" // TODO: To be removed. #include "GuiMode.h" -class ofxPiMapper; +#define PIMAPPER_DEF_SURFACES_XML_FILE "defaultSurfaces.xml" +#define PIMAPPER_USER_SURFACES_XML_FILE "surfaces.xml" namespace ofx { namespace piMapper { @@ -26,14 +29,21 @@ class ApplicationBaseState; class Application { public: - Application(ofxPiMapper * opm); + Application(); ~Application(); ApplicationBaseState * getState(); - ofxPiMapper * getOfxPiMapper(); // Temporary method. + void setup(); void draw(); void onKeyPressed(ofKeyEventArgs & args); + void addFboSource(FboSource & fboSource); + + bool loadXmlSettings(string fileName); + + SurfaceManagerGui * getGui(){ return &_gui; }; + SurfaceManager * getSurfaceManager(){ return &_surfaceManager; }; + CmdManager * getCmdManager(){ return &_cmdManager; }; protected: void setState(ApplicationBaseState * st); @@ -43,7 +53,12 @@ class Application { friend class SetApplicationStateCmd; ApplicationBaseState * _state; - ofxPiMapper * _ofxPiMapper; + + CmdManager _cmdManager; + SurfaceManagerGui _gui; + MediaServer _mediaServer; + SurfaceManager _surfaceManager; + Info _info; }; diff --git a/src/Application/PresentationState.cpp b/src/Application/PresentationState.cpp index 83979ed..6f5f7e1 100644 --- a/src/Application/PresentationState.cpp +++ b/src/Application/PresentationState.cpp @@ -12,7 +12,9 @@ PresentationState * PresentationState::instance(){ return _instance; } -void PresentationState::draw(Application * app){} +void PresentationState::draw(Application * app){ + app->getGui()->draw(); +} } // namespace piMapper } // namespace ofx \ No newline at end of file diff --git a/src/Application/ProjectionMappingState.cpp b/src/Application/ProjectionMappingState.cpp index f65413b..907036d 100644 --- a/src/Application/ProjectionMappingState.cpp +++ b/src/Application/ProjectionMappingState.cpp @@ -12,43 +12,45 @@ ProjectionMappingState * ProjectionMappingState::instance(){ return _instance; } -void ProjectionMappingState::draw(Application * app){} +void ProjectionMappingState::draw(Application * app){ + app->getGui()->draw(); +} void ProjectionMappingState::onKeyPressed(Application * app, ofKeyEventArgs & args){ switch(args.key){ case 't': - app->getOfxPiMapper()->getCmdManager()->exec( + app->getCmdManager()->exec( new AddSurfaceCmd( - app->getOfxPiMapper(), + app->getSurfaceManager(), SurfaceType::TRIANGLE_SURFACE) ); break; case 'q': - app->getOfxPiMapper()->getCmdManager()->exec( + app->getCmdManager()->exec( new AddSurfaceCmd( - app->getOfxPiMapper(), + app->getSurfaceManager(), SurfaceType::QUAD_SURFACE) ); break; case OF_KEY_BACKSPACE: - app->getOfxPiMapper()->getCmdManager()->exec( - new RmSurfaceCmd(app->getOfxPiMapper())); + app->getCmdManager()->exec( + new RmSurfaceCmd(app->getSurfaceManager())); break; case 'p': - if(app->getOfxPiMapper()->getSurfaceManager()->getSelectedSurface() == 0){ + if(app->getSurfaceManager()->getSelectedSurface() == 0){ break; } - if(app->getOfxPiMapper()->getSurfaceManager()->getSelectedSurface()->getType() == + if(app->getSurfaceManager()->getSelectedSurface()->getType() == SurfaceType::QUAD_SURFACE){ - app->getOfxPiMapper()->getCmdManager()->exec( + app->getCmdManager()->exec( new TogglePerspectiveCmd( - (QuadSurface *)app->getOfxPiMapper()->getSurfaceManager()->getSelectedSurface() ) ); + (QuadSurface *)app->getSurfaceManager()->getSelectedSurface() ) ); } break; diff --git a/src/Application/ProjectionMappingState.h b/src/Application/ProjectionMappingState.h index 1d7ec42..bdea4f5 100644 --- a/src/Application/ProjectionMappingState.h +++ b/src/Application/ProjectionMappingState.h @@ -5,6 +5,7 @@ #include "ofLog.h" #include "ofGraphics.h" #include "AddSurfaceCmd.h" +#include "RmSurfaceCmd.h" #include "TogglePerspectiveCmd.h" #include "SurfaceType.h" diff --git a/src/Application/SourceSelectionState.cpp b/src/Application/SourceSelectionState.cpp index 35ee443..7019b07 100644 --- a/src/Application/SourceSelectionState.cpp +++ b/src/Application/SourceSelectionState.cpp @@ -12,7 +12,9 @@ SourceSelectionState * SourceSelectionState::instance(){ return _instance; } -void SourceSelectionState::draw(Application * app){} +void SourceSelectionState::draw(Application * app){ + app->getGui()->draw(); +} } // namespace piMapper } // namespace ofx \ No newline at end of file diff --git a/src/Application/TextureMappingState.cpp b/src/Application/TextureMappingState.cpp index 2b504c1..a347ea9 100644 --- a/src/Application/TextureMappingState.cpp +++ b/src/Application/TextureMappingState.cpp @@ -12,7 +12,9 @@ TextureMappingState * TextureMappingState::instance(){ return _instance; } -void TextureMappingState::draw(Application * app){} +void TextureMappingState::draw(Application * app){ + app->getGui()->draw(); +} } // namespace piMapper } // namespace ofx \ No newline at end of file diff --git a/src/Commands/AddSurfaceCmd.cpp b/src/Commands/AddSurfaceCmd.cpp index 49539b4..23fde7f 100644 --- a/src/Commands/AddSurfaceCmd.cpp +++ b/src/Commands/AddSurfaceCmd.cpp @@ -3,19 +3,19 @@ namespace ofx { namespace piMapper { -AddSurfaceCmd::AddSurfaceCmd(ofxPiMapper * app, int surfaceType){ - _app = app; +AddSurfaceCmd::AddSurfaceCmd(SurfaceManager * sm, int surfaceType){ + _surfaceManager = sm; _surfaceType = surfaceType; } void AddSurfaceCmd::exec(){ BaseSurface * surface = SurfaceFactory::instance()->createSurface(_surfaceType); - _app->getSurfaceManager()->addSurface(surface); + _surfaceManager->addSurface(surface); } void AddSurfaceCmd::undo(){ ofLogNotice("AddSurfaceCmd", "undo"); - _app->getSurfaceManager()->removeSurface(); + _surfaceManager->removeSurface(); } } // namespace piMapper diff --git a/src/Commands/AddSurfaceCmd.h b/src/Commands/AddSurfaceCmd.h index a7444c4..cd4497c 100644 --- a/src/Commands/AddSurfaceCmd.h +++ b/src/Commands/AddSurfaceCmd.h @@ -1,6 +1,6 @@ #pragma once -#include "ofxPiMapper.h" +#include "SurfaceManager.h" #include "BaseCmd.h" #include "SurfaceType.h" #include "BaseSurface.h" @@ -14,18 +14,14 @@ namespace piMapper { class AddSurfaceCmd : public BaseUndoCmd { public: - AddSurfaceCmd(ofxPiMapper * app, int surfaceType); + AddSurfaceCmd(SurfaceManager * sm, int surfaceType); void exec(); void undo(); private: - ofxPiMapper * _app; + SurfaceManager * _surfaceManager; int _surfaceType; - // TODO: Should use some kind of factory class here - void addTriangleSurface(); - void addQuadSurface(); - }; } // namespace piMapper diff --git a/src/Commands/RmSurfaceCmd.cpp b/src/Commands/RmSurfaceCmd.cpp index 41ce11d..d997db1 100644 --- a/src/Commands/RmSurfaceCmd.cpp +++ b/src/Commands/RmSurfaceCmd.cpp @@ -3,16 +3,16 @@ namespace ofx { namespace piMapper { -RmSurfaceCmd::RmSurfaceCmd(ofxPiMapper * app){ - _app = app; +RmSurfaceCmd::RmSurfaceCmd(SurfaceManager * sm){ + _surfaceManager = sm; _surface = 0; } void RmSurfaceCmd::exec(){ // Store the surface, this implies that the surfaceManager's // removeSelectedSurface does not destroy the surface. - _surface = _app->getSurfaceManager()->getSelectedSurface(); - _app->getSurfaceManager()->removeSelectedSurface(); + _surface = _surfaceManager->getSelectedSurface(); + _surfaceManager->removeSelectedSurface(); } void RmSurfaceCmd::undo(){ @@ -20,8 +20,8 @@ void RmSurfaceCmd::undo(){ if(_surface == 0){ ofLogError("RmSurfaceCmd", "No surface stored"); } - _app->getSurfaceManager()->addSurface(_surface); - _app->getSurfaceManager()->selectSurface(_surface); + _surfaceManager->addSurface(_surface); + _surfaceManager->selectSurface(_surface); _surface = 0; } diff --git a/src/Commands/RmSurfaceCmd.h b/src/Commands/RmSurfaceCmd.h index 862cb4e..6d7d71a 100644 --- a/src/Commands/RmSurfaceCmd.h +++ b/src/Commands/RmSurfaceCmd.h @@ -4,7 +4,7 @@ #pragma once -#include "ofxPiMapper.h" +#include "SurfaceManager.h" #include "BaseCmd.h" #include "BaseSurface.h" @@ -16,12 +16,12 @@ namespace piMapper { class RmSurfaceCmd : public BaseUndoCmd { public: - RmSurfaceCmd(ofxPiMapper * app); + RmSurfaceCmd(SurfaceManager * sm); void exec(); void undo(); private: - ofxPiMapper * _app; + SurfaceManager * _surfaceManager; BaseSurface * _surface; }; diff --git a/src/Commands/SetApplicationStateCmd.h b/src/Commands/SetApplicationStateCmd.h index bd497cf..0776215 100644 --- a/src/Commands/SetApplicationStateCmd.h +++ b/src/Commands/SetApplicationStateCmd.h @@ -2,6 +2,7 @@ #include "BaseCmd.h" #include "Application.h" +#include "SurfaceManagerGui.h" namespace ofx { namespace piMapper { diff --git a/src/ofxPiMapper.cpp b/src/ofxPiMapper.cpp index 2dd5f01..c5e49e0 100644 --- a/src/ofxPiMapper.cpp +++ b/src/ofxPiMapper.cpp @@ -1,72 +1,17 @@ #include "ofxPiMapper.h" ofxPiMapper::ofxPiMapper(){ - _setupComplete = false; - _gui = new ofx::piMapper::SurfaceManagerGui(); - _cmdManager = new ofx::piMapper::CmdManager(); - _mediaServer = new ofx::piMapper::MediaServer(); - _surfaceManager = new ofx::piMapper::SurfaceManager(); - _info = new ofx::piMapper::Info(); + _application = new ofx::piMapper::Application(); } void ofxPiMapper::setup(){ - ofLogNotice("ofxPiMapper") << "Setting up..."; - - _surfaceManager->setMediaServer(_mediaServer); - _gui->setMediaServer(_mediaServer); - _gui->setCmdManager(_cmdManager); - - if(!loadXmlSettings(PIMAPPER_USER_SURFACES_XML_FILE)){ - ofLogWarning("ofxPiMapper::setup()") << "Failed to load user settings" << endl; - if(!loadXmlSettings(PIMAPPER_DEF_SURFACES_XML_FILE)){ - ofLogWarning("ofxPiMapper::setup()") << "Failed to load default settings" << endl; - } - } - - _gui->setSurfaceManager(_surfaceManager); - _application = new ofx::piMapper::Application(this); - - _setupComplete = true; - ofLogNotice("ofxPiMapper") << "Setup complete"; + _application->setup(); } void ofxPiMapper::draw(){ - if(!_setupComplete){ - return; - } - _gui->draw(); _application->draw(); - _info->draw(); } void ofxPiMapper::registerFboSource(ofx::piMapper::FboSource & fboSource){ - _mediaServer->addFboSource(fboSource); -} - -bool ofxPiMapper::loadXmlSettings(string fileName){ - if(!ofFile::doesFileExist(fileName)){ - ofLogError("ofxPiMapper::loadXmlSettings()") << fileName << " does not exist"; - return false; - } - if(!_surfaceManager->loadXmlSettings(fileName)){ - ofLogError("ofxPiMapper::loadXmlSettings()") << "Failed to load " << fileName << endl; - return false; - } - return true; -} - -ofx::piMapper::CmdManager * ofxPiMapper::getCmdManager(){ - return _cmdManager; -} - -ofx::piMapper::SurfaceManagerGui * ofxPiMapper::getGui(){ - return _gui; -} - -ofx::piMapper::SurfaceManager * ofxPiMapper::getSurfaceManager(){ - return _surfaceManager; -} - -ofx::piMapper::Info * ofxPiMapper::getInfo(){ - return _info; + _application->addFboSource(fboSource); } \ No newline at end of file diff --git a/src/ofxPiMapper.h b/src/ofxPiMapper.h index 5f88956..2eb2a01 100644 --- a/src/ofxPiMapper.h +++ b/src/ofxPiMapper.h @@ -11,9 +11,6 @@ #include "Application.h" #include "Info.h" -#define PIMAPPER_DEF_SURFACES_XML_FILE "defaultSurfaces.xml" -#define PIMAPPER_USER_SURFACES_XML_FILE "surfaces.xml" - namespace ofx { namespace piMapper { class Application; @@ -31,25 +28,6 @@ class ofxPiMapper { void registerFboSource(ofx::piMapper::FboSource & fboSource); bool loadXmlSettings(string fileName); - ofx::piMapper::CmdManager * getCmdManager(); - ofx::piMapper::SurfaceManagerGui * getGui(); - ofx::piMapper::SurfaceManager * getSurfaceManager(); - ofx::piMapper::Info * getInfo(); - - // TODO: Redesign ofxPiMapper so that there is a separation between - // data structures like triangle and quad surfaces and their GUIs. - // Trianlge and quad surfaces should be still able to draw themselves - // by using their individual draw methods. The GUI layer would consume - // triangle and quad surface lists to construct interactive user - // interface on top of them. - private: - bool _setupComplete; - - ofx::piMapper::CmdManager * _cmdManager; - ofx::piMapper::MediaServer * _mediaServer; - ofx::piMapper::SurfaceManager * _surfaceManager; - ofx::piMapper::SurfaceManagerGui * _gui; ofx::piMapper::Application * _application; - ofx::piMapper::Info * _info; }; \ No newline at end of file