From 038758ce33ef321d29bba236185d8fd1c185f38f Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Wed, 14 Sep 2016 12:30:20 +0300 Subject: [PATCH] Fix ScaleWidget not capturing new surface after duplicate --- src/Application/Application.cpp | 7 +++ src/Application/Application.h | 1 + src/Application/States/ApplicationBaseState.h | 1 + .../States/ProjectionMappingState.cpp | 5 +- .../States/ProjectionMappingState.h | 1 + src/Commands/SelNextSurfaceCmd.cpp | 4 -- src/Commands/SelPrevSurfaceCmd.cpp | 4 -- src/Commands/SelSurfaceCmd.cpp | 4 -- src/Gui/Widgets/ScaleWidget.cpp | 58 +++++++++++-------- src/Gui/Widgets/ScaleWidget.h | 6 +- src/Surfaces/SurfaceManager.cpp | 2 + src/ofxPiMapper.cpp | 4 ++ src/ofxPiMapper.h | 1 + 13 files changed, 60 insertions(+), 38 deletions(-) diff --git a/src/Application/Application.cpp b/src/Application/Application.cpp index f19915f..60c8e64 100644 --- a/src/Application/Application.cpp +++ b/src/Application/Application.cpp @@ -44,6 +44,13 @@ void Application::setup(){ if(_isSSHConnection){ consoleListener.setup(this); } + + // TODO: Consider whether this is the right place for it + Gui::instance()->getScaleWidget().setSurfaceManager(&_surfaceManager); +} + +void Application::update(){ + _state->update(this); } ApplicationBaseState * Application::getState(){ diff --git a/src/Application/Application.h b/src/Application/Application.h index 4bdafca..18d062f 100644 --- a/src/Application/Application.h +++ b/src/Application/Application.h @@ -39,6 +39,7 @@ class Application : public KeyListener { ApplicationBaseState * getState(); void setup(); + void update(); void draw(); void onKeyPressed(ofKeyEventArgs & args); diff --git a/src/Application/States/ApplicationBaseState.h b/src/Application/States/ApplicationBaseState.h index 07d2457..68a2f67 100644 --- a/src/Application/States/ApplicationBaseState.h +++ b/src/Application/States/ApplicationBaseState.h @@ -12,6 +12,7 @@ class Application; class ApplicationBaseState { public: + virtual void update(Application * app){} virtual void draw(Application * app){} virtual void setState(Application * app, ApplicationBaseState * st); diff --git a/src/Application/States/ProjectionMappingState.cpp b/src/Application/States/ProjectionMappingState.cpp index a15db1a..8359436 100644 --- a/src/Application/States/ProjectionMappingState.cpp +++ b/src/Application/States/ProjectionMappingState.cpp @@ -16,6 +16,10 @@ ProjectionMappingState * ProjectionMappingState::instance(){ return _instance; } +void ProjectionMappingState::update(Application * app){ + Gui::instance()->getScaleWidget().update(); +} + void ProjectionMappingState::draw(Application * app){ ofPushStyle(); ofSetColor(255, 255, 255, 255); @@ -306,7 +310,6 @@ void ProjectionMappingState::onJointPressed(Application * app, GuiJointEvent & e void ProjectionMappingState::onSurfacePressed(Application * app, GuiSurfaceEvent & e){ if(app->getSurfaceManager()->getSelectedSurface() != e.surface){ app->getCmdManager()->exec(new SelSurfaceCmd(app->getSurfaceManager(), e.surface )); - Gui::instance()->getScaleWidget().setSurface(app->getSurfaceManager()->getSelectedSurface()); } app->getCmdManager()->exec(new StartDragSurfaceCmd(e.surface)); diff --git a/src/Application/States/ProjectionMappingState.h b/src/Application/States/ProjectionMappingState.h index 23357c4..9022e22 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 update(Application * app); void draw(Application * app); void onKeyPressed(Application * app, ofKeyEventArgs & args); void onMousePressed(Application * app, ofMouseEventArgs & args); diff --git a/src/Commands/SelNextSurfaceCmd.cpp b/src/Commands/SelNextSurfaceCmd.cpp index 09198fc..bc18cb5 100644 --- a/src/Commands/SelNextSurfaceCmd.cpp +++ b/src/Commands/SelNextSurfaceCmd.cpp @@ -10,15 +10,11 @@ SelNextSurfaceCmd::SelNextSurfaceCmd(SurfaceManager * surfaceManager){ void SelNextSurfaceCmd::exec(){ _prevSelectedSurface = _surfaceManager->getSelectedSurface(); _surfaceManager->selectNextSurface(); - Gui::instance()->getScaleWidget().setSurface(_surfaceManager->getSelectedSurface()); } void SelNextSurfaceCmd::undo(){ ofLogNotice("SelNextSurfaceCmd", "undo"); _surfaceManager->selectSurface(_prevSelectedSurface); - if(_prevSelectedSurface != 0){ - Gui::instance()->getScaleWidget().setSurface(_prevSelectedSurface); - } _prevSelectedSurface = 0; } diff --git a/src/Commands/SelPrevSurfaceCmd.cpp b/src/Commands/SelPrevSurfaceCmd.cpp index 24cd2b6..da63b0d 100644 --- a/src/Commands/SelPrevSurfaceCmd.cpp +++ b/src/Commands/SelPrevSurfaceCmd.cpp @@ -10,15 +10,11 @@ SelPrevSurfaceCmd::SelPrevSurfaceCmd(SurfaceManager * surfaceManager){ void SelPrevSurfaceCmd::exec(){ _prevSelectedSurface = _surfaceManager->getSelectedSurface(); _surfaceManager->selectPrevSurface(); - Gui::instance()->getScaleWidget().setSurface(_surfaceManager->getSelectedSurface()); } void SelPrevSurfaceCmd::undo(){ ofLogNotice("SelPrevSurfaceCmd", "undo"); _surfaceManager->selectSurface(_prevSelectedSurface); - if(_prevSelectedSurface != 0){ - Gui::instance()->getScaleWidget().setSurface(_prevSelectedSurface); - } _prevSelectedSurface = 0; } diff --git a/src/Commands/SelSurfaceCmd.cpp b/src/Commands/SelSurfaceCmd.cpp index 8235e65..9c90de0 100644 --- a/src/Commands/SelSurfaceCmd.cpp +++ b/src/Commands/SelSurfaceCmd.cpp @@ -11,15 +11,11 @@ SelSurfaceCmd::SelSurfaceCmd(SurfaceManager * surfaceManager, BaseSurface * surf void SelSurfaceCmd::exec(){ _prevSelectedSurface = _surfaceManager->getSelectedSurface(); _surfaceManager->selectSurface(_surfaceToSelect); - Gui::instance()->getScaleWidget().setSurface(_surfaceToSelect); } void SelSurfaceCmd::undo(){ ofLogNotice("SelSurfaceCmd", "undo"); _surfaceManager->selectSurface(_prevSelectedSurface); - if(_prevSelectedSurface != 0){ - Gui::instance()->getScaleWidget().setSurface(_prevSelectedSurface); - } _surfaceToSelect = 0; _prevSelectedSurface = 0; } diff --git a/src/Gui/Widgets/ScaleWidget.cpp b/src/Gui/Widgets/ScaleWidget.cpp index c2d70c0..b461090 100644 --- a/src/Gui/Widgets/ScaleWidget.cpp +++ b/src/Gui/Widgets/ScaleWidget.cpp @@ -11,24 +11,37 @@ ScaleWidget::ScaleWidget(){ _handle.height = 20; _scale = 1.0f; - _surface = 0; + _surfaceManager = 0; + _selectedSurface = 0; } void ScaleWidget::setup(){ - + } void ScaleWidget::update(){ - + if(_surfaceManager == 0){ + return; + } + + if(_selectedSurface != _surfaceManager->getSelectedSurface()){ + _selectedSurface = _surfaceManager->getSelectedSurface(); + setRect(_surfaceManager->getSelectedSurface()->getBoundingBox()); + } } void ScaleWidget::draw(){ - if(_surface != 0){ - ofPoint centroid = _surface->getBoundingBox().getCenter(); + if(_surfaceManager == 0){ + return; + } + + if(_surfaceManager->getSelectedSurface() != 0){ + ofPoint centroid = _surfaceManager->getSelectedSurface()->getBoundingBox().getCenter(); float lineLength = centroid.distance( ofPoint( - _surface->getBoundingBox().x + _surface->getBoundingBox().width, - _surface->getBoundingBox().y)); + _surfaceManager->getSelectedSurface()->getBoundingBox().x + + _surfaceManager->getSelectedSurface()->getBoundingBox().width, + _surfaceManager->getSelectedSurface()->getBoundingBox().y)); // Handle surface move float dx = _line[0].x - centroid.x; @@ -92,36 +105,30 @@ void ScaleWidget::onMouseReleased(ofMouseEventArgs & args){ void ScaleWidget::onMouseDragged(ofMouseEventArgs & args){ if(_dragging){ - if(_surface == 0){ - cout << "No surface selected" << endl; + if(_surfaceManager == 0){ return; } - ofRectangle box = _surface->getBoundingBox(); + if(_surfaceManager->getSelectedSurface() == 0){ + return; + } + + ofRectangle box = _surfaceManager->getSelectedSurface()->getBoundingBox(); float boxAspect = box.width / box.height; ofPolyline newLine = _line; newLine[1].x = args.x; newLine[1].y = args.y; - _scale = _surface->getScale() / + _scale = _surfaceManager->getSelectedSurface()->getScale() / _line[0].distance(_line[1]) * newLine[0].distance(newLine[1]); - //float lineAspect = (newLine[1].x - newLine[0].x) / (newLine[1].y - newLine[0].y); - - //if(lineAspect < boxAspect){ - // _line[1].x = args.x; - // _line[1].y = (_line[0].y - (_line[1].x - _line[0].x) / boxAspect); - //} - _line = newLine; _handle.x = _line[1].x - (_handle.width / 2.0f); _handle.y = _line[1].y - (_handle.height / 2.0f); - //_surface->scaleTo(_scale); - GuiWidgetEvent e; e.args = args; ofNotifyEvent(guiWidgetEvent, e, this); @@ -150,9 +157,14 @@ void ScaleWidget::setRect(ofRectangle rect){ _handle.y = rect.y - (_handle.height / 2.0f); } -void ScaleWidget::setSurface(ofx::piMapper::BaseSurface * s){ - _surface = s; - setRect(s->getBoundingBox()); +void ScaleWidget::setSurfaceManager(SurfaceManager * sm){ + _surfaceManager = sm; + + if(_surfaceManager->getSelectedSurface() == 0){ + return; + } + + setRect(_surfaceManager->getSelectedSurface()->getBoundingBox()); } } // namespace piMapper diff --git a/src/Gui/Widgets/ScaleWidget.h b/src/Gui/Widgets/ScaleWidget.h index 39b7ee6..b95e5e7 100644 --- a/src/Gui/Widgets/ScaleWidget.h +++ b/src/Gui/Widgets/ScaleWidget.h @@ -5,6 +5,7 @@ #include "GuiBaseWidget.h" #include "ofGraphics.h" #include "BaseSurface.h" +#include "SurfaceManager.h" namespace ofx { namespace piMapper { @@ -24,7 +25,7 @@ class ScaleWidget : public GuiBaseWidget { bool inside(float x, float y); // This should be the size of the objet's bounding box - void setSurface(BaseSurface * s); + void setSurfaceManager(SurfaceManager * sm); float getScale(){ return _scale; @@ -39,7 +40,8 @@ class ScaleWidget : public GuiBaseWidget { bool _dragging; - BaseSurface * _surface; + BaseSurface * _selectedSurface; + SurfaceManager * _surfaceManager; void setRect(ofRectangle rect); }; diff --git a/src/Surfaces/SurfaceManager.cpp b/src/Surfaces/SurfaceManager.cpp index d96b401..da1ca41 100644 --- a/src/Surfaces/SurfaceManager.cpp +++ b/src/Surfaces/SurfaceManager.cpp @@ -99,6 +99,8 @@ BaseSurface * SurfaceManager::selectSurface(int index){ } BaseSurface * SurfaceManager::selectSurface(BaseSurface * surface){ + cout << "SurfaceManager::selectSurface()" << endl; + for(int i = 0; i < SurfaceStack::instance()->size(); i++){ if(SurfaceStack::instance()->at(i) == surface){ selectedSurface = surface; diff --git a/src/ofxPiMapper.cpp b/src/ofxPiMapper.cpp index e313d7a..734397d 100644 --- a/src/ofxPiMapper.cpp +++ b/src/ofxPiMapper.cpp @@ -6,6 +6,10 @@ void ofxPiMapper::setup(){ _application.setup(); } +void ofxPiMapper::update(){ + _application.update(); +} + void ofxPiMapper::draw(){ _application.draw(); } diff --git a/src/ofxPiMapper.h b/src/ofxPiMapper.h index a593446..64c3990 100644 --- a/src/ofxPiMapper.h +++ b/src/ofxPiMapper.h @@ -23,6 +23,7 @@ class ofxPiMapper { ofxPiMapper(); void setup(); + void update(); void draw(); void registerFboSource(ofx::piMapper::FboSource & fboSource);