Search in sources :

Example 96 with Location

use of com.cburch.logisim.data.Location 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)

Example 97 with Location

use of com.cburch.logisim.data.Location in project logisim-evolution by reds-heig.

the class Analyze method getPinLabels.

// 
// getPinLabels
// 
/**
 * Returns a sorted map from Pin objects to String objects, listed in
 * canonical order (top-down order, with ties broken left-right).
 */
public static SortedMap<Instance, String> getPinLabels(Circuit circuit) {
    Comparator<Instance> locOrder = new Comparator<Instance>() {

        public int compare(Instance ac, Instance bc) {
            Location a = ac.getLocation();
            Location b = bc.getLocation();
            if (a.getY() < b.getY())
                return -1;
            if (a.getY() > b.getY())
                return 1;
            if (a.getX() < b.getX())
                return -1;
            if (a.getX() > b.getX())
                return 1;
            return a.hashCode() - b.hashCode();
        }
    };
    SortedMap<Instance, String> ret = new TreeMap<Instance, String>(locOrder);
    // Put the pins into the TreeMap, with null labels
    for (Instance pin : circuit.getAppearance().getPortOffsets(Direction.EAST).values()) {
        ret.put(pin, null);
    }
    // Process first the pins that the user has given labels.
    ArrayList<Instance> pinList = new ArrayList<Instance>(ret.keySet());
    HashSet<String> labelsTaken = new HashSet<String>();
    for (Instance pin : pinList) {
        String label = pin.getAttributeSet().getValue(StdAttr.LABEL);
        label = toValidLabel(label);
        if (label != null) {
            if (labelsTaken.contains(label)) {
                int i = 2;
                while (labelsTaken.contains(label + i)) i++;
                label = label + i;
            }
            ret.put(pin, label);
            labelsTaken.add(label);
        }
    }
    // Now process the unlabeled pins.
    for (Instance pin : pinList) {
        if (ret.get(pin) != null)
            continue;
        String defaultList;
        if (Pin.FACTORY.isInputPin(pin)) {
            defaultList = Strings.get("defaultInputLabels");
            if (defaultList.indexOf(",") < 0) {
                defaultList = "a,b,c,d,e,f,g,h";
            }
        } else {
            defaultList = Strings.get("defaultOutputLabels");
            if (defaultList.indexOf(",") < 0) {
                defaultList = "x,y,z,u,v,w,s,t";
            }
        }
        String[] options = defaultList.split(",");
        String label = null;
        for (int i = 0; label == null && i < options.length; i++) {
            if (!labelsTaken.contains(options[i])) {
                label = options[i];
            }
        }
        if (label == null) {
            // This is an extreme measure that should never happen
            // if the default labels are defined properly and the
            // circuit doesn't exceed the maximum number of pins.
            int i = 1;
            do {
                i++;
                label = "x" + i;
            } while (labelsTaken.contains(label));
        }
        labelsTaken.add(label);
        ret.put(pin, label);
    }
    return ret;
}
Also used : Instance(com.cburch.logisim.instance.Instance) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) Comparator(java.util.Comparator) Location(com.cburch.logisim.data.Location) HashSet(java.util.HashSet)

Example 98 with Location

use of com.cburch.logisim.data.Location in project logisim-evolution by reds-heig.

the class Poly method insertHandle.

@Override
public void insertHandle(Handle desired, Handle previous) {
    Location loc = desired.getLocation();
    Handle[] hs = handles;
    Handle prev;
    if (previous == null) {
        PolyUtil.ClosestResult result = PolyUtil.getClosestPoint(loc, closed, hs);
        prev = result.getPreviousHandle();
    } else {
        prev = previous;
    }
    Handle[] is = new Handle[hs.length + 1];
    boolean inserted = false;
    for (int i = 0; i < hs.length; i++) {
        if (inserted) {
            is[i + 1] = hs[i];
        } else if (hs[i].equals(prev)) {
            inserted = true;
            is[i] = hs[i];
            is[i + 1] = desired;
        } else {
            is[i] = hs[i];
        }
    }
    if (!inserted) {
        throw new IllegalArgumentException("no such handle");
    }
    setHandles(is);
}
Also used : Location(com.cburch.logisim.data.Location) Handle(com.cburch.draw.model.Handle)

