1 package net.mograsim.logic.ui.editor.handles;
3 import java.util.Optional;
5 import org.eclipse.swt.SWT;
6 import org.eclipse.swt.widgets.Display;
8 import net.haspamelodica.swt.helper.gcs.GeneralGC;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
11 import net.mograsim.logic.ui.editor.states.EditorState;
12 import net.mograsim.logic.ui.model.wires.GUIWire;
13 import net.mograsim.logic.ui.model.wires.GUIWire.PathChangedListener;
15 public class WireHandle extends Handle implements PathChangedListener
17 private boolean selected = false;
18 private final static double WIDTH = 2.0;
19 private final static double WIDTH_SQUARED = WIDTH * WIDTH;
20 public final GUIWire parent;
22 public WireHandle(GUIWire parent)
25 parent.addPathChangedListener(this);
33 parent.removePathChangedListener(this);
36 public void updateBounds()
38 Rectangle r = parent.getBounds();
40 setSize(r.width, r.height);
44 public void render(GeneralGC gc)
48 gc.setLineWidth(WIDTH);
49 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_YELLOW));
50 gc.drawPolyline(parent.getEffectivePath());
55 public void onSelect()
58 callRedrawListeners();
62 public void onDeselect()
65 callRedrawListeners();
69 public void reqDelete()
75 public boolean contains(double x, double y)
77 return click(parent, x, y).isPresent();
81 public boolean click(double x, double y, int stateMask, EditorState state)
83 Optional<WireClickData> op = click(parent, x, y);
86 WireClickData data = op.get();
87 return state.clickedHandle(new WireHandleClickInfo(this, data.segment, data.pos, stateMask));
90 public static class WireHandleClickInfo extends HandleClickInfo
92 public final int segment;
93 public final Point posOnWire;
94 WireHandleClickInfo(WireHandle clicked, int segment, Point posOnWire, int stateMask)
96 super(clicked, stateMask);
97 this.segment = segment;
98 this.posOnWire = posOnWire;
103 private static Optional<WireClickData> click(GUIWire w, double x, double y)
105 Rectangle modifiedBounds = w.getBounds();
106 modifiedBounds.x -= WIDTH;
107 modifiedBounds.y -= WIDTH;
108 modifiedBounds.width += WIDTH * 2;
109 modifiedBounds.height += WIDTH * 2;
110 if (modifiedBounds.contains(x, y))
112 double[] effectivePath = w.getEffectivePath();
113 for (int i = 3; i < effectivePath.length; i += 2)
115 double a1 = effectivePath[i - 3], a2 = effectivePath[i - 2], b1 = effectivePath[i - 1],
116 b2 = effectivePath[i], r1 = b2 - a2, r2 = a1 - b1;
118 double f = ((x - a1) * r2 + (a2 - y) * r1) / (-r2 * r2 - r1 * r1);
119 if (f >= 0 && f <= 1)
121 double e1 = a1 + f * (b1 - a1), e2 = a2 + f * (b2 - a2);
124 if (r1 * r1 + r2 * r2 <= WIDTH_SQUARED)
125 return Optional.of(new WireClickData(new Point(e1, e2), (i / 2) - 1));
129 return Optional.empty();
132 private final static class WireClickData
134 WireClickData(Point pos, int segment)
137 this.segment = segment;
141 * Position on the wire that is closest to the click
143 public final Point pos;
145 * Segment of the wire that the {@link Point} pos is on
147 public final int segment;
151 public HandleType getType()
153 return HandleType.WIRE;
157 public void pathChanged(GUIWire wire, int diff)