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 8242666..9d31c30 100644 (file)
@@ -6,10 +6,12 @@ import java.util.Set;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.layout.FillLayout;
 import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Event;
 import org.eclipse.swt.widgets.Shell;
 
 import era.mi.components.gui.BasicGUIComponent;
 import era.mi.components.gui.GUIAndGate;
+import era.mi.components.gui.GUIManualSwitch;
 import era.mi.components.gui.GUIMerger;
 import era.mi.components.gui.GUIMux;
 import era.mi.components.gui.GUINotGate;
@@ -27,6 +29,7 @@ public class LogicUI
 {
        private final Display                                           display;
        private final Shell                                                     shell;
+       private final ZoomableCanvas                            canvas;
        private final Set<BasicGUIComponent>            components;
        private final Map<BasicGUIComponent, Point>     componentPositions;
 
@@ -35,21 +38,26 @@ public class LogicUI
                display = new Display();
                shell = new Shell(display);
                shell.setLayout(new FillLayout());
-               ZoomableCanvas canvas = new ZoomableCanvas(shell, SWT.NONE);
+               canvas = new ZoomableCanvas(shell, SWT.NONE);
 
                components = new HashSet<>();
                componentPositions = new HashMap<>();
                initComponents();
 
                canvas.addZoomedRenderer(gc -> components.forEach(component -> drawComponent(gc, component)));
-               new ZoomableCanvasUserInput(canvas).enableUserInput();
+               ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);
+               userInput.buttonDrag = 3;
+               userInput.buttonZoom = 2;
+               userInput.enableUserInput();
                new ZoomableCanvasOverlay(canvas, null).enableScale();
+               canvas.addListener(SWT.MouseDown, this::mouseDown);
        }
        private void initComponents()
        {
                Simulation.TIMELINE.reset();
                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),
                                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);
+               addComponent(new GUIManualSwitch(a), 160, 10);
                addComponent(new GUIAndGate(1, f, a, b), 130, 10);
                addComponent(new GUINotGate(1, f, g), 100, 10);
                addComponent(new GUIMerger(h, c, g), 70, 10);
@@ -73,6 +81,21 @@ public class LogicUI
                                tgc.fillOval(connectionPoint.x - 1, connectionPoint.y - 1, 2, 2);
                }
        }
+       private void mouseDown(Event e)
+       {
+               if(e.button == 1)
+               {
+                       Point click = canvas.displayToWorldCoords(e.x, e.y);
+                       for(BasicGUIComponent component : components)
+                               if(component.getBounds().translate(componentPositions.get(component)).contains(click))
+                               {
+                                       if(component.clicked(click.x, click.y))
+                                               canvas.redraw();
+                                       break;
+                               }
+               }
+       }
+
        public void run()
        {
                shell.open();
index 925fe4b..54384b9 100644 (file)
@@ -16,6 +16,13 @@ public interface BasicGUIComponent
         * Used for calculating which component is clicked.
         */
        public Rectangle getBounds();
+       /**
+        * Called when this component is clicked. Relative coordinates of the click are given.
+        */
+       public default boolean clicked(double x, double y)
+       {
+               return false;
+       }
 
        //TODO this code will be replaced by code in BasicComponent.
        /**
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..b12177f
--- /dev/null
@@ -0,0 +1,72 @@
+package era.mi.components.gui;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import era.mi.logic.components.ManualSwitch;
+import era.mi.logic.wires.WireArray;
+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;
+
+public class GUIManualSwitch extends ManualSwitch implements BasicGUIComponent
+{
+       private final List<WireArray>   connectedWireArrays;
+       private final List<Point>               wireArrayConnectionPoints;
+
+       public GUIManualSwitch(WireArray output)
+       {
+               super(output);
+
+               List<WireArray> connectedWireArraysModifiable = new ArrayList<>();
+               List<Point> wireArrayConnectionPointsModifiable = new ArrayList<>();
+
+               connectedWireArraysModifiable.add(output);
+               wireArrayConnectionPointsModifiable.add(new Point(20, 7.5));
+
+               this.connectedWireArrays = Collections.unmodifiableList(connectedWireArraysModifiable);
+               this.wireArrayConnectionPoints = Collections.unmodifiableList(wireArrayConnectionPointsModifiable);
+       }
+
+       @Override
+       public Rectangle getBounds()
+       {
+               return new Rectangle(0, 0, 20, 15);
+       }
+       @Override
+       public void render(GeneralGC gc)
+       {
+               gc.drawRectangle(0, 0, 20, 15);
+               String label = isOn() ? "ON" : "OFF";
+               Font oldFont = gc.getFont();
+               Font labelFont = new Font(oldFont.getName(), 6, oldFont.getStyle());
+               gc.setFont(labelFont);
+               Point textExtent = gc.textExtent(label);
+               gc.drawText(label, 10 - textExtent.x / 2, 7.5 - textExtent.y / 2, true);
+               gc.setFont(oldFont);
+       }
+       @Override
+       public boolean clicked(double x, double y)
+       {
+               toggle();
+               return true;
+       }
+
+       @Override
+       public int getConnectedWireArraysCount()
+       {
+               return connectedWireArrays.size();
+       }
+       @Override
+       public WireArray getConnectedWireArray(int connectionIndex)
+       {
+               return connectedWireArrays.get(connectionIndex);
+       }
+       @Override
+       public Point getWireArrayConnectionPoint(int connectionI)
+       {
+               return wireArrayConnectionPoints.get(connectionI);
+       }
+}
\ No newline at end of file
index 81cacb0..3fba149 160000 (submodule)
--- a/SWTHelper
+++ b/SWTHelper
@@ -1 +1 @@
-Subproject commit 81cacb00d348265dbb7bef45e0e0f8f55584ca35
+Subproject commit 3fba149afcd03ab2c9efdd817dca1daf0a8cfb05