Skip to content

Commit

Permalink
Reimplemented the closest port snap feature for #698
Browse files Browse the repository at this point in the history
  • Loading branch information
highperformancecoder committed Nov 16, 2017
1 parent 09aaaca commit b87d738
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 25 deletions.
5 changes: 0 additions & 5 deletions engine/evalOp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
#define finite isfinite
#endif

namespace
{
inline double sqr(double x) {return x*x;}
}

namespace minsky
{

Expand Down
2 changes: 2 additions & 0 deletions geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace minsky
typedef boost::geometry::model::ring<Point> Polygon;
typedef boost::geometry::model::box<Point> Rectangle;

template <class T> inline T sqr(T x) {return x*x;}

#ifndef M_PI
static const float M_PI = 3.1415926535f;
#endif
Expand Down
25 changes: 22 additions & 3 deletions model/canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,35 @@ namespace minsky
}
}

shared_ptr<Port> Canvas::closestInPort(float x, float y) const
{
shared_ptr<Port> closestPort;
auto minD=numeric_limits<float>::max();
model->recursiveDo(&GroupItems::items,
[&](const Items&, Items::const_iterator i)
{
if ((*i)->group.lock()->displayContents())
for (auto& p: (*i)->ports)
{
float d=sqr(p->x()-x)+sqr(p->y()-y);
if (d<minD)
{
minD=d;
closestPort=p;
}
}
return false;
});
return closestPort;
}

void Canvas::mouseUp(float x, float y)
{
mouseMove(x,y);
y+=yoffs;
if (fromPort.get())
{
if (auto dest=model->findAny(&Group::items,
[&](const ItemPtr& i){return i->contains(x,y);}))
if (auto to=dest->closestInPort(x,y))
if (auto to=closestInPort(x,y))
model->addWire(fromPort,to);
fromPort.reset();
}
Expand Down
3 changes: 3 additions & 0 deletions model/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ namespace minsky
void mouseUp(float x, float y);
void mouseMove(float x, float y);

/// return closest visible port to (x,y). nullptr is nothing suitable
std::shared_ptr<Port> closestInPort(float x, float y) const;

/// select all items in a given region
void select(float x0, float y0, float x1, float y1) {
select(LassoBox(x0,y0,x1,y1));
Expand Down
4 changes: 0 additions & 4 deletions model/group.cc
Original file line number Diff line number Diff line change
Expand Up @@ -689,10 +689,6 @@ namespace minsky
}


namespace {
inline float sqr(float x) {return x*x;}
}

void Group::draw(cairo_t* cairo) const
{
double angle=rotation * M_PI / 180.0;
Expand Down
5 changes: 0 additions & 5 deletions model/item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,6 @@ namespace minsky
}
}

namespace
{
inline float sqr(float x) {return x*x;}
}

shared_ptr<Port> Item::closestOutPort(float x, float y) const
{
if (auto v=select(x,y))
Expand Down
2 changes: 0 additions & 2 deletions model/operation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ using namespace ecolab;

namespace
{
inline double sqr(double x) {return x*x;}

struct DrawBinOp
{
cairo_t *cairo;
Expand Down
6 changes: 0 additions & 6 deletions model/wire.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ using namespace std;
// undocumented internal function in the Tk library
extern "C" int TkMakeBezierCurve(Tk_Canvas,double*,int,int,void*,double*);

namespace
{
inline float sqr(float x) {return x*x;}
}

namespace minsky
{
vector<float> Wire::_coords() const
Expand Down Expand Up @@ -240,7 +235,6 @@ namespace minsky

namespace
{
inline float sqr(float x) {return x*x;}

// returns true if x,y lies close to the line segment (x0,y0)-(x1,y1)
bool segNear(float x0, float y0, float x1, float y1, float x, float y)
Expand Down

0 comments on commit b87d738

Please sign in to comment.