SnapToGeometry.js
Summary
No overview generated for 'SnapToGeometry.js'
draw2d.SnapToGeometry=function( workflow)
{
draw2d.SnapToHelper.call(this,workflow);
}
draw2d.SnapToGeometry.prototype = new draw2d.SnapToHelper;
draw2d.SnapToGeometry.THRESHOLD = 5.0;
draw2d.SnapToGeometry.prototype.snapPoint=function( snapOrientation, inputPoint, resultPoint)
{
if(this.rows==null || this.cols==null)
this.populateRowsAndCols();
if ((snapOrientation & draw2d.SnapToHelper.EAST) != 0)
{
var rightCorrection = this.getCorrectionFor(this.cols, inputPoint.getX() - 1, 1);
if (rightCorrection != draw2d.SnapToGeometry.THRESHOLD)
{
snapOrientation &= ~draw2d.SnapToHelper.EAST;
resultPoint.x += rightCorrection;
}
}
if ((snapOrientation & draw2d.SnapToHelper.WEST) != 0)
{
var leftCorrection = this.getCorrectionFor(this.cols, inputPoint.getX(), -1);
if (leftCorrection != draw2d.SnapToGeometry.THRESHOLD)
{
snapOrientation &= ~draw2d.SnapToHelper.WEST;
resultPoint.x += leftCorrection;
}
}
if ((snapOrientation & draw2d.SnapToHelper.SOUTH) != 0)
{
var bottomCorrection = this.getCorrectionFor(this.rows, inputPoint.getY() - 1, 1);
if (bottomCorrection != draw2d.SnapToGeometry.THRESHOLD)
{
snapOrientation &= ~draw2d.SnapToHelper.SOUTH;
resultPoint.y += bottomCorrection;
}
}
if ((snapOrientation & draw2d.SnapToHelper.NORTH) != 0)
{
var topCorrection = this.getCorrectionFor(this.rows, inputPoint.getY(), -1);
if (topCorrection != draw2d.SnapToGeometry.THRESHOLD)
{
snapOrientation &= ~draw2d.SnapToHelper.NORTH;
resultPoint.y += topCorrection;
}
}
return snapOrientation;
}
draw2d.SnapToGeometry.prototype.snapRectangle=function( inputBounds, resultBounds)
{
var topLeftResult = inputBounds.getTopLeft();
var bottomRightResult = inputBounds.getBottomRight();
var snapDirectionsTopLeft = this.snapPoint(draw2d.SnapToHelper.NORTH_WEST, inputBounds.getTopLeft(), topLeftResult);
resultBounds.x = topLeftResult.x;
resultBounds.y = topLeftResult.y
var snapDirectionsBottomRight = this.snapPoint(draw2d.SnapToHelper.SOUTH_EAST, inputBounds.getBottomRight(), bottomRightResult);
if(snapDirectionsTopLeft & draw2d.SnapToHelper.WEST)
resultBounds.x = bottomRightResult.x-inputBounds.getWidth();
if(snapDirectionsTopLeft & draw2d.SnapToHelper.NORTH)
resultBounds.y = bottomRightResult.y-inputBounds.getHeight();
return snapDirectionsTopLeft |snapDirectionsBottomRight;
}
draw2d.SnapToGeometry.prototype.populateRowsAndCols=function()
{
this.rows = new Array();
this.cols = new Array();
var figures = this.workflow.getDocument().getFigures();
var index =0;
for (var i = 0; i < figures.getSize();i++ )
{
var figure = figures.get(i);
if(figure != this.workflow.getCurrentSelection())
{
var bounds = figure.getBounds();
this.cols[index * 3] = new draw2d.SnapToGeometryEntry(-1, bounds.getX());
this.rows[index * 3] = new draw2d.SnapToGeometryEntry(-1, bounds.getY());
this.cols[index * 3 + 1] = new draw2d.SnapToGeometryEntry(0, bounds.x + (bounds.getWidth() - 1) / 2);
this.rows[index * 3 + 1] = new draw2d.SnapToGeometryEntry(0, bounds.y + (bounds.getHeight() - 1) / 2);
this.cols[index * 3 + 2] = new draw2d.SnapToGeometryEntry(1, bounds.getRight() - 1);
this.rows[index * 3 + 2] = new draw2d.SnapToGeometryEntry(1, bounds.getBottom() - 1);
index++;
}
}
}
draw2d.SnapToGeometry.prototype.getCorrectionFor=function( entries, value, side)
{
var resultMag = draw2d.SnapToGeometry.THRESHOLD;
var result = draw2d.SnapToGeometry.THRESHOLD;
for (var i = 0; i < entries.length; i++)
{
var entry = entries[i];
var magnitude;
if (entry.type == -1 && side != 0)
{
magnitude = Math.abs(value - entry.location);
if (magnitude < resultMag)
{
resultMag = magnitude;
result = entry.location - value;
}
}
else if (entry.type == 0 && side == 0)
{
magnitude = Math.abs(value - entry.location);
if (magnitude < resultMag)
{
resultMag = magnitude;
result = entry.location - value;
}
}
else if (entry.type == 1 && side != 0)
{
magnitude = Math.abs(value - entry.location);
if (magnitude < resultMag)
{
resultMag = magnitude;
result = entry.location - value;
}
}
}
return result;
}
draw2d.SnapToGeometry.prototype.onSetDocumentDirty=function()
{
this.rows=null;
this.cols=null;
}
Documentation generated by
JSDoc on Thu Feb 7 23:45:47 2008