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