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