Renamed logic.ui to logic.model
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / GUIManualSwitch.java
diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUIManualSwitch.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUIManualSwitch.java
new file mode 100644 (file)
index 0000000..2fd1749
--- /dev/null
@@ -0,0 +1,148 @@
+package net.mograsim.logic.model.model.components.atomic;
+
+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.LogicObservable;
+import net.mograsim.logic.core.LogicObserver;
+import net.mograsim.logic.core.components.ManualSwitch;
+import net.mograsim.logic.core.types.Bit;
+import net.mograsim.logic.core.types.BitVectorFormatter;
+import net.mograsim.logic.core.wires.Wire.ReadEnd;
+import net.mograsim.logic.model.model.ViewModelModifiable;
+import net.mograsim.logic.model.model.components.GUIComponent;
+import net.mograsim.logic.model.model.wires.Pin;
+import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
+import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapter;
+import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
+import net.mograsim.preferences.Preferences;
+
+public class GUIManualSwitch extends GUIComponent
+{
+       private static final double width = 20;
+       private static final double height = 15;
+       private static final double fontHeight = 5;
+
+       private final Pin outputPin;
+
+       private final LogicObserver logicObs;
+       private ManualSwitch logicSwitch;
+       private ReadEnd end;
+
+       public GUIManualSwitch(ViewModelModifiable model)
+       {
+               this(model, null);
+       }
+
+       public GUIManualSwitch(ViewModelModifiable model, String name)
+       {
+               super(model, name);
+               logicObs = (i) -> requestRedraw();
+
+               setSize(width, height);
+               addPin(this.outputPin = new Pin(this, "", 1, width, height / 2));
+       }
+
+       @Override
+       public void render(GeneralGC gc, Rectangle visibleRegion)
+       {
+               // TODO maybe draw switch state too?
+               Color foreground = Preferences.current().getColor("net.mograsim.logic.ui.color.foreground");
+               if (foreground != null)
+                       gc.setForeground(foreground);
+               gc.drawRectangle(getBounds());
+               String label = BitVectorFormatter.formatValueAsString(end);
+               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.ui.color.text");
+               if (textColor != null)
+                       gc.setForeground(textColor);
+               gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
+               gc.setFont(oldFont);
+       }
+
+       public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
+       {
+               deregisterLogicObs(this.end);
+               deregisterLogicObs(this.logicSwitch);
+               this.logicSwitch = logicSwitch;
+               this.end = end;
+               registerLogicObs(end);
+               registerLogicObs(logicSwitch);
+       }
+
+       public boolean hasLogicModelBinding()
+       {
+               return logicSwitch != null;
+       }
+
+       @Override
+       public void setHighLevelState(String stateID, Object newState)
+       {
+               switch (stateID)
+               {
+               case "out":
+                       if (logicSwitch != null)
+                               logicSwitch.setToValueOf((Bit) newState);
+                       break;
+               default:
+                       super.setHighLevelState(stateID, newState);
+                       break;
+               }
+       }
+
+       @Override
+       public Object getHighLevelState(String stateID)
+       {
+               switch (stateID)
+               {
+               case "out":
+                       if (logicSwitch != null)
+                               return logicSwitch.getValue();
+                       return null;
+               default:
+                       return super.getHighLevelState(stateID);
+               }
+       }
+
+       private void registerLogicObs(LogicObservable observable)
+       {
+               if (observable != null)
+                       observable.registerObserver(logicObs);
+       }
+
+       private void deregisterLogicObs(LogicObservable observable)
+       {
+               if (observable != null)
+                       observable.deregisterObserver(logicObs);
+       }
+
+       @Override
+       public boolean clicked(double x, double y)
+       {
+               if (logicSwitch != null)
+                       logicSwitch.toggle();
+               return true;
+       }
+
+       public ManualSwitch getManualSwitch()
+       {
+               return logicSwitch;
+       }
+
+       public Pin getOutputPin()
+       {
+               return outputPin;
+       }
+
+       static
+       {
+               ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
+               IndirectGUIComponentCreator.setComponentSupplier(GUIManualSwitch.class.getName(), (m, p, n) -> new GUIManualSwitch(m, n));
+       }
+}
\ No newline at end of file