From 198b4456c5207af8c01c0c5537a5cc433053ec0d Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Tue, 27 Sep 2016 11:55:00 +0300 Subject: [PATCH] Change owner of ProjectionEditorWidget to Gui --- .../States/ProjectionMappingState.cpp | 30 +++++++--- .../States/ProjectionMappingState.h | 1 + src/Gui/Gui.cpp | 4 ++ src/Gui/Gui.h | 3 + src/Gui/Widgets/ProjectionEditorWidget.cpp | 60 +++++++++++++++++++ src/Gui/Widgets/ProjectionEditorWidget.h | 6 ++ src/Surfaces/SurfaceManagerGui.cpp | 34 ++++++----- src/Surfaces/SurfaceManagerGui.h | 6 +- 8 files changed, 118 insertions(+), 26 deletions(-) diff --git a/src/Application/States/ProjectionMappingState.cpp b/src/Application/States/ProjectionMappingState.cpp index c0fdc37..6682c7c 100644 --- a/src/Application/States/ProjectionMappingState.cpp +++ b/src/Application/States/ProjectionMappingState.cpp @@ -16,8 +16,13 @@ ProjectionMappingState * ProjectionMappingState::instance(){ return _instance; } +void ProjectionMappingState::setup(Application *app){ + Gui::instance()->getProjectionEditorWidget().setSurfaceManager(app->getSurfaceManager()); +} + void ProjectionMappingState::update(Application * app){ - app->getGui()->getProjectionEditorWidget()->update(); + //app->getGui()->getProjectionEditorWidget()->update(); + Gui::instance()->getProjectionEditorWidget().update(); Gui::instance()->getScaleWidget().update(); } @@ -28,7 +33,8 @@ void ProjectionMappingState::draw(Application * app){ ofPopStyle(); // TODO: Extract projection editor, make it a widget - app->getGui()->getProjectionEditorWidget()->draw(); + //app->getGui()->getProjectionEditorWidget()->draw(); + Gui::instance()->getProjectionEditorWidget().draw(); // Draw scale widget. The size of the widget is being set on surface select. BaseSurface * selectedSurface = app->getSurfaceManager()->getSelectedSurface(); @@ -292,11 +298,14 @@ void ProjectionMappingState::onMousePressed(Application * app, ofMouseEventArgs int hitJointIndex = -1; BaseSurface * hitSurface = 0; - hitJoint = app->getGui()->getProjectionEditorWidget()->hitTestJoints(ofVec2f(args.x, args.y)); + //hitJoint = app->getGui()->getProjectionEditorWidget()->hitTestJoints(ofVec2f(args.x, args.y)); + hitJoint = Gui::instance()->getProjectionEditorWidget().hitTestJoints(ofVec2f(args.x, args.y)); if(hitJoint){ - for(int i = app->getGui()->getProjectionEditorWidget()->getJoints()->size() - 1; i >= 0 ; --i){ - if((*app->getGui()->getProjectionEditorWidget()->getJoints())[i] == hitJoint){ + //for(int i = app->getGui()->getProjectionEditorWidget()->getJoints()->size() - 1; i >= 0 ; --i){ + for(int i = Gui::instance()->getProjectionEditorWidget().getJoints()->size() - 1; i >= 0 ; --i){ + //if((*app->getGui()->getProjectionEditorWidget()->getJoints())[i] == hitJoint){ + if((*Gui::instance()->getProjectionEditorWidget().getJoints())[i] == hitJoint){ hitJointIndex = i; break; } @@ -327,19 +336,22 @@ void ProjectionMappingState::onMousePressed(Application * app, ofMouseEventArgs void ProjectionMappingState::onMouseReleased(Application * app, ofMouseEventArgs & args){ Gui::instance()->onMouseReleased(args); - app->getGui()->stopDrag(); - app->getGui()->getProjectionEditorWidget()->stopDragJoints(); + app->getGui()->stopDrag(); // TODO: handle this locally + //app->getGui()->getProjectionEditorWidget()->stopDragJoints(); + Gui::instance()->getProjectionEditorWidget().stopDragJoints(); } void ProjectionMappingState::onMouseDragged(Application * app, ofMouseEventArgs & args){ Gui::instance()->onMouseDragged(args); - app->getGui()->getProjectionEditorWidget()->mouseDragged(args); + //app->getGui()->getProjectionEditorWidget()->mouseDragged(args); + Gui::instance()->getProjectionEditorWidget().mouseDragged(args); // TODO: Handle app->getGui()->clickPosition and app->getGui()->bDrag locally. if(app->getGui()->bDrag){ ofVec2f mousePosition = ofVec2f(args.x, args.y); ofVec2f distance = mousePosition - app->getGui()->clickPosition; - app->getGui()->getProjectionEditorWidget()->moveSelectedSurface(distance); + //app->getGui()->getProjectionEditorWidget()->moveSelectedSurface(distance); + Gui::instance()->getProjectionEditorWidget().moveSelectedSurface(distance); app->getGui()->clickPosition = mousePosition; } } diff --git a/src/Application/States/ProjectionMappingState.h b/src/Application/States/ProjectionMappingState.h index 3df93a2..fbb43fc 100644 --- a/src/Application/States/ProjectionMappingState.h +++ b/src/Application/States/ProjectionMappingState.h @@ -40,6 +40,7 @@ class ProjectionMappingState : public ApplicationBaseState { public: static ProjectionMappingState * instance(); + void setup(Application * app); void update(Application * app); void draw(Application * app); void onKeyPressed(Application * app, ofKeyEventArgs & args); diff --git a/src/Gui/Gui.cpp b/src/Gui/Gui.cpp index a4af37e..1061df6 100644 --- a/src/Gui/Gui.cpp +++ b/src/Gui/Gui.cpp @@ -100,6 +100,10 @@ TextureEditorWidget & Gui::getTextureEditorWidget(){ return _textureEditorWidget; } +ProjectionEditorWidget & Gui::getProjectionEditorWidget(){ + return _projectionEditorWidget; +} + void Gui::onScaleWidgetEvent(GuiWidgetEvent & event){ GuiEvent e; e.args = event.args; diff --git a/src/Gui/Gui.h b/src/Gui/Gui.h index 3b6672a..2150b5a 100644 --- a/src/Gui/Gui.h +++ b/src/Gui/Gui.h @@ -9,6 +9,7 @@ #include "SurfaceHighlightWidget.h" #include "TextureHighlightWidget.h" #include "TextureEditorWidget.h" +#include "ProjectionEditorWidget.h" namespace ofx { namespace piMapper { @@ -69,6 +70,7 @@ class Gui { SurfaceHighlightWidget & getSurfaceHighlightWidget(); TextureHighlightWidget & getTextureHighlightWidget(); TextureEditorWidget & getTextureEditorWidget(); + ProjectionEditorWidget & getProjectionEditorWidget(); // Consider these as a part of the application states/modes. void onMousePressed(ofMouseEventArgs & args); @@ -90,6 +92,7 @@ class Gui { SurfaceHighlightWidget _surfaceHighlightWidget; TextureHighlightWidget _textureHighlightWidget; TextureEditorWidget _textureEditorWidget; + ProjectionEditorWidget _projectionEditorWidget; }; } // piMapper diff --git a/src/Gui/Widgets/ProjectionEditorWidget.cpp b/src/Gui/Widgets/ProjectionEditorWidget.cpp index ed5a6b3..bb770db 100644 --- a/src/Gui/Widgets/ProjectionEditorWidget.cpp +++ b/src/Gui/Widgets/ProjectionEditorWidget.cpp @@ -102,7 +102,31 @@ void ProjectionEditorWidget::gotMessage(ofMessage & msg){ } void ProjectionEditorWidget::setSurfaceManager(SurfaceManager * newSurfaceManager){ + if(surfaceManager != 0){ + ofRemoveListener(surfaceManager->vertexChangedEvent, this, + &ProjectionEditorWidget::onVertexChanged); + ofRemoveListener(surfaceManager->verticesChangedEvent, this, + &ProjectionEditorWidget::onVerticesChanged); + ofRemoveListener(surfaceManager->surfaceSelectedEvent, this, + &ProjectionEditorWidget::onSurfaceSelected); + ofRemoveListener(surfaceManager->vertexSelectedEvent, this, + &ProjectionEditorWidget::onVertexSelected); + ofRemoveListener(surfaceManager->vertexUnselectedEvent, this, + &ProjectionEditorWidget::onVertexUnselected); + } + surfaceManager = newSurfaceManager; + + ofAddListener(surfaceManager->vertexChangedEvent, this, + &ProjectionEditorWidget::onVertexChanged); + ofAddListener(surfaceManager->verticesChangedEvent, this, + &ProjectionEditorWidget::onVerticesChanged); + ofAddListener(surfaceManager->surfaceSelectedEvent, this, + &ProjectionEditorWidget::onSurfaceSelected); + ofAddListener(surfaceManager->vertexSelectedEvent, this, + &ProjectionEditorWidget::onVertexSelected); + ofAddListener(surfaceManager->vertexUnselectedEvent, this, + &ProjectionEditorWidget::onVertexUnselected); } void ProjectionEditorWidget::clearJoints(){ @@ -183,6 +207,42 @@ vector * ProjectionEditorWidget::getJoints(){ return &joints; } +void ProjectionEditorWidget::onVertexChanged(int & i){ + bool isDragged = getJoints()->at(i)->isDragged(); + createJoints(); + getJoints()->at(i)->select(); + if(isDragged){ + getJoints()->at(i)->startDrag(); + }else{ + getJoints()->at(i)->stopDrag(); + } +} + +void ProjectionEditorWidget::onVerticesChanged(vector & vertices){ + createJoints(); +} + +void ProjectionEditorWidget::onSurfaceSelected(int & surfaceIndex){ + createJoints(); +} + +void ProjectionEditorWidget::onVertexSelected(int & vertexIndex){ + if(getJoints()->size() == 0){ + return; + } + + unselectAllJoints(); + getJoints()->at(vertexIndex)->select(); +} + +void ProjectionEditorWidget::onVertexUnselected(int & vertexIndex){ + if(getJoints()->size() == 0){ + return; + } + + unselectAllJoints(); +} + void ProjectionEditorWidget::drawJoints(){ for(int i = 0; i < joints.size(); i++){ joints[i]->draw(); diff --git a/src/Gui/Widgets/ProjectionEditorWidget.h b/src/Gui/Widgets/ProjectionEditorWidget.h index bd77af2..1e6eab5 100644 --- a/src/Gui/Widgets/ProjectionEditorWidget.h +++ b/src/Gui/Widgets/ProjectionEditorWidget.h @@ -32,6 +32,12 @@ class ProjectionEditorWidget { void setSnapDistance(float newSnapDistance); CircleJoint * hitTestJoints(ofVec2f pos); vector * getJoints(); + + void onVertexChanged(int & i); + void onVerticesChanged(vector & vertices); + void onSurfaceSelected(int & surfaceIndex); + void onVertexSelected(int & vertexIndex); + void onVertexUnselected(int & vertexIndex); private: SurfaceManager * surfaceManager; diff --git a/src/Surfaces/SurfaceManagerGui.cpp b/src/Surfaces/SurfaceManagerGui.cpp index cc15235..0079155 100644 --- a/src/Surfaces/SurfaceManagerGui.cpp +++ b/src/Surfaces/SurfaceManagerGui.cpp @@ -28,7 +28,7 @@ void SurfaceManagerGui::setSurfaceManager(SurfaceManager * newSurfaceManager){ ofAddListener(newSurfaceManager->vertexSelectedEvent, this, &SurfaceManagerGui::onVertexSelected); ofAddListener(newSurfaceManager->vertexUnselectedEvent, this, &SurfaceManagerGui::onVertexUnselected); - projectionEditorWidget.setSurfaceManager(surfaceManager); +// projectionEditorWidget.setSurfaceManager(surfaceManager); sourcesEditor.setSurfaceManager(surfaceManager); } @@ -63,11 +63,13 @@ void SurfaceManagerGui::setMode(int newGuiMode){ sourcesEditor.disable(); } +/* if(guiMode == GuiMode::PROJECTION_MAPPING){ projectionEditorWidget.enable(); }else{ projectionEditorWidget.disable(); } +*/ } int SurfaceManagerGui::getMode(){ @@ -82,48 +84,52 @@ void SurfaceManagerGui::stopDrag(){ bDrag = false; } -ProjectionEditorWidget * SurfaceManagerGui::getProjectionEditorWidget(){ - return &projectionEditorWidget; -} +//ProjectionEditorWidget * SurfaceManagerGui::getProjectionEditorWidget(){ +// return &projectionEditorWidget; +//} SourcesEditor * SurfaceManagerGui::getSourcesEditor(){ return &sourcesEditor; } void SurfaceManagerGui::onVertexChanged(int & i){ - bool isDragged = projectionEditorWidget.getJoints()->at(i)->isDragged(); - projectionEditorWidget.createJoints(); - projectionEditorWidget.getJoints()->at(i)->select(); - if(isDragged){ - projectionEditorWidget.getJoints()->at(i)->startDrag(); - }else{ - projectionEditorWidget.getJoints()->at(i)->stopDrag(); - } + //bool isDragged = projectionEditorWidget.getJoints()->at(i)->isDragged(); + //projectionEditorWidget.createJoints(); + //projectionEditorWidget.getJoints()->at(i)->select(); + //if(isDragged){ + // projectionEditorWidget.getJoints()->at(i)->startDrag(); + //}else{ + // projectionEditorWidget.getJoints()->at(i)->stopDrag(); + //} } void SurfaceManagerGui::onVerticesChanged(vector & vertices){ - projectionEditorWidget.createJoints(); + //projectionEditorWidget.createJoints(); } void SurfaceManagerGui::onSurfaceSelected(int & surfaceIndex){ - projectionEditorWidget.createJoints(); + //projectionEditorWidget.createJoints(); } void SurfaceManagerGui::onVertexSelected(int & vertexIndex){ + /* if(projectionEditorWidget.getJoints()->size() == 0){ return; } projectionEditorWidget.unselectAllJoints(); projectionEditorWidget.getJoints()->at(vertexIndex)->select(); + */ } void SurfaceManagerGui::onVertexUnselected(int & vertexIndex){ + /* if(projectionEditorWidget.getJoints()->size() == 0){ return; } projectionEditorWidget.unselectAllJoints(); + */ } } // namespace piMapper diff --git a/src/Surfaces/SurfaceManagerGui.h b/src/Surfaces/SurfaceManagerGui.h index 302c627..c1ca8e6 100644 --- a/src/Surfaces/SurfaceManagerGui.h +++ b/src/Surfaces/SurfaceManagerGui.h @@ -6,7 +6,7 @@ #include "ofGraphics.h" #include "SurfaceManager.h" -#include "ProjectionEditorWidget.h" +//#include "ProjectionEditorWidget.h" #include "SourcesEditor.h" #include "GuiMode.h" #include "CmdManager.h" @@ -35,7 +35,7 @@ class SurfaceManagerGui { void startDrag(); void stopDrag(); - ProjectionEditorWidget * getProjectionEditorWidget(); + //ProjectionEditorWidget * getProjectionEditorWidget(); SourcesEditor * getSourcesEditor(); void onVertexChanged(int & i); @@ -50,7 +50,7 @@ class SurfaceManagerGui { private: SurfaceManager * surfaceManager; MediaServer * mediaServer; - ProjectionEditorWidget projectionEditorWidget; + //ProjectionEditorWidget projectionEditorWidget; SourcesEditor sourcesEditor; int guiMode;