Beispiel #1
0
  @Override
  public Bounds getOffsetBounds(AttributeSet attrsBase) {
    GateAttributes attrs = (GateAttributes) attrsBase;
    Direction facing = attrs.facing;
    int size = ((Integer) attrs.size.getValue()).intValue();
    int inputs = attrs.inputs;
    if (inputs % 2 == 0) {
      inputs++;
    }
    int negated = attrs.negated;

    int width = size + bonusWidth + (negateOutput ? 10 : 0);
    if (negated != 0) {
      width += 10;
    }
    int height = Math.max(10 * inputs, size);
    if (facing == Direction.SOUTH) {
      return Bounds.create(-height / 2, -width, height, width);
    } else if (facing == Direction.NORTH) {
      return Bounds.create(-height / 2, 0, height, width);
    } else if (facing == Direction.WEST) {
      return Bounds.create(0, -height / 2, width, height);
    } else {
      return Bounds.create(-width, -height / 2, width, height);
    }
  }
 private void computeSize(boolean immediate) {
   hidePopup();
   Bounds bounds;
   CircuitState circState = circuitState;
   if (circState == null) {
     bounds = Bounds.create(0, 0, 50, 50);
   } else {
     bounds = circState.getCircuit().getAppearance().getAbsoluteBounds();
   }
   int width = bounds.getX() + bounds.getWidth() + BOUNDS_BUFFER;
   int height = bounds.getY() + bounds.getHeight() + BOUNDS_BUFFER;
   Size dim;
   if (canvasPane == null) {
     dim = new Size(width, height);
   } else {
     dim = canvasPane.supportPreferredSize(width, height);
   }
   if (!immediate) {
     Bounds old = oldPreferredSize;
     if (old != null
         && Math.abs(old.getWidth() - dim.width) < THRESH_SIZE_UPDATE
         && Math.abs(old.getHeight() - dim.height) < THRESH_SIZE_UPDATE) {
       return;
     }
   }
   oldPreferredSize = Bounds.create(0, 0, dim.width, dim.height);
   setPreferredSize(dim);
   revalidate();
 }
Beispiel #3
0
 static void drawALU(Graphics g, Bounds bds) {
   int wid = bds.getWidth();
   int ht = bds.getHeight();
   int x0 = bds.getX();
   int x1 = x0 + wid;
   int y0 = bds.getY();
   int y1 = y0 + ht;
   int xp[] = {x0, x1, x1, x0, x0, x0 + 20, x0};
   int yp[] = {y0, y0 + 30, y1 - 30, y1, y1 - 40, y1 - 50, y1 - 60};
   GraphicsUtil.switchToWidth(g, 2);
   g.drawPolygon(xp, yp, 7);
 }
Beispiel #4
0
 @Override
 public Bounds getOffsetBounds(AttributeSet attrs) {
   Direction facing = attrs.getValue(StdAttr.FACING);
   if (facing == Direction.EAST) {
     return Bounds.create(-42, -6, 42, 12);
   } else if (facing == Direction.WEST) {
     return Bounds.create(0, -6, 42, 12);
   } else if (facing == Direction.NORTH) {
     return Bounds.create(-6, 0, 12, 42);
   } else {
     return Bounds.create(-6, -42, 12, 42);
   }
 }
Beispiel #5
0
 public Bounds getBounds(long addr, Bounds bds) {
   int addrBits = getAddrBits();
   int boxX = bds.getX() + (addrBits <= 12 ? ENTRY_XOFFS12 : ENTRY_XOFFS32);
   int boxW = addrBits <= 12 ? TABLE_WIDTH12 : TABLE_WIDTH32;
   if (addr < 0) {
     int addrLen = (contents.getWidth() + 3) / 4;
     int width = ADDR_WIDTH_PER_CHAR * addrLen;
     return Bounds.create(boxX - width, bds.getY() + ENTRY_YOFFS, width, ENTRY_HEIGHT);
   } else {
     int bdsX = addrToX(bds, addr);
     int bdsY = addrToY(bds, addr);
     return Bounds.create(bdsX, bdsY, boxW / columns, ENTRY_HEIGHT);
   }
 }
