Esempio n. 1
0
 @SuppressWarnings("unchecked")
 private void updatePointList() {
   for (SmoothPathSegment sps : spsMap.values()) sps.remove();
   spsMap.clear();
   for (LinearPathSegment lps : lpsMap.values()) lps.remove();
   lpsMap.clear();
   if (arrow != null) arrow.remove();
   Set<PathPoint> pps = ((HashMap<PathPoint, PointVisual>) pvMap.clone()).keySet();
   int s = pvList.size();
   ActiveArrayList<PathPoint> pp = path.points;
   int s2 = pp.size();
   while (s > s2) pvList.remove(--s);
   pvList.ensureCapacity(s2);
   for (int i = 0; i < s2; i++) {
     PathPoint p = pp.get(i);
     PointVisual v = pvMap.get(p);
     if (v == null) {
       v = new PointVisual(p);
       pvMap.put(p, v);
     } else {
       pps.remove(p);
     }
     if (i >= s) pvList.add(v);
     else pvList.set(i, v);
   }
   if (pps.contains(properties.get(PPathEditor.SELECTED_POINT)))
     properties.put(PPathEditor.SELECTED_POINT, null);
   for (PathPoint pathPoint : pps) pvMap.remove(pathPoint).remove();
   if (path.get(PPath.SMOOTH)) {
     boolean closed = path.properties.get(PPath.CLOSED);
     if (s2 >= 3) {
       PathPoint[] rpp = new PathPoint[4];
       for (int i = 0; i < 4; i++) rpp[i] = pp.get(i % s2);
       int i2 = closed ? s2 + 1 : s2 - 2;
       for (int i = 1; i < i2; i++) {
         PathPoint p = rpp[1];
         spsMap.put(p, new SmoothPathSegment(rpp));
         rpp = Arrays.copyOfRange(rpp, 1, 5);
         rpp[3] = pp.get((i + 3) % s2);
       }
       if (!closed) {
         PathPoint p = pp.get(0);
         spsMap.put(p, new SmoothPathSegment(null, p, pp.get(1), pp.get(2)));
         p = pp.get(s2 - 2);
         spsMap.put(p, new SmoothPathSegment(pp.get(s2 - 3), p, pp.get(s2 - 1), null));
       }
       arrow = new PathArrow(spsMap.get(pp.get(0)));
     }
   } else {
     if (s2 >= 2) {
       for (int i = 0; i < s2 - 1; i++) {
         PathPoint p = path.points.get(i);
         lpsMap.put(p, new LinearPathSegment(p, path.points.get(i + 1)));
       }
       if (path.properties.get(PPath.CLOSED)) {
         PathPoint p = path.points.get(s2 - 1);
         lpsMap.put(p, new LinearPathSegment(p, path.points.get(0)));
       }
       arrow = new PathArrow(null);
     }
   }
 }
Esempio n. 2
0
 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;
   }
 }