Search in sources :

Example 6 with Instance

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

the class SubcircuitFactory method drawPinsName.

private void drawPinsName(InstancePainter painter, Bounds bds, Direction facing, Direction defaultFacing) {
    Map<Direction, List<Instance>> edge;
    Collection<Instance> pins = source.getAppearance().GetCircuitPin().getPins();
    edge = new HashMap<Direction, List<Instance>>();
    edge.put(Direction.EAST, new ArrayList<Instance>());
    edge.put(Direction.WEST, new ArrayList<Instance>());
    int MaxLeftLabelLength = 0;
    int MaxRightLabelLength = 0;
    double angle;
    for (Instance pin : pins) {
        Direction pinEdge;
        Text label = new Text(0, 0, pin.getAttributeValue(StdAttr.LABEL));
        int LabelWidth = label.getText().length() * DrawAttr.FixedFontCharWidth;
        if (pin.getAttributeValue(Pin.ATTR_TYPE)) {
            pinEdge = Direction.EAST;
            if (LabelWidth > MaxRightLabelLength)
                MaxRightLabelLength = LabelWidth;
        } else {
            pinEdge = Direction.WEST;
            if (LabelWidth > MaxLeftLabelLength)
                MaxLeftLabelLength = LabelWidth;
        }
        List<Instance> e = edge.get(pinEdge);
        e.add(pin);
    }
    for (Map.Entry<Direction, List<Instance>> entry : edge.entrySet()) {
        source.getAppearance().sortPinsList(entry.getValue(), entry.getKey());
    }
    /* Here we manage to have always the beginning of the pin label
		 * close to the pin and avoid the label being upside down.
		 */
    angle = facing.toRadians();
    angle = Math.toRadians(-Math.toDegrees(angle) % Math.toDegrees(Math.PI));
    placePins(painter, edge, bds.getX(), bds.getY(), bds.getWidth(), bds.getHeight(), angle, facing);
}
Also used : Instance(com.cburch.logisim.instance.Instance) Text(com.cburch.draw.shapes.Text) Direction(com.cburch.logisim.data.Direction) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with Instance

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

the class SubcircuitFactory method drawCircuitName.

/**
 * This function is in charge of displaying a subcircuit,
 * 	and to take care of rotating the strings accordingly.
 * @param painter the painter gotten from paint event
 * @param bds			border limits
 * @param facing		Direction in case of rotation
 * @param defaultFacing Default direction when element was
 * 						created
 */
