0ca71c7790082af27d7f2c68298ea7ccddbf37c7
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / GUISplitter.java
1 package net.mograsim.logic.model.model.components.atomic;
2
3 import org.eclipse.swt.SWT;
4
5 import com.google.gson.JsonElement;
6 import com.google.gson.JsonPrimitive;
7
8 import net.haspamelodica.swt.helper.gcs.GeneralGC;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
10 import net.mograsim.logic.core.types.BitVectorFormatter;
11 import net.mograsim.logic.core.wires.Wire.ReadEnd;
12 import net.mograsim.logic.model.model.ViewModelModifiable;
13 import net.mograsim.logic.model.model.components.GUIComponent;
14 import net.mograsim.logic.model.model.wires.Pin;
15 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
16 import net.mograsim.logic.model.modeladapter.componentadapters.SplitterAdapter;
17 import net.mograsim.logic.model.serializing.IdentifierGetter;
18 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
19 import net.mograsim.preferences.ColorDefinition;
20 import net.mograsim.preferences.ColorManager;
21 import net.mograsim.preferences.Preferences;
22
23 public class GUISplitter extends GUIComponent
24 {
25         private static final double width = 10;
26         private static final double heightPerPin = 10;
27
28         public final int logicWidth;
29         private final Pin inputPin;
30
31         private ReadEnd inputEnd;
32         private final ReadEnd[] outputEnds;
33
34         public GUISplitter(ViewModelModifiable model, int logicWidth)
35         {
36                 this(model, logicWidth, null);
37         }
38
39         public GUISplitter(ViewModelModifiable model, int logicWidth, String name)
40         {
41                 super(model, name);
42                 this.logicWidth = logicWidth;
43                 setSize(width, logicWidth * heightPerPin);
44                 addPin(this.inputPin = new Pin(this, "I", logicWidth, 0, (logicWidth - 1) * heightPerPin / 2));
45                 double outputHeight = 0;
46                 for (int i = 0; i < logicWidth; i++, outputHeight += 10)
47                         addPin(new Pin(this, "O" + i, 1, width, outputHeight));
48                 outputEnds = new ReadEnd[logicWidth];
49         }
50
51         @Override
52         public void render(GeneralGC gc, Rectangle visibleRegion)
53         {
54                 double posX = getPosX();
55                 double posY = getPosY();
56
57                 ColorDefinition c = BitVectorFormatter.formatAsColor(inputEnd);
58                 if (c != null)
59                         gc.setForeground(ColorManager.current().toColor(c));
60                 double inLineY = posY + (logicWidth - 1) * heightPerPin / 2;
61                 gc.drawLine(posX, inLineY, posX + width / 2, inLineY);
62                 double outputHeight = posY;
63                 for (int i = 0; i < logicWidth; i++, outputHeight += 10)
64                 {
65                         c = BitVectorFormatter.formatAsColor(outputEnds[i]);
66                         if (c != null)
67                                 gc.setForeground(ColorManager.current().toColor(c));
68                         gc.drawLine(posX + width / 2, outputHeight, posX + width, outputHeight);
69                 }
70                 gc.setForeground(Preferences.current().getColor("net.mograsim.logic.model.color.foreground"));
71                 int oldLineCap = gc.getLineCap();
72                 int lineJoin = gc.getLineJoin();
73                 // TODO find better "replacement" for JOIN_BEVEL
74                 gc.setLineCap(lineJoin == SWT.JOIN_MITER ? SWT.CAP_SQUARE : lineJoin == SWT.JOIN_ROUND ? SWT.CAP_ROUND : SWT.CAP_SQUARE);
75                 gc.drawLine(posX + width / 2, posY, posX + width / 2, posY + heightPerPin * (logicWidth - 1));
76                 gc.setLineCap(oldLineCap);
77         }
78
79         @Override
80         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
81         {
82                 return new JsonPrimitive(logicWidth);
83         }
84
85         public void setLogicModelBinding(ReadEnd inputEnd, ReadEnd[] outputEnds)
86         {
87                 this.inputEnd = inputEnd;
88                 System.arraycopy(outputEnds, 0, this.outputEnds, 0, logicWidth);
89         }
90
91         public Pin getInputPin()
92         {
93                 return inputPin;
94         }
95
96         static
97         {
98                 ViewLogicModelAdapter.addComponentAdapter(new SplitterAdapter());
99                 IndirectGUIComponentCreator.setComponentSupplier(GUISplitter.class.getCanonicalName(),
100                                 (m, p, n) -> new GUISplitter(m, p.getAsInt(), n));
101         }
102 }