-
Notifications
You must be signed in to change notification settings - Fork 0
/
Applications_DraggableExample.pde
77 lines (67 loc) · 1.78 KB
/
Applications_DraggableExample.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
class Applications_DraggableExample extends Window {
float bx;
float by;
int boxSizeX = 20;
int boxSizeY = 20;
boolean overBox = false;
boolean locked = false;
float xOffset = 0.0;
float yOffset = 0.0;
// https://gitlab.freedesktop.org/wayland/weston/-/blob/master/clients/simple-egl.c#L358
@Override
void setup() {
// if this is called after a resize then
// the user will lose the current position of the box after a resize
bx = width/2.0;
by = height/2.0;
// move rectMode into draw as PGraphics recreation seems to reset rectMode
}
@Override
void draw() {
graphics.background(0);
// reset rectMode here
graphics.rectMode(RADIUS);
// we risk loosing the box entirely if the user resizes the graphics object
// to less than bx, by
// if this happens the user will need to resize the graphics object
// move the box to a suitible location
// then resize the graphics object again
// Test if the cursor is over the box
if (mouseX > bx-boxSizeX && mouseX < bx+boxSizeX &&
mouseY > by-boxSizeY && mouseY < by+boxSizeY) {
overBox = true;
if(!locked) {
graphics.stroke(255);
graphics.fill(153);
}
} else {
graphics.stroke(153);
graphics.fill(153);
overBox = false;
}
// Draw the box
graphics.rect(bx, by, boxSizeX, boxSizeY);
}
@Override
void mousePressed() {
if(overBox) {
locked = true;
graphics.fill(255, 255, 255);
} else {
locked = false;
}
xOffset = mouseX-bx;
yOffset = mouseY-by;
}
@Override
void mouseDragged() {
if(locked) {
bx = mouseX-xOffset;
by = mouseY-yOffset;
}
}
@Override
void mouseReleased() {
locked = false;
}
}