X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fmodel%2Fcomponents%2Fatomic%2FSimpleRectangularModelGate.java;fp=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fmodel%2Fcomponents%2Fatomic%2FSimpleRectangularModelGate.java;h=890c482f9bbe0aa52219bfb2523b9179b44416df;hb=93b398d6271a538a2a4c9f4de07a3b4a8a2a7fd4;hp=0000000000000000000000000000000000000000;hpb=0a04a4ed66ecebd4254541c4977599f6052c115a;p=Mograsim.git diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/SimpleRectangularModelGate.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/SimpleRectangularModelGate.java new file mode 100644 index 00000000..890c482f --- /dev/null +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/SimpleRectangularModelGate.java @@ -0,0 +1,111 @@ +package net.mograsim.logic.model.model.components.atomic; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.swt.graphics.Color; + +import com.google.gson.JsonPrimitive; + +import net.haspamelodica.swt.helper.gcs.GeneralGC; +import net.haspamelodica.swt.helper.swtobjectwrappers.Font; +import net.haspamelodica.swt.helper.swtobjectwrappers.Point; +import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle; +import net.mograsim.logic.model.model.ViewModelModifiable; +import net.mograsim.logic.model.model.components.ModelComponent; +import net.mograsim.logic.model.model.wires.MovablePin; +import net.mograsim.logic.model.model.wires.Pin; +import net.mograsim.logic.model.model.wires.PinUsage; +import net.mograsim.logic.model.serializing.IdentifyParams; +import net.mograsim.preferences.Preferences; + +public class SimpleRectangularModelGate extends ModelComponent +{ + private static final double width = 20; + private static final double pinDistance = 10; + private static final double fontHeight = 5; + private static final double invertedCircleDiam = 3.5; + + private final String id; + + private final String label; + private final boolean isInverted; + protected final int logicWidth; + private final double rectWidth; + + private MovablePin outputPin; + private final List inputPins; + + protected SimpleRectangularModelGate(ViewModelModifiable model, String id, String label, boolean isInverted, int logicWidth, String name) + { + super(model, name); + this.id = id; + this.label = label; + this.logicWidth = logicWidth; + this.isInverted = isInverted; + this.rectWidth = width - (isInverted ? invertedCircleDiam : 0); + this.outputPin = new MovablePin(this, "Y", logicWidth, PinUsage.OUTPUT, width, 0); + addPin(outputPin); + this.inputPins = new ArrayList<>(); + setInputCount(1); + } + + protected void setInputCount(int inputCount) + { + int oldInputCount = inputPins.size(); + setSize(width, inputCount * pinDistance); + if (oldInputCount > inputCount) + while (inputPins.size() > inputCount) + removePin(inputPins.remove(inputCount).name); + else if (oldInputCount < inputCount) + for (int i = oldInputCount; i < inputCount; i++) + { + // TODO what for more than 24 input pins? + Pin pin = new Pin(this, String.valueOf((char) ('A' + i)), logicWidth, PinUsage.INPUT, 0, pinDistance / 2 + i * pinDistance); + inputPins.add(pin); + addPin(pin); + } + outputPin.setRelPos(width, inputCount * pinDistance / 2); + } + + @Override + public void render(GeneralGC gc, Rectangle visibleRegion) + { + Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground"); + if (foreground != null) + gc.setForeground(foreground); + double height = (getPins().size() - 1) * pinDistance; + gc.drawRectangle(getPosX(), getPosY(), rectWidth, height); + if (isInverted) + gc.drawOval(getPosX() + rectWidth, getPosY() + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam); + Font oldFont = gc.getFont(); + Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle()); + gc.setFont(labelFont); + Point textExtent = gc.textExtent(label); + Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text"); + if (textColor != null) + gc.setForeground(textColor); + gc.drawText(label, getPosX() + (rectWidth - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true); + gc.setFont(oldFont); + } + + // serializing + + @Override + public String getIDForSerializing(IdentifyParams idParams) + { + return id; + } + + /** + * {@link SimpleRectangularModelGate}s implementation returns a {@link JsonPrimitive} of type int containing the {@link #logicWidth} of + * this component. + * + * @see ModelComponent#getParamsForSerializing() + */ + @Override + public Integer getParamsForSerializing(IdentifyParams idParams) + { + return logicWidth; + } +} \ No newline at end of file