1 package net.mograsim.logic.model.model.components.atomic;
3 import org.eclipse.swt.graphics.Color;
5 import com.google.gson.JsonElement;
6 import com.google.gson.JsonPrimitive;
8 import net.haspamelodica.swt.helper.gcs.GeneralGC;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
12 import net.mograsim.logic.core.LogicObserver;
13 import net.mograsim.logic.core.components.ManualSwitch;
14 import net.mograsim.logic.core.types.Bit;
15 import net.mograsim.logic.core.types.BitVector;
16 import net.mograsim.logic.core.types.BitVectorFormatter;
17 import net.mograsim.logic.model.model.ViewModelModifiable;
18 import net.mograsim.logic.model.model.components.GUIComponent;
19 import net.mograsim.logic.model.model.wires.Pin;
20 import net.mograsim.logic.model.model.wires.PinUsage;
21 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
22 import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapter;
23 import net.mograsim.logic.model.serializing.IdentifierGetter;
24 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
25 import net.mograsim.preferences.Preferences;
27 public class GUIManualSwitch extends GUIComponent
29 private static final double width = 20;
30 private static final double height = 15;
31 private static final double fontHeight = 5;
32 private static final double heightMiniButtons = 4; // 0 is disabled
34 public final int logicWidth;
35 private final Pin outputPin;
37 private final LogicObserver logicObs;
38 private ManualSwitch logicSwitch;
40 public GUIManualSwitch(ViewModelModifiable model, int logicWidth)
42 this(model, logicWidth, null);
45 public GUIManualSwitch(ViewModelModifiable model, int logicWidth, String name)
48 this.logicWidth = logicWidth;
49 logicObs = (i) -> model.requestRedraw();
51 setSize(width, height);
52 addPin(this.outputPin = new Pin(this, "", logicWidth, PinUsage.OUTPUT, width, height / 2));
56 public void render(GeneralGC gc, Rectangle visibleRegion)
58 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
59 if (foreground != null)
60 gc.setForeground(foreground);
61 gc.drawRectangle(getBounds());
62 String label = BitVectorFormatter.formatAsString(logicSwitch == null ? null : logicSwitch.getValues());
63 Font oldFont = gc.getFont();
64 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
65 gc.setFont(labelFont);
66 Point textExtent = gc.textExtent(label);
67 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
68 if (textColor != null)
69 gc.setForeground(textColor);
70 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
73 if (logicSwitch != null && logicWidth > 1 && heightMiniButtons > 0 && visibleRegion.y < getPosY() + heightMiniButtons)
77 gc.drawLine(x, y + heightMiniButtons, x + width, y + heightMiniButtons);
78 Color c = gc.getBackground();
79 gc.setBackground(gc.getForeground());
80 BitVector bv = logicSwitch.getValues();
81 double part = width / bv.length();
82 for (int i = 0; i < bv.length(); i++)
84 double start = x + part * i;
86 gc.drawLine(start, y, start, y + heightMiniButtons);
87 if (bv.getMSBit(i) == Bit.ONE)
89 // gc.fillRectangle(start, y, part, heightMiniButtons); // alternative, but not always visible what Bit is where
90 gc.drawLine(start, y, start + part, y + heightMiniButtons);
91 gc.drawLine(start + part, y, start, y + heightMiniButtons);
98 public void setLogicModelBinding(ManualSwitch logicSwitch)
100 if (this.logicSwitch != null)
101 this.logicSwitch.deregisterObserver(logicObs);
102 this.logicSwitch = logicSwitch;
103 if (logicSwitch != null)
104 logicSwitch.registerObserver(logicObs);
107 public boolean hasLogicModelBinding()
109 return logicSwitch != null;
113 public Object getHighLevelState(String stateID)
118 if (logicSwitch != null)
119 return logicSwitch.getValues();
122 return super.getHighLevelState(stateID);
127 public void setHighLevelState(String stateID, Object newState)
132 if (logicSwitch != null)
133 logicSwitch.setState((BitVector) newState);
136 super.setHighLevelState(stateID, newState);
142 public boolean clicked(double x, double y)
144 if (logicSwitch != null)
146 if (heightMiniButtons > 0 && y - getPosY() < heightMiniButtons)
148 int part = (int) ((x - getPosX()) * logicWidth / width);
149 logicSwitch.setState(logicSwitch.getValues().withBitChanged(part, Bit::not));
152 logicSwitch.toggle();
158 public ManualSwitch getManualSwitch()
163 public Pin getOutputPin()
169 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
171 return new JsonPrimitive(logicWidth);
176 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
177 IndirectGUIComponentCreator.setComponentSupplier(GUIManualSwitch.class.getName(),
178 (m, p, n) -> new GUIManualSwitch(m, p.getAsInt(), n));