private void drawCircuitName(InstancePainter painter, Bounds bds, Direction facing, Direction defaultFacing) {
    String label = source.getName();
    Map<Direction, List<Instance>> edge;
    int x;
    int y;
    int y_label = 0;
    /* This is made to count the pins on each side */
    edge = new HashMap<Direction, List<Instance>>();
    edge.put(Direction.EAST, new ArrayList<Instance>());
    edge.put(Direction.WEST, new ArrayList<Instance>());
    Collection<Instance> pins = source.getAppearance().GetCircuitPin().getPins();
    if (!pins.isEmpty()) {
        for (Instance pin : pins) {
            Direction pinEdge;
            if (pin.getAttributeValue(Pin.ATTR_TYPE)) {
                pinEdge = Direction.EAST;
            } else {
                pinEdge = Direction.WEST;
            }
            List<Instance> e = edge.get(pinEdge);
            e.add(pin);
        }
    }
    int maxNumber = Math.max(edge.get(Direction.EAST).size(), edge.get(Direction.WEST).size());
    maxNumber--;
    if (label != null && !label.equals("")) {
        Direction up = Direction.NORTH;
        Font font = DrawAttr.DEFAULT_NAME_FONT;
        Graphics g = painter.getGraphics().create();
        FontMetrics m = g.getFontMetrics(font);
        double angle;
        if (facing == Direction.WEST) {
            angle = Math.PI / 2 - (up.toRadians() - defaultFacing.toRadians());
            x = bds.getX() + bds.getWidth() / 2;
            y = bds.getY() + m.getAscent() / 2;
            y_label = y;
            g.fillRect(bds.getX() + CircuitAppearance.PINLENGTH, bds.getY(), bds.getWidth() - 2 * CircuitAppearance.PINLENGTH, m.getHeight());
        } else {
            angle = Math.PI / 2 - (up.toRadians() - defaultFacing.toRadians()) - facing.toRadians();
            if (facing == Direction.NORTH) {
                x = bds.getX() + bds.getWidth() / 2 + m.getAscent();
                y = bds.getY() + bds.getHeight() / 2;
                /* Get position regarding the box itself meaning that when rotated the x\y axis follows */
                y_label = y + maxNumber * m.getAscent();
                g.fillRect(bds.getX() + bds.getWidth() - m.getHeight() - 1, bds.getY() + CircuitAppearance.PINLENGTH, m.getHeight(), bds.getHeight() - 2 * CircuitAppearance.PINLENGTH);
            } else if (facing == Direction.EAST) {
                x = bds.getX() + bds.getWidth() / 2;
                y = bds.getY() + bds.getHeight() - m.getAscent();
                /* Get position regarding the box itself meaning that when rotated the  x\y axis follows */
                y_label = y;
                g.fillRect(bds.getX() + CircuitAppearance.PINLENGTH, bds.getY() + bds.getHeight() - m.getHeight(), bds.getWidth() - 2 * CircuitAppearance.PINLENGTH, m.getHeight());
            } else {
                x = bds.getX() + bds.getWidth() / 2 - m.getAscent();
                y = bds.getY() + bds.getHeight() / 2;
                y_label = y + maxNumber * m.getAscent();
                g.fillRect(bds.getX() + 1, bds.getY() + CircuitAppearance.PINLENGTH, m.getHeight(), bds.getHeight() - 2 * CircuitAppearance.PINLENGTH);
            }
        }
        if (g instanceof Graphics2D && Math.abs(angle) > 0.01) {
            Graphics2D g2 = (Graphics2D) g;
            g2.rotate(angle, x, y);
        }
        g.setFont(font);
        GraphicsUtil.drawCenteredColoredText(g, label, Color.WHITE, Color.BLACK, x, y_label);
        g.dispose();
    }
}
Also used : Instance(com.cburch.logisim.instance.Instance) Direction(com.cburch.logisim.data.Direction) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) Graphics(java.awt.Graphics) FontMetrics(java.awt.FontMetrics) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with Instance

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

the class CircuitPins method transactionCompleted.

public void transactionCompleted(ReplacementMap repl) {
    // determine the changes
    Set<Instance> adds = new HashSet<Instance>();
    Set<Instance> removes = new HashSet<Instance>();
    Map<Instance, Instance> replaces = new HashMap<Instance, Instance>();
    for (Component comp : repl.getAdditions()) {
        if (comp.getFactory() instanceof Pin) {
            Instance in = Instance.getInstanceFor(comp);
            boolean added = pins.add(in);
            if (added) {
                comp.addComponentListener(myComponentListener);
                in.getAttributeSet().addAttributeListener(myComponentListener);
                adds.add(in);
            }
        }
    }
    for (Component comp : repl.getRemovals()) {
        if (comp.getFactory() instanceof Pin) {
            Instance in = Instance.getInstanceFor(comp);
            boolean removed = pins.remove(in);
            if (removed) {
                comp.removeComponentListener(myComponentListener);
                in.getAttributeSet().removeAttributeListener(myComponentListener);
                Collection<Component> rs = repl.getComponentsReplacing(comp);
                if (rs.isEmpty()) {
                    removes.add(in);
                } else {
                    Component r = rs.iterator().next();
                    Instance rin = Instance.getInstanceFor(r);
                    adds.remove(rin);
                    replaces.put(in, rin);
                }
            }
        }
    }
    appearanceManager.updatePorts(adds, removes, replaces, getPins());
}
Also used : Pin(com.cburch.logisim.std.wiring.Pin) Instance(com.cburch.logisim.instance.Instance) HashMap(java.util.HashMap) Component(com.cburch.logisim.comp.Component) HashSet(java.util.HashSet)

Example 9 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) {
    for (Instance pin : pins) {
        dest.add(new AppearancePort(Location.create(x, y), pin));
        x += dx;
        y += dy;
    }
}
Also used : Instance(com.cburch.logisim.instance.Instance)

Example 10 with Instance

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

the class DefaultAppearance method new_build.

