HighLevelStates now support adding/removing listeners
[Mograsim.git] / plugins / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / ModelManualSwitch.java
index 7a99504..d4e0437 100644 (file)
@@ -1,5 +1,9 @@
 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;
@@ -19,6 +23,7 @@ import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
 import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapter;
 import net.mograsim.logic.model.serializing.IdentifyParams;
 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
+import net.mograsim.logic.model.snippets.HighLevelStateHandler;
 import net.mograsim.preferences.Preferences;
 
 public class ModelManualSwitch extends ModelComponent
@@ -34,6 +39,8 @@ public class ModelManualSwitch extends ModelComponent
        private final LogicObserver logicObs;
        private CoreManualSwitch manualSwitch;
 
+       private final List<Consumer<Object>> hlsListeners;
+
        public ModelManualSwitch(LogicModelModifiable model, int logicWidth)
        {
                this(model, logicWidth, null);
@@ -43,11 +50,88 @@ 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<Object> 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<Object> 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();
        }
 
@@ -58,7 +142,7 @@ 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());
                Font oldFont = gc.getFont();
                Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
                gc.setFont(labelFont);
@@ -76,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++)
                        {
@@ -108,35 +192,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 +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();
@@ -176,6 +231,11 @@ public class ModelManualSwitch extends ModelComponent
                return logicWidth;
        }
 
+       private BitVector getOutValues()
+       {
+               return manualSwitch.getValues();
+       }
+
        static
        {
                LogicCoreAdapter.addComponentAdapter(new ManualSwitchAdapter());