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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#!/usr/bin/env python
'''
scribble.py
This module defines the Scribble class. This
class implements the GLScene interface to render
scribble strokes using OpenGL.
Alif Wahid, <awah005@users.sourceforge.net>
August, 2003.
'''
import gc
import pygtk
pygtk.require('2.0')
from gtk.gtkgl.apputils import *
class Scribble (GLScene,
GLSceneButton,
GLSceneButtonMotion):
''' Implements the GLScene interface to
render a Scribble scene in this case.
'''
def __init__ (self):
GLScene.__init__(self)
# The list of brush strokes to be drawn. The coordinates are
# the centre of each brush stroke drawn along with the thickness,
# in the form of (x,y,thickness).
self.__brushStrokeList = []
# The thickness can be adjusted by the user.
self.thickness = 4
# The background colour is white by default.
self.colourBg = [1.0, 1.0, 1.0, 0.0]
# The foreground colour is black by default.
self.colourFg = [0.0, 0.0, 0.0, 0.0]
def init (self):
# Set the background colour to the GUI colour.
glClearColor(self.colourBg[0], self.colourBg[1], self.colourBg[2], self.colourBg[3])
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
def display (self, width, height):
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# Set the foreground colour.
glColor3f(self.colourFg[0], self.colourFg[1], self.colourFg[2])
# Draw the complete list of brush strokes.
for coord in self.__brushStrokeList:
# Draw a rectangle as the brush stroke.
glRectf(coord[0]+coord[2], coord[1]-coord[2], coord[0]-coord[2], coord[1]+coord[2])
def reshape (self, width, height):
# Handle the OpenGL viewport resizing.
glViewport (0, 0, width, height)
glMatrixMode (GL_PROJECTION)
glLoadIdentity ()
glOrtho (0.0, width, 0.0, height, -1.0, 1.0)
glMatrixMode (GL_MODELVIEW)
glLoadIdentity ()
def button_press (self, width, height, event):
# On left mouse button click, append one brush stroke
# that needs to be drawn.
if event.button == 1:
self.queue_brush_stroke_draw(event.x, height-event.y, self.thickness)
def button_release (self, width, height, event):
pass
def button_motion (self, width, height, event):
# Append subsequent strokes to the list due to drag motion.
if event.state & gtk.gdk.BUTTON1_MASK:
self.queue_brush_stroke_draw(event.x, height-event.y, self.thickness)
# Two extra functions to give a nice API for
# embedding this class in other applications.
def queue_brush_stroke_draw (self, *args):
self.__brushStrokeList.append(args)
self.queue_draw() # Update/render the scene drawable.
def clear_all_brush_strokes (self):
self.__brushStrokeList = None
gc.collect()
self.__brushStrokeList = []
|