private static List<CanvasObject> new_build(Collection<Instance> pins, String CircuitName, Graphics g) {
    Map<Direction, List<Instance>> edge;
    edge = new HashMap<Direction, List<Instance>>();
    edge.put(Direction.EAST, new ArrayList<Instance>());
    edge.put(Direction.WEST, new ArrayList<Instance>());
    int MaxLeftLabelLength = 0;
    int MaxRightLabelLength = 0;
    int TitleWidth = CircuitName.length() * DrawAttr.FixedFontCharWidth;
    if (!pins.isEmpty()) {
        for (Instance pin : pins) {
            Direction pinEdge;
            Text label = new Text(0, 0, pin.getAttributeValue(StdAttr.LABEL));
            int LabelWidth = label.getText().length() * DrawAttr.FixedFontCharWidth;
            if (pin.getAttributeValue(Pin.ATTR_TYPE)) {
                pinEdge = Direction.EAST;
                if (LabelWidth > MaxRightLabelLength)
                    MaxRightLabelLength = LabelWidth;
            } else {
                pinEdge = Direction.WEST;
                if (LabelWidth > MaxLeftLabelLength)
                    MaxLeftLabelLength = LabelWidth;
            }
            List<Instance> e = edge.get(pinEdge);
            e.add(pin);
        }
    }
    for (Map.Entry<Direction, List<Instance>> entry : edge.entrySet()) {
        sortPinList(entry.getValue(), entry.getKey());
    }
    int numEast = edge.get(Direction.EAST).size();
    int numWest = edge.get(Direction.WEST).size();
    int maxVert = Math.max(numEast, numWest);
    int dy = ((DrawAttr.FixedFontHeight + (DrawAttr.FixedFontHeight >> 2) + 5) / 10) * 10;
    int textWidth = (MaxLeftLabelLength + MaxRightLabelLength + 35) < (TitleWidth + 15) ? TitleWidth + 15 : (MaxLeftLabelLength + MaxRightLabelLength + 35);
    int Thight = ((DrawAttr.FixedFontHeight + 10) / 10) * 10;
    int width = (textWidth / 10) * 10 + 20;
    int height = (maxVert > 0) ? maxVert * dy + Thight : 10 + Thight;
    int sdy = (DrawAttr.FixedFontAscent - DrawAttr.FixedFontDescent) >> 1;
    // compute position of anchor relative to top left corner of box
    int ax;
    int ay;
    if (numEast > 0) {
        // anchor is on east side
        ax = width;
        ay = 10;
    } else if (numWest > 0) {
        // anchor is on west side
        ax = 0;
        ay = 10;
    } else {
        // anchor is top left corner
        ax = 0;
        ay = 0;
    }
    // place rectangle so anchor is on the grid
    int rx = OFFS + (9 - (ax + 9) % 10);
    int ry = OFFS + (9 - (ay + 9) % 10);
    List<CanvasObject> ret = new ArrayList<CanvasObject>();
    placePins(ret, edge.get(Direction.WEST), rx, ry + 10, 0, dy, true, sdy);
    placePins(ret, edge.get(Direction.EAST), rx + width, ry + 10, 0, dy, false, sdy);
    Rectangle rect = new Rectangle(rx + 10, ry + height - Thight, width - 20, Thight);
    rect.setValue(DrawAttr.STROKE_WIDTH, Integer.valueOf(1));
    rect.setValue(DrawAttr.PAINT_TYPE, DrawAttr.PAINT_FILL);
    rect.setValue(DrawAttr.FILL_COLOR, Color.black);
    ret.add(rect);
    rect = new Rectangle(rx + 10, ry, width - 20, height);
    rect.setValue(DrawAttr.STROKE_WIDTH, Integer.valueOf(2));
    ret.add(rect);
    Text label = new Text(rx + (width >> 1), ry + (height - DrawAttr.FixedFontDescent - 5), CircuitName);
    label.getLabel().setHorizontalAlignment(EditableLabel.CENTER);
    label.getLabel().setColor(Color.white);
    label.getLabel().setFont(DrawAttr.DEFAULT_NAME_FONT);
    ret.add(label);
    ret.add(new AppearanceAnchor(Location.create(rx + ax, ry + ay)));
    return ret;
}
Also used : Instance(com.cburch.logisim.instance.Instance) ArrayList(java.util.ArrayList) Rectangle(com.cburch.draw.shapes.Rectangle) Text(com.cburch.draw.shapes.Text) Direction(com.cburch.logisim.data.Direction) CanvasObject(com.cburch.draw.model.CanvasObject) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

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