1 package net.mograsim.logic.model.model.components.atomic;
4 import java.util.concurrent.atomic.AtomicReference;
6 import net.haspamelodica.swt.helper.gcs.GeneralGC;
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
8 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
9 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
10 import net.mograsim.logic.model.model.LogicModelModifiable;
11 import net.mograsim.logic.model.model.components.ModelComponent;
12 import net.mograsim.logic.model.model.wires.Pin;
13 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
14 import net.mograsim.logic.model.modeladapter.componentadapters.SimpleRectangularHardcodedModelComponentAdapter;
15 import net.mograsim.logic.model.serializing.IdentifyParams;
16 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
17 import net.mograsim.logic.model.snippets.outlinerenderers.DefaultOutlineRenderer;
18 import net.mograsim.logic.model.snippets.symbolrenderers.CenteredTextSymbolRenderer;
19 import net.mograsim.logic.model.snippets.symbolrenderers.CenteredTextSymbolRenderer.CenteredTextParams;
20 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer;
21 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams;
22 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
24 public abstract class SimpleRectangularHardcodedModelComponent extends ModelComponent
26 private static final double centerTextHeight = 5;
27 private static final double pinNamesHeight = 3.5;
28 private static final double pinNamesMargin = .5;
30 private final String id;
32 private final DefaultOutlineRenderer outlineRenderer;
33 private final CenteredTextSymbolRenderer centerTextRenderer;
34 private final PinNamesSymbolRenderer pinNamesRenderer;
36 private AtomicReference<Object> state;
37 private Runnable recalculate;
39 // creation and destruction
41 public SimpleRectangularHardcodedModelComponent(LogicModelModifiable model, String id, String name, String centerText)
43 this(model, id, name, centerText, true);
46 public SimpleRectangularHardcodedModelComponent(LogicModelModifiable model, String id, String name, String centerText, boolean callInit)
48 super(model, name, false);
50 this.outlineRenderer = new DefaultOutlineRenderer(this);
51 CenteredTextParams centeredTextParams = new CenteredTextParams();
52 centeredTextParams.text = centerText;
53 centeredTextParams.fontHeight = centerTextHeight;
54 this.centerTextRenderer = new CenteredTextSymbolRenderer(this, centeredTextParams);
55 PinNamesParams pinNamesParams = new PinNamesParams();
56 pinNamesParams.pinLabelHeight = pinNamesHeight;
57 pinNamesParams.pinLabelMargin = pinNamesMargin;
58 this.pinNamesRenderer = new PinNamesSymbolRenderer(this, pinNamesParams);
59 addPinRemovedListener(this::pinRemoved);
60 setHighLevelStateHandler(new HighLevelStateHandler()
63 public String getIDForSerializing(IdentifyParams idParams)
65 return null;// we don't need to serialize this; it's implicit since we are a SimpleRectangularHardcodedModelComponent
69 public Object getParamsForSerializing(IdentifyParams idParams)
75 public Object getHighLevelState(String stateID)
77 return SimpleRectangularHardcodedModelComponent.this.getHighLevelState(state.get(), stateID);
81 public void setHighLevelState(String stateID, Object newState)
83 state.updateAndGet(s -> SimpleRectangularHardcodedModelComponent.this.setHighLevelState(s, stateID, newState));
94 protected void addPin(Pin pin, Position namePosition)
96 super.addPin(pin); // do this first to catch errors
97 pinNamesRenderer.setPinPosition(pin, namePosition);
100 private void pinRemoved(Pin pin)
102 pinNamesRenderer.setPinPosition(pin, null);
107 @SuppressWarnings({ "static-method", "unused" }) // this method is intended to be overridden
108 protected Object getHighLevelState(Object state, String stateID)
110 throw new IllegalArgumentException("No high level state with ID " + stateID);
113 @SuppressWarnings({ "static-method", "unused" }) // this method is intended to be overridden
114 protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
116 throw new IllegalArgumentException("No high level state with ID " + stateID);
121 public abstract Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds);
123 // core model binding
125 public void setCoreModelBindingAndResetState(AtomicReference<Object> state, Runnable recalculate)
128 this.recalculate = recalculate;
131 // "graphical" operations
134 public void render(GeneralGC gc, Rectangle visibleRegion)
136 outlineRenderer.render(gc, visibleRegion);
137 centerTextRenderer.render(gc, visibleRegion);
138 pinNamesRenderer.render(gc, visibleRegion);
144 public String getIDForSerializing(IdentifyParams idParams)
149 // operations no longer supported
152 protected void addPin(Pin pin)
154 throw new UnsupportedOperationException("Can't add pins without setting usage, call addPin(Pin [, Position]) instead");
159 LogicCoreAdapter.addComponentAdapter(new SimpleRectangularHardcodedModelComponentAdapter());