This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
/
ICM_Help_Week3_Slider.pde
68 lines (50 loc) · 1.78 KB
/
ICM_Help_Week3_Slider.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
//Center of the circle (aka Slider Button)
float circleX, circleY;
// Width and Height of the Circle as represented by Diameter
float diameter = 50;
float radius = diameter/2;
// Slider attributes
int sliderY, sliderHeight, sliderLeft, sliderRight, sliderWidth;
void setup() {
size(400, 400);
sliderY = 50;
sliderHeight = 10;
circleX = width/2;
circleY = sliderY + sliderHeight/2;
// Start the slider 50 pixels in from the left-hand side
// End the slider 50 pixels in from the right-hand side
sliderLeft = 50;
sliderWidth = width - 2*sliderLeft;
sliderRight = sliderLeft + sliderWidth;
}
void draw() {
background(255);
// Draw the line of the slider
fill(200, 200, 200, 255);
rect(50, sliderY, sliderWidth, sliderHeight);
stroke(0);
fill(255, 255, 255, 0);
// Make the slider button's x-location follow the mouse
circleX = mouseX;
// Constrain the slider button so it doesn't fall off the ends of the slider
// Take the width of the slider button into consideration
float leftCircleX = sliderLeft + radius;
float rightCircleX = sliderRight - radius;
// Constrain the X-Position of the slider button
circleX = constrain(circleX, leftCircleX, rightCircleX);
//Draw the slider button
ellipse(circleX, circleY, diameter, diameter);
line(circleX, circleY-radius, circleX, circleY+radius);
line(circleX-radius, circleY, circleX+radius, circleY);
ellipse(circleX, circleY, radius/2, radius/2);
}
// Behind the scenes, this is what the constrain function is doing
float constrainBehindTheScenes(float whatamIconstraining, float bottomEnd, float topEnd) {
if(whatamIconstraining < bottomEnd) {
whatamIconstraining = bottomEnd;
}
if (whatamIconstraining > topEnd) {
whatamIconstraining = topEnd;
}
return whatamIconstraining;
}