00001 import pygame
00002 import pygame.locals
00003 import numpy as np
00004 import time
00005 import copy
00006
00007
00008 class Slider(object):
00009
00010
00011 def __init__(self, pos, value=0):
00012 self.pos = pos
00013 self.size = (275,15)
00014
00015 self.bar = pygame.Surface((275, 15))
00016 self.bar.fill((200, 200, 200))
00017 self.slider = pygame.Surface((20, 15))
00018 self.slider.fill((230, 230, 230))
00019 pygame.draw.rect(self.bar, (0, 0, 0), (0, 0, 275, 15), 2)
00020 pygame.draw.rect(self.slider, (0, 0, 0), (-1, -1, 20, 15), 2)
00021 self.slider.set_at((19, 14), (0, 0, 0))
00022 self.brect = self.bar.get_rect(topleft = pos)
00023 self.srect = self.slider.get_rect(topleft = pos)
00024 self.srect.left = value+pos[0]
00025 self.clicked = False
00026 self.value = value
00027 self.font_size = 15
00028 self.font = pygame.font.SysFont("Times New Roman", self.font_size)
00029 self.text = ''
00030
00031 def set_text(self, text):
00032 ''' set the text to be displayed below the slider
00033 '''
00034 self.text = text
00035
00036
00037 def update(self):
00038 mousebutton = pygame.mouse.get_pressed()
00039 cursor = pygame.locals.Rect(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1], 1, 1)
00040 if cursor.colliderect(self.brect):
00041 if mousebutton[0]:
00042 self.clicked = True
00043 else:
00044 self.clicked = False
00045 if not mousebutton[0]:
00046 self.clicked = False
00047 if self.clicked:
00048 self.srect.center = cursor.center
00049 self.srect.clamp_ip(self.brect)
00050 self.value = self.srect.left - self.brect.left
00051
00052
00053 def render(self, surface):
00054 surface.blit(self.bar, self.brect)
00055 surface.blit(self.slider, self.srect)
00056 ren = self.font.render(self.text,1,(0,0,0))
00057 surface.blit(ren, (self.pos[0], self.pos[1]+self.font_size+2))
00058
00059