-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolygonMask.pde
182 lines (157 loc) · 3.75 KB
/
PolygonMask.pde
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
public class PolygonMask
{
PGraphics mask;
ArrayList<PVector> polygon; // points in the mask, unscaled and relative to mask, not to sketch
String filename;
int width;
int height;
float blurRadius;
boolean inverse;
char settingsKey;
float scale = 1.0; // scale mask coordinates
float mouseOffsetX; // needed if layer isn't positioned at 0, 0 in the sketch
float mouseOffsetY;
boolean editing = false;
PolygonMask(String filename, int width, int height, float blurRadius, boolean inverse, char settingsKey,
float scale, float mouseOffsetX, float mouseOffsetY)
{
this.filename = filename;
this.width = width;
this.height = height;
this.blurRadius = blurRadius;
this.inverse = inverse;
this.settingsKey = settingsKey;
this.scale = scale;
this.mouseOffsetX = mouseOffsetX;
this.mouseOffsetY = mouseOffsetY;
mask = createGraphics(width, height);
polygon = new ArrayList<PVector>();
registerMethod("mouseEvent", this);
registerMethod("keyEvent", this);
registerDraw(this);
loadPolygon();
updateMask();
}
void savePolygon()
{
int length = polygon.size();
String[] lines = new String[length];
for (int i = 0; i < length; i++)
{
PVector vec = polygon.get(i);
lines[i] = vec.x + "\t" + vec.y;
}
saveStrings(filename, lines);
}
boolean loadPolygon()
{
String[] lines = loadStrings(filename);
if (lines == null)
return false;
for (int i=0; i<lines.length; i++)
{
String[] strings = split(lines[i], "\t");
PVector vec = new PVector(float(strings[0]), float(strings[1]));
polygon.add(vec);
}
return true;
}
void updateMask()
{
if (polygon.size() <= 2)
return;
float grey = 255;
if (inverse)
grey = 0;
mask.beginDraw();
mask.background(0);
mask.fill(255);
mask.noStroke();
mask.beginShape();
for (PVector v: polygon)
{
mask.vertex(v.x * scale, v.y * scale);
}
mask.endShape();
mask.endDraw();
if (blurRadius > 0)
mask.filter(BLUR, blurRadius);
mask.loadPixels();
for (int i=0; i<mask.pixels.length; i++)
{
color pixel = mask.pixels[i];
float alpha = 0;
if (inverse) {
alpha = 255 - red(pixel);
}
else {
alpha = red(pixel);
}
mask.pixels[i] = color(grey, grey, grey, alpha);
}
mask.updatePixels();
}
void createEllipticalMask()
{
mask.beginDraw();
mask.background(0);
mask.fill(255);
mask.noStroke();
mask.ellipse(width/2, height/2, width-90, height-90);
mask.endDraw();
if (blurRadius > 0)
mask.filter(BLUR, blurRadius);
mask.loadPixels();
for (int i=0; i<mask.pixels.length; i++)
{
color pixel = mask.pixels[i];
mask.pixels[i] = color(0, 0, 0, 255 - red(pixel));
}
mask.updatePixels();
}
public void mouseEvent(MouseEvent event)
{
if (event.getAction() == MouseEvent.CLICK)
{
if (editing)
{
polygon.add(new PVector( (mouseX - mouseOffsetX) / scale, (mouseY - mouseOffsetY) / scale));
}
}
}
void keyEvent(KeyEvent event)
{
if ( (event.getAction() == KeyEvent.RELEASE) && (event.getKey() == settingsKey) )
{
if (editing)
endEditing();
else
startEditing();
}
}
void startEditing()
{
editing = true;
polygon.clear();
}
void endEditing()
{
editing = false;
savePolygon();
updateMask();
}
void draw()
{
if (editing)
{
noStroke();
fill(255);
beginShape();
for (PVector v: polygon)
{
vertex(v.x, v.y);
}
endShape();
}
}
}