@Override protected void validate() { if (segment == null) { PathPoint p = path.points.get(0); PathPoint p2 = path.points.get(1); int x = p.getX(); int y = p.getY(); calculatePoints(x, y, Math.atan2(y - p2.getY(), p2.getX() - x)); } else { segment.validate(); if (path.get(PPath.CLOSED)) { int i = segment.px.length - 1; int x = segment.px[i]; int y = segment.py[i]; int i2 = i - 1; while (i2 > 0 && sqrdist(segment.px[i2] - x, segment.py[i2] - y) < 4) i2--; calculatePoints( x + segment.bounds.x, y + segment.bounds.y, Math.atan2(segment.py[i2] - y, x - segment.px[i2])); } else { int x = segment.px[0]; int y = segment.py[0]; int i = 1; while (i < segment.px.length - 1 && sqrdist(segment.px[i] - x, segment.py[i] - y) < 4) i++; calculatePoints( x + segment.bounds.x, y + segment.bounds.y, Math.atan2(y - segment.py[i], segment.px[i] - x)); } } Rectangle bounds = new Rectangle(-1, -1); for (int i = 0; i < 4; i++) bounds.add(px[i], py[i]); for (int i = 0; i < 4; i++) { px[i] -= bounds.x; py[i] -= bounds.y; } setBounds(bounds); }
protected void calculateBounds() { bounds.setLocation(point.getX() - HPS, point.getY() - HPS); }
private void mouseEvent(MouseEvent e) { Point p = e.getPoint().getLocation(); componentToVisual(p); int s = POINT_MOUSE_RANGE - POINT_SIZE / 2 + 1; Iterator<Visual> vi = binVisual.intersect(new Rectangle(p.x - s, p.y - s, 2 * s, 2 * s)); PathPoint ppOver = null; int posd = POINT_MOUSE_RANGE * POINT_MOUSE_RANGE; while (vi.hasNext()) { Visual v = vi.next(); if (v instanceof PointVisual) { PathPoint pp = ((PointVisual) v).point; int xd = pp.getX() - p.x; int yd = pp.getY() - p.y; int sd = xd * xd + yd * yd; if (sd <= posd) { ppOver = pp; posd = sd; break; } } } switch (e.getID()) { case MouseEvent.MOUSE_PRESSED: ppPressed = ppOver; switch (e.getButton()) { case MouseEvent.BUTTON1: if (ppOver == null) { PathPoint pp = properties.get(PPathEditor.SELECTED_POINT); ppOver = new PathPoint(p.x, p.y, pp == null ? 100 : pp.getSpeed()); if ((e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) == 0) movePathPoint(ppOver, p.x, p.y, true); path.points.add(ppOver); ppPressed = ppOver; } else if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0) { int i = path.points.indexOf(ppOver); ppOver = new PathPoint(ppOver.getX(), ppOver.getY(), ppOver.getSpeed()); path.points.add(i, ppOver); ppPressed = ppOver; } properties.put(PPathEditor.SELECTED_POINT, ppOver); dragging = true; dragOffset = p.getLocation(); dragOffset.translate(-ppOver.getX(), -ppOver.getY()); break; } break; case MouseEvent.MOUSE_DRAGGED: if (dragging) { lockBounds(); PathPoint pp = properties.get(PPathEditor.SELECTED_POINT); if (pp == null) { ppPressed = null; dragging = false; unlockBounds(); break; } movePathPoint( pp, p.x - dragOffset.x, p.y - dragOffset.y, (e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) == 0); } else if (ppPressed != ppOver) ppPressed = null; break; case MouseEvent.MOUSE_RELEASED: switch (e.getButton()) { case MouseEvent.BUTTON1: dragging = false; unlockBounds(); break; case MouseEvent.BUTTON3: if (dragging) { dragging = false; unlockBounds(); } else if (ppPressed != null) path.points.remove(ppPressed); break; } ppPressed = null; } }