-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thumbnail.pde
43 lines (36 loc) · 1.34 KB
/
Thumbnail.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
public class Thumbnail {
private PImage img;
private PImage originalImage;
private PVector pos;
private int imageSize = 32;
public Thumbnail(PImage img) {
this.originalImage = img.copy();
this.img = img.copy();
if (this.originalImage.width < this.originalImage.height) {
float ratio = this.originalImage.height / (float)this.originalImage.width;
this.img.resize(imageSize, floor(imageSize * ratio));
} else {
float ratio = this.originalImage.width / (float)this.originalImage.height;
this.img.resize(floor(imageSize * ratio), imageSize);
}
this.img = this.img.get(0, 0, imageSize, imageSize);
}
public void display(float x, float y) {
pos = new PVector(x, y);
fill(0);
rect(x - img.width/2.0 - 2, y - img.height/2.0 - 2, img.width + 4, img.height + 4);
image(img, x, y);
}
public final boolean mousePressed() {
return isHovered();
}
public boolean isHovered() {
if (pos == null) return false;
float px = pos.x - imageSize/2;
float py = pos.y - imageSize/2;
if (mouseX >= px && mouseX <= px + imageSize && mouseY >= py && mouseY <= py + imageSize) {
return true;
}
return false;
}
}