@Override
  public void duplicate() {
    Selection sel = canvas.getSelection();
    int n = sel.getSelected().size();
    List<CanvasObject> select = new ArrayList<CanvasObject>(n);
    List<CanvasObject> clones = new ArrayList<CanvasObject>(n);
    for (CanvasObject o : sel.getSelected()) {
      if (o.canRemove()) {
        CanvasObject copy = o.clone();
        copy.translate(10, 10);
        clones.add(copy);
        select.add(copy);
      } else {
        select.add(o);
      }
    }

    if (!clones.isEmpty()) {
      canvas
          .getProject()
          .doAction(
              new SelectionAction(
                  canvas,
                  Strings.getter("duplicateSelectionAction"),
                  null,
                  clones,
                  select,
                  null,
                  null));
    }
  }
  @Override
  public void delete() {
    Selection sel = canvas.getSelection();
    int n = sel.getSelected().size();
    List<CanvasObject> select = new ArrayList<CanvasObject>(n);
    List<CanvasObject> remove = new ArrayList<CanvasObject>(n);
    Location anchorLocation = null;
    Direction anchorFacing = null;
    for (CanvasObject o : sel.getSelected()) {
      if (o.canRemove()) {
        remove.add(o);
      } else {
        select.add(o);
        if (o instanceof AppearanceAnchor) {
          AppearanceAnchor anchor = (AppearanceAnchor) o;
          anchorLocation = anchor.getLocation();
          anchorFacing = anchor.getFacing();
        }
      }
    }

    if (!remove.isEmpty()) {
      canvas
          .getProject()
          .doAction(
              new SelectionAction(
                  canvas,
                  Strings.getter("deleteSelectionAction"),
                  remove,
                  null,
                  select,
                  anchorLocation,
                  anchorFacing));
    }
  }
Exemplo n.º 3
0
 public void drawSuppressed(android.graphics.Canvas c, CanvasObject shape, Paint p) {
   String state = suppressed.get(shape);
   if (state.equals(MOVING_HANDLE)) {
     shape.paint(c, curHandleGesture, p);
   } else if (state.equals(TRANSLATING)) {
     c.translate(moveDx, moveDy);
     shape.paint(c, null, p);
   }
 }
  @Override
  public void paste() {
    ClipboardContents clip = Clipboard.get();
    Collection<CanvasObject> contents = clip.getElements();
    List<CanvasObject> add = new ArrayList<CanvasObject>(contents.size());
    for (CanvasObject o : contents) {
      add.add(o.clone());
    }
    if (add.isEmpty()) return;

    // find how far we have to translate shapes so that at least one of the
    // pasted shapes doesn't match what's already in the model
    Collection<CanvasObject> raw = canvas.getModel().getObjectsFromBottom();
    MatchingSet<CanvasObject> cur = new MatchingSet<CanvasObject>(raw);
    int dx = 0;
    while (true) {
      // if any shapes in "add" aren't in canvas, we are done
      boolean allMatch = true;
      for (CanvasObject o : add) {
        if (!cur.contains(o)) {
          allMatch = false;
          break;
        }
      }
      if (!allMatch) break;

      // otherwise translate everything by 10 pixels and repeat test
      for (CanvasObject o : add) {
        o.translate(10, 10);
      }
      dx += 10;
    }

    Location anchorLocation = clip.getAnchorLocation();
    if (anchorLocation != null && dx != 0) {
      anchorLocation = anchorLocation.translate(dx, dx);
    }

    canvas
        .getProject()
        .doAction(
            new SelectionAction(
                canvas,
                Strings.getter("pasteClipboardAction"),
                null,
                add,
                add,
                anchorLocation,
                clip.getAnchorFacing()));
  }
Exemplo n.º 5
0
 @Override
 public void mouseReleased(Canvas canvas, MouseEvent e) {
   if (active) {
     updateMouse(canvas, e.getX(), e.getY(), e.getModifiersEx());
     Location start = mouseStart;
     Location end = mouseEnd;
     CanvasObject add = null;
     if (!start.equals(end)) {
       active = false;
       CanvasModel model = canvas.getModel();
       Location[] ends = {start, end};
       List<Location> locs = UnmodifiableList.create(ends);
       add = attrs.applyTo(new Poly(false, locs));
       add.setValue(DrawAttr.PAINT_TYPE, DrawAttr.PAINT_STROKE);
       canvas.doAction(new ModelAddAction(model, add));
       repaintArea(canvas);
     }
     canvas.toolGestureComplete(this, add);
   }
 }
  @Override
  public void computeEnabled() {
    Project proj = canvas.getProject();
    Circuit circ = canvas.getCircuit();
    Selection sel = canvas.getSelection();
    boolean selEmpty = sel.isEmpty();
    boolean canChange = proj.getLogisimFile().contains(circ);
    boolean clipExists = !Clipboard.isEmpty();
    boolean selHasRemovable = false;
    for (CanvasObject o : sel.getSelected()) {
      if (!(o instanceof AppearanceElement)) {
        selHasRemovable = true;
      }
    }
    boolean canRaise;
    boolean canLower;
    if (!selEmpty && canChange) {
      Map<CanvasObject, Integer> zs = ZOrder.getZIndex(sel.getSelected(), canvas.getModel());
      int zmin = Integer.MAX_VALUE;
      int zmax = Integer.MIN_VALUE;
      int count = 0;
      for (Map.Entry<CanvasObject, Integer> entry : zs.entrySet()) {
        if (!(entry.getKey() instanceof AppearanceElement)) {
          count++;
          int z = entry.getValue().intValue();
          if (z < zmin) zmin = z;
          if (z > zmax) zmax = z;
        }
      }
      int maxPoss = AppearanceCanvas.getMaxIndex(canvas.getModel());
      if (count > 0 && count <= maxPoss) {
        canRaise = zmin <= maxPoss - count;
        canLower = zmax >= count;
      } else {
        canRaise = false;
        canLower = false;
      }
    } else {
      canRaise = false;
      canLower = false;
    }
    boolean canAddCtrl = false;
    boolean canRemCtrl = false;
    Handle handle = sel.getSelectedHandle();
    if (handle != null && canChange) {
      CanvasObject o = handle.getObject();
      canAddCtrl = o.canInsertHandle(handle.getLocation()) != null;
      canRemCtrl = o.canDeleteHandle(handle.getLocation()) != null;
    }

    setEnabled(LogisimMenuBar.CUT, selHasRemovable && canChange);
    setEnabled(LogisimMenuBar.COPY, !selEmpty);
    setEnabled(LogisimMenuBar.PASTE, canChange && clipExists);
    setEnabled(LogisimMenuBar.DELETE, selHasRemovable && canChange);
    setEnabled(LogisimMenuBar.DUPLICATE, !selEmpty && canChange);
    setEnabled(LogisimMenuBar.SELECT_ALL, true);
    setEnabled(LogisimMenuBar.RAISE, canRaise);
    setEnabled(LogisimMenuBar.LOWER, canLower);
    setEnabled(LogisimMenuBar.RAISE_TOP, canRaise);
    setEnabled(LogisimMenuBar.LOWER_BOTTOM, canLower);
    setEnabled(LogisimMenuBar.ADD_CONTROL, canAddCtrl);
    setEnabled(LogisimMenuBar.REMOVE_CONTROL, canRemCtrl);
  }