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=ef3c817ebfcb52921c0aa19dc46b69c7ef4c5307;hb=648fc6e69e09fe4467cb6bac47934be1a7dcf0d6;hp=7a995041aa919061acd38ca003ad12955a3bff36;hpb=7d05144c25daa53e60fc9ed9fd503546a86567f8;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 7a995041..ef3c817e 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,32 +1,41 @@ package net.mograsim.logic.model.model.components.atomic; +import static net.mograsim.logic.model.preferences.RenderPreferences.FOREGROUND_COLOR; +import static net.mograsim.logic.model.preferences.RenderPreferences.TEXT_COLOR; + +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; import net.mograsim.logic.core.types.Bit; import net.mograsim.logic.core.types.BitVector; -import net.mograsim.logic.core.types.BitVectorFormatter; +import net.mograsim.logic.model.BitVectorFormatter; import net.mograsim.logic.model.model.LogicModelModifiable; import net.mograsim.logic.model.model.components.ModelComponent; import net.mograsim.logic.model.model.wires.Pin; import net.mograsim.logic.model.model.wires.PinUsage; import net.mograsim.logic.model.modeladapter.LogicCoreAdapter; import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapter; +import net.mograsim.logic.model.preferences.RenderPreferences; import net.mograsim.logic.model.serializing.IdentifyParams; import net.mograsim.logic.model.serializing.IndirectModelComponentCreator; -import net.mograsim.preferences.Preferences; +import net.mograsim.logic.model.snippets.HighLevelStateHandler; +import net.mograsim.logic.model.util.TextRenderingHelper; 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; @@ -34,6 +43,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); @@ -43,30 +54,106 @@ 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 get(String stateID) + { + switch (stateID) + { + case "out": + if (manualSwitch != null) + return getOutValues(); + return null; + default: + throw new IllegalArgumentException("No high level state with ID " + stateID); + } + } + + @Override + public void set(String stateID, Object newState) + { + switch (stateID) + { + case "out": + if (manualSwitch != null) + manualSwitch.setState((BitVector) newState); + break; + default: + throw new IllegalArgumentException("No high level state with ID " + stateID); + } + } + + @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) + { + return null; + } + + @Override + public Object getParamsForSerializing(IdentifyParams idParams) + { + return null; + } + }); + init(); } @Override - public void render(GeneralGC gc, Rectangle visibleRegion) + public void render(GeneralGC gc, RenderPreferences renderPrefs, Rectangle visibleRegion) { - Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground"); + Color foreground = renderPrefs.getColor(FOREGROUND_COLOR); 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"); + Color textColor = renderPrefs.getColor(TEXT_COLOR); 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) @@ -76,7 +163,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++) { @@ -108,35 +195,6 @@ public class ModelManualSwitch extends ModelComponent return manualSwitch != null; } - @Override - public Object getHighLevelState(String stateID) - { - switch (stateID) - { - case "out": - if (manualSwitch != null) - return manualSwitch.getValues(); - return null; - default: - return super.getHighLevelState(stateID); - } - } - - @Override - public void setHighLevelState(String stateID, Object newState) - { - switch (stateID) - { - case "out": - if (manualSwitch != null) - manualSwitch.setState((BitVector) newState); - break; - default: - super.setHighLevelState(stateID, newState); - break; - } - } - @Override public boolean clicked(double x, double y) { @@ -145,7 +203,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(); @@ -176,6 +234,11 @@ public class ModelManualSwitch extends ModelComponent return logicWidth; } + private BitVector getOutValues() + { + return manualSwitch.getValues(); + } + static { LogicCoreAdapter.addComponentAdapter(new ManualSwitchAdapter());