Started restructuring LogicUI
[Mograsim.git] / LogicUI / src / era / mi / gui / LogicUICanvas.java
index fd6d4da..4e61988 100644 (file)
@@ -1,19 +1,18 @@
 package era.mi.gui;
 
-import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Map;
 import java.util.Set;
 
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Event;
 
-import era.mi.gui.components.BasicGUIComponent;
-import era.mi.gui.wires.GUIWire;
+import era.mi.gui.model.components.GUIComponent;
+import era.mi.gui.model.wires.GUIWire;
+import era.mi.gui.model.wires.Pin;
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
-import net.haspamelodica.swt.helper.gcs.TranslatedGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas;
 
 /**
@@ -23,8 +22,7 @@ import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas;
  */
 public class LogicUICanvas extends ZoomableCanvas
 {
-       private final Set<BasicGUIComponent> components;
-       private final Map<BasicGUIComponent, Point> componentPositions;
+       private final Set<GUIComponent> components;
        private final Set<GUIWire> wires;
 
        public LogicUICanvas(Composite parent, int style)
@@ -32,10 +30,13 @@ public class LogicUICanvas extends ZoomableCanvas
                super(parent, style);
 
                components = new HashSet<>();
-               componentPositions = new HashMap<>();
                wires = new HashSet<>();
 
-               addZoomedRenderer(gc -> components.forEach(c -> drawComponent(gc, c)));
+               addZoomedRenderer(gc ->
+               {
+                       Rectangle visibleRegion = new Rectangle(offX, offY, gW / zoom, gH / zoom);
+                       components.forEach(c -> drawComponent(gc, c, visibleRegion));
+               });
                addZoomedRenderer(gc -> wires.forEach(w -> w.render(gc)));
                addListener(SWT.MouseDown, this::mouseDown);
        }
@@ -45,10 +46,10 @@ public class LogicUICanvas extends ZoomableCanvas
         * 
         * @author Daniel Kirschten
         */
-       public <C extends BasicGUIComponent> C addComponent(C component, double x, double y)
+       // TODO replace with model change listener
+       public <C extends GUIComponent> C addComponent(C component)
        {
                components.add(component);
-               componentPositions.put(component, new Point(x, y));
                return component;
        }
 
@@ -58,18 +59,21 @@ public class LogicUICanvas extends ZoomableCanvas
         * 
         * @author Daniel Kirschten
         */
-       public void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2,
-                       int component2ConnectionIndex, Point... path)
+       // TODO replace with model change listener
+       public void addWire(Pin pin1, Pin pin2, Point... path)
        {
-               wires.add(new GUIWire(this::redrawThreadsafe, component1, component1ConnectionIndex, componentPositions.get(component1), component2,
-                               component2ConnectionIndex, componentPositions.get(component2), path));
+               wires.add(new GUIWire(this::redrawThreadsafe, pin1, pin2, path));
        }
 
-       private void drawComponent(GeneralGC gc, BasicGUIComponent component)
+       private void drawComponent(GeneralGC gc, GUIComponent component, Rectangle visibleRegion)
        {
-               TranslatedGC tgc = new TranslatedGC(gc, componentPositions.get(component));
-               component.render(tgc);
-               tgc.setBackground(getDisplay().getSystemColor(SWT.COLOR_BLUE));
+               component.render(gc, visibleRegion);
+               gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_CYAN));
+               for (Pin p : component.getPins())
+               {
+                       Point pos = p.getPos();
+                       gc.fillOval(pos.x - 1, pos.y - 1, 2, 2);
+               }
        }
 
        private void mouseDown(Event e)
@@ -77,8 +81,8 @@ public class LogicUICanvas extends ZoomableCanvas
                if (e.button == 1)
                {
                        Point click = displayToWorldCoords(e.x, e.y);
-                       for (BasicGUIComponent component : components)
-                               if (component.getBounds().translate(componentPositions.get(component)).contains(click))
+                       for (GUIComponent component : components)
+                               if (component.getBounds().contains(click))
                                {
                                        if (component.clicked(click.x, click.y))
                                                redraw();