Search in sources :

Example 26 with CanvasObject

use of com.cburch.draw.model.CanvasObject 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);
}
Also used : HandleGesture(com.cburch.draw.model.HandleGesture) CanvasObject(com.cburch.draw.model.CanvasObject) Selection(com.cburch.draw.canvas.Selection) Handle(com.cburch.draw.model.Handle)

Example 27 with CanvasObject

use of com.cburch.draw.model.CanvasObject 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);
    }
}
Also used : CanvasObject(com.cburch.draw.model.CanvasObject) Selection(com.cburch.draw.canvas.Selection) Handle(com.cburch.draw.model.Handle)

Example 28 with CanvasObject

use of com.cburch.draw.model.CanvasObject in project logisim-evolution by reds-heig.

the class SelectTool method mouseReleased.

@Override
public void mouseReleased(Canvas canvas, MouseEvent e) {
    beforePressSelection = null;
    beforePressHandle = null;
    setMouse(canvas, e.getX(), e.getY(), e.getModifiersEx());
    CanvasModel model = canvas.getModel();
    Selection selection = canvas.getSelection();
    Set<CanvasObject> selected = selection.getSelected();
    int action = curAction;
    curAction = IDLE;
    if (!dragEffective) {
        Location loc = dragEnd;
        CanvasObject o = getObjectAt(model, loc.getX(), loc.getY(), false);
        if (o != null) {
            Handle han = o.canDeleteHandle(loc);
            if (han != null) {
                selection.setHandleSelected(han);
            } else {
                han = o.canInsertHandle(loc);
                if (han != null) {
                    selection.setHandleSelected(han);
                }
            }
        }
    }
    Location start = dragStart;
    int x1 = e.getX();
    int y1 = e.getY();
    switch(action) {
        case MOVE_ALL:
            Location moveDelta = selection.getMovingDelta();
            if (dragEffective && !moveDelta.equals(Location.create(0, 0))) {
                canvas.doAction(new ModelTranslateAction(model, selected, moveDelta.getX(), moveDelta.getY()));
            }
            break;
        case MOVE_HANDLE:
            HandleGesture gesture = curGesture;
            curGesture = null;
            if (dragEffective && gesture != null) {
                ModelMoveHandleAction act;
                act = new ModelMoveHandleAction(model, gesture);
                canvas.doAction(act);
                Handle result = act.getNewHandle();
                if (result != null) {
                    Handle h = result.getObject().canDeleteHandle(result.getLocation());
                    selection.setHandleSelected(h);
                }
            }
            break;
        case RECT_SELECT:
            if (dragEffective) {
                Bounds bds = Bounds.create(start).add(x1, y1);
                selection.setSelected(canvas.getModel().getObjectsIn(bds), true);
            } else {
                CanvasObject clicked;
                clicked = getObjectAt(model, start.getX(), start.getY(), true);
                if (clicked != null) {
                    selection.clearSelected();
                    selection.setSelected(clicked, true);
                }
            }
            break;
        case RECT_TOGGLE:
            if (dragEffective) {
                Bounds bds = Bounds.create(start).add(x1, y1);
                selection.toggleSelected(canvas.getModel().getObjectsIn(bds));
            } else {
                CanvasObject clicked;
                clicked = getObjectAt(model, start.getX(), start.getY(), true);
                selection.setSelected(clicked, !selected.contains(clicked));
            }
            break;
        default:
            break;
    }
    selection.clearDrawsSuppressed();
    repaintArea(canvas);
}
Also used : HandleGesture(com.cburch.draw.model.HandleGesture) CanvasObject(com.cburch.draw.model.CanvasObject) Selection(com.cburch.draw.canvas.Selection) ModelTranslateAction(com.cburch.draw.actions.ModelTranslateAction) Bounds(com.cburch.logisim.data.Bounds) CanvasModel(com.cburch.draw.model.CanvasModel) ModelMoveHandleAction(com.cburch.draw.actions.ModelMoveHandleAction) Location(com.cburch.logisim.data.Location) Handle(com.cburch.draw.model.Handle)

Example 29 with CanvasObject

use of com.cburch.draw.model.CanvasObject in project logisim-evolution by reds-heig.

the class ZOrder method getPrevious.

private static CanvasObject getPrevious(CanvasObject query, List<CanvasObject> objs, CanvasModel model, Collection<? extends CanvasObject> ignore) {
    int index = getIndex(query, objs);
    if (index <= 0) {
        return null;
    } else {
        Set<CanvasObject> set = toSet(model.getObjectsOverlapping(query));
        ListIterator<CanvasObject> it = objs.listIterator(index);
        while (it.hasPrevious()) {
            CanvasObject o = it.previous();
            if (set.contains(o) && !ignore.contains(o))
                return o;
        }
        return null;
    }
}
Also used : CanvasObject(com.cburch.draw.model.CanvasObject)

