Browse Source

Move all essential application parts to `Application` class

master
Krisjanis Rijnieks 9 years ago
parent
commit
0d6106b22a
  1. 61
      src/Application/Application.cpp
  2. 27
      src/Application/Application.h
  3. 4
      src/Application/PresentationState.cpp
  4. 24
      src/Application/ProjectionMappingState.cpp
  5. 1
      src/Application/ProjectionMappingState.h
  6. 4
      src/Application/SourceSelectionState.cpp
  7. 4
      src/Application/TextureMappingState.cpp
  8. 8
      src/Commands/AddSurfaceCmd.cpp
  9. 10
      src/Commands/AddSurfaceCmd.h
  10. 12
      src/Commands/RmSurfaceCmd.cpp
  11. 6
      src/Commands/RmSurfaceCmd.h
  12. 1
      src/Commands/SetApplicationStateCmd.h
  13. 61
      src/ofxPiMapper.cpp
  14. 22
      src/ofxPiMapper.h

61
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

27
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;
};

4
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

24
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;

1
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"

4
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

4
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

8
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

10
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

12
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;
}

6
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;
};

1
src/Commands/SetApplicationStateCmd.h

@ -2,6 +2,7 @@
#include "BaseCmd.h"
#include "Application.h"
#include "SurfaceManagerGui.h"
namespace ofx {
namespace piMapper {

61
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);
}

22
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;
};
Loading…
Cancel
Save