From 921b2e20da6382090fdec0c62551ea1fdb92a495 Mon Sep 17 00:00:00 2001 From: Felix Dubrownik Date: Sun, 7 Sep 2014 20:38:39 +0200 Subject: [PATCH 1/4] Renamed the files --- src/{ofxBaseJoint.cpp => BaseJoint.cpp} | 0 src/{ofxBaseJoint.h => BaseJoint.h} | 0 src/{ofxBaseSurface.cpp => BaseSurface.cpp} | 0 src/{ofxBaseSurface.h => BaseSurface.h} | 0 src/{ofxCircleJoint.cpp => CircleJoint.cpp} | 0 src/{ofxCircleJoint.h => CircleJoint.h} | 0 src/{ofxEditorType.h => EditorType.h} | 0 src/{ofxGuiMode.h => GuiMode.h} | 0 src/{ofxProjectionEditor.cpp => ProjectionEditor.cpp} | 0 src/{ofxProjectionEditor.h => ProjectionEditor.h} | 0 src/{ofxQuadSurface.cpp => QuadSurface.cpp} | 0 src/{ofxQuadSurface.h => QuadSurface.h} | 0 src/{ofxSourcesEditor.cpp => SourcesEditor.cpp} | 0 src/{ofxSourcesEditor.h => SourcesEditor.h} | 0 src/{ofxSurfaceManager.cpp => SurfaceManager.cpp} | 0 src/{ofxSurfaceManager.h => SurfaceManager.h} | 0 src/{ofxSurfaceManagerGui.cpp => SurfaceManagerGui.cpp} | 0 src/{ofxSurfaceManagerGui.h => SurfaceManagerGui.h} | 0 src/{ofxSurfaceType.h => SurfaceType.h} | 0 src/{ofxTextureEditor.cpp => TextureEditor.cpp} | 0 src/{ofxTextureEditor.h => TextureEditor.h} | 0 src/{ofxTriangleSurface.cpp => TriangleSurface.cpp} | 0 src/{ofxTriangleSurface.h => TriangleSurface.h} | 0 23 files changed, 0 insertions(+), 0 deletions(-) rename src/{ofxBaseJoint.cpp => BaseJoint.cpp} (100%) rename src/{ofxBaseJoint.h => BaseJoint.h} (100%) rename src/{ofxBaseSurface.cpp => BaseSurface.cpp} (100%) rename src/{ofxBaseSurface.h => BaseSurface.h} (100%) rename src/{ofxCircleJoint.cpp => CircleJoint.cpp} (100%) rename src/{ofxCircleJoint.h => CircleJoint.h} (100%) rename src/{ofxEditorType.h => EditorType.h} (100%) rename src/{ofxGuiMode.h => GuiMode.h} (100%) rename src/{ofxProjectionEditor.cpp => ProjectionEditor.cpp} (100%) rename src/{ofxProjectionEditor.h => ProjectionEditor.h} (100%) rename src/{ofxQuadSurface.cpp => QuadSurface.cpp} (100%) rename src/{ofxQuadSurface.h => QuadSurface.h} (100%) rename src/{ofxSourcesEditor.cpp => SourcesEditor.cpp} (100%) rename src/{ofxSourcesEditor.h => SourcesEditor.h} (100%) rename src/{ofxSurfaceManager.cpp => SurfaceManager.cpp} (100%) rename src/{ofxSurfaceManager.h => SurfaceManager.h} (100%) rename src/{ofxSurfaceManagerGui.cpp => SurfaceManagerGui.cpp} (100%) rename src/{ofxSurfaceManagerGui.h => SurfaceManagerGui.h} (100%) rename src/{ofxSurfaceType.h => SurfaceType.h} (100%) rename src/{ofxTextureEditor.cpp => TextureEditor.cpp} (100%) rename src/{ofxTextureEditor.h => TextureEditor.h} (100%) rename src/{ofxTriangleSurface.cpp => TriangleSurface.cpp} (100%) rename src/{ofxTriangleSurface.h => TriangleSurface.h} (100%) diff --git a/src/ofxBaseJoint.cpp b/src/BaseJoint.cpp similarity index 100% rename from src/ofxBaseJoint.cpp rename to src/BaseJoint.cpp diff --git a/src/ofxBaseJoint.h b/src/BaseJoint.h similarity index 100% rename from src/ofxBaseJoint.h rename to src/BaseJoint.h diff --git a/src/ofxBaseSurface.cpp b/src/BaseSurface.cpp similarity index 100% rename from src/ofxBaseSurface.cpp rename to src/BaseSurface.cpp diff --git a/src/ofxBaseSurface.h b/src/BaseSurface.h similarity index 100% rename from src/ofxBaseSurface.h rename to src/BaseSurface.h diff --git a/src/ofxCircleJoint.cpp b/src/CircleJoint.cpp similarity index 100% rename from src/ofxCircleJoint.cpp rename to src/CircleJoint.cpp diff --git a/src/ofxCircleJoint.h b/src/CircleJoint.h similarity index 100% rename from src/ofxCircleJoint.h rename to src/CircleJoint.h diff --git a/src/ofxEditorType.h b/src/EditorType.h similarity index 100% rename from src/ofxEditorType.h rename to src/EditorType.h diff --git a/src/ofxGuiMode.h b/src/GuiMode.h similarity index 100% rename from src/ofxGuiMode.h rename to src/GuiMode.h diff --git a/src/ofxProjectionEditor.cpp b/src/ProjectionEditor.cpp similarity index 100% rename from src/ofxProjectionEditor.cpp rename to src/ProjectionEditor.cpp diff --git a/src/ofxProjectionEditor.h b/src/ProjectionEditor.h similarity index 100% rename from src/ofxProjectionEditor.h rename to src/ProjectionEditor.h diff --git a/src/ofxQuadSurface.cpp b/src/QuadSurface.cpp similarity index 100% rename from src/ofxQuadSurface.cpp rename to src/QuadSurface.cpp diff --git a/src/ofxQuadSurface.h b/src/QuadSurface.h similarity index 100% rename from src/ofxQuadSurface.h rename to src/QuadSurface.h diff --git a/src/ofxSourcesEditor.cpp b/src/SourcesEditor.cpp similarity index 100% rename from src/ofxSourcesEditor.cpp rename to src/SourcesEditor.cpp diff --git a/src/ofxSourcesEditor.h b/src/SourcesEditor.h similarity index 100% rename from src/ofxSourcesEditor.h rename to src/SourcesEditor.h diff --git a/src/ofxSurfaceManager.cpp b/src/SurfaceManager.cpp similarity index 100% rename from src/ofxSurfaceManager.cpp rename to src/SurfaceManager.cpp diff --git a/src/ofxSurfaceManager.h b/src/SurfaceManager.h similarity index 100% rename from src/ofxSurfaceManager.h rename to src/SurfaceManager.h diff --git a/src/ofxSurfaceManagerGui.cpp b/src/SurfaceManagerGui.cpp similarity index 100% rename from src/ofxSurfaceManagerGui.cpp rename to src/SurfaceManagerGui.cpp diff --git a/src/ofxSurfaceManagerGui.h b/src/SurfaceManagerGui.h similarity index 100% rename from src/ofxSurfaceManagerGui.h rename to src/SurfaceManagerGui.h diff --git a/src/ofxSurfaceType.h b/src/SurfaceType.h similarity index 100% rename from src/ofxSurfaceType.h rename to src/SurfaceType.h diff --git a/src/ofxTextureEditor.cpp b/src/TextureEditor.cpp similarity index 100% rename from src/ofxTextureEditor.cpp rename to src/TextureEditor.cpp diff --git a/src/ofxTextureEditor.h b/src/TextureEditor.h similarity index 100% rename from src/ofxTextureEditor.h rename to src/TextureEditor.h diff --git a/src/ofxTriangleSurface.cpp b/src/TriangleSurface.cpp similarity index 100% rename from src/ofxTriangleSurface.cpp rename to src/TriangleSurface.cpp diff --git a/src/ofxTriangleSurface.h b/src/TriangleSurface.h similarity index 100% rename from src/ofxTriangleSurface.h rename to src/TriangleSurface.h From 4605d05756cf6518c52574eacbe905f19a8b38a5 Mon Sep 17 00:00:00 2001 From: Felix Dubrownik Date: Sun, 7 Sep 2014 20:39:21 +0200 Subject: [PATCH 2/4] Renamed RadioList --- src/ui/{ofxRadioList.cpp => RadioList.cpp} | 0 src/ui/{ofxRadioList.h => RadioList.h} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/ui/{ofxRadioList.cpp => RadioList.cpp} (100%) rename src/ui/{ofxRadioList.h => RadioList.h} (100%) diff --git a/src/ui/ofxRadioList.cpp b/src/ui/RadioList.cpp similarity index 100% rename from src/ui/ofxRadioList.cpp rename to src/ui/RadioList.cpp diff --git a/src/ui/ofxRadioList.h b/src/ui/RadioList.h similarity index 100% rename from src/ui/ofxRadioList.h rename to src/ui/RadioList.h From 66283175f14ff4661b0bb59300051955c9686929 Mon Sep 17 00:00:00 2001 From: Felix Dubrownik Date: Sun, 7 Sep 2014 21:48:09 +0200 Subject: [PATCH 3/4] Refactoring: Renaming Classes, introducing namespace --- example/src/ofApp.cpp | 14 +++--- example/src/ofApp.h | 11 ++--- src/BaseJoint.cpp | 49 +++++++++++--------- src/BaseJoint.h | 90 +++++++++++++++++++------------------ src/BaseSurface.cpp | 21 +++++---- src/BaseSurface.h | 11 ++--- src/CircleJoint.cpp | 19 +++++--- src/CircleJoint.h | 15 ++++--- src/EditorType.h | 9 ++-- src/GuiMode.h | 10 ++--- src/ProjectionEditor.cpp | 87 +++++++++++++++++++----------------- src/ProjectionEditor.h | 25 ++++++----- src/QuadSurface.cpp | 48 +++++++++++--------- src/QuadSurface.h | 17 +++---- src/SourcesEditor.cpp | 44 +++++++++--------- src/SourcesEditor.h | 23 +++++----- src/SurfaceManager.cpp | 94 ++++++++++++++++++++------------------- src/SurfaceManager.h | 35 ++++++++------- src/SurfaceManagerGui.cpp | 90 +++++++++++++++++++------------------ src/SurfaceManagerGui.h | 34 +++++++------- src/SurfaceType.h | 9 ++-- src/TextureEditor.cpp | 68 +++++++++++++++------------- src/TextureEditor.h | 27 ++++++----- src/TriangleSurface.cpp | 46 ++++++++++--------- src/TriangleSurface.h | 18 ++++---- src/ofxPiMapper.h | 9 ++-- src/ui/RadioList.cpp | 70 +++++++++++++++-------------- src/ui/RadioList.h | 18 +++++--- 28 files changed, 536 insertions(+), 475 deletions(-) diff --git a/example/src/ofApp.cpp b/example/src/ofApp.cpp index 2cab8a6..18e5777 100755 --- a/example/src/ofApp.cpp +++ b/example/src/ofApp.cpp @@ -85,10 +85,10 @@ void ofApp::keyPressed(int key) cout << "Key pressed: " << static_cast(key) << endl; switch (key) { - case '1': gui.setMode(ofxGuiMode::NONE); break; - case '2': gui.setMode(ofxGuiMode::TEXTURE_MAPPING); break; - case '3': gui.setMode(ofxGuiMode::PROJECTION_MAPPING); break; - case '4': gui.setMode(ofxGuiMode::SOURCE_SELECTION); break; + case '1': gui.setMode(ofx::piMapper::GuiMode::NONE); break; + case '2': gui.setMode(ofx::piMapper::GuiMode::TEXTURE_MAPPING); break; + case '3': gui.setMode(ofx::piMapper::GuiMode::PROJECTION_MAPPING); break; + case '4': gui.setMode(ofx::piMapper::GuiMode::SOURCE_SELECTION); break; case 'i': bShowInfo = !bShowInfo; break; case 'r': addRandomSurface(); break; case 'q': addQuadSurface(); break; @@ -103,7 +103,7 @@ void ofApp::keyPressed(int key) void ofApp::addRandomSurface() { - int surfaceType = ofxSurfaceType::TRIANGLE_SURFACE; + int surfaceType = ofx::piMapper::SurfaceType::TRIANGLE_SURFACE; vector vertices; vertices.push_back( ofVec2f( ofRandomWidth(), ofRandomHeight() ) ); vertices.push_back( ofVec2f( ofRandomWidth(), ofRandomHeight() ) ); @@ -120,7 +120,7 @@ void ofApp::addRandomSurface() void ofApp::addQuadSurface() { - int surfaceType = ofxSurfaceType::QUAD_SURFACE; + int surfaceType = ofx::piMapper::SurfaceType::QUAD_SURFACE; vector vertices; int border = 50; @@ -143,7 +143,7 @@ void ofApp::addQuadSurface() void ofApp::addSurface() { - int surfaceType = ofxSurfaceType::TRIANGLE_SURFACE; + int surfaceType = ofx::piMapper::SurfaceType::TRIANGLE_SURFACE; vector vertices; vertices.push_back( ofVec2f( (float)ofGetWidth()/2.0f, 0.0f ) ); vertices.push_back( ofVec2f( (float)ofGetWidth(), (float)ofGetHeight() ) ); diff --git a/example/src/ofApp.h b/example/src/ofApp.h index 05d7a8f..10c1c99 100755 --- a/example/src/ofApp.h +++ b/example/src/ofApp.h @@ -1,5 +1,4 @@ -#ifndef H_OF_APP -#define H_OF_APP +#pragma once #include "ofMain.h" #include "ofxPiMapper.h" @@ -20,12 +19,10 @@ public: void setFboAsTexture(); ofImage image; - ofxSurfaceManager surfaceManager; - ofxSurfaceManagerGui gui; + ofx::piMapper::SurfaceManager surfaceManager; + ofx::piMapper::SurfaceManagerGui gui; bool bShowInfo; ofFbo* fbo; vector rects; vector rectSpeeds; -}; - -#endif \ No newline at end of file +}; \ No newline at end of file diff --git a/src/BaseJoint.cpp b/src/BaseJoint.cpp index 22d147d..19c490a 100644 --- a/src/BaseJoint.cpp +++ b/src/BaseJoint.cpp @@ -1,30 +1,33 @@ -#include "ofxBaseJoint.h" +#include "BaseJoint.h" -ofxBaseJoint::ofxBaseJoint() +namespace ofx{ +namespace piMapper{ + +BaseJoint::BaseJoint() { setDefaultColors(); setDefaultProperties(); registerMouseEvents(); } -ofxBaseJoint::~ofxBaseJoint() +BaseJoint::~BaseJoint() { unregisterMouseEvents(); } -void ofxBaseJoint::registerMouseEvents() +void BaseJoint::registerMouseEvents() { - ofAddListener(ofEvents().mousePressed, this, &ofxBaseJoint::mousePressed); - ofAddListener(ofEvents().mouseDragged, this, &ofxBaseJoint::mouseDragged); + ofAddListener(ofEvents().mousePressed, this, &BaseJoint::mousePressed); + ofAddListener(ofEvents().mouseDragged, this, &BaseJoint::mouseDragged); } -void ofxBaseJoint::unregisterMouseEvents() +void BaseJoint::unregisterMouseEvents() { - ofRemoveListener(ofEvents().mousePressed, this, &ofxBaseJoint::mousePressed); - ofRemoveListener(ofEvents().mouseDragged, this, &ofxBaseJoint::mouseDragged); + ofRemoveListener(ofEvents().mousePressed, this, &BaseJoint::mousePressed); + ofRemoveListener(ofEvents().mouseDragged, this, &BaseJoint::mouseDragged); } -void ofxBaseJoint::mousePressed(ofMouseEventArgs& args) +void BaseJoint::mousePressed(ofMouseEventArgs& args) { if ( hitTest(ofVec2f(args.x, args.y)) ) { //selected = true; @@ -33,53 +36,53 @@ void ofxBaseJoint::mousePressed(ofMouseEventArgs& args) } } -void ofxBaseJoint::mouseReleased(int x, int y, int button) +void BaseJoint::mouseReleased(int x, int y, int button) { stopDrag(); } -void ofxBaseJoint::mouseDragged(ofMouseEventArgs& args) +void BaseJoint::mouseDragged(ofMouseEventArgs& args) { if ( !bDrag ) return; position = ofVec2f(args.x, args.y) + clickDistance; } -void ofxBaseJoint::startDrag() +void BaseJoint::startDrag() { bDrag = true; } -void ofxBaseJoint::stopDrag() +void BaseJoint::stopDrag() { bDrag = false; } -void ofxBaseJoint::select() +void BaseJoint::select() { selected = true; } -void ofxBaseJoint::unselect() +void BaseJoint::unselect() { selected = false; } -void ofxBaseJoint::setClickDistance(ofVec2f newClickDistance) +void BaseJoint::setClickDistance(ofVec2f newClickDistance) { clickDistance = newClickDistance; } -bool ofxBaseJoint::isDragged() +bool BaseJoint::isDragged() { return bDrag; } -bool ofxBaseJoint::isSelected() +bool BaseJoint::isSelected() { return selected; } -void ofxBaseJoint::setDefaultColors() +void BaseJoint::setDefaultColors() { fillColor = ofColor(0, 255, 255, 0); strokeColor = ofColor(255, 255, 255); @@ -87,7 +90,7 @@ void ofxBaseJoint::setDefaultColors() strokeColorSelected = ofColor(255, 0, 0); } -void ofxBaseJoint::setDefaultProperties() +void BaseJoint::setDefaultProperties() { enabled = true; visible = true; @@ -96,4 +99,6 @@ void ofxBaseJoint::setDefaultProperties() bDrag = false; selected = false; strokeWidth = 1.5f; -} \ No newline at end of file +} + +}} \ No newline at end of file diff --git a/src/BaseJoint.h b/src/BaseJoint.h index d3decb7..5e5824d 100644 --- a/src/BaseJoint.h +++ b/src/BaseJoint.h @@ -1,48 +1,50 @@ -#ifndef H_OFX_BASE_JOINT -#define H_OFX_BASE_JOINT +#pragma once #include "ofMain.h" -class ofxBaseJoint { -public: - ofxBaseJoint(); - ~ofxBaseJoint(); +namespace ofx{ +namespace piMapper{ - void registerMouseEvents(); - void unregisterMouseEvents(); - - ofVec2f position; - bool enabled; - bool visible; - bool selected; - - void mousePressed(ofMouseEventArgs& args); - void mouseReleased(int x, int y, int button); - void mouseDragged(ofMouseEventArgs& args); - void startDrag(); - void stopDrag(); - void select(); - void unselect(); - void setClickDistance(ofVec2f newClickDistance); - bool isDragged(); - bool isSelected(); - - virtual void update(){}; - virtual void draw(){}; - virtual bool hitTest(ofVec2f position){}; - -protected: - ofColor fillColor; - ofColor strokeColor; - ofColor fillColorSelected; - ofColor strokeColorSelected; - float strokeWidth; - ofVec2f clickDistance; // How far from the center of the joint the user has clicked? - bool bDrag; - -private: - void setDefaultColors(); - void setDefaultProperties(); -}; - -#endif + class BaseJoint { + public: + BaseJoint(); + ~BaseJoint(); + + void registerMouseEvents(); + void unregisterMouseEvents(); + + ofVec2f position; + bool enabled; + bool visible; + bool selected; + + void mousePressed(ofMouseEventArgs& args); + void mouseReleased(int x, int y, int button); + void mouseDragged(ofMouseEventArgs& args); + void startDrag(); + void stopDrag(); + void select(); + void unselect(); + void setClickDistance(ofVec2f newClickDistance); + bool isDragged(); + bool isSelected(); + + virtual void update(){}; + virtual void draw(){}; + virtual bool hitTest(ofVec2f position){}; + + protected: + ofColor fillColor; + ofColor strokeColor; + ofColor fillColorSelected; + ofColor strokeColorSelected; + float strokeWidth; + ofVec2f clickDistance; // How far from the center of the joint the user has clicked? + bool bDrag; + + private: + void setDefaultColors(); + void setDefaultProperties(); + }; +} +} diff --git a/src/BaseSurface.cpp b/src/BaseSurface.cpp index 67db7ef..7350b29 100644 --- a/src/BaseSurface.cpp +++ b/src/BaseSurface.cpp @@ -1,12 +1,15 @@ -#include "ofxBaseSurface.h" +#include "BaseSurface.h" -ofxBaseSurface::ofxBaseSurface() +namespace ofx{ +namespace piMapper{ + +BaseSurface::BaseSurface() { ofEnableNormalizedTexCoords(); createDefaultTexture(); } -void ofxBaseSurface::createDefaultTexture() +void BaseSurface::createDefaultTexture() { ofPixels pixels; pixels.allocate(500, 500, 1); @@ -38,7 +41,7 @@ void ofxBaseSurface::createDefaultTexture() texture = &defaultTexture; } -void ofxBaseSurface::drawTexture(ofVec2f position) +void BaseSurface::drawTexture(ofVec2f position) { ofMesh texMesh; texMesh.addVertex(position); @@ -56,17 +59,19 @@ void ofxBaseSurface::drawTexture(ofVec2f position) texture->unbind(); } -void ofxBaseSurface::setTexture(ofTexture *texturePtr) +void BaseSurface::setTexture(ofTexture *texturePtr) { texture = texturePtr; } -ofTexture* ofxBaseSurface::getTexture() +ofTexture* BaseSurface::getTexture() { return texture; } -ofTexture* ofxBaseSurface::getDefaultTexture() +ofTexture* BaseSurface::getDefaultTexture() { return &defaultTexture; -} \ No newline at end of file +} + +}} \ No newline at end of file diff --git a/src/BaseSurface.h b/src/BaseSurface.h index 9cd98b6..e8e5b8f 100644 --- a/src/BaseSurface.h +++ b/src/BaseSurface.h @@ -1,15 +1,16 @@ -#ifndef H_OFX_BASE_SURFACE -#define H_OFX_BASE_SURFACE +#pragma once #include "ofMain.h" #include using namespace std; -class ofxBaseSurface +namespace ofx{ + namespace piMapper{ +class BaseSurface { public: - ofxBaseSurface(); + BaseSurface(); virtual void setup(){}; virtual void draw(){}; virtual void setVertex(int index, ofVec2f p){}; @@ -37,4 +38,4 @@ protected: void createDefaultTexture(); }; -#endif \ No newline at end of file + }} \ No newline at end of file diff --git a/src/CircleJoint.cpp b/src/CircleJoint.cpp index 4abbc5c..938659f 100644 --- a/src/CircleJoint.cpp +++ b/src/CircleJoint.cpp @@ -1,16 +1,19 @@ -#include "ofxCircleJoint.h" +#include "CircleJoint.h" -ofxCircleJoint::ofxCircleJoint() +namespace ofx{ +namespace piMapper{ + +CircleJoint::CircleJoint() { setDefaultProperties(); } -void ofxCircleJoint::update() +void CircleJoint::update() { if (!enabled) return; } -void ofxCircleJoint::draw() +void CircleJoint::draw() { if (!visible) return; if (!enabled) return; @@ -38,14 +41,16 @@ void ofxCircleJoint::draw() ofPopStyle(); } -void ofxCircleJoint::setDefaultProperties() +void CircleJoint::setDefaultProperties() { radius = 10.0f; } -bool ofxCircleJoint::hitTest(ofVec2f pos) +bool CircleJoint::hitTest(ofVec2f pos) { float distance = position.distance(pos); if ( distance < radius ) return true; else return false; -} \ No newline at end of file +} + + }} \ No newline at end of file diff --git a/src/CircleJoint.h b/src/CircleJoint.h index f3016e9..812bc3c 100644 --- a/src/CircleJoint.h +++ b/src/CircleJoint.h @@ -1,13 +1,15 @@ -#ifndef H_OFX_CIRCLE_JOINT -#define H_OFX_CIRCLE_JOINT +#pragma once #include "ofMain.h" -#include "ofxBaseJoint.h" +#include "BaseJoint.h" -class ofxCircleJoint : public ofxBaseJoint + +namespace ofx{ +namespace piMapper{ +class CircleJoint : public BaseJoint { public: - ofxCircleJoint(); + CircleJoint(); void update(); void draw(); @@ -18,5 +20,4 @@ private: void setDefaultProperties(); }; - -#endif \ No newline at end of file +}} \ No newline at end of file diff --git a/src/EditorType.h b/src/EditorType.h index cc0c1c8..af5650b 100644 --- a/src/EditorType.h +++ b/src/EditorType.h @@ -1,7 +1,8 @@ -#ifndef H_OFX_EDITOR_TYPE -#define H_OFX_EDITOR_TYPE +#pragma once -struct ofxEditorType +namespace ofx{ + namespace piMapper{ +struct EditorType { enum { TEXTURE, @@ -9,4 +10,4 @@ struct ofxEditorType }; }; -#endif \ No newline at end of file +}} \ No newline at end of file diff --git a/src/GuiMode.h b/src/GuiMode.h index c60fe71..4c65a74 100644 --- a/src/GuiMode.h +++ b/src/GuiMode.h @@ -1,7 +1,8 @@ -#ifndef H_OFX_GUI_MODE -#define H_OFX_GUI_MODE +#pragma once -struct ofxGuiMode +namespace ofx{ + namespace piMapper{ +struct GuiMode { enum { NONE, @@ -10,5 +11,4 @@ struct ofxGuiMode SOURCE_SELECTION }; }; - -#endif \ No newline at end of file + }} \ No newline at end of file diff --git a/src/ProjectionEditor.cpp b/src/ProjectionEditor.cpp index 7560ba8..12adcca 100644 --- a/src/ProjectionEditor.cpp +++ b/src/ProjectionEditor.cpp @@ -1,6 +1,8 @@ -#include "ofxProjectionEditor.h" +#include "ProjectionEditor.h" -ofxProjectionEditor::ofxProjectionEditor() +namespace ofx{ + namespace piMapper{ +ProjectionEditor::ProjectionEditor() { surfaceManager = NULL; bShiftKeyDown = false; @@ -8,62 +10,62 @@ ofxProjectionEditor::ofxProjectionEditor() enable(); } -ofxProjectionEditor::~ofxProjectionEditor() +ProjectionEditor::~ProjectionEditor() { clearJoints(); surfaceManager = NULL; disable(); } -void ofxProjectionEditor::registerAppEvents() +void ProjectionEditor::registerAppEvents() { - ofAddListener(ofEvents().update, this, &ofxProjectionEditor::update); - ofAddListener(ofEvents().messageEvent, this, &ofxProjectionEditor::gotMessage); + ofAddListener(ofEvents().update, this, &ProjectionEditor::update); + ofAddListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage); } -void ofxProjectionEditor::unregisterAppEvents() +void ProjectionEditor::unregisterAppEvents() { - ofRemoveListener(ofEvents().update, this, &ofxProjectionEditor::update); - ofRemoveListener(ofEvents().messageEvent, this, &ofxProjectionEditor::gotMessage); + ofRemoveListener(ofEvents().update, this, &ProjectionEditor::update); + ofRemoveListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage); } -void ofxProjectionEditor::registerMouseEvents() +void ProjectionEditor::registerMouseEvents() { - ofAddListener(ofEvents().mouseDragged, this, &ofxProjectionEditor::mouseDragged); + ofAddListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged); } -void ofxProjectionEditor::unregisterMouseEvents() +void ProjectionEditor::unregisterMouseEvents() { - ofRemoveListener(ofEvents().mouseDragged, this, &ofxProjectionEditor::mouseDragged); + ofRemoveListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged); } -void ofxProjectionEditor::registerKeyEvents() +void ProjectionEditor::registerKeyEvents() { - ofAddListener(ofEvents().keyPressed, this, &ofxProjectionEditor::keyPressed); - ofAddListener(ofEvents().keyReleased, this, &ofxProjectionEditor::keyReleased); + ofAddListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); + ofAddListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased); } -void ofxProjectionEditor::unregisterKeyEvents() +void ProjectionEditor::unregisterKeyEvents() { - ofRemoveListener(ofEvents().keyPressed, this, &ofxProjectionEditor::keyPressed); - ofRemoveListener(ofEvents().keyReleased, this, &ofxProjectionEditor::keyReleased); + ofRemoveListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); + ofRemoveListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased); } -void ofxProjectionEditor::enable() +void ProjectionEditor::enable() { registerAppEvents(); registerMouseEvents(); registerKeyEvents(); } -void ofxProjectionEditor::disable() +void ProjectionEditor::disable() { unregisterAppEvents(); unregisterMouseEvents(); unregisterKeyEvents(); } -void ofxProjectionEditor::update(ofEventArgs &args) +void ProjectionEditor::update(ofEventArgs &args) { // update surface if one of the joints is being dragged for ( int i=0; igetSelectedSurface() == NULL ) return; @@ -91,14 +93,14 @@ void ofxProjectionEditor::draw() drawJoints(); } -void ofxProjectionEditor::mouseDragged(ofMouseEventArgs &args) +void ProjectionEditor::mouseDragged(ofMouseEventArgs &args) { ofVec2f mousePosition = ofVec2f(args.x, args.y); // Collect all vertices of the projection surfaces vector allVertices; for ( int i=0; isize(); i++ ) { - ofxBaseSurface* surface = surfaceManager->getSurface(i); + BaseSurface* surface = surfaceManager->getSurface(i); if ( surface == surfaceManager->getSelectedSurface() ) { continue; // Don't add vertices of selected surface } @@ -125,7 +127,7 @@ void ofxProjectionEditor::mouseDragged(ofMouseEventArgs &args) } } -void ofxProjectionEditor::keyPressed(ofKeyEventArgs &args) +void ProjectionEditor::keyPressed(ofKeyEventArgs &args) { int key = args.key; float moveStep; @@ -142,7 +144,7 @@ void ofxProjectionEditor::keyPressed(ofKeyEventArgs &args) } } -void ofxProjectionEditor::keyReleased(ofKeyEventArgs &args) +void ProjectionEditor::keyReleased(ofKeyEventArgs &args) { int key = args.key; switch (key) { @@ -150,7 +152,7 @@ void ofxProjectionEditor::keyReleased(ofKeyEventArgs &args) } } -void ofxProjectionEditor::gotMessage(ofMessage& msg) +void ProjectionEditor::gotMessage(ofMessage& msg) { if (msg.message == "surfaceSelected") { // refresh gui @@ -159,12 +161,12 @@ void ofxProjectionEditor::gotMessage(ofMessage& msg) } } -void ofxProjectionEditor::setSurfaceManager(ofxSurfaceManager *newSurfaceManager) +void ProjectionEditor::setSurfaceManager(SurfaceManager *newSurfaceManager) { surfaceManager = newSurfaceManager; } -void ofxProjectionEditor::clearJoints() +void ProjectionEditor::clearJoints() { while ( joints.size() ) { delete joints.back(); @@ -172,7 +174,7 @@ void ofxProjectionEditor::clearJoints() } } -void ofxProjectionEditor::createJoints() +void ProjectionEditor::createJoints() { if ( surfaceManager == NULL ) return; clearJoints(); @@ -185,12 +187,12 @@ void ofxProjectionEditor::createJoints() vector& vertices = surfaceManager->getSelectedSurface()->getVertices(); for ( int i=0; iposition = ofVec2f(vertices[i].x, vertices[i].y); } } -void ofxProjectionEditor::updateJoints() +void ProjectionEditor::updateJoints() { vector& vertices = surfaceManager->getSelectedSurface()->getVertices(); for ( int i=0; iunselect(); } } -void ofxProjectionEditor::moveSelectedSurface(ofVec2f by) +void ProjectionEditor::moveSelectedSurface(ofVec2f by) { if ( surfaceManager == NULL ) return; if ( surfaceManager->getSelectedSurface() == NULL ) return; @@ -217,18 +219,18 @@ void ofxProjectionEditor::moveSelectedSurface(ofVec2f by) updateJoints(); } -void ofxProjectionEditor::stopDragJoints() +void ProjectionEditor::stopDragJoints() { for (int i=0; istopDrag(); } } -void ofxProjectionEditor::moveSelection(ofVec2f by) +void ProjectionEditor::moveSelection(ofVec2f by) { // check if joints selected bool bJointSelected = false; - ofxBaseJoint* selectedJoint; + BaseJoint* selectedJoint; for ( int i=0; iisSelected()) { bJointSelected = true; @@ -244,12 +246,12 @@ void ofxProjectionEditor::moveSelection(ofVec2f by) } } -void ofxProjectionEditor::setSnapDistance(float newSnapDistance) +void ProjectionEditor::setSnapDistance(float newSnapDistance) { fSnapDistance = newSnapDistance; } -ofxCircleJoint* ofxProjectionEditor::hitTestJoints(ofVec2f pos) +CircleJoint* ProjectionEditor::hitTestJoints(ofVec2f pos) { for ( int i=0; ihitTest(pos) ){ @@ -259,9 +261,10 @@ ofxCircleJoint* ofxProjectionEditor::hitTestJoints(ofVec2f pos) return NULL; } -void ofxProjectionEditor::drawJoints() +void ProjectionEditor::drawJoints() { for ( int i=0; idraw(); } -} \ No newline at end of file +} + }} \ No newline at end of file diff --git a/src/ProjectionEditor.h b/src/ProjectionEditor.h index c4b95ec..6a7a877 100755 --- a/src/ProjectionEditor.h +++ b/src/ProjectionEditor.h @@ -1,14 +1,15 @@ -#ifndef H_OFX_PROJECTION_EDITOR -#define H_OFX_PROJECTION_EDITOR +#pragma once -#include "ofxSurfaceManager.h" -#include "ofxCircleJoint.h" +#include "SurfaceManager.h" +#include "CircleJoint.h" -class ofxProjectionEditor +namespace ofx{ + namespace piMapper{ +class ProjectionEditor { public: - ofxProjectionEditor(); - ~ofxProjectionEditor(); + ProjectionEditor(); + ~ProjectionEditor(); void registerAppEvents(); void unregisterAppEvents(); @@ -26,7 +27,7 @@ public: void keyPressed(ofKeyEventArgs& args); void keyReleased(ofKeyEventArgs& args); void gotMessage(ofMessage& msg); - void setSurfaceManager(ofxSurfaceManager* newSurfaceManager); + void setSurfaceManager(SurfaceManager* newSurfaceManager); void clearJoints(); void createJoints(); void updateJoints(); @@ -36,15 +37,15 @@ public: void updateVertices(); void moveSelection(ofVec2f by); void setSnapDistance(float newSnapDistance); - ofxCircleJoint* hitTestJoints(ofVec2f pos); + CircleJoint* hitTestJoints(ofVec2f pos); private: - ofxSurfaceManager* surfaceManager; - vector joints; + SurfaceManager* surfaceManager; + vector joints; bool bShiftKeyDown; float fSnapDistance; void drawJoints(); }; -#endif \ No newline at end of file + }} \ No newline at end of file diff --git a/src/QuadSurface.cpp b/src/QuadSurface.cpp index 91f296b..e71f45c 100644 --- a/src/QuadSurface.cpp +++ b/src/QuadSurface.cpp @@ -1,17 +1,19 @@ -#include "ofxQuadSurface.h" +#include "QuadSurface.h" -ofxQuadSurface::ofxQuadSurface() +namespace ofx{ + namespace piMapper{ +QuadSurface::QuadSurface() { - cout << "ofxQuadSurface constructor." << endl; + cout << "QuadSurface constructor." << endl; setup(); } -ofxQuadSurface::~ofxQuadSurface() +QuadSurface::~QuadSurface() { - cout << "ofxQuadSurface destructor." << endl; + cout << "QuadSurface destructor." << endl; } -void ofxQuadSurface::setup() +void QuadSurface::setup() { // Create 4 points for the 2 triangles ofVec2f p1 = ofVec2f(0, 0); @@ -28,7 +30,7 @@ void ofxQuadSurface::setup() setup( p1, p2, p3, p4, t1, t2, t3, t4, texture ); } -void ofxQuadSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4, +void QuadSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofVec2f t4, ofTexture* texturePtr ) { // Assign texture @@ -70,7 +72,7 @@ void ofxQuadSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4, calculate4dTextureCoords(); } -void ofxQuadSurface::draw() +void QuadSurface::draw() { /*if(mesh.haveVertsChanged() || mesh.haveTexCoordsChanged()){ calculate4dTextureCoords(); @@ -84,7 +86,7 @@ void ofxQuadSurface::draw() texture->unbind(); } -void ofxQuadSurface::setVertex(int index, ofVec2f p) +void QuadSurface::setVertex(int index, ofVec2f p) { if ( index > 3 ) { ofLog() << "Vertex with this index does not exist: " << index << endl; @@ -95,7 +97,7 @@ void ofxQuadSurface::setVertex(int index, ofVec2f p) calculate4dTextureCoords(); } -void ofxQuadSurface::setTexCoord(int index, ofVec2f t) +void QuadSurface::setTexCoord(int index, ofVec2f t) { if ( index > 3 ) { ofLog() << "Texture coordinate with this index does not exist: " << index << endl; @@ -106,7 +108,7 @@ void ofxQuadSurface::setTexCoord(int index, ofVec2f t) calculate4dTextureCoords(); } -void ofxQuadSurface::moveBy(ofVec2f v) +void QuadSurface::moveBy(ofVec2f v) { vector& vertices = getVertices(); for (int i=0; i 3 ) { ofLog() << "Vertex with this index does not exist: " << index << endl; @@ -143,7 +145,7 @@ ofVec2f ofxQuadSurface::getVertex(int index) return ofVec2f(vert.x, vert.y); } -ofVec2f ofxQuadSurface::getTexCoord(int index) +ofVec2f QuadSurface::getTexCoord(int index) { if (index > 3) { throw std::runtime_error("Texture coordinate index out of bounds."); @@ -152,7 +154,7 @@ ofVec2f ofxQuadSurface::getTexCoord(int index) return mesh.getTexCoord(index); } -ofPolyline ofxQuadSurface::getHitArea() +ofPolyline QuadSurface::getHitArea() { ofPolyline line; line.addVertex( ofPoint( mesh.getVertex(0).x, mesh.getVertex(0).y ) ); @@ -164,7 +166,7 @@ ofPolyline ofxQuadSurface::getHitArea() return line; } -ofPolyline ofxQuadSurface::getTextureHitArea() +ofPolyline QuadSurface::getTextureHitArea() { ofPolyline line; vector& texCoords = mesh.getTexCoords(); @@ -177,19 +179,19 @@ ofPolyline ofxQuadSurface::getTextureHitArea() return line; } -vector& ofxQuadSurface::getVertices() +vector& QuadSurface::getVertices() { // return only joint vertices return mesh.getVertices(); } -vector& ofxQuadSurface::getTexCoords() +vector& QuadSurface::getTexCoords() { return mesh.getTexCoords(); } -void ofxQuadSurface::calculate4dTextureCoords() +void QuadSurface::calculate4dTextureCoords() { // Perspective Warping with OpenGL Fixed Pipeline and q coordinates // see: @@ -259,4 +261,6 @@ void ofxQuadSurface::calculate4dTextureCoords() quadTexCoordinates[13] = t3.y * q3; quadTexCoordinates[15] = q3; -} \ No newline at end of file +} + + }} \ No newline at end of file diff --git a/src/QuadSurface.h b/src/QuadSurface.h index 4c6cb9d..87469cb 100755 --- a/src/QuadSurface.h +++ b/src/QuadSurface.h @@ -1,15 +1,16 @@ -#ifndef H_OFX_QUAD_SURFACE -#define H_OFX_QUAD_SURFACE +#pragma once #include "ofMain.h" -#include "ofxBaseSurface.h" -#include "ofxSurfaceType.h" +#include "BaseSurface.h" +#include "SurfaceType.h" -class ofxQuadSurface : public ofxBaseSurface +namespace ofx{ + namespace piMapper{ +class QuadSurface : public BaseSurface { public: - ofxQuadSurface(); - ~ofxQuadSurface(); + QuadSurface(); + ~QuadSurface(); void setup(); @@ -37,4 +38,4 @@ private: GLfloat quadTexCoordinates[16]; }; -#endif \ No newline at end of file + }} \ No newline at end of file diff --git a/src/SourcesEditor.cpp b/src/SourcesEditor.cpp index 5b5603c..05c9a83 100644 --- a/src/SourcesEditor.cpp +++ b/src/SourcesEditor.cpp @@ -1,12 +1,14 @@ -#include "ofxSourcesEditor.h" +#include "SourcesEditor.h" -ofxSourcesEditor::ofxSourcesEditor() +namespace ofx{ + namespace piMapper{ +SourcesEditor::SourcesEditor() { defImgDir = DEFAULT_IMAGES_DIR; registerAppEvents(); } -ofxSourcesEditor::~ofxSourcesEditor() +SourcesEditor::~SourcesEditor() { unregisterAppEvents(); delete gui; @@ -16,19 +18,19 @@ ofxSourcesEditor::~ofxSourcesEditor() } } -void ofxSourcesEditor::registerAppEvents() +void SourcesEditor::registerAppEvents() { - ofAddListener(ofEvents().setup, this, &ofxSourcesEditor::setup); + ofAddListener(ofEvents().setup, this, &SourcesEditor::setup); } -void ofxSourcesEditor::unregisterAppEvents() +void SourcesEditor::unregisterAppEvents() { - ofRemoveListener(ofEvents().setup, this, &ofxSourcesEditor::setup); + ofRemoveListener(ofEvents().setup, this, &SourcesEditor::setup); } -void ofxSourcesEditor::setup(ofEventArgs& args) +void SourcesEditor::setup(ofEventArgs& args) { - gui = new ofxRadioList(); + gui = new RadioList(); // read directory contents ofDirectory imgDir; @@ -44,10 +46,10 @@ void ofxSourcesEditor::setup(ofEventArgs& args) gui->setup("Images", vnames); gui->setPosition(20, 20); - ofAddListener(gui->radioSelectedEvent, this, &ofxSourcesEditor::guiEvent); + ofAddListener(gui->radioSelectedEvent, this, &SourcesEditor::guiEvent); } -void ofxSourcesEditor::draw() +void SourcesEditor::draw() { // Don't draw if there is no source selected if ( surfaceManager->getSelectedSurface() == NULL ) { @@ -57,7 +59,7 @@ void ofxSourcesEditor::draw() gui->draw(); } -void ofxSourcesEditor::loadImage( string name, string path ) +void SourcesEditor::loadImage( string name, string path ) { images.push_back(new ofImage()); images.back()->loadImage(path); @@ -67,12 +69,12 @@ void ofxSourcesEditor::loadImage( string name, string path ) ofSendMessage("imageLoaded"); } -void ofxSourcesEditor::disable() +void SourcesEditor::disable() { gui->disable(); } -void ofxSourcesEditor::enable() +void SourcesEditor::enable() { // Don't enable if there is no surface selected if ( surfaceManager->getSelectedSurface() == NULL ) { @@ -83,12 +85,12 @@ void ofxSourcesEditor::enable() gui->enable(); } -void ofxSourcesEditor::setSurfaceManager(ofxSurfaceManager *newSurfaceManager) +void SourcesEditor::setSurfaceManager(SurfaceManager *newSurfaceManager) { surfaceManager = newSurfaceManager; } -void ofxSourcesEditor::selectImageSourceRadioButton(string name) +void SourcesEditor::selectImageSourceRadioButton(string name) { if (name == "none") { gui->unselectAll(); @@ -104,12 +106,12 @@ void ofxSourcesEditor::selectImageSourceRadioButton(string name) } } -int ofxSourcesEditor::getLoadedTexCount() +int SourcesEditor::getLoadedTexCount() { return images.size(); } -ofTexture* ofxSourcesEditor::getTexture(int index) +ofTexture* SourcesEditor::getTexture(int index) { if (index >= images.size()){ throw std::runtime_error("Texture index out of bounds."); @@ -118,7 +120,7 @@ ofTexture* ofxSourcesEditor::getTexture(int index) return &images[index]->getTextureReference(); } -void ofxSourcesEditor::guiEvent(string &imageName) +void SourcesEditor::guiEvent(string &imageName) { string name = imageName; @@ -132,4 +134,6 @@ void ofxSourcesEditor::guiEvent(string &imageName) ofTexture* texture = surfaceManager->loadImageSource(name, ss.str()); surfaceManager->getSelectedSurface()->setTexture(texture); surfaceManager->manageMemory(); -} \ No newline at end of file +} + + }} \ No newline at end of file diff --git a/src/SourcesEditor.h b/src/SourcesEditor.h index 33aad38..8311d1f 100644 --- a/src/SourcesEditor.h +++ b/src/SourcesEditor.h @@ -1,18 +1,19 @@ -#ifndef H_OFX_SOURCES_EDITOR -#define H_OFX_SOURCES_EDITOR +#pragma once #include "ofGraphics.h" #include "ofEvents.h" -#include "ofxSurfaceManager.h" -#include "ofxRadioList.h" +#include "SurfaceManager.h" +#include "RadioList.h" #define DEFAULT_IMAGES_DIR "sources/images/"; -class ofxSourcesEditor +namespace ofx{ + namespace piMapper{ +class SourcesEditor { public: - ofxSourcesEditor(); - ~ofxSourcesEditor(); + SourcesEditor(); + ~SourcesEditor(); void registerAppEvents(); void unregisterAppEvents(); @@ -22,19 +23,19 @@ public: void loadImage( string name, string path ); void disable(); void enable(); - void setSurfaceManager(ofxSurfaceManager* newSurfaceManager); + void setSurfaceManager(SurfaceManager* newSurfaceManager); void selectImageSourceRadioButton(string name); int getLoadedTexCount(); ofTexture* getTexture(int index); private: - ofxSurfaceManager* surfaceManager; - ofxRadioList* gui; + SurfaceManager* surfaceManager; + RadioList* gui; string defImgDir; void guiEvent(string &imageName); vector images; vector imageNames; }; -#endif \ No newline at end of file + }} \ No newline at end of file diff --git a/src/SurfaceManager.cpp b/src/SurfaceManager.cpp index e7df328..48ac560 100644 --- a/src/SurfaceManager.cpp +++ b/src/SurfaceManager.cpp @@ -1,43 +1,45 @@ -#include "ofxSurfaceManager.h" +#include "SurfaceManager.h" -ofxSurfaceManager::ofxSurfaceManager() +namespace ofx{ + namespace piMapper{ +SurfaceManager::SurfaceManager() { } -ofxSurfaceManager::~ofxSurfaceManager() +SurfaceManager::~SurfaceManager() { clear(); } -void ofxSurfaceManager::draw() +void SurfaceManager::draw() { for ( int i=0; idraw(); } } -void ofxSurfaceManager::addSurface(int surfaceType) +void SurfaceManager::addSurface(int surfaceType) { - if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) { - surfaces.push_back( new ofxTriangleSurface() ); + if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) { + surfaces.push_back( new TriangleSurface() ); } - else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) { - surfaces.push_back( new ofxQuadSurface() ); + else if (surfaceType == SurfaceType::QUAD_SURFACE ) { + surfaces.push_back( new QuadSurface() ); } else { throw std::runtime_error("Attempt to add non-existing surface type."); } } -void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr) +void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr) { - if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) { - surfaces.push_back( new ofxTriangleSurface() ); + if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) { + surfaces.push_back( new TriangleSurface() ); surfaces.back()->setTexture(texturePtr); } - else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) { - surfaces.push_back( new ofxQuadSurface() ); + else if (surfaceType == SurfaceType::QUAD_SURFACE ) { + surfaces.push_back( new QuadSurface() ); surfaces.back()->setTexture(texturePtr); } else { @@ -45,9 +47,9 @@ void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr) } } -void ofxSurfaceManager::addSurface(int surfaceType, vector vertices, vector texCoords) +void SurfaceManager::addSurface(int surfaceType, vector vertices, vector texCoords) { - if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) { + if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) { if ( vertices.size() < 3 ) { throw std::runtime_error("There must be 3 vertices for a triangle surface."); @@ -55,7 +57,7 @@ void ofxSurfaceManager::addSurface(int surfaceType, vector vertices, ve throw std::runtime_error("There must be 3 texture coordinates for a triangle surface."); } - surfaces.push_back( new ofxTriangleSurface() ); + surfaces.push_back( new TriangleSurface() ); for ( int i=0; i<3; i++ ) { surfaces.back()->setVertex(i, vertices[i]); @@ -63,14 +65,14 @@ void ofxSurfaceManager::addSurface(int surfaceType, vector vertices, ve } } - else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) { + else if (surfaceType == SurfaceType::QUAD_SURFACE ) { if ( vertices.size() < 4 ) { throw std::runtime_error("There must be 4 vertices for a quad surface."); } else if (texCoords.size() < 4) { throw std::runtime_error("There must be 4 texture coordinates for a quad surface."); } - surfaces.push_back( new ofxQuadSurface() ); + surfaces.push_back( new QuadSurface() ); for ( int i=0; i<4; i++ ) { surfaces.back()->setVertex(i, vertices[i]); @@ -83,9 +85,9 @@ void ofxSurfaceManager::addSurface(int surfaceType, vector vertices, ve } -void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vector vertices, vector texCoords) +void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vector vertices, vector texCoords) { - if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) { + if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) { if ( vertices.size() < 3 ) { throw std::runtime_error("There must be 3 vertices for a triangle surface."); @@ -93,7 +95,7 @@ void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vecto throw std::runtime_error("Thre must be 3 texture coordinates for a triangle surface."); } - surfaces.push_back( new ofxTriangleSurface() ); + surfaces.push_back( new TriangleSurface() ); surfaces.back()->setTexture(texturePtr); for ( int i=0; i<3; i++ ) { @@ -102,14 +104,14 @@ void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vecto } } - else if (surfaceType == ofxSurfaceType::QUAD_SURFACE ) { + else if (surfaceType == SurfaceType::QUAD_SURFACE ) { if ( vertices.size() < 4 ) { throw std::runtime_error("There must be 4 vertices for a quad surface."); } else if (texCoords.size() < 4) { throw std::runtime_error("Thre must be 4 texture coordinates for a quad surface."); } - surfaces.push_back( new ofxQuadSurface() ); + surfaces.push_back( new QuadSurface() ); surfaces.back()->setTexture(texturePtr); for ( int i=0; i<4; i++ ) { @@ -122,7 +124,7 @@ void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vecto } } -void ofxSurfaceManager::removeSelectedSurface() +void SurfaceManager::removeSelectedSurface() { if ( selectedSurface == NULL ) return; @@ -136,7 +138,7 @@ void ofxSurfaceManager::removeSelectedSurface() } } -void ofxSurfaceManager::manageMemory() +void SurfaceManager::manageMemory() { // check if each of the sources is assigned to a surface or not for ( int i=0; igetType(); + // // surfaceType == SurfaceType::TRIANGLE_SURFACE + // SurfaceType surfaceType = &surface->getType(); // xmlSettings.addValue("surface-type", surfaceType); // xmlSettings.popTag(); // type @@ -253,7 +255,7 @@ void ofxSurfaceManager::saveXmlSettings(string fileName) xmlSettings.save(fileName); } -void ofxSurfaceManager::loadXmlSettings(string fileName) +void SurfaceManager::loadXmlSettings(string fileName) { @@ -337,9 +339,9 @@ void ofxSurfaceManager::loadXmlSettings(string fileName) // now we have variables sourceName and sourceTexture // by checking those we can use one or another addSurface method if ( sourceName != "none" && sourceTexture != NULL ) { - addSurface(ofxSurfaceType::TRIANGLE_SURFACE, sourceTexture, vertices, texCoords); + addSurface(SurfaceType::TRIANGLE_SURFACE, sourceTexture, vertices, texCoords); } else { - addSurface(ofxSurfaceType::TRIANGLE_SURFACE, vertices, texCoords); + addSurface(SurfaceType::TRIANGLE_SURFACE, vertices, texCoords); } } // it's a quad ? @@ -390,9 +392,9 @@ void ofxSurfaceManager::loadXmlSettings(string fileName) // now we have variables sourceName and sourceTexture // by checking those we can use one or another addSurface method if ( sourceName != "none" && sourceTexture != NULL ) { - addSurface(ofxSurfaceType::QUAD_SURFACE, sourceTexture, vertices, texCoords); + addSurface(SurfaceType::QUAD_SURFACE, sourceTexture, vertices, texCoords); } else { - addSurface(ofxSurfaceType::QUAD_SURFACE, vertices, texCoords); + addSurface(SurfaceType::QUAD_SURFACE, vertices, texCoords); } @@ -404,7 +406,7 @@ void ofxSurfaceManager::loadXmlSettings(string fileName) xmlSettings.popTag(); // surfaces } -ofxBaseSurface* ofxSurfaceManager::selectSurface(int index) +BaseSurface* SurfaceManager::selectSurface(int index) { if ( index >= surfaces.size() ) { throw std::runtime_error("Surface index out of bounds."); @@ -416,17 +418,17 @@ ofxBaseSurface* ofxSurfaceManager::selectSurface(int index) ofSendMessage("surfaceSelected"); } -ofxBaseSurface* ofxSurfaceManager::getSelectedSurface() +BaseSurface* SurfaceManager::getSelectedSurface() { return selectedSurface; } -void ofxSurfaceManager::deselectSurface() +void SurfaceManager::deselectSurface() { selectedSurface = NULL; } -ofTexture* ofxSurfaceManager::loadImageSource(string name, string path) +ofTexture* SurfaceManager::loadImageSource(string name, string path) { // check if it is loaded for ( int i=0; igetTextureReference(); } -string ofxSurfaceManager::getSelectedSurfaceSourceName() +string SurfaceManager::getSelectedSurfaceSourceName() { if ( selectedSurface == NULL ) { return "none"; @@ -455,7 +457,7 @@ string ofxSurfaceManager::getSelectedSurfaceSourceName() return getSurfaceSourceName( selectedSurface ); } -string ofxSurfaceManager::getSurfaceSourceName(ofxBaseSurface *surface) +string SurfaceManager::getSurfaceSourceName(BaseSurface *surface) { ofTexture* tex = surface->getTexture(); for ( int i=0; i= surfaces.size() ) { throw std::runtime_error("Surface index out of bounds."); @@ -477,10 +479,10 @@ ofxBaseSurface* ofxSurfaceManager::getSurface(int index) return surfaces[index]; } -int ofxSurfaceManager::size() +int SurfaceManager::size() { return surfaces.size(); } - + }} diff --git a/src/SurfaceManager.h b/src/SurfaceManager.h index 90e8bb1..48df5c5 100755 --- a/src/SurfaceManager.h +++ b/src/SurfaceManager.h @@ -1,20 +1,23 @@ -#ifndef H_OFX_SURFACE_MANAGER -#define H_OFX_SURFACE_MANAGER +#pragma once + +#include "BaseSurface.h" +#include "TriangleSurface.h" +#include "QuadSurface.h" +#include "SurfaceType.h" -#include "ofxBaseSurface.h" -#include "ofxTriangleSurface.h" -#include "ofxQuadSurface.h" -#include "ofxSurfaceType.h" #include "ofEvents.h" #include "ofxXmlSettings.h" using namespace std; -class ofxSurfaceManager + +namespace ofx{ + namespace piMapper{ +class SurfaceManager { public: - ofxSurfaceManager(); - ~ofxSurfaceManager(); + SurfaceManager(); + ~SurfaceManager(); void draw(); void addSurface(int surfaceType); @@ -27,22 +30,22 @@ public: void saveXmlSettings(string fileName); void loadXmlSettings(string fileName); - ofxBaseSurface* getSurface(int index); + BaseSurface* getSurface(int index); int size(); - ofxBaseSurface* selectSurface(int index); - ofxBaseSurface* getSelectedSurface(); + BaseSurface* selectSurface(int index); + BaseSurface* getSelectedSurface(); void deselectSurface(); ofTexture* loadImageSource(string name, string path); string getSelectedSurfaceSourceName(); - string getSurfaceSourceName( ofxBaseSurface* surface ); + string getSurfaceSourceName( BaseSurface* surface ); private: - vector surfaces; - ofxBaseSurface* selectedSurface; + vector surfaces; + BaseSurface* selectedSurface; vector loadedImageSourceNames; vector loadedImageSources; ofxXmlSettings xmlSettings; }; -#endif \ No newline at end of file + }} \ No newline at end of file diff --git a/src/SurfaceManagerGui.cpp b/src/SurfaceManagerGui.cpp index 49d5a1e..e932810 100644 --- a/src/SurfaceManagerGui.cpp +++ b/src/SurfaceManagerGui.cpp @@ -1,41 +1,43 @@ -#include "ofxSurfaceManagerGui.h" +#include "SurfaceManagerGui.h" -ofxSurfaceManagerGui::ofxSurfaceManagerGui() +namespace ofx{ + namespace piMapper{ +SurfaceManagerGui::SurfaceManagerGui() { surfaceManager = NULL; - guiMode = ofxGuiMode::NONE; + guiMode = GuiMode::NONE; bDrag = false; registerMouseEvents(); ofHideCursor(); } -ofxSurfaceManagerGui::~ofxSurfaceManagerGui() +SurfaceManagerGui::~SurfaceManagerGui() { unregisterMouseEvents(); surfaceManager = NULL; } -void ofxSurfaceManagerGui::registerMouseEvents() +void SurfaceManagerGui::registerMouseEvents() { - ofAddListener(ofEvents().mousePressed, this, &ofxSurfaceManagerGui::mousePressed); - ofAddListener(ofEvents().mouseReleased, this, &ofxSurfaceManagerGui::mouseReleased); - ofAddListener(ofEvents().mouseDragged, this, &ofxSurfaceManagerGui::mouseDragged); + ofAddListener(ofEvents().mousePressed, this, &SurfaceManagerGui::mousePressed); + ofAddListener(ofEvents().mouseReleased, this, &SurfaceManagerGui::mouseReleased); + ofAddListener(ofEvents().mouseDragged, this, &SurfaceManagerGui::mouseDragged); } -void ofxSurfaceManagerGui::unregisterMouseEvents() +void SurfaceManagerGui::unregisterMouseEvents() { - ofRemoveListener(ofEvents().mousePressed, this, &ofxSurfaceManagerGui::mousePressed); - ofRemoveListener(ofEvents().mouseReleased, this, &ofxSurfaceManagerGui::mouseReleased); - ofRemoveListener(ofEvents().mouseDragged, this, &ofxSurfaceManagerGui::mouseDragged); + ofRemoveListener(ofEvents().mousePressed, this, &SurfaceManagerGui::mousePressed); + ofRemoveListener(ofEvents().mouseReleased, this, &SurfaceManagerGui::mouseReleased); + ofRemoveListener(ofEvents().mouseDragged, this, &SurfaceManagerGui::mouseDragged); } -void ofxSurfaceManagerGui::draw() +void SurfaceManagerGui::draw() { if ( surfaceManager == NULL ) return; - if ( guiMode == ofxGuiMode::NONE ) { + if ( guiMode == GuiMode::NONE ) { surfaceManager->draw(); - } else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { + } else if ( guiMode == GuiMode::TEXTURE_MAPPING ) { // draw the texture of the selected surface if ( surfaceManager->getSelectedSurface() != NULL ) { @@ -57,7 +59,7 @@ void ofxSurfaceManagerGui::draw() // draw texture editing GUI on top textureEditor.draw(); - } else if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) { + } else if ( guiMode == GuiMode::PROJECTION_MAPPING ) { // draw projection surfaces first surfaceManager->draw(); @@ -68,7 +70,7 @@ void ofxSurfaceManagerGui::draw() // draw projection mapping editing gui projectionEditor.draw(); - } else if ( guiMode == ofxGuiMode::SOURCE_SELECTION ) { + } else if ( guiMode == GuiMode::SOURCE_SELECTION ) { // draw projection surfaces first surfaceManager->draw(); @@ -79,15 +81,15 @@ void ofxSurfaceManagerGui::draw() } } -void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args) +void SurfaceManagerGui::mousePressed(ofMouseEventArgs &args) { - if ( guiMode == ofxGuiMode::NONE ) { + if ( guiMode == GuiMode::NONE ) { return; - } else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { + } else if ( guiMode == GuiMode::TEXTURE_MAPPING ) { bool bSurfaceSelected = false; - ofxCircleJoint* hitJoint = textureEditor.hitTestJoints(ofVec2f(args.x, args.y)); + CircleJoint* hitJoint = textureEditor.hitTestJoints(ofVec2f(args.x, args.y)); if ( hitJoint != NULL ) { textureEditor.unselectAllJoints(); hitJoint->select(); @@ -105,11 +107,11 @@ void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args) } } - } else if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) { + } else if ( guiMode == GuiMode::PROJECTION_MAPPING ) { bool bSurfaceSelected = false; - ofxCircleJoint* hitJoint = projectionEditor.hitTestJoints(ofVec2f(args.x, args.y)); + CircleJoint* hitJoint = projectionEditor.hitTestJoints(ofVec2f(args.x, args.y)); if ( hitJoint != NULL ) { projectionEditor.unselectAllJoints(); hitJoint->select(); @@ -141,51 +143,51 @@ void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args) projectionEditor.clearJoints(); surfaceManager->deselectSurface(); } - } else if ( guiMode == ofxGuiMode::SOURCE_SELECTION ) { + } else if ( guiMode == GuiMode::SOURCE_SELECTION ) { } } -void ofxSurfaceManagerGui::mouseReleased(ofMouseEventArgs &args) +void SurfaceManagerGui::mouseReleased(ofMouseEventArgs &args) { stopDrag(); projectionEditor.stopDragJoints(); textureEditor.stopDragJoints(); } -void ofxSurfaceManagerGui::mouseDragged(ofMouseEventArgs &args) +void SurfaceManagerGui::mouseDragged(ofMouseEventArgs &args) { if (bDrag) { ofVec2f mousePosition = ofVec2f(args.x, args.y); ofVec2f distance = mousePosition - clickPosition; - if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) { + if ( guiMode == GuiMode::PROJECTION_MAPPING ) { // add this distance to all vertices in surface projectionEditor.moveSelectedSurface(distance); - } else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { + } else if ( guiMode == GuiMode::TEXTURE_MAPPING ) { textureEditor.moveTexCoords(distance); } clickPosition = mousePosition; } } -void ofxSurfaceManagerGui::setSurfaceManager(ofxSurfaceManager* newSurfaceManager) +void SurfaceManagerGui::setSurfaceManager(SurfaceManager* newSurfaceManager) { surfaceManager = newSurfaceManager; projectionEditor.setSurfaceManager( surfaceManager ); sourcesEditor.setSurfaceManager( surfaceManager ); } -void ofxSurfaceManagerGui::setMode(int newGuiMode) +void SurfaceManagerGui::setMode(int newGuiMode) { - if (newGuiMode != ofxGuiMode::NONE && - newGuiMode != ofxGuiMode::TEXTURE_MAPPING && - newGuiMode != ofxGuiMode::PROJECTION_MAPPING && - newGuiMode != ofxGuiMode::SOURCE_SELECTION) { + if (newGuiMode != GuiMode::NONE && + newGuiMode != GuiMode::TEXTURE_MAPPING && + newGuiMode != GuiMode::PROJECTION_MAPPING && + newGuiMode != GuiMode::SOURCE_SELECTION) { throw std::runtime_error("Trying to set invalid mode."); } - if ( newGuiMode == ofxGuiMode::NONE ) { + if ( newGuiMode == GuiMode::NONE ) { ofHideCursor(); } else { ofShowCursor(); @@ -193,7 +195,7 @@ void ofxSurfaceManagerGui::setMode(int newGuiMode) guiMode = newGuiMode; - if ( guiMode == ofxGuiMode::SOURCE_SELECTION ) { + if ( guiMode == GuiMode::SOURCE_SELECTION ) { sourcesEditor.enable(); string sourceName = surfaceManager->getSelectedSurfaceSourceName(); sourcesEditor.selectImageSourceRadioButton(sourceName); @@ -201,7 +203,7 @@ void ofxSurfaceManagerGui::setMode(int newGuiMode) sourcesEditor.disable(); } - if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { + if ( guiMode == GuiMode::TEXTURE_MAPPING ) { textureEditor.enable(); // refresh texture editor surface reference textureEditor.setSurface(surfaceManager->getSelectedSurface()); @@ -209,14 +211,14 @@ void ofxSurfaceManagerGui::setMode(int newGuiMode) textureEditor.disable(); } - if (guiMode == ofxGuiMode::PROJECTION_MAPPING) { + if (guiMode == GuiMode::PROJECTION_MAPPING) { projectionEditor.enable(); } else { projectionEditor.disable(); } } -void ofxSurfaceManagerGui::drawSelectedSurfaceHighlight() +void SurfaceManagerGui::drawSelectedSurfaceHighlight() { if ( surfaceManager->getSelectedSurface() == NULL ) return; @@ -229,7 +231,7 @@ void ofxSurfaceManagerGui::drawSelectedSurfaceHighlight() ofPopStyle(); } -void ofxSurfaceManagerGui::drawSelectedSurfaceTextureHighlight() +void SurfaceManagerGui::drawSelectedSurfaceTextureHighlight() { if ( surfaceManager->getSelectedSurface() == NULL ) return; @@ -243,12 +245,14 @@ void ofxSurfaceManagerGui::drawSelectedSurfaceTextureHighlight() } -void ofxSurfaceManagerGui::startDrag() +void SurfaceManagerGui::startDrag() { bDrag = true; } -void ofxSurfaceManagerGui::stopDrag() +void SurfaceManagerGui::stopDrag() { bDrag = false; -} \ No newline at end of file +} + + }} \ No newline at end of file diff --git a/src/SurfaceManagerGui.h b/src/SurfaceManagerGui.h index 605d8a1..0dc8743 100644 --- a/src/SurfaceManagerGui.h +++ b/src/SurfaceManagerGui.h @@ -1,22 +1,24 @@ -#ifndef H_OFX_SURFACE_MANAGER_GUI -#define H_OFX_SURFACE_MANAGER_GUI +#pragma once // I'm starting to think, maybe we should use ofxStateMachine here. // Would make sense. TODO later. #include "ofEvents.h" -#include "ofxSurfaceManager.h" -#include "ofxTextureEditor.h" -#include "ofxProjectionEditor.h" -#include "ofxSourcesEditor.h" -#include "ofxGuiMode.h" #include "ofGraphics.h" -class ofxSurfaceManagerGui +#include "SurfaceManager.h" +#include "TextureEditor.h" +#include "ProjectionEditor.h" +#include "SourcesEditor.h" +#include "GuiMode.h" + +namespace ofx{ + namespace piMapper{ +class SurfaceManagerGui { public: - ofxSurfaceManagerGui(); - ~ofxSurfaceManagerGui(); + SurfaceManagerGui(); + ~SurfaceManagerGui(); void registerMouseEvents(); void unregisterMouseEvents(); @@ -25,7 +27,7 @@ public: void mousePressed(ofMouseEventArgs& args); void mouseReleased(ofMouseEventArgs& args); void mouseDragged(ofMouseEventArgs& args); - void setSurfaceManager(ofxSurfaceManager* newSurfaceManager); + void setSurfaceManager(SurfaceManager* newSurfaceManager); void setMode(int newGuiMode); void drawSelectedSurfaceHighlight(); void drawSelectedSurfaceTextureHighlight(); @@ -33,14 +35,14 @@ public: void stopDrag(); private: - ofxSurfaceManager* surfaceManager; - ofxTextureEditor textureEditor; - ofxProjectionEditor projectionEditor; - ofxSourcesEditor sourcesEditor; + SurfaceManager* surfaceManager; + TextureEditor textureEditor; + ProjectionEditor projectionEditor; + SourcesEditor sourcesEditor; int guiMode; bool bDrag; ofVec2f clickPosition; }; -#endif \ No newline at end of file + }} \ No newline at end of file diff --git a/src/SurfaceType.h b/src/SurfaceType.h index cbda131..21deb9b 100755 --- a/src/SurfaceType.h +++ b/src/SurfaceType.h @@ -1,7 +1,8 @@ -#ifndef H_OFX_SURFACE_TYPE -#define H_OFX_SURFACE_TYPE +#pragma once -struct ofxSurfaceType +namespace ofx{ + namespace piMapper{ +struct SurfaceType { enum { TRIANGLE_SURFACE, @@ -9,4 +10,4 @@ struct ofxSurfaceType }; }; -#endif \ No newline at end of file + }} \ No newline at end of file diff --git a/src/TextureEditor.cpp b/src/TextureEditor.cpp index 0a1aa41..a0a9eba 100755 --- a/src/TextureEditor.cpp +++ b/src/TextureEditor.cpp @@ -1,53 +1,55 @@ -#include "ofxTextureEditor.h" +#include "TextureEditor.h" -ofxTextureEditor::ofxTextureEditor() +namespace ofx{ + namespace piMapper{ +TextureEditor::TextureEditor() { clear(); enable(); } -ofxTextureEditor::~ofxTextureEditor() +TextureEditor::~TextureEditor() { clear(); disable(); } -void ofxTextureEditor::registerAppEvents() +void TextureEditor::registerAppEvents() { - ofAddListener(ofEvents().update, this, &ofxTextureEditor::update); + ofAddListener(ofEvents().update, this, &TextureEditor::update); } -void ofxTextureEditor::unregisterAppEvents() +void TextureEditor::unregisterAppEvents() { - ofRemoveListener(ofEvents().update, this, &ofxTextureEditor::update); + ofRemoveListener(ofEvents().update, this, &TextureEditor::update); } -void ofxTextureEditor::registerKeyEvents() +void TextureEditor::registerKeyEvents() { - ofAddListener(ofEvents().keyPressed, this, &ofxTextureEditor::keyPressed); - ofAddListener(ofEvents().keyReleased, this, &ofxTextureEditor::keyReleased); + ofAddListener(ofEvents().keyPressed, this, &TextureEditor::keyPressed); + ofAddListener(ofEvents().keyReleased, this, &TextureEditor::keyReleased); } -void ofxTextureEditor::unregisterKeyEvents() +void TextureEditor::unregisterKeyEvents() { - ofRemoveListener(ofEvents().keyPressed, this, &ofxTextureEditor::keyPressed); - ofRemoveListener(ofEvents().keyReleased, this, &ofxTextureEditor::keyReleased); + ofRemoveListener(ofEvents().keyPressed, this, &TextureEditor::keyPressed); + ofRemoveListener(ofEvents().keyReleased, this, &TextureEditor::keyReleased); } -void ofxTextureEditor::enable() +void TextureEditor::enable() { registerAppEvents(); registerKeyEvents(); bShiftKeyDown = false; } -void ofxTextureEditor::disable() +void TextureEditor::disable() { unregisterAppEvents(); unregisterKeyEvents(); } -void ofxTextureEditor::update(ofEventArgs &args) +void TextureEditor::update(ofEventArgs &args) { if ( surface == NULL ) return; @@ -62,7 +64,7 @@ void ofxTextureEditor::update(ofEventArgs &args) } } -void ofxTextureEditor::keyPressed(ofKeyEventArgs &args) +void TextureEditor::keyPressed(ofKeyEventArgs &args) { int key = args.key; float moveStep; @@ -79,7 +81,7 @@ void ofxTextureEditor::keyPressed(ofKeyEventArgs &args) } } -void ofxTextureEditor::keyReleased(ofKeyEventArgs &args) +void TextureEditor::keyReleased(ofKeyEventArgs &args) { int key = args.key; switch (key) { @@ -88,33 +90,33 @@ void ofxTextureEditor::keyReleased(ofKeyEventArgs &args) } -void ofxTextureEditor::draw() +void TextureEditor::draw() { if (surface == NULL) return; drawJoints(); } -void ofxTextureEditor::drawJoints() +void TextureEditor::drawJoints() { for ( int i=0; idraw(); } } -void ofxTextureEditor::setSurface(ofxBaseSurface* newSurface) +void TextureEditor::setSurface(BaseSurface* newSurface) { surface = newSurface; createJoints(); } -void ofxTextureEditor::clear() +void TextureEditor::clear() { surface = NULL; clearJoints(); } -void ofxTextureEditor::createJoints() +void TextureEditor::createJoints() { if ( surface == NULL ) return; clearJoints(); @@ -122,12 +124,12 @@ void ofxTextureEditor::createJoints() ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(), surface->getTexture()->getHeight()); for ( int i=0; iposition = texCoords[i] * textureSize; } } -void ofxTextureEditor::clearJoints() +void TextureEditor::clearJoints() { while ( joints.size() ) { delete joints.back(); @@ -135,14 +137,14 @@ void ofxTextureEditor::clearJoints() } } -void ofxTextureEditor::unselectAllJoints() +void TextureEditor::unselectAllJoints() { for ( int i=0; iunselect(); } } -void ofxTextureEditor::moveTexCoords(ofVec2f by) +void TextureEditor::moveTexCoords(ofVec2f by) { if ( surface == NULL ) return; vector& texCoords = surface->getTexCoords(); @@ -153,18 +155,18 @@ void ofxTextureEditor::moveTexCoords(ofVec2f by) } } -void ofxTextureEditor::stopDragJoints() +void TextureEditor::stopDragJoints() { for (int i=0; istopDrag(); } } -void ofxTextureEditor::moveSelection(ofVec2f by) +void TextureEditor::moveSelection(ofVec2f by) { // check if joints selected bool bJointSelected = false; - ofxBaseJoint* selectedJoint; + BaseJoint* selectedJoint; for ( int i=0; iisSelected()) { bJointSelected = true; @@ -180,7 +182,7 @@ void ofxTextureEditor::moveSelection(ofVec2f by) } } -ofxCircleJoint* ofxTextureEditor::hitTestJoints(ofVec2f pos) +CircleJoint* TextureEditor::hitTestJoints(ofVec2f pos) { for ( int i=0; ihitTest(pos) ){ @@ -188,4 +190,6 @@ ofxCircleJoint* ofxTextureEditor::hitTestJoints(ofVec2f pos) } } return NULL; -} \ No newline at end of file +} + + }} \ No newline at end of file diff --git a/src/TextureEditor.h b/src/TextureEditor.h index 18f30ef..1387984 100644 --- a/src/TextureEditor.h +++ b/src/TextureEditor.h @@ -1,15 +1,18 @@ -#ifndef H_OFX_TEXTURE_EDITOR -#define H_OFX_TEXTURE_EDITOR +#pragma once #include "ofEvents.h" -#include "ofxBaseSurface.h" -#include "ofxCircleJoint.h" -class ofxTextureEditor +#include "BaseSurface.h" +#include "CircleJoint.h" + + +namespace ofx{ + namespace piMapper{ +class TextureEditor { public: - ofxTextureEditor(); - ~ofxTextureEditor(); + TextureEditor(); + ~TextureEditor(); void registerAppEvents(); void unregisterAppEvents(); @@ -23,7 +26,7 @@ public: void keyReleased(ofKeyEventArgs& args); void draw(); void drawJoints(); - void setSurface(ofxBaseSurface* newSurface); + void setSurface(BaseSurface* newSurface); void clear(); void createJoints(); void clearJoints(); @@ -31,12 +34,12 @@ public: void moveTexCoords(ofVec2f by); void stopDragJoints(); void moveSelection(ofVec2f by); - ofxCircleJoint* hitTestJoints(ofVec2f pos); + CircleJoint* hitTestJoints(ofVec2f pos); private: - ofxBaseSurface* surface; - vector joints; + BaseSurface* surface; + vector joints; bool bShiftKeyDown; }; -#endif \ No newline at end of file + }} \ No newline at end of file diff --git a/src/TriangleSurface.cpp b/src/TriangleSurface.cpp index 56d0a44..950e360 100644 --- a/src/TriangleSurface.cpp +++ b/src/TriangleSurface.cpp @@ -1,17 +1,19 @@ -#include "ofxTriangleSurface.h" +#include "TriangleSurface.h" -ofxTriangleSurface::ofxTriangleSurface() +namespace ofx{ + namespace piMapper{ +TriangleSurface::TriangleSurface() { - cout << "ofxTriangleSurface constructor." << endl; + cout << "TriangleSurface constructor." << endl; setup(); } -ofxTriangleSurface::~ofxTriangleSurface() +TriangleSurface::~TriangleSurface() { - cout << "ofxTriangleSurface destructor." << endl; + cout << "TriangleSurface destructor." << endl; } -void ofxTriangleSurface::setup() +void TriangleSurface::setup() { // Create 3 points for the triangle ofVec2f p1 = ofVec2f(ofGetWidth()/2.0f, 0); @@ -26,7 +28,7 @@ void ofxTriangleSurface::setup() setup( p1, p2, p3, t1, t2, t3, texture ); } -void ofxTriangleSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr ) +void TriangleSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr ) { // Assign texture texture = texturePtr; @@ -45,14 +47,14 @@ void ofxTriangleSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, mesh.addTexCoord(t3); } -void ofxTriangleSurface::draw() +void TriangleSurface::draw() { texture->bind(); mesh.draw(); texture->unbind(); } -void ofxTriangleSurface::setVertex(int index, ofVec2f p) +void TriangleSurface::setVertex(int index, ofVec2f p) { if ( index > 2 ) { ofLog() << "Vertex with this index does not exist: " << index << endl; @@ -62,7 +64,7 @@ void ofxTriangleSurface::setVertex(int index, ofVec2f p) mesh.setVertex(index, p); } -void ofxTriangleSurface::setTexCoord(int index, ofVec2f t) +void TriangleSurface::setTexCoord(int index, ofVec2f t) { if ( index > 2 ) { ofLog() << "Texture coordinate with this index does not exist: " << index << endl; @@ -72,7 +74,7 @@ void ofxTriangleSurface::setTexCoord(int index, ofVec2f t) mesh.setTexCoord(index, t); } -void ofxTriangleSurface::moveBy(ofVec2f v) +void TriangleSurface::moveBy(ofVec2f v) { vector& vertices = getVertices(); for (int i=0; i 2 ) { ofLog() << "Vertex with this index does not exist: " << index << endl; @@ -108,7 +110,7 @@ ofVec2f ofxTriangleSurface::getVertex(int index) return ofVec2f(vert.x, vert.y); } -ofVec2f ofxTriangleSurface::getTexCoord(int index) +ofVec2f TriangleSurface::getTexCoord(int index) { if (index > 2) { throw std::runtime_error("Texture coordinate index out of bounds."); @@ -117,7 +119,7 @@ ofVec2f ofxTriangleSurface::getTexCoord(int index) return mesh.getTexCoord(index); } -ofPolyline ofxTriangleSurface::getHitArea() +ofPolyline TriangleSurface::getHitArea() { ofPolyline line; line.addVertex( ofPoint( mesh.getVertex(0).x, mesh.getVertex(0).y ) ); @@ -128,7 +130,7 @@ ofPolyline ofxTriangleSurface::getHitArea() return line; } -ofPolyline ofxTriangleSurface::getTextureHitArea() +ofPolyline TriangleSurface::getTextureHitArea() { ofPolyline line; vector& texCoords = mesh.getTexCoords(); @@ -141,14 +143,16 @@ ofPolyline ofxTriangleSurface::getTextureHitArea() return line; } -vector& ofxTriangleSurface::getVertices() +vector& TriangleSurface::getVertices() { // return only joint vertices return mesh.getVertices(); } -vector& ofxTriangleSurface::getTexCoords() +vector& TriangleSurface::getTexCoords() { return mesh.getTexCoords(); -} \ No newline at end of file +} + + }} \ No newline at end of file diff --git a/src/TriangleSurface.h b/src/TriangleSurface.h index 27d3865..f1b2e74 100644 --- a/src/TriangleSurface.h +++ b/src/TriangleSurface.h @@ -1,15 +1,17 @@ -#ifndef H_OFX_TRIANGLE_SURFACE -#define H_OFX_TRIANGLE_SURFACE +#pragma once #include "ofMain.h" -#include "ofxBaseSurface.h" -#include "ofxSurfaceType.h" +#include "BaseSurface.h" +#include "SurfaceType.h" -class ofxTriangleSurface : public ofxBaseSurface + +namespace ofx{ + namespace piMapper{ +class TriangleSurface : public BaseSurface { public: - ofxTriangleSurface(); - ~ofxTriangleSurface(); + TriangleSurface(); + ~TriangleSurface(); void setup(); void setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr ); @@ -28,4 +30,4 @@ public: vector& getTexCoords(); }; -#endif \ No newline at end of file + }} \ No newline at end of file diff --git a/src/ofxPiMapper.h b/src/ofxPiMapper.h index 4f65bd2..b0b5fea 100644 --- a/src/ofxPiMapper.h +++ b/src/ofxPiMapper.h @@ -1,7 +1,4 @@ -#ifndef H_OFX_PI_MAPPER -#define H_OFX_PI_MAPPER +#pragma once -#include "ofxSurfaceManager.h" -#include "ofxSurfaceManagerGui.h" - -#endif \ No newline at end of file +#include "SurfaceManager.h" +#include "SurfaceManagerGui.h" \ No newline at end of file diff --git a/src/ui/RadioList.cpp b/src/ui/RadioList.cpp index e974284..a1d5cf5 100644 --- a/src/ui/RadioList.cpp +++ b/src/ui/RadioList.cpp @@ -1,29 +1,31 @@ -#include "ofxRadioList.h" +#include "RadioList.h" -ofxRadioList::ofxRadioList() +namespace ofx{ + namespace piMapper{ +RadioList::RadioList() { storedTitle = ""; storedSelectedItem = 0; } -ofxRadioList::ofxRadioList(vector &labels) +RadioList::RadioList(vector &labels) { - ofxRadioList(); + RadioList(); setup(labels); } -ofxRadioList::ofxRadioList(string title, vector &labels) +RadioList::RadioList(string title, vector &labels) { - ofxRadioList(); + RadioList(); setup(title, labels); } -ofxRadioList::~ofxRadioList() +RadioList::~RadioList() { clear(); } -void ofxRadioList::setup(vector &labels) +void RadioList::setup(vector &labels) { // Copy incomming labels for later use storedLabels = labels; @@ -34,14 +36,14 @@ void ofxRadioList::setup(vector &labels) ofxToggle* toggle = new ofxToggle(); toggle->setup(false); toggle->setName(labels[i]); - toggle->addListener(this, &ofxRadioList::onToggleClicked); + toggle->addListener(this, &RadioList::onToggleClicked); guiGroup.add(toggle); } cout << "num items: " << guiGroup.getNumControls() << endl; } -void ofxRadioList::setup(string title, vector &labels) +void RadioList::setup(string title, vector &labels) { // Store title for later use storedTitle = title; @@ -49,28 +51,28 @@ void ofxRadioList::setup(string title, vector &labels) setup(labels); } -void ofxRadioList::draw() +void RadioList::draw() { guiGroup.draw(); } -void ofxRadioList::setTitle(string title) +void RadioList::setTitle(string title) { storedTitle = title; guiGroup.setName(title); } -void ofxRadioList::setPosition(ofPoint p) +void RadioList::setPosition(ofPoint p) { guiGroup.setPosition(p); } -void ofxRadioList::setPosition(float x, float y) +void RadioList::setPosition(float x, float y) { guiGroup.setPosition(x, y); } -void ofxRadioList::selectItem(int index) +void RadioList::selectItem(int index) { if (index >= guiGroup.getNumControls()) { return; @@ -79,16 +81,16 @@ void ofxRadioList::selectItem(int index) unselectAll(); ofxToggle* toggle = static_cast(guiGroup.getControl(index)); - toggle->removeListener(this, &ofxRadioList::onToggleClicked); + toggle->removeListener(this, &RadioList::onToggleClicked); *toggle = true; // Select the specific radio button - toggle->addListener(this, &ofxRadioList::onToggleClicked); + toggle->addListener(this, &RadioList::onToggleClicked); string name = toggle->getName(); ofNotifyEvent(radioSelectedEvent, name, this); storedSelectedItem = index; } -void ofxRadioList::enable() +void RadioList::enable() { if (guiGroup.getNumControls() >= 0) { clear(); @@ -99,63 +101,63 @@ void ofxRadioList::enable() // Select the stored selected item without throwing an event ofxToggle* toggle = static_cast(guiGroup.getControl(storedSelectedItem)); - toggle->removeListener(this, &ofxRadioList::onToggleClicked); + toggle->removeListener(this, &RadioList::onToggleClicked); *toggle = true; - toggle->addListener(this, &ofxRadioList::onToggleClicked); + toggle->addListener(this, &RadioList::onToggleClicked); cout << "num items after enable: " << guiGroup.getNumControls() << endl; } -void ofxRadioList::disable() +void RadioList::disable() { // Just remove everything clear(); } -void ofxRadioList::clear() +void RadioList::clear() { int i; for (i = 0; i < guiGroup.getNumControls(); i++) { ofxToggle* toggle = static_cast(guiGroup.getControl(i)); - toggle->removeListener(this, &ofxRadioList::onToggleClicked); + toggle->removeListener(this, &RadioList::onToggleClicked); delete toggle; } guiGroup.clear(); } -void ofxRadioList::unselectAll() +void RadioList::unselectAll() { int i; for (i = 0; i < guiGroup.getNumControls(); i++) { ofxToggle* toggle = static_cast(guiGroup.getControl(i)); ofParameter* paramPtr = static_cast*>(&toggle->getParameter()); - toggle->removeListener(this, &ofxRadioList::onToggleClicked); + toggle->removeListener(this, &RadioList::onToggleClicked); *toggle = false; - toggle->addListener(this, &ofxRadioList::onToggleClicked); + toggle->addListener(this, &RadioList::onToggleClicked); } } -ofPoint ofxRadioList::getPosition() +ofPoint RadioList::getPosition() { return guiGroup.getPosition(); } -float ofxRadioList::getWidth() +float RadioList::getWidth() { return guiGroup.getWidth(); } -float ofxRadioList::getHeight() +float RadioList::getHeight() { return guiGroup.getHeight(); } -string ofxRadioList::getTitle() +string RadioList::getTitle() { return guiGroup.getName(); } -string ofxRadioList::getItemName(int index) +string RadioList::getItemName(int index) { if (index >= guiGroup.getNumControls()) { return ""; @@ -165,12 +167,12 @@ string ofxRadioList::getItemName(int index) return toggle->getName(); } -int ofxRadioList::size() +int RadioList::size() { return guiGroup.getNumControls(); } -void ofxRadioList::onToggleClicked(bool &toggleValue) +void RadioList::onToggleClicked(bool &toggleValue) { unselectAll(); @@ -186,3 +188,5 @@ void ofxRadioList::onToggleClicked(bool &toggleValue) } } } + + }} diff --git a/src/ui/RadioList.h b/src/ui/RadioList.h index 2736352..0d1daea 100644 --- a/src/ui/RadioList.h +++ b/src/ui/RadioList.h @@ -5,13 +5,15 @@ #include "ofxToggle.h" #include "ofxLabel.h" -class ofxRadioList +namespace ofx{ + namespace piMapper{ +class RadioList { public: - ofxRadioList(); - ofxRadioList(vector &labels); - ofxRadioList(string title, vector &labels); - ~ofxRadioList(); + RadioList(); + RadioList(vector &labels); + RadioList(string title, vector &labels); + ~RadioList(); void setup(vector &labels); void setup(string title, vector &labels); @@ -32,7 +34,7 @@ public: int size(); // This event notifies about a toggle being selected and passes it's name to the listeners. - // Use ofAddListener(ofxRadioListInstance.radioSelectedEvent, listenerClassPtr, &listenerClass::listenerMethod) + // Use ofAddListener(RadioListInstance.radioSelectedEvent, listenerClassPtr, &listenerClass::listenerMethod) // to listen to this. Listner method void listenerMethod(string & radioName) ofEvent radioSelectedEvent; @@ -43,4 +45,6 @@ private: int storedSelectedItem; void onToggleClicked(bool &toggleValue); -}; \ No newline at end of file +}; + + }} \ No newline at end of file From 6d44d1cae79a9e5cc1a1e358716794fac2cf5b26 Mon Sep 17 00:00:00 2001 From: Felix Dubrownik Date: Sun, 7 Sep 2014 22:08:39 +0200 Subject: [PATCH 4/4] ran clang Format with google code Style --- example/src/ofApp.cpp | 307 +++++++------- example/src/ofApp.h | 41 +- src/BaseJoint.cpp | 126 +++--- src/BaseJoint.h | 89 +++-- src/BaseSurface.cpp | 119 +++--- src/BaseSurface.h | 65 ++- src/CircleJoint.cpp | 85 ++-- src/CircleJoint.h | 31 +- src/EditorType.h | 16 +- src/GuiMode.h | 17 +- src/ProjectionEditor.cpp | 411 ++++++++++--------- src/ProjectionEditor.h | 89 ++--- src/QuadSurface.cpp | 473 +++++++++++----------- src/QuadSurface.h | 63 ++- src/SourcesEditor.cpp | 202 +++++----- src/SourcesEditor.h | 61 ++- src/SurfaceManager.cpp | 818 ++++++++++++++++++-------------------- src/SurfaceManager.h | 73 ++-- src/SurfaceManagerGui.cpp | 431 ++++++++++---------- src/SurfaceManagerGui.h | 64 ++- src/SurfaceType.h | 16 +- src/TextureEditor.cpp | 288 +++++++------- src/TextureEditor.h | 70 ++-- src/TriangleSurface.cpp | 233 +++++------ src/TriangleSurface.h | 49 ++- src/ui/RadioList.cpp | 267 ++++++------- src/ui/RadioList.h | 85 ++-- 27 files changed, 2194 insertions(+), 2395 deletions(-) diff --git a/example/src/ofApp.cpp b/example/src/ofApp.cpp index 18e5777..260ebfe 100755 --- a/example/src/ofApp.cpp +++ b/example/src/ofApp.cpp @@ -1,164 +1,183 @@ #include "ofApp.h" -void ofApp::setup() -{ - bShowInfo = false; - - // check if the surfaces.xml file is there - // if not - load defaultSurfaces.xml - if ( ofFile::doesFileExist("surfaces.xml") ) { - surfaceManager.loadXmlSettings("surfaces.xml"); - } else { - surfaceManager.loadXmlSettings("defaultSurfaces.xml"); - } - - // Pass the surface manager to the mapper graphical user interface - gui.setSurfaceManager( &surfaceManager ); - - // Create FBO - fbo = new ofFbo(); - fbo->allocate( 500, 500 ); - setFboAsTexture(); - - // Genereate rects - int numRects = 20; // change this to add more or less rects - for ( int i=0; igetHeight()), fbo->getWidth(), ofRandom(20)) ); - rectSpeeds.push_back( (1.0f + ofRandom(5)) ); - } +void ofApp::setup() { + bShowInfo = false; + + // check if the surfaces.xml file is there + // if not - load defaultSurfaces.xml + if (ofFile::doesFileExist("surfaces.xml")) { + surfaceManager.loadXmlSettings("surfaces.xml"); + } else { + surfaceManager.loadXmlSettings("defaultSurfaces.xml"); + } + + // Pass the surface manager to the mapper graphical user interface + gui.setSurfaceManager(&surfaceManager); + + // Create FBO + fbo = new ofFbo(); + fbo->allocate(500, 500); + setFboAsTexture(); + + // Genereate rects + int numRects = 20; // change this to add more or less rects + for (int i = 0; i < numRects; i++) { + rects.push_back(ofRectangle(0, ofRandom(fbo->getHeight()), fbo->getWidth(), + ofRandom(20))); + rectSpeeds.push_back((1.0f + ofRandom(5))); + } } -void ofApp::update() -{ - // Move rects - for ( int i=0; i fbo->getHeight() ) { - rects[i].y = -rects[i].getHeight(); - } - } - - // Fill FBO - fbo->begin(); - ofClear(0); - ofBackground(0); - ofSetColor(255); - for ( int i=0; i fbo->getHeight()) { + rects[i].y = -rects[i].getHeight(); } - fbo->end(); + } + + // Fill FBO + fbo->begin(); + ofClear(0); + ofBackground(0); + ofSetColor(255); + for (int i = 0; i < rects.size(); i++) { + ofRect(rects[i]); + } + fbo->end(); } -void ofApp::draw() -{ - // Draw the piMapper GUI - gui.draw(); - - if ( bShowInfo ) { - // Draw instructions - stringstream ss; - ss << "There are 4 modes:\n\n"; - ss << " 1. Presentation mode\n"; - ss << " 2. Texture mapping mode\n"; - ss << " 3. Projection mapping mode\n"; - ss << " 4. Source selection mode\n\n"; - ss << "You can switch between the modes by using <1>, <2>, <3> and <4> keys on the keyboard.\n\n"; - ss << "Press or to add random or normal surface.\n"; - ss << "Press to add a new quad surface.\n"; - ss << "Press to save the composition.\n"; - ss << "Press to toggle fullscreen.\n"; - ss << "Press to reassign the fbo texture to the first surface\n"; - ss << "Hit to hide this message."; - - ofDrawBitmapStringHighlight(ss.str(), 10, 20, ofColor(0,0,0,100), ofColor(255,255,255,200)); - } +void ofApp::draw() { + // Draw the piMapper GUI + gui.draw(); + + if (bShowInfo) { + // Draw instructions + stringstream ss; + ss << "There are 4 modes:\n\n"; + ss << " 1. Presentation mode\n"; + ss << " 2. Texture mapping mode\n"; + ss << " 3. Projection mapping mode\n"; + ss << " 4. Source selection mode\n\n"; + ss << "You can switch between the modes by using <1>, <2>, <3> and <4> " + "keys on the keyboard.\n\n"; + ss << "Press or to add random or normal surface.\n"; + ss << "Press to add a new quad surface.\n"; + ss << "Press to save the composition.\n"; + ss << "Press to toggle fullscreen.\n"; + ss << "Press to reassign the fbo texture to the first surface\n"; + ss << "Hit to hide this message."; + + ofDrawBitmapStringHighlight(ss.str(), 10, 20, ofColor(0, 0, 0, 100), + ofColor(255, 255, 255, 200)); + } } -void ofApp::exit() -{ - // Clear FBO from mem - delete fbo; +void ofApp::exit() { + // Clear FBO from mem + delete fbo; } -void ofApp::keyPressed(int key) -{ - cout << "Key pressed: " << static_cast(key) << endl; - - switch (key) { - case '1': gui.setMode(ofx::piMapper::GuiMode::NONE); break; - case '2': gui.setMode(ofx::piMapper::GuiMode::TEXTURE_MAPPING); break; - case '3': gui.setMode(ofx::piMapper::GuiMode::PROJECTION_MAPPING); break; - case '4': gui.setMode(ofx::piMapper::GuiMode::SOURCE_SELECTION); break; - case 'i': bShowInfo = !bShowInfo; break; - case 'r': addRandomSurface(); break; - case 'q': addQuadSurface(); break; - case 'n': addSurface(); break; - case 'f': ofToggleFullscreen(); break; - case 's': surfaceManager.saveXmlSettings("surfaces.xml"); break; - case 'a': setFboAsTexture(); break; - case OF_KEY_BACKSPACE: surfaceManager.removeSelectedSurface(); break; - default: break; - } +void ofApp::keyPressed(int key) { + cout << "Key pressed: " << static_cast(key) << endl; + + switch (key) { + case '1': + gui.setMode(ofx::piMapper::GuiMode::NONE); + break; + case '2': + gui.setMode(ofx::piMapper::GuiMode::TEXTURE_MAPPING); + break; + case '3': + gui.setMode(ofx::piMapper::GuiMode::PROJECTION_MAPPING); + break; + case '4': + gui.setMode(ofx::piMapper::GuiMode::SOURCE_SELECTION); + break; + case 'i': + bShowInfo = !bShowInfo; + break; + case 'r': + addRandomSurface(); + break; + case 'q': + addQuadSurface(); + break; + case 'n': + addSurface(); + break; + case 'f': + ofToggleFullscreen(); + break; + case 's': + surfaceManager.saveXmlSettings("surfaces.xml"); + break; + case 'a': + setFboAsTexture(); + break; + case OF_KEY_BACKSPACE: + surfaceManager.removeSelectedSurface(); + break; + default: + break; + } } -void ofApp::addRandomSurface() -{ - int surfaceType = ofx::piMapper::SurfaceType::TRIANGLE_SURFACE; - vector vertices; - vertices.push_back( ofVec2f( ofRandomWidth(), ofRandomHeight() ) ); - vertices.push_back( ofVec2f( ofRandomWidth(), ofRandomHeight() ) ); - vertices.push_back( ofVec2f( ofRandomWidth(), ofRandomHeight() ) ); - vector texCoords; - texCoords.push_back( ofVec2f( ofRandomuf(), ofRandomuf() ) ); - texCoords.push_back( ofVec2f( ofRandomuf(), ofRandomuf() ) ); - texCoords.push_back( ofVec2f( ofRandomuf(), ofRandomuf() ) ); - surfaceManager.addSurface(surfaceType, vertices, texCoords); - - // select this surface right away - surfaceManager.selectSurface(surfaceManager.size()-1); +void ofApp::addRandomSurface() { + int surfaceType = ofx::piMapper::SurfaceType::TRIANGLE_SURFACE; + vector vertices; + vertices.push_back(ofVec2f(ofRandomWidth(), ofRandomHeight())); + vertices.push_back(ofVec2f(ofRandomWidth(), ofRandomHeight())); + vertices.push_back(ofVec2f(ofRandomWidth(), ofRandomHeight())); + vector texCoords; + texCoords.push_back(ofVec2f(ofRandomuf(), ofRandomuf())); + texCoords.push_back(ofVec2f(ofRandomuf(), ofRandomuf())); + texCoords.push_back(ofVec2f(ofRandomuf(), ofRandomuf())); + surfaceManager.addSurface(surfaceType, vertices, texCoords); + + // select this surface right away + surfaceManager.selectSurface(surfaceManager.size() - 1); } -void ofApp::addQuadSurface() -{ - int surfaceType = ofx::piMapper::SurfaceType::QUAD_SURFACE; - vector vertices; - - int border = 50; - vertices.push_back(ofVec2f(border, border)); - vertices.push_back(ofVec2f(ofGetWidth() - border, border)); - vertices.push_back(ofVec2f(ofGetWidth() - border, ofGetHeight() - border)); - vertices.push_back(ofVec2f(border, ofGetHeight() - border)); - - 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))); - - surfaceManager.addSurface(surfaceType, vertices, texCoords); - - // select this surface right away - surfaceManager.selectSurface(surfaceManager.size()-1); +void ofApp::addQuadSurface() { + int surfaceType = ofx::piMapper::SurfaceType::QUAD_SURFACE; + vector vertices; + + int border = 50; + vertices.push_back(ofVec2f(border, border)); + vertices.push_back(ofVec2f(ofGetWidth() - border, border)); + vertices.push_back(ofVec2f(ofGetWidth() - border, ofGetHeight() - border)); + vertices.push_back(ofVec2f(border, ofGetHeight() - border)); + + 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))); + + surfaceManager.addSurface(surfaceType, vertices, texCoords); + + // select this surface right away + surfaceManager.selectSurface(surfaceManager.size() - 1); } -void ofApp::addSurface() -{ - int surfaceType = ofx::piMapper::SurfaceType::TRIANGLE_SURFACE; - vector vertices; - vertices.push_back( ofVec2f( (float)ofGetWidth()/2.0f, 0.0f ) ); - vertices.push_back( ofVec2f( (float)ofGetWidth(), (float)ofGetHeight() ) ); - vertices.push_back( ofVec2f( 0.0f, (float)ofGetHeight() ) ); - 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 ) ); - surfaceManager.addSurface(surfaceType, vertices, texCoords); - - // select this surface right away - surfaceManager.selectSurface(surfaceManager.size()-1); +void ofApp::addSurface() { + int surfaceType = ofx::piMapper::SurfaceType::TRIANGLE_SURFACE; + vector vertices; + vertices.push_back(ofVec2f((float)ofGetWidth() / 2.0f, 0.0f)); + vertices.push_back(ofVec2f((float)ofGetWidth(), (float)ofGetHeight())); + vertices.push_back(ofVec2f(0.0f, (float)ofGetHeight())); + 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)); + surfaceManager.addSurface(surfaceType, vertices, texCoords); + + // select this surface right away + surfaceManager.selectSurface(surfaceManager.size() - 1); } -void ofApp::setFboAsTexture() -{ - surfaceManager.getSurface(0)->setTexture( &fbo->getTextureReference() ); +void ofApp::setFboAsTexture() { + surfaceManager.getSurface(0)->setTexture(&fbo->getTextureReference()); } \ No newline at end of file diff --git a/example/src/ofApp.h b/example/src/ofApp.h index 10c1c99..1a81109 100755 --- a/example/src/ofApp.h +++ b/example/src/ofApp.h @@ -3,26 +3,25 @@ #include "ofMain.h" #include "ofxPiMapper.h" -class ofApp : public ofBaseApp -{ -public: - void setup(); - void update(); - void draw(); - void exit(); +class ofApp : public ofBaseApp { + public: + void setup(); + void update(); + void draw(); + void exit(); - void keyPressed(int key); - - void addRandomSurface(); - void addQuadSurface(); - void addSurface(); - void setFboAsTexture(); - - ofImage image; - ofx::piMapper::SurfaceManager surfaceManager; - ofx::piMapper::SurfaceManagerGui gui; - bool bShowInfo; - ofFbo* fbo; - vector rects; - vector rectSpeeds; + void keyPressed(int key); + + void addRandomSurface(); + void addQuadSurface(); + void addSurface(); + void setFboAsTexture(); + + ofImage image; + ofx::piMapper::SurfaceManager surfaceManager; + ofx::piMapper::SurfaceManagerGui gui; + bool bShowInfo; + ofFbo* fbo; + vector rects; + vector rectSpeeds; }; \ No newline at end of file diff --git a/src/BaseJoint.cpp b/src/BaseJoint.cpp index 19c490a..37b96b2 100644 --- a/src/BaseJoint.cpp +++ b/src/BaseJoint.cpp @@ -1,104 +1,72 @@ #include "BaseJoint.h" -namespace ofx{ -namespace piMapper{ - -BaseJoint::BaseJoint() -{ - setDefaultColors(); - setDefaultProperties(); - registerMouseEvents(); -} +namespace ofx { +namespace piMapper { -BaseJoint::~BaseJoint() -{ - unregisterMouseEvents(); +BaseJoint::BaseJoint() { + setDefaultColors(); + setDefaultProperties(); + registerMouseEvents(); } -void BaseJoint::registerMouseEvents() -{ - ofAddListener(ofEvents().mousePressed, this, &BaseJoint::mousePressed); - ofAddListener(ofEvents().mouseDragged, this, &BaseJoint::mouseDragged); -} +BaseJoint::~BaseJoint() { unregisterMouseEvents(); } -void BaseJoint::unregisterMouseEvents() -{ - ofRemoveListener(ofEvents().mousePressed, this, &BaseJoint::mousePressed); - ofRemoveListener(ofEvents().mouseDragged, this, &BaseJoint::mouseDragged); +void BaseJoint::registerMouseEvents() { + ofAddListener(ofEvents().mousePressed, this, &BaseJoint::mousePressed); + ofAddListener(ofEvents().mouseDragged, this, &BaseJoint::mouseDragged); } -void BaseJoint::mousePressed(ofMouseEventArgs& args) -{ - if ( hitTest(ofVec2f(args.x, args.y)) ) { - //selected = true; - clickDistance = position - ofVec2f(args.x, args.y); - //startDrag(); - } +void BaseJoint::unregisterMouseEvents() { + ofRemoveListener(ofEvents().mousePressed, this, &BaseJoint::mousePressed); + ofRemoveListener(ofEvents().mouseDragged, this, &BaseJoint::mouseDragged); } -void BaseJoint::mouseReleased(int x, int y, int button) -{ - stopDrag(); +void BaseJoint::mousePressed(ofMouseEventArgs& args) { + if (hitTest(ofVec2f(args.x, args.y))) { + // selected = true; + clickDistance = position - ofVec2f(args.x, args.y); + // startDrag(); + } } -void BaseJoint::mouseDragged(ofMouseEventArgs& args) -{ - if ( !bDrag ) return; - position = ofVec2f(args.x, args.y) + clickDistance; -} +void BaseJoint::mouseReleased(int x, int y, int button) { stopDrag(); } -void BaseJoint::startDrag() -{ - bDrag = true; +void BaseJoint::mouseDragged(ofMouseEventArgs& args) { + if (!bDrag) return; + position = ofVec2f(args.x, args.y) + clickDistance; } -void BaseJoint::stopDrag() -{ - bDrag = false; -} +void BaseJoint::startDrag() { bDrag = true; } -void BaseJoint::select() -{ - selected = true; -} +void BaseJoint::stopDrag() { bDrag = false; } -void BaseJoint::unselect() -{ - selected = false; -} +void BaseJoint::select() { selected = true; } -void BaseJoint::setClickDistance(ofVec2f newClickDistance) -{ - clickDistance = newClickDistance; -} +void BaseJoint::unselect() { selected = false; } -bool BaseJoint::isDragged() -{ - return bDrag; +void BaseJoint::setClickDistance(ofVec2f newClickDistance) { + clickDistance = newClickDistance; } -bool BaseJoint::isSelected() -{ - return selected; -} +bool BaseJoint::isDragged() { return bDrag; } -void BaseJoint::setDefaultColors() -{ - fillColor = ofColor(0, 255, 255, 0); - strokeColor = ofColor(255, 255, 255); - fillColorSelected = ofColor(255, 255, 0, 0); - strokeColorSelected = ofColor(255, 0, 0); +bool BaseJoint::isSelected() { return selected; } + +void BaseJoint::setDefaultColors() { + fillColor = ofColor(0, 255, 255, 0); + strokeColor = ofColor(255, 255, 255); + fillColorSelected = ofColor(255, 255, 0, 0); + strokeColorSelected = ofColor(255, 0, 0); } -void BaseJoint::setDefaultProperties() -{ - enabled = true; - visible = true; - position = ofVec2f(20.0f, 20.0f); - clickDistance = ofVec2f(0.0f, 0.0f); - bDrag = false; - selected = false; - strokeWidth = 1.5f; +void BaseJoint::setDefaultProperties() { + enabled = true; + visible = true; + position = ofVec2f(20.0f, 20.0f); + clickDistance = ofVec2f(0.0f, 0.0f); + bDrag = false; + selected = false; + strokeWidth = 1.5f; +} } - -}} \ No newline at end of file +} \ No newline at end of file diff --git a/src/BaseJoint.h b/src/BaseJoint.h index 5e5824d..d2fdcf0 100644 --- a/src/BaseJoint.h +++ b/src/BaseJoint.h @@ -2,49 +2,50 @@ #include "ofMain.h" -namespace ofx{ -namespace piMapper{ - - class BaseJoint { - public: - BaseJoint(); - ~BaseJoint(); - - void registerMouseEvents(); - void unregisterMouseEvents(); - - ofVec2f position; - bool enabled; - bool visible; - bool selected; - - void mousePressed(ofMouseEventArgs& args); - void mouseReleased(int x, int y, int button); - void mouseDragged(ofMouseEventArgs& args); - void startDrag(); - void stopDrag(); - void select(); - void unselect(); - void setClickDistance(ofVec2f newClickDistance); - bool isDragged(); - bool isSelected(); - - virtual void update(){}; - virtual void draw(){}; - virtual bool hitTest(ofVec2f position){}; - - protected: - ofColor fillColor; - ofColor strokeColor; - ofColor fillColorSelected; - ofColor strokeColorSelected; - float strokeWidth; - ofVec2f clickDistance; // How far from the center of the joint the user has clicked? - bool bDrag; - - private: - void setDefaultColors(); - void setDefaultProperties(); - }; +namespace ofx { +namespace piMapper { + +class BaseJoint { + public: + BaseJoint(); + ~BaseJoint(); + + void registerMouseEvents(); + void unregisterMouseEvents(); + + ofVec2f position; + bool enabled; + bool visible; + bool selected; + + void mousePressed(ofMouseEventArgs& args); + void mouseReleased(int x, int y, int button); + void mouseDragged(ofMouseEventArgs& args); + void startDrag(); + void stopDrag(); + void select(); + void unselect(); + void setClickDistance(ofVec2f newClickDistance); + bool isDragged(); + bool isSelected(); + + virtual void update() {}; + virtual void draw() {}; + virtual bool hitTest(ofVec2f position) {}; + + protected: + ofColor fillColor; + ofColor strokeColor; + ofColor fillColorSelected; + ofColor strokeColorSelected; + float strokeWidth; + ofVec2f clickDistance; // How far from the center of the joint the user has + // clicked? + bool bDrag; + + private: + void setDefaultColors(); + void setDefaultProperties(); +}; } } diff --git a/src/BaseSurface.cpp b/src/BaseSurface.cpp index 7350b29..b674cd8 100644 --- a/src/BaseSurface.cpp +++ b/src/BaseSurface.cpp @@ -1,77 +1,70 @@ #include "BaseSurface.h" -namespace ofx{ -namespace piMapper{ +namespace ofx { +namespace piMapper { -BaseSurface::BaseSurface() -{ - ofEnableNormalizedTexCoords(); - createDefaultTexture(); +BaseSurface::BaseSurface() { + ofEnableNormalizedTexCoords(); + createDefaultTexture(); } -void BaseSurface::createDefaultTexture() -{ - ofPixels pixels; - pixels.allocate(500, 500, 1); - for ( int i=0; igetWidth(), 0.0f)); - texMesh.addVertex(position + ofVec2f(texture->getWidth(), texture->getHeight())); - texMesh.addVertex(position + ofVec2f(0.0f, texture->getHeight())); - texMesh.addTriangle(0, 2, 3); - texMesh.addTriangle(0, 1, 2); - texMesh.addTexCoord(ofVec2f(0.0f, 0.0f)); - texMesh.addTexCoord(ofVec2f(1.0f, 0.0f)); - texMesh.addTexCoord(ofVec2f(1.0f, 1.0f)); - texMesh.addTexCoord(ofVec2f(0.0f, 1.0f)); - texture->bind(); - texMesh.draw(); - texture->unbind(); -} + // load pixels into texture + defaultTexture.loadData(pixels); -void BaseSurface::setTexture(ofTexture *texturePtr) -{ - texture = texturePtr; + // Assign default texture to texture pointer + texture = &defaultTexture; } -ofTexture* BaseSurface::getTexture() -{ - return texture; +void BaseSurface::drawTexture(ofVec2f position) { + ofMesh texMesh; + texMesh.addVertex(position); + texMesh.addVertex(position + ofVec2f(texture->getWidth(), 0.0f)); + texMesh.addVertex(position + + ofVec2f(texture->getWidth(), texture->getHeight())); + texMesh.addVertex(position + ofVec2f(0.0f, texture->getHeight())); + texMesh.addTriangle(0, 2, 3); + texMesh.addTriangle(0, 1, 2); + texMesh.addTexCoord(ofVec2f(0.0f, 0.0f)); + texMesh.addTexCoord(ofVec2f(1.0f, 0.0f)); + texMesh.addTexCoord(ofVec2f(1.0f, 1.0f)); + texMesh.addTexCoord(ofVec2f(0.0f, 1.0f)); + texture->bind(); + texMesh.draw(); + texture->unbind(); } -ofTexture* BaseSurface::getDefaultTexture() -{ - return &defaultTexture; -} +void BaseSurface::setTexture(ofTexture* texturePtr) { texture = texturePtr; } + +ofTexture* BaseSurface::getTexture() { return texture; } -}} \ No newline at end of file +ofTexture* BaseSurface::getDefaultTexture() { return &defaultTexture; } +} +} \ No newline at end of file diff --git a/src/BaseSurface.h b/src/BaseSurface.h index e8e5b8f..bf6703f 100644 --- a/src/BaseSurface.h +++ b/src/BaseSurface.h @@ -5,37 +5,36 @@ using namespace std; -namespace ofx{ - namespace piMapper{ -class BaseSurface -{ -public: - BaseSurface(); - virtual void setup(){}; - virtual void draw(){}; - virtual void setVertex(int index, ofVec2f p){}; - virtual void setTexCoord(int index, ofVec2f t){}; - virtual void moveBy(ofVec2f v){}; - virtual int getType(){}; - virtual bool hitTest(ofVec2f p){}; - virtual ofPolyline getHitArea(){}; - virtual ofPolyline getTextureHitArea(){}; - virtual vector& getVertices(){}; - virtual vector& getTexCoords(){}; - - // Draws a texture using ofMesh - void drawTexture(ofVec2f position); - void setTexture(ofTexture* texturePtr); - - ofTexture* getTexture(); - ofTexture* getDefaultTexture(); - -protected: - ofMesh mesh; - ofTexture* texture; - ofTexture defaultTexture; - - void createDefaultTexture(); -}; +namespace ofx { +namespace piMapper { +class BaseSurface { + public: + BaseSurface(); + virtual void setup() {}; + virtual void draw() {}; + virtual void setVertex(int index, ofVec2f p) {}; + virtual void setTexCoord(int index, ofVec2f t) {}; + virtual void moveBy(ofVec2f v) {}; + virtual int getType() {}; + virtual bool hitTest(ofVec2f p) {}; + virtual ofPolyline getHitArea() {}; + virtual ofPolyline getTextureHitArea() {}; + virtual vector& getVertices() {}; + virtual vector& getTexCoords() {}; + + // Draws a texture using ofMesh + void drawTexture(ofVec2f position); + void setTexture(ofTexture* texturePtr); + + ofTexture* getTexture(); + ofTexture* getDefaultTexture(); - }} \ No newline at end of file + protected: + ofMesh mesh; + ofTexture* texture; + ofTexture defaultTexture; + + void createDefaultTexture(); +}; +} +} \ No newline at end of file diff --git a/src/CircleJoint.cpp b/src/CircleJoint.cpp index 938659f..1e3bb3d 100644 --- a/src/CircleJoint.cpp +++ b/src/CircleJoint.cpp @@ -1,56 +1,49 @@ #include "CircleJoint.h" -namespace ofx{ -namespace piMapper{ +namespace ofx { +namespace piMapper { -CircleJoint::CircleJoint() -{ - setDefaultProperties(); -} +CircleJoint::CircleJoint() { setDefaultProperties(); } -void CircleJoint::update() -{ - if (!enabled) return; +void CircleJoint::update() { + if (!enabled) return; } -void CircleJoint::draw() -{ - if (!visible) return; - if (!enabled) return; - - ofPushStyle(); - ofFill(); - - if ( selected ) { - ofSetColor(fillColorSelected); - } else { - ofSetColor(fillColor); - } - - ofCircle(position.x, position.y, radius); - ofNoFill(); - - if ( selected ) { - ofSetColor(strokeColorSelected); - } else { - ofSetColor(strokeColor); - } - - ofSetLineWidth(strokeWidth); - ofCircle(position.x, position.y, radius); - ofPopStyle(); -} +void CircleJoint::draw() { + if (!visible) return; + if (!enabled) return; -void CircleJoint::setDefaultProperties() -{ - radius = 10.0f; -} + ofPushStyle(); + ofFill(); + + if (selected) { + ofSetColor(fillColorSelected); + } else { + ofSetColor(fillColor); + } + + ofCircle(position.x, position.y, radius); + ofNoFill(); -bool CircleJoint::hitTest(ofVec2f pos) -{ - float distance = position.distance(pos); - if ( distance < radius ) return true; - else return false; + if (selected) { + ofSetColor(strokeColorSelected); + } else { + ofSetColor(strokeColor); + } + + ofSetLineWidth(strokeWidth); + ofCircle(position.x, position.y, radius); + ofPopStyle(); } - }} \ No newline at end of file +void CircleJoint::setDefaultProperties() { radius = 10.0f; } + +bool CircleJoint::hitTest(ofVec2f pos) { + float distance = position.distance(pos); + if (distance < radius) + return true; + else + return false; +} +} +} \ No newline at end of file diff --git a/src/CircleJoint.h b/src/CircleJoint.h index 812bc3c..ade616f 100644 --- a/src/CircleJoint.h +++ b/src/CircleJoint.h @@ -3,21 +3,20 @@ #include "ofMain.h" #include "BaseJoint.h" +namespace ofx { +namespace piMapper { +class CircleJoint : public BaseJoint { + public: + CircleJoint(); -namespace ofx{ -namespace piMapper{ -class CircleJoint : public BaseJoint -{ -public: - CircleJoint(); - - void update(); - void draw(); - bool hitTest(ofVec2f position); - -private: - float radius; - - void setDefaultProperties(); + void update(); + void draw(); + bool hitTest(ofVec2f position); + + private: + float radius; + + void setDefaultProperties(); }; -}} \ No newline at end of file +} +} \ No newline at end of file diff --git a/src/EditorType.h b/src/EditorType.h index af5650b..b7fdc14 100644 --- a/src/EditorType.h +++ b/src/EditorType.h @@ -1,13 +1,9 @@ #pragma once -namespace ofx{ - namespace piMapper{ -struct EditorType -{ - enum { - TEXTURE, - PROJECTION - }; +namespace ofx { +namespace piMapper { +struct EditorType { + enum { TEXTURE, PROJECTION }; }; - -}} \ No newline at end of file +} +} \ No newline at end of file diff --git a/src/GuiMode.h b/src/GuiMode.h index 4c65a74..f6b1ac4 100644 --- a/src/GuiMode.h +++ b/src/GuiMode.h @@ -1,14 +1,9 @@ #pragma once -namespace ofx{ - namespace piMapper{ -struct GuiMode -{ - enum { - NONE, - TEXTURE_MAPPING, - PROJECTION_MAPPING, - SOURCE_SELECTION - }; +namespace ofx { +namespace piMapper { +struct GuiMode { + enum { NONE, TEXTURE_MAPPING, PROJECTION_MAPPING, SOURCE_SELECTION }; }; - }} \ No newline at end of file +} +} \ No newline at end of file diff --git a/src/ProjectionEditor.cpp b/src/ProjectionEditor.cpp index 12adcca..1851cd2 100644 --- a/src/ProjectionEditor.cpp +++ b/src/ProjectionEditor.cpp @@ -1,270 +1,263 @@ #include "ProjectionEditor.h" -namespace ofx{ - namespace piMapper{ -ProjectionEditor::ProjectionEditor() -{ - surfaceManager = NULL; - bShiftKeyDown = false; - fSnapDistance = 10.0f; - enable(); +namespace ofx { +namespace piMapper { +ProjectionEditor::ProjectionEditor() { + surfaceManager = NULL; + bShiftKeyDown = false; + fSnapDistance = 10.0f; + enable(); } -ProjectionEditor::~ProjectionEditor() -{ - clearJoints(); - surfaceManager = NULL; - disable(); +ProjectionEditor::~ProjectionEditor() { + clearJoints(); + surfaceManager = NULL; + disable(); } -void ProjectionEditor::registerAppEvents() -{ - ofAddListener(ofEvents().update, this, &ProjectionEditor::update); - ofAddListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage); +void ProjectionEditor::registerAppEvents() { + ofAddListener(ofEvents().update, this, &ProjectionEditor::update); + ofAddListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage); } -void ProjectionEditor::unregisterAppEvents() -{ - ofRemoveListener(ofEvents().update, this, &ProjectionEditor::update); - ofRemoveListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage); +void ProjectionEditor::unregisterAppEvents() { + ofRemoveListener(ofEvents().update, this, &ProjectionEditor::update); + ofRemoveListener(ofEvents().messageEvent, this, + &ProjectionEditor::gotMessage); } -void ProjectionEditor::registerMouseEvents() -{ - ofAddListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged); +void ProjectionEditor::registerMouseEvents() { + ofAddListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged); } -void ProjectionEditor::unregisterMouseEvents() -{ - ofRemoveListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged); +void ProjectionEditor::unregisterMouseEvents() { + ofRemoveListener(ofEvents().mouseDragged, this, + &ProjectionEditor::mouseDragged); } -void ProjectionEditor::registerKeyEvents() -{ - ofAddListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); - ofAddListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased); +void ProjectionEditor::registerKeyEvents() { + ofAddListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); + ofAddListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased); } -void ProjectionEditor::unregisterKeyEvents() -{ - ofRemoveListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); - ofRemoveListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased); +void ProjectionEditor::unregisterKeyEvents() { + ofRemoveListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); + ofRemoveListener(ofEvents().keyReleased, this, + &ProjectionEditor::keyReleased); } -void ProjectionEditor::enable() -{ - registerAppEvents(); - registerMouseEvents(); - registerKeyEvents(); +void ProjectionEditor::enable() { + registerAppEvents(); + registerMouseEvents(); + registerKeyEvents(); } -void ProjectionEditor::disable() -{ - unregisterAppEvents(); - unregisterMouseEvents(); - unregisterKeyEvents(); +void ProjectionEditor::disable() { + unregisterAppEvents(); + unregisterMouseEvents(); + unregisterKeyEvents(); } -void ProjectionEditor::update(ofEventArgs &args) -{ - // update surface if one of the joints is being dragged - for ( int i=0; iisDragged() || joints[i]->isSelected() ) { - - if ( surfaceManager->getSelectedSurface() != NULL ) { - // update vertex to new location - surfaceManager->getSelectedSurface()->setVertex(i, joints[i]->position); - } else { - // clear joints if there is no surface selected - // as the remove selected surface in the surface manager - // is not supposed to access joints here - joints.clear(); - } - break; - } +void ProjectionEditor::update(ofEventArgs& args) { + // update surface if one of the joints is being dragged + for (int i = 0; i < joints.size(); i++) { + if (joints[i]->isDragged() || joints[i]->isSelected()) { + if (surfaceManager->getSelectedSurface() != NULL) { + // update vertex to new location + surfaceManager->getSelectedSurface()->setVertex(i, joints[i]->position); + } else { + // clear joints if there is no surface selected + // as the remove selected surface in the surface manager + // is not supposed to access joints here + joints.clear(); + } + break; } + } } -void ProjectionEditor::draw() -{ - if ( surfaceManager == NULL ) return; - if ( surfaceManager->getSelectedSurface() == NULL ) return; - if ( joints.size() <= 0 ) createJoints(); - drawJoints(); +void ProjectionEditor::draw() { + if (surfaceManager == NULL) return; + if (surfaceManager->getSelectedSurface() == NULL) return; + if (joints.size() <= 0) createJoints(); + drawJoints(); } -void ProjectionEditor::mouseDragged(ofMouseEventArgs &args) -{ - ofVec2f mousePosition = ofVec2f(args.x, args.y); - - // Collect all vertices of the projection surfaces - vector allVertices; - for ( int i=0; isize(); i++ ) { - BaseSurface* surface = surfaceManager->getSurface(i); - if ( surface == surfaceManager->getSelectedSurface() ) { - continue; // Don't add vertices of selected surface - } - for ( int j=0; jgetVertices().size(); j++ ) { - allVertices.push_back(&surface->getVertices()[j]); - } +void ProjectionEditor::mouseDragged(ofMouseEventArgs& args) { + ofVec2f mousePosition = ofVec2f(args.x, args.y); + + // Collect all vertices of the projection surfaces + vector allVertices; + for (int i = 0; i < surfaceManager->size(); i++) { + BaseSurface* surface = surfaceManager->getSurface(i); + if (surface == surfaceManager->getSelectedSurface()) { + continue; // Don't add vertices of selected surface + } + for (int j = 0; j < surface->getVertices().size(); j++) { + allVertices.push_back(&surface->getVertices()[j]); } - - // Snap currently dragged joint to nearest vertex - for ( int i=0; iisDragged() ) { - // Snap it! - for ( int j=0; jposition = *allVertices[j]; - ofVec2f clickDistance = joints[i]->position - ofVec2f(args.x, args.y); - joints[i]->setClickDistance(clickDistance); - break; - } - } + } + + // Snap currently dragged joint to nearest vertex + for (int i = 0; i < joints.size(); i++) { + if (joints[i]->isDragged()) { + // Snap it! + for (int j = 0; j < allVertices.size(); j++) { + float distance = mousePosition.distance(*allVertices[j]); + // cout << "distance: " << distance << endl; + if (distance < fSnapDistance) { + joints[i]->position = *allVertices[j]; + ofVec2f clickDistance = joints[i]->position - ofVec2f(args.x, args.y); + joints[i]->setClickDistance(clickDistance); + break; } + } } + } } -void ProjectionEditor::keyPressed(ofKeyEventArgs &args) -{ - int key = args.key; - float moveStep; - - if (bShiftKeyDown) moveStep = 10.0f; - else moveStep = 0.5f; - - switch (key) { - case OF_KEY_LEFT: moveSelection(ofVec2f(-moveStep,0.0f)); break; - case OF_KEY_RIGHT: moveSelection(ofVec2f(moveStep,0.0f)); break; - case OF_KEY_UP: moveSelection(ofVec2f(0.0f,-moveStep)); break; - case OF_KEY_DOWN: moveSelection(ofVec2f(0.0f,moveStep)); break; - case OF_KEY_SHIFT: bShiftKeyDown = true; break; - } +void ProjectionEditor::keyPressed(ofKeyEventArgs& args) { + int key = args.key; + float moveStep; + + if (bShiftKeyDown) + moveStep = 10.0f; + else + moveStep = 0.5f; + + switch (key) { + case OF_KEY_LEFT: + moveSelection(ofVec2f(-moveStep, 0.0f)); + break; + case OF_KEY_RIGHT: + moveSelection(ofVec2f(moveStep, 0.0f)); + break; + case OF_KEY_UP: + moveSelection(ofVec2f(0.0f, -moveStep)); + break; + case OF_KEY_DOWN: + moveSelection(ofVec2f(0.0f, moveStep)); + break; + case OF_KEY_SHIFT: + bShiftKeyDown = true; + break; + } } -void ProjectionEditor::keyReleased(ofKeyEventArgs &args) -{ - int key = args.key; - switch (key) { - case OF_KEY_SHIFT: bShiftKeyDown = false; break; - } +void ProjectionEditor::keyReleased(ofKeyEventArgs& args) { + int key = args.key; + switch (key) { + case OF_KEY_SHIFT: + bShiftKeyDown = false; + break; + } } -void ProjectionEditor::gotMessage(ofMessage& msg) -{ - if (msg.message == "surfaceSelected") { - // refresh gui - clearJoints(); - createJoints(); - } +void ProjectionEditor::gotMessage(ofMessage& msg) { + if (msg.message == "surfaceSelected") { + // refresh gui + clearJoints(); + createJoints(); + } } -void ProjectionEditor::setSurfaceManager(SurfaceManager *newSurfaceManager) -{ - surfaceManager = newSurfaceManager; +void ProjectionEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) { + surfaceManager = newSurfaceManager; } -void ProjectionEditor::clearJoints() -{ - while ( joints.size() ) { - delete joints.back(); - joints.pop_back(); - } +void ProjectionEditor::clearJoints() { + while (joints.size()) { + delete joints.back(); + joints.pop_back(); + } } -void ProjectionEditor::createJoints() -{ - if ( surfaceManager == NULL ) return; - clearJoints(); - - if ( surfaceManager->getSelectedSurface() == NULL ) { - ofLog(OF_LOG_WARNING, "Trying to create joints while no surface selected."); - return; - } - - vector& vertices = surfaceManager->getSelectedSurface()->getVertices(); - - for ( int i=0; iposition = ofVec2f(vertices[i].x, vertices[i].y); - } +void ProjectionEditor::createJoints() { + if (surfaceManager == NULL) return; + clearJoints(); + + if (surfaceManager->getSelectedSurface() == NULL) { + ofLog(OF_LOG_WARNING, "Trying to create joints while no surface selected."); + return; + } + + vector& vertices = + surfaceManager->getSelectedSurface()->getVertices(); + + for (int i = 0; i < vertices.size(); i++) { + joints.push_back(new CircleJoint()); + joints.back()->position = ofVec2f(vertices[i].x, vertices[i].y); + } } -void ProjectionEditor::updateJoints() -{ - vector& vertices = surfaceManager->getSelectedSurface()->getVertices(); - for ( int i=0; iposition = ofVec2f(vertices[i].x, vertices[i].y); - } +void ProjectionEditor::updateJoints() { + vector& vertices = + surfaceManager->getSelectedSurface()->getVertices(); + for (int i = 0; i < vertices.size(); i++) { + joints[i]->position = ofVec2f(vertices[i].x, vertices[i].y); + } } -void ProjectionEditor::unselectAllJoints() -{ - for ( int i=0; iunselect(); - } +void ProjectionEditor::unselectAllJoints() { + for (int i = 0; i < joints.size(); i++) { + joints[i]->unselect(); + } } -void ProjectionEditor::moveSelectedSurface(ofVec2f by) -{ - if ( surfaceManager == NULL ) return; - if ( surfaceManager->getSelectedSurface() == NULL ) return; - surfaceManager->getSelectedSurface()->moveBy(by); - /*vector& vertices = surfaceManager->getSelectedSurface()->getVertices(); - for (int i=0; igetSelectedSurface() == NULL) return; + surfaceManager->getSelectedSurface()->moveBy(by); + /*vector& vertices = + surfaceManager->getSelectedSurface()->getVertices(); + for (int i=0; istopDrag(); - } +void ProjectionEditor::stopDragJoints() { + for (int i = 0; i < joints.size(); i++) { + joints[i]->stopDrag(); + } } -void ProjectionEditor::moveSelection(ofVec2f by) -{ - // check if joints selected - bool bJointSelected = false; - BaseJoint* selectedJoint; - for ( int i=0; iisSelected()) { - bJointSelected = true; - selectedJoint = joints[i]; - break; - } - } - - if ( bJointSelected ) { - selectedJoint->position += by; - } else { - moveSelectedSurface(by); +void ProjectionEditor::moveSelection(ofVec2f by) { + // check if joints selected + bool bJointSelected = false; + BaseJoint* selectedJoint; + for (int i = 0; i < joints.size(); i++) { + if (joints[i]->isSelected()) { + bJointSelected = true; + selectedJoint = joints[i]; + break; } + } + + if (bJointSelected) { + selectedJoint->position += by; + } else { + moveSelectedSurface(by); + } } -void ProjectionEditor::setSnapDistance(float newSnapDistance) -{ - fSnapDistance = newSnapDistance; +void ProjectionEditor::setSnapDistance(float newSnapDistance) { + fSnapDistance = newSnapDistance; } -CircleJoint* ProjectionEditor::hitTestJoints(ofVec2f pos) -{ - for ( int i=0; ihitTest(pos) ){ - return joints[i]; - } +CircleJoint* ProjectionEditor::hitTestJoints(ofVec2f pos) { + for (int i = 0; i < joints.size(); i++) { + if (joints[i]->hitTest(pos)) { + return joints[i]; } - return NULL; + } + return NULL; } -void ProjectionEditor::drawJoints() -{ - for ( int i=0; idraw(); - } +void ProjectionEditor::drawJoints() { + for (int i = 0; i < joints.size(); i++) { + joints[i]->draw(); + } +} } - }} \ No newline at end of file +} \ No newline at end of file diff --git a/src/ProjectionEditor.h b/src/ProjectionEditor.h index 6a7a877..50b8582 100755 --- a/src/ProjectionEditor.h +++ b/src/ProjectionEditor.h @@ -3,49 +3,48 @@ #include "SurfaceManager.h" #include "CircleJoint.h" -namespace ofx{ - namespace piMapper{ -class ProjectionEditor -{ -public: - ProjectionEditor(); - ~ProjectionEditor(); - - void registerAppEvents(); - void unregisterAppEvents(); - void registerMouseEvents(); - void unregisterMouseEvents(); - void registerKeyEvents(); - void unregisterKeyEvents(); - - void enable(); - void disable(); - - void update(ofEventArgs& args); - void draw(); - void mouseDragged(ofMouseEventArgs& args); - void keyPressed(ofKeyEventArgs& args); - void keyReleased(ofKeyEventArgs& args); - void gotMessage(ofMessage& msg); - void setSurfaceManager(SurfaceManager* newSurfaceManager); - void clearJoints(); - void createJoints(); - void updateJoints(); - void unselectAllJoints(); - void moveSelectedSurface(ofVec2f by); - void stopDragJoints(); - void updateVertices(); - void moveSelection(ofVec2f by); - void setSnapDistance(float newSnapDistance); - CircleJoint* hitTestJoints(ofVec2f pos); - -private: - SurfaceManager* surfaceManager; - vector joints; - bool bShiftKeyDown; - float fSnapDistance; - - void drawJoints(); -}; +namespace ofx { +namespace piMapper { +class ProjectionEditor { + public: + ProjectionEditor(); + ~ProjectionEditor(); + + void registerAppEvents(); + void unregisterAppEvents(); + void registerMouseEvents(); + void unregisterMouseEvents(); + void registerKeyEvents(); + void unregisterKeyEvents(); + + void enable(); + void disable(); - }} \ No newline at end of file + void update(ofEventArgs& args); + void draw(); + void mouseDragged(ofMouseEventArgs& args); + void keyPressed(ofKeyEventArgs& args); + void keyReleased(ofKeyEventArgs& args); + void gotMessage(ofMessage& msg); + void setSurfaceManager(SurfaceManager* newSurfaceManager); + void clearJoints(); + void createJoints(); + void updateJoints(); + void unselectAllJoints(); + void moveSelectedSurface(ofVec2f by); + void stopDragJoints(); + void updateVertices(); + void moveSelection(ofVec2f by); + void setSnapDistance(float newSnapDistance); + CircleJoint* hitTestJoints(ofVec2f pos); + + private: + SurfaceManager* surfaceManager; + vector joints; + bool bShiftKeyDown; + float fSnapDistance; + + void drawJoints(); +}; +} +} \ No newline at end of file diff --git a/src/QuadSurface.cpp b/src/QuadSurface.cpp index e71f45c..c72587c 100644 --- a/src/QuadSurface.cpp +++ b/src/QuadSurface.cpp @@ -1,266 +1,243 @@ #include "QuadSurface.h" -namespace ofx{ - namespace piMapper{ -QuadSurface::QuadSurface() -{ - cout << "QuadSurface constructor." << endl; - setup(); -} - -QuadSurface::~QuadSurface() -{ - cout << "QuadSurface destructor." << endl; -} - -void QuadSurface::setup() -{ - // Create 4 points for the 2 triangles - ofVec2f p1 = ofVec2f(0, 0); - ofVec2f p2 = ofVec2f(0, ofGetHeight()); - ofVec2f p3 = ofVec2f(ofGetWidth(), ofGetHeight()); - ofVec2f p4 = ofVec2f(ofGetWidth(), 0); - - // Create 4 point for the texture coordinates - ofVec2f t1 = ofVec2f(ofVec2f(0.0f, 0.0f)); - ofVec2f t2 = ofVec2f(ofVec2f(1.0f, 0.0f)); - ofVec2f t3 = ofVec2f(ofVec2f(1.0f, 1.0f)); - ofVec2f t4 = ofVec2f(ofVec2f(0.0f, 1.0f)); - - setup( p1, p2, p3, p4, t1, t2, t3, t4, texture ); -} - -void QuadSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4, - ofVec2f t1, ofVec2f t2, ofVec2f t3, ofVec2f t4, ofTexture* texturePtr ) -{ - // Assign texture - texture = texturePtr; - - // Clear mesh - mesh.clear(); - - // Create a surface with the points - mesh.addVertex( p1 ); - mesh.addVertex( p2 ); - mesh.addVertex( p3 ); - mesh.addVertex( p4 ); - - // Add 2 triangles - mesh.addTriangle(0, 2, 3); - mesh.addTriangle(0, 1, 2); - - // Add texture coordinates - mesh.addTexCoord(t1); - mesh.addTexCoord(t2); - mesh.addTexCoord(t3); - mesh.addTexCoord(t4); - - // Pure GL setup - // indices - quadIndices[0] = 0; - quadIndices[1] = 1; - quadIndices[2] = 2; - quadIndices[3] = 0; - quadIndices[4] = 2; - quadIndices[5] = 3; - //tex coords (those are alway 0) - quadTexCoordinates[2] = 0; - quadTexCoordinates[6] = 0; - quadTexCoordinates[10] = 0; - quadTexCoordinates[14] = 0; - - calculate4dTextureCoords(); -} - -void QuadSurface::draw() -{ - /*if(mesh.haveVertsChanged() || mesh.haveTexCoordsChanged()){ - calculate4dTextureCoords(); - }*/ - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glTexCoordPointer(4, GL_FLOAT, 0, quadTexCoordinates); - glVertexPointer(3, GL_FLOAT, 0, quadVertices); - - texture->bind(); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, quadIndices); - texture->unbind(); -} +namespace ofx { +namespace piMapper { +QuadSurface::QuadSurface() { + cout << "QuadSurface constructor." << endl; + setup(); +} + +QuadSurface::~QuadSurface() { cout << "QuadSurface destructor." << endl; } + +void QuadSurface::setup() { + // Create 4 points for the 2 triangles + ofVec2f p1 = ofVec2f(0, 0); + ofVec2f p2 = ofVec2f(0, ofGetHeight()); + ofVec2f p3 = ofVec2f(ofGetWidth(), ofGetHeight()); + ofVec2f p4 = ofVec2f(ofGetWidth(), 0); + + // Create 4 point for the texture coordinates + ofVec2f t1 = ofVec2f(ofVec2f(0.0f, 0.0f)); + ofVec2f t2 = ofVec2f(ofVec2f(1.0f, 0.0f)); + ofVec2f t3 = ofVec2f(ofVec2f(1.0f, 1.0f)); + ofVec2f t4 = ofVec2f(ofVec2f(0.0f, 1.0f)); -void QuadSurface::setVertex(int index, ofVec2f p) -{ - if ( index > 3 ) { - ofLog() << "Vertex with this index does not exist: " << index << endl; - return; - } - - mesh.setVertex(index, p); - calculate4dTextureCoords(); -} + setup(p1, p2, p3, p4, t1, t2, t3, t4, texture); +} + +void QuadSurface::setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4, + ofVec2f t1, ofVec2f t2, ofVec2f t3, ofVec2f t4, + ofTexture* texturePtr) { + // Assign texture + texture = texturePtr; -void QuadSurface::setTexCoord(int index, ofVec2f t) -{ - if ( index > 3 ) { - ofLog() << "Texture coordinate with this index does not exist: " << index << endl; - return; - } - - mesh.setTexCoord(index, t); - calculate4dTextureCoords(); -} + // Clear mesh + mesh.clear(); -void QuadSurface::moveBy(ofVec2f v) -{ - vector& vertices = getVertices(); - for (int i=0; i 3 ) { - ofLog() << "Vertex with this index does not exist: " << index << endl; - throw std::runtime_error("Vertex index out of bounds."); - } - - ofVec3f vert = mesh.getVertex(index); - return ofVec2f(vert.x, vert.y); -} + // Pure GL setup + // indices + quadIndices[0] = 0; + quadIndices[1] = 1; + quadIndices[2] = 2; + quadIndices[3] = 0; + quadIndices[4] = 2; + quadIndices[5] = 3; + // tex coords (those are alway 0) + quadTexCoordinates[2] = 0; + quadTexCoordinates[6] = 0; + quadTexCoordinates[10] = 0; + quadTexCoordinates[14] = 0; -ofVec2f QuadSurface::getTexCoord(int index) -{ - if (index > 3) { - throw std::runtime_error("Texture coordinate index out of bounds."); - } - - return mesh.getTexCoord(index); + calculate4dTextureCoords(); } -ofPolyline QuadSurface::getHitArea() -{ - ofPolyline line; - line.addVertex( ofPoint( mesh.getVertex(0).x, mesh.getVertex(0).y ) ); - line.addVertex( ofPoint( mesh.getVertex(1).x, mesh.getVertex(1).y ) ); - line.addVertex( ofPoint( mesh.getVertex(2).x, mesh.getVertex(2).y ) ); - line.addVertex( ofPoint( mesh.getVertex(3).x, mesh.getVertex(3).y ) ); - line.close(); - - return line; -} +void QuadSurface::draw() { + /*if(mesh.haveVertsChanged() || mesh.haveTexCoordsChanged()){ + calculate4dTextureCoords(); + }*/ + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(4, GL_FLOAT, 0, quadTexCoordinates); + glVertexPointer(3, GL_FLOAT, 0, quadVertices); -ofPolyline QuadSurface::getTextureHitArea() -{ - ofPolyline line; - vector& texCoords = mesh.getTexCoords(); - ofVec2f textureSize = ofVec2f(texture->getWidth(), texture->getHeight()); - for ( int i=0; ibind(); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, quadIndices); + texture->unbind(); } -vector& QuadSurface::getVertices() -{ - // return only joint vertices - return mesh.getVertices(); -} +void QuadSurface::setVertex(int index, ofVec2f p) { + if (index > 3) { + ofLog() << "Vertex with this index does not exist: " << index << endl; + return; + } -vector& QuadSurface::getTexCoords() -{ - - return mesh.getTexCoords(); + mesh.setVertex(index, p); + calculate4dTextureCoords(); } -void QuadSurface::calculate4dTextureCoords() -{ - // Perspective Warping with OpenGL Fixed Pipeline and q coordinates - // see: - // http://www.reedbeta.com/blog/2012/05/26/quadrilateral-interpolation-part-1/ - // for information on the technique - // Pue OpenGL is used because the ofMesh sadly doesn't support ofVec4f as - // texture coordinates. - // calculate intersection point - ofVec3f p0 = mesh.getVertex(0); - ofVec3f p1 = mesh.getVertex(1); - ofVec3f p2 = mesh.getVertex(2); - ofVec3f p3 = mesh.getVertex(3); - - ofVec3f t0 = mesh.getTexCoord(0); - ofVec3f t1 = mesh.getTexCoord(1); - ofVec3f t2 = mesh.getTexCoord(2); - ofVec3f t3 = mesh.getTexCoord(3); - - ofPoint interSect; - ofLineSegmentIntersection(ofPoint(p0.x, p0.y), ofPoint(p2.x, p2.y), - ofPoint(p1.x, p1.y), ofPoint(p3.x, p3.y), - interSect); - ofVec3f interSecVec = ofVec3f(interSect.x, interSect.y, 0); - - // calculate distances to intersection point - float d0 = interSecVec.distance(p0); - float d1 = interSecVec.distance(p1); - float d2 = interSecVec.distance(p2); - float d3 = interSecVec.distance(p3); - - // vertices - // top left corner - quadVertices[0] = p0.x; - quadVertices[1] = p0.y; - quadVertices[2] = 0; - // top right corner - quadVertices[3] = p1.x; - quadVertices[4] = p1.y; - quadVertices[5] = 0; - // bottom right corner - quadVertices[6] = p2.x; - quadVertices[7] = p2.y; - quadVertices[8] = 0; - // bottom left corner - quadVertices[9] = p3.x; - quadVertices[10] = p3.y; - quadVertices[11] = 0; - - float q0 = (d0 + d2) / (d2); - float q1 = (d1 + d3) / (d3); - float q2 = (d2 + d0) / (d0); - float q3 = (d3 + d1) / (d1); - - quadTexCoordinates[0] = t0.x; - quadTexCoordinates[1] = t0.y; - quadTexCoordinates[3] = q0; - - quadTexCoordinates[4] = t1.x * q1; - quadTexCoordinates[5] = t1.y; - quadTexCoordinates[7] = q1; - - quadTexCoordinates[8] = t2.x * q2; - quadTexCoordinates[9] = t2.y * q2; - quadTexCoordinates[11] = q2; - - quadTexCoordinates[12] = t3.x; - quadTexCoordinates[13] = t3.y * q3; - quadTexCoordinates[15] = q3; - -} +void QuadSurface::setTexCoord(int index, ofVec2f t) { + if (index > 3) { + ofLog() << "Texture coordinate with this index does not exist: " << index + << endl; + return; + } + + mesh.setTexCoord(index, t); + calculate4dTextureCoords(); +} + +void QuadSurface::moveBy(ofVec2f v) { + vector& vertices = getVertices(); + for (int i = 0; i < vertices.size(); i++) { + vertices[i] += v; + } + calculate4dTextureCoords(); +} + +int QuadSurface::getType() { return SurfaceType::QUAD_SURFACE; } + +bool QuadSurface::hitTest(ofVec2f p) { + // Construct ofPolyline from vertices + ofPolyline line = getHitArea(); + + if (line.inside(p.x, p.y)) { + return true; + } else { + return false; + } +} + +ofVec2f QuadSurface::getVertex(int index) { + if (index > 3) { + ofLog() << "Vertex with this index does not exist: " << index << endl; + throw std::runtime_error("Vertex index out of bounds."); + } + + ofVec3f vert = mesh.getVertex(index); + return ofVec2f(vert.x, vert.y); +} + +ofVec2f QuadSurface::getTexCoord(int index) { + if (index > 3) { + throw std::runtime_error("Texture coordinate index out of bounds."); + } + + return mesh.getTexCoord(index); +} + +ofPolyline QuadSurface::getHitArea() { + ofPolyline line; + line.addVertex(ofPoint(mesh.getVertex(0).x, mesh.getVertex(0).y)); + line.addVertex(ofPoint(mesh.getVertex(1).x, mesh.getVertex(1).y)); + line.addVertex(ofPoint(mesh.getVertex(2).x, mesh.getVertex(2).y)); + line.addVertex(ofPoint(mesh.getVertex(3).x, mesh.getVertex(3).y)); + line.close(); + + return line; +} + +ofPolyline QuadSurface::getTextureHitArea() { + ofPolyline line; + vector& texCoords = mesh.getTexCoords(); + ofVec2f textureSize = ofVec2f(texture->getWidth(), texture->getHeight()); + for (int i = 0; i < texCoords.size(); i++) { + line.addVertex(ofPoint(texCoords[i] * textureSize)); + } + line.close(); + + return line; +} - }} \ No newline at end of file +vector& QuadSurface::getVertices() { + // return only joint vertices + return mesh.getVertices(); +} + +vector& QuadSurface::getTexCoords() { return mesh.getTexCoords(); } + +void QuadSurface::calculate4dTextureCoords() { + // Perspective Warping with OpenGL Fixed Pipeline and q coordinates + // see: + // http://www.reedbeta.com/blog/2012/05/26/quadrilateral-interpolation-part-1/ + // for information on the technique + // Pue OpenGL is used because the ofMesh sadly doesn't support ofVec4f as + // texture coordinates. + // calculate intersection point + ofVec3f p0 = mesh.getVertex(0); + ofVec3f p1 = mesh.getVertex(1); + ofVec3f p2 = mesh.getVertex(2); + ofVec3f p3 = mesh.getVertex(3); + + ofVec3f t0 = mesh.getTexCoord(0); + ofVec3f t1 = mesh.getTexCoord(1); + ofVec3f t2 = mesh.getTexCoord(2); + ofVec3f t3 = mesh.getTexCoord(3); + + ofPoint interSect; + ofLineSegmentIntersection(ofPoint(p0.x, p0.y), ofPoint(p2.x, p2.y), + ofPoint(p1.x, p1.y), ofPoint(p3.x, p3.y), + interSect); + ofVec3f interSecVec = ofVec3f(interSect.x, interSect.y, 0); + + // calculate distances to intersection point + float d0 = interSecVec.distance(p0); + float d1 = interSecVec.distance(p1); + float d2 = interSecVec.distance(p2); + float d3 = interSecVec.distance(p3); + + // vertices + // top left corner + quadVertices[0] = p0.x; + quadVertices[1] = p0.y; + quadVertices[2] = 0; + // top right corner + quadVertices[3] = p1.x; + quadVertices[4] = p1.y; + quadVertices[5] = 0; + // bottom right corner + quadVertices[6] = p2.x; + quadVertices[7] = p2.y; + quadVertices[8] = 0; + // bottom left corner + quadVertices[9] = p3.x; + quadVertices[10] = p3.y; + quadVertices[11] = 0; + + float q0 = (d0 + d2) / (d2); + float q1 = (d1 + d3) / (d3); + float q2 = (d2 + d0) / (d0); + float q3 = (d3 + d1) / (d1); + + quadTexCoordinates[0] = t0.x; + quadTexCoordinates[1] = t0.y; + quadTexCoordinates[3] = q0; + + quadTexCoordinates[4] = t1.x * q1; + quadTexCoordinates[5] = t1.y; + quadTexCoordinates[7] = q1; + + quadTexCoordinates[8] = t2.x * q2; + quadTexCoordinates[9] = t2.y * q2; + quadTexCoordinates[11] = q2; + + quadTexCoordinates[12] = t3.x; + quadTexCoordinates[13] = t3.y * q3; + quadTexCoordinates[15] = q3; +} +} +} \ No newline at end of file diff --git a/src/QuadSurface.h b/src/QuadSurface.h index 87469cb..223e367 100755 --- a/src/QuadSurface.h +++ b/src/QuadSurface.h @@ -4,38 +4,37 @@ #include "BaseSurface.h" #include "SurfaceType.h" -namespace ofx{ - namespace piMapper{ -class QuadSurface : public BaseSurface -{ -public: - QuadSurface(); - ~QuadSurface(); - - void setup(); +namespace ofx { +namespace piMapper { +class QuadSurface : public BaseSurface { + public: + QuadSurface(); + ~QuadSurface(); - void setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4, - ofVec2f t1, ofVec2f t2, ofVec2f t3, ofVec2f t4, - ofTexture* texturePtr ); + void setup(); - void draw(); - void setVertex( int index, ofVec2f p ); - void setTexCoord( int index, ofVec2f t ); - void moveBy(ofVec2f v); - - int getType(); - bool hitTest(ofVec2f p); - ofVec2f getVertex(int index); - ofVec2f getTexCoord(int index); - ofPolyline getHitArea(); - ofPolyline getTextureHitArea(); - vector& getVertices(); - vector& getTexCoords(); -private: - void calculate4dTextureCoords(); - GLfloat quadVertices[12]; - GLubyte quadIndices[6]; - GLfloat quadTexCoordinates[16]; -}; + void setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4, ofVec2f t1, + ofVec2f t2, ofVec2f t3, ofVec2f t4, ofTexture* texturePtr); + + void draw(); + void setVertex(int index, ofVec2f p); + void setTexCoord(int index, ofVec2f t); + void moveBy(ofVec2f v); - }} \ No newline at end of file + int getType(); + bool hitTest(ofVec2f p); + ofVec2f getVertex(int index); + ofVec2f getTexCoord(int index); + ofPolyline getHitArea(); + ofPolyline getTextureHitArea(); + vector& getVertices(); + vector& getTexCoords(); + + private: + void calculate4dTextureCoords(); + GLfloat quadVertices[12]; + GLubyte quadIndices[6]; + GLfloat quadTexCoordinates[16]; +}; +} +} \ No newline at end of file diff --git a/src/SourcesEditor.cpp b/src/SourcesEditor.cpp index 05c9a83..452ddb9 100644 --- a/src/SourcesEditor.cpp +++ b/src/SourcesEditor.cpp @@ -1,139 +1,121 @@ #include "SourcesEditor.h" -namespace ofx{ - namespace piMapper{ -SourcesEditor::SourcesEditor() -{ - defImgDir = DEFAULT_IMAGES_DIR; - registerAppEvents(); +namespace ofx { +namespace piMapper { +SourcesEditor::SourcesEditor() { + defImgDir = DEFAULT_IMAGES_DIR; + registerAppEvents(); } -SourcesEditor::~SourcesEditor() -{ - unregisterAppEvents(); - delete gui; - while ( images.size() ) { - delete images.back(); - images.pop_back(); - } +SourcesEditor::~SourcesEditor() { + unregisterAppEvents(); + delete gui; + while (images.size()) { + delete images.back(); + images.pop_back(); + } } -void SourcesEditor::registerAppEvents() -{ - ofAddListener(ofEvents().setup, this, &SourcesEditor::setup); +void SourcesEditor::registerAppEvents() { + ofAddListener(ofEvents().setup, this, &SourcesEditor::setup); } -void SourcesEditor::unregisterAppEvents() -{ - ofRemoveListener(ofEvents().setup, this, &SourcesEditor::setup); +void SourcesEditor::unregisterAppEvents() { + ofRemoveListener(ofEvents().setup, this, &SourcesEditor::setup); } -void SourcesEditor::setup(ofEventArgs& args) -{ - gui = new RadioList(); - - // read directory contents - ofDirectory imgDir; - imgDir.listDir(defImgDir); - imgDir.sort(); - - vector vnames; - - for(int i = 0; i < (int)imgDir.size(); i++){ - //images[i].loadImage(imgDir.getPath(i)); - vnames.push_back(imgDir.getName(i)); - } - - gui->setup("Images", vnames); - gui->setPosition(20, 20); - ofAddListener(gui->radioSelectedEvent, this, &SourcesEditor::guiEvent); -} +void SourcesEditor::setup(ofEventArgs& args) { + gui = new RadioList(); -void SourcesEditor::draw() -{ - // Don't draw if there is no source selected - if ( surfaceManager->getSelectedSurface() == NULL ) { - return; - } - - gui->draw(); + // read directory contents + ofDirectory imgDir; + imgDir.listDir(defImgDir); + imgDir.sort(); + + vector vnames; + + for (int i = 0; i < (int)imgDir.size(); i++) { + // images[i].loadImage(imgDir.getPath(i)); + vnames.push_back(imgDir.getName(i)); + } + + gui->setup("Images", vnames); + gui->setPosition(20, 20); + ofAddListener(gui->radioSelectedEvent, this, &SourcesEditor::guiEvent); } -void SourcesEditor::loadImage( string name, string path ) -{ - images.push_back(new ofImage()); - images.back()->loadImage(path); - - imageNames.push_back(name); - - ofSendMessage("imageLoaded"); +void SourcesEditor::draw() { + // Don't draw if there is no source selected + if (surfaceManager->getSelectedSurface() == NULL) { + return; + } + + gui->draw(); } -void SourcesEditor::disable() -{ - gui->disable(); +void SourcesEditor::loadImage(string name, string path) { + images.push_back(new ofImage()); + images.back()->loadImage(path); + + imageNames.push_back(name); + + ofSendMessage("imageLoaded"); } -void SourcesEditor::enable() -{ - // Don't enable if there is no surface selected - if ( surfaceManager->getSelectedSurface() == NULL ) { - cout << "No surface selected. Not enable()ing source list." << endl; - return; - } - - gui->enable(); +void SourcesEditor::disable() { gui->disable(); } + +void SourcesEditor::enable() { + // Don't enable if there is no surface selected + if (surfaceManager->getSelectedSurface() == NULL) { + cout << "No surface selected. Not enable()ing source list." << endl; + return; + } + + gui->enable(); } -void SourcesEditor::setSurfaceManager(SurfaceManager *newSurfaceManager) -{ - surfaceManager = newSurfaceManager; +void SourcesEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) { + surfaceManager = newSurfaceManager; } -void SourcesEditor::selectImageSourceRadioButton(string name) -{ - if (name == "none") { - gui->unselectAll(); +void SourcesEditor::selectImageSourceRadioButton(string name) { + if (name == "none") { + gui->unselectAll(); + return; + } else { + int i; + for (i = 0; i < gui->size(); i++) { + if (gui->getItemName(i) == name) { + gui->selectItem(i); return; - } else { - int i; - for (i = 0; i < gui->size(); i++) { - if (gui->getItemName(i) == name) { - gui->selectItem(i); - return; - } - } + } } + } } -int SourcesEditor::getLoadedTexCount() -{ - return images.size(); -} +int SourcesEditor::getLoadedTexCount() { return images.size(); } -ofTexture* SourcesEditor::getTexture(int index) -{ - if (index >= images.size()){ - throw std::runtime_error("Texture index out of bounds."); - } - - return &images[index]->getTextureReference(); -} +ofTexture* SourcesEditor::getTexture(int index) { + if (index >= images.size()) { + throw std::runtime_error("Texture index out of bounds."); + } -void SourcesEditor::guiEvent(string &imageName) -{ - string name = imageName; - - if ( surfaceManager->getSelectedSurface() == NULL ) { - return; - } - - stringstream ss; - ss << defImgDir << name; - cout << "attempt to load image: " << ss.str() << endl; - ofTexture* texture = surfaceManager->loadImageSource(name, ss.str()); - surfaceManager->getSelectedSurface()->setTexture(texture); - surfaceManager->manageMemory(); + return &images[index]->getTextureReference(); } - }} \ No newline at end of file +void SourcesEditor::guiEvent(string& imageName) { + string name = imageName; + + if (surfaceManager->getSelectedSurface() == NULL) { + return; + } + + stringstream ss; + ss << defImgDir << name; + cout << "attempt to load image: " << ss.str() << endl; + ofTexture* texture = surfaceManager->loadImageSource(name, ss.str()); + surfaceManager->getSelectedSurface()->setTexture(texture); + surfaceManager->manageMemory(); +} +} +} \ No newline at end of file diff --git a/src/SourcesEditor.h b/src/SourcesEditor.h index 8311d1f..ee4462b 100644 --- a/src/SourcesEditor.h +++ b/src/SourcesEditor.h @@ -7,35 +7,34 @@ #define DEFAULT_IMAGES_DIR "sources/images/"; -namespace ofx{ - namespace piMapper{ -class SourcesEditor -{ -public: - SourcesEditor(); - ~SourcesEditor(); - - void registerAppEvents(); - void unregisterAppEvents(); - - void setup(ofEventArgs& args); - void draw(); - void loadImage( string name, string path ); - void disable(); - void enable(); - void setSurfaceManager(SurfaceManager* newSurfaceManager); - void selectImageSourceRadioButton(string name); - - int getLoadedTexCount(); - ofTexture* getTexture(int index); - -private: - SurfaceManager* surfaceManager; - RadioList* gui; - string defImgDir; - void guiEvent(string &imageName); - vector images; - vector imageNames; -}; +namespace ofx { +namespace piMapper { +class SourcesEditor { + public: + SourcesEditor(); + ~SourcesEditor(); + + void registerAppEvents(); + void unregisterAppEvents(); + + void setup(ofEventArgs& args); + void draw(); + void loadImage(string name, string path); + void disable(); + void enable(); + void setSurfaceManager(SurfaceManager* newSurfaceManager); + void selectImageSourceRadioButton(string name); - }} \ No newline at end of file + int getLoadedTexCount(); + ofTexture* getTexture(int index); + + private: + SurfaceManager* surfaceManager; + RadioList* gui; + string defImgDir; + void guiEvent(string& imageName); + vector images; + vector imageNames; +}; +} +} \ No newline at end of file diff --git a/src/SurfaceManager.cpp b/src/SurfaceManager.cpp index 48ac560..d29ca09 100644 --- a/src/SurfaceManager.cpp +++ b/src/SurfaceManager.cpp @@ -1,183 +1,168 @@ #include "SurfaceManager.h" -namespace ofx{ - namespace piMapper{ -SurfaceManager::SurfaceManager() -{ - +namespace ofx { +namespace piMapper { +SurfaceManager::SurfaceManager() {} + +SurfaceManager::~SurfaceManager() { clear(); } + +void SurfaceManager::draw() { + for (int i = 0; i < surfaces.size(); i++) { + surfaces[i]->draw(); + } } -SurfaceManager::~SurfaceManager() -{ - clear(); +void SurfaceManager::addSurface(int surfaceType) { + if (surfaceType == SurfaceType::TRIANGLE_SURFACE) { + surfaces.push_back(new TriangleSurface()); + } else if (surfaceType == SurfaceType::QUAD_SURFACE) { + surfaces.push_back(new QuadSurface()); + } else { + throw std::runtime_error("Attempt to add non-existing surface type."); + } } -void SurfaceManager::draw() -{ - for ( int i=0; idraw(); - } +void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr) { + if (surfaceType == SurfaceType::TRIANGLE_SURFACE) { + surfaces.push_back(new TriangleSurface()); + surfaces.back()->setTexture(texturePtr); + } else if (surfaceType == SurfaceType::QUAD_SURFACE) { + surfaces.push_back(new QuadSurface()); + surfaces.back()->setTexture(texturePtr); + } else { + throw std::runtime_error("Attempt to add non-existing surface type."); + } } -void SurfaceManager::addSurface(int surfaceType) -{ - if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) { - surfaces.push_back( new TriangleSurface() ); - } - else if (surfaceType == SurfaceType::QUAD_SURFACE ) { - surfaces.push_back( new QuadSurface() ); +void SurfaceManager::addSurface(int surfaceType, vector vertices, + vector texCoords) { + if (surfaceType == SurfaceType::TRIANGLE_SURFACE) { + if (vertices.size() < 3) { + throw std::runtime_error( + "There must be 3 vertices for a triangle surface."); + } else if (texCoords.size() < 3) { + throw std::runtime_error( + "There must be 3 texture coordinates for a triangle surface."); } - else { - throw std::runtime_error("Attempt to add non-existing surface type."); + + surfaces.push_back(new TriangleSurface()); + + for (int i = 0; i < 3; i++) { + surfaces.back()->setVertex(i, vertices[i]); + surfaces.back()->setTexCoord(i, texCoords[i]); } -} -void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr) -{ - if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) { - surfaces.push_back( new TriangleSurface() ); - surfaces.back()->setTexture(texturePtr); - } - else if (surfaceType == SurfaceType::QUAD_SURFACE ) { - surfaces.push_back( new QuadSurface() ); - surfaces.back()->setTexture(texturePtr); + } else if (surfaceType == SurfaceType::QUAD_SURFACE) { + if (vertices.size() < 4) { + throw std::runtime_error("There must be 4 vertices for a quad surface."); + } else if (texCoords.size() < 4) { + throw std::runtime_error( + "There must be 4 texture coordinates for a quad surface."); } - else { - throw std::runtime_error("Attempt to add non-existing surface type."); + + surfaces.push_back(new QuadSurface()); + + for (int i = 0; i < 4; i++) { + surfaces.back()->setVertex(i, vertices[i]); + surfaces.back()->setTexCoord(i, texCoords[i]); } + } else { + throw std::runtime_error("Attempt to add non-existing surface type."); + } } -void SurfaceManager::addSurface(int surfaceType, vector vertices, vector texCoords) -{ - if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) { - - if ( vertices.size() < 3 ) { - throw std::runtime_error("There must be 3 vertices for a triangle surface."); - } else if (texCoords.size() < 3) { - throw std::runtime_error("There must be 3 texture coordinates for a triangle surface."); - } - - surfaces.push_back( new TriangleSurface() ); - - for ( int i=0; i<3; i++ ) { - surfaces.back()->setVertex(i, vertices[i]); - surfaces.back()->setTexCoord(i, texCoords[i]); - } - - } - else if (surfaceType == SurfaceType::QUAD_SURFACE ) { - if ( vertices.size() < 4 ) { - throw std::runtime_error("There must be 4 vertices for a quad surface."); - } else if (texCoords.size() < 4) { - throw std::runtime_error("There must be 4 texture coordinates for a quad surface."); - } - - surfaces.push_back( new QuadSurface() ); - - for ( int i=0; i<4; i++ ) { - surfaces.back()->setVertex(i, vertices[i]); - surfaces.back()->setTexCoord(i, texCoords[i]); - } - } - else { - throw std::runtime_error("Attempt to add non-existing surface type."); +void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, + vector vertices, + vector texCoords) { + if (surfaceType == SurfaceType::TRIANGLE_SURFACE) { + if (vertices.size() < 3) { + throw std::runtime_error( + "There must be 3 vertices for a triangle surface."); + } else if (texCoords.size() < 3) { + throw std::runtime_error( + "Thre must be 3 texture coordinates for a triangle surface."); } -} + surfaces.push_back(new TriangleSurface()); + surfaces.back()->setTexture(texturePtr); -void SurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vector vertices, vector texCoords) -{ - if ( surfaceType == SurfaceType::TRIANGLE_SURFACE ) { - - if ( vertices.size() < 3 ) { - throw std::runtime_error("There must be 3 vertices for a triangle surface."); - } else if (texCoords.size() < 3) { - throw std::runtime_error("Thre must be 3 texture coordinates for a triangle surface."); - } - - surfaces.push_back( new TriangleSurface() ); - surfaces.back()->setTexture(texturePtr); - - for ( int i=0; i<3; i++ ) { - surfaces.back()->setVertex(i, vertices[i]); - surfaces.back()->setTexCoord(i, texCoords[i]); - } - - } - else if (surfaceType == SurfaceType::QUAD_SURFACE ) { - if ( vertices.size() < 4 ) { - throw std::runtime_error("There must be 4 vertices for a quad surface."); - } else if (texCoords.size() < 4) { - throw std::runtime_error("Thre must be 4 texture coordinates for a quad surface."); - } - - surfaces.push_back( new QuadSurface() ); - surfaces.back()->setTexture(texturePtr); - - for ( int i=0; i<4; i++ ) { - surfaces.back()->setVertex(i, vertices[i]); - surfaces.back()->setTexCoord(i, texCoords[i]); - } + for (int i = 0; i < 3; i++) { + surfaces.back()->setVertex(i, vertices[i]); + surfaces.back()->setTexCoord(i, texCoords[i]); } - else { - throw std::runtime_error("Attempt to add non-existing surface type."); + + } else if (surfaceType == SurfaceType::QUAD_SURFACE) { + if (vertices.size() < 4) { + throw std::runtime_error("There must be 4 vertices for a quad surface."); + } else if (texCoords.size() < 4) { + throw std::runtime_error( + "Thre must be 4 texture coordinates for a quad surface."); } -} -void SurfaceManager::removeSelectedSurface() -{ - if ( selectedSurface == NULL ) return; - - for ( int i=0; isetTexture(texturePtr); + + for (int i = 0; i < 4; i++) { + surfaces.back()->setVertex(i, vertices[i]); + surfaces.back()->setTexCoord(i, texCoords[i]); } + } else { + throw std::runtime_error("Attempt to add non-existing surface type."); + } } -void SurfaceManager::manageMemory() -{ - // check if each of the sources is assigned to a surface or not - for ( int i=0; igetTexture() == &loadedImageSources[i]->getTextureReference() ) { - bAssigned = true; - break; - } - } - - if ( !bAssigned ) { - // purge the image source from memory - delete loadedImageSources[i]; - loadedImageSources.erase(loadedImageSources.begin()+i); - cout << "Deleting image source: " << loadedImageSourceNames[i] << endl; - loadedImageSourceNames.erase(loadedImageSourceNames.begin()+i); - i--; - } +void SurfaceManager::removeSelectedSurface() { + if (selectedSurface == NULL) return; + + for (int i = 0; i < surfaces.size(); i++) { + if (surfaces[i] == selectedSurface) { + delete surfaces[i]; + surfaces.erase(surfaces.begin() + i); + selectedSurface = NULL; + break; } + } } -void SurfaceManager::clear() -{ - // delete all extra allocations from the heap - while ( surfaces.size() ) { - delete surfaces.back(); - surfaces.pop_back(); - } - - while ( loadedImageSources.size() ) { - delete loadedImageSources.back(); - loadedImageSources.pop_back(); +void SurfaceManager::manageMemory() { + // check if each of the sources is assigned to a surface or not + for (int i = 0; i < loadedImageSources.size(); i++) { + bool bAssigned = false; + + for (int j = 0; j < surfaces.size(); j++) { + if (surfaces[j]->getTexture() == + &loadedImageSources[i]->getTextureReference()) { + bAssigned = true; + break; + } } - - while ( loadedImageSourceNames.size() ) { - loadedImageSourceNames.pop_back(); + + if (!bAssigned) { + // purge the image source from memory + delete loadedImageSources[i]; + loadedImageSources.erase(loadedImageSources.begin() + i); + cout << "Deleting image source: " << loadedImageSourceNames[i] << endl; + loadedImageSourceNames.erase(loadedImageSourceNames.begin() + i); + i--; } + } +} + +void SurfaceManager::clear() { + // delete all extra allocations from the heap + while (surfaces.size()) { + delete surfaces.back(); + surfaces.pop_back(); + } + + while (loadedImageSources.size()) { + delete loadedImageSources.back(); + loadedImageSources.pop_back(); + } + + while (loadedImageSourceNames.size()) { + loadedImageSourceNames.pop_back(); + } } // String getTypeString(SurfaceType e) @@ -190,299 +175,288 @@ void SurfaceManager::clear() // } // } -void SurfaceManager::saveXmlSettings(string fileName) -{ - xmlSettings.clear(); - - // save surfaces - xmlSettings.addTag("surfaces"); - xmlSettings.pushTag("surfaces"); - for ( int i=0; i* vertices = &surface->getVertices(); - for ( int j=0; jsize(); j++ ) { - xmlSettings.addTag("vertex"); - xmlSettings.pushTag("vertex", j); - ofVec3f* vertex = &(*vertices)[j]; - xmlSettings.addValue("x", vertex->x); - xmlSettings.addValue("y", vertex->y); - - // we don't need z as it will be 0 anyways - - xmlSettings.popTag(); // vertex - } - xmlSettings.popTag(); // vertices - - xmlSettings.addTag("texCoords"); - xmlSettings.pushTag("texCoords"); - vector* texCoords = &surface->getTexCoords(); - for ( int j=0; jsize(); j++ ) { - xmlSettings.addTag("texCoord"); - xmlSettings.pushTag("texCoord", j); - ofVec2f* texCoord = &(*texCoords)[j]; - xmlSettings.addValue("x", texCoord->x); - xmlSettings.addValue("y", texCoord->y); - xmlSettings.popTag(); // texCoord - } - xmlSettings.popTag(); // texCoords - - xmlSettings.addTag("source"); - xmlSettings.pushTag("source"); - - xmlSettings.addValue("source-type", "image"); - xmlSettings.addValue("source-name", getSurfaceSourceName(surface)); - //xmlSettings.addValue("source-path", "/root/etc/image.jpg"); - xmlSettings.popTag(); // source - - - // xmlSettings.addTag("type"); - // xmlSettings.pushTag("type"); - // // surfaceType == SurfaceType::TRIANGLE_SURFACE - // SurfaceType surfaceType = &surface->getType(); - // xmlSettings.addValue("surface-type", surfaceType); - // xmlSettings.popTag(); // type - - xmlSettings.popTag(); // surface +void SurfaceManager::saveXmlSettings(string fileName) { + xmlSettings.clear(); + + // save surfaces + xmlSettings.addTag("surfaces"); + xmlSettings.pushTag("surfaces"); + for (int i = 0; i < surfaces.size(); i++) { + xmlSettings.addTag("surface"); + xmlSettings.pushTag("surface", i); + BaseSurface* surface = surfaces[i]; + + xmlSettings.addTag("vertices"); + xmlSettings.pushTag("vertices"); + vector* vertices = &surface->getVertices(); + for (int j = 0; j < vertices->size(); j++) { + xmlSettings.addTag("vertex"); + xmlSettings.pushTag("vertex", j); + ofVec3f* vertex = &(*vertices)[j]; + xmlSettings.addValue("x", vertex->x); + xmlSettings.addValue("y", vertex->y); + + // we don't need z as it will be 0 anyways + + xmlSettings.popTag(); // vertex } - xmlSettings.popTag(); // surfaces - - xmlSettings.save(fileName); -} + xmlSettings.popTag(); // vertices + + xmlSettings.addTag("texCoords"); + xmlSettings.pushTag("texCoords"); + vector* texCoords = &surface->getTexCoords(); + for (int j = 0; j < texCoords->size(); j++) { + xmlSettings.addTag("texCoord"); + xmlSettings.pushTag("texCoord", j); + ofVec2f* texCoord = &(*texCoords)[j]; + xmlSettings.addValue("x", texCoord->x); + xmlSettings.addValue("y", texCoord->y); + xmlSettings.popTag(); // texCoord + } + xmlSettings.popTag(); // texCoords -void SurfaceManager::loadXmlSettings(string fileName) -{ + xmlSettings.addTag("source"); + xmlSettings.pushTag("source"); + xmlSettings.addValue("source-type", "image"); + xmlSettings.addValue("source-name", getSurfaceSourceName(surface)); + // xmlSettings.addValue("source-path", "/root/etc/image.jpg"); + xmlSettings.popTag(); // source - if (!xmlSettings.loadFile(fileName)){ - ofLog(OF_LOG_WARNING, "Could not load XML settings."); - return; + // xmlSettings.addTag("type"); + // xmlSettings.pushTag("type"); + // // surfaceType == SurfaceType::TRIANGLE_SURFACE + // SurfaceType surfaceType = &surface->getType(); + // xmlSettings.addValue("surface-type", surfaceType); + // xmlSettings.popTag(); // type + + xmlSettings.popTag(); // surface + } + xmlSettings.popTag(); // surfaces + + xmlSettings.save(fileName); +} + +void SurfaceManager::loadXmlSettings(string fileName) { + if (!xmlSettings.loadFile(fileName)) { + ofLog(OF_LOG_WARNING, "Could not load XML settings."); + return; + } + + if (!xmlSettings.tagExists("surfaces")) { + ofLog(OF_LOG_WARNING, "XML settings is empty or has wrong markup."); + return; + } + + xmlSettings.pushTag("surfaces"); + + int numSurfaces = xmlSettings.getNumTags("surface"); + for (int i = 0; i < numSurfaces; i++) { + xmlSettings.pushTag("surface", i); + // attempt to load surface source + xmlSettings.pushTag("source"); + string sourceType = xmlSettings.getValue("source-type", "image"); + string sourceName = xmlSettings.getValue("source-name", "none"); + ofTexture* sourceTexture = NULL; + if (sourceName != "none") { + stringstream ss; + ss << "sources/images/" << sourceName; // TODO: reuse constants here + sourceTexture = loadImageSource(sourceName, ss.str()); } - - if (!xmlSettings.tagExists("surfaces")){ - ofLog(OF_LOG_WARNING, "XML settings is empty or has wrong markup."); - return; + xmlSettings.popTag(); // source + + // // attempt to load surface type + // ofLog(OF_LOG_WARNING, "Attempt to load surface type."); + // xmlSettings.pushTag("type"); + // string surfaceType = xmlSettings.getValue("surface-type", + // "TRIANGLE_SURFACE"); + // xmlSettings.popTag(); // type + + xmlSettings.pushTag("vertices"); + vector vertices; + + int vertexCount = xmlSettings.getNumTags("vertex"); + + // it's a triangle ? + if (vertexCount == 3) { + ofLog(OF_LOG_NOTICE, "create Triangle"); + xmlSettings.pushTag("vertex", 0); + vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), + xmlSettings.getValue("y", 0.0f))); + xmlSettings.popTag(); + + xmlSettings.pushTag("vertex", 1); + vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f), + xmlSettings.getValue("y", 0.0f))); + xmlSettings.popTag(); + + xmlSettings.pushTag("vertex", 2); + vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), + xmlSettings.getValue("y", 100.0f))); + xmlSettings.popTag(); + + xmlSettings.popTag(); // vertices + + xmlSettings.pushTag("texCoords"); + + vector texCoords; + + xmlSettings.pushTag("texCoord", 0); + texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), + xmlSettings.getValue("y", 0.0f))); + xmlSettings.popTag(); + + xmlSettings.pushTag("texCoord", 1); + texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f), + xmlSettings.getValue("y", 0.0f))); + xmlSettings.popTag(); + + xmlSettings.pushTag("texCoord", 2); + texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), + xmlSettings.getValue("y", 1.0f))); + xmlSettings.popTag(); + + xmlSettings.popTag(); // texCoords + + // now we have variables sourceName and sourceTexture + // by checking those we can use one or another addSurface method + if (sourceName != "none" && sourceTexture != NULL) { + addSurface(SurfaceType::TRIANGLE_SURFACE, sourceTexture, vertices, + texCoords); + } else { + addSurface(SurfaceType::TRIANGLE_SURFACE, vertices, texCoords); + } } - - xmlSettings.pushTag("surfaces"); - - int numSurfaces = xmlSettings.getNumTags("surface"); - for ( int i=0; i vertices; - - int vertexCount = xmlSettings.getNumTags("vertex"); - - - //it's a triangle ? - if (vertexCount == 3) - { - ofLog(OF_LOG_NOTICE, "create Triangle"); - xmlSettings.pushTag("vertex", 0); - vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.pushTag("vertex", 1); - vertices.push_back( ofVec2f( xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 0.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.pushTag("vertex", 2); - vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 100.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.popTag(); // vertices - - xmlSettings.pushTag("texCoords"); - - vector texCoords; - - xmlSettings.pushTag("texCoord", 0); - texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.pushTag("texCoord", 1); - texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 0.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.pushTag("texCoord", 2); - texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 1.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.popTag(); // texCoords - - - // now we have variables sourceName and sourceTexture - // by checking those we can use one or another addSurface method - if ( sourceName != "none" && sourceTexture != NULL ) { - addSurface(SurfaceType::TRIANGLE_SURFACE, sourceTexture, vertices, texCoords); - } else { - addSurface(SurfaceType::TRIANGLE_SURFACE, vertices, texCoords); - } - } - // it's a quad ? - else if (vertexCount == 4) - // if (surface-type == QUAD_SURFACE) - { - xmlSettings.pushTag("vertex", 0); - vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.pushTag("vertex", 1); - vertices.push_back( ofVec2f( xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 0.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.pushTag("vertex", 2); - vertices.push_back( ofVec2f( xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 100.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.pushTag("vertex", 3); - vertices.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 100.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.popTag(); // vertices - - xmlSettings.pushTag("texCoords"); - - vector texCoords; - - xmlSettings.pushTag("texCoord", 0); - texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.pushTag("texCoord", 1); - texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 0.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.pushTag("texCoord", 2); - texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 1.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.pushTag("texCoord", 3); - texCoords.push_back( ofVec2f( xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 1.0f) ) ); - xmlSettings.popTag(); - - xmlSettings.popTag(); // texCoords - - - // now we have variables sourceName and sourceTexture - // by checking those we can use one or another addSurface method - if ( sourceName != "none" && sourceTexture != NULL ) { - addSurface(SurfaceType::QUAD_SURFACE, sourceTexture, vertices, texCoords); - } else { - addSurface(SurfaceType::QUAD_SURFACE, vertices, texCoords); - } - - - } - - xmlSettings.popTag(); // surface + // it's a quad ? + else if (vertexCount == 4) + // if (surface-type == QUAD_SURFACE) + { + xmlSettings.pushTag("vertex", 0); + vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), + xmlSettings.getValue("y", 0.0f))); + xmlSettings.popTag(); + + xmlSettings.pushTag("vertex", 1); + vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f), + xmlSettings.getValue("y", 0.0f))); + xmlSettings.popTag(); + + xmlSettings.pushTag("vertex", 2); + vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f), + xmlSettings.getValue("y", 100.0f))); + xmlSettings.popTag(); + + xmlSettings.pushTag("vertex", 3); + vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), + xmlSettings.getValue("y", 100.0f))); + xmlSettings.popTag(); + + xmlSettings.popTag(); // vertices + + xmlSettings.pushTag("texCoords"); + + vector texCoords; + + xmlSettings.pushTag("texCoord", 0); + texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), + xmlSettings.getValue("y", 0.0f))); + xmlSettings.popTag(); + + xmlSettings.pushTag("texCoord", 1); + texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f), + xmlSettings.getValue("y", 0.0f))); + xmlSettings.popTag(); + + xmlSettings.pushTag("texCoord", 2); + texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f), + xmlSettings.getValue("y", 1.0f))); + xmlSettings.popTag(); + + xmlSettings.pushTag("texCoord", 3); + texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), + xmlSettings.getValue("y", 1.0f))); + xmlSettings.popTag(); + + xmlSettings.popTag(); // texCoords + + // now we have variables sourceName and sourceTexture + // by checking those we can use one or another addSurface method + if (sourceName != "none" && sourceTexture != NULL) { + addSurface(SurfaceType::QUAD_SURFACE, sourceTexture, vertices, + texCoords); + } else { + addSurface(SurfaceType::QUAD_SURFACE, vertices, texCoords); + } } - - xmlSettings.popTag(); // surfaces -} -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"); -} + xmlSettings.popTag(); // surface + } -BaseSurface* SurfaceManager::getSelectedSurface() -{ - return selectedSurface; + xmlSettings.popTag(); // surfaces } -void SurfaceManager::deselectSurface() -{ - selectedSurface = NULL; -} +BaseSurface* SurfaceManager::selectSurface(int index) { + if (index >= surfaces.size()) { + throw std::runtime_error("Surface index out of bounds."); + } -ofTexture* SurfaceManager::loadImageSource(string name, string path) -{ - // check if it is loaded - for ( int i=0; igetTextureReference(); - } - } - - // not loaded - load - ofImage* image = new ofImage(); - if ( !image->loadImage(path) ){ - return NULL; - } - loadedImageSources.push_back(image); - loadedImageSourceNames.push_back(name); - return &image->getTextureReference(); + selectedSurface = surfaces[index]; + + // notify that a new surface has been selected + ofSendMessage("surfaceSelected"); } -string SurfaceManager::getSelectedSurfaceSourceName() -{ - if ( selectedSurface == NULL ) { - return "none"; +BaseSurface* SurfaceManager::getSelectedSurface() { return selectedSurface; } + +void SurfaceManager::deselectSurface() { selectedSurface = NULL; } + +ofTexture* SurfaceManager::loadImageSource(string name, string path) { + // check if it is loaded + for (int i = 0; i < loadedImageSourceNames.size(); i++) { + if (loadedImageSourceNames[i] == name) { + // this image is already loaded + return &loadedImageSources[i]->getTextureReference(); } - - return getSurfaceSourceName( selectedSurface ); + } + + // not loaded - load + ofImage* image = new ofImage(); + if (!image->loadImage(path)) { + return NULL; + } + loadedImageSources.push_back(image); + loadedImageSourceNames.push_back(name); + return &image->getTextureReference(); } -string SurfaceManager::getSurfaceSourceName(BaseSurface *surface) -{ - ofTexture* tex = surface->getTexture(); - for ( int i=0; igetTextureReference()) { - return loadedImageSourceNames[i]; - } - } - +string SurfaceManager::getSelectedSurfaceSourceName() { + if (selectedSurface == NULL) { return "none"; + } + + return getSurfaceSourceName(selectedSurface); } -BaseSurface* SurfaceManager::getSurface(int index) -{ - if ( index >= surfaces.size() ) { - throw std::runtime_error("Surface index out of bounds."); - return NULL; +string SurfaceManager::getSurfaceSourceName(BaseSurface* surface) { + ofTexture* tex = surface->getTexture(); + for (int i = 0; i < loadedImageSources.size(); i++) { + if (tex == &loadedImageSources[i]->getTextureReference()) { + return loadedImageSourceNames[i]; } - - return surfaces[index]; -} + } -int SurfaceManager::size() -{ - return surfaces.size(); + return "none"; } - }} +BaseSurface* SurfaceManager::getSurface(int index) { + if (index >= surfaces.size()) { + throw std::runtime_error("Surface index out of bounds."); + return NULL; + } + + return surfaces[index]; +} +int SurfaceManager::size() { return surfaces.size(); } +} +} diff --git a/src/SurfaceManager.h b/src/SurfaceManager.h index 48df5c5..38af95f 100755 --- a/src/SurfaceManager.h +++ b/src/SurfaceManager.h @@ -10,42 +10,41 @@ using namespace std; +namespace ofx { +namespace piMapper { +class SurfaceManager { + public: + SurfaceManager(); + ~SurfaceManager(); -namespace ofx{ - namespace piMapper{ -class SurfaceManager -{ -public: - SurfaceManager(); - ~SurfaceManager(); - - void draw(); - void addSurface(int surfaceType); - void addSurface(int surfaceType, ofTexture* texturePtr); - void addSurface(int surfaceType, vector vertices, vector texCoords); - void addSurface(int surfaceType, ofTexture* texturePtr, vector vertices, vector texCoords); - void removeSelectedSurface(); - void manageMemory(); // deletes unasigned sources - void clear(); - void saveXmlSettings(string fileName); - void loadXmlSettings(string fileName); - - BaseSurface* getSurface(int index); - int size(); - BaseSurface* selectSurface(int index); - BaseSurface* getSelectedSurface(); - void deselectSurface(); - ofTexture* loadImageSource(string name, string path); - string getSelectedSurfaceSourceName(); - string getSurfaceSourceName( BaseSurface* surface ); - -private: - vector surfaces; - BaseSurface* selectedSurface; - vector loadedImageSourceNames; - vector loadedImageSources; - ofxXmlSettings xmlSettings; - -}; + void draw(); + void addSurface(int surfaceType); + void addSurface(int surfaceType, ofTexture* texturePtr); + void addSurface(int surfaceType, vector vertices, + vector texCoords); + void addSurface(int surfaceType, ofTexture* texturePtr, + vector vertices, vector texCoords); + void removeSelectedSurface(); + void manageMemory(); // deletes unasigned sources + void clear(); + void saveXmlSettings(string fileName); + void loadXmlSettings(string fileName); + + BaseSurface* getSurface(int index); + int size(); + BaseSurface* selectSurface(int index); + BaseSurface* getSelectedSurface(); + void deselectSurface(); + ofTexture* loadImageSource(string name, string path); + string getSelectedSurfaceSourceName(); + string getSurfaceSourceName(BaseSurface* surface); - }} \ No newline at end of file + private: + vector surfaces; + BaseSurface* selectedSurface; + vector loadedImageSourceNames; + vector loadedImageSources; + ofxXmlSettings xmlSettings; +}; +} +} \ No newline at end of file diff --git a/src/SurfaceManagerGui.cpp b/src/SurfaceManagerGui.cpp index e932810..ce73e99 100644 --- a/src/SurfaceManagerGui.cpp +++ b/src/SurfaceManagerGui.cpp @@ -1,258 +1,243 @@ #include "SurfaceManagerGui.h" -namespace ofx{ - namespace piMapper{ -SurfaceManagerGui::SurfaceManagerGui() -{ - surfaceManager = NULL; - guiMode = GuiMode::NONE; - bDrag = false; - registerMouseEvents(); - ofHideCursor(); +namespace ofx { +namespace piMapper { +SurfaceManagerGui::SurfaceManagerGui() { + surfaceManager = NULL; + guiMode = GuiMode::NONE; + bDrag = false; + registerMouseEvents(); + ofHideCursor(); } -SurfaceManagerGui::~SurfaceManagerGui() -{ - unregisterMouseEvents(); - surfaceManager = NULL; +SurfaceManagerGui::~SurfaceManagerGui() { + unregisterMouseEvents(); + surfaceManager = NULL; } -void SurfaceManagerGui::registerMouseEvents() -{ - ofAddListener(ofEvents().mousePressed, this, &SurfaceManagerGui::mousePressed); - ofAddListener(ofEvents().mouseReleased, this, &SurfaceManagerGui::mouseReleased); - ofAddListener(ofEvents().mouseDragged, this, &SurfaceManagerGui::mouseDragged); +void SurfaceManagerGui::registerMouseEvents() { + ofAddListener(ofEvents().mousePressed, this, + &SurfaceManagerGui::mousePressed); + ofAddListener(ofEvents().mouseReleased, this, + &SurfaceManagerGui::mouseReleased); + ofAddListener(ofEvents().mouseDragged, this, + &SurfaceManagerGui::mouseDragged); } -void SurfaceManagerGui::unregisterMouseEvents() -{ - ofRemoveListener(ofEvents().mousePressed, this, &SurfaceManagerGui::mousePressed); - ofRemoveListener(ofEvents().mouseReleased, this, &SurfaceManagerGui::mouseReleased); - ofRemoveListener(ofEvents().mouseDragged, this, &SurfaceManagerGui::mouseDragged); +void SurfaceManagerGui::unregisterMouseEvents() { + ofRemoveListener(ofEvents().mousePressed, this, + &SurfaceManagerGui::mousePressed); + ofRemoveListener(ofEvents().mouseReleased, this, + &SurfaceManagerGui::mouseReleased); + ofRemoveListener(ofEvents().mouseDragged, this, + &SurfaceManagerGui::mouseDragged); } -void SurfaceManagerGui::draw() -{ - if ( surfaceManager == NULL ) return; - - if ( guiMode == GuiMode::NONE ) { - surfaceManager->draw(); - } else if ( guiMode == GuiMode::TEXTURE_MAPPING ) { - - // draw the texture of the selected surface - if ( surfaceManager->getSelectedSurface() != NULL ) { - surfaceManager->getSelectedSurface()->drawTexture( ofVec2f(0,0) ); - } - - // draw surfaces with opacity - ofPushStyle(); - ofSetColor(255, 255, 255, 200); - surfaceManager->draw(); - ofPopStyle(); - - // highlight selected surface - drawSelectedSurfaceHighlight(); - - // hilight selected surface texture - drawSelectedSurfaceTextureHighlight(); - - // draw texture editing GUI on top - textureEditor.draw(); - - } else if ( guiMode == GuiMode::PROJECTION_MAPPING ) { - - // draw projection surfaces first - surfaceManager->draw(); - - // highlight selected surface - drawSelectedSurfaceHighlight(); - - // draw projection mapping editing gui - projectionEditor.draw(); - - } else if ( guiMode == GuiMode::SOURCE_SELECTION ) { - // draw projection surfaces first - surfaceManager->draw(); - - // highlight selected surface - drawSelectedSurfaceHighlight(); - - sourcesEditor.draw(); - } -} +void SurfaceManagerGui::draw() { + if (surfaceManager == NULL) return; -void SurfaceManagerGui::mousePressed(ofMouseEventArgs &args) -{ - if ( guiMode == GuiMode::NONE ) { - return; - } else if ( guiMode == GuiMode::TEXTURE_MAPPING ) { - - bool bSurfaceSelected = false; - - CircleJoint* hitJoint = textureEditor.hitTestJoints(ofVec2f(args.x, args.y)); - if ( hitJoint != NULL ) { - textureEditor.unselectAllJoints(); - hitJoint->select(); - hitJoint->startDrag(); - bSurfaceSelected = true; - } else { - textureEditor.unselectAllJoints(); - } - - if ( surfaceManager->getSelectedSurface() != NULL && !bSurfaceSelected ) { - // hittest texture area to see if we are hitting the texture surface - if ( surfaceManager->getSelectedSurface()->getTextureHitArea().inside(args.x, args.y) ) { - clickPosition = ofVec2f(args.x, args.y); - startDrag(); - } - } - - } else if ( guiMode == GuiMode::PROJECTION_MAPPING ) { - - bool bSurfaceSelected = false; - - CircleJoint* hitJoint = projectionEditor.hitTestJoints(ofVec2f(args.x, args.y)); - if ( hitJoint != NULL ) { - projectionEditor.unselectAllJoints(); - hitJoint->select(); - hitJoint->startDrag(); - 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; - } - } - } - - if ( bSurfaceSelected && hitJoint == NULL ) { - // if not hitting the joints, start drag only if we have a selected surface - clickPosition = ofVec2f(args.x, args.y); - startDrag(); - } - - if ( !bSurfaceSelected ) { - // unselect if no surface selected - projectionEditor.clearJoints(); - surfaceManager->deselectSurface(); - } - } else if ( guiMode == GuiMode::SOURCE_SELECTION ) { - + if (guiMode == GuiMode::NONE) { + surfaceManager->draw(); + } else if (guiMode == GuiMode::TEXTURE_MAPPING) { + // draw the texture of the selected surface + if (surfaceManager->getSelectedSurface() != NULL) { + surfaceManager->getSelectedSurface()->drawTexture(ofVec2f(0, 0)); } -} -void SurfaceManagerGui::mouseReleased(ofMouseEventArgs &args) -{ - stopDrag(); - projectionEditor.stopDragJoints(); - textureEditor.stopDragJoints(); + // draw surfaces with opacity + ofPushStyle(); + ofSetColor(255, 255, 255, 200); + surfaceManager->draw(); + ofPopStyle(); + + // highlight selected surface + drawSelectedSurfaceHighlight(); + + // hilight selected surface texture + drawSelectedSurfaceTextureHighlight(); + + // draw texture editing GUI on top + textureEditor.draw(); + + } else if (guiMode == GuiMode::PROJECTION_MAPPING) { + // draw projection surfaces first + surfaceManager->draw(); + + // highlight selected surface + drawSelectedSurfaceHighlight(); + + // draw projection mapping editing gui + projectionEditor.draw(); + + } else if (guiMode == GuiMode::SOURCE_SELECTION) { + // draw projection surfaces first + surfaceManager->draw(); + + // highlight selected surface + drawSelectedSurfaceHighlight(); + + sourcesEditor.draw(); + } } -void SurfaceManagerGui::mouseDragged(ofMouseEventArgs &args) -{ - if (bDrag) { - ofVec2f mousePosition = ofVec2f(args.x, args.y); - ofVec2f distance = mousePosition - clickPosition; - - if ( guiMode == GuiMode::PROJECTION_MAPPING ) { - // add this distance to all vertices in surface - projectionEditor.moveSelectedSurface(distance); - } else if ( guiMode == GuiMode::TEXTURE_MAPPING ) { - textureEditor.moveTexCoords(distance); - } - clickPosition = mousePosition; +void SurfaceManagerGui::mousePressed(ofMouseEventArgs& args) { + if (guiMode == GuiMode::NONE) { + return; + } else if (guiMode == GuiMode::TEXTURE_MAPPING) { + bool bSurfaceSelected = false; + + CircleJoint* hitJoint = + textureEditor.hitTestJoints(ofVec2f(args.x, args.y)); + if (hitJoint != NULL) { + textureEditor.unselectAllJoints(); + hitJoint->select(); + hitJoint->startDrag(); + bSurfaceSelected = true; + } else { + textureEditor.unselectAllJoints(); } -} -void SurfaceManagerGui::setSurfaceManager(SurfaceManager* newSurfaceManager) -{ - surfaceManager = newSurfaceManager; - projectionEditor.setSurfaceManager( surfaceManager ); - sourcesEditor.setSurfaceManager( surfaceManager ); -} + if (surfaceManager->getSelectedSurface() != NULL && !bSurfaceSelected) { + // hittest texture area to see if we are hitting the texture surface + if (surfaceManager->getSelectedSurface()->getTextureHitArea().inside( + args.x, args.y)) { + clickPosition = ofVec2f(args.x, args.y); + startDrag(); + } + } + + } else if (guiMode == GuiMode::PROJECTION_MAPPING) { + bool bSurfaceSelected = false; -void SurfaceManagerGui::setMode(int newGuiMode) -{ - if (newGuiMode != GuiMode::NONE && - newGuiMode != GuiMode::TEXTURE_MAPPING && - newGuiMode != GuiMode::PROJECTION_MAPPING && - newGuiMode != GuiMode::SOURCE_SELECTION) { - throw std::runtime_error("Trying to set invalid mode."); + CircleJoint* hitJoint = + projectionEditor.hitTestJoints(ofVec2f(args.x, args.y)); + if (hitJoint != NULL) { + projectionEditor.unselectAllJoints(); + hitJoint->select(); + hitJoint->startDrag(); + bSurfaceSelected = true; } - - if ( newGuiMode == GuiMode::NONE ) { - ofHideCursor(); - } else { - ofShowCursor(); + + // 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; + } + } } - - guiMode = newGuiMode; - - if ( guiMode == GuiMode::SOURCE_SELECTION ) { - sourcesEditor.enable(); - string sourceName = surfaceManager->getSelectedSurfaceSourceName(); - sourcesEditor.selectImageSourceRadioButton(sourceName); - } else { - sourcesEditor.disable(); + + if (bSurfaceSelected && hitJoint == NULL) { + // if not hitting the joints, start drag only if we have a selected + // surface + clickPosition = ofVec2f(args.x, args.y); + startDrag(); } - - if ( guiMode == GuiMode::TEXTURE_MAPPING ) { - textureEditor.enable(); - // refresh texture editor surface reference - textureEditor.setSurface(surfaceManager->getSelectedSurface()); - } else { - textureEditor.disable(); + + if (!bSurfaceSelected) { + // unselect if no surface selected + projectionEditor.clearJoints(); + surfaceManager->deselectSurface(); } - + } else if (guiMode == GuiMode::SOURCE_SELECTION) { + } +} + +void SurfaceManagerGui::mouseReleased(ofMouseEventArgs& args) { + stopDrag(); + projectionEditor.stopDragJoints(); + textureEditor.stopDragJoints(); +} + +void SurfaceManagerGui::mouseDragged(ofMouseEventArgs& args) { + if (bDrag) { + ofVec2f mousePosition = ofVec2f(args.x, args.y); + ofVec2f distance = mousePosition - clickPosition; + if (guiMode == GuiMode::PROJECTION_MAPPING) { - projectionEditor.enable(); - } else { - projectionEditor.disable(); + // add this distance to all vertices in surface + projectionEditor.moveSelectedSurface(distance); + } else if (guiMode == GuiMode::TEXTURE_MAPPING) { + textureEditor.moveTexCoords(distance); } + clickPosition = mousePosition; + } } -void SurfaceManagerGui::drawSelectedSurfaceHighlight() -{ - if ( surfaceManager->getSelectedSurface() == NULL ) return; - - ofPolyline line = surfaceManager->getSelectedSurface()->getHitArea(); - - ofPushStyle(); - ofSetLineWidth(1); - ofSetColor(255, 255, 255, 255); - line.draw(); - ofPopStyle(); +void SurfaceManagerGui::setSurfaceManager(SurfaceManager* newSurfaceManager) { + surfaceManager = newSurfaceManager; + projectionEditor.setSurfaceManager(surfaceManager); + sourcesEditor.setSurfaceManager(surfaceManager); } -void SurfaceManagerGui::drawSelectedSurfaceTextureHighlight() -{ - if ( surfaceManager->getSelectedSurface() == NULL ) return; - - ofPolyline line = surfaceManager->getSelectedSurface()->getTextureHitArea(); - - ofPushStyle(); - ofSetLineWidth(1); - ofSetColor(255, 255, 0, 255); - line.draw(); - ofPopStyle(); +void SurfaceManagerGui::setMode(int newGuiMode) { + if (newGuiMode != GuiMode::NONE && newGuiMode != GuiMode::TEXTURE_MAPPING && + newGuiMode != GuiMode::PROJECTION_MAPPING && + newGuiMode != GuiMode::SOURCE_SELECTION) { + throw std::runtime_error("Trying to set invalid mode."); + } + if (newGuiMode == GuiMode::NONE) { + ofHideCursor(); + } else { + ofShowCursor(); + } + + guiMode = newGuiMode; + + if (guiMode == GuiMode::SOURCE_SELECTION) { + sourcesEditor.enable(); + string sourceName = surfaceManager->getSelectedSurfaceSourceName(); + sourcesEditor.selectImageSourceRadioButton(sourceName); + } else { + sourcesEditor.disable(); + } + + if (guiMode == GuiMode::TEXTURE_MAPPING) { + textureEditor.enable(); + // refresh texture editor surface reference + textureEditor.setSurface(surfaceManager->getSelectedSurface()); + } else { + textureEditor.disable(); + } + + if (guiMode == GuiMode::PROJECTION_MAPPING) { + projectionEditor.enable(); + } else { + projectionEditor.disable(); + } } -void SurfaceManagerGui::startDrag() -{ - bDrag = true; +void SurfaceManagerGui::drawSelectedSurfaceHighlight() { + if (surfaceManager->getSelectedSurface() == NULL) return; + + ofPolyline line = surfaceManager->getSelectedSurface()->getHitArea(); + + ofPushStyle(); + ofSetLineWidth(1); + ofSetColor(255, 255, 255, 255); + line.draw(); + ofPopStyle(); } -void SurfaceManagerGui::stopDrag() -{ - bDrag = false; +void SurfaceManagerGui::drawSelectedSurfaceTextureHighlight() { + if (surfaceManager->getSelectedSurface() == NULL) return; + + ofPolyline line = surfaceManager->getSelectedSurface()->getTextureHitArea(); + + ofPushStyle(); + ofSetLineWidth(1); + ofSetColor(255, 255, 0, 255); + line.draw(); + ofPopStyle(); } - }} \ No newline at end of file +void SurfaceManagerGui::startDrag() { bDrag = true; } + +void SurfaceManagerGui::stopDrag() { bDrag = false; } +} +} \ No newline at end of file diff --git a/src/SurfaceManagerGui.h b/src/SurfaceManagerGui.h index 0dc8743..1c62cf6 100644 --- a/src/SurfaceManagerGui.h +++ b/src/SurfaceManagerGui.h @@ -12,37 +12,35 @@ #include "SourcesEditor.h" #include "GuiMode.h" -namespace ofx{ - namespace piMapper{ -class SurfaceManagerGui -{ -public: - SurfaceManagerGui(); - ~SurfaceManagerGui(); - - void registerMouseEvents(); - void unregisterMouseEvents(); - - void draw(); - void mousePressed(ofMouseEventArgs& args); - void mouseReleased(ofMouseEventArgs& args); - void mouseDragged(ofMouseEventArgs& args); - void setSurfaceManager(SurfaceManager* newSurfaceManager); - void setMode(int newGuiMode); - void drawSelectedSurfaceHighlight(); - void drawSelectedSurfaceTextureHighlight(); - void startDrag(); - void stopDrag(); - -private: - SurfaceManager* surfaceManager; - TextureEditor textureEditor; - ProjectionEditor projectionEditor; - SourcesEditor sourcesEditor; - int guiMode; - bool bDrag; - ofVec2f clickPosition; - -}; +namespace ofx { +namespace piMapper { +class SurfaceManagerGui { + public: + SurfaceManagerGui(); + ~SurfaceManagerGui(); + + void registerMouseEvents(); + void unregisterMouseEvents(); - }} \ No newline at end of file + void draw(); + void mousePressed(ofMouseEventArgs& args); + void mouseReleased(ofMouseEventArgs& args); + void mouseDragged(ofMouseEventArgs& args); + void setSurfaceManager(SurfaceManager* newSurfaceManager); + void setMode(int newGuiMode); + void drawSelectedSurfaceHighlight(); + void drawSelectedSurfaceTextureHighlight(); + void startDrag(); + void stopDrag(); + + private: + SurfaceManager* surfaceManager; + TextureEditor textureEditor; + ProjectionEditor projectionEditor; + SourcesEditor sourcesEditor; + int guiMode; + bool bDrag; + ofVec2f clickPosition; +}; +} +} \ No newline at end of file diff --git a/src/SurfaceType.h b/src/SurfaceType.h index 21deb9b..9db0cca 100755 --- a/src/SurfaceType.h +++ b/src/SurfaceType.h @@ -1,13 +1,9 @@ #pragma once -namespace ofx{ - namespace piMapper{ -struct SurfaceType -{ - enum { - TRIANGLE_SURFACE, - QUAD_SURFACE - }; +namespace ofx { +namespace piMapper { +struct SurfaceType { + enum { TRIANGLE_SURFACE, QUAD_SURFACE }; }; - - }} \ No newline at end of file +} +} \ No newline at end of file diff --git a/src/TextureEditor.cpp b/src/TextureEditor.cpp index a0a9eba..feb0728 100755 --- a/src/TextureEditor.cpp +++ b/src/TextureEditor.cpp @@ -1,195 +1,189 @@ #include "TextureEditor.h" -namespace ofx{ - namespace piMapper{ -TextureEditor::TextureEditor() -{ - clear(); - enable(); +namespace ofx { +namespace piMapper { +TextureEditor::TextureEditor() { + clear(); + enable(); } -TextureEditor::~TextureEditor() -{ - clear(); - disable(); +TextureEditor::~TextureEditor() { + clear(); + disable(); } -void TextureEditor::registerAppEvents() -{ - ofAddListener(ofEvents().update, this, &TextureEditor::update); +void TextureEditor::registerAppEvents() { + ofAddListener(ofEvents().update, this, &TextureEditor::update); } -void TextureEditor::unregisterAppEvents() -{ - ofRemoveListener(ofEvents().update, this, &TextureEditor::update); +void TextureEditor::unregisterAppEvents() { + ofRemoveListener(ofEvents().update, this, &TextureEditor::update); } -void TextureEditor::registerKeyEvents() -{ - ofAddListener(ofEvents().keyPressed, this, &TextureEditor::keyPressed); - ofAddListener(ofEvents().keyReleased, this, &TextureEditor::keyReleased); +void TextureEditor::registerKeyEvents() { + ofAddListener(ofEvents().keyPressed, this, &TextureEditor::keyPressed); + ofAddListener(ofEvents().keyReleased, this, &TextureEditor::keyReleased); } -void TextureEditor::unregisterKeyEvents() -{ - ofRemoveListener(ofEvents().keyPressed, this, &TextureEditor::keyPressed); - ofRemoveListener(ofEvents().keyReleased, this, &TextureEditor::keyReleased); +void TextureEditor::unregisterKeyEvents() { + ofRemoveListener(ofEvents().keyPressed, this, &TextureEditor::keyPressed); + ofRemoveListener(ofEvents().keyReleased, this, &TextureEditor::keyReleased); } -void TextureEditor::enable() -{ - registerAppEvents(); - registerKeyEvents(); - bShiftKeyDown = false; +void TextureEditor::enable() { + registerAppEvents(); + registerKeyEvents(); + bShiftKeyDown = false; } -void TextureEditor::disable() -{ - unregisterAppEvents(); - unregisterKeyEvents(); +void TextureEditor::disable() { + unregisterAppEvents(); + unregisterKeyEvents(); } -void TextureEditor::update(ofEventArgs &args) -{ - if ( surface == NULL ) return; - - // update surface if one of the joints is being dragged - ofVec2f textureSize = ofVec2f( surface->getTexture()->getWidth(), surface->getTexture()->getHeight() ); - for ( int i=0; iisDragged() || joints[i]->isSelected() ) { - // update vertex to new location - surface->setTexCoord(i, joints[i]->position / textureSize); - break; - } +void TextureEditor::update(ofEventArgs& args) { + if (surface == NULL) return; + + // update surface if one of the joints is being dragged + ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(), + surface->getTexture()->getHeight()); + for (int i = 0; i < joints.size(); i++) { + if (joints[i]->isDragged() || joints[i]->isSelected()) { + // update vertex to new location + surface->setTexCoord(i, joints[i]->position / textureSize); + break; } + } } -void TextureEditor::keyPressed(ofKeyEventArgs &args) -{ - int key = args.key; - float moveStep; - - if (bShiftKeyDown) moveStep = 10.0f; - else moveStep = 0.5f; - - switch (key) { - case OF_KEY_LEFT: moveSelection(ofVec2f(-moveStep,0.0f)); break; - case OF_KEY_RIGHT: moveSelection(ofVec2f(moveStep,0.0f)); break; - case OF_KEY_UP: moveSelection(ofVec2f(0.0f,-moveStep)); break; - case OF_KEY_DOWN: moveSelection(ofVec2f(0.0f,moveStep)); break; - case OF_KEY_SHIFT: bShiftKeyDown = true; break; - } +void TextureEditor::keyPressed(ofKeyEventArgs& args) { + int key = args.key; + float moveStep; + + if (bShiftKeyDown) + moveStep = 10.0f; + else + moveStep = 0.5f; + + switch (key) { + case OF_KEY_LEFT: + moveSelection(ofVec2f(-moveStep, 0.0f)); + break; + case OF_KEY_RIGHT: + moveSelection(ofVec2f(moveStep, 0.0f)); + break; + case OF_KEY_UP: + moveSelection(ofVec2f(0.0f, -moveStep)); + break; + case OF_KEY_DOWN: + moveSelection(ofVec2f(0.0f, moveStep)); + break; + case OF_KEY_SHIFT: + bShiftKeyDown = true; + break; + } } -void TextureEditor::keyReleased(ofKeyEventArgs &args) -{ - int key = args.key; - switch (key) { - case OF_KEY_SHIFT: bShiftKeyDown = false; break; - } +void TextureEditor::keyReleased(ofKeyEventArgs& args) { + int key = args.key; + switch (key) { + case OF_KEY_SHIFT: + bShiftKeyDown = false; + break; + } } +void TextureEditor::draw() { + if (surface == NULL) return; -void TextureEditor::draw() -{ - if (surface == NULL) return; - - drawJoints(); + drawJoints(); } -void TextureEditor::drawJoints() -{ - for ( int i=0; idraw(); - } +void TextureEditor::drawJoints() { + for (int i = 0; i < joints.size(); i++) { + joints[i]->draw(); + } } -void TextureEditor::setSurface(BaseSurface* newSurface) -{ - surface = newSurface; - createJoints(); +void TextureEditor::setSurface(BaseSurface* newSurface) { + surface = newSurface; + createJoints(); } -void TextureEditor::clear() -{ - surface = NULL; - clearJoints(); +void TextureEditor::clear() { + surface = NULL; + clearJoints(); } -void TextureEditor::createJoints() -{ - if ( surface == NULL ) return; - clearJoints(); - vector& texCoords = surface->getTexCoords(); - ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(), surface->getTexture()->getHeight()); - - for ( int i=0; iposition = texCoords[i] * textureSize; - } -} +void TextureEditor::createJoints() { + if (surface == NULL) return; + clearJoints(); + vector& texCoords = surface->getTexCoords(); + ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(), + surface->getTexture()->getHeight()); -void TextureEditor::clearJoints() -{ - while ( joints.size() ) { - delete joints.back(); - joints.pop_back(); - } + for (int i = 0; i < texCoords.size(); i++) { + joints.push_back(new CircleJoint()); + joints.back()->position = texCoords[i] * textureSize; + } } -void TextureEditor::unselectAllJoints() -{ - for ( int i=0; iunselect(); - } +void TextureEditor::clearJoints() { + while (joints.size()) { + delete joints.back(); + joints.pop_back(); + } } -void TextureEditor::moveTexCoords(ofVec2f by) -{ - if ( surface == NULL ) return; - vector& texCoords = surface->getTexCoords(); - ofVec2f textureSize = ofVec2f( surface->getTexture()->getWidth(), surface->getTexture()->getHeight() ); - for (int i=0; iposition += by; - texCoords[i] = joints[i]->position / textureSize; - } +void TextureEditor::unselectAllJoints() { + for (int i = 0; i < joints.size(); i++) { + joints[i]->unselect(); + } } -void TextureEditor::stopDragJoints() -{ - for (int i=0; istopDrag(); - } +void TextureEditor::moveTexCoords(ofVec2f by) { + if (surface == NULL) return; + vector& texCoords = surface->getTexCoords(); + ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(), + surface->getTexture()->getHeight()); + for (int i = 0; i < texCoords.size(); i++) { + joints[i]->position += by; + texCoords[i] = joints[i]->position / textureSize; + } } -void TextureEditor::moveSelection(ofVec2f by) -{ - // check if joints selected - bool bJointSelected = false; - BaseJoint* selectedJoint; - for ( int i=0; iisSelected()) { - bJointSelected = true; - selectedJoint = joints[i]; - break; - } - } - - if ( bJointSelected ) { - selectedJoint->position += by; - } else { - moveTexCoords(by); +void TextureEditor::stopDragJoints() { + for (int i = 0; i < joints.size(); i++) { + joints[i]->stopDrag(); + } +} + +void TextureEditor::moveSelection(ofVec2f by) { + // check if joints selected + bool bJointSelected = false; + BaseJoint* selectedJoint; + for (int i = 0; i < joints.size(); i++) { + if (joints[i]->isSelected()) { + bJointSelected = true; + selectedJoint = joints[i]; + break; } + } + + if (bJointSelected) { + selectedJoint->position += by; + } else { + moveTexCoords(by); + } } -CircleJoint* TextureEditor::hitTestJoints(ofVec2f pos) -{ - for ( int i=0; ihitTest(pos) ){ - return joints[i]; - } +CircleJoint* TextureEditor::hitTestJoints(ofVec2f pos) { + for (int i = 0; i < joints.size(); i++) { + if (joints[i]->hitTest(pos)) { + return joints[i]; } - return NULL; + } + return NULL; } - - }} \ No newline at end of file +} +} \ No newline at end of file diff --git a/src/TextureEditor.h b/src/TextureEditor.h index 1387984..cae4383 100644 --- a/src/TextureEditor.h +++ b/src/TextureEditor.h @@ -5,41 +5,39 @@ #include "BaseSurface.h" #include "CircleJoint.h" +namespace ofx { +namespace piMapper { +class TextureEditor { + public: + TextureEditor(); + ~TextureEditor(); -namespace ofx{ - namespace piMapper{ -class TextureEditor -{ -public: - TextureEditor(); - ~TextureEditor(); - - void registerAppEvents(); - void unregisterAppEvents(); - void registerKeyEvents(); - void unregisterKeyEvents(); - void enable(); - void disable(); - - void update(ofEventArgs& args); - void keyPressed(ofKeyEventArgs& args); - void keyReleased(ofKeyEventArgs& args); - void draw(); - void drawJoints(); - void setSurface(BaseSurface* newSurface); - void clear(); - void createJoints(); - void clearJoints(); - void unselectAllJoints(); - void moveTexCoords(ofVec2f by); - void stopDragJoints(); - void moveSelection(ofVec2f by); - CircleJoint* hitTestJoints(ofVec2f pos); - -private: - BaseSurface* surface; - vector joints; - bool bShiftKeyDown; -}; + void registerAppEvents(); + void unregisterAppEvents(); + void registerKeyEvents(); + void unregisterKeyEvents(); + void enable(); + void disable(); + + void update(ofEventArgs& args); + void keyPressed(ofKeyEventArgs& args); + void keyReleased(ofKeyEventArgs& args); + void draw(); + void drawJoints(); + void setSurface(BaseSurface* newSurface); + void clear(); + void createJoints(); + void clearJoints(); + void unselectAllJoints(); + void moveTexCoords(ofVec2f by); + void stopDragJoints(); + void moveSelection(ofVec2f by); + CircleJoint* hitTestJoints(ofVec2f pos); - }} \ No newline at end of file + private: + BaseSurface* surface; + vector joints; + bool bShiftKeyDown; +}; +} +} \ No newline at end of file diff --git a/src/TriangleSurface.cpp b/src/TriangleSurface.cpp index 950e360..b5d816e 100644 --- a/src/TriangleSurface.cpp +++ b/src/TriangleSurface.cpp @@ -1,158 +1,139 @@ #include "TriangleSurface.h" -namespace ofx{ - namespace piMapper{ -TriangleSurface::TriangleSurface() -{ - cout << "TriangleSurface constructor." << endl; - setup(); +namespace ofx { +namespace piMapper { +TriangleSurface::TriangleSurface() { + cout << "TriangleSurface constructor." << endl; + setup(); } -TriangleSurface::~TriangleSurface() -{ - cout << "TriangleSurface destructor." << endl; +TriangleSurface::~TriangleSurface() { + cout << "TriangleSurface destructor." << endl; } -void TriangleSurface::setup() -{ - // Create 3 points for the triangle - ofVec2f p1 = ofVec2f(ofGetWidth()/2.0f, 0); - ofVec2f p2 = ofVec2f(ofVec2f(0, ofGetHeight())); - ofVec2f p3 = ofVec2f(ofGetWidth(), ofGetHeight()); - - // Create 3 point for the texture coordinates - ofVec2f t1 = ofVec2f(0.5f, 0); - ofVec2f t2 = ofVec2f(0, 1.0f); - ofVec2f t3 = ofVec2f(1, 1.0f); - - setup( p1, p2, p3, t1, t2, t3, texture ); -} +void TriangleSurface::setup() { + // Create 3 points for the triangle + ofVec2f p1 = ofVec2f(ofGetWidth() / 2.0f, 0); + ofVec2f p2 = ofVec2f(ofVec2f(0, ofGetHeight())); + ofVec2f p3 = ofVec2f(ofGetWidth(), ofGetHeight()); -void TriangleSurface::setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr ) -{ - // Assign texture - texture = texturePtr; - - // Clear mesh - mesh.clear(); - - // Create a surface with the points - mesh.addVertex( p1 ); - mesh.addVertex( p2 ); - mesh.addVertex( p3 ); - - // Add texture coordinates - mesh.addTexCoord(t1); - mesh.addTexCoord(t2); - mesh.addTexCoord(t3); -} + // Create 3 point for the texture coordinates + ofVec2f t1 = ofVec2f(0.5f, 0); + ofVec2f t2 = ofVec2f(0, 1.0f); + ofVec2f t3 = ofVec2f(1, 1.0f); -void TriangleSurface::draw() -{ - texture->bind(); - mesh.draw(); - texture->unbind(); + setup(p1, p2, p3, t1, t2, t3, texture); } -void TriangleSurface::setVertex(int index, ofVec2f p) -{ - if ( index > 2 ) { - ofLog() << "Vertex with this index does not exist: " << index << endl; - return; - } - - mesh.setVertex(index, p); +void TriangleSurface::setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, + ofVec2f t2, ofVec2f t3, ofTexture* texturePtr) { + // Assign texture + texture = texturePtr; + + // Clear mesh + mesh.clear(); + + // Create a surface with the points + mesh.addVertex(p1); + mesh.addVertex(p2); + mesh.addVertex(p3); + + // Add texture coordinates + mesh.addTexCoord(t1); + mesh.addTexCoord(t2); + mesh.addTexCoord(t3); } -void TriangleSurface::setTexCoord(int index, ofVec2f t) -{ - if ( index > 2 ) { - ofLog() << "Texture coordinate with this index does not exist: " << index << endl; - return; - } - - mesh.setTexCoord(index, t); +void TriangleSurface::draw() { + texture->bind(); + mesh.draw(); + texture->unbind(); } -void TriangleSurface::moveBy(ofVec2f v) -{ - vector& vertices = getVertices(); - for (int i=0; i 2) { + ofLog() << "Vertex with this index does not exist: " << index << endl; + return; + } + + mesh.setVertex(index, p); } -int TriangleSurface::getType() -{ - return SurfaceType::TRIANGLE_SURFACE; +void TriangleSurface::setTexCoord(int index, ofVec2f t) { + if (index > 2) { + ofLog() << "Texture coordinate with this index does not exist: " << index + << endl; + return; + } + + mesh.setTexCoord(index, t); } -bool TriangleSurface::hitTest(ofVec2f p) -{ - // Construct ofPolyline from vertices - ofPolyline line = getHitArea(); - - if ( line.inside(p.x, p.y) ) { - return true; - } else { - return false; - } +void TriangleSurface::moveBy(ofVec2f v) { + vector& vertices = getVertices(); + for (int i = 0; i < vertices.size(); i++) { + vertices[i] += v; + } } -ofVec2f TriangleSurface::getVertex(int index) -{ - if ( index > 2 ) { - ofLog() << "Vertex with this index does not exist: " << index << endl; - throw std::runtime_error("Vertex index out of bounds."); - } - - ofVec3f vert = mesh.getVertex(index); - return ofVec2f(vert.x, vert.y); +int TriangleSurface::getType() { return SurfaceType::TRIANGLE_SURFACE; } + +bool TriangleSurface::hitTest(ofVec2f p) { + // Construct ofPolyline from vertices + ofPolyline line = getHitArea(); + + if (line.inside(p.x, p.y)) { + return true; + } else { + return false; + } } -ofVec2f TriangleSurface::getTexCoord(int index) -{ - if (index > 2) { - throw std::runtime_error("Texture coordinate index out of bounds."); - } - - return mesh.getTexCoord(index); +ofVec2f TriangleSurface::getVertex(int index) { + if (index > 2) { + ofLog() << "Vertex with this index does not exist: " << index << endl; + throw std::runtime_error("Vertex index out of bounds."); + } + + ofVec3f vert = mesh.getVertex(index); + return ofVec2f(vert.x, vert.y); } -ofPolyline TriangleSurface::getHitArea() -{ - ofPolyline line; - line.addVertex( ofPoint( mesh.getVertex(0).x, mesh.getVertex(0).y ) ); - line.addVertex( ofPoint( mesh.getVertex(1).x, mesh.getVertex(1).y ) ); - line.addVertex( ofPoint( mesh.getVertex(2).x, mesh.getVertex(2).y ) ); - line.close(); - - return line; +ofVec2f TriangleSurface::getTexCoord(int index) { + if (index > 2) { + throw std::runtime_error("Texture coordinate index out of bounds."); + } + + return mesh.getTexCoord(index); } -ofPolyline TriangleSurface::getTextureHitArea() -{ - ofPolyline line; - vector& texCoords = mesh.getTexCoords(); - ofVec2f textureSize = ofVec2f(texture->getWidth(), texture->getHeight()); - for ( int i=0; i& TriangleSurface::getVertices() -{ - // return only joint vertices - return mesh.getVertices(); +ofPolyline TriangleSurface::getTextureHitArea() { + ofPolyline line; + vector& texCoords = mesh.getTexCoords(); + ofVec2f textureSize = ofVec2f(texture->getWidth(), texture->getHeight()); + for (int i = 0; i < texCoords.size(); i++) { + line.addVertex(ofPoint(texCoords[i] * textureSize)); + } + line.close(); + + return line; } -vector& TriangleSurface::getTexCoords() -{ - - return mesh.getTexCoords(); +vector& TriangleSurface::getVertices() { + // return only joint vertices + return mesh.getVertices(); } - }} \ No newline at end of file +vector& TriangleSurface::getTexCoords() { return mesh.getTexCoords(); } +} +} \ No newline at end of file diff --git a/src/TriangleSurface.h b/src/TriangleSurface.h index f1b2e74..c3e968c 100644 --- a/src/TriangleSurface.h +++ b/src/TriangleSurface.h @@ -4,30 +4,29 @@ #include "BaseSurface.h" #include "SurfaceType.h" +namespace ofx { +namespace piMapper { +class TriangleSurface : public BaseSurface { + public: + TriangleSurface(); + ~TriangleSurface(); -namespace ofx{ - namespace piMapper{ -class TriangleSurface : public BaseSurface -{ -public: - TriangleSurface(); - ~TriangleSurface(); - - void setup(); - void setup( ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr ); - void draw(); - void setVertex( int index, ofVec2f p ); - void setTexCoord( int index, ofVec2f t ); - void moveBy(ofVec2f v); - - int getType(); - bool hitTest(ofVec2f p); - ofVec2f getVertex(int index); - ofVec2f getTexCoord(int index); - ofPolyline getHitArea(); - ofPolyline getTextureHitArea(); - vector& getVertices(); - vector& getTexCoords(); -}; + void setup(); + void setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2, + ofVec2f t3, ofTexture* texturePtr); + void draw(); + void setVertex(int index, ofVec2f p); + void setTexCoord(int index, ofVec2f t); + void moveBy(ofVec2f v); - }} \ No newline at end of file + int getType(); + bool hitTest(ofVec2f p); + ofVec2f getVertex(int index); + ofVec2f getTexCoord(int index); + ofPolyline getHitArea(); + ofPolyline getTextureHitArea(); + vector& getVertices(); + vector& getTexCoords(); +}; +} +} \ No newline at end of file diff --git a/src/ui/RadioList.cpp b/src/ui/RadioList.cpp index a1d5cf5..c280edc 100644 --- a/src/ui/RadioList.cpp +++ b/src/ui/RadioList.cpp @@ -1,192 +1,155 @@ #include "RadioList.h" -namespace ofx{ - namespace piMapper{ -RadioList::RadioList() -{ - storedTitle = ""; - storedSelectedItem = 0; +namespace ofx { +namespace piMapper { +RadioList::RadioList() { + storedTitle = ""; + storedSelectedItem = 0; } -RadioList::RadioList(vector &labels) -{ - RadioList(); - setup(labels); +RadioList::RadioList(vector& labels) { + RadioList(); + setup(labels); } -RadioList::RadioList(string title, vector &labels) -{ - RadioList(); - setup(title, labels); +RadioList::RadioList(string title, vector& labels) { + RadioList(); + setup(title, labels); } -RadioList::~RadioList() -{ - clear(); -} +RadioList::~RadioList() { clear(); } -void RadioList::setup(vector &labels) -{ - // Copy incomming labels for later use - storedLabels = labels; - - // Create toggles with labels from the labels arg - int i; - for (i = 0; i < labels.size(); i++) { - ofxToggle* toggle = new ofxToggle(); - toggle->setup(false); - toggle->setName(labels[i]); - toggle->addListener(this, &RadioList::onToggleClicked); - guiGroup.add(toggle); - } - - cout << "num items: " << guiGroup.getNumControls() << endl; +void RadioList::setup(vector& labels) { + // Copy incomming labels for later use + storedLabels = labels; + + // Create toggles with labels from the labels arg + int i; + for (i = 0; i < labels.size(); i++) { + ofxToggle* toggle = new ofxToggle(); + toggle->setup(false); + toggle->setName(labels[i]); + toggle->addListener(this, &RadioList::onToggleClicked); + guiGroup.add(toggle); + } + + cout << "num items: " << guiGroup.getNumControls() << endl; } -void RadioList::setup(string title, vector &labels) -{ - // Store title for later use - storedTitle = title; - guiGroup.setName(title); - setup(labels); +void RadioList::setup(string title, vector& labels) { + // Store title for later use + storedTitle = title; + guiGroup.setName(title); + setup(labels); } -void RadioList::draw() -{ - guiGroup.draw(); +void RadioList::draw() { guiGroup.draw(); } + +void RadioList::setTitle(string title) { + storedTitle = title; + guiGroup.setName(title); } -void RadioList::setTitle(string title) -{ - storedTitle = title; - guiGroup.setName(title); +void RadioList::setPosition(ofPoint p) { guiGroup.setPosition(p); } + +void RadioList::setPosition(float x, float y) { guiGroup.setPosition(x, y); } + +void RadioList::selectItem(int index) { + if (index >= guiGroup.getNumControls()) { + return; + } + + unselectAll(); + + ofxToggle* toggle = static_cast(guiGroup.getControl(index)); + toggle->removeListener(this, &RadioList::onToggleClicked); + *toggle = true; // Select the specific radio button + toggle->addListener(this, &RadioList::onToggleClicked); + string name = toggle->getName(); + ofNotifyEvent(radioSelectedEvent, name, this); + + storedSelectedItem = index; } -void RadioList::setPosition(ofPoint p) -{ - guiGroup.setPosition(p); +void RadioList::enable() { + if (guiGroup.getNumControls() >= 0) { + clear(); + } + + // Rebuild everyting + setup(storedTitle, storedLabels); + + // Select the stored selected item without throwing an event + ofxToggle* toggle = + static_cast(guiGroup.getControl(storedSelectedItem)); + toggle->removeListener(this, &RadioList::onToggleClicked); + *toggle = true; + toggle->addListener(this, &RadioList::onToggleClicked); + + cout << "num items after enable: " << guiGroup.getNumControls() << endl; } -void RadioList::setPosition(float x, float y) -{ - guiGroup.setPosition(x, y); +void RadioList::disable() { + // Just remove everything + clear(); } -void RadioList::selectItem(int index) -{ - if (index >= guiGroup.getNumControls()) { - return; - } - - unselectAll(); - - ofxToggle* toggle = static_cast(guiGroup.getControl(index)); +void RadioList::clear() { + int i; + for (i = 0; i < guiGroup.getNumControls(); i++) { + ofxToggle* toggle = static_cast(guiGroup.getControl(i)); toggle->removeListener(this, &RadioList::onToggleClicked); - *toggle = true; // Select the specific radio button - toggle->addListener(this, &RadioList::onToggleClicked); - string name = toggle->getName(); - ofNotifyEvent(radioSelectedEvent, name, this); - - storedSelectedItem = index; + delete toggle; + } + guiGroup.clear(); } -void RadioList::enable() -{ - if (guiGroup.getNumControls() >= 0) { - clear(); - } - - // Rebuild everyting - setup(storedTitle, storedLabels); - - // Select the stored selected item without throwing an event - ofxToggle* toggle = static_cast(guiGroup.getControl(storedSelectedItem)); +void RadioList::unselectAll() { + int i; + for (i = 0; i < guiGroup.getNumControls(); i++) { + ofxToggle* toggle = static_cast(guiGroup.getControl(i)); + ofParameter* paramPtr = + static_cast*>(&toggle->getParameter()); toggle->removeListener(this, &RadioList::onToggleClicked); - *toggle = true; + *toggle = false; toggle->addListener(this, &RadioList::onToggleClicked); - - cout << "num items after enable: " << guiGroup.getNumControls() << endl; + } } -void RadioList::disable() -{ - // Just remove everything - clear(); -} +ofPoint RadioList::getPosition() { return guiGroup.getPosition(); } -void RadioList::clear() -{ - int i; - for (i = 0; i < guiGroup.getNumControls(); i++) { - ofxToggle* toggle = static_cast(guiGroup.getControl(i)); - toggle->removeListener(this, &RadioList::onToggleClicked); - delete toggle; - } - guiGroup.clear(); -} +float RadioList::getWidth() { return guiGroup.getWidth(); } -void RadioList::unselectAll() -{ - int i; - for (i = 0; i < guiGroup.getNumControls(); i++) { - ofxToggle* toggle = static_cast(guiGroup.getControl(i)); - ofParameter* paramPtr = static_cast*>(&toggle->getParameter()); - toggle->removeListener(this, &RadioList::onToggleClicked); - *toggle = false; - toggle->addListener(this, &RadioList::onToggleClicked); - } -} +float RadioList::getHeight() { return guiGroup.getHeight(); } -ofPoint RadioList::getPosition() -{ - return guiGroup.getPosition(); -} +string RadioList::getTitle() { return guiGroup.getName(); } -float RadioList::getWidth() -{ - return guiGroup.getWidth(); -} +string RadioList::getItemName(int index) { + if (index >= guiGroup.getNumControls()) { + return ""; + } -float RadioList::getHeight() -{ - return guiGroup.getHeight(); + ofxToggle* toggle = static_cast(guiGroup.getControl(index)); + return toggle->getName(); } -string RadioList::getTitle() -{ - return guiGroup.getName(); -} +int RadioList::size() { return guiGroup.getNumControls(); } -string RadioList::getItemName(int index) -{ - if (index >= guiGroup.getNumControls()) { - return ""; - } - - ofxToggle* toggle = static_cast(guiGroup.getControl(index)); - return toggle->getName(); -} +void RadioList::onToggleClicked(bool& toggleValue) { + unselectAll(); -int RadioList::size() -{ - return guiGroup.getNumControls(); -} + // Search for the actual toggle triggering the event + int i; + for (i = 0; i < guiGroup.getNumControls(); i++) { + ofxToggle* toggle = static_cast(guiGroup.getControl(i)); + ofParameter* paramPtr = + static_cast*>(&toggle->getParameter()); -void RadioList::onToggleClicked(bool &toggleValue) -{ - unselectAll(); - - // Search for the actual toggle triggering the event - int i; - for (i = 0; i < guiGroup.getNumControls(); i++) { - ofxToggle* toggle = static_cast(guiGroup.getControl(i)); - ofParameter* paramPtr = static_cast*>(&toggle->getParameter()); - - if (&(paramPtr->get()) == &toggleValue) { - selectItem(i); - break; - } + if (&(paramPtr->get()) == &toggleValue) { + selectItem(i); + break; } + } +} +} } - - }} diff --git a/src/ui/RadioList.h b/src/ui/RadioList.h index 0d1daea..45b7327 100644 --- a/src/ui/RadioList.h +++ b/src/ui/RadioList.h @@ -5,46 +5,47 @@ #include "ofxToggle.h" #include "ofxLabel.h" -namespace ofx{ - namespace piMapper{ -class RadioList -{ -public: - RadioList(); - RadioList(vector &labels); - RadioList(string title, vector &labels); - ~RadioList(); - - void setup(vector &labels); - void setup(string title, vector &labels); - void draw(); - void setTitle(string title); - void setPosition(ofPoint p); - void setPosition(float x, float y); - void selectItem(int index); - void enable(); - void disable(); - void clear(); - void unselectAll(); - ofPoint getPosition(); - float getWidth(); - float getHeight(); - string getTitle(); - string getItemName(int index); - int size(); - - // This event notifies about a toggle being selected and passes it's name to the listeners. - // Use ofAddListener(RadioListInstance.radioSelectedEvent, listenerClassPtr, &listenerClass::listenerMethod) - // to listen to this. Listner method void listenerMethod(string & radioName) - ofEvent radioSelectedEvent; - -private: - vector storedLabels; - string storedTitle; - ofxGuiGroup guiGroup; - int storedSelectedItem; - - void onToggleClicked(bool &toggleValue); -}; +namespace ofx { +namespace piMapper { +class RadioList { + public: + RadioList(); + RadioList(vector &labels); + RadioList(string title, vector &labels); + ~RadioList(); + + void setup(vector &labels); + void setup(string title, vector &labels); + void draw(); + void setTitle(string title); + void setPosition(ofPoint p); + void setPosition(float x, float y); + void selectItem(int index); + void enable(); + void disable(); + void clear(); + void unselectAll(); + ofPoint getPosition(); + float getWidth(); + float getHeight(); + string getTitle(); + string getItemName(int index); + int size(); + + // This event notifies about a toggle being selected and passes it's name to + // the listeners. + // Use ofAddListener(RadioListInstance.radioSelectedEvent, listenerClassPtr, + // &listenerClass::listenerMethod) + // to listen to this. Listner method void listenerMethod(string & radioName) + ofEvent radioSelectedEvent; - }} \ No newline at end of file + private: + vector storedLabels; + string storedTitle; + ofxGuiGroup guiGroup; + int storedSelectedItem; + + void onToggleClicked(bool &toggleValue); +}; +} +} \ No newline at end of file