Search in sources :

Example 26 with Instance

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

the class PortManager method computeDefaultLocation.

private static Location computeDefaultLocation(CircuitAppearance appear, Instance pin, Map<Instance, AppearancePort> others) {
    // Determine which locations are being used in canvas, and look for
    // which instances facing the same way in layout
    Set<Location> usedLocs = new HashSet<Location>();
    List<Instance> sameWay = new ArrayList<Instance>();
    Direction facing = pin.getAttributeValue(StdAttr.FACING);
    for (Map.Entry<Instance, AppearancePort> entry : others.entrySet()) {
        Instance pin2 = entry.getKey();
        Location loc = entry.getValue().getLocation();
        usedLocs.add(loc);
        if (pin2.getAttributeValue(StdAttr.FACING) == facing) {
            sameWay.add(pin2);
        }
    }
    // if at least one faces the same way, place pin relative to that
    if (sameWay.size() > 0) {
        sameWay.add(pin);
        DefaultAppearance.sortPinList(sameWay, facing);
        boolean isFirst = false;
        // (preferably previous in map)
        Instance neighbor = null;
        for (Instance p : sameWay) {
            if (p == pin) {
                break;
            } else {
                neighbor = p;
            }
        }
        if (neighbor == null) {
            // pin must have been first in list
            neighbor = sameWay.get(1);
        }
        int dx;
        int dy;
        if (facing == Direction.EAST || facing == Direction.WEST) {
            dx = 0;
            dy = isFirst ? -10 : 10;
        } else {
            dx = isFirst ? -10 : 10;
            dy = 0;
        }
        Location loc = others.get(neighbor).getLocation();
        do {
            loc = loc.translate(dx, dy);
        } while (usedLocs.contains(loc));
        if (loc.getX() >= 0 && loc.getY() >= 0) {
            return loc;
        }
        do {
            loc = loc.translate(-dx, -dy);
        } while (usedLocs.contains(loc));
        return loc;
    }
    // otherwise place it on the boundary of the bounding rectangle
    Bounds bds = appear.getAbsoluteBounds();
    int x;
    int y;
    int dx = 0;
    int dy = 0;
    if (facing == Direction.EAST) {
        // on west side by default
        x = bds.getX() - 7;
        y = bds.getY() + 5;
        dy = 10;
    } else if (facing == Direction.WEST) {
        // on east side by default
        x = bds.getX() + bds.getWidth() - 3;
        y = bds.getY() + 5;
        dy = 10;
    } else if (facing == Direction.SOUTH) {
        // on north side by default
        x = bds.getX() + 5;
        y = bds.getY() - 7;
        dx = 10;
    } else {
        // on south side by default
        x = bds.getX() + 5;
        y = bds.getY() + bds.getHeight() - 3;
        dx = 10;
    }
    // round coordinates up to ensure they're on grid
    x = (x + 9) / 10 * 10;
    y = (y + 9) / 10 * 10;
    Location loc = Location.create(x, y);
    while (usedLocs.contains(loc)) {
        loc = loc.translate(dx, dy);
    }
    return loc;
}
Also used : Instance(com.cburch.logisim.instance.Instance) Bounds(com.cburch.logisim.data.Bounds) ArrayList(java.util.ArrayList) Direction(com.cburch.logisim.data.Direction) HashMap(java.util.HashMap) Map(java.util.Map) Location(com.cburch.logisim.data.Location) HashSet(java.util.HashSet)

Example 27 with Instance

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

the class CircuitAppearance method getPortOffsets.

public SortedMap<Location, Instance> getPortOffsets(Direction facing) {
    Location anchor = null;
    Direction defaultFacing = Direction.EAST;
    List<AppearancePort> ports = new ArrayList<AppearancePort>();
    for (CanvasObject shape : getObjectsFromBottom()) {
        if (shape instanceof AppearancePort) {
            ports.add((AppearancePort) shape);
        } else if (shape instanceof AppearanceAnchor) {
            AppearanceAnchor o = (AppearanceAnchor) shape;
            anchor = o.getLocation();
            defaultFacing = o.getFacing();
        }
    }
    SortedMap<Location, Instance> ret = new TreeMap<Location, Instance>();
    for (AppearancePort port : ports) {
        Location loc = port.getLocation();
        if (anchor != null) {
            loc = loc.translate(-anchor.getX(), -anchor.getY());
        }
        if (facing != defaultFacing) {
            loc = loc.rotate(defaultFacing, facing, 0, 0);
        }
        ret.put(loc, port.getPin());
    }
    return ret;
}
Also used : CanvasObject(com.cburch.draw.model.CanvasObject) Instance(com.cburch.logisim.instance.Instance) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) Direction(com.cburch.logisim.data.Direction) Location(com.cburch.logisim.data.Location)

Example 28 with Instance

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

the class Text method paintGhost.

// 
// graphics methods
// 
@Override
public void paintGhost(InstancePainter painter) {
    TextAttributes attrs = (TextAttributes) painter.getAttributeSet();
    String text = attrs.getText();
    if (text == null || text.equals(""))
        return;
    int halign = attrs.getHorizontalAlign();
    int valign = attrs.getVerticalAlign();
    Graphics g = painter.getGraphics();
    Font old = g.getFont();
    g.setFont(attrs.getFont());
    GraphicsUtil.drawText(g, text, 0, 0, halign, valign);
    String textTrim = text.endsWith(" ") ? text.substring(0, text.length() - 1) : text;
    Bounds newBds;
    if (textTrim.equals("")) {
        newBds = Bounds.EMPTY_BOUNDS;
    } else {
        Rectangle bdsOut = GraphicsUtil.getTextBounds(g, textTrim, 0, 0, halign, valign);
        newBds = Bounds.create(bdsOut).expand(4);
    }
    if (attrs.setOffsetBounds(newBds)) {
        Instance instance = painter.getInstance();
        if (instance != null)
            instance.recomputeBounds();
    }
    g.setFont(old);
}
Also used : Graphics(java.awt.Graphics) Instance(com.cburch.logisim.instance.Instance) Bounds(com.cburch.logisim.data.Bounds) Rectangle(java.awt.Rectangle) Font(java.awt.Font)

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