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 boolean selected = false;
15         public final GUIWire parent;
16         
17         private int pointIndex;
18
19         public WirePointHandle(GUIWire parent, int pointIndex)
20         {
21                 super();
22                 this.parent = parent;
23                 this.pointIndex = pointIndex;
24                 setSize(END_OFFSET, END_OFFSET);
25                 Point pathPoint = parent.getPathPoint(pointIndex);
26                 moveTo(pathPoint.x, pathPoint.y);
27         }
28
29         void updatePos()
30         {
31                 Point p = parent.getPathPoint(pointIndex);
32                 moveTo(p.x, p.y);
33         }
34         
35         @Override
36         public void render(GeneralGC gc)
37         {
38                 gc.setLineWidth(1.0);
39                 gc.setForeground(Display.getDefault().getSystemColor(selected ? SWT.COLOR_YELLOW : SWT.COLOR_BLUE));
40                 gc.drawLine(getPosX(), getPosY(), getPosX() + END_OFFSET, getPosY() + END_OFFSET);
41         }
42
43         @Override
44         public void reqMove(double x, double y)
45         {
46                 parent.setPathPoint(new Point(x, y), pointIndex);
47         }
48         
49         @Override
50         public void onSelect()
51         {
52                 selected = true;
53                 callRedrawListeners();
54         }
55
56         @Override
57         public void onDeselect()
58         {
59                 selected = false;
60                 callRedrawListeners();
61         }
62         
63         @Override
64         public void reqDelete()
65         {
66                 parent.removePathPoint(pointIndex);
67         }
68         
69         @Override
70         public HandleType getType()
71         {
72                 return HandleType.WIRE_POINT;
73         }
74
75         /**
76          * Sets the index of the {@link Point} within the parent {@link Wire}s path that is controlled by this handle
77          * @param index Index of the Point in the Wires path.
78          * @throws IndexOutOfBoundsException
79          */
80         public void setIndex(int index)
81         {
82                 this.pointIndex = index;
83                 updatePos();
84         }
85 }