Search in sources :

Example 91 with Component

use of com.cburch.logisim.comp.Component in project logisim-evolution by reds-heig.

the class AddTool method mouseReleased.

@Override
public void mouseReleased(Canvas canvas, Graphics g, MouseEvent e) {
    Component added = null;
    if (state == SHOW_ADD) {
        Circuit circ = canvas.getCircuit();
        if (!canvas.getProject().getLogisimFile().contains(circ))
            return;
        if (shouldSnap)
            Canvas.snapToGrid(e);
        moveTo(canvas, g, e.getX(), e.getY());
        Location loc = Location.create(e.getX(), e.getY());
        ComponentFactory source = getFactory();
        AttributeSet attrsCopy = (AttributeSet) attrs.clone();
        if (attrsCopy.containsAttribute(StdAttr.LABEL)) {
            attrsCopy.setValue(StdAttr.LABEL, AutoLabler.GetCurrent(canvas.getCircuit(), source));
            if (AutoLabler.IsActive(canvas.getCircuit())) {
                if (AutoLabler.hasNext(canvas.getCircuit()))
                    AutoLabler.GetNext(canvas.getCircuit(), source);
                else
                    AutoLabler.Stop(canvas.getCircuit());
            } else
                AutoLabler.SetLabel("", canvas.getCircuit(), source);
        }
        if (source == null)
            return;
        Component c = source.createComponent(loc, attrsCopy);
        if (circ.hasConflict(c)) {
            canvas.setErrorMessage(Strings.getter("exclusiveError"));
            return;
        }
        Bounds bds = c.getBounds(g);
        if (bds.getX() < 0 || bds.getY() < 0) {
            canvas.setErrorMessage(Strings.getter("negativeCoordError"));
            return;
        }
        try {
            CircuitMutation mutation = new CircuitMutation(circ);
            mutation.add(c);
            Action action = mutation.toAction(Strings.getter("addComponentAction", factory.getDisplayGetter()));
            canvas.getProject().doAction(action);
            lastAddition = action;
            added = c;
            canvas.repaint();
        } catch (CircuitException ex) {
            JOptionPane.showMessageDialog(canvas.getProject().getFrame(), ex.getMessage());
        }
        setState(canvas, SHOW_GHOST);
    } else if (state == SHOW_ADD_NO) {
        setState(canvas, SHOW_NONE);
    }
    Project proj = canvas.getProject();
    Tool next = determineNext(proj);
    if (next != null) {
        proj.setTool(next);
        Action act = SelectionActions.dropAll(canvas.getSelection());
        if (act != null) {
            proj.doAction(act);
        }
        if (added != null)
            canvas.getSelection().add(added);
    }
}
Also used : Project(com.cburch.logisim.proj.Project) Action(com.cburch.logisim.proj.Action) ToolAttributeAction(com.cburch.logisim.gui.main.ToolAttributeAction) CircuitException(com.cburch.logisim.circuit.CircuitException) AttributeSet(com.cburch.logisim.data.AttributeSet) ComponentFactory(com.cburch.logisim.comp.ComponentFactory) Bounds(com.cburch.logisim.data.Bounds) CircuitMutation(com.cburch.logisim.circuit.CircuitMutation) Circuit(com.cburch.logisim.circuit.Circuit) Component(com.cburch.logisim.comp.Component) Location(com.cburch.logisim.data.Location)

Example 92 with Component

use of com.cburch.logisim.comp.Component in project logisim-evolution by reds-heig.

the class EditTool method isWiringPoint.

private boolean isWiringPoint(Canvas canvas, Location loc, int modsEx) {
    boolean wiring = (modsEx & MouseEvent.ALT_DOWN_MASK) == 0;
    boolean select = !wiring;
    if (canvas != null && canvas.getSelection() != null) {
        Collection<Component> sel = canvas.getSelection().getComponents();
        if (sel != null) {
            for (Component c : sel) {
                if (c instanceof Wire) {
                    Wire w = (Wire) c;
                    if (w.contains(loc) && !w.endsAt(loc))
                        return select;
                }
            }
        }
    }
    Circuit circ = canvas.getCircuit();
    Collection<? extends Component> at = circ.getComponents(loc);
    if (at != null && at.size() > 0)
        return wiring;
    for (Wire w : circ.getWires()) {
        if (w.contains(loc)) {
            return wiring;
        }
    }
    return select;
}
Also used : Circuit(com.cburch.logisim.circuit.Circuit) Component(com.cburch.logisim.comp.Component) Wire(com.cburch.logisim.circuit.Wire)

Example 93 with Component

use of com.cburch.logisim.comp.Component in project logisim-evolution by reds-heig.

the class WiringTool method checkForRepairs.

