Browse Source

Add AddSurfaceCmd and move surface creation key commands

- from ofxPiMapper to application state machine
master
Krisjanis Rijnieks 10 years ago
parent
commit
8867edb8f2
  1. 6
      example/example.xcodeproj/project.pbxproj
  2. 6
      src/Application/Application.cpp
  3. 3
      src/Application/Application.h
  4. 2
      src/Application/ApplicationBaseState.h
  5. 29
      src/Application/ProjectionMappingState.cpp
  6. 3
      src/Application/ProjectionMappingState.h
  7. 61
      src/Commands/AddSurfaceCmd.cpp
  8. 31
      src/Commands/AddSurfaceCmd.h
  9. 8
      src/Surfaces/SurfaceManager.cpp
  10. 2
      src/Surfaces/SurfaceManager.h
  11. 35
      src/ofxPiMapper.cpp
  12. 5
      src/ofxPiMapper.h

6
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 = "<group>"; };
396841731BC91F6F009F0BAE /* TextureMappingState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TextureMappingState.cpp; sourceTree = "<group>"; };
396841741BC91F6F009F0BAE /* TextureMappingState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextureMappingState.h; sourceTree = "<group>"; };
396841801BC93DDC009F0BAE /* AddSurfaceCmd.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = AddSurfaceCmd.cpp; path = Commands/AddSurfaceCmd.cpp; sourceTree = "<group>"; };
396841811BC93DDC009F0BAE /* AddSurfaceCmd.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AddSurfaceCmd.h; path = Commands/AddSurfaceCmd.h; sourceTree = "<group>"; };
397EFC7B1A08E7680009286E /* ofxPiMapper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofxPiMapper.cpp; sourceTree = "<group>"; };
397EFC7D1A08FE720009286E /* FboSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FboSource.cpp; sourceTree = "<group>"; };
397EFC7E1A08FE720009286E /* FboSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FboSource.h; sourceTree = "<group>"; };
@ -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 */,

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

3
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);

2
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

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

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

61
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<ofVec2f> 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<ofVec2f> 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<ofVec2f> 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<ofVec2f> 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

31
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

8
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()) {

2
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);

35
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();

5
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();

Loading…
Cancel
Save