-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeManager.java
More file actions
162 lines (133 loc) · 3.18 KB
/
Copy pathShapeManager.java
File metadata and controls
162 lines (133 loc) · 3.18 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.util.Iterator;
import java.util.Vector;
import java.util.ArrayList;
import processing.core.*;
class ShapeManager{
static PApplet pa;
static Vector<Shape> shapes=new Vector<Shape>();
static boolean translating=false;
static ModifiableVector dragPoint=null;
static Shape current=null;
static boolean limitedPlacement=false;
static void init(PApplet p){
pa=p;
newShape();
}
static void draw() {
Iterator<Shape> i=shapes.iterator();
while (i.hasNext()) {
Shape s=i.next();
s.draw(!i.hasNext());
}
}
static void setFocus(Shape inFocus) {
for (Shape s:shapes) {
s.inFocus(false);
}
inFocus.inFocus(true);
current=inFocus;
}
static boolean mouseDragged(int mouseX, int mouseY, int pmouseX, int pmouseY) {
translating=false;
int vx=mouseX-pmouseX;
int vy=mouseY-pmouseY;
for (Shape s: shapes) {
if (dragPoint==null) {
ModifiableVector v=s.intersectsPoint(pmouseX,pmouseY);
if (v!=null) {
dragPoint=v;
setFocus(s);
s.setPointSelected(dragPoint.getPoint());
}
}
if (dragPoint!=null) {
translating=true;
float zoom=ZoomControl.getZoom();
int x=(int)(mouseX/zoom+0.5f);
int y=(int)(mouseY/zoom+0.5f);
int cx=Camera.getX();
int cy=Camera.getY();
x-=cx;
y-=cy;
if (limitedPlacement){
PVector prev=dragPoint.getPreviousPoint();
float dx=x-prev.x;
float dy=y-prev.y;
float angle=PApplet.atan2(dy,dx);
angle=PApplet.round((angle+(PConstants.QUARTER_PI)/2)/PConstants.QUARTER_PI)*PConstants.QUARTER_PI;
float r=PApplet.sqrt(dx*dx+dy*dy);
float rx=1*r;
float ry=0*r;
x=(int)(prev.x+(PApplet.cos(angle)*rx-PApplet.sin(angle)*ry));
y=(int)(prev.y+(PApplet.cos(angle)*ry+PApplet.sin(angle)*rx));
}
dragPoint.setPos(x,y);
return true;
}
}
return false;
}
static void mouseReleased(int mouseX, int mouseY, int pmouseX, int pmouseY) {
dragPoint=null;
if (GUI.clicked(mouseX,mouseY)) {
return;
}
int vx=mouseX-pmouseX;
int vy=mouseY-pmouseY;
if (!translating && vx*vx+vy*vy<5*5) {
Shape clicked=null;
for (Shape s: shapes) {
ModifiableVector v=s.intersectsPoint(mouseX,mouseY);
if (v!=null) {
for (Shape sh:shapes){
sh.inFocus(false);
}
s.setPointSelected(v.getPoint());
s.inFocus(true);
clicked=s;
break;
}
}
if (clicked!=null) {
setFocus(clicked);
} else {
current.addPoint(mouseX,mouseY);
}
}
translating=false;
}
static void limitDegrees(boolean on){
limitedPlacement=on;
}
static Shape getCurrentShape() {
return current;
}
static void newShape(){
if (current!=null && current.getVertexCount()==0) {
return;
}
Shape s=new Shape(pa);
shapes.add(s);
setFocus(s);
}
static PVector[][] getShapeCoordinates(){
int toExport=0;
for (Shape s: shapes) {
if (s.getVertexCount()>0) toExport++;
}
PVector[][] verticiesByShape=new PVector[toExport][];
int index=0;
for (Shape s: shapes) {
if (s.getVertexCount()==0) continue;
verticiesByShape[index]=s.getVerticies();
index++;
}
return verticiesByShape;
}
static void reset(){
current=null;
dragPoint=null;
translating=false;
shapes.clear();
}
}