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;
}
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;
}
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);
}
Aggregations