import org.eclipse.swt.SWT;\r
import org.eclipse.swt.layout.FillLayout;\r
import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.swt.widgets.Event;\r
import org.eclipse.swt.widgets.Shell;\r
\r
import era.mi.components.gui.BasicGUIComponent;\r
import era.mi.components.gui.GUIAndGate;\r
+import era.mi.components.gui.GUIManualSwitch;\r
import era.mi.components.gui.GUIMerger;\r
import era.mi.components.gui.GUIMux;\r
import era.mi.components.gui.GUINotGate;\r
{\r
private final Display display;\r
private final Shell shell;\r
+ private final ZoomableCanvas canvas;\r
private final Set<BasicGUIComponent> components;\r
private final Map<BasicGUIComponent, Point> componentPositions;\r
\r
display = new Display();\r
shell = new Shell(display);\r
shell.setLayout(new FillLayout());\r
- ZoomableCanvas canvas = new ZoomableCanvas(shell, SWT.NONE);\r
+ canvas = new ZoomableCanvas(shell, SWT.NONE);\r
\r
components = new HashSet<>();\r
componentPositions = new HashMap<>();\r
initComponents();\r
\r
canvas.addZoomedRenderer(gc -> components.forEach(component -> drawComponent(gc, component)));\r
- new ZoomableCanvasUserInput(canvas).enableUserInput();\r
+ ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);\r
+ userInput.buttonDrag = 3;\r
+ userInput.buttonZoom = 2;\r
+ userInput.enableUserInput();\r
new ZoomableCanvasOverlay(canvas, null).enableScale();\r
+ canvas.addListener(SWT.MouseDown, this::mouseDown);\r
}\r
private void initComponents()\r
{\r
Simulation.TIMELINE.reset();\r
WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), c = new WireArray(1, 10), d = new WireArray(2, 1), e = new WireArray(1, 1),\r
f = new WireArray(1, 1), g = new WireArray(1, 1), h = new WireArray(2, 1), i = new WireArray(2, 1), j = new WireArray(1, 1), k = new WireArray(1, 1);\r
+ addComponent(new GUIManualSwitch(a), 160, 10);\r
addComponent(new GUIAndGate(1, f, a, b), 130, 10);\r
addComponent(new GUINotGate(1, f, g), 100, 10);\r
addComponent(new GUIMerger(h, c, g), 70, 10);\r
tgc.fillOval(connectionPoint.x - 1, connectionPoint.y - 1, 2, 2);\r
}\r
}\r
+ private void mouseDown(Event e)\r
+ {\r
+ if(e.button == 1)\r
+ {\r
+ Point click = canvas.displayToWorldCoords(e.x, e.y);\r
+ for(BasicGUIComponent component : components)\r
+ if(component.getBounds().translate(componentPositions.get(component)).contains(click))\r
+ {\r
+ if(component.clicked(click.x, click.y))\r
+ canvas.redraw();\r
+ break;\r
+ }\r
+ }\r
+ }\r
+\r
public void run()\r
{\r
shell.open();\r
--- /dev/null
+package era.mi.components.gui;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\r
+import era.mi.logic.components.ManualSwitch;\r
+import era.mi.logic.wires.WireArray;\r
+import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
+import net.haspamelodica.swt.helper.swtobjectwrappers.Font;\r
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;\r
+import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;\r
+\r
+public class GUIManualSwitch extends ManualSwitch implements BasicGUIComponent\r
+{\r
+ private final List<WireArray> connectedWireArrays;\r
+ private final List<Point> wireArrayConnectionPoints;\r
+\r
+ public GUIManualSwitch(WireArray output)\r
+ {\r
+ super(output);\r
+\r
+ List<WireArray> connectedWireArraysModifiable = new ArrayList<>();\r
+ List<Point> wireArrayConnectionPointsModifiable = new ArrayList<>();\r
+\r
+ connectedWireArraysModifiable.add(output);\r
+ wireArrayConnectionPointsModifiable.add(new Point(20, 7.5));\r
+\r
+ this.connectedWireArrays = Collections.unmodifiableList(connectedWireArraysModifiable);\r
+ this.wireArrayConnectionPoints = Collections.unmodifiableList(wireArrayConnectionPointsModifiable);\r
+ }\r
+\r
+ @Override\r
+ public Rectangle getBounds()\r
+ {\r
+ return new Rectangle(0, 0, 20, 15);\r
+ }\r
+ @Override\r
+ public void render(GeneralGC gc)\r
+ {\r
+ gc.drawRectangle(0, 0, 20, 15);\r
+ String label = isOn() ? "ON" : "OFF";\r
+ Font oldFont = gc.getFont();\r
+ Font labelFont = new Font(oldFont.getName(), 6, oldFont.getStyle());\r
+ gc.setFont(labelFont);\r
+ Point textExtent = gc.textExtent(label);\r
+ gc.drawText(label, 10 - textExtent.x / 2, 7.5 - textExtent.y / 2, true);\r
+ gc.setFont(oldFont);\r
+ }\r
+ @Override\r
+ public boolean clicked(double x, double y)\r
+ {\r
+ toggle();\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public int getConnectedWireArraysCount()\r
+ {\r
+ return connectedWireArrays.size();\r
+ }\r
+ @Override\r
+ public WireArray getConnectedWireArray(int connectionIndex)\r
+ {\r
+ return connectedWireArrays.get(connectionIndex);\r
+ }\r
+ @Override\r
+ public Point getWireArrayConnectionPoint(int connectionI)\r
+ {\r
+ return wireArrayConnectionPoints.get(connectionI);\r
+ }\r
+}
\ No newline at end of file