X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fmodel%2Fcomponents%2Fatomic%2FModelManualSwitch.java;h=789798254491d489a9cbed7ce3010fcf6e5253e0;hb=13e03ecec5e28dab69820e5ec4db5c1a2c5a3c73;hp=cf9735aebe524e131deab3519b43b4a8d08e6815;hpb=0c97f70d199e1ffcd2bc13817f7bb12867ba033e;p=Mograsim.git diff --git a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelManualSwitch.java b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelManualSwitch.java index cf9735ae..78979825 100644 --- a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelManualSwitch.java +++ b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelManualSwitch.java @@ -1,10 +1,13 @@ package net.mograsim.logic.model.model.components.atomic; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + import org.eclipse.swt.graphics.Color; import net.haspamelodica.swt.helper.gcs.GeneralGC; import net.haspamelodica.swt.helper.swtobjectwrappers.Font; -import net.haspamelodica.swt.helper.swtobjectwrappers.Point; import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle; import net.mograsim.logic.core.LogicObserver; import net.mograsim.logic.core.components.CoreManualSwitch; @@ -20,14 +23,16 @@ import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapt import net.mograsim.logic.model.serializing.IdentifyParams; import net.mograsim.logic.model.serializing.IndirectModelComponentCreator; import net.mograsim.logic.model.snippets.HighLevelStateHandler; +import net.mograsim.logic.model.util.TextRenderingHelper; import net.mograsim.preferences.Preferences; public class ModelManualSwitch extends ModelComponent { private static final double width = 20; - private static final double height = 15; + private static final double height = 10; private static final double fontHeight = 5; private static final double heightMiniButtons = 4; // 0 is disabled + private static final double textMargin = 0.5; public final int logicWidth; private final Pin outputPin; @@ -35,6 +40,8 @@ public class ModelManualSwitch extends ModelComponent private final LogicObserver logicObs; private CoreManualSwitch manualSwitch; + private final List> hlsListeners; + public ModelManualSwitch(LogicModelModifiable model, int logicWidth) { this(model, logicWidth, null); @@ -44,21 +51,29 @@ public class ModelManualSwitch extends ModelComponent { super(model, name, false); this.logicWidth = logicWidth; - logicObs = (i) -> model.requestRedraw(); setSize(width, height); addPin(this.outputPin = new Pin(model, this, "", logicWidth, PinUsage.OUTPUT, width, height / 2)); + hlsListeners = new ArrayList<>(); + + logicObs = i -> + { + model.requestRedraw(); + BitVector v = getOutValues(); + hlsListeners.forEach(l -> l.accept(v)); + }; + setHighLevelStateHandler(new HighLevelStateHandler() { @Override - public Object getHighLevelState(String stateID) + public Object get(String stateID) { switch (stateID) { case "out": if (manualSwitch != null) - return manualSwitch.getValues(); + return getOutValues(); return null; default: throw new IllegalArgumentException("No high level state with ID " + stateID); @@ -66,7 +81,7 @@ public class ModelManualSwitch extends ModelComponent } @Override - public void setHighLevelState(String stateID, Object newState) + public void set(String stateID, Object newState) { switch (stateID) { @@ -79,6 +94,32 @@ public class ModelManualSwitch extends ModelComponent } } + @Override + public void addListener(String stateID, Consumer stateChanged) + { + switch (stateID) + { + case "out": + hlsListeners.add(stateChanged); + break; + default: + throw new IllegalArgumentException("No high level state with ID " + stateID); + } + } + + @Override + public void removeListener(String stateID, java.util.function.Consumer stateChanged) + { + switch (stateID) + { + case "out": + hlsListeners.remove(stateChanged); + break; + default: + throw new IllegalArgumentException("No high level state with ID " + stateID); + } + } + @Override public String getIDForSerializing(IdentifyParams idParams) { @@ -102,15 +143,14 @@ public class ModelManualSwitch extends ModelComponent if (foreground != null) gc.setForeground(foreground); gc.drawRectangle(getBounds()); - String label = BitVectorFormatter.formatAsString(manualSwitch == null ? null : manualSwitch.getValues()); + String label = BitVectorFormatter.formatAsString(manualSwitch == null ? null : getOutValues(), false); Font oldFont = gc.getFont(); Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle()); gc.setFont(labelFont); - Point textExtent = gc.textExtent(label); Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text"); if (textColor != null) gc.setForeground(textColor); - gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true); + TextRenderingHelper.drawTextFitting(gc, label, getBounds(), textMargin, true); gc.setFont(oldFont); if (manualSwitch != null && logicWidth > 1 && heightMiniButtons > 0 && visibleRegion.y < getPosY() + heightMiniButtons) @@ -120,7 +160,7 @@ public class ModelManualSwitch extends ModelComponent gc.drawLine(x, y + heightMiniButtons, x + width, y + heightMiniButtons); Color c = gc.getBackground(); gc.setBackground(gc.getForeground()); - BitVector bv = manualSwitch.getValues(); + BitVector bv = getOutValues(); double part = width / bv.length(); for (int i = 0; i < bv.length(); i++) { @@ -160,7 +200,7 @@ public class ModelManualSwitch extends ModelComponent if (heightMiniButtons > 0 && y - getPosY() < heightMiniButtons) { int part = (int) ((x - getPosX()) * logicWidth / width); - manualSwitch.setState(manualSwitch.getValues().withBitChanged(part, Bit::not)); + manualSwitch.setState(getOutValues().withBitChanged(part, Bit::not)); } else { manualSwitch.toggle(); @@ -191,6 +231,11 @@ public class ModelManualSwitch extends ModelComponent return logicWidth; } + private BitVector getOutValues() + { + return manualSwitch.getValues(); + } + static { LogicCoreAdapter.addComponentAdapter(new ManualSwitchAdapter());