6283c507770e79ddd7bc6f52c8abae9ffcc68701
[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
30         private ReadEnd inputEnd;
31         private final ReadEnd[] outputEnds;
32
33         public GUISplitter(ViewModelModifiable model, int logicWidth, String name)
34         {
35                 super(model, name);
36                 this.logicWidth = logicWidth;
37                 setSize(width, logicWidth * heightPerPin);
38                 addPin(new Pin(this, "I", logicWidth, 0, (logicWidth - 1) * heightPerPin / 2));
39                 double outputHeight = 0;
40                 for (int i = 0; i < logicWidth; i++, outputHeight += 10)
41                         addPin(new Pin(this, "O" + i, 1, width, outputHeight));
42                 outputEnds = new ReadEnd[logicWidth];
43         }
44
45         @Override
46         public void render(GeneralGC gc, Rectangle visibleRegion)
47         {
48                 double posX = getPosX();
49                 double posY = getPosY();
50
51                 ColorDefinition c = BitVectorFormatter.formatAsColor(inputEnd);
52                 if (c != null)
53                         gc.setForeground(ColorManager.current().toColor(c));
54                 double inLineY = posY + (logicWidth - 1) * heightPerPin / 2;
55                 gc.drawLine(posX, inLineY, posX + width / 2, inLineY);
56                 gc.setForeground(Preferences.current().getColor("net.mograsim.logic.model.color.foreground"));
57                 int oldLineCap = gc.getLineCap();
58                 int lineJoin = gc.getLineJoin();
59                 // TODO find better "replacement" for JOIN_BEVEL
60                 gc.setLineCap(lineJoin == SWT.JOIN_MITER ? SWT.CAP_SQUARE : lineJoin == SWT.JOIN_ROUND ? SWT.CAP_ROUND : SWT.CAP_SQUARE);
61                 gc.drawLine(posX + width / 2, posY, posX + width / 2, posY + heightPerPin * (logicWidth - 1));
62                 gc.setLineCap(oldLineCap);
63                 double outputHeight = posY;
64                 for (int i = 0; i < logicWidth; i++, outputHeight += 10)
65                 {
66                         c = BitVectorFormatter.formatAsColor(outputEnds[i]);
67                         if (c != null)
68                                 gc.setForeground(ColorManager.current().toColor(c));
69                         gc.drawLine(posX + width / 2, outputHeight, posX + width, outputHeight);
70                 }
71         }
72
73         @Override
74         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
75         {
76                 return new JsonPrimitive(logicWidth);
77         }
78
79         public void setLogicModelBinding(ReadEnd inputEnd, ReadEnd[] outputEnds)
80         {
81                 this.inputEnd = inputEnd;
82                 System.arraycopy(outputEnds, 0, this.outputEnds, 0, logicWidth);
83         }
84
85         static
86         {
87                 ViewLogicModelAdapter.addComponentAdapter(new SplitterAdapter());
88                 IndirectGUIComponentCreator.setComponentSupplier(GUISplitter.class.getCanonicalName(),
89                                 (m, p, n) -> new GUISplitter(m, p.getAsInt(), n));
90         }
91 }