-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopArtCube.pde
174 lines (138 loc) · 4.03 KB
/
PopArtCube.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
class PopArtCube {
float x;
float y;
float width;
float height;
int imgCount = 2;
PImage[] imgs = new PImage[imgCount]; // [front image, back image]
int[] imgIndexes = new int[imgCount];
int imgAddIndex = 0;
int imgIndex = 0; // 0 : front image
int status = 0; // 0 : image showing, 1 : transition
boolean isRunning = false;
PGraphics[] fadeOutMasks;
PGraphics[] fadeInMasks;
int maskCount;
int maskIndex = 0;
PImage colorImg;
boolean isNextImg = false;
int transitionCount = 0;
boolean isStop = false;
boolean isUpside = false;
PopArtCube (float x, float y, float w, float h) {
// empty frame
this.x = x;
this.y = y;
this.width = w;
this.height = h;
for(int i = 0; i < imgCount; i++) {
imgs[i] = createImage((int)width, (int)height, RGB);
}
}
void addImage(PImage img, int index) {
imgIndexes[imgAddIndex] = index;
imgs[imgAddIndex].copy(img, 0, 0, (int)width, (int)height, 0, 0, (int)width, (int)height);
imgAddIndex = (imgAddIndex + 1) % imgCount;
}
void setColor(color cubeColor) {
this.colorImg = createImage(int(width), int(height), RGB);
colorImg.loadPixels();
for (int i = 0; i < colorImg.pixels.length; i++) {
colorImg.pixels[i] = cubeColor;
}
colorImg.updatePixels();
}
void setMasks(PGraphics[] fadeOutMasks, PGraphics[] fadeInMasks) {
this.fadeOutMasks = fadeOutMasks;
this.fadeInMasks = fadeInMasks;
this.maskCount = fadeOutMasks.length;
}
void update() {
if (status == 1) {
}
}
void display() {
fill(0);
noStroke();
rect(x, y, width, height);
textureMode(NORMAL);
if (status == 0) {
image(imgs[imgIndex], x, y);
}
else {
if (isUpside) {
float transValue = map(maskIndex, 0, maskCount, 0, height);
imgs[imgIndex].mask(fadeOutMasks[maskIndex]);
beginShape();
texture(imgs[imgIndex]);
vertex(x, y, 0, 0);
vertex(x + width, y, 1, 0);
vertex(x + width, y + height - transValue, 1, 1);
vertex(x, y + height - transValue, 0, 1);
endShape();
imgs[(imgIndex + 1) % imgCount].mask(fadeInMasks[maskIndex]);
beginShape();
texture(imgs[(imgIndex + 1) % imgCount]);
vertex(x, y + height - transValue, 0, 0);
vertex(x + width, y + height - transValue, 1, 0);
vertex(x + width, y + height, 1, 1);
vertex(x, y + height, 0, 1);
endShape();
} else {
float transValue = map(maskIndex, 0, maskCount, 0, height);
imgs[imgIndex].mask(fadeInMasks[maskCount - maskIndex - 1]);
beginShape();
texture(imgs[imgIndex]);
vertex(x, y + transValue, 0, 0);
vertex(x + width, y + transValue, 1, 0);
vertex(x + width, y + height, 1, 1);
vertex(x, y + height, 0, 1);
endShape();
imgs[(imgIndex + 1) % imgCount].mask(fadeOutMasks[maskCount - maskIndex - 1]);
beginShape();
texture(imgs[(imgIndex + 1) % imgCount]);
vertex(x, y, 0, 0);
vertex(x + width, y, 1, 0);
vertex(x + width, y + transValue, 1, 1);
vertex(x, y + transValue, 0, 1);
endShape();
}
maskIndex++;
if (maskIndex == maskCount) {
status = 0; // change status to show
imgIndex = (imgIndex + 1) % imgCount; // change front image index
maskIndex = 0;
transitionCount++;
}
}
}
boolean transition() {
if (isStop) {
isRunning = false;
} else {
if (isRunning) {
if (status == 0) {
isRunning = false;
}
}
else {
this.status = 1;
isRunning = true;
}
}
return isRunning;
}
void transitionStop() {
isStop = true;
}
int getImgIndex() {
return imgIndexes[imgIndex];
}
void setDirection() {
if (random(1) > 0.5) {
isUpside = true;
} else {
isUpside = false;
}
}
}