-
Notifications
You must be signed in to change notification settings - Fork 1
/
View.pde
128 lines (111 loc) · 2.93 KB
/
View.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
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
121
122
123
124
125
126
127
class View {
float x, y, w, h;
ArrayList subviews;
View(float x_, float y_, float w_, float h_)
{
x = x_;
y = y_;
w = w_;
h = h_;
subviews = new ArrayList();
}
void draw()
{
pushMatrix();
translate(x, y);
// draw out content, then our subviews on top
drawContent();
for (int i = 0; i < subviews.size(); i++) {
View v = (View)subviews.get(i);
v.draw();
}
popMatrix();
}
void drawContent()
{
// override this
// when this is called, the coordinate system is local to the view,
// i.e. 0,0 is the top left corner of this view
}
boolean contentPressed(float lx, float ly)
{
// override this
// lx, ly are in the local coordinate system of the view,
// i.e. 0,0 is the top left corner of this view
// return false if the click is to "pass through" this view
return true;
}
boolean contentDragged(float lx, float ly)
{
return true;
}
boolean contentClicked(float lx, float ly)
{
return true;
}
boolean contentMouseWheel(float lx, float ly, int delta)
{
return false;
}
boolean ptInRect(float px, float py, float rx, float ry, float rw, float rh)
{
return px >= rx && px <= rx+rw && py >= ry && py <= ry+rh;
}
boolean mousePressed(float px, float py)
{
if (!ptInRect(px, py, x, y, w, h)) return false;
float lx = px - x;
float ly = py - y;
// check our subviews first
for (int i = subviews.size()-1; i >= 0; i--) {
View v = (View)subviews.get(i);
if (v.mousePressed(lx, ly)) return true;
}
return contentPressed(lx, ly);
}
boolean mouseDragged(float px, float py)
{
if (!ptInRect(px, py, x, y, w, h)) return false;
float lx = px - x;
float ly = py - y;
// check our subviews first
for (int i = subviews.size()-1; i >= 0; i--) {
View v = (View)subviews.get(i);
if (v.mouseDragged(lx, ly)) return true;
}
return contentDragged(lx, ly);
}
boolean mouseClicked(float px, float py)
{
if (!ptInRect(px, py, x, y, w, h)) return false;
float lx = px - x;
float ly = py - y;
// check our subviews first
for (int i = subviews.size()-1; i >= 0; i--) {
View v = (View)subviews.get(i);
if (v.mouseClicked(lx, ly)) return true;
}
return contentClicked(lx, ly);
}
boolean mouseWheel(float px, float py, int delta)
{
if (!ptInRect(px, py, x, y, w, h)) return false;
float lx = px - x;
float ly = py - y;
// check our subviews first
for (int i = subviews.size()-1; i >= 0; i--) {
View v = (View)subviews.get(i);
if (v.mouseWheel(lx, ly, delta)) return true;
}
return contentMouseWheel(lx, ly, delta);
}
boolean keypressed(){
char c = (char) key;
for (int i = subviews.size()-1; i >= 0; i--) {
// System.out.println(i);
View v = (View)subviews.get(i);
if (v.keypressed()) return true;
}
return false;
}
}