-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListBox.pde
101 lines (87 loc) · 2.51 KB
/
ListBox.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
interface ListDataSource {
String getText(int index);
Object get(int index);
int count();
boolean selected(int index);
}
class MissingListDataSource implements ListDataSource {
String msg;
MissingListDataSource(String msg_) { msg = msg_; }
String getText(int index) { return msg; }
Object get(int index) { return null; }
int count() { return 1; }
boolean selected(int index) { return false; }
}
class ArrowButton extends Button {
ListBox myList;
boolean isUp;
String myElement;
ArrowButton(float x_, float y_, float w_, float h_, Object element, PImage theImage, boolean isDirectional, ListBox theList)
{
super(x_,y_,w_,h_, element,theImage,isDirectional);
myList = theList;
myElement = element.toString();
}
boolean contentClicked(float lx, float ly)
{
if(hasDirection){
tint(0, 130,109,200);
image(myImage,0,0);
}
if (myElement == "uparrow")
myList.scrollTo(myList.myListCounter - 1);
else if (myElement == "downarrow")
myList.scrollTo(myList.myListCounter + 1);
println(myList.myListCounter);
return true;
}
}
class ListBox extends View{
final int rowHeight = 20;
final int barSize = 14;
ListDataSource data;
color myColor = color(255,255,255);
int myListCounter = 0;
ListBox(float x_, float y_, float w_, float h_, ListDataSource data_)
{
super(x_,y_,w_,h_);
data = data_;
PImage uparrow = loadImage("uparrow.png");
PImage downarrow = loadImage("downarrow.png");
subviews.add(new ArrowButton(w-barSize, 0, barSize, barSize, "uparrow", uparrow, true, this));
subviews.add(new ArrowButton(w-barSize, h-barSize, barSize, barSize, "downarrow", downarrow, true, this));
subviews.add(new VBar(w-barSize-1, barSize, barSize+1, h-barSize*2, this));
}
int maxScroll()
{
return data.count() - int(h/rowHeight);
}
void scrollTo(int index)
{
myListCounter = min(max(index, 0), maxScroll());
}
void drawContent()
{
fill(myColor);
rect(0,0,w,h);
fill(0);
noStroke();
for(int i = myListCounter; i < myListCounter+(h/rowHeight) && i < data.count(); i++) {
float rowy = (i-myListCounter)*rowHeight;
if (data.selected(i)) {
fill(shipRed);
rect(0, rowy, w, rowHeight);
fill(255);
} else {
fill(0);
}
text(data.getText(i), 8, rowy);
}
}
boolean contentClicked(float lx, float ly)
{
int index = int(ly/rowHeight) + myListCounter;
buttonClicked(data.get(index));
return true;
}
}