1 package net.mograsim.logic.model.model.components.atomic;
3 import org.eclipse.swt.graphics.Color;
5 import net.haspamelodica.swt.helper.gcs.GeneralGC;
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
9 import net.mograsim.logic.core.LogicObserver;
10 import net.mograsim.logic.core.components.ManualSwitch;
11 import net.mograsim.logic.core.types.Bit;
12 import net.mograsim.logic.core.types.BitVector;
13 import net.mograsim.logic.core.types.BitVectorFormatter;
14 import net.mograsim.logic.model.model.ViewModelModifiable;
15 import net.mograsim.logic.model.model.components.GUIComponent;
16 import net.mograsim.logic.model.model.wires.Pin;
17 import net.mograsim.logic.model.model.wires.PinUsage;
18 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
19 import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapter;
20 import net.mograsim.logic.model.serializing.IdentifierGetter;
21 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
22 import net.mograsim.preferences.Preferences;
24 public class GUIManualSwitch extends GUIComponent
26 private static final double width = 20;
27 private static final double height = 15;
28 private static final double fontHeight = 5;
29 private static final double heightMiniButtons = 4; // 0 is disabled
31 public final int logicWidth;
32 private final Pin outputPin;
34 private final LogicObserver logicObs;
35 private ManualSwitch logicSwitch;
37 public GUIManualSwitch(ViewModelModifiable model, int logicWidth)
39 this(model, logicWidth, null);
42 public GUIManualSwitch(ViewModelModifiable model, int logicWidth, String name)
45 this.logicWidth = logicWidth;
46 logicObs = (i) -> model.requestRedraw();
48 setSize(width, height);
49 addPin(this.outputPin = new Pin(this, "", logicWidth, PinUsage.OUTPUT, width, height / 2));
53 public void render(GeneralGC gc, Rectangle visibleRegion)
55 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
56 if (foreground != null)
57 gc.setForeground(foreground);
58 gc.drawRectangle(getBounds());
59 String label = BitVectorFormatter.formatAsString(logicSwitch == null ? null : logicSwitch.getValues());
60 Font oldFont = gc.getFont();
61 Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
62 gc.setFont(labelFont);
63 Point textExtent = gc.textExtent(label);
64 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
65 if (textColor != null)
66 gc.setForeground(textColor);
67 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
70 if (logicSwitch != null && logicWidth > 1 && heightMiniButtons > 0 && visibleRegion.y < getPosY() + heightMiniButtons)
74 gc.drawLine(x, y + heightMiniButtons, x + width, y + heightMiniButtons);
75 Color c = gc.getBackground();
76 gc.setBackground(gc.getForeground());
77 BitVector bv = logicSwitch.getValues();
78 double part = width / bv.length();
79 for (int i = 0; i < bv.length(); i++)
81 double start = x + part * i;
83 gc.drawLine(start, y, start, y + heightMiniButtons);
84 if (bv.getMSBit(i) == Bit.ONE)
86 // gc.fillRectangle(start, y, part, heightMiniButtons); // alternative, but not always visible what Bit is where
87 gc.drawLine(start, y, start + part, y + heightMiniButtons);
88 gc.drawLine(start + part, y, start, y + heightMiniButtons);
95 public void setLogicModelBinding(ManualSwitch logicSwitch)
97 if (this.logicSwitch != null)
98 this.logicSwitch.deregisterObserver(logicObs);
99 this.logicSwitch = logicSwitch;
100 if (logicSwitch != null)
101 logicSwitch.registerObserver(logicObs);
104 public boolean hasLogicModelBinding()
106 return logicSwitch != null;
110 public Object getHighLevelState(String stateID)
115 if (logicSwitch != null)
116 return logicSwitch.getValues();
119 return super.getHighLevelState(stateID);
124 public void setHighLevelState(String stateID, Object newState)
129 if (logicSwitch != null)
130 logicSwitch.setState((BitVector) newState);
133 super.setHighLevelState(stateID, newState);
139 public boolean clicked(double x, double y)
141 if (logicSwitch != null)
143 if (heightMiniButtons > 0 && y - getPosY() < heightMiniButtons)
145 int part = (int) ((x - getPosX()) * logicWidth / width);
146 logicSwitch.setState(logicSwitch.getValues().withBitChanged(part, Bit::not));
149 logicSwitch.toggle();
155 public ManualSwitch getManualSwitch()
160 public Pin getOutputPin()
166 public Integer getParamsForSerializing(IdentifierGetter idGetter)
173 ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
174 IndirectGUIComponentCreator.setComponentSupplier(GUIManualSwitch.class.getName(),
175 (m, p, n) -> new GUIManualSwitch(m, p.getAsInt(), n));