Search in sources :

Example 21 with Instance

use of com.cburch.logisim.instance.Instance in project logisim-evolution by reds-heig.

the class SubcircuitFactory method computePorts.

void computePorts(Instance instance) {
    Direction facing = instance.getAttributeValue(StdAttr.FACING);
    Map<Location, Instance> portLocs = source.getAppearance().getPortOffsets(facing);
    Port[] ports = new Port[portLocs.size()];
    Instance[] pins = new Instance[portLocs.size()];
    int i = -1;
    for (Map.Entry<Location, Instance> portLoc : portLocs.entrySet()) {
        i++;
        Location loc = portLoc.getKey();
        Instance pin = portLoc.getValue();
        String type = Pin.FACTORY.isInputPin(pin) ? Port.INPUT : Port.OUTPUT;
        BitWidth width = pin.getAttributeValue(StdAttr.WIDTH);
        ports[i] = new Port(loc.getX(), loc.getY(), type, width);
        pins[i] = pin;
        String label = pin.getAttributeValue(StdAttr.LABEL);
        if (label != null && label.length() > 0) {
            ports[i].setToolTip(StringUtil.constantGetter(label));
        }
    }
    CircuitAttributes attrs = (CircuitAttributes) instance.getAttributeSet();
    attrs.setPinInstances(pins);
    instance.setPorts(ports);
    instance.recomputeBounds();
    // since this affects the circuit's bounds
    configureLabel(instance);
}
Also used : Instance(com.cburch.logisim.instance.Instance) Port(com.cburch.logisim.instance.Port) Direction(com.cburch.logisim.data.Direction) BitWidth(com.cburch.logisim.data.BitWidth) HashMap(java.util.HashMap) Map(java.util.Map) Location(com.cburch.logisim.data.Location)

Example 22 with Instance

use of com.cburch.logisim.instance.Instance in project logisim-evolution by reds-heig.

the class SubcircuitFactory method propagate.

@Override
public void propagate(InstanceState superState) {
    CircuitState subState = getSubstate(superState);
    CircuitAttributes attrs = (CircuitAttributes) superState.getAttributeSet();
    Instance[] pins = attrs.getPinInstances();
    for (int i = 0; i < pins.length; i++) {
        Instance pin = pins[i];
        InstanceState pinState = subState.getInstanceState(pin);
        if (Pin.FACTORY.isInputPin(pin)) {
            Value newVal = superState.getPortValue(i);
            Value oldVal = Pin.FACTORY.getValue(pinState);
            if (!newVal.equals(oldVal)) {
                Pin.FACTORY.setValue(pinState, newVal);
                Pin.FACTORY.propagate(pinState);
            }
        } else {
            // it is output-only
            Value val = pinState.getPortValue(0);
            superState.setPort(i, val, 1);
        }
    }
}
Also used : InstanceState(com.cburch.logisim.instance.InstanceState) Instance(com.cburch.logisim.instance.Instance) Value(com.cburch.logisim.data.Value)

Example 23 with Instance

use of com.cburch.logisim.instance.Instance in project logisim-evolution by reds-heig.

the class SubcircuitFactory method placePins.

/**
 * This function will be in charge of placing the name of the pin and rotate them
 * 		accordingly avoid the names being upside down
 * @param painter
 * @param edge
 * @param x
 * @param y
 * @param dx
 * @param dy
 * @param LeftSide
 * @param angle
 */
