Browse Source

Add primitive and buggy ScaleWidget functionality

master
Krisjanis Rijnieks 9 years ago
parent
commit
8ad0a307e4
  1. 2
      src/Application/GuiBaseWidget.h
  2. 10
      src/Application/ProjectionMappingState.cpp
  3. 35
      src/Application/ScaleWidget.cpp
  4. 12
      src/Application/ScaleWidget.h
  5. 4
      src/Commands/ScaleSurfaceDnCmd.cpp
  6. 4
      src/Commands/ScaleSurfaceUpCmd.cpp
  7. 7
      src/Surfaces/BaseSurface.cpp
  8. 8
      src/Surfaces/BaseSurface.h
  9. 6
      src/Surfaces/SurfaceManagerGui.cpp

2
src/Application/GuiBaseWidget.h

@ -21,6 +21,8 @@ class GuiBaseWidget {
virtual bool inside(float x, float y) = 0; virtual bool inside(float x, float y) = 0;
virtual float getScale(){}
ofEvent <GuiWidgetEvent> guiWidgetEvent; ofEvent <GuiWidgetEvent> guiWidgetEvent;
}; };

10
src/Application/ProjectionMappingState.cpp

@ -16,12 +16,10 @@ void ProjectionMappingState::draw(Application * app){
app->getGui()->draw(); 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(); BaseSurface * selectedSurface = app->getSurfaceManager()->getSelectedSurface();
if(selectedSurface != 0){ if(selectedSurface != 0){
ofRectangle boundingBox = selectedSurface->getBoundingBox();
Gui::instance()->getScaleWidget().setRect(boundingBox);
Gui::instance()->getScaleWidget().draw(); Gui::instance()->getScaleWidget().draw();
} }
@ -292,6 +290,9 @@ void ProjectionMappingState::onKeyPressed(Application * app, ofKeyEventArgs & ar
case '-': // Scale surface down case '-': // Scale surface down
if(app->getSurfaceManager()->getSelectedSurface() != 0){ if(app->getSurfaceManager()->getSelectedSurface() != 0){
if(app->getSurfaceManager()->getSelectedSurface()->getScale() <= 0.21f){
break;
}
app->getCmdManager()->exec( app->getCmdManager()->exec(
new ScaleSurfaceDnCmd( new ScaleSurfaceDnCmd(
app->getSurfaceManager()->getSelectedSurface(), 0.2f)); app->getSurfaceManager()->getSelectedSurface(), 0.2f));
@ -325,6 +326,7 @@ void ProjectionMappingState::onJointPressed(Application * app, GuiJointEvent & e
void ProjectionMappingState::onSurfacePressed(Application * app, GuiSurfaceEvent & e){ void ProjectionMappingState::onSurfacePressed(Application * app, GuiSurfaceEvent & e){
if(app->getSurfaceManager()->getSelectedSurface() != e.surface){ if(app->getSurfaceManager()->getSelectedSurface() != e.surface){
app->getCmdManager()->exec(new SelSurfaceCmd(app->getSurfaceManager(), 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)); app->getCmdManager()->exec(new StartDragSurfaceCmd(e.surface));
@ -342,6 +344,8 @@ void ProjectionMappingState::onGuiEvent(Application * app, GuiEvent & e){
cout << "Scale Released" << endl; cout << "Scale Released" << endl;
}else if(e.args.type == e.args.Dragged){ }else if(e.args.type == e.args.Dragged){
cout << "Scale Dragged" << endl; cout << "Scale Dragged" << endl;
cout << "Scale: " << e.widget->getScale() << endl;
app->getSurfaceManager()->getSelectedSurface()->scaleTo(e.widget->getScale());
} }
} }
} }

35
src/Application/ScaleWidget.cpp

@ -11,6 +11,7 @@ ScaleWidget::ScaleWidget(){
_handle.height = 20; _handle.height = 20;
_scale = 1.0f; _scale = 1.0f;
_surface = 0;
} }
void ScaleWidget::setup(){ void ScaleWidget::setup(){
@ -39,7 +40,6 @@ void ScaleWidget::draw(){
void ScaleWidget::onMousePressed(ofMouseEventArgs & args){ void ScaleWidget::onMousePressed(ofMouseEventArgs & args){
if(_handle.inside(args.x, args.y)){ if(_handle.inside(args.x, args.y)){
_dragging = true; _dragging = true;
_originalLine = _line;
GuiWidgetEvent e; GuiWidgetEvent e;
e.args = args; e.args = args;
@ -59,11 +59,33 @@ void ScaleWidget::onMouseReleased(ofMouseEventArgs & args){
void ScaleWidget::onMouseDragged(ofMouseEventArgs & args){ void ScaleWidget::onMouseDragged(ofMouseEventArgs & args){
if(_dragging){ if(_dragging){
_handle.x = args.x - (_handle.width / 2.0f); if(_surface == 0){
_handle.y = args.y - (_handle.height / 2.0f); cout << "No surface selected" << endl;
return;
}
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].x = args.x;
_line[1].y = args.y; _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; GuiWidgetEvent e;
e.args = args; e.args = args;
@ -93,5 +115,10 @@ void ScaleWidget::setRect(ofRectangle rect){
_handle.y = rect.y - (_handle.height / 2.0f); _handle.y = rect.y - (_handle.height / 2.0f);
} }
void ScaleWidget::setSurface(ofx::piMapper::BaseSurface * s){
_surface = s;
setRect(s->getBoundingBox());
}
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

12
src/Application/ScaleWidget.h

@ -4,6 +4,7 @@
#include "ofPolyline.h" #include "ofPolyline.h"
#include "GuiBaseWidget.h" #include "GuiBaseWidget.h"
#include "ofGraphics.h" #include "ofGraphics.h"
#include "BaseSurface.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
@ -23,19 +24,24 @@ class ScaleWidget : public GuiBaseWidget {
bool inside(float x, float y); bool inside(float x, float y);
// This should be the size of the objet's bounding box // 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: private:
ofRectangle _handle; ofRectangle _handle;
ofPolyline _line; ofPolyline _line;
ofPolyline _originalLine;
float _scale; float _scale;
bool _dragging; bool _dragging;
BaseSurface * _surface;
void setRect(ofRectangle rect);
}; };
} // namespace piMapper } // namespace piMapper

4
src/Commands/ScaleSurfaceDnCmd.cpp

@ -10,12 +10,12 @@ ScaleSurfaceDnCmd::ScaleSurfaceDnCmd(BaseSurface * selectedSurface, float by){
void ScaleSurfaceDnCmd::exec(){ void ScaleSurfaceDnCmd::exec(){
ofLogNotice("ScaleSurfaceDnCmd", "exec"); ofLogNotice("ScaleSurfaceDnCmd", "exec");
_selectedSurface->scaleTo(1.0f - _by); _selectedSurface->scaleTo(_selectedSurface->getScale() - _by);
} }
void ScaleSurfaceDnCmd::undo(){ void ScaleSurfaceDnCmd::undo(){
ofLogNotice("ScaleSurfaceCmd", "undo"); ofLogNotice("ScaleSurfaceCmd", "undo");
_selectedSurface->scaleTo(1.0f / (1.0f - _by)); _selectedSurface->scaleTo(_selectedSurface->getScale() + _by);
} }
} // namespace piMapper } // namespace piMapper

4
src/Commands/ScaleSurfaceUpCmd.cpp

@ -10,12 +10,12 @@ ScaleSurfaceUpCmd::ScaleSurfaceUpCmd(BaseSurface * selectedSurface, float by){
void ScaleSurfaceUpCmd::exec(){ void ScaleSurfaceUpCmd::exec(){
ofLogNotice("ScaleSurfaceUpCmd", "exec"); ofLogNotice("ScaleSurfaceUpCmd", "exec");
_selectedSurface->scaleTo(1.0f + _by); _selectedSurface->scaleTo(_selectedSurface->getScale() + _by);
} }
void ScaleSurfaceUpCmd::undo(){ void ScaleSurfaceUpCmd::undo(){
ofLogNotice("ScaleSurfaceUpCmd", "undo"); ofLogNotice("ScaleSurfaceUpCmd", "undo");
_selectedSurface->scaleTo(1.0f / (1.0f + _by)); _selectedSurface->scaleTo(_selectedSurface->getScale() - _by);
} }
} // namespace piMapper } // namespace piMapper

7
src/Surfaces/BaseSurface.cpp

@ -5,6 +5,7 @@ namespace piMapper {
BaseSurface::BaseSurface(){ BaseSurface::BaseSurface(){
_moved = false; _moved = false;
_scale = 1.0f;
createDefaultTexture(); createDefaultTexture();
} }
@ -95,13 +96,15 @@ void BaseSurface::setMoved(bool moved){
void BaseSurface::scaleTo(float scale){ void BaseSurface::scaleTo(float scale){
cout << "TriangleSurface::scaleTo()" << endl; cout << "TriangleSurface::scaleTo()" << endl;
ofVec3f centroid = mesh.getCentroid(); ofPoint centroid = getBoundingBox().getCenter();
for(unsigned int i = 0; i < mesh.getVertices().size(); ++i){ 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; d *= scale;
mesh.getVertices()[i] = centroid + d; mesh.getVertices()[i] = centroid + d;
} }
_scale = scale;
ofNotifyEvent(verticesChangedEvent, mesh.getVertices(), this); ofNotifyEvent(verticesChangedEvent, mesh.getVertices(), this);
} }

8
src/Surfaces/BaseSurface.h

@ -45,6 +45,8 @@ class BaseSurface {
bool getMoved(); bool getMoved();
float getScale(){ return _scale; }
ofMesh & getMesh(); ofMesh & getMesh();
ofRectangle & getBoundingBox(); ofRectangle & getBoundingBox();
@ -53,13 +55,19 @@ class BaseSurface {
protected: protected:
ofMesh mesh; ofMesh mesh;
ofRectangle _boundingBox; ofRectangle _boundingBox;
ofTexture defaultTexture; ofTexture defaultTexture;
BaseSource * source; BaseSource * source;
BaseSource * defaultSource; BaseSource * defaultSource;
void createDefaultTexture(); void createDefaultTexture();
bool _moved; bool _moved;
float _scale;
}; };
} // namespace piMapper } // namespace piMapper

6
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->select();
hitJoint->startDrag(); hitJoint->startDrag();
Gui::instance()->notifyJointPressed(args, hitJointIndex); Gui::instance()->notifyJointPressed(args, hitJointIndex);
@ -166,8 +168,6 @@ void SurfaceManagerGui::mousePressed(ofMouseEventArgs & args){
clickPosition = ofVec2f(args.x, args.y); clickPosition = ofVec2f(args.x, args.y);
startDrag(); // TODO: Should be something like `hitSurface->startDrag()` startDrag(); // TODO: Should be something like `hitSurface->startDrag()`
Gui::instance()->notifySurfacePressed(args, hitSurface); Gui::instance()->notifySurfacePressed(args, hitSurface);
}else if(Gui::instance()->getScaleWidget().inside(args.x, args.y)){
//
}else{ }else{
Gui::instance()->notifyBackgroundPressed(args); Gui::instance()->notifyBackgroundPressed(args);
} }

Loading…
Cancel
Save