Merge branch 'development' of
[Mograsim.git] / net.mograsim.logic.model.editor / src / net / mograsim / logic / model / editor / handles / WirePointHandle.java
1 package net.mograsim.logic.model.editor.handles;
2
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.widgets.Display;
5
6 import net.haspamelodica.swt.helper.gcs.GeneralGC;
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
8 import net.mograsim.logic.core.wires.Wire;
9 import net.mograsim.logic.model.model.wires.GUIWire;
10
11 public class WirePointHandle extends Handle
12 {
13         private final static int END_OFFSET = 4;
14         private final HandleManager manager;
15         private boolean selected = false;
16         public final GUIWire parent;
17
18         private int pointIndex;
19
20         public WirePointHandle(HandleManager manager, GUIWire parent, int pointIndex)
21         {
22                 super();
23                 this.manager = manager;
24                 this.parent = parent;
25                 this.pointIndex = pointIndex;
26                 setSize(END_OFFSET, END_OFFSET);
27                 Point pathPoint = parent.getPathPoint(pointIndex);
28                 moveTo(pathPoint.x, pathPoint.y);
29         }
30
31         void updatePos()
32         {
33                 Point p = parent.getPathPoint(pointIndex);
34                 moveTo(p.x, p.y);
35         }
36
37         @Override
38         public void render(GeneralGC gc)
39         {
40                 gc.setLineWidth(1.0);
41                 gc.setForeground(Display.getDefault().getSystemColor(selected ? SWT.COLOR_YELLOW : SWT.COLOR_BLUE));
42                 gc.drawLine(getPosX(), getPosY(), getPosX() + END_OFFSET, getPosY() + END_OFFSET);
43         }
44
45         @Override
46         public void reqMove(double x, double y)
47         {
48                 parent.setPathPoint(new Point(x, y), pointIndex);
49         }
50
51         @Override
52         public void onSelect()
53         {
54                 selected = true;
55                 callRedrawListeners();
56         }
57
58         @Override
59         public void onDeselect()
60         {
61                 selected = false;
62                 callRedrawListeners();
63         }
64
65         @Override
66         public void reqDelete()
67         {
68                 manager.destroyWirePointHandle(parent, this);
69         }
70
71         @Override
72         public HandleType getType()
73         {
74                 return HandleType.WIRE_POINT;
75         }
76
77         /**
78          * Sets the index of the {@link Point} within the parent {@link Wire}s path that is controlled by this handle
79          * 
80          * @param index Index of the Point in the Wires path.
81          * @throws IndexOutOfBoundsException
82          */
83         public void setIndex(int index)
84         {
85                 this.pointIndex = index;
86                 updatePos();
87         }
88 }