Search in sources :

Example 1 with CircuitMutation

use of com.cburch.logisim.circuit.CircuitMutation in project logisim-evolution by reds-heig.

the class AttrTableSelectionModel method setValueRequested.

@Override
public void setValueRequested(Attribute<Object> attr, Object value) throws AttrTableSetException {
    Selection selection = frame.getCanvas().getSelection();
    Circuit circuit = frame.getCanvas().getCircuit();
    if (selection.isEmpty() && circuit != null) {
        AttrTableCircuitModel circuitModel = new AttrTableCircuitModel(project, circuit);
        circuitModel.setValueRequested(attr, value);
    } else {
        SetAttributeAction act = new SetAttributeAction(circuit, Strings.getter("selectionAttributeAction"));
        AutoLabel labler = null;
        if (attr.equals(StdAttr.LABEL)) {
            labler = new AutoLabel((String) value, circuit);
        }
        SortedSet<Component> comps = new TreeSet<Component>(new PositionComparator());
        comps.addAll(selection.getComponents());
        for (Component comp : comps) {
            if (!(comp instanceof Wire)) {
                if (comp.getFactory() instanceof SubcircuitFactory) {
                    SubcircuitFactory fac = (SubcircuitFactory) comp.getFactory();
                    if (attr.equals(CircuitAttributes.NAMED_CIRCUIT_BOX) || attr.equals(CircuitAttributes.NAME_ATTR)) {
                        try {
                            CircuitMutation mutation = new CircuitMutation(fac.getSubcircuit());
                            mutation.setForCircuit(attr, value);
                            Action action = mutation.toAction(null);
                            project.doAction(action);
                        } catch (CircuitException ex) {
                            JOptionPane.showMessageDialog(project.getFrame(), ex.getMessage());
                        }
                        return;
                    }
                }
                if (attr.equals(StdAttr.LABEL)) {
                    if (labler.hasNext(circuit)) {
                        if (comps.size() > 1) {
                            act.set(comp, attr, labler.GetNext(circuit, comp.getFactory()));
                        } else {
                            if (getAttributeSet().getValue(StdAttr.LABEL).equals((String) value))
                                return;
                            else
                                act.set(comp, attr, labler.GetCurrent(circuit, comp.getFactory()));
                        }
                    } else
                        act.set(comp, attr, "");
                } else
                    act.set(comp, attr, value);
            }
        }
        project.doAction(act);
    }
}
Also used : Action(com.cburch.logisim.proj.Action) SetAttributeAction(com.cburch.logisim.tools.SetAttributeAction) CircuitException(com.cburch.logisim.circuit.CircuitException) AutoLabel(com.cburch.logisim.util.AutoLabel) Circuit(com.cburch.logisim.circuit.Circuit) SetAttributeAction(com.cburch.logisim.tools.SetAttributeAction) Wire(com.cburch.logisim.circuit.Wire) TreeSet(java.util.TreeSet) SubcircuitFactory(com.cburch.logisim.circuit.SubcircuitFactory) CircuitMutation(com.cburch.logisim.circuit.CircuitMutation) Component(com.cburch.logisim.comp.Component)

Example 2 with CircuitMutation

use of com.cburch.logisim.circuit.CircuitMutation in project logisim-evolution by reds-heig.

the class WiringTool method performShortening.

private boolean performShortening(Canvas canvas, Location drag0, Location drag1) {
    Wire shorten = willShorten(drag0, drag1);
    if (shorten == null) {
        return false;
    } else {
        CircuitMutation xn = new CircuitMutation(canvas.getCircuit());
        StringGetter actName;
        Wire result = getShortenResult(shorten, drag0, drag1);
        if (result == null) {
            xn.remove(shorten);
            actName = Strings.getter("removeComponentAction", shorten.getFactory().getDisplayGetter());
        } else {
            xn.replace(shorten, result);
            actName = Strings.getter("shortenWireAction");
        }
        canvas.getProject().doAction(xn.toAction(actName));
        return true;
    }
}
Also used : CircuitMutation(com.cburch.logisim.circuit.CircuitMutation) Wire(com.cburch.logisim.circuit.Wire) StringGetter(com.cburch.logisim.util.StringGetter)

Example 3 with CircuitMutation

use of com.cburch.logisim.circuit.CircuitMutation in project logisim-evolution by reds-heig.

the class LoadedLibrary method replaceAll.

private static void replaceAll(Circuit circuit, Map<ComponentFactory, ComponentFactory> compMap) {
    ArrayList<Component> toReplace = null;
    for (Component comp : circuit.getNonWires()) {
        if (compMap.containsKey(comp.getFactory())) {
            if (toReplace == null)
                toReplace = new ArrayList<Component>();
            toReplace.add(comp);
        }
    }
    if (toReplace != null) {
        CircuitMutation xn = new CircuitMutation(circuit);
        for (Component comp : toReplace) {
            xn.remove(comp);
            ComponentFactory factory = compMap.get(comp.getFactory());
            if (factory != null) {
                AttributeSet newAttrs = createAttributes(factory, comp.getAttributeSet());
                xn.add(factory.createComponent(comp.getLocation(), newAttrs));
            }
        }
        xn.execute();
    }
}
Also used : AttributeSet(com.cburch.logisim.data.AttributeSet) ComponentFactory(com.cburch.logisim.comp.ComponentFactory) ArrayList(java.util.ArrayList) CircuitMutation(com.cburch.logisim.circuit.CircuitMutation) Component(com.cburch.logisim.comp.Component)

