Skip to content

Commit

Permalink
Merge pull request #199 from Josephblt/issues166-140
Browse files Browse the repository at this point in the history
Issue #166 - Issue #140
  • Loading branch information
SprocketNYC authored Oct 26, 2017
2 parents 6543a72 + 3558983 commit 4c0c955
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 68 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/ait/lienzo/client/core/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class Attribute

public static final Attribute CORNER_RADIUS = new Attribute("cornerRadius", MESSAGES.cornerRadiusLabel(), MESSAGES.cornerRadiusDescription(), AttributeType.NUMBER_TYPE, true);

public static final Attribute SIZE_CONSTRAINTS = new Attribute("sizeConstraints", MESSAGES.sizeConstraintsLabel(), MESSAGES.sizeConstraintsDescription(), AttributeType.NUMBER_TYPE, true);

public static final Attribute FILL = new Attribute("fill", MESSAGES.fillLabel(), MESSAGES.fillDescription(), AttributeType.FILL_TYPE, true);

public static final Attribute STROKE = new Attribute("stroke", MESSAGES.strokeLabel(), MESSAGES.strokeDescription(), AttributeType.STROKE_TYPE, true);
Expand Down Expand Up @@ -281,4 +283,4 @@ public final String toString()
{
return m_prop;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public interface MessageConstants extends Constants
@DefaultStringValue("The radius of a 90 degree arc, which is used as a rounded corner.")
public String cornerRadiusDescription();

@DefaultStringValue("Size constraints")
public String sizeConstraintsLabel();

@DefaultStringValue("Min/Max width and Min/Max height constraint values in pixels.")
public String sizeConstraintsDescription();

@DefaultStringValue("Fill")
public String fillLabel();

Expand Down Expand Up @@ -476,7 +482,7 @@ public interface MessageConstants extends Constants

@DefaultStringValue("If a shape should be filled for events on the selection layer.")
public String fillShapeForSelectionDescription();

@DefaultStringValue("Fill Shape Bounding Box For Selection")
public String fillBoundsForSelectionLabel();

Expand Down Expand Up @@ -674,4 +680,4 @@ public interface MessageConstants extends Constants

@DefaultStringValue("Arrow Ratio")
public String arrowRatioDescription();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
import com.ait.lienzo.client.core.types.PathPartList;
import com.ait.lienzo.client.core.types.Point2D;
import com.ait.lienzo.client.core.types.Point2DArray;
import com.ait.lienzo.client.core.util.Geometry;
import com.ait.lienzo.client.widget.DragConstraintEnforcer;
import com.ait.lienzo.client.widget.DragContext;
import com.ait.lienzo.shared.core.types.ColorName;
import com.ait.lienzo.shared.core.types.DragMode;
import com.ait.lienzo.shared.core.types.ShapeType;
Expand All @@ -66,6 +69,7 @@
public abstract class AbstractMultiPathPartShape<T extends AbstractMultiPathPartShape<T>> extends Shape<T>
{
private final NFastArrayList<PathPartList> m_points = new NFastArrayList<PathPartList>();
private NFastArrayList<PathPartList> m_cornerPoints = new NFastArrayList<PathPartList>();

protected AbstractMultiPathPartShape(final ShapeType type)
{
Expand All @@ -80,7 +84,13 @@ protected AbstractMultiPathPartShape(final ShapeType type, final JSONObject node
@Override
public BoundingBox getBoundingBox()
{
final int size = m_points.size();
NFastArrayList<PathPartList> points = m_points;

if (getCornerRadius() > 0)
{
points = m_cornerPoints;
}
final int size = points.size();

if (size < 1)
{
Expand All @@ -90,7 +100,7 @@ public BoundingBox getBoundingBox()

for (int i = 0; i < size; i++)
{
bbox.add(m_points.get(i).getBoundingBox());
bbox.add(points.get(i).getBoundingBox());
}
return bbox;
}
Expand All @@ -114,6 +124,31 @@ public T clear()
return cast();
}

@Override
protected boolean prepare(Context2D context, Attributes attr, double alpha)
{
double radius = getCornerRadius();

if (radius != 0)
{
m_cornerPoints = new NFastArrayList<PathPartList>();

for (int i = 0; i < m_points.size(); i++)
{
PathPartList baseList = m_points.get(i);

Point2DArray basePoints = baseList.getPoints();

PathPartList cornerList = new PathPartList();

Geometry.drawArcJoinedLines(cornerList, baseList, basePoints, radius);

m_cornerPoints.add(cornerList);
}
}
return true;
}

protected final void add(final PathPartList list)
{
m_points.add(list);
Expand All @@ -124,6 +159,18 @@ public final NFastArrayList<PathPartList> getPathPartListArray()
return m_points;
}

public final NFastArrayList<PathPartList> getActualPathPartListArray()
{
if (getCornerRadius() > 0)
{
return m_cornerPoints;
}
else
{
return m_points;
}
}

@Override
protected void drawWithoutTransforms(final Context2D context, double alpha, BoundingBox bounds)
{
Expand All @@ -137,7 +184,13 @@ protected void drawWithoutTransforms(final Context2D context, double alpha, Boun
}
if (prepare(context, attr, alpha))
{
final int size = m_points.size();
NFastArrayList<PathPartList> points = m_points;

if (getCornerRadius() > 0)
{
points = m_cornerPoints;
}
final int size = points.size();

if (size < 1)
{
Expand All @@ -147,7 +200,7 @@ protected void drawWithoutTransforms(final Context2D context, double alpha, Boun
{
setAppliedShadow(false);

final PathPartList list = m_points.get(i);
final PathPartList list = points.get(i);

if (list.size() > 1)
{
Expand All @@ -163,6 +216,28 @@ protected void drawWithoutTransforms(final Context2D context, double alpha, Boun
}
}

public BoundingBox getSizeConstraints()
{
return getAttributes().getSizeConstraints();
}

public T setSizeConstraints(final BoundingBox sizeConstraints)
{
getAttributes().setSizeConstraints(sizeConstraints);
return refresh();
}

public double getCornerRadius()
{
return getAttributes().getCornerRadius();
}

public T setCornerRadius(final double radius)
{
getAttributes().setCornerRadius(radius);
return refresh();
}

@Override
public IControlHandleFactory getControlHandleFactory()
{
Expand Down Expand Up @@ -669,11 +744,11 @@ public void init()
{
ResizeHandleDragHandler topRightHandler = new ResizeHandleDragHandler(m_shape, m_listOfPaths, m_chlist, m_prim, this);

register(m_prim.addNodeDragMoveHandler(topRightHandler));

register(m_prim.addNodeDragStartHandler(topRightHandler));
m_prim.setDragConstraints(topRightHandler);

register(m_prim.addNodeDragEndHandler(topRightHandler));


}

public int getPosition()
Expand Down Expand Up @@ -825,7 +900,7 @@ void updateOtherHandles(double dx, double dy, double offsetX, double offsetY, do
}
}

public static class ResizeHandleDragHandler implements NodeDragStartHandler, NodeDragMoveHandler, NodeDragEndHandler
public static class ResizeHandleDragHandler implements DragConstraintEnforcer, NodeDragEndHandler
{
private final Shape<?> m_shape;

Expand Down Expand Up @@ -865,7 +940,7 @@ public ResizeHandleDragHandler(Shape<?> shape, NFastArrayList<PathPartList> list
}

@Override
public void onNodeDragStart(NodeDragStartEvent event)
public void startDrag(DragContext dragContext)
{
BoundingBox box = m_shape.getBoundingBox();

Expand Down Expand Up @@ -997,13 +1072,14 @@ public int compare(ResizeControlHandle h1, ResizeControlHandle h2)
}

@Override
public void onNodeDragMove(NodeDragMoveEvent event)
public boolean adjust(Point2D dxy)
{
if ((m_handle.isActive()) && (m_chlist.isActive()))
{
double dx = event.getDragContext().getDistanceAdjusted().getX();

double dy = event.getDragContext().getDistanceAdjusted().getY();
if (!adjustPrimitive(dxy))
{
return false;
}

for (PathPartList list : m_listOfPaths)
{
Expand All @@ -1020,25 +1096,29 @@ public void onNodeDragMove(NodeDragMoveEvent event)
{
NFastDoubleArrayJSO doubles = m_entries.get(i);
double x = doubles.get(0);
double newX = m_handle.getX(m_boxStartX, m_boxStartY, m_boxStartWidth, m_boxStartHeight, x, dx);
double newX = m_handle.getX(m_boxStartX, m_boxStartY, m_boxStartWidth, m_boxStartHeight, x, dxy.getX());

double y = doubles.get(1);
double newY = m_handle.getY(m_boxStartX, m_boxStartY, m_boxStartWidth, m_boxStartHeight, y, dy);
double newY = m_handle.getY(m_boxStartX, m_boxStartY, m_boxStartWidth, m_boxStartHeight, y, dxy.getY());

points.set(0, newX);
points.set(1, newY);

break;
}
}
}
list.resetBoundingBox();
}
m_handle.updateOtherHandles(dx, dy, m_offsetX, m_offsetY, m_boxStartX, m_boxStartY, m_boxStartWidth, m_boxStartHeight);

m_handle.updateOtherHandles(dxy.getX(), dxy.getY(), m_offsetX, m_offsetY, m_boxStartX, m_boxStartY, m_boxStartWidth, m_boxStartHeight);

m_shape.refresh();

m_shape.getLayer().batch();
}

return true;
}

@Override
Expand Down Expand Up @@ -1083,5 +1163,117 @@ private void copyDoubles()
}
}
}

public boolean adjustPrimitive(Point2D dxy)
{
BoundingBox sizeConstraints = m_shape.getAttributes().getSizeConstraints();

if (sizeConstraints == null)
{
return true;
}

double minWidth = sizeConstraints.getMinX();

double maxWidth = sizeConstraints.getMaxX();

double minHeight = sizeConstraints.getMinY();

double maxHeight = sizeConstraints.getMaxY();

Point2D adjustedDelta = adjustForPosition(dxy);

double adjustedX = adjustedDelta.getX();

double adjustedY = adjustedDelta.getY();

double width = m_boxStartWidth + adjustedX;

double height = m_boxStartHeight + adjustedY;

boolean needsAdjustment = false;

if (width < minWidth)
{
double difference = width - minWidth;

adjustedDelta.setX(adjustedX - difference);
}
else
{
needsAdjustment = true;
}

if (width > maxWidth)
{
double difference = width - maxWidth;

adjustedDelta.setX(adjustedX - difference);
}
else
{
needsAdjustment = true;
}

if (height < minHeight)
{
double difference = height - minHeight;

adjustedDelta.setY(adjustedY - difference);
}
else
{
needsAdjustment = true;
}

if (height > maxHeight)
{
double difference = height - maxHeight;

adjustedDelta.setY(adjustedY - difference);
}
else
{
needsAdjustment = true;
}

adjustedDelta = adjustForPosition(adjustedDelta);

dxy.setX(adjustedDelta.getX());

dxy.setY(adjustedDelta.getY());

return needsAdjustment;
}

private Point2D adjustForPosition(Point2D dxy)
{
Point2D adjustedDXY = dxy.copy();

double x = adjustedDXY.getX();

double y = adjustedDXY.getY();

switch (m_handle.getPosition())
{
case 0: //tl
x *= -1;
y *= -1;
break;
case 1: //tr
y *= -1;
break;
case 2: //br
break;
case 3: //bl
x *= -1;
break;
}

adjustedDXY.setX(x);
adjustedDXY.setY(y);

return adjustedDXY;
}
}
}
}
Loading

0 comments on commit 4c0c955

Please sign in to comment.