-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathTranslateHandle.java
More file actions
140 lines (122 loc) · 4.36 KB
/
TranslateHandle.java
File metadata and controls
140 lines (122 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package nodebox.handle;
import nodebox.graphics.*;
import nodebox.node.Node;
public class TranslateHandle extends AbstractHandle {
public static final int HANDLE_LENGTH = 100;
private enum DragState {
NONE, CENTER, HORIZONTAL, VERTICAL
}
private String txName, tyName;
private float px, py;
private float ox, oy;
private float handleLength = HANDLE_LENGTH;
private DragState dragState = DragState.NONE;
public TranslateHandle(Node node) {
this(node, "tx", "ty");
}
public TranslateHandle(Node node, String txName, String tyName) {
super(node);
this.txName = txName;
this.tyName = tyName;
}
public void draw(GraphicsContext ctx) {
float x = node.asFloat(txName);
float y = node.asFloat(tyName);
ctx.rectmode(GraphicsContext.RectMode.CENTER);
Path p = new Path();
p.setFillColor(HANDLE_COLOR);
ctx.stroke(HANDLE_COLOR);
p.setStrokeColor(null);
ctx.nofill();
drawDot(ctx, x, y);
if (dragState == DragState.NONE) {
// Horizontal and vertical direction lines.
ctx.line(x, y, x + handleLength, y);
ctx.line(x, y, x, y + handleLength);
// Vertical arrow
p.moveto(x, y + handleLength + 3);
p.lineto(x - 5, y + handleLength - 3);
p.lineto(x + 5, y + handleLength - 3);
// Horizontal arrow
p.moveto(x + handleLength + 3, y);
p.lineto(x + handleLength - 3, y - 5);
p.lineto(x + handleLength - 3, y + 5);
} else if (dragState == DragState.CENTER) {
ctx.line(px, py, x, y);
drawDot(ctx, x, y);
} else if (dragState == DragState.HORIZONTAL) {
float x0, x1;
ctx.line(px - handleLength, y, x + handleLength, y);
if (x + handleLength > px - handleLength) {
// arrow points right
x0 = x + handleLength + 3;
x1 = x + handleLength - 3;
} else {
// arrow points left
x0 = x + handleLength - 3;
x1 = x + handleLength + 3;
}
p.moveto(x0, y);
p.lineto(x1, y - 5);
p.lineto(x1, y + 5);
} else if (dragState == DragState.VERTICAL) {
float y0, y1;
ctx.line(x, py - handleLength, x, y + handleLength);
if (y + handleLength > py - handleLength) {
// arrow points down
y0 = y + handleLength + 3;
y1 = y + handleLength - 3;
} else {
// arrow points up
y0 = y + handleLength - 3;
y1 = y + handleLength + 3;
}
p.moveto(x, y0);
p.lineto(x - 5, y1);
p.lineto(x + 5, y1);
}
ctx.nostroke();
ctx.draw(p);
}
@Override
public boolean mousePressed(Point pt) {
px = pt.getX();
py = pt.getY();
float x = ox = node.asFloat(txName);
float y = oy = node.asFloat(tyName);
Rect centerRect = createHitRectangle(x, y);
Rect horRect = createHitRectangle(x + handleLength, y);
Rect vertRect = createHitRectangle(x, y + handleLength);
if (centerRect.contains(pt))
dragState = DragState.CENTER;
else if (horRect.contains(pt))
dragState = DragState.HORIZONTAL;
else if (vertRect.contains(pt))
dragState = DragState.VERTICAL;
return (dragState != DragState.NONE);
}
@Override
public boolean mouseDragged(Point pt) {
if (dragState == DragState.NONE) return false;
float dx = pt.x - px;
float dy = pt.y - py;
if (dx == 0 && dy == 0) return false;
startCombiningEdits("Set Value");
if (dragState == DragState.CENTER) {
silentSet(txName, ox + dx);
silentSet(tyName, oy + dy);
} else if (dragState == DragState.HORIZONTAL)
silentSet(txName, ox + dx);
else if (dragState == DragState.VERTICAL)
silentSet(tyName, oy + dy);
return true;
}
@Override
public boolean mouseReleased(Point pt) {
if (dragState == DragState.NONE) return false;
dragState = DragState.NONE;
stopCombiningEdits();
viewer.repaint();
return true;
}
}