Example 30 with CanvasObject

use of com.cburch.draw.model.CanvasObject in project logisim-evolution by reds-heig.

the class PortManager method performUpdate.

private void performUpdate(Set<Instance> adds, Set<Instance> removes, Map<Instance, Instance> replaces, Collection<Instance> allPins) {
    // Find the current objects corresponding to pins
    Map<Instance, AppearancePort> oldObjects;
    oldObjects = new HashMap<Instance, AppearancePort>();
    AppearanceAnchor anchor = null;
    for (CanvasObject o : appearance.getObjectsFromBottom()) {
        if (o instanceof AppearancePort) {
            AppearancePort port = (AppearancePort) o;
            oldObjects.put(port.getPin(), port);
        } else if (o instanceof AppearanceAnchor) {
            anchor = (AppearanceAnchor) o;
        }
    }
    // ensure we have the anchor in the circuit
    if (anchor == null) {
        for (CanvasObject o : DefaultAppearance.build(allPins, appearance.IsNamedBoxShaped(), appearance.getName(), appearance.GetGraphics())) {
            if (o instanceof AppearanceAnchor) {
                anchor = (AppearanceAnchor) o;
            }
        }
        if (anchor == null) {
            anchor = new AppearanceAnchor(Location.create(100, 100));
        }
        int dest = appearance.getObjectsFromBottom().size();
        appearance.addObjects(dest, Collections.singleton(anchor));
    }
    // Compute how the ports should change
    ArrayList<AppearancePort> portRemoves;
    portRemoves = new ArrayList<AppearancePort>(removes.size());
    ArrayList<AppearancePort> portAdds;
    portAdds = new ArrayList<AppearancePort>(adds.size());
    // handle removals
    for (Instance pin : removes) {
        AppearancePort port = oldObjects.remove(pin);
        if (port != null) {
            portRemoves.add(port);
        }
    }
    // handle replacements
    ArrayList<Instance> addsCopy = new ArrayList<Instance>(adds);
    for (Map.Entry<Instance, Instance> entry : replaces.entrySet()) {
        AppearancePort port = oldObjects.remove(entry.getKey());
        if (port != null) {
            port.setPin(entry.getValue());
            oldObjects.put(entry.getValue(), port);
        } else {
            // this really shouldn't happen, but just to make sure...
            addsCopy.add(entry.getValue());
        }
    }
    // handle additions
    DefaultAppearance.sortPinList(addsCopy, Direction.EAST);
    // I'm just sorting them so it works predictably.
    for (Instance pin : addsCopy) {
        if (!oldObjects.containsKey(pin)) {
            Location loc = computeDefaultLocation(appearance, pin, oldObjects);
            AppearancePort o = new AppearancePort(loc, pin);
            portAdds.add(o);
            oldObjects.put(pin, o);
        }
    }
    // Now update the appearance
    appearance.replaceAutomatically(portRemoves, portAdds);
}
Also used : Instance(com.cburch.logisim.instance.Instance) ArrayList(java.util.ArrayList) CanvasObject(com.cburch.draw.model.CanvasObject) HashMap(java.util.HashMap) Map(java.util.Map) Location(com.cburch.logisim.data.Location)

Aggregations

CanvasObject (com.cburch.draw.model.CanvasObject)45 Location (com.cburch.logisim.data.Location)15 ArrayList (java.util.ArrayList)13 Handle (com.cburch.draw.model.Handle)9 Selection (com.cburch.draw.canvas.Selection)8 ModelAddAction (com.cburch.draw.actions.ModelAddAction)7 Direction (com.cburch.logisim.data.Direction)5 HandleGesture (com.cburch.draw.model.HandleGesture)4 Poly (com.cburch.draw.shapes.Poly)4 Instance (com.cburch.logisim.instance.Instance)4 Map (java.util.Map)4 Rectangle (com.cburch.draw.shapes.Rectangle)3 HashMap (java.util.HashMap)3 AbstractCanvasObject (com.cburch.draw.model.AbstractCanvasObject)2 CanvasModel (com.cburch.draw.model.CanvasModel)2 Circuit (com.cburch.logisim.circuit.Circuit)2 AppearanceElement (com.cburch.logisim.circuit.appear.AppearanceElement)2 AppearancePort (com.cburch.logisim.circuit.appear.AppearancePort)2 Attribute (com.cburch.logisim.data.Attribute)2 Bounds (com.cburch.logisim.data.Bounds)2