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; } }
void doZoom() { if (owner == null) { return; } owner.pack(); Dimension screenSize = owner.getToolkit().getScreenSize(); Dimension windowSize = owner.getPreferredSize(); Point windowLoc = owner.getLocation(); boolean locChanged = false; boolean sizeChanged = false; if (windowLoc.x + windowSize.width > screenSize.width) { windowLoc.x = Math.max(0, screenSize.width - windowSize.width); locChanged = true; if (windowLoc.x + windowSize.width > screenSize.width) { windowSize.width = screenSize.width - windowLoc.x; sizeChanged = true; } } if (windowLoc.y + windowSize.height > screenSize.height) { windowLoc.y = Math.max(0, screenSize.height - windowSize.height); locChanged = true; if (windowLoc.y + windowSize.height > screenSize.height) { windowSize.height = screenSize.height - windowLoc.y; sizeChanged = true; } } if (locChanged) { owner.setLocation(windowLoc); } if (sizeChanged) { owner.setSize(windowSize); } }
@Override public boolean contains(Location loc, boolean assumeFilled) { Object type = getPaintType(); if (assumeFilled && type == DrawAttr.PAINT_STROKE) { type = DrawAttr.PAINT_STROKE_FILL; } if (type != DrawAttr.PAINT_FILL) { int stroke = getStrokeWidth(); double[] q = toArray(loc); double[] p0 = toArray(this.p0); double[] p1 = toArray(this.p1); double[] p2 = toArray(this.p2); double[] p = CurveUtil.findNearestPoint(q, p0, p1, p2); if (p == null) { return false; } int thr; if (type == DrawAttr.PAINT_STROKE) { thr = Math.max(Line.ON_LINE_THRESH, stroke / 2); } else { thr = stroke / 2; } if (LineUtil.distanceSquared(p[0], p[1], q[0], q[1]) < thr * thr) { return true; } } if (type != DrawAttr.PAINT_STROKE) { QuadCurve2D curve = getCurve(null); if (curve.contains(loc.getX(), loc.getY())) { return true; } } return false; }
private void drawInstance(InstancePainter painter, boolean isGhost) { Bounds bds = painter.getBounds(); Object powerLoc = painter.getAttributeValue(Wiring.ATTR_GATE); Direction facing = painter.getAttributeValue(StdAttr.FACING); boolean flip = (facing == Direction.SOUTH || facing == Direction.WEST) == (powerLoc == Wiring.GATE_TOP_LEFT); int degrees = Direction.WEST.toDegrees() - facing.toDegrees(); if (flip) { degrees += 180; } double radians = Math.toRadians((degrees + 360) % 360); Graphics2D g = (Graphics2D) painter.getGraphics().create(); g.rotate(radians, bds.getX() + 20, bds.getY() + 20); g.translate(bds.getX(), bds.getY()); GraphicsUtil.switchToWidth(g, Wire.WIDTH); Color gate0 = g.getColor(); Color gate1 = gate0; Color input = gate0; Color output = gate0; Color platform = gate0; if (!isGhost && painter.getShowState()) { gate0 = painter.getPort(GATE0).getColor(); gate1 = painter.getPort(GATE0).getColor(); input = painter.getPort(INPUT).getColor(); output = painter.getPort(OUTPUT).getColor(); platform = computeOutput(painter).getColor(); } g.setColor(flip ? input : output); g.drawLine(0, 20, 11, 20); g.drawLine(11, 13, 11, 27); g.setColor(flip ? output : input); g.drawLine(29, 20, 40, 20); g.drawLine(29, 13, 29, 27); g.setColor(gate0); g.drawLine(20, 35, 20, 40); GraphicsUtil.switchToWidth(g, 1); g.drawOval(18, 30, 4, 4); g.drawLine(10, 30, 30, 30); GraphicsUtil.switchToWidth(g, Wire.WIDTH); g.setColor(gate1); g.drawLine(20, 9, 20, 0); GraphicsUtil.switchToWidth(g, 1); g.drawLine(10, 10, 30, 10); g.setColor(platform); g.drawLine(9, 12, 31, 12); g.drawLine(9, 28, 31, 28); // arrow if (flip) { g.drawLine(18, 17, 21, 20); g.drawLine(18, 23, 21, 20); } else { g.drawLine(22, 17, 19, 20); g.drawLine(22, 23, 19, 20); } g.dispose(); }