1 package net.mograsim.logic.model.model.components.atomic;
3 import org.eclipse.swt.graphics.Color;
5 import com.google.gson.JsonSyntaxException;
7 import net.haspamelodica.swt.helper.gcs.GeneralGC;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
9 import net.mograsim.logic.model.model.LogicModelModifiable;
10 import net.mograsim.logic.model.model.components.ModelComponent;
11 import net.mograsim.logic.model.model.components.Orientation;
12 import net.mograsim.logic.model.model.components.OrientationCalculator;
13 import net.mograsim.logic.model.model.wires.Pin;
14 import net.mograsim.logic.model.model.wires.PinUsage;
15 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
16 import net.mograsim.logic.model.modeladapter.componentadapters.TriStateBufferAdapter;
17 import net.mograsim.logic.model.serializing.IdentifyParams;
18 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
19 import net.mograsim.logic.model.util.JsonHandler;
20 import net.mograsim.preferences.Preferences;
22 public class ModelTriStateBuffer extends ModelComponent
25 private static final double width = 20;
26 private static final double height = 20;
30 private double[] path;
32 private ModelTriStateBufferParams params;
33 private OrientationCalculator oc;
35 public ModelTriStateBuffer(LogicModelModifiable model, ModelTriStateBufferParams params)
37 this(model, params, null);
40 public ModelTriStateBuffer(LogicModelModifiable model, ModelTriStateBufferParams params, String name)
42 super(model, name, false);
45 oc = new OrientationCalculator(params.orientation, width, height);
47 double wHalf = width / 2;
48 double hHalf = height / 2;
49 double hQuar = height / 4;
51 this.input = new Pin(model, this, "IN", params.logicWidth, PinUsage.INPUT, oc.newX(0, hHalf), oc.newY(0, hHalf));
52 this.output = new Pin(model, this, "OUT", params.logicWidth, PinUsage.OUTPUT, oc.newX(width, hHalf), oc.newY(width, hHalf));
53 this.enable = new Pin(model, this, "EN", 1, PinUsage.INPUT, oc.newX(wHalf, hQuar), oc.newY(wHalf, hQuar));
54 this.path = new double[] { oc.newX(0, 0), oc.newY(0, 0), oc.newX(width, hHalf), oc.newY(width, hHalf), oc.newX(0, height),
57 setSize(oc.width(), oc.height());
65 public final Pin getInputPin()
70 public final Pin getOutputPin()
75 public final Pin getEnablePin()
81 public void render(GeneralGC gc, Rectangle visibleRegion)
83 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
84 if (foreground != null)
85 gc.setForeground(foreground);
88 gc.drawPolygon(new double[] { x + path[0], y + path[1], x + path[2], y + path[3], x + path[4], y + path[5] });
92 public String getIDForSerializing(IdentifyParams idParams)
94 return "TriStateBuffer";
98 public ModelTriStateBufferParams getParamsForSerializing(IdentifyParams idParams)
105 LogicCoreAdapter.addComponentAdapter(new TriStateBufferAdapter());
106 IndirectModelComponentCreator.setComponentSupplier(ModelTriStateBuffer.class.getName(), (m, p, n) ->
108 ModelTriStateBufferParams params = JsonHandler.fromJsonTree(p, ModelTriStateBufferParams.class);
110 throw new JsonSyntaxException("Invalid!!!");
111 return new ModelTriStateBuffer(m, params, n);
115 public static class ModelTriStateBufferParams
118 Orientation orientation;
120 public ModelTriStateBufferParams(int logicWidth, Orientation orientation)
122 this.logicWidth = logicWidth;
123 this.orientation = orientation;