Example 99 with Location

use of com.cburch.logisim.data.Location in project logisim-evolution by reds-heig.

the class Poly method canInsertHandle.

@Override
public Handle canInsertHandle(Location loc) {
    PolyUtil.ClosestResult result = PolyUtil.getClosestPoint(loc, closed, handles);
    int thresh = Math.max(Line.ON_LINE_THRESH, getStrokeWidth() / 2);
    if (result.getDistanceSq() < thresh * thresh) {
        Location resLoc = result.getLocation();
        if (result.getPreviousHandle().isAt(resLoc) || result.getNextHandle().isAt(resLoc)) {
            return null;
        } else {
            return new Handle(this, result.getLocation());
        }
    } else {
        return null;
    }
}
Also used : Location(com.cburch.logisim.data.Location) Handle(com.cburch.draw.model.Handle)

Example 100 with Location

use of com.cburch.logisim.data.Location in project logisim-evolution by reds-heig.

the class SvgCreator method createText.

public static Element createText(Document doc, Text text) {
    Element elt = doc.createElement("text");
    Location loc = text.getLocation();
    Font font = text.getValue(DrawAttr.FONT);
    Color fill = text.getValue(DrawAttr.FILL_COLOR);
    Object halign = text.getValue(DrawAttr.ALIGNMENT);
    elt.setAttribute("x", "" + loc.getX());
    elt.setAttribute("y", "" + loc.getY());
    if (!colorMatches(fill, Color.BLACK)) {
        elt.setAttribute("fill", getColorString(fill));
    }
    if (showOpacity(fill)) {
        elt.setAttribute("fill-opacity", getOpacityString(fill));
    }
    elt.setAttribute("font-family", font.getFamily());
    elt.setAttribute("font-size", "" + font.getSize());
    int style = font.getStyle();
    if ((style & Font.ITALIC) != 0) {
        elt.setAttribute("font-style", "italic");
    }
    if ((style & Font.BOLD) != 0) {
        elt.setAttribute("font-weight", "bold");
    }
    if (halign == DrawAttr.ALIGN_LEFT) {
        elt.setAttribute("text-anchor", "start");
    } else if (halign == DrawAttr.ALIGN_RIGHT) {
        elt.setAttribute("text-anchor", "end");
    } else {
        elt.setAttribute("text-anchor", "middle");
    }
    elt.appendChild(doc.createTextNode(text.getText()));
    return elt;
}
Also used : Element(org.w3c.dom.Element) Color(java.awt.Color) AbstractCanvasObject(com.cburch.draw.model.AbstractCanvasObject) Font(java.awt.Font) Location(com.cburch.logisim.data.Location)

Aggregations

Location (com.cburch.logisim.data.Location)169 Direction (com.cburch.logisim.data.Direction)39 Graphics (java.awt.Graphics)35 Component (com.cburch.logisim.comp.Component)26 Bounds (com.cburch.logisim.data.Bounds)24 ArrayList (java.util.ArrayList)23 CanvasObject (com.cburch.draw.model.CanvasObject)17 BitWidth (com.cburch.logisim.data.BitWidth)16 Wire (com.cburch.logisim.circuit.Wire)13 HashMap (java.util.HashMap)10 Handle (com.cburch.draw.model.Handle)9 AttributeSet (com.cburch.logisim.data.AttributeSet)9 Value (com.cburch.logisim.data.Value)9 Instance (com.cburch.logisim.instance.Instance)9 Port (com.cburch.logisim.instance.Port)9 Color (java.awt.Color)9 Graphics2D (java.awt.Graphics2D)9 HashSet (java.util.HashSet)9 Circuit (com.cburch.logisim.circuit.Circuit)8 EndData (com.cburch.logisim.comp.EndData)7