1 package net.mograsim.logic.model.model.components.atomic;
3 import org.eclipse.swt.graphics.Color;
5 import com.google.gson.Gson;
6 import com.google.gson.JsonElement;
7 import com.google.gson.JsonSyntaxException;
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
11 import net.mograsim.logic.model.model.ViewModelModifiable;
12 import net.mograsim.logic.model.model.components.GUIComponent;
13 import net.mograsim.logic.model.model.components.Orientation;
14 import net.mograsim.logic.model.model.components.OrientationCalculator;
15 import net.mograsim.logic.model.model.wires.Pin;
16 import net.mograsim.logic.model.model.wires.PinUsage;
17 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
18 import net.mograsim.logic.model.modeladapter.componentadapters.TriStateBufferAdapter;
19 import net.mograsim.logic.model.serializing.IdentifierGetter;
20 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
21 import net.mograsim.preferences.Preferences;
23 public class GUITriStateBuffer extends GUIComponent
26 private static final double width = 20;
27 private static final double height = 20;
31 private double[] path;
33 private GUITriStateBufferParams params;
34 private OrientationCalculator oc;
36 public GUITriStateBuffer(ViewModelModifiable model, GUITriStateBufferParams params)
38 this(model, params, null);
41 public GUITriStateBuffer(ViewModelModifiable model, GUITriStateBufferParams params, String name)
46 oc = new OrientationCalculator(params.orientation, width, height);
48 double wHalf = width / 2;
49 double hHalf = height / 2;
50 double hQuar = height / 4;
52 this.input = new Pin(this, "IN", params.logicWidth, PinUsage.INPUT, oc.newX(0, hHalf), oc.newY(0, hHalf));
53 this.output = new Pin(this, "OUT", params.logicWidth, PinUsage.OUTPUT, oc.newX(width, hHalf), oc.newY(width, hHalf));
54 this.enable = new Pin(this, "EN", 1, PinUsage.INPUT, oc.newX(wHalf, hQuar), oc.newY(wHalf, hQuar));
55 this.path = new double[] { oc.newX(0, 0), oc.newY(0, 0), oc.newX(width, hHalf), oc.newY(width, hHalf), oc.newX(0, height),
58 setSize(oc.width(), oc.height());
65 public void render(GeneralGC gc, Rectangle visibleRegion)
67 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
68 if (foreground != null)
69 gc.setForeground(foreground);
72 gc.drawPolygon(new double[] { x + path[0], y + path[1], x + path[2], y + path[3], x + path[4], y + path[5] });
76 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
78 return new Gson().toJsonTree(params);
83 ViewLogicModelAdapter.addComponentAdapter(new TriStateBufferAdapter());
84 IndirectGUIComponentCreator.setComponentSupplier(GUITriStateBuffer.class.getName(), (m, p, n) ->
86 GUITriStateBufferParams params = new Gson().fromJson(p, GUITriStateBufferParams.class);
88 throw new JsonSyntaxException("Invalid!!!");
89 return new GUITriStateBuffer(m, params, n);
93 public static class GUITriStateBufferParams
96 Orientation orientation;
98 public GUITriStateBufferParams(int logicWidth, Orientation orientation)
100 this.logicWidth = logicWidth;
101 this.orientation = orientation;