initial work on OpenGL

This commit is contained in:
Skyler Lehmkuhl 2012-03-06 11:13:01 -05:00
parent 3ff0c79189
commit a30d6e1170
1 changed files with 113 additions and 87 deletions

190
svlgui.py
View File

@ -40,6 +40,9 @@ FRAMERATE=50
#Width and height are the width and height of the document #Width and height are the width and height of the document
WIDTH, HEIGHT = 500, 500 WIDTH, HEIGHT = 500, 500
#Whether we are using OpenGL for rendering.
USING_GL = True
#Object which has the keyboard focus. #Object which has the keyboard focus.
FOCUS = None FOCUS = None
@ -166,6 +169,9 @@ if sys.platform=="linux2":
from GUI.StdMenus import basic_menus, file_cmds, print_cmds from GUI.StdMenus import basic_menus, file_cmds, print_cmds
from GUI.StdButtons import DefaultButton, CancelButton from GUI.StdButtons import DefaultButton, CancelButton
from GUI.Files import FileType from GUI.Files import FileType
if USING_GL:
from OpenGL.GL import *
from GUI import GL
from GUI.Geometry import offset_rect, rect_sized from GUI.Geometry import offset_rect, rect_sized
#If we can import this, we are in the install directory. Mangle media paths accordingly. #If we can import this, we are in the install directory. Mangle media paths accordingly.
@ -789,59 +795,76 @@ class Canvas(Widget):
self.canvas.connect("button-release-event", onMouseUp) self.canvas.connect("button-release-event", onMouseUp)
self.canvas.connect("motion_notify_event", onMouseMove) self.canvas.connect("motion_notify_event", onMouseMove)
elif SYSTEM=="osx": elif SYSTEM=="osx":
class OSXCanvas (ScrollableView): if USING_GL:
def draw(self, canvas, update_rect): class OSXCanvas(GL.GLView):
canvas.erase_rect(update_rect) def init_context(self):
for i in self.objs: glViewport(0, 0, width, height);
i.draw(canvas) def init_projection(self):
glEnable( GL_TEXTURE_2D );
def mouse_down(self, event): glEnable (GL_BLEND);
self.become_target() glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
x, y = event.position def render(self):
try: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluOrtho2D(0, width, 0, height); #TODO: width, height
glMatrixMode(GL_MODELVIEW);
for i in self.objs: for i in self.objs:
i._onMouseDown(x, y) i.draw(None)
except ObjectDeletedError: self.canvas = OSXCanvas(double_buffer=True,multisample=True,samples_per_pixel=8)
return else:
self.invalidate_rect([0,0,self.extent[0],self.extent[1]]) class OSXCanvas (ScrollableView):
def draw(self, canvas, update_rect):
canvas.erase_rect(update_rect)
for i in self.objs:
i.draw(canvas)
def mouse_drag(self, event): def mouse_down(self, event):
x, y = event.position self.become_target()
for i in self.objs: x, y = event.position
i._onMouseDrag(x, y) try:
self.invalidate_rect([0,0,self.extent[0],self.extent[1]]) for i in self.objs:
i._onMouseDown(x, y)
except ObjectDeletedError:
return
self.invalidate_rect([0,0,self.extent[0],self.extent[1]])
def mouse_move(self, event): def mouse_drag(self, event):
x, y = event.position x, y = event.position
for i in self.objs: for i in self.objs:
i._onMouseMove(x, y) i._onMouseDrag(x, y)
self.invalidate_rect([0,0,self.extent[0],self.extent[1]]) self.invalidate_rect([0,0,self.extent[0],self.extent[1]])
def mouse_up(self, event): def mouse_move(self, event):
x, y = event.position x, y = event.position
for i in self.objs: for i in self.objs:
i._onMouseUp(x, y) i._onMouseMove(x, y)
self.invalidate_rect([0,0,self.extent[0],self.extent[1]]) self.invalidate_rect([0,0,self.extent[0],self.extent[1]])
def key_down(self, event): def mouse_up(self, event):
keydict = {127:"backspace",63272:"delete",63232:"up_arrow",63233:"down_arrow", x, y = event.position
63235:"right_arrow",63234:"left_arrow",13:"enter",9:"tab", for i in self.objs:
63236:"F1",63237:"F2",63238:"F3",63239:"F4",63240:"F5", i._onMouseUp(x, y)
63241:"F6",63242:"F7",63243:"F8",27:"escape"} self.invalidate_rect([0,0,self.extent[0],self.extent[1]])
if not event.unichars=='':
if ord(event.unichars) in keydict: def key_down(self, event):
key = keydict[ord(event.unichars)] keydict = {127:"backspace",63272:"delete",63232:"up_arrow",63233:"down_arrow",
63235:"right_arrow",63234:"left_arrow",13:"enter",9:"tab",
63236:"F1",63237:"F2",63238:"F3",63239:"F4",63240:"F5",
63241:"F6",63242:"F7",63243:"F8",27:"escape"}
if not event.unichars=='':
if ord(event.unichars) in keydict:
key = keydict[ord(event.unichars)]
else:
key = event.unichars
else: else:
key = event.unichars key = event.key.upper()
else: for i in self.objs:
key = event.key.upper() i._onKeyDown(key)
for i in self.objs: self.invalidate_rect([0,0,self.extent[0],self.extent[1]])
i._onKeyDown(key)
self.invalidate_rect([0,0,self.extent[0],self.extent[1]])
def key_up(self, event): def key_up(self, event):
pass pass
self.canvas = OSXCanvas(extent = (width, height), scrolling = 'hv') self.canvas = OSXCanvas(extent = (width, height), scrolling = 'hv')
self.canvas.objs = self.objs self.canvas.objs = self.objs
elif SYSTEM=="html": elif SYSTEM=="html":
global ids global ids
@ -1161,43 +1184,46 @@ class Shape (object):
tb+="cr.stroke()\n" tb+="cr.stroke()\n"
tb+="cr.restore()\n" tb+="cr.restore()\n"
elif SYSTEM=="osx": elif SYSTEM=="osx":
cr.gsave() if USING_GL:
if sep=="\\": pass
# Very ugly hack for Windows. :( else:
# Windows doesn't respect coordinate transformations cr.gsave()
# with respect to translation, so we have to do this if sep=="\\":
# bit ourselves. # Very ugly hack for Windows. :(
# Windows doesn't respect coordinate transformations
# with respect to translation, so we have to do this
# bit ourselves.
# Rotation in radians # Rotation in radians
radrot = parent.group.rotation*math.pi/180 radrot = parent.group.rotation*math.pi/180
# Coordinate transform: multiplication by a rotation matrix # Coordinate transform: multiplication by a rotation matrix
cr.translate(self.x*math.cos(radrot)-self.y*math.sin(radrot), self.x*math.sin(radrot)+self.y*math.cos(radrot)) cr.translate(self.x*math.cos(radrot)-self.y*math.sin(radrot), self.x*math.sin(radrot)+self.y*math.cos(radrot))
else: else:
cr.translate(self.x,self.y) cr.translate(self.x,self.y)
cr.rotate(self.rotation) cr.rotate(self.rotation)
cr.scale(self.xscale*1.0, self.yscale*1.0) cr.scale(self.xscale*1.0, self.yscale*1.0)
cr.newpath() cr.newpath()
cr.pencolor = self.linecolor.pygui cr.pencolor = self.linecolor.pygui
cr.fillcolor = self.fillcolor.pygui cr.fillcolor = self.fillcolor.pygui
for i in self.shapedata: for i in self.shapedata:
if i[0]=="M": if i[0]=="M":
point = (i[1], i[2]) point = (i[1], i[2])
cr.moveto(point[0],point[1]) cr.moveto(point[0],point[1])
elif i[0]=="L": elif i[0]=="L":
point = (i[1], i[2]) point = (i[1], i[2])
cr.lineto(point[0],point[1]) cr.lineto(point[0],point[1])
elif i[0]=="C": elif i[0]=="C":
pointa = (i[1], i[2]) pointa = (i[1], i[2])
pointb = (i[3], i[4]) pointb = (i[3], i[4])
pointc = (i[5], i[6]) pointc = (i[5], i[6])
### Mac OSX needs custom PyGUI for this to work ### ### Mac OSX needs custom PyGUI for this to work ###
cr.curveto((pointa[0],pointa[1]),(pointb[0],pointb[1]),(pointc[0],pointc[1])) cr.curveto((pointa[0],pointa[1]),(pointb[0],pointb[1]),(pointc[0],pointc[1]))
if self.filled: if self.filled:
cr.closepath() cr.closepath()
cr.fill_stroke() cr.fill_stroke()
else: else:
cr.stroke() cr.stroke()
cr.grestore() cr.grestore()
elif SYSTEM=="html": elif SYSTEM=="html":
tb = "" tb = ""
tb+="cr.save()\n" tb+="cr.save()\n"