Example 4 with CircuitMutation

use of com.cburch.logisim.circuit.CircuitMutation in project logisim-evolution by reds-heig.

the class CircuitBuilder method build.

public static CircuitMutation build(Circuit destCirc, AnalyzerModel model, boolean twoInputs, boolean useNands) {
    CircuitMutation result = new CircuitMutation(destCirc);
    result.clear();
    Layout[] layouts = new Layout[model.getOutputs().size()];
    int maxWidth = 0;
    for (int i = 0; i < layouts.length; i++) {
        String output = model.getOutputs().get(i);
        Expression expr = model.getOutputExpressions().getExpression(output);
        CircuitDetermination det = CircuitDetermination.create(expr);
        if (det != null) {
            if (twoInputs)
                det.convertToTwoInputs();
            if (useNands)
                det.convertToNands();
            det.repair();
            layouts[i] = layoutGates(det);
            maxWidth = Math.max(maxWidth, layouts[i].width);
        } else {
            layouts[i] = null;
        }
    }
    InputData inputData = computeInputData(model);
    int x = inputData.getStartX();
    int y = 10;
    int outputX = x + maxWidth + 20;
    for (int i = 0; i < layouts.length; i++) {
        String outputName = model.getOutputs().get(i);
        Layout layout = layouts[i];
        Location output;
        int height;
        if (layout == null) {
            output = Location.create(outputX, y + 20);
            height = 40;
        } else {
            int dy = 0;
            if (layout.outputY < 20)
                dy = 20 - layout.outputY;
            height = Math.max(dy + layout.height, 40);
            output = Location.create(outputX, y + dy + layout.outputY);
            placeComponents(result, layouts[i], x, y + dy, inputData, output);
        }
        placeOutput(result, output, outputName);
        y += height + 10;
    }
    placeInputs(result, inputData);
    return result;
}
Also used : Expression(com.cburch.logisim.analyze.model.Expression) CircuitMutation(com.cburch.logisim.circuit.CircuitMutation) Location(com.cburch.logisim.data.Location)

Example 5 with CircuitMutation

use of com.cburch.logisim.circuit.CircuitMutation in project logisim-evolution by reds-heig.

the class BuildCircuitButton method performAction.

private void performAction(Project dest, String name, boolean replace, final boolean twoInputs, final boolean useNands) {
    if (replace) {
        final Circuit circuit = dest.getLogisimFile().getCircuit(name);
        if (circuit == null) {
            JOptionPane.showMessageDialog(parent, "Internal error prevents replacing circuit.", "Internal Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        CircuitMutation xn = CircuitBuilder.build(circuit, model, twoInputs, useNands);
        dest.doAction(xn.toAction(Strings.getter("replaceCircuitAction")));
    } else {
        // add the circuit
        Circuit circuit = new Circuit(name, dest.getLogisimFile(), dest);
        CircuitMutation xn = CircuitBuilder.build(circuit, model, twoInputs, useNands);
        xn.execute();
        dest.doAction(LogisimFileActions.addCircuit(circuit));
        dest.setCurrentCircuit(circuit);
    }
}
Also used : CircuitMutation(com.cburch.logisim.circuit.CircuitMutation) Circuit(com.cburch.logisim.circuit.Circuit)

Aggregations

CircuitMutation (com.cburch.logisim.circuit.CircuitMutation)10 Component (com.cburch.logisim.comp.Component)4 Action (com.cburch.logisim.proj.Action)4 Circuit (com.cburch.logisim.circuit.Circuit)3 CircuitException (com.cburch.logisim.circuit.CircuitException)3 Wire (com.cburch.logisim.circuit.Wire)3 AttributeSet (com.cburch.logisim.data.AttributeSet)3 Location (com.cburch.logisim.data.Location)3 SubcircuitFactory (com.cburch.logisim.circuit.SubcircuitFactory)2 ComponentFactory (com.cburch.logisim.comp.ComponentFactory)2 StringGetter (com.cburch.logisim.util.StringGetter)2 ArrayList (java.util.ArrayList)2 Expression (com.cburch.logisim.analyze.model.Expression)1 CircuitTransactionResult (com.cburch.logisim.circuit.CircuitTransactionResult)1 Bounds (com.cburch.logisim.data.Bounds)1 AttrTableSetException (com.cburch.logisim.gui.generic.AttrTableSetException)1 ToolAttributeAction (com.cburch.logisim.gui.main.ToolAttributeAction)1 Project (com.cburch.logisim.proj.Project)1 AddTool (com.cburch.logisim.tools.AddTool)1 SetAttributeAction (com.cburch.logisim.tools.SetAttributeAction)1