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

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


Author: arigo
Date: Tue Jan 20 23:26:15 2004
New Revision: 2870

Modified:
   shpy/trunk/dist/shpy/ui_pygame.py
Log:
Scrolling.


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 23:26:15 2004
@@ -62,25 +62,31 @@
         self.changed[self.root.users] = 1
 
     def repaint(self):
-        self.screen.fill((255,255,255))
-        self.bring_lastline_in_view_of_cursor()
-        self.line2ypos = {}
-        
-        index = self.lastline_cell.lines.index(self.lastline)
-        lines = self.lastline_cell.lines[:index+1]
-        ypos = self.drawlinesbackwards(lines)
-        if ypos > 0:
-            index = self.celllist.index(self.lastline_cell)
-            remainingcells = self.celllist[:index]
-            while remainingcells and ypos > 0:
-                cell = remainingcells.pop()
-                ypos = self.drawspacing(ypos)
-                ypos = self.drawlinesbackwards(cell.lines, ypos)
-        self.drawcursors()
-        pygame.display.flip()
+        while 1:
+            self.screen.fill((255,255,255))
+            self.line2ypos = {}
+
+            index = self.lastline_cell.lines.index(self.lastline)
+            lines = self.lastline_cell.lines[:index+1]
+            ypos = self.drawlinesbackwards(lines)
+            if ypos > 0:
+                index = self.celllist.index(self.lastline_cell)
+                remainingcells = self.celllist[:index]
+                while remainingcells and ypos > 0:
+                    cell = remainingcells.pop()
+                    ypos = self.drawspacing(ypos)
+                    ypos = self.drawlinesbackwards(cell.lines, ypos)
+            cursor_in_view = self.drawcursors()
+            pygame.display.flip()
+            if cursor_in_view:
+                break
+            self.scroll_a_bit_towards_cursor()
 
     def drawcursors(self):
-        for cursor in self.root.users.__dict__.values():
+        cursors = self.root.users.__dict__.values()
+        assert self.cursor in cursors
+        cursor_in_view = False
+        for cursor in cursors:
             x = cursor.x
             line = cursor.line
             try:
@@ -93,6 +99,9 @@
             charimage = self.font.render(char, 1, (255,255,255), cursor.color)
             startoflineimagesize = self.font.size(line.content[:x])
             self.screen.blit(charimage, (startoflineimagesize[0], ypos))
+            if cursor is self.cursor and ypos >= 0:
+                cursor_in_view = True
+        return cursor_in_view
 
     def drawspacing(self, ypos):
         self.screen.fill((160, 160, 160), Rect(0, ypos-2, 100, 1))
@@ -110,9 +119,42 @@
             self.line2ypos[line] = ypos
         return ypos
                 
-    def bring_lastline_in_view_of_cursor(self):
-        pass
-    
+    def scroll_a_bit_towards_cursor(self):
+        if self.cursor.cell is self.lastline_cell:
+            cursorindex = self.cursor.cell.lines.index(self.cursor.line)
+            lastlineindex = self.lastline_cell.lines.index(self.lastline)
+        else:
+            cursorindex = self.celllist.index(self.cursor.cell)
+            lastlineindex = self.celllist.index(self.lastline_cell)
+        if cursorindex < lastlineindex:
+            self.lastline, self.lastline_cell = self.previousline(self.lastline, self.lastline_cell)
+        else:
+            self.lastline, self.lastline_cell = self.nextline(self.lastline, self.lastline_cell)
+
+    def previousline(self, line, cell):
+        index = cell.lines.index(line)
+        if index > 0:
+            return cell.lines[index-1], cell
+        else:
+            # XXX this changes if we have output cells ...
+            index = self.celllist.index(cell)
+            if index > 0:
+                cell = self.celllist[index-1]
+                return cell.lines[-1], cell
+        return line, cell
+
+    def nextline(self, line, cell):
+        index = cell.lines.index(line)
+        if index + 1 < len(cell.lines):
+            return cell.lines[index+1], cell
+        else:
+            # XXX this changes if we have output cells ...
+            index = self.celllist.index(cell)
+            if index + 1 < len(self.celllist):
+                cell = self.celllist[index+1]
+                return cell.lines[0], cell
+        return line, cell
+
 ##    def drawcell(self, screen, cell):
 ##        self.scroll_cursor_into_view(screen)
 ##        start = -self.vscroll//self.fontheight
@@ -169,26 +211,10 @@
         self.cursor.x = len(self.cursor.line.content)
         
     def K_UP(self, event):
-        index = self.cursor.cell.lines.index(self.cursor.line)
-        if index > 0:
-            self.cursor.line = self.cursor.cell.lines[index-1]
-        else:
-            # XXX this changes if we have output cells ...
-            index = self.celllist.index(self.cursor.cell)
-            if index > 0:
-                self.cursor.cell = self.celllist[index-1]
-                self.cursor.line = self.cursor.cell.lines[-1]
+        self.cursor.line, self.cursor.cell = self.previousline(self.cursor.line, self.cursor.cell)
                 
     def K_DOWN(self, event):
-        index = self.cursor.cell.lines.index(self.cursor.line)
-        if index + 1 < len(self.cursor.cell.lines):
-            self.cursor.line = self.cursor.cell.lines[index+1]
-        else:
-            # XXX this changes if we have output cells ...
-            index = self.celllist.index(self.cursor.cell)
-            if index + 1 < len(self.celllist):
-                self.cursor.cell = self.celllist[index+1]
-                self.cursor.line = self.cursor.cell.lines[0]
+        self.cursor.line, self.cursor.cell = self.nextline(self.cursor.line, self.cursor.cell)
 
     def K_LEFT(self, event):
         if self.cursor.x > 0: