use of com.cburch.logisim.data.Direction 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();
}
}
use of com.cburch.logisim.data.Direction in project logisim-evolution by reds-heig.
the class SubcircuitFactory method paintBase.
private void paintBase(InstancePainter painter, Graphics g) {
CircuitAttributes attrs = (CircuitAttributes) painter.getAttributeSet();
Direction facing = attrs.getFacing();
Direction defaultFacing = source.getAppearance().getFacing();
Location loc = painter.getLocation();
g.translate(loc.getX(), loc.getY());
source.getAppearance().paintSubcircuit(g, facing);
drawCircuitLabel(painter, getOffsetBounds(attrs), facing, defaultFacing);
g.translate(-loc.getX(), -loc.getY());
painter.drawLabel();
}
use of com.cburch.logisim.data.Direction in project logisim-evolution by reds-heig.
the class SubcircuitFactory method drawCircuitLabel.
private void drawCircuitLabel(InstancePainter painter, Bounds bds, Direction facing, Direction defaultFacing) {
AttributeSet staticAttrs = source.getStaticAttributes();
String label = staticAttrs.getValue(CircuitAttributes.CIRCUIT_LABEL_ATTR);
if (label != null && !label.equals("")) {
Direction up = staticAttrs.getValue(CircuitAttributes.CIRCUIT_LABEL_FACING_ATTR);
Font font = staticAttrs.getValue(CircuitAttributes.CIRCUIT_LABEL_FONT_ATTR);
int back = label.indexOf('\\');
int lines = 1;
boolean backs = false;
while (back >= 0 && back <= label.length() - 2) {
char c = label.charAt(back + 1);
if (c == 'n')
lines++;
else if (c == '\\')
backs = true;
back = label.indexOf('\\', back + 2);
}
int x = bds.getX() + bds.getWidth() / 2;
int y = bds.getY() + bds.getHeight() / 2;
Graphics g = painter.getGraphics().create();
double angle = Math.PI / 2 - (up.toRadians() - defaultFacing.toRadians()) - facing.toRadians();
if (g instanceof Graphics2D && Math.abs(angle) > 0.01) {
Graphics2D g2 = (Graphics2D) g;
g2.rotate(angle, x, y);
}
g.setFont(font);
if (lines == 1 && !backs) {
GraphicsUtil.drawCenteredText(g, label, x, y);
} else {
FontMetrics fm = g.getFontMetrics();
int height = fm.getHeight();
y = y - (height * lines - fm.getLeading()) / 2 + fm.getAscent();
back = label.indexOf('\\');
while (back >= 0 && back <= label.length() - 2) {
char c = label.charAt(back + 1);
if (c == 'n') {
String line = label.substring(0, back);
GraphicsUtil.drawText(g, line, x, y, GraphicsUtil.H_CENTER, GraphicsUtil.V_BASELINE);
y += height;
label = label.substring(back + 2);
back = label.indexOf('\\');
} else if (c == '\\') {
label = label.substring(0, back) + label.substring(back + 1);
back = label.indexOf('\\', back + 1);
} else {
back = label.indexOf('\\', back + 2);
}
}
GraphicsUtil.drawText(g, label, x, y, GraphicsUtil.H_CENTER, GraphicsUtil.V_BASELINE);
}
g.dispose();
}
}
use of com.cburch.logisim.data.Direction 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;
}
use of com.cburch.logisim.data.Direction in project logisim-evolution by reds-heig.
the class DefaultAppearance method old_build.
private static List<CanvasObject> old_build(Collection<Instance> pins) {
Map<Direction, List<Instance>> edge;
edge = new HashMap<Direction, List<Instance>>();
edge.put(Direction.NORTH, new ArrayList<Instance>());
edge.put(Direction.SOUTH, new ArrayList<Instance>());
edge.put(Direction.EAST, new ArrayList<Instance>());
edge.put(Direction.WEST, new ArrayList<Instance>());
for (Instance pin : pins) {
Direction pinFacing = pin.getAttributeValue(StdAttr.FACING);
Direction pinEdge = pinFacing.reverse();
List<Instance> e = edge.get(pinEdge);
e.add(pin);
}
for (Map.Entry<Direction, List<Instance>> entry : edge.entrySet()) {
sortPinList(entry.getValue(), entry.getKey());
}
int numNorth = edge.get(Direction.NORTH).size();
int numSouth = edge.get(Direction.SOUTH).size();
int numEast = edge.get(Direction.EAST).size();
int numWest = edge.get(Direction.WEST).size();
int maxVert = Math.max(numNorth, numSouth);
int maxHorz = Math.max(numEast, numWest);
int offsNorth = computeOffset(numNorth, numSouth, maxHorz);
int offsSouth = computeOffset(numSouth, numNorth, maxHorz);
int offsEast = computeOffset(numEast, numWest, maxVert);
int offsWest = computeOffset(numWest, numEast, maxVert);
int width = computeDimension(maxVert, maxHorz);
int height = computeDimension(maxHorz, maxVert);
// 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 = offsEast;
} else if (numNorth > 0) {
// anchor is on north side
ax = offsNorth;
ay = 0;
} else if (numWest > 0) {
// anchor is on west side
ax = 0;
ay = offsWest;
} else if (numSouth > 0) {
// anchor is on south side
ax = offsSouth;
ay = height;
} 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);
Location e0 = Location.create(rx + (width - 8) / 2, ry + 1);
Location e1 = Location.create(rx + (width + 8) / 2, ry + 1);
Location ct = Location.create(rx + width / 2, ry + 11);
Curve notch = new Curve(e0, e1, ct);
notch.setValue(DrawAttr.STROKE_WIDTH, Integer.valueOf(2));
notch.setValue(DrawAttr.STROKE_COLOR, Color.GRAY);
Rectangle rect = new Rectangle(rx, ry, width, height);
rect.setValue(DrawAttr.STROKE_WIDTH, Integer.valueOf(2));
List<CanvasObject> ret = new ArrayList<CanvasObject>();
ret.add(notch);
ret.add(rect);
placePins(ret, edge.get(Direction.WEST), rx, ry + offsWest, 0, 10);
placePins(ret, edge.get(Direction.EAST), rx + width, ry + offsEast, 0, 10);
placePins(ret, edge.get(Direction.NORTH), rx + offsNorth, ry, 10, 0);
placePins(ret, edge.get(Direction.SOUTH), rx + offsSouth, ry + height, 10, 0);
ret.add(new AppearanceAnchor(Location.create(rx + ax, ry + ay)));
return ret;
}
Aggregations