Wires connected to a component now get deleted with the component
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / SimpleRectangularModelGate.java
1 package net.mograsim.logic.model.model.components.atomic;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.eclipse.swt.graphics.Color;
7
8 import com.google.gson.JsonPrimitive;
9
10 import net.haspamelodica.swt.helper.gcs.GeneralGC;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
13 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
14 import net.mograsim.logic.model.model.LogicModelModifiable;
15 import net.mograsim.logic.model.model.components.ModelComponent;
16 import net.mograsim.logic.model.model.wires.MovablePin;
17 import net.mograsim.logic.model.model.wires.Pin;
18 import net.mograsim.logic.model.model.wires.PinUsage;
19 import net.mograsim.logic.model.serializing.IdentifyParams;
20 import net.mograsim.preferences.Preferences;
21
22 public class SimpleRectangularModelGate extends ModelComponent
23 {
24         private static final double width = 20;
25         private static final double pinDistance = 10;
26         private static final double fontHeight = 5;
27         private static final double invertedCircleDiam = 3.5;
28
29         private final String id;
30
31         private final String label;
32         private final boolean isInverted;
33         protected final int logicWidth;
34         private final double rectWidth;
35
36         private MovablePin outputPin;
37         private final List<Pin> inputPins;
38
39         protected SimpleRectangularModelGate(LogicModelModifiable model, String id, String label, boolean isInverted, int logicWidth,
40                         String name)
41         {
42                 super(model, name);
43                 this.id = id;
44                 this.label = label;
45                 this.logicWidth = logicWidth;
46                 this.isInverted = isInverted;
47                 this.rectWidth = width - (isInverted ? invertedCircleDiam : 0);
48                 this.outputPin = new MovablePin(model, this, "Y", logicWidth, PinUsage.OUTPUT, width, 0);
49                 addPin(outputPin);
50                 this.inputPins = new ArrayList<>();
51                 setInputCount(1);
52         }
53
54         protected void setInputCount(int inputCount)
55         {
56                 int oldInputCount = inputPins.size();
57                 setSize(width, inputCount * pinDistance);
58                 if (oldInputCount > inputCount)
59                         while (inputPins.size() > inputCount)
60                                 removePin(inputPins.remove(inputCount).name);
61                 else if (oldInputCount < inputCount)
62                         for (int i = oldInputCount; i < inputCount; i++)
63                         {
64                                 // TODO what for more than 24 input pins?
65                                 Pin pin = new Pin(model, this, String.valueOf((char) ('A' + i)), logicWidth, PinUsage.INPUT, 0,
66                                                 pinDistance / 2 + i * pinDistance);
67                                 inputPins.add(pin);
68                                 addPin(pin);
69                         }
70                 outputPin.setRelPos(width, inputCount * pinDistance / 2);
71         }
72
73         @Override
74         public void render(GeneralGC gc, Rectangle visibleRegion)
75         {
76                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
77                 if (foreground != null)
78                         gc.setForeground(foreground);
79                 double height = (getPins().size() - 1) * pinDistance;
80                 gc.drawRectangle(getPosX(), getPosY(), rectWidth, height);
81                 if (isInverted)
82                         gc.drawOval(getPosX() + rectWidth, getPosY() + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam);
83                 Font oldFont = gc.getFont();
84                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
85                 gc.setFont(labelFont);
86                 Point textExtent = gc.textExtent(label);
87                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
88                 if (textColor != null)
89                         gc.setForeground(textColor);
90                 gc.drawText(label, getPosX() + (rectWidth - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
91                 gc.setFont(oldFont);
92         }
93
94         // serializing
95
96         @Override
97         public String getIDForSerializing(IdentifyParams idParams)
98         {
99                 return id;
100         }
101
102         /**
103          * {@link SimpleRectangularModelGate}s implementation returns a {@link JsonPrimitive} of type int containing the {@link #logicWidth} of
104          * this component.
105          * 
106          * @see ModelComponent#getParamsForSerializing()
107          */
108         @Override
109         public Integer getParamsForSerializing(IdentifyParams idParams)
110         {
111                 return logicWidth;
112         }
113 }