private Wire checkForRepairs(Canvas canvas, Wire w, Location end) {
    if (w.getLength() <= 10)
        // don't repair a short wire to nothing
        return w;
    if (!canvas.getCircuit().getNonWires(end).isEmpty())
        return w;
    int delta = (end.equals(w.getEnd0()) ? 10 : -10);
    Location cand;
    if (w.isVertical()) {
        cand = Location.create(end.getX(), end.getY() + delta);
    } else {
        cand = Location.create(end.getX() + delta, end.getY());
    }
    for (Component comp : canvas.getCircuit().getNonWires(cand)) {
        if (comp.getBounds().contains(end, 2)) {
            WireRepair repair = (WireRepair) comp.getFeature(WireRepair.class);
            if (repair != null && repair.shouldRepairWire(new WireRepairData(w, cand))) {
                w = Wire.create(w.getOtherEnd(end), cand);
                canvas.repaint(end.getX() - 13, end.getY() - 13, 26, 26);
                return w;
            }
        }
    }
    return w;
}
Also used : Component(com.cburch.logisim.comp.Component) Location(com.cburch.logisim.data.Location)

Example 94 with Component

use of com.cburch.logisim.comp.Component in project logisim-evolution by reds-heig.

the class MoveGesture method computeConnections.

private static Set<ConnectionData> computeConnections(Circuit circuit, Set<Component> selected) {
    if (selected == null || selected.isEmpty())
        return Collections.emptySet();
    // first identify locations that might be connected
    Set<Location> locs = new HashSet<Location>();
    for (Component comp : selected) {
        for (EndData end : comp.getEnds()) {
            locs.add(end.getLocation());
        }
    }
    // now see which of them require connection
    Set<ConnectionData> conns = new HashSet<ConnectionData>();
    for (Location loc : locs) {
        boolean found = false;
        for (Component comp : circuit.getComponents(loc)) {
            if (!selected.contains(comp)) {
                found = true;
                break;
            }
        }
        if (found) {
            List<Wire> wirePath;
            Location wirePathStart;
            Wire lastOnPath = findWire(circuit, loc, selected, null);
            if (lastOnPath == null) {
                wirePath = Collections.emptyList();
                wirePathStart = loc;
            } else {
                wirePath = new ArrayList<Wire>();
                Location cur = loc;
                for (Wire w = lastOnPath; w != null; w = findWire(circuit, cur, selected, w)) {
                    wirePath.add(w);
                    cur = w.getOtherEnd(cur);
                }
                Collections.reverse(wirePath);
                wirePathStart = cur;
            }
            Direction dir = null;
            if (lastOnPath != null) {
                Location other = lastOnPath.getOtherEnd(loc);
                int dx = loc.getX() - other.getX();
                int dy = loc.getY() - other.getY();
                if (Math.abs(dx) > Math.abs(dy)) {
                    dir = dx > 0 ? Direction.EAST : Direction.WEST;
                } else {
                    dir = dy > 0 ? Direction.SOUTH : Direction.NORTH;
                }
            }
            conns.add(new ConnectionData(loc, dir, wirePath, wirePathStart));
        }
    }
    return conns;
}
Also used : EndData(com.cburch.logisim.comp.EndData) Wire(com.cburch.logisim.circuit.Wire) Direction(com.cburch.logisim.data.Direction) Component(com.cburch.logisim.comp.Component) Location(com.cburch.logisim.data.Location) HashSet(java.util.HashSet)

Example 95 with Component

use of com.cburch.logisim.comp.Component in project logisim-evolution by reds-heig.

the class MoveResult method print.

public void print(PrintStream out) {
    boolean printed = false;
    for (Component w : replacements.getAdditions()) {
        printed = true;
        out.println("add " + w);
    }
    for (Component w : replacements.getRemovals()) {
        printed = true;
        out.println("del " + w);
    }
    for (Component w : replacements.getReplacedComponents()) {
        printed = true;
        out.print("repl " + w + " by");
        for (Component w2 : replacements.getComponentsReplacing(w)) {
            out.print(" " + w2);
        }
        out.println();
    }
    if (!printed) {
        out.println("no replacements");
    }
}
Also used : Component(com.cburch.logisim.comp.Component)

Aggregations

Component (com.cburch.logisim.comp.Component)97 Location (com.cburch.logisim.data.Location)26 ArrayList (java.util.ArrayList)20 Circuit (com.cburch.logisim.circuit.Circuit)19 AttributeSet (com.cburch.logisim.data.AttributeSet)13 Wire (com.cburch.logisim.circuit.Wire)12 Bounds (com.cburch.logisim.data.Bounds)11 HashMap (java.util.HashMap)11 HashSet (java.util.HashSet)11 SubcircuitFactory (com.cburch.logisim.circuit.SubcircuitFactory)8 ComponentFactory (com.cburch.logisim.comp.ComponentFactory)8 EndData (com.cburch.logisim.comp.EndData)8 InstanceComponent (com.cburch.logisim.instance.InstanceComponent)8 Project (com.cburch.logisim.proj.Project)7 Pin (com.cburch.logisim.std.wiring.Pin)7 Graphics (java.awt.Graphics)7 CircuitState (com.cburch.logisim.circuit.CircuitState)6 Splitter (com.cburch.logisim.circuit.Splitter)6 Selection (com.cburch.logisim.gui.main.Selection)6 InstanceState (com.cburch.logisim.instance.InstanceState)5