Beispiel #6
0
 @Override
 public void translate(int dx, int dy) {
     p0 = p0.translate(dx, dy);
     p1 = p1.translate(dx, dy);
     p2 = p2.translate(dx, dy);
     bounds = bounds.translate(dx, dy);
 }
Beispiel #7
0
  private int addrToY(Bounds bds, long addr) {
    long topRow = curScroll / columns;
    long row = addr / columns;
    if (row < topRow || row >= topRow + ROWS) {
      return -1;
    }

    return (int) (bds.getY() + ENTRY_YOFFS + ENTRY_HEIGHT * (row - topRow));
  }
Beispiel #8
0
 @Override
 public void mouseReleased(Canvas canvas, Graphics g, MouseEvent e) {
   Project proj = canvas.getProject();
   if (state == MOVING) {
     setState(proj, IDLE);
     computeDxDy(proj, e, g);
     int dx = curDx;
     int dy = curDy;
     if (dx != 0 || dy != 0) {
       if (!proj.getLogisimFile().contains(canvas.getCircuit())) {
         canvas.setErrorMessage(Strings.getter("cannotModifyError"));
       } else if (proj.getSelection().hasConflictWhenMoved(dx, dy)) {
         canvas.setErrorMessage(Strings.getter("exclusiveError"));
       } else {
         boolean connect = shouldConnect(canvas, e.getModifiersEx());
         drawConnections = false;
         ReplacementMap repl;
         if (connect) {
           MoveGesture gesture = moveGesture;
           if (gesture == null) {
             gesture =
                 new MoveGesture(
                     new MoveRequestHandler(canvas),
                     canvas.getCircuit(),
                     canvas.getSelection().getAnchoredComponents());
           }
           canvas.setErrorMessage(new ComputingMessage(dx, dy), COLOR_COMPUTING);
           MoveResult result = gesture.forceRequest(dx, dy);
           clearCanvasMessage(canvas, dx, dy);
           repl = result.getReplacementMap();
         } else {
           repl = null;
         }
         Selection sel = proj.getSelection();
         proj.doAction(SelectionActions.translate(sel, dx, dy, repl));
       }
     }
     moveGesture = null;
     proj.repaintCanvas();
   } else if (state == RECT_SELECT) {
     Bounds bds = Bounds.create(start).add(start.getX() + curDx, start.getY() + curDy);
     Circuit circuit = canvas.getCircuit();
     Selection sel = proj.getSelection();
     Collection<Component> in_sel = sel.getComponentsWithin(bds, g);
     for (Component comp : circuit.getAllWithin(bds, g)) {
       if (!in_sel.contains(comp)) sel.add(comp);
     }
     Action act = SelectionActions.drop(sel, in_sel);
     if (act != null) {
       proj.doAction(act);
     }
     setState(proj, IDLE);
     proj.repaintCanvas();
   }
 }
