public static Action setAttribute(AttributeSet attrs, Attribute<?> attr, Object value) {
		Object oldValue = attrs.getValue(attr);
		if (!oldValue.equals(value)) {
			return new SetAction(attrs, attr, value);
		} else {
			return null;
		}
	}
Example #2
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);
   }
 }
Example #3
0
 @Override
 public boolean contains(Location loc, AttributeSet attrs) {
   if (super.contains(loc, attrs)) {
     Direction facing = attrs.getValue(StdAttr.FACING);
     Location center = Location.create(0, 0).translate(facing, -20);
     return center.manhattanDistanceTo(loc) < 24;
   } else {
     return false;
   }
 }
Example #4
0
 public EndData toEnd(Location loc, AttributeSet attrs) {
   Location pt = loc.translate(dx, dy);
   if (widthFixed != null) {
     return new EndData(pt, widthFixed, type, exclude);
   } else {
     Object val = attrs.getValue(widthAttr);
     if (!(val instanceof BitWidth)) {
       throw new IllegalArgumentException("Width attribute not set");
     }
     return new EndData(pt, (BitWidth) val, type, exclude);
   }
 }
Example #5
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);
   }
 }
Example #6
0
  @Override
  public void propagate(InstanceState state) {
    GateAttributes attrs = (GateAttributes) state.getAttributeSet();
    int inputCount = attrs.inputs;
    int negated = attrs.negated;
    AttributeSet opts = state.getProject().getOptions().getAttributeSet();
    boolean errorIfUndefined =
        opts.getValue(Options.ATTR_GATE_UNDEFINED).equals(Options.GATE_UNDEFINED_ERROR);

    Value[] inputs = new Value[inputCount];
    int numInputs = 0;
    boolean error = false;
    for (int i = 1; i <= inputCount; i++) {
      if (state.isPortConnected(i)) {
        int negatedBit = (negated >> (i - 1)) & 1;
        if (negatedBit == 1) {
          inputs[numInputs] = state.getPort(i).not();
        } else {
          inputs[numInputs] = state.getPort(i);
        }
        numInputs++;
      } else {
        if (errorIfUndefined) {
          error = true;
        }
      }
    }
    Value out = null;
    if (numInputs == 0 || error) {
      out = Value.createError(attrs.width);
    } else {
      out = computeOutput(inputs, numInputs, state);
      out = pullOutput(out, attrs.out);
    }
    state.setPort(0, out, GateAttributes.DELAY);
  }
Example #7
0
 @Override
 public Bounds getOffsetBounds(AttributeSet attrs) {
   return Bounds.create(0, -8, 14, 16)
       .rotate(Direction.EAST, attrs.getValue(StdAttr.FACING), 0, 0);
 }
Example #8
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);
 }
Example #9
0
  @Override
  public void mousePressed(Canvas canvas, Graphics g, MouseEvent e) {
    Project proj = canvas.getProject();
    Circuit circ = canvas.getCircuit();

    if (!proj.getLogisimFile().contains(circ)) {
      if (caret != null) caret.cancelEditing();
      canvas.setErrorMessage(Strings.getter("cannotModifyError"));
      return;
    }

    // Maybe user is clicking within the current caret.
    if (caret != null) {
      if (caret.getBounds(g).contains(e.getX(), e.getY())) { // Yes
        caret.mousePressed(e);
        proj.repaintCanvas();
        return;
      } else { // No. End the current caret.
        caret.stopEditing();
      }
    }
    // caret will be null at this point

    // Otherwise search for a new caret.
    int x = e.getX();
    int y = e.getY();
    Location loc = Location.create(x, y);
    ComponentUserEvent event = new ComponentUserEvent(canvas, x, y);

    // First search in selection.
    for (Component comp : proj.getSelection().getComponentsContaining(loc, g)) {
      TextEditable editable = (TextEditable) comp.getFeature(TextEditable.class);
      if (editable != null) {
        caret = editable.getTextCaret(event);
        if (caret != null) {
          proj.getFrame().viewComponentAttributes(circ, comp);
          caretComponent = comp;
          caretCreatingText = false;
          break;
        }
      }
    }

    // Then search in circuit
    if (caret == null) {
      for (Component comp : circ.getAllContaining(loc, g)) {
        TextEditable editable = (TextEditable) comp.getFeature(TextEditable.class);
        if (editable != null) {
          caret = editable.getTextCaret(event);
          if (caret != null) {
            proj.getFrame().viewComponentAttributes(circ, comp);
            caretComponent = comp;
            caretCreatingText = false;
            break;
          }
        }
      }
    }

    // if nothing found, create a new label
    if (caret == null) {
      if (loc.getX() < 0 || loc.getY() < 0) return;
      AttributeSet copy = (AttributeSet) attrs.clone();
      caretComponent = Text.FACTORY.createComponent(loc, copy);
      caretCreatingText = true;
      TextEditable editable = (TextEditable) caretComponent.getFeature(TextEditable.class);
      if (editable != null) {
        caret = editable.getTextCaret(event);
        proj.getFrame().viewComponentAttributes(circ, caretComponent);
      }
    }

    if (caret != null) {
      caretCanvas = canvas;
      caretCircuit = canvas.getCircuit();
      caret.addCaretListener(listener);
      caretCircuit.addCircuitListener(listener);
    }
    proj.repaintCanvas();
  }
Example #10
0
 private static Value getPullValue(AttributeSet attrs) {
   AttributeOption opt = attrs.getValue(ATTR_PULL_TYPE);
   return (Value) opt.getValue();
 }
 public <E> E getAttributeValue(Attribute<E> attr) {
   InstanceComponent c = comp;
   AttributeSet as = c == null ? attrs : c.getAttributeSet();
   return as.getValue(attr);
 }
Example #12
0
 private String getType(AttributeSet attrs) {
   AttributeOption topt = attrs.getValue(ATTR_TYPE);
   return (String) topt.getValue();
 }
		@Override
		public void undo(Project proj) {
			attrs.setValue(attr, oldval);
		}
		@Override
		public void doIt(Project proj) {
			oldval = attrs.getValue(attr);
			attrs.setValue(attr, newval);
		}