From 8ad0a307e41467e470b5befa8176e6be72e534b5 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Mon, 22 Aug 2016 22:35:31 +0300 Subject: [PATCH] Add primitive and buggy ScaleWidget functionality --- src/Application/GuiBaseWidget.h | 2 ++ src/Application/ProjectionMappingState.cpp | 10 ++++-- src/Application/ScaleWidget.cpp | 37 +++++++++++++++++++--- src/Application/ScaleWidget.h | 12 +++++-- src/Commands/ScaleSurfaceDnCmd.cpp | 4 +-- src/Commands/ScaleSurfaceUpCmd.cpp | 4 +-- src/Surfaces/BaseSurface.cpp | 7 ++-- src/Surfaces/BaseSurface.h | 10 +++++- src/Surfaces/SurfaceManagerGui.cpp | 6 ++-- 9 files changed, 71 insertions(+), 21 deletions(-) diff --git a/src/Application/GuiBaseWidget.h b/src/Application/GuiBaseWidget.h index 571d3f9..b16da44 100644 --- a/src/Application/GuiBaseWidget.h +++ b/src/Application/GuiBaseWidget.h @@ -21,6 +21,8 @@ class GuiBaseWidget { virtual bool inside(float x, float y) = 0; + virtual float getScale(){} + ofEvent guiWidgetEvent; }; diff --git a/src/Application/ProjectionMappingState.cpp b/src/Application/ProjectionMappingState.cpp index 2df897d..aa713c8 100644 --- a/src/Application/ProjectionMappingState.cpp +++ b/src/Application/ProjectionMappingState.cpp @@ -16,12 +16,10 @@ void ProjectionMappingState::draw(Application * app){ app->getGui()->draw(); /* - Draw scale widget. + Draw scale widget. The size of the widget is being set on surface select. */ BaseSurface * selectedSurface = app->getSurfaceManager()->getSelectedSurface(); if(selectedSurface != 0){ - ofRectangle boundingBox = selectedSurface->getBoundingBox(); - Gui::instance()->getScaleWidget().setRect(boundingBox); Gui::instance()->getScaleWidget().draw(); } @@ -292,6 +290,9 @@ void ProjectionMappingState::onKeyPressed(Application * app, ofKeyEventArgs & ar case '-': // Scale surface down if(app->getSurfaceManager()->getSelectedSurface() != 0){ + if(app->getSurfaceManager()->getSelectedSurface()->getScale() <= 0.21f){ + break; + } app->getCmdManager()->exec( new ScaleSurfaceDnCmd( app->getSurfaceManager()->getSelectedSurface(), 0.2f)); @@ -325,6 +326,7 @@ 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)); @@ -342,6 +344,8 @@ void ProjectionMappingState::onGuiEvent(Application * app, GuiEvent & e){ cout << "Scale Released" << endl; }else if(e.args.type == e.args.Dragged){ cout << "Scale Dragged" << endl; + cout << "Scale: " << e.widget->getScale() << endl; + app->getSurfaceManager()->getSelectedSurface()->scaleTo(e.widget->getScale()); } } } diff --git a/src/Application/ScaleWidget.cpp b/src/Application/ScaleWidget.cpp index 0dd681f..3e03ee8 100644 --- a/src/Application/ScaleWidget.cpp +++ b/src/Application/ScaleWidget.cpp @@ -11,6 +11,7 @@ ScaleWidget::ScaleWidget(){ _handle.height = 20; _scale = 1.0f; + _surface = 0; } void ScaleWidget::setup(){ @@ -39,7 +40,6 @@ void ScaleWidget::draw(){ void ScaleWidget::onMousePressed(ofMouseEventArgs & args){ if(_handle.inside(args.x, args.y)){ _dragging = true; - _originalLine = _line; GuiWidgetEvent e; e.args = args; @@ -59,11 +59,33 @@ void ScaleWidget::onMouseReleased(ofMouseEventArgs & args){ void ScaleWidget::onMouseDragged(ofMouseEventArgs & args){ if(_dragging){ - _handle.x = args.x - (_handle.width / 2.0f); - _handle.y = args.y - (_handle.height / 2.0f); + if(_surface == 0){ + cout << "No surface selected" << endl; + return; + } - _line[1].x = args.x; - _line[1].y = args.y; + ofRectangle box = _surface->getBoundingBox(); + float boxAspect = box.width / box.height; + + ofPolyline newLine = _line; + newLine[1].x = args.x; + newLine[1].y = args.y; + + _scale = _surface->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); + } + + _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; @@ -93,5 +115,10 @@ 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()); +} + } // namespace piMapper } // namespace ofx diff --git a/src/Application/ScaleWidget.h b/src/Application/ScaleWidget.h index 51ce803..146ad50 100644 --- a/src/Application/ScaleWidget.h +++ b/src/Application/ScaleWidget.h @@ -4,6 +4,7 @@ #include "ofPolyline.h" #include "GuiBaseWidget.h" #include "ofGraphics.h" +#include "BaseSurface.h" namespace ofx { namespace piMapper { @@ -23,19 +24,24 @@ class ScaleWidget : public GuiBaseWidget { bool inside(float x, float y); // This should be the size of the objet's bounding box - void setRect(ofRectangle rect); + void setSurface(BaseSurface * s); - float getScale(); + float getScale(){ + return _scale; + } private: ofRectangle _handle; ofPolyline _line; - ofPolyline _originalLine; float _scale; bool _dragging; + + BaseSurface * _surface; + + void setRect(ofRectangle rect); }; } // namespace piMapper diff --git a/src/Commands/ScaleSurfaceDnCmd.cpp b/src/Commands/ScaleSurfaceDnCmd.cpp index 621bf95..ace267a 100644 --- a/src/Commands/ScaleSurfaceDnCmd.cpp +++ b/src/Commands/ScaleSurfaceDnCmd.cpp @@ -10,12 +10,12 @@ ScaleSurfaceDnCmd::ScaleSurfaceDnCmd(BaseSurface * selectedSurface, float by){ void ScaleSurfaceDnCmd::exec(){ ofLogNotice("ScaleSurfaceDnCmd", "exec"); - _selectedSurface->scaleTo(1.0f - _by); + _selectedSurface->scaleTo(_selectedSurface->getScale() - _by); } void ScaleSurfaceDnCmd::undo(){ ofLogNotice("ScaleSurfaceCmd", "undo"); - _selectedSurface->scaleTo(1.0f / (1.0f - _by)); + _selectedSurface->scaleTo(_selectedSurface->getScale() + _by); } } // namespace piMapper diff --git a/src/Commands/ScaleSurfaceUpCmd.cpp b/src/Commands/ScaleSurfaceUpCmd.cpp index 3465753..9c93bc3 100644 --- a/src/Commands/ScaleSurfaceUpCmd.cpp +++ b/src/Commands/ScaleSurfaceUpCmd.cpp @@ -10,12 +10,12 @@ ScaleSurfaceUpCmd::ScaleSurfaceUpCmd(BaseSurface * selectedSurface, float by){ void ScaleSurfaceUpCmd::exec(){ ofLogNotice("ScaleSurfaceUpCmd", "exec"); - _selectedSurface->scaleTo(1.0f + _by); + _selectedSurface->scaleTo(_selectedSurface->getScale() + _by); } void ScaleSurfaceUpCmd::undo(){ ofLogNotice("ScaleSurfaceUpCmd", "undo"); - _selectedSurface->scaleTo(1.0f / (1.0f + _by)); + _selectedSurface->scaleTo(_selectedSurface->getScale() - _by); } } // namespace piMapper diff --git a/src/Surfaces/BaseSurface.cpp b/src/Surfaces/BaseSurface.cpp index c95e8a1..039bf81 100644 --- a/src/Surfaces/BaseSurface.cpp +++ b/src/Surfaces/BaseSurface.cpp @@ -5,6 +5,7 @@ namespace piMapper { BaseSurface::BaseSurface(){ _moved = false; + _scale = 1.0f; createDefaultTexture(); } @@ -95,13 +96,15 @@ void BaseSurface::setMoved(bool moved){ void BaseSurface::scaleTo(float scale){ cout << "TriangleSurface::scaleTo()" << endl; - ofVec3f centroid = mesh.getCentroid(); + ofPoint centroid = getBoundingBox().getCenter(); for(unsigned int i = 0; i < mesh.getVertices().size(); ++i){ - ofVec3f d = mesh.getVertices()[i] - centroid; + ofVec3f d = (mesh.getVertices()[i] - centroid) / _scale; d *= scale; mesh.getVertices()[i] = centroid + d; } + _scale = scale; + ofNotifyEvent(verticesChangedEvent, mesh.getVertices(), this); } diff --git a/src/Surfaces/BaseSurface.h b/src/Surfaces/BaseSurface.h index 49c2040..b554d7b 100644 --- a/src/Surfaces/BaseSurface.h +++ b/src/Surfaces/BaseSurface.h @@ -45,6 +45,8 @@ class BaseSurface { bool getMoved(); + float getScale(){ return _scale; } + ofMesh & getMesh(); ofRectangle & getBoundingBox(); @@ -53,13 +55,19 @@ class BaseSurface { protected: ofMesh mesh; + ofRectangle _boundingBox; + ofTexture defaultTexture; + BaseSource * source; BaseSource * defaultSource; + void createDefaultTexture(); + bool _moved; - + + float _scale; }; } // namespace piMapper diff --git a/src/Surfaces/SurfaceManagerGui.cpp b/src/Surfaces/SurfaceManagerGui.cpp index 8efdd67..6bd4eec 100644 --- a/src/Surfaces/SurfaceManagerGui.cpp +++ b/src/Surfaces/SurfaceManagerGui.cpp @@ -158,7 +158,9 @@ void SurfaceManagerGui::mousePressed(ofMouseEventArgs & args){ } } - if(hitJoint){ + if(Gui::instance()->getScaleWidget().inside(args.x, args.y)){ + // + }else if(hitJoint){ hitJoint->select(); hitJoint->startDrag(); Gui::instance()->notifyJointPressed(args, hitJointIndex); @@ -166,8 +168,6 @@ void SurfaceManagerGui::mousePressed(ofMouseEventArgs & args){ clickPosition = ofVec2f(args.x, args.y); startDrag(); // TODO: Should be something like `hitSurface->startDrag()` Gui::instance()->notifySurfacePressed(args, hitSurface); - }else if(Gui::instance()->getScaleWidget().inside(args.x, args.y)){ - // }else{ Gui::instance()->notifyBackgroundPressed(args); }