Beispiel #9
0
  private void computeDxDy(Project proj, MouseEvent e, Graphics g) {
    Bounds bds = proj.getSelection().getBounds(g);
    int dx;
    int dy;
    if (bds == Bounds.EMPTY_BOUNDS) {
      dx = e.getX() - start.getX();
      dy = e.getY() - start.getY();
    } else {
      dx = Math.max(e.getX() - start.getX(), -bds.getX());
      dy = Math.max(e.getY() - start.getY(), -bds.getY());
    }

    Selection sel = proj.getSelection();
    if (sel.shouldSnap()) {
      dx = Canvas.snapXToGrid(dx);
      dy = Canvas.snapYToGrid(dy);
    }
    curDx = dx;
    curDy = dy;
  }
  //
  // graphics methods
  //
  @Override
  public void paintInstance(InstancePainter painter) {
    Graphics g = painter.getGraphics();
    FontMetrics fm = g.getFontMetrics();
    int asc = fm.getAscent();

    painter.drawBounds();

    String s0;
    String type = getType(painter.getAttributeSet());
    if (type.equals("zero")) s0 = Strings.get("extenderZeroLabel");
    else if (type.equals("one")) s0 = Strings.get("extenderOneLabel");
    else if (type.equals("sign")) s0 = Strings.get("extenderSignLabel");
    else if (type.equals("input")) s0 = Strings.get("extenderInputLabel");
    else s0 = "???"; // should never happen
    String s1 = Strings.get("extenderMainLabel");
    Bounds bds = painter.getBounds();
    int x = bds.getX() + bds.getWidth() / 2;
    int y0 = bds.getY() + (bds.getHeight() / 2 + asc) / 2;
    int y1 = bds.getY() + (3 * bds.getHeight() / 2 + asc) / 2;
    GraphicsUtil.drawText(g, s0, x, y0, GraphicsUtil.H_CENTER, GraphicsUtil.V_BASELINE);
    GraphicsUtil.drawText(g, s1, x, y1, GraphicsUtil.H_CENTER, GraphicsUtil.V_BASELINE);

    BitWidth w0 = painter.getAttributeValue(ATTR_OUT_WIDTH);
    BitWidth w1 = painter.getAttributeValue(ATTR_IN_WIDTH);
    painter.drawPort(0, "" + w0.getWidth(), Direction.WEST);
    painter.drawPort(1, "" + w1.getWidth(), Direction.EAST);
    if (type.equals("input")) painter.drawPort(2);
  }
 public BitExtender() {
   super("Bit Extender", Strings.getter("extenderComponent"));
   setIconName("extender.gif");
   setAttributes(
       new Attribute[] {ATTR_IN_WIDTH, ATTR_OUT_WIDTH, ATTR_TYPE},
       new Object[] {BitWidth.create(8), BitWidth.create(16), ATTR_TYPE.parse("sign")});
   setFacingAttribute(StdAttr.FACING);
   setKeyConfigurator(
       JoinedConfigurator.create(
           new BitWidthConfigurator(ATTR_OUT_WIDTH),
           new BitWidthConfigurator(ATTR_IN_WIDTH, 1, Value.MAX_WIDTH, 0)));
   setOffsetBounds(Bounds.create(-40, -20, 40, 40));
 }
Beispiel #12
0
  public Negator() {
    super("Negator", __("negatorComponent"));
    setAttributes(new Attribute[] {StdAttr.WIDTH}, new Object[] {BitWidth.create(8)});
    setKeyConfigurator(new BitWidthConfigurator(StdAttr.WIDTH));
    setOffsetBounds(Bounds.create(-40, -20, 40, 40));
    setIconName("negator.svg");

    Port[] ps = new Port[2];
    ps[IN] = new Port(-40, 0, Port.INPUT, StdAttr.WIDTH);
    ps[OUT] = new Port(0, 0, Port.OUTPUT, StdAttr.WIDTH);
    ps[IN].setToolTip(__("negatorInputTip"));
    ps[OUT].setToolTip(__("negatorOutputTip"));
    setPorts(ps);
  }
Beispiel #13
0
 @Override
 public Bounds getOffsetBounds(AttributeSet attrs) {
   Object value = attrs.getValue(ATTR_SIZE);
   if (value == SIZE_NARROW) {
     Direction facing = attrs.getValue(StdAttr.FACING);
     if (facing == Direction.SOUTH) return Bounds.create(-9, -20, 18, 20);
     if (facing == Direction.NORTH) return Bounds.create(-9, 0, 18, 20);
     if (facing == Direction.WEST) return Bounds.create(0, -9, 20, 18);
     return Bounds.create(-20, -9, 20, 18);
   } else {
     Direction facing = attrs.getValue(StdAttr.FACING);
     if (facing == Direction.SOUTH) return Bounds.create(-9, -30, 18, 30);
     if (facing == Direction.NORTH) return Bounds.create(-9, 0, 18, 30);
     if (facing == Direction.WEST) return Bounds.create(0, -9, 30, 18);
     return Bounds.create(-30, -9, 30, 18);
   }
 }
