1 package net.mograsim.logic.model.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.model.editor.states.EditorState;
12 import net.mograsim.logic.model.model.LogicModelModifiable;
13 import net.mograsim.logic.model.model.wires.ModelWire;
15 public class WireHandle extends Handle
17 private boolean selected = false;
18 private final static double WIDTH = 2.0;
19 private final static double WIDTH_SQUARED = WIDTH * WIDTH;
20 private final LogicModelModifiable model;
21 public final ModelWire parent;
23 public WireHandle(LogicModelModifiable model, ModelWire parent)
28 parent.addPathChangedListener(c -> updateBounds());
36 parent.removePathChangedListener(c -> updateBounds());
39 public void updateBounds()
41 Rectangle r = parent.getBounds();
43 setSize(r.width, r.height);
47 public void render(GeneralGC gc)
51 gc.setLineWidth(WIDTH);
52 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_YELLOW));
53 gc.drawPolyline(parent.getEffectivePath());
58 public void onSelect()
61 callRedrawListeners();
65 public void onDeselect()
68 callRedrawListeners();
72 public void reqDelete()
74 model.destroyWire(parent);
78 public boolean contains(double x, double y)
80 return click(parent, x, y).isPresent();
84 public boolean click(double x, double y, int stateMask, EditorState state)
86 Optional<WireClickData> op = click(parent, x, y);
89 WireClickData data = op.get();
90 return state.clickedHandle(new WireHandleClickInfo(this, data.segment, data.pos, stateMask));
93 public static class WireHandleClickInfo extends HandleClickInfo
95 public final int segment;
96 public final Point posOnWire;
98 WireHandleClickInfo(WireHandle clicked, int segment, Point posOnWire, int stateMask)
100 super(clicked, stateMask);
101 this.segment = segment;
102 this.posOnWire = posOnWire;
107 private static Optional<WireClickData> click(ModelWire w, double x, double y)
109 Rectangle modifiedBounds = w.getBounds();
110 modifiedBounds.x -= WIDTH;
111 modifiedBounds.y -= WIDTH;
112 modifiedBounds.width += WIDTH * 2;
113 modifiedBounds.height += WIDTH * 2;
114 if (modifiedBounds.contains(x, y))
116 double[] effectivePath = w.getEffectivePath();
117 for (int i = 3; i < effectivePath.length; i += 2)
119 double a1 = effectivePath[i - 3], a2 = effectivePath[i - 2], b1 = effectivePath[i - 1], b2 = effectivePath[i], r1 = b2 - a2,
122 double f = ((x - a1) * r2 + (a2 - y) * r1) / (-r2 * r2 - r1 * r1);
123 if (f >= 0 && f <= 1)
125 double e1 = a1 + f * (b1 - a1), e2 = a2 + f * (b2 - a2);
128 if (r1 * r1 + r2 * r2 <= WIDTH_SQUARED)
129 return Optional.of(new WireClickData(new Point(e1, e2), (i / 2) - 1));
133 return Optional.empty();
136 private final static class WireClickData
138 WireClickData(Point pos, int segment)
141 this.segment = segment;
145 * Position on the wire that is closest to the click
147 public final Point pos;
149 * Segment of the wire that the {@link Point} pos is on
151 public final int segment;
155 public HandleType getType()
157 return HandleType.WIRE;