[shpy-commit] r2854 - shpy/trunk/dist/shpy

arigo@codespeak.net arigo@codespeak.net
Tue, 20 Jan 2004 15:33:27 +0100 (MET)


Author: arigo
Date: Tue Jan 20 15:33:26 2004
New Revision: 2854

Modified:
   shpy/trunk/dist/shpy/ui_pygame.py
Log:
Inserting text, return and backspace keys.


Modified: shpy/trunk/dist/shpy/ui_pygame.py
==============================================================================
--- shpy/trunk/dist/shpy/ui_pygame.py	(original)
+++ shpy/trunk/dist/shpy/ui_pygame.py	Tue Jan 20 15:33:26 2004
@@ -95,27 +95,67 @@
             self.vscroll -= ypos + charimage.get_size()[1] - screen.get_size()[1]
             self.drawcell(screen, self.cell)
 
-    def K_UP(self):
+    def PRINTABLE_KEY(self, event):
+        assert event.unicode
+        x = self.cursor.x
+        y = self.cursor.y
+        line = self.cell.lines[y]
+        line = line[:x] + event.unicode + line[x:]
+        self.cell.lines[y] = line
+        self.cursor.x += len(event.unicode)
+        self.changed[self.cell] = 1
+        
+    def K_UP(self, event):
         if self.cursor.y > 0:
             self.cursor.y -= 1
 
-    def K_DOWN(self):
+    def K_DOWN(self, event):
         if self.cursor.y < len(self.cell.lines)-1:
             self.cursor.y += 1
 
-    def K_LEFT(self):
+    def K_LEFT(self, event):
         if self.cursor.x > 0:
             self.cursor.x -= 1
 
-    def K_RIGHT(self):
+    def K_RIGHT(self, event):
         line = self.cell.lines[self.cursor.y]
         if self.cursor.x < len(line):
             self.cursor.x += 1
 
+    def K_BACKSPACE(self, event):
+        x = self.cursor.x
+        y = self.cursor.y
+        if x > 0:
+            line = self.cell.lines[y]
+            line = line[:x-1] + line[x:]
+            self.cell.lines[y] = line
+            self.cursor.x -= 1
+        elif y > 0:
+            line = self.cell.lines[y]
+            prevline = self.cell.lines[y-1]
+            self.cell.lines[y-1:y+1] = [prevline + line]
+            self.cursor.y -= 1
+            self.cursor.x = len(prevline)
+        self.changed[self.cell] = 1
+
+    def K_RETURN(self, event):
+        x = self.cursor.x
+        y = self.cursor.y
+        line = self.cell.lines[y]
+        self.cell.lines[y:y+1] = [line[:x], line[x:]]
+        self.cursor.x = 0
+        self.cursor.y += 1
+        self.changed[self.cell] = 1
+
     def run(self):
         repaintdelay = 0
         self.invalid = True
+        self.changed = {}
         while 1:
+            if self.changed:
+                self.notifychanges(*self.changed.keys())
+                self.changed.clear()
+                self.invalid = True
             if self.invalid:
                 if repaintdelay and pygame.event.peek():
                     repaintdelay -= 1   # don't repaint right now, process next event first
@@ -126,15 +166,16 @@
             event = pygame.event.wait()
             print event
             if event.type == KEYDOWN:
-                if event.key not in KEYMAP:
-                    continue
-                keyname = KEYMAP[event.key]
-                for receiver in [self]:
-                    method = getattr(receiver, keyname, None)
+                keynames = []
+                if event.key in KEYMAP:
+                    keynames.append(KEYMAP[event.key])
+                if event.unicode:
+                    keynames.append("PRINTABLE_KEY")
+                for keyname in keynames:
+                    method = getattr(self, keyname, None)
                     if method:
-                        method()
-                        self.notifychanges(self.cursor)
-                        self.invalid = True
+                        method(event)
+                        self.changed[self.cursor] = 1
                         break
             if event.type == QUIT:
                 break