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=fd6d4daa030e919e9d55679a1dc011b3e335c3df;hb=da0a4b7d0619283bb4384091356ce3f105c84e78;hp=0000000000000000000000000000000000000000;hpb=d51bf9c3df364fa37f80730a02a67009edf729e4;p=Mograsim.git diff --git a/LogicUI/src/era/mi/gui/LogicUICanvas.java b/LogicUI/src/era/mi/gui/LogicUICanvas.java new file mode 100644 index 00000000..fd6d4daa --- /dev/null +++ b/LogicUI/src/era/mi/gui/LogicUICanvas.java @@ -0,0 +1,89 @@ +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 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.zoomablecanvas.ZoomableCanvas; + +/** + * Simulation visualizer canvas. + * + * @author Daniel Kirschten + */ +public class LogicUICanvas extends ZoomableCanvas +{ + private final Set components; + private final Map componentPositions; + private final Set wires; + + public LogicUICanvas(Composite parent, int style) + { + super(parent, style); + + components = new HashSet<>(); + componentPositions = new HashMap<>(); + wires = new HashSet<>(); + + addZoomedRenderer(gc -> components.forEach(c -> drawComponent(gc, c))); + addZoomedRenderer(gc -> wires.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 + */ + public C addComponent(C component, double x, double y) + { + components.add(component); + componentPositions.put(component, new Point(x, y)); + 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 + */ + public void addWire(BasicGUIComponent component1, int component1ConnectionIndex, BasicGUIComponent component2, + int component2ConnectionIndex, Point... path) + { + wires.add(new GUIWire(this::redrawThreadsafe, component1, component1ConnectionIndex, componentPositions.get(component1), component2, + component2ConnectionIndex, componentPositions.get(component2), path)); + } + + private void drawComponent(GeneralGC gc, BasicGUIComponent component) + { + TranslatedGC tgc = new TranslatedGC(gc, componentPositions.get(component)); + component.render(tgc); + tgc.setBackground(getDisplay().getSystemColor(SWT.COLOR_BLUE)); + } + + private void mouseDown(Event e) + { + if (e.button == 1) + { + Point click = 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)) + redraw(); + break; + } + } + } +} \ No newline at end of file