-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathHSlider.pde
119 lines (104 loc) · 2.8 KB
/
HSlider.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
int spaceBtwLines = 10;
int markSize = 10;
color sliderColor = 150;
color activeSliderColor = 255;
class HSlider extends View {
private float value;
private int index_1;
private int index_2;
private String minLabel;
private String maxLabel;
String[] labels;
private String title;
private boolean movingMin;
private boolean movingMax;
private int charactersToShow;
HSlider(float x_, float y_, float w_, float h_,String[] _labels,String _title,int _chr)
{
super(x_, y_, (int(textWidth(_title)) + (_labels.length*spaceBtwLines)), (textAscent() + textDescent() + markSize+2));
index_1 = 0;
index_2 = _labels.length-1;
labels = _labels;
title = _title;
charactersToShow = _chr;
}
HSlider(float x_, float y_, float w_, float h_,String[] _labels,int _chr)
{
super(x_, y_, (_labels.length*spaceBtwLines), (textAscent() + textDescent() + markSize+2));
index_1 = 0;
index_2 = _labels.length-1;
labels = _labels;
title = "";
charactersToShow = _chr;
}
void drawContent()
{
textAlign(LEFT,CENTER);
textFont(font,normalFontSize);
fill(textColor);
text(title, 0, 0);
textFont(font,smallFontSize);
for (int i = 0; i < labels.length; i++) {
float x = int(textWidth(title)) + i*spaceBtwLines;
if (i == index_1) {
strokeWeight(2);
stroke(activeSliderColor);
line(x, 0, x, markSize);
textAlign(CENTER, TOP);
text(labels[index_1].substring(0,charactersToShow), x, markSize+2);
}
else if (i == index_2) {
strokeWeight(2);
stroke(activeSliderColor);
line(x, 0, x, markSize);
textAlign(CENTER, TOP);
text(labels[index_2].substring(0,charactersToShow), x, markSize+2);
}
else {
strokeWeight(1);
stroke(sliderColor);
line(x, 0, x,markSize-4);
}
}
}
boolean contentPressed(float lx, float ly)
{
value = constrain(int((lx - textWidth(title)) / spaceBtwLines),0,labels.length-1);
if (value == index_1)
{
movingMin = true;
movingMax = false;
}
else if (value == index_2){
movingMax = true;
movingMin = false;
}
else {
movingMin = false;
movingMax = false;
}
return true;
}
boolean contentDragged(float lx, float ly)
{
value = constrain(int((lx - textWidth(title)) / spaceBtwLines),0,labels.length-1);
if (movingMin){
index_1 = int(value);
}else if (movingMax){
index_2 = int(value);
}
return true;
}
int minIndex(){
if (index_1 > index_2){
return index_2;
}
return index_1;
}
int maxIndex(){
if (index_1 > index_2){
return index_1;
}
return index_2;
}
}