Beispiel #14
0
 static void configureLabel(Instance instance, boolean isRectangular, Location control) {
   Object facing = instance.getAttributeValue(StdAttr.FACING);
   Bounds bds = instance.getBounds();
   int x;
   int y;
   int halign;
   if (facing == Direction.NORTH || facing == Direction.SOUTH) {
     x = bds.getX() + bds.getWidth() / 2 + 2;
     y = bds.getY() - 2;
     halign = TextField.H_LEFT;
   } else { // west or east
     y = isRectangular ? bds.getY() - 2 : bds.getY();
     if (control != null && control.getY() == bds.getY()) {
       // the control line will get in the way
       x = control.getX() + 2;
       halign = TextField.H_LEFT;
     } else {
       x = bds.getX() + bds.getWidth() / 2;
       halign = TextField.H_CENTER;
     }
   }
   instance.setTextField(StdAttr.LABEL, StdAttr.LABEL_FONT, x, y, halign, TextField.V_BASELINE);
 }
Beispiel #15
0
  private int addrToX(Bounds bds, long addr) {
    int addrBits = getAddrBits();
    int boxX = bds.getX() + (addrBits <= 12 ? ENTRY_XOFFS12 : ENTRY_XOFFS32);
    int boxW = addrBits <= 12 ? TABLE_WIDTH12 : TABLE_WIDTH32;

    long topRow = curScroll / columns;
    long row = addr / columns;
    if (row < topRow || row >= topRow + ROWS) {
      return -1;
    }

    int col = (int) (addr - row * columns);
    if (col < 0 || col >= columns) {
      return -1;
    }

    return boxX + boxW * col / columns;
  }
Beispiel #16
0
 @Override
 public boolean contains(Location loc, AttributeSet attrsBase) {
   GateAttributes attrs = (GateAttributes) attrsBase;
   if (super.contains(loc, attrs)) {
     if (attrs.negated == 0) {
       return true;
     } else {
       Direction facing = attrs.facing;
       Bounds bds = getOffsetBounds(attrsBase);
       int delt;
       if (facing == Direction.NORTH) {
         delt = loc.getY() - (bds.getY() + bds.getHeight());
       } else if (facing == Direction.SOUTH) {
         delt = loc.getY() - bds.getY();
       } else if (facing == Direction.WEST) {
         delt = loc.getX() - (bds.getX() + bds.getHeight());
       } else {
         delt = loc.getX() - bds.getX();
       }
       if (Math.abs(delt) > 5) {
         return true;
       } else {
         int inputs = attrs.inputs;
         for (int i = 1; i <= inputs; i++) {
           Location offs = getInputOffset(attrs, i);
           if (loc.manhattanDistanceTo(offs) <= 5) {
             return true;
           }
         }
         return false;
       }
     }
   } else {
     return false;
   }
 }
 public void drawRectangle(Bounds bds, String label) {
   context.drawRectangle(bds.getX(), bds.getY(), bds.getWidth(), bds.getHeight(), label);
 }
Beispiel #18
0
 @Override
 public Bounds getOffsetBounds(AttributeSet attrs) {
   return Bounds.create(0, -8, 14, 16)
       .rotate(Direction.EAST, attrs.getValue(StdAttr.FACING), 0, 0);
 }
