use of com.cburch.draw.model.Handle in project logisim-evolution by reds-heig.
the class ModelMoveHandleAction method undoSub.
@Override
void undoSub(CanvasModel model) {
Handle oldHandle = gesture.getHandle();
int dx = oldHandle.getX() - newHandle.getX();
int dy = oldHandle.getY() - newHandle.getY();
HandleGesture reverse = new HandleGesture(newHandle, dx, dy, 0);
model.moveHandle(reverse);
}
use of com.cburch.draw.model.Handle in project logisim-evolution by reds-heig.
the class Curve method getHandleArray.
private Handle[] getHandleArray(HandleGesture gesture) {
if (gesture == null) {
return new Handle[] { new Handle(this, p0), new Handle(this, p1), new Handle(this, p2) };
} else {
Handle g = gesture.getHandle();
int gx = g.getX() + gesture.getDeltaX();
int gy = g.getY() + gesture.getDeltaY();
Handle[] ret = { new Handle(this, p0), new Handle(this, p1), new Handle(this, p2) };
if (g.isAt(p0)) {
if (gesture.isShiftDown()) {
Location p = LineUtil.snapTo8Cardinals(p2, gx, gy);
ret[0] = new Handle(this, p);
} else {
ret[0] = new Handle(this, gx, gy);
}
} else if (g.isAt(p2)) {
if (gesture.isShiftDown()) {
Location p = LineUtil.snapTo8Cardinals(p0, gx, gy);
ret[2] = new Handle(this, p);
} else {
ret[2] = new Handle(this, gx, gy);
}
} else if (g.isAt(p1)) {
if (gesture.isShiftDown()) {
double x0 = p0.getX();
double y0 = p0.getY();
double x1 = p2.getX();
double y1 = p2.getY();
double midx = (x0 + x1) / 2;
double midy = (y0 + y1) / 2;
double dx = x1 - x0;
double dy = y1 - y0;
double[] p = LineUtil.nearestPointInfinite(gx, gy, midx, midy, midx - dy, midy + dx);
gx = (int) Math.round(p[0]);
gy = (int) Math.round(p[1]);
}
if (gesture.isAltDown()) {
double[] e0 = { p0.getX(), p0.getY() };
double[] e1 = { p2.getX(), p2.getY() };
double[] mid = { gx, gy };
double[] ct = CurveUtil.interpolate(e0, e1, mid);
gx = (int) Math.round(ct[0]);
gy = (int) Math.round(ct[1]);
}
ret[1] = new Handle(this, gx, gy);
}
return ret;
}
}
use of com.cburch.draw.model.Handle in project logisim-evolution by reds-heig.
the class SelectTool method draw.
@Override
public void draw(Canvas canvas, Graphics g) {
Selection selection = canvas.getSelection();
int action = curAction;
Location start = dragStart;
Location end = dragEnd;
HandleGesture gesture = null;
boolean drawHandles;
switch(action) {
case MOVE_ALL:
drawHandles = !dragEffective;
break;
case MOVE_HANDLE:
drawHandles = !dragEffective;
if (dragEffective)
gesture = curGesture;
break;
default:
drawHandles = true;
}
CanvasObject moveHandleObj = null;
if (gesture != null)
moveHandleObj = gesture.getHandle().getObject();
if (drawHandles) {
// unscale the coordinate system so that the stroke width isn't
// scaled
double zoom = 1.0;
Graphics gCopy = g.create();
if (gCopy instanceof Graphics2D) {
zoom = canvas.getZoomFactor();
if (zoom != 1.0) {
((Graphics2D) gCopy).scale(1.0 / zoom, 1.0 / zoom);
}
}
GraphicsUtil.switchToWidth(gCopy, 1);
int size = (int) Math.ceil(HANDLE_SIZE * Math.sqrt(zoom));
int offs = size / 2;
for (CanvasObject obj : selection.getSelected()) {
List<Handle> handles;
if (action == MOVE_HANDLE && obj == moveHandleObj) {
handles = obj.getHandles(gesture);
} else {
handles = obj.getHandles(null);
}
for (Handle han : handles) {
int x = han.getX();
int y = han.getY();
if (action == MOVE_ALL && dragEffective) {
Location delta = selection.getMovingDelta();
x += delta.getX();
y += delta.getY();
}
x = (int) Math.round(zoom * x);
y = (int) Math.round(zoom * y);
gCopy.clearRect(x - offs, y - offs, size, size);
gCopy.drawRect(x - offs, y - offs, size, size);
}
}
Handle selHandle = selection.getSelectedHandle();
if (selHandle != null) {
int x = selHandle.getX();
int y = selHandle.getY();
if (action == MOVE_ALL && dragEffective) {
Location delta = selection.getMovingDelta();
x += delta.getX();
y += delta.getY();
}
x = (int) Math.round(zoom * x);
y = (int) Math.round(zoom * y);
int[] xs = { x - offs, x, x + offs, x };
int[] ys = { y, y - offs, y, y + offs };
gCopy.setColor(Color.WHITE);
gCopy.fillPolygon(xs, ys, 4);
gCopy.setColor(Color.BLACK);
gCopy.drawPolygon(xs, ys, 4);
}
}
switch(action) {
case RECT_SELECT:
case RECT_TOGGLE:
if (dragEffective) {
// find rectangle currently to show
int x0 = start.getX();
int y0 = start.getY();
int x1 = end.getX();
int y1 = end.getY();
if (x1 < x0) {
int t = x0;
x0 = x1;
x1 = t;
}
if (y1 < y0) {
int t = y0;
y0 = y1;
y1 = t;
}
// make the region that's not being selected darker
int w = canvas.getWidth();
int h = canvas.getHeight();
g.setColor(RECT_SELECT_BACKGROUND);
g.fillRect(0, 0, w, y0);
g.fillRect(0, y0, x0, y1 - y0);
g.fillRect(x1, y0, w - x1, y1 - y0);
g.fillRect(0, y1, w, h - y1);
// now draw the rectangle
g.setColor(Color.GRAY);
g.drawRect(x0, y0, x1 - x0, y1 - y0);
}
break;
default:
break;
}
}
use of com.cburch.draw.model.Handle in project logisim-evolution by reds-heig.
the class SelectTool method mousePressed.
@Override
public void mousePressed(Canvas canvas, MouseEvent e) {
beforePressSelection = new ArrayList<CanvasObject>(canvas.getSelection().getSelected());
beforePressHandle = canvas.getSelection().getSelectedHandle();
int mx = e.getX();
int my = e.getY();
boolean shift = (e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0;
dragStart = Location.create(mx, my);
dragEffective = false;
dragEnd = dragStart;
lastMouseX = mx;
lastMouseY = my;
Selection selection = canvas.getSelection();
selection.setHandleSelected(null);
// see whether user is pressing within an existing handle
int halfSize = getHandleSize(canvas) / 2;
CanvasObject clicked = null;
for (CanvasObject shape : selection.getSelected()) {
List<Handle> handles = shape.getHandles(null);
for (Handle han : handles) {
int dx = han.getX() - mx;
int dy = han.getY() - my;
if (dx >= -halfSize && dx <= halfSize && dy >= -halfSize && dy <= halfSize) {
if (shape.canMoveHandle(han)) {
curAction = MOVE_HANDLE;
curGesture = new HandleGesture(han, 0, 0, e.getModifiersEx());
repaintArea(canvas);
return;
} else if (clicked == null) {
clicked = shape;
}
}
}
}
// see whether the user is clicking within a shape
if (clicked == null) {
clicked = getObjectAt(canvas.getModel(), e.getX(), e.getY(), false);
}
if (clicked != null) {
if (shift && selection.isSelected(clicked)) {
selection.setSelected(clicked, false);
curAction = IDLE;
} else {
if (!shift && !selection.isSelected(clicked)) {
selection.clearSelected();
}
selection.setSelected(clicked, true);
selection.setMovingShapes(selection.getSelected(), 0, 0);
curAction = MOVE_ALL;
}
repaintArea(canvas);
return;
}
clicked = getObjectAt(canvas.getModel(), e.getX(), e.getY(), true);
if (clicked != null && selection.isSelected(clicked)) {
if (shift) {
selection.setSelected(clicked, false);
curAction = IDLE;
} else {
selection.setMovingShapes(selection.getSelected(), 0, 0);
curAction = MOVE_ALL;
}
repaintArea(canvas);
return;
}
if (shift) {
curAction = RECT_TOGGLE;
} else {
selection.clearSelected();
curAction = RECT_SELECT;
}
repaintArea(canvas);
}
use of com.cburch.draw.model.Handle in project logisim-evolution by reds-heig.
the class SelectTool method cancelMousePress.
@Override
public void cancelMousePress(Canvas canvas) {
List<CanvasObject> before = beforePressSelection;
Handle handle = beforePressHandle;
beforePressSelection = null;
beforePressHandle = null;
if (before != null) {
curAction = IDLE;
Selection sel = canvas.getSelection();
sel.clearDrawsSuppressed();
sel.setMovingShapes(Collections.<CanvasObject>emptySet(), 0, 0);
sel.clearSelected();
sel.setSelected(before, true);
sel.setHandleSelected(handle);
repaintArea(canvas);
}
}
Aggregations