allpy
changeset 13:e3ba4c63e622
In sandbox: Some not-really-working stuff (on topic of text rendering)
author | Danya Alexeyevsky <dendik@kodomo.fbb.msu.ru> |
---|---|
date | Thu, 10 Jun 2010 20:28:29 +0400 |
parents | bd3d695f9906 |
children | 7a770b1abe23 |
files | sandbox/bufferedcanvas.py sandbox/gl.py sandbox/wx-dc.py |
diffstat | 3 files changed, 240 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/sandbox/bufferedcanvas.py Thu Jun 10 20:28:29 2010 +0400 1.3 @@ -0,0 +1,142 @@ 1.4 +""" 1.5 +BufferedCanvas -- Double-buffered, flicker-free canvas widget 1.6 +Copyright (C) 2005, 2006 Daniel Keep 1.7 + 1.8 +To use this widget, just override or replace the draw method. 1.9 +This will be called whenever the widget size changes, or when 1.10 +the update method is explicitly called. 1.11 + 1.12 +Please submit any improvements/bugfixes/ideas to the following 1.13 +url: 1.14 + 1.15 + http://wiki.wxpython.org/index.cgi/BufferedCanvas 1.16 + 1.17 +2006-04-29: Added bugfix for a crash on Mac provided by Marc Jans. 1.18 +""" 1.19 + 1.20 +# Hint: try removing '.sp4msux0rz' 1.21 +__author__ = 'Daniel Keep <daniel.keep.sp4msux0rz@gmail.com>' 1.22 + 1.23 +__license__ = """ 1.24 +This library is free software; you can redistribute it and/or 1.25 +modify it under the terms of the GNU Lesser General Public License as 1.26 +published by the Free Software Foundation; either version 2.1 of the 1.27 +License, or (at your option) any later version. 1.28 + 1.29 +As a special exception, the copyright holders of this library 1.30 +hereby recind Section 3 of the GNU Lesser General Public License. This 1.31 +means that you MAY NOT apply the terms of the ordinary GNU General 1.32 +Public License instead of this License to any given copy of the 1.33 +Library. This has been done to prevent users of the Library from being 1.34 +denied access or the ability to use future improvements. 1.35 + 1.36 +This library is distributed in the hope that it will be useful, but 1.37 +WITHOUT ANY WARRANTY; without even the implied warranty of 1.38 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 1.39 +General Public License for more details. 1.40 + 1.41 +You should have received a copy of the GNU Lesser General Public 1.42 +License along with this library; if not, write to the Free Software 1.43 +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 1.44 +""" 1.45 + 1.46 +__all__ = ['BufferedCanvas'] 1.47 + 1.48 +import wx 1.49 +import wx.lib.scrolledpanel as sp 1.50 + 1.51 +class BufferedCanvas(sp.ScrolledPanel): 1.52 + """ 1.53 + Implements a double-buffered, flicker-free canvas widget. 1.54 + 1.55 + Standard usage is to subclass this class, and override the 1.56 + draw method. The draw method is passed a device context, which 1.57 + should be used to do your drawing. 1.58 + 1.59 + Also, you should NOT call dc.BeginDrawing() and dc.EndDrawing() -- 1.60 + these methods are automatically called for you, although you still 1.61 + need to manually clear the device context. 1.62 + 1.63 + If you want to force a redraw (for whatever reason), you should 1.64 + call the update method. This is because the draw method is never 1.65 + called as a result of an EVT_PAINT event. 1.66 + """ 1.67 + 1.68 + # These are our two buffers. Just be aware that when the buffers 1.69 + # are flipped, the REFERENCES are swapped. So I wouldn't want to 1.70 + # try holding onto explicit references to one or the other ;) 1.71 + buffer = None 1.72 + backbuffer = None 1.73 + 1.74 + def __init__(self, 1.75 + parent, 1.76 + ID=-1, 1.77 + pos=wx.DefaultPosition, 1.78 + size=wx.DefaultSize, 1.79 + style=wx.NO_FULL_REPAINT_ON_RESIZE): 1.80 + sp.ScrolledPanel.__init__(self,parent,ID,pos,size,style) 1.81 + 1.82 + # Bind events 1.83 + self.Bind(wx.EVT_PAINT, self.onPaint) 1.84 + self.Bind(wx.EVT_SIZE, self.onSize) 1.85 + 1.86 + # Disable background erasing (flicker-licious) 1.87 + def disable_event(*pargs,**kwargs): 1.88 + pass # the sauce, please 1.89 + self.Bind(wx.EVT_ERASE_BACKGROUND, disable_event) 1.90 + 1.91 + # Ensure that the buffers are setup correctly 1.92 + self.onSize(None) 1.93 + 1.94 + ## 1.95 + ## General methods 1.96 + ## 1.97 + 1.98 + def draw(self,dc): 1.99 + """ 1.100 + Stub: called when the canvas needs to be re-drawn. 1.101 + """ 1.102 + pass 1.103 + 1.104 + 1.105 + def flip(self): 1.106 + """ 1.107 + Flips the front and back buffers. 1.108 + """ 1.109 + self.buffer,self.backbuffer = self.backbuffer,self.buffer 1.110 + self.Refresh() 1.111 + 1.112 + 1.113 + def update(self): 1.114 + """ 1.115 + Causes the canvas to be updated. 1.116 + """ 1.117 + dc = wx.MemoryDC() 1.118 + dc.SelectObject(self.backbuffer) 1.119 + dc.BeginDrawing() 1.120 + self.draw(dc) 1.121 + dc.EndDrawing() 1.122 + self.flip() 1.123 + 1.124 + ## 1.125 + ## Event handlers 1.126 + ## 1.127 + 1.128 + def onPaint(self, event): 1.129 + # Blit the front buffer to the screen 1.130 + dc = wx.BufferedPaintDC(self, self.buffer) 1.131 + 1.132 + 1.133 + def onSize(self, event): 1.134 + # Here we need to create a new off-screen buffer to hold 1.135 + # the in-progress drawings on. 1.136 + width,height = self.GetClientSizeTuple() 1.137 + if width == 0: 1.138 + width = 1 1.139 + if height == 0: 1.140 + height = 1 1.141 + self.buffer = wx.EmptyBitmap(width,height) 1.142 + self.backbuffer = wx.EmptyBitmap(width,height) 1.143 + 1.144 + # Now update the screen 1.145 + self.update()
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/sandbox/gl.py Thu Jun 10 20:28:29 2010 +0400 2.3 @@ -0,0 +1,46 @@ 2.4 +#!/usr/bin/python 2.5 +# http://disruption.ca/gutil/example3/example3a.html 2.6 +# vim: set noet: 2.7 + 2.8 +import gutil 2.9 +import pygame 2.10 +from pygame.locals import * 2.11 +from OpenGL.GL import * 2.12 + 2.13 +from Image import Image 2.14 + 2.15 + 2.16 +def main(): 2.17 + pygame.init() 2.18 + gutil.initializeDisplay(800, 600) 2.19 + 2.20 + done = False 2.21 + 2.22 + cow = Image('cow') 2.23 + alien = Image('alien') 2.24 + 2.25 + white = (255,255,255,255) 2.26 + stringTex, w, h, tw, th = gutil.loadText("Cow!", color=white) 2.27 + stringDL = gutil.createTexDL(stringTex, tw, th) 2.28 + 2.29 + while not done: 2.30 + glClear(GL_COLOR_BUFFER_BIT) 2.31 + glLoadIdentity() 2.32 + glColor4f(1.0,1.0,1.0,1.0) 2.33 + glTranslatef(100, 400, 0) 2.34 + glCallList(stringDL) 2.35 + 2.36 + cow.draw(pos=(100,100),width=128,height=128) 2.37 + alien.draw(pos=(400, 400),rotation=-15,color=(.9,.3,.2,1)) 2.38 + 2.39 + 2.40 + pygame.display.flip() 2.41 + 2.42 + eventlist = pygame.event.get() 2.43 + for event in eventlist: 2.44 + if event.type == QUIT \ 2.45 + or event.type == KEYDOWN and event.key == K_ESCAPE: 2.46 + done = True 2.47 + 2.48 +if __name__ == '__main__': 2.49 + main()
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/sandbox/wx-dc.py Thu Jun 10 20:28:29 2010 +0400 3.3 @@ -0,0 +1,52 @@ 3.4 +#!/usr/bin/python 3.5 + 3.6 +import wx 3.7 +import bufferedcanvas 3.8 +import common 3.9 + 3.10 +class Text(bufferedcanvas.BufferedCanvas): 3.11 + 3.12 + def __init__(self, seqs, *args, **kw): 3.13 + self.seqs = seqs 3.14 + self.font = wx.FFont(12, wx.FONTFAMILY_MODERN) 3.15 + bufferedcanvas.BufferedCanvas.__init__(self, *args, **kw) 3.16 + 3.17 + dc = wx.ClientDC(self) 3.18 + dc.SetFont(self.font) 3.19 + w = (len(seqs[0][1])+1) * dc.GetCharWidth() 3.20 + h = (len(seqs)+1) * (dc.GetCharHeight() + 1) 3.21 + self.size = w, h 3.22 + self.SetVirtualSize(self.size) 3.23 + 3.24 + self.Bind(wx.EVT_SCROLL, self.onPaint) 3.25 + 3.26 + self.SetupScrolling() 3.27 + 3.28 + def draw(self, dc): 3.29 + dc.SetBackground(wx.Brush("White")) 3.30 + dc.Clear() 3.31 + dc.SetFont(self.font) 3.32 + 3.33 + w = dc.GetCharWidth() 3.34 + h = dc.GetCharHeight() 3.35 + for i, (name, body, ids, colors) in enumerate(self.seqs): 3.36 + for j in xrange(len(body)): 3.37 + x = j * w 3.38 + y = i * (h + 1) 3.39 + dc.SetBrush(wx.Brush(colors[j])) 3.40 + dc.SetPen(wx.Pen(colors[j])) 3.41 + dc.DrawRectangle(x, y, x + w, y + h) 3.42 + dc.SetPen(wx.Pen("Black")) 3.43 + dc.DrawText(body[j], x, y) 3.44 + 3.45 + if hasattr(self, 'size'): 3.46 + self.SetVirtualSize(self.size) 3.47 + 3.48 +seqs = common.autoload('data/small.fasta') 3.49 + 3.50 +app = wx.App(False) 3.51 +top = wx.Frame(None, title='Example', size=(500, 500)) 3.52 +text = Text(seqs, top) 3.53 +top.Show(True) 3.54 + 3.55 +app.MainLoop()