from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * class button: def __init__(self, state=0): self.state = state self.x = 0 self.y = 0 self.width = 10 self.height = 10 self.label = "" self.margin = 1 def setGeometry(self, x, y, width, height): self.x = x self.y = y self.width = width self.height = height def setLabel(self, label, margin=1): self.label = label self.margin = margin def isOn(self): return self.state def draw(self): if self.state: glColor3f(1, 0, 0) else: glColor3f(0.7, 0.7, 0.7) glRectf(self.x, self.y, self.x+self.width, self.y+self.height) glColor3f(1, 1, 0) glRasterPos2f(self.x+self.margin, self.y+self.margin) for c in self.label: glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, ord(c)) def mouseClick(self, x, y): if (x >= self.x) and (x <= self.x + self.width) and \ (y >= self.y) and (y <= self.y + self.height): self.state = not self.state