Beispiel #19
0
  private void drawInstance(InstancePainter painter, boolean isGhost) {
    Bounds bds = painter.getBounds();
    Object powerLoc = painter.getAttributeValue(Wiring.ATTR_GATE);
    Direction facing = painter.getAttributeValue(StdAttr.FACING);
    boolean flip =
        (facing == Direction.SOUTH || facing == Direction.WEST)
            == (powerLoc == Wiring.GATE_TOP_LEFT);

    int degrees = Direction.WEST.toDegrees() - facing.toDegrees();
    if (flip) {
      degrees += 180;
    }

    double radians = Math.toRadians((degrees + 360) % 360);

    Graphics2D g = (Graphics2D) painter.getGraphics().create();
    g.rotate(radians, bds.getX() + 20, bds.getY() + 20);
    g.translate(bds.getX(), bds.getY());
    GraphicsUtil.switchToWidth(g, Wire.WIDTH);

    Color gate0 = g.getColor();
    Color gate1 = gate0;
    Color input = gate0;
    Color output = gate0;
    Color platform = gate0;
    if (!isGhost && painter.getShowState()) {
      gate0 = painter.getPort(GATE0).getColor();
      gate1 = painter.getPort(GATE0).getColor();
      input = painter.getPort(INPUT).getColor();
      output = painter.getPort(OUTPUT).getColor();
      platform = computeOutput(painter).getColor();
    }

    g.setColor(flip ? input : output);
    g.drawLine(0, 20, 11, 20);
    g.drawLine(11, 13, 11, 27);

    g.setColor(flip ? output : input);
    g.drawLine(29, 20, 40, 20);
    g.drawLine(29, 13, 29, 27);

    g.setColor(gate0);
    g.drawLine(20, 35, 20, 40);
    GraphicsUtil.switchToWidth(g, 1);
    g.drawOval(18, 30, 4, 4);
    g.drawLine(10, 30, 30, 30);
    GraphicsUtil.switchToWidth(g, Wire.WIDTH);

    g.setColor(gate1);
    g.drawLine(20, 9, 20, 0);
    GraphicsUtil.switchToWidth(g, 1);
    g.drawLine(10, 10, 30, 10);

    g.setColor(platform);
    g.drawLine(9, 12, 31, 12);
    g.drawLine(9, 28, 31, 28);
    // arrow
    if (flip) {
      g.drawLine(18, 17, 21, 20);
      g.drawLine(18, 23, 21, 20);
    } else {
      g.drawLine(22, 17, 19, 20);
      g.drawLine(22, 23, 19, 20);
    }

    g.dispose();
  }
Beispiel #20
0
 @Override
 public Bounds getOffsetBounds(AttributeSet attrs) {
   Direction facing = attrs.getValue(StdAttr.FACING);
   return Bounds.create(0, -20, 40, 40).rotate(Direction.WEST, facing, 0, 0);
 }
Beispiel #21
0
  @Override
  public void draw(Canvas canvas, ComponentDrawContext context) {
    Project proj = canvas.getProject();
    int dx = curDx;
    int dy = curDy;
    if (state == MOVING) {
      proj.getSelection().drawGhostsShifted(context, dx, dy);

      MoveGesture gesture = moveGesture;
      if (gesture != null && drawConnections && (dx != 0 || dy != 0)) {
        MoveResult result = gesture.findResult(dx, dy);
        if (result != null) {
          Collection<Wire> wiresToAdd = result.getWiresToAdd();
          Graphics g = context.getGraphics();
          GraphicsUtil.switchToWidth(g, 3);
          g.setColor(Color.GRAY);
          for (Wire w : wiresToAdd) {
            Location loc0 = w.getEnd0();
            Location loc1 = w.getEnd1();
            g.drawLine(loc0.getX(), loc0.getY(), loc1.getX(), loc1.getY());
          }
          GraphicsUtil.switchToWidth(g, 1);
          g.setColor(COLOR_UNMATCHED);
          for (Location conn : result.getUnconnectedLocations()) {
            int connX = conn.getX();
            int connY = conn.getY();
            g.fillOval(connX - 3, connY - 3, 6, 6);
            g.fillOval(connX + dx - 3, connY + dy - 3, 6, 6);
          }
        }
      }
    } else if (state == RECT_SELECT) {
      int left = start.getX();
      int right = left + dx;
      if (left > right) {
        int i = left;
        left = right;
        right = i;
      }
      int top = start.getY();
      int bot = top + dy;
      if (top > bot) {
        int i = top;
        top = bot;
        bot = i;
      }

      Graphics gBase = context.getGraphics();
      int w = right - left - 1;
      int h = bot - top - 1;
      if (w > 2 && h > 2) {
        gBase.setColor(BACKGROUND_RECT_SELECT);
        gBase.fillRect(left + 1, top + 1, w - 1, h - 1);
      }

      Circuit circ = canvas.getCircuit();
      Bounds bds = Bounds.create(left, top, right - left, bot - top);
      for (Component c : circ.getAllWithin(bds)) {
        Location cloc = c.getLocation();
        Graphics gDup = gBase.create();
        context.setGraphics(gDup);
        c.getFactory()
            .drawGhost(context, COLOR_RECT_SELECT, cloc.getX(), cloc.getY(), c.getAttributeSet());
        gDup.dispose();
      }

      gBase.setColor(COLOR_RECT_SELECT);
      GraphicsUtil.switchToWidth(gBase, 2);
      if (w < 0) w = 0;
      if (h < 0) h = 0;
      gBase.drawRect(left, top, w, h);
    }
  }
