-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathScaleHandle.java
More file actions
136 lines (123 loc) · 4.41 KB
/
ScaleHandle.java
File metadata and controls
136 lines (123 loc) · 4.41 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
package nodebox.handle;
import nodebox.graphics.CanvasContext;
import nodebox.graphics.GraphicsContext;
import nodebox.graphics.Point;
import nodebox.graphics.Rect;
import nodebox.node.Node;
public class ScaleHandle extends AbstractHandle {
public static final int HANDLE_WIDTH = 100;
public static final int HANDLE_HEIGHT = 100;
private enum DragState {
NONE, TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT
}
private String sxName, syName;
private float px, py;
private float ox, oy;
private float handleWidth = HANDLE_WIDTH;
private float handleHeight = HANDLE_HEIGHT;
private boolean scaleHorizontal = true;
private boolean scaleVertical = true;
private DragState dragState = DragState.NONE;
public ScaleHandle(Node node) {
this(node, "sx", "sy");
}
public ScaleHandle(Node node, String sxName, String syName) {
super(node);
this.sxName = sxName;
this.syName = syName;
}
public void draw(GraphicsContext ctx) {
ctx.nofill();
ctx.stroke(HANDLE_COLOR);
float halfWidth = handleWidth / 2;
float halfHeight = handleHeight / 2;
ctx.rectmode(GraphicsContext.RectMode.CENTER);
ctx.rect(0, 0, handleWidth, handleHeight);
drawDot(ctx, -halfWidth, -halfHeight);
drawDot(ctx, halfWidth, -halfHeight);
drawDot(ctx, -halfWidth, halfHeight);
drawDot(ctx, halfWidth, halfHeight);
}
@Override
public boolean mousePressed(Point pt) {
float left = -handleWidth / 2;
float right = handleWidth / 2;
float top = -handleHeight / 2;
float bottom = handleHeight / 2;
Rect topLeft = createHitRectangle(left, top);
Rect topRight = createHitRectangle(right, top);
Rect bottomLeft = createHitRectangle(left, bottom);
Rect bottomRight = createHitRectangle(right, bottom);
px = pt.getX();
py = pt.getY();
ox = node.asFloat(sxName);
oy = node.asFloat(syName);
if (topLeft.contains(pt))
dragState = DragState.TOP_LEFT;
else if (topRight.contains(pt))
dragState = DragState.TOP_RIGHT;
else if (bottomLeft.contains(pt))
dragState = DragState.BOTTOM_LEFT;
else if (bottomRight.contains(pt))
dragState = DragState.BOTTOM_RIGHT;
else
dragState = DragState.NONE;
return (dragState != DragState.NONE);
}
@Override
public boolean mouseDragged(Point pt) {
if (dragState == DragState.NONE) return false;
float x = pt.getX();
float y = pt.getY();
float dx = x - px;
float dy = y - py;
if (dx == 0 && dy == 0) return false;
startCombiningEdits("Set Value");
if (dragState == DragState.TOP_LEFT) {
handleWidth = HANDLE_WIDTH - dx * 2;
handleHeight = HANDLE_HEIGHT - dy * 2;
} else if (dragState == DragState.TOP_RIGHT) {
handleWidth = HANDLE_WIDTH + dx * 2;
handleHeight = HANDLE_HEIGHT - dy * 2;
} else if (dragState == DragState.BOTTOM_LEFT) {
handleWidth = HANDLE_WIDTH - dx * 2;
handleHeight = HANDLE_HEIGHT + dy * 2;
} else if (dragState == DragState.BOTTOM_RIGHT) {
handleWidth = HANDLE_WIDTH + dx * 2;
handleHeight = HANDLE_HEIGHT + dy * 2;
}
float pctX = handleWidth / HANDLE_WIDTH;
float pctY = handleHeight / HANDLE_HEIGHT;
if (scaleHorizontal)
silentSet(sxName, ox * pctX);
else
handleWidth = HANDLE_WIDTH;
if (scaleVertical)
silentSet(syName, oy * pctY);
else
handleHeight = HANDLE_HEIGHT;
return true;
}
@Override
public boolean mouseReleased(Point pt) {
if (dragState == DragState.NONE) return false;
handleWidth = HANDLE_WIDTH;
handleHeight = HANDLE_HEIGHT;
dragState = DragState.NONE;
stopCombiningEdits();
viewer.repaint();
return true;
}
@Override
public boolean keyPressed(int keyCode, int modifiers) {
if ((modifiers & SHIFT_DOWN) != 0) scaleVertical = false;
else if ((modifiers & META_DOWN) != 0) scaleHorizontal = false;
return true;
}
@Override
public boolean keyReleased(int keyCode, int modifiers) {
scaleHorizontal = true;
scaleVertical = true;
return false;
}
}