Components now can be clicked
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Wed, 15 May 2019 09:48:47 +0000 (11:48 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Wed, 15 May 2019 09:48:47 +0000 (11:48 +0200)
LogicUI/src/LogicUI.java
LogicUI/src/era/mi/components/gui/BasicGUIComponent.java
LogicUI/src/era/mi/components/gui/GUIManualSwitch.java [new file with mode: 0644]
SWTHelper

index d0e23b4..f3864a4 100644 (file)
@@ -6,10 +6,12 @@ import java.util.Set;
 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
@@ -27,6 +29,7 @@ public class LogicUI
 {\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
@@ -35,21 +38,26 @@ public class LogicUI
                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
@@ -73,6 +81,21 @@ public class LogicUI
                                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
index 55929b9..db586a2 100644 (file)
@@ -16,6 +16,13 @@ public interface BasicGUIComponent
         * Used for calculating which component is clicked.\r
         */\r
        public Rectangle getBounds();\r
+       /**\r
+        * Called when this component is clicked. Relative coordinates of the click are given.\r
+        */\r
+       public default boolean clicked(double x, double y)\r
+       {\r
+               return false;\r
+       }\r
 \r
        //TODO this code will be replaced by code in BasicComponent.\r
        /**\r
diff --git a/LogicUI/src/era/mi/components/gui/GUIManualSwitch.java b/LogicUI/src/era/mi/components/gui/GUIManualSwitch.java
new file mode 100644 (file)
index 0000000..73ba263
--- /dev/null
@@ -0,0 +1,72 @@
+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
index 81cacb0..3fba149 160000 (submodule)
--- a/SWTHelper
+++ b/SWTHelper
@@ -1 +1 @@
-Subproject commit 81cacb00d348265dbb7bef45e0e0f8f55584ca35
+Subproject commit 3fba149afcd03ab2c9efdd817dca1daf0a8cfb05