-
Notifications
You must be signed in to change notification settings - Fork 0
/
DraggableImage.pde
74 lines (58 loc) · 1.8 KB
/
DraggableImage.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 DraggableImage extends Image {
PVector mouseDownPos;
PVector draggedPos = new PVector();
boolean dragging = false;
public DraggableImage() {
super();
}
public DraggableImage(String imagePath) {
this.img = loadImage(imagePath);
this.pos = new PVector();
}
public void updatePos() {
this.pos.add(draggedPos.x, draggedPos.y);
this.draggedPos = new PVector();
}
public void setDragging(boolean value) {
this.dragging = value;
}
public void setDraggedPos(float x, float y) {
this.draggedPos = new PVector(x, y);
}
public void setDraggedPos(PVector pos) {
this.draggedPos = pos;
}
public PVector getDraggedPos() {
return this.draggedPos;
}
public void display() {
display(draggedPos.x, draggedPos.y);
}
public void moveImage(int direction, int amount) {
switch (direction) {
case UP: pos.y += amount; break;
case DOWN: pos.y -= amount; break;
case LEFT: pos.x += amount; break;
case RIGHT: pos.x -= amount; break;
//default: println("Invalid direction " + direction); break;
}
}
/* Mouse functions */
public final boolean mousePressed() {
boolean hovering = isHovering();
if (hovering) {
mouseDownPos = new PVector(mouseX, mouseY);
dragging = true;
}
return hovering;
}
public void mouseDragged() {
if (dragging) {
PVector draggedPos = new PVector(mouseX - mouseDownPos.x, mouseY - mouseDownPos.y);
setDraggedPos(draggedPos);
}
}
public final void mouseReleased() {
updatePos();
}
}