-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.pde
74 lines (58 loc) · 2.21 KB
/
Image.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
public class Image {
PImage img;
PVector pos;
public Image() {
this.img = null;
this.pos = new PVector();
}
public void setImage(String path) {
PImage img = loadImage(path);
this.setImage(img);
}
public void setImage(PImage img) {
this.img = img.copy();
}
public void setPos(float x, float y) {
this.pos = new PVector(x, y);
}
public void setPos(PVector pos) {
this.pos = pos;
}
public PImage getImage() {
return img.copy();
}
public PVector getPos() {
return this.pos;
}
public void display() {
display(0, 0);
}
public void display(float offsetX, float offsetY) {
if (img == null) return;
PVector center = new PVector(width/2, height/2);
PVector imgCenter = new PVector(img.width/2, img.height/2);
float x = pos.x + offsetX, // Image pos with drawing offset
y = pos.y + offsetY, // Image pos with drawing offset
dx = center.x / zoom, // Draw image at x coord
dy = center.y / zoom, // Draw image at y coord
dw = width / zoom + 1, // Draw image with width
dh = height / zoom + 1; // Draw image with height
int sx1 = (int) (imgCenter.x - dx - x / zoom), // Use image region x1
sy1 = (int) (imgCenter.y - dy - y / zoom), // Use image region y1
sx2 = sx1 + (int) dw, // Use image region x2
sy2 = sy1 + (int) dh; // Use image region y2
pushMatrix();
scale(zoom);
imageMode(CENTER);
image(img, dx, dy, dw, dh, sx1, sy1, sx2, sy2);
popMatrix();
}
public final boolean isHovering() {
PVector center = new PVector(width/2, height/2);
PVector mousePos = new PVector(mouseX - center.x, mouseY - center.y);
float imgWidth = img.width * zoom;
float imgHeight = img.height * zoom;
return (mousePos.x > pos.x - imgWidth/2.0 && mousePos.x < pos.x + imgWidth/2.0)
&& (mousePos.y > pos.y - imgHeight/2.0 && mousePos.y < pos.y + imgHeight/2.0);
}
}