-
Notifications
You must be signed in to change notification settings - Fork 48
/
slider.py
42 lines (40 loc) · 1.39 KB
/
slider.py
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
class Slider:
def __init__(self,low,high,default):
'''slider has range from low to high
and is set to default'''
self.low = low
self.high = high
self.val = default
self.clicked = False
self.label = '' #blank label
def position(self,x,y):
'''slider's position on screen'''
self.x = x
self.y = y
#the position of the rect you slide:
self.rectx = self.x + map(self.val,self.low,self.high,0,120)
self.recty = self.y - 10
def value(self):
'''updates the slider and returns value'''
#gray line behind slider
strokeWeight(4)
stroke(200)
line(self.x,self.y,self.x + 120,self.y)
#press mouse to move slider
if mousePressed and dist(mouseX,mouseY,self.rectx,self.recty) < 20:
self.rectx = mouseX
#constrain rectangle
self.rectx = constrain(self.rectx, self.x, self.x + 120)
#draw rectangle
strokeWeight(1)
stroke(0)
fill(255)
rect(self.rectx,self.recty,10,20)
self.val = map(self.rectx,self.x,self.x + 120,self.low,self.high)
#draw label
fill(0)
textSize(10)
text(int(self.val),self.rectx,self.recty + 35)
#text label
text(self.label,self.x + 135,self.y);
return self.val