Search in sources :

Example 1 with Text

use of com.cburch.draw.shapes.Text 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 2 with Text

use of com.cburch.draw.shapes.Text 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)

Example 3 with Text

use of com.cburch.draw.shapes.Text in project logisim-evolution by reds-heig.

the class TextTool method commitText.

private void commitText(Canvas canvas) {
    Text cur = curText;
    boolean isNew = isTextNew;
    String newText = field.getText();
    if (cur == null) {
        return;
    }
    cancelText(canvas);
    if (isNew) {
        if (!newText.equals("")) {
            cur.setText(newText);
            canvas.doAction(new ModelAddAction(canvas.getModel(), cur));
        }
    } else {
        String oldText = cur.getText();
        if (newText.equals("")) {
            canvas.doAction(new ModelRemoveAction(canvas.getModel(), cur));
        } else if (!oldText.equals(newText)) {
            canvas.doAction(new ModelEditTextAction(canvas.getModel(), cur, newText));
        }
    }
}
Also used : ModelAddAction(com.cburch.draw.actions.ModelAddAction) ModelRemoveAction(com.cburch.draw.actions.ModelRemoveAction) Text(com.cburch.draw.shapes.Text) ModelEditTextAction(com.cburch.draw.actions.ModelEditTextAction)

Example 4 with Text

use of com.cburch.draw.shapes.Text in project logisim-evolution by reds-heig.

the class TextTool method cancelText.

private void cancelText(Canvas canvas) {
    Text cur = curText;
    if (cur != null) {
        curText = null;
        cur.removeAttributeListener(fieldListener);
        canvas.remove(field);
        canvas.getSelection().clearSelected();
        canvas.repaint();
    }
}
Also used : Text(com.cburch.draw.shapes.Text)

Example 5 with Text

use of com.cburch.draw.shapes.Text in project logisim-evolution by reds-heig.

the class TextTool method mousePressed.

@Override
public void mousePressed(Canvas canvas, MouseEvent e) {
    if (curText != null) {
        commitText(canvas);
    }
    Text clicked = null;
    boolean found = false;
    int mx = e.getX();
    int my = e.getY();
    Location mloc = Location.create(mx, my);
    for (CanvasObject o : canvas.getModel().getObjectsFromTop()) {
        if (o instanceof Text && o.contains(mloc, true)) {
            clicked = (Text) o;
            found = true;
            break;
        }
    }
    if (!found) {
        clicked = attrs.applyTo(new Text(mx, my, ""));
    }
    curText = clicked;
    curCanvas = canvas;
    isTextNew = !found;
    clicked.getLabel().configureTextField(field, canvas.getZoomFactor());
    field.setText(clicked.getText());
    canvas.add(field);
    Point fieldLoc = field.getLocation();
    double zoom = canvas.getZoomFactor();
    fieldLoc.x = (int) Math.round(mx * zoom - fieldLoc.x);
    fieldLoc.y = (int) Math.round(my * zoom - fieldLoc.y);
    int caret = field.viewToModel(fieldLoc);
    if (caret >= 0) {
        field.setCaretPosition(caret);
    }
    field.requestFocus();
    canvas.getSelection().setSelected(clicked, true);
    canvas.getSelection().setHidden(Collections.singleton(clicked), true);
    clicked.addAttributeListener(fieldListener);
    canvas.repaint();
}
Also used : CanvasObject(com.cburch.draw.model.CanvasObject) Text(com.cburch.draw.shapes.Text) Point(java.awt.Point) Point(java.awt.Point) Location(com.cburch.logisim.data.Location)

Aggregations

Text (com.cburch.draw.shapes.Text)6 Instance (com.cburch.logisim.instance.Instance)3 CanvasObject (com.cburch.draw.model.CanvasObject)2 Rectangle (com.cburch.draw.shapes.Rectangle)2 Direction (com.cburch.logisim.data.Direction)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ModelAddAction (com.cburch.draw.actions.ModelAddAction)1 ModelEditTextAction (com.cburch.draw.actions.ModelEditTextAction)1 ModelRemoveAction (com.cburch.draw.actions.ModelRemoveAction)1 Location (com.cburch.logisim.data.Location)1 Color (java.awt.Color)1 Point (java.awt.Point)1