[pypy-svn] r7263 - pypy/trunk/src/pypy/translator/tool/pygame

bob at codespeak.net bob at codespeak.net
Tue Nov 16 10:23:54 CET 2004


Author: bob
Date: Tue Nov 16 10:23:54 2004
New Revision: 7263

Modified:
   pypy/trunk/src/pypy/translator/tool/pygame/graphdisplay.py
Log:
beginnings of a new event system and UI for the viewer tool



Modified: pypy/trunk/src/pypy/translator/tool/pygame/graphdisplay.py
==============================================================================
--- pypy/trunk/src/pypy/translator/tool/pygame/graphdisplay.py	(original)
+++ pypy/trunk/src/pypy/translator/tool/pygame/graphdisplay.py	Tue Nov 16 10:23:54 2004
@@ -1,10 +1,29 @@
 from __future__ import generators
 import autopath
-import os, time
+import os, time, sys
 import pygame
 from pygame.locals import *
 from drawgraph import GraphRenderer
 
+
+METAKEYS = dict([
+    (ident[len('KMOD_'):].lower(), getattr(pygame.locals, ident))
+    for ident in dir(pygame.locals) if ident.startswith('KMOD_') and ident != 'KMOD_NONE'
+])
+
+if sys.platform == 'darwin':
+    PMETA = 'lmeta'
+else:
+    PMETA = 'lalt'
+
+METAKEYS['meta'] = METAKEYS[PMETA]
+METAKEYS['shift'] = METAKEYS['lshift']
+
+KEYS = dict([
+    (ident[len('K_'):].lower(), getattr(pygame.locals, ident))
+    for ident in dir(pygame.locals) if ident.startswith('K_')
+])
+
 class Display(object):
     
     def __init__(self, (w,h)=(800,740)):
@@ -14,35 +33,89 @@
     def resize(self, (w,h)):
         self.width = w
         self.height = h
-        self.screen = pygame.display.set_mode((w, h), HWSURFACE|RESIZABLE)
+        self.screen = pygame.display.set_mode((w, h), HWSURFACE|RESIZABLE, 32)
 
 
 class GraphDisplay(Display):
     STATUSBARFONT = os.path.join(autopath.this_dir, 'VeraMoBd.ttf')
     ANIM_STEP = 0.07
 
+    KEYS = {
+        'meta =' : ('zoom', 1),
+        'meta -' : ('zoom', -1),
+        'meta 0' : 'zoom_actual_size',
+        'meta 1' : 'zoom_to_fit',
+        'meta q' : 'quit',
+        'escape' : 'quit',
+        'meta f4' : 'quit',
+        #'meta left' : 'go_back',
+        #'meta right' : 'go_forward',
+        'meta left': 'layout_back',
+        'p' : 'layout_back',
+        'backspace' : 'layout_back',
+        'left' : ('pan', (-1, 0)),
+        'right' : ('pan', (1, 0)),
+        'up' : ('pan', (0, -1)),
+        'down' : ('pan', (0, 1)),
+        'shift left' : ('fast_pan', (-1, 0)),
+        'shift right' : ('fast_pan', (1, 0)),
+        'shift up' : ('fast_pan', (0, -1)),
+        'shift down' : ('fast_pan', (0, 1)),
+    }
+        
+        
     def __init__(self, layout):
         super(GraphDisplay, self).__init__()
         self.font = pygame.font.Font(self.STATUSBARFONT, 16)
         self.viewers_history = []
         self.viewer = None
         self.method_cache = {}
+        self.key_cache = {}
+        self.initialize_keys()
         self.setlayout(layout)
 
+    def initialize_keys(self):
+        for strnames, methodname in self.KEYS.iteritems():
+            names = strnames.split()
+            mod = 0
+            key = 0
+            if not isinstance(methodname, basestring):
+                methodname, args = methodname[0], methodname[1:]
+            else:
+                args = ()
+            for name in names:
+                if name in METAKEYS:
+                    mod |= METAKEYS[name]
+                elif name in KEYS:
+                    key = KEYS[name]
+                else:
+                    assert len(name) == 1
+                    key = ord(name)
+            method = getattr(self, methodname, None)
+            if method is None:
+                print 'Can not implement key mapping %r, %s.%s does not exist' % (
+                        strnames, self.__class__.__name__, methodname)
+            self.key_cache[(key, mod)] = (method, args)
+    
     def setlayout(self, layout):
         if self.viewer:
             self.viewers_history.append(self.viewer)
         self.layout = layout
         self.viewer = GraphRenderer(self.screen, layout)
-        # center and scale to view the whole graph
+        self.zoom_to_fit()
+
+    def zoom_to_fit(self):
+        """
+        center and scale to view the whole graph
+        """
         self.viewer.setoffset((self.viewer.width - self.width) // 2,
                               (self.viewer.height - self.height) // 2)
         f = min(float(self.width-40) / self.viewer.width,
-                float(self.height-40) / self.viewer.height)
-        if f < 1.0:
-            self.viewer.shiftscale(f)
+                float(self.height-40) / self.viewer.height,
+                1.0)
+        self.viewer.shiftscale(f)
         self.updated_viewer()
-
+    
     def updated_viewer(self):
         self.sethighlight()
         self.statusbarinfo = None
@@ -223,10 +296,9 @@
         self.notifymousepos(event.pos)
 
     def process_KeyDown(self, event):
-        if event.key in [K_p, K_LEFT, K_BACKSPACE]:
-            self.layout_back()
-        elif event.key == K_ESCAPE:
-            self.events.insert(0, pygame.event.Event(QUIT))
+        method, args = self.key_cache.get((event.key, event.mod), (None, None))
+        if method is not None:
+            method(*args)
 
     def process_VideoResize(self, event):
         # short-circuit if there are more resize events pending
@@ -236,8 +308,11 @@
         self.must_redraw = True
 
     def process_Quit(self, event):
-        raise StopIteration
+        self.quit()
      
+    def quit(self):
+        raise StopIteration
+    
     def run(self):
         self.dragging = self.click_origin = self.click_time = None
         events = self.events = []



More information about the Pypy-commit mailing list