89366013892ec806c46f9293b2deac7292e55e72
[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                 this(model, id, label, isInverted, logicWidth, name, true);
43         }
44
45         protected SimpleRectangularModelGate(LogicModelModifiable model, String id, String label, boolean isInverted, int logicWidth,
46                         String name, boolean callInit)
47         {
48                 super(model, name, false);
49                 this.id = id;
50                 this.label = label;
51                 this.logicWidth = logicWidth;
52                 this.isInverted = isInverted;
53                 this.rectWidth = width - (isInverted ? invertedCircleDiam : 0);
54                 this.outputPin = new MovablePin(model, this, "Y", logicWidth, PinUsage.OUTPUT, width, 0);
55                 addPin(outputPin);
56                 this.inputPins = new ArrayList<>();
57                 setInputCount(1);
58
59                 if (callInit)
60                         init();
61         }
62
63         protected void setInputCount(int inputCount)
64         {
65                 int oldInputCount = inputPins.size();
66                 setSize(width, inputCount * pinDistance);
67                 if (oldInputCount > inputCount)
68                         while (inputPins.size() > inputCount)
69                                 removePin(inputPins.remove(inputCount).name);
70                 else if (oldInputCount < inputCount)
71                         for (int i = oldInputCount; i < inputCount; i++)
72                         {
73                                 // TODO what for more than 24 input pins?
74                                 Pin pin = new Pin(model, this, String.valueOf((char) ('A' + i)), logicWidth, PinUsage.INPUT, 0,
75                                                 pinDistance / 2 + i * pinDistance);
76                                 inputPins.add(pin);
77                                 addPin(pin);
78                         }
79                 outputPin.setRelPos(width, inputCount * pinDistance / 2);
80         }
81
82         @Override
83         public void render(GeneralGC gc, Rectangle visibleRegion)
84         {
85                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
86                 if (foreground != null)
87                         gc.setForeground(foreground);
88                 double height = (getPins().size() - 1) * pinDistance;
89                 gc.drawRectangle(getPosX(), getPosY(), rectWidth, height);
90                 if (isInverted)
91                         gc.drawOval(getPosX() + rectWidth, getPosY() + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam);
92                 Font oldFont = gc.getFont();
93                 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
94                 gc.setFont(labelFont);
95                 Point textExtent = gc.textExtent(label);
96                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
97                 if (textColor != null)
98                         gc.setForeground(textColor);
99                 gc.drawText(label, getPosX() + (rectWidth - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
100                 gc.setFont(oldFont);
101         }
102
103         // serializing
104
105         @Override
106         public String getIDForSerializing(IdentifyParams idParams)
107         {
108                 return id;
109         }
110
111         /**
112          * {@link SimpleRectangularModelGate}s implementation returns a {@link JsonPrimitive} of type int containing the {@link #logicWidth} of
113          * this component.
114          * 
115          * @see ModelComponent#getParamsForSerializing()
116          */
117         @Override
118         public Integer getParamsForSerializing(IdentifyParams idParams)
119         {
120                 return logicWidth;
121         }
122 }