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. 37
      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 float getScale(){}
ofEvent <GuiWidgetEvent> guiWidgetEvent;
};

10
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());
}
}
}

37
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

12
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

4
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

4
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

7
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);
}

8
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

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->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);
}

Loading…
Cancel
Save