X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=LogicUI%2Fsrc%2Fera%2Fmi%2Fgui%2FLogicUICanvas.java;fp=LogicUI%2Fsrc%2Fera%2Fmi%2Fgui%2FLogicUICanvas.java;h=da338013fe528388956f67920d90eb8fad149760;hb=f2886cbd57dd08b797921fc2421b41bd92915799;hp=4e6198843db6bf104740a950b1b70e2673741b1d;hpb=4a6a0a2c85c1a16112efaf7ca6d2b5fba3c9b466;p=Mograsim.git diff --git a/LogicUI/src/era/mi/gui/LogicUICanvas.java b/LogicUI/src/era/mi/gui/LogicUICanvas.java index 4e619884..da338013 100644 --- a/LogicUI/src/era/mi/gui/LogicUICanvas.java +++ b/LogicUI/src/era/mi/gui/LogicUICanvas.java @@ -1,14 +1,11 @@ package era.mi.gui; -import java.util.HashSet; -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.model.ViewModel; 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.swtobjectwrappers.Point; @@ -22,49 +19,26 @@ import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas; */ public class LogicUICanvas extends ZoomableCanvas { - private final Set components; - private final Set wires; + private final ViewModel model; - public LogicUICanvas(Composite parent, int style) + public LogicUICanvas(Composite parent, int style, ViewModel model) { super(parent, style); - components = new HashSet<>(); - wires = new HashSet<>(); + this.model = model; + + model.addComponentAddedListener(c -> redrawThreadsafe()); + model.addWireAddedListener(c -> redrawThreadsafe()); addZoomedRenderer(gc -> { Rectangle visibleRegion = new Rectangle(offX, offY, gW / zoom, gH / zoom); - components.forEach(c -> drawComponent(gc, c, visibleRegion)); + model.getComponents().forEach(c -> drawComponent(gc, c, visibleRegion)); }); - addZoomedRenderer(gc -> wires.forEach(w -> w.render(gc))); + addZoomedRenderer(gc -> model.getWires().forEach(w -> w.render(gc))); addListener(SWT.MouseDown, this::mouseDown); } - /** - * Add a component to be drawn. Returns the given component for convenience. - * - * @author Daniel Kirschten - */ - // TODO replace with model change listener - public C addComponent(C component) - { - components.add(component); - return component; - } - - /** - * Add a graphical wire between the given connection points of the given components. The given components have to be added and the given - * connection points have to be connected logically first. - * - * @author Daniel Kirschten - */ - // TODO replace with model change listener - public void addWire(Pin pin1, Pin pin2, Point... path) - { - wires.add(new GUIWire(this::redrawThreadsafe, pin1, pin2, path)); - } - private void drawComponent(GeneralGC gc, GUIComponent component, Rectangle visibleRegion) { component.render(gc, visibleRegion); @@ -81,11 +55,10 @@ public class LogicUICanvas extends ZoomableCanvas if (e.button == 1) { Point click = displayToWorldCoords(e.x, e.y); - for (GUIComponent component : components) - if (component.getBounds().contains(click)) + for (GUIComponent component : model.getComponents()) + if (component.getBounds().contains(click) && component.clicked(click.x, click.y)) { - if (component.clicked(click.x, click.y)) - redraw(); + redraw(); break; } }