-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandles.cpp
More file actions
120 lines (120 loc) · 2.85 KB
/
Copy pathhandles.cpp
File metadata and controls
120 lines (120 loc) · 2.85 KB
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
/**
* Handles.
*
* Click and drag the white boxes to change their position.
*/
bool firstMousePress = false;
class Handle {
public:
int x, y;
int boxx, boxy;
int stretch;
int size;
bool over;
bool press;
bool locked = false;
bool otherslocked = false;
ArrayList<Handle>* others;
Handle(int ix, int iy, int il, int is, ArrayList<Handle>* o) {
x = ix;
y = iy;
stretch = il;
size = is;
boxx = x + stretch - size / 2;
boxy = y - size / 2;
others = o;
over = false;
press = false;
}
void update() {
boxx = x + stretch;
boxy = y - size / 2;
for (int i = 0; i < others->size(); i++) {
if (others->get(i)->locked == true) {
otherslocked = true;
break;
} else {
otherslocked = false;
}
}
if (otherslocked == false) {
overEvent();
pressEvent();
}
if (press) {
stretch = lock(mouseX - width / 2 - size / 2, 0, width / 2 - size - 1);
}
}
void overEvent() {
if (overRect(boxx, boxy, size, size)) {
over = true;
} else {
over = false;
}
}
void pressEvent() {
if ((over && firstMousePress) || locked) {
press = true;
locked = true;
} else {
press = false;
}
}
void releaseEvent() {
locked = false;
}
void display() {
line(x, y, x + stretch, y);
fill(255);
stroke(0);
rect(boxx, boxy, size, size);
if (over || press) {
line(boxx, boxy, boxx + size, boxy + size);
line(boxx, boxy + size, boxx + size, boxy);
}
}
bool overRect(int rx, int ry, int rw, int rh) {
if (mouseX >= rx && mouseX <= rx + rw &&
mouseY >= ry && mouseY <= ry + rh) {
return true;
} else {
return false;
}
}
int lock(int val, int minv, int maxv) {
return min(max(val, minv), maxv);
}
};
ArrayList<Handle> handles;
void setup() {
size(640, 360);
int num = height / 15;
int hsize = 10;
handles = ArrayList<Handle>();
for (int i = 0; i < num; i++) {
handles.add(new Handle(width / 2, 10 + i * 15, 50 - hsize / 2, 10, &handles));
}
}
void draw() {
background(153);
for (int i = 0; i < handles.size(); i++) {
Handle* h = handles.get(i);
h->update();
h->display();
}
fill(0);
rect(0, 0, width / 2, height);
if (firstMousePress) {
firstMousePress = false;
}
}
void mousePressed() {
if (!firstMousePress) {
firstMousePress = true;
}
}
void mouseReleased() {
for (int i = 0; i < handles.size(); i++) {
handles.get(i)->releaseEvent();
}
}