Beispiel #22
0
  private void paintBase(InstancePainter painter) {
    GateAttributes attrs = (GateAttributes) painter.getAttributeSet();
    Direction facing = attrs.facing;
    int inputs = attrs.inputs;
    int negated = attrs.negated;

    Object shape = painter.getGateShape();
    Location loc = painter.getLocation();
    Bounds bds = painter.getOffsetBounds();
    int width = bds.getWidth();
    int height = bds.getHeight();
    if (facing == Direction.NORTH || facing == Direction.SOUTH) {
      int t = width;
      width = height;
      height = t;
    }
    if (negated != 0) {
      width -= 10;
    }

    Graphics g = painter.getGraphics();
    Color baseColor = g.getColor();
    if (shape == AppPreferences.SHAPE_SHAPED && paintInputLines) {
      PainterShaped.paintInputLines(painter, this);
    } else if (negated != 0) {
      for (int i = 0; i < inputs; i++) {
        int negatedBit = (negated >> i) & 1;
        if (negatedBit == 1) {
          Location in = getInputOffset(attrs, i);
          Location cen = in.translate(facing, 5);
          painter.drawDongle(loc.getX() + cen.getX(), loc.getY() + cen.getY());
        }
      }
    }

    g.setColor(baseColor);
    g.translate(loc.getX(), loc.getY());
    double rotate = 0.0;
    if (facing != Direction.EAST && g instanceof Graphics2D) {
      rotate = -facing.toRadians();
      Graphics2D g2 = (Graphics2D) g;
      g2.rotate(rotate);
    }

    if (shape == AppPreferences.SHAPE_RECTANGULAR) {
      paintRectangular(painter, width, height);
    } else if (shape == AppPreferences.SHAPE_DIN40700) {
      paintDinShape(painter, width, height, inputs);
      // SHAPE_SHAPED
    } else {
      if (negateOutput) {
        g.translate(-10, 0);
        paintShape(painter, width - 10, height);
        painter.drawDongle(5, 0);
        g.translate(10, 0);
      } else {
        paintShape(painter, width, height);
      }
    }

    if (rotate != 0.0) {
      ((Graphics2D) g).rotate(-rotate);
    }
    g.translate(-loc.getX(), -loc.getY());

    painter.drawLabel();
  }
Beispiel #23
0
 public Bounds getOffsetBounds(AttributeSet attrs) {
   return Bounds.create(-30, -50, 60, 100);
 }
Beispiel #24
0
 public Bounds getBounds() {
   int x0 = e0.getX();
   int y0 = e0.getY();
   return Bounds.create(x0 - 2, y0 - 2, e1.getX() - x0 + 5, e1.getY() - y0 + 5);
 }