1 package net.mograsim.logic.model.model.components.atomic;
3 import static net.mograsim.logic.model.preferences.RenderPreferences.FOREGROUND_COLOR;
5 import org.eclipse.swt.graphics.Color;
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.LogicModelModifiable;
12 import net.mograsim.logic.model.model.components.ModelComponent;
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.LogicCoreAdapter;
18 import net.mograsim.logic.model.modeladapter.componentadapters.TriStateBufferAdapter;
19 import net.mograsim.logic.model.preferences.RenderPreferences;
20 import net.mograsim.logic.model.serializing.IdentifyParams;
21 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
22 import net.mograsim.logic.model.util.JsonHandler;
24 public class ModelTriStateBuffer extends ModelComponent
27 private static final double width = 20;
28 private static final double height = 20;
32 private double[] path;
34 private ModelTriStateBufferParams params;
35 private OrientationCalculator oc;
37 public ModelTriStateBuffer(LogicModelModifiable model, ModelTriStateBufferParams params)
39 this(model, params, null);
42 public ModelTriStateBuffer(LogicModelModifiable model, ModelTriStateBufferParams params, String name)
44 super(model, name, false);
47 oc = new OrientationCalculator(params.orientation, width, height);
49 double wHalf = width / 2;
50 double hHalf = height / 2;
51 double hQuar = height / 4;
53 this.input = new Pin(model, this, "IN", params.logicWidth, PinUsage.INPUT, oc.newX(0, hHalf), oc.newY(0, hHalf));
54 this.output = new Pin(model, this, "OUT", params.logicWidth, PinUsage.OUTPUT, oc.newX(width, hHalf), oc.newY(width, hHalf));
55 this.enable = new Pin(model, this, "EN", 1, PinUsage.INPUT, oc.newX(wHalf, hQuar), oc.newY(wHalf, hQuar));
56 this.path = new double[] { oc.newX(0, 0), oc.newY(0, 0), oc.newX(width, hHalf), oc.newY(width, hHalf), oc.newX(0, height),
59 setSize(oc.width(), oc.height());
67 public final Pin getInputPin()
72 public final Pin getOutputPin()
77 public final Pin getEnablePin()
83 public void render(GeneralGC gc, RenderPreferences renderPrefs, Rectangle visibleRegion)
85 Color foreground = renderPrefs.getColor(FOREGROUND_COLOR);
86 if (foreground != null)
87 gc.setForeground(foreground);
90 gc.drawPolygon(new double[] { x + path[0], y + path[1], x + path[2], y + path[3], x + path[4], y + path[5] });
94 public String getIDForSerializing(IdentifyParams idParams)
96 return "TriStateBuffer";
100 public ModelTriStateBufferParams getParamsForSerializing(IdentifyParams idParams)
107 LogicCoreAdapter.addComponentAdapter(new TriStateBufferAdapter());
108 IndirectModelComponentCreator.setComponentSupplier(ModelTriStateBuffer.class.getName(), (m, p, n) ->
110 ModelTriStateBufferParams params = JsonHandler.fromJsonTree(p, ModelTriStateBufferParams.class);
112 throw new JsonSyntaxException("Invalid!!!");
113 return new ModelTriStateBuffer(m, params, n);
117 public static class ModelTriStateBufferParams
120 Orientation orientation;
122 public ModelTriStateBufferParams(int logicWidth, Orientation orientation)
124 this.logicWidth = logicWidth;
125 this.orientation = orientation;