private static void placePins(InstancePainter painter, Map<Direction, List<Instance>> edge, int x, int y, int width, int height, double angle, Direction facing) {
    /* Here is checking which side of the circuit has
		 * the most pins
		 */
    int ldx = 0;
    int ldy = 0;
    int maxVert = Math.max(edge.get(Direction.EAST).size(), edge.get(Direction.WEST).size());
    List<Instance> pins_west = edge.get(Direction.WEST);
    List<Instance> pins_east = edge.get(Direction.EAST);
    Font pins_font = DrawAttr.DEFAULT_FIXED_PICH_FONT;
    for (int i = 0; i < maxVert; i++) {
        /* Draw East side pins */
        if (pins_east.size() > i) {
            Instance pin_east = pins_east.get(i);
            if (pin_east.getAttributeSet().containsAttribute(StdAttr.LABEL)) {
                Graphics g1 = painter.getGraphics().create();
                FontMetrics m = g1.getFontMetrics(pins_font);
                if (Direction.WEST == facing) {
                    ldx = 20;
                    ldy = -(i * (m.getHeight() + 5) - 2);
                } else {
                    ldx = -m.stringWidth(pin_east.getAttributeValue(StdAttr.LABEL)) - 20;
                    ldy = (i * (m.getHeight() + 5) + 5);
                }
                ((Graphics2D) g1).rotate(angle);
                ((Graphics2D) g1).setFont(pins_font);
                ((Graphics2D) g1).drawString(pin_east.getAttributeValue(StdAttr.LABEL), ldx, ldy);
                g1.dispose();
            }
        }
        /* Draw West side pins */
        if (pins_west.size() > i) {
            Instance pin_west = pins_west.get(i);
            if (pin_west.getAttributeSet().containsAttribute(StdAttr.LABEL)) {
                Graphics g1 = painter.getGraphics().create();
                FontMetrics m = g1.getFontMetrics(pins_font);
                ((Graphics2D) g1).setFont(pins_font);
                ((Graphics2D) g1).rotate(angle);
                if (Direction.SOUTH == facing) {
                    ldx = -height + 20;
                    ldy = (i * (m.getHeight() + 5) + 5);
                } else if (Direction.WEST == facing) {
                    ldx = width - m.stringWidth(pin_west.getAttributeValue(StdAttr.LABEL)) - 20;
                    ldy = -(i * (m.getHeight() + 5) - 2);
                } else if (Direction.EAST == facing) {
                    ldx = -width + 20;
                    ldy = (i * (m.getHeight() + 5) + 5);
                } else if (Direction.NORTH == facing) {
                    ldx = -height + 20;
                    ldy = (i * (m.getHeight() + 5) + 5);
                }
                ((Graphics2D) g1).drawString(pin_west.getAttributeValue(StdAttr.LABEL), ldx, ldy);
                g1.dispose();
            }
        }
    }
}
Also used : Graphics(java.awt.Graphics) Instance(com.cburch.logisim.instance.Instance) FontMetrics(java.awt.FontMetrics) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Example 24 with Instance

use of com.cburch.logisim.instance.Instance in project logisim-evolution by reds-heig.

the class DefaultAppearance method placePins.

private static void placePins(List<CanvasObject> dest, List<Instance> pins, int x, int y, int dx, int dy, boolean LeftSide, int ldy) {
    int halign;
    Color color = Color.DARK_GRAY;
    int ldx;
    for (Instance pin : pins) {
        int offset = (pin.getAttributeValue(StdAttr.WIDTH).getWidth() > 1) ? Wire.WIDTH_BUS >> 1 : Wire.WIDTH >> 1;
        int height = (pin.getAttributeValue(StdAttr.WIDTH).getWidth() > 1) ? Wire.WIDTH_BUS : Wire.WIDTH;
        Rectangle rect;
        if (LeftSide) {
            ldx = 15;
            halign = EditableLabel.LEFT;
            rect = new Rectangle(x, y - offset, 10, height);
        } else {
            ldx = -15;
            halign = EditableLabel.RIGHT;
            rect = new Rectangle(x - 10, y - offset, 10, height);
        }
        rect.setValue(DrawAttr.STROKE_WIDTH, Integer.valueOf(1));
        rect.setValue(DrawAttr.PAINT_TYPE, DrawAttr.PAINT_FILL);
        rect.setValue(DrawAttr.FILL_COLOR, Color.BLACK);
        dest.add(rect);
        dest.add(new AppearancePort(Location.create(x, y), pin));
        if (pin.getAttributeSet().containsAttribute(StdAttr.LABEL)) {
            Text label = new Text(x + ldx, y + ldy, pin.getAttributeValue(StdAttr.LABEL));
            label.getLabel().setHorizontalAlignment(halign);
            label.getLabel().setColor(color);
            label.getLabel().setFont(DrawAttr.DEFAULT_FIXED_PICH_FONT);
            dest.add(label);
        }
        x += dx;
        y += dy;
    }
}
Also used : Instance(com.cburch.logisim.instance.Instance) Color(java.awt.Color) Rectangle(com.cburch.draw.shapes.Rectangle) Text(com.cburch.draw.shapes.Text)

Example 25 with Instance

use of com.cburch.logisim.instance.Instance 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

Instance (com.cburch.logisim.instance.Instance)28 ArrayList (java.util.ArrayList)14 Map (java.util.Map)10 Direction (com.cburch.logisim.data.Direction)9 Location (com.cburch.logisim.data.Location)9 HashMap (java.util.HashMap)9 Graphics (java.awt.Graphics)5 CanvasObject (com.cburch.draw.model.CanvasObject)4 Component (com.cburch.logisim.comp.Component)4 Bounds (com.cburch.logisim.data.Bounds)4 InstanceState (com.cburch.logisim.instance.InstanceState)4 List (java.util.List)4 TreeMap (java.util.TreeMap)4 Rectangle (com.cburch.draw.shapes.Rectangle)3 Text (com.cburch.draw.shapes.Text)3 CircuitState (com.cburch.logisim.circuit.CircuitState)3 Value (com.cburch.logisim.data.Value)3 Font (java.awt.Font)3 FontMetrics (java.awt.FontMetrics)3 Graphics2D (java.awt.Graphics2D)3