def _text_(self, text, offs, stroke=False, color=None): ''' ''' if stroke: font = GLUT_STROKE_ROMAN glutStrokeWidth(font, 50) for c in text: glutStrokeCharacter(font, ord(c)) else: glDisable(GL_LIGHTING) co = color if (color and (isinstance(color, tuple) or isinstance(color, list))) else (0, 1, 0) glColor3f(*co) font = GLUT_BITMAP_HELVETICA_18 glRasterPos2f(*offs) for c in text: glutBitmapCharacter(font, ord(c)) glEnable(GL_LIGHTING)
def paintGL(self): if self.opengl_data is None: glClear(GL.GL_COLOR_BUFFER_BIT) else: if self.opengl_resize and self.opengl_keep_aspect: x_scale = self.width() / float(self.opengl_width) y_scale = self.height() / float(self.opengl_height) scale = min(x_scale, y_scale) if x_scale > y_scale: glRasterPos2f(-self.opengl_width * scale / self.width(), -1) else: glRasterPos2f(-1, -self.opengl_height * scale / self.height()) else: glRasterPos2f(-1, -1) glDrawPixels(self.opengl_width, self.opengl_height, self.opengl_format, self.opengl_dtype, self.opengl_data)
def draw(self): """ Draw this menu on the screen. """ from OpenGL.GL import glRasterPos2f from OpenGL.GLUT import glutBitmapCharacter, GLUT_BITMAP_HELVETICA_18 view = glGetInteger(GL_VIEWPORT) # Switch to an orthogonal projection for drawing the menu. glDisable(GL_LIGHTING) glDisable(GL_DEPTH_TEST) glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() # Height and width of the viewport. vheight = view[3] - view[1] vwidth = view[2] - view[0] right = 100.0 top = float(vheight) / float(vwidth) * right glOrtho(0.0, right, 0.0, top, 0.0, 1.0) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() gluLookAt(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) # glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glEnable(GL_BLEND) glEnable(GL_LINE_SMOOTH) glEnable(GL_POLYGON_SMOOTH) # # Draw the background box # nopts = len(self.opts) # Constants used to size the menu elements. ratio = right / vwidth charRise = 22 * ratio charFall = 8 * ratio charWidth = 12 * ratio vmargin = 10 * ratio hmargin = 20 * ratio # itemHeight = charRise + charFall + vmargin if self.opts: # Include the length of the title in maxChars so that # the menu is never narrower than the title. maxChars = max([len(s) for s in self.opts + (self.title,)]) totalHeight = itemHeight * len(self.opts) + vmargin else: maxChars = len(self.title) totalHeight = itemHeight + vmargin width = charWidth * maxChars + hmargin * 2.0 titleWidth = charWidth * len(self.title) + hmargin * 2.0 glColor4f(*self.bgColor) glPushMatrix() # Translate into the center of the display. glTranslatef(50.0, top / 2.0, 0.0) # Now translate this whole figure so that it's centered. glTranslatef(width / -2.0, totalHeight / -2.0, 0.0) glBegin(GL_QUADS) glVertex3f(0.0, 0.0, 0.0) glVertex3f(width, 0.0, 0.0) glVertex3f(width, totalHeight, 0.0) glVertex3f(0.0, totalHeight, 0.0) glEnd() glPushMatrix() glColor4f(*self.fgColor) glTranslatef(0.0, totalHeight, 0.0) # Draw the background for the title. glBegin(GL_QUADS) glVertex3f(0.0, 0.0, 0.0) glVertex3f(titleWidth, 0.0, 0.0) glVertex3f(titleWidth, itemHeight, 0.0) glVertex3f(0.0, itemHeight, 0.0) glEnd() glPopMatrix() # # Draw the selection frame for the currently selected item # glColor4f(*self.selColor) # push is 1/2 the linewidth glPushMatrix() glTranslatef(0.0, itemHeight * (nopts - self.selection - 1) + vmargin / 2.0, 0.0) glBegin(GL_QUADS) glVertex3f(hmargin / 4.0, vmargin / 4.0, 0.0) glVertex3f(width - hmargin / 4.0, vmargin / 4.0, 0.0) glVertex3f(width - hmargin / 4.0, itemHeight - vmargin / 4.0, 0.0) glVertex3f(hmargin / 4.0, itemHeight - vmargin / 4.0, 0.0) glEnd() glPopMatrix() # # Draw the menu items # # Translate into position for the last item. glTranslatef(hmargin, charFall + vmargin, 0.0) # Draw items, moving up as we go for i in range(nopts - 1, -1, -1): glPushMatrix() glColor4f(*self.fgColor) glRasterPos2f(0, 0) for c in self.opts[i]: glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, ord(c)) glPopMatrix() glTranslatef(0.0, itemHeight, 0.0) # # Draw the menu title. # glTranslatef(0.0, vmargin / 2.0, 0.0) glColor4f(*self.bgColor) glRasterPos2f(0, 0) for c in self.title: glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, ord(c)) glPopMatrix() # # end draw # glPopMatrix() glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glEnable(GL_LIGHTING) glEnable(GL_DEPTH_TEST)