[shpy-commit] r2841 - in shpy/trunk/dist/shpy: . net
arigo@codespeak.net
arigo@codespeak.net
Tue, 20 Jan 2004 00:28:19 +0100 (MET)
Author: arigo
Date: Tue Jan 20 00:28:17 2004
New Revision: 2841
Modified:
shpy/trunk/dist/shpy/net/gateway.py
shpy/trunk/dist/shpy/net/register.py
shpy/trunk/dist/shpy/ui_pygame.py
Log:
multiple cursors. Don't look too closely at the source, but
feel free to play around chasing everyone else's cursor :-)
Modified: shpy/trunk/dist/shpy/net/gateway.py
==============================================================================
--- shpy/trunk/dist/shpy/net/gateway.py (original)
+++ shpy/trunk/dist/shpy/net/gateway.py Tue Jan 20 00:28:17 2004
@@ -65,7 +65,7 @@
while self.running:
source = self.queue.get()
try:
- print "executing source", source[:10]
+ print "executing source", source
co = dyncode.makecode(source)
exec co in self.gateway.ns
except:
Modified: shpy/trunk/dist/shpy/net/register.py
==============================================================================
--- shpy/trunk/dist/shpy/net/register.py (original)
+++ shpy/trunk/dist/shpy/net/register.py Tue Jan 20 00:28:17 2004
@@ -34,8 +34,9 @@
if not reload:
l.append("""
try:
- import %(modname)s
- except ImportError:
+ import sys
+ %(modname)s = sys.modules[%(modname)r]
+ except KeyError:
""")
l.append("""
import new, sys
@@ -46,6 +47,7 @@
self.exec_remote("".join(l) % locals())
def registerclient(self, reload=False):
+ self.ns['getstructure'] = structure.getstructure
self.import_remote('shared.py', 'shared', reload)
self.exec_remote("shared.registerclient(gateway)")
# import the root
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 00:28:17 2004
@@ -1,7 +1,10 @@
+import autopath
+
import sys
import pygame
from pygame.locals import *
-import net.register
+import shpy.net.register
+from shpy.net.structure import Structure, representstructure, getstructureid
#RESOLUTION = (800, 600)
RESOLUTION = (400, 300)
@@ -14,8 +17,8 @@
class InputCell:
- def __init__(self):
- text = open('ui_pygame.py', 'r').read() # test
+ def __init__(self, cellstructure):
+ self.cellstructure = cellstructure
self.lines = text.split('\n')
assert self.lines
self.cursor = None
@@ -67,23 +70,6 @@
cell.cursor = self
self.x, self.y = position
- def K_UP(self):
- if self.y > 0:
- self.y -= 1
-
- def K_DOWN(self):
- if self.y < len(self.cell.lines)-1:
- self.y += 1
-
- def K_LEFT(self):
- if self.x > 0:
- self.x -= 1
-
- def K_RIGHT(self):
- line = self.cell.lines[self.y]
- if self.x < len(line):
- self.x += 1
-
KEYMAP = {}
for name, value in pygame.locals.__dict__.items():
@@ -100,25 +86,107 @@
## return image
+def getusername():
+ import os
+ try:
+ return os.getlogin()
+ except:
+ import random
+ return 'somebody%d' % random.randrange(0, 100)
+
+def getcolor():
+ username = getusername()
+ try:
+ return {'arigo': (0x60, 0, 0),
+ 'hpk': (0, 0, 0x80),
+ }[username]
+ except KeyError:
+ import random
+ r = random.randrange(0, 0x80)
+ g = random.randrange(0, 0x80)
+ b = random.randrange(0, 0x80)
+ return (r, g, b)
+
+
class Terminal:
def __init__(self, hostport):
ns = {'terminal': self}
- self.servergateway = net.register.ServerGateway(hostport, ns)
+ self.servergateway = shpy.net.register.ServerGateway(hostport, ns)
pygame.init()
self.screen = pygame.display.set_mode(RESOLUTION)
self.font = pygame.font.Font(FONT, HEIGHT)
- self.cell = InputCell()
- self.cursor = Cursor()
- self.cursor.entercell(self.cell, (10, 10))
self.root = self.servergateway.registerclient()
- import pdb; pdb.set_trace()
+ username = getusername()
+ try:
+ self.cursor = getattr(self.root.users, username)
+ except AttributeError:
+ self.cursor = Structure(x=0, y=0, color=getcolor())
+ setattr(self.root.users, username, self.cursor)
+ self.notifychanges(self.cursor, self.root.users)
+ print self.root.users.__dict__
+ self.cell = self.root.cells.list[0]
+ self.vscroll = 0
def repaint(self):
self.screen.fill((255,255,255))
- self.cell.draw(self.screen, self.font)
+ self.drawcell(self.cell)
pygame.display.flip()
+ def drawcell(self, cell):
+ ypos = self.vscroll
+ yposlist = []
+ for line in cell.lines:
+ yposlist.append(ypos)
+ lineimage = self.font.render(line or ' ', 1, (0,0,0))
+ self.screen.blit(lineimage, (0, ypos))
+ ypos += lineimage.get_size()[1]
+ self.drawcursors(yposlist)
+
+ def drawcursors(self, yposlist):
+ othercursors = self.root.users.__dict__.values()
+ try:
+ othercursors.remove(self.cursor)
+ except ValueError:
+ pass
+ othercursors.append(self.cursor)
+ for cursor in othercursors:
+ x = cursor.x
+ y = cursor.y
+ if y >= len(yposlist):
+ y = len(yposlist)-1
+ line = self.cell.lines[y]
+ xpos = self.font.size(line[:x])[0]
+ ypos = yposlist[y]
+ char = line[x:x+1] or ' '
+ charimage = self.font.render(char, 1, (255,255,255), cursor.color)
+ self.screen.blit(charimage, (xpos, ypos))
+ if ypos < 0:
+ self.screen.fill((255, 255, 255))
+ self.vscroll -= ypos
+ self.drawcell(self.cell)
+ elif ypos + charimage.get_size()[1] > self.screen.get_size()[1]:
+ self.screen.fill((255, 255, 255))
+ self.vscroll -= ypos + charimage.get_size()[1] - self.screen.get_size()[1]
+ self.drawcell(self.cell)
+
+ def K_UP(self):
+ if self.cursor.y > 0:
+ self.cursor.y -= 1
+
+ def K_DOWN(self):
+ if self.cursor.y < len(self.cell.lines)-1:
+ self.cursor.y += 1
+
+ def K_LEFT(self):
+ if self.cursor.x > 0:
+ self.cursor.x -= 1
+
+ def K_RIGHT(self):
+ line = self.cell.lines[self.cursor.y]
+ if self.cursor.x < len(line):
+ self.cursor.x += 1
+
def run(self):
invalid = True
while 1:
@@ -131,11 +199,11 @@
if event.key not in KEYMAP:
continue
keyname = KEYMAP[event.key]
- for receiver in [self.cursor, self.cell, self]:
+ for receiver in [self]:
method = getattr(receiver, keyname, None)
if method:
method()
- self.sendcursorupdate()
+ self.notifychanges(self.cursor)
invalid = True
break
if event.type == QUIT:
@@ -146,18 +214,21 @@
def postrepaintevent(self):
pygame.event.post(pygame.event.Event(REPAINTEVENT))
- def sendcursorupdate(self):
- self.servergateway.exec_remote('''
+ def notifychanges(self, *structures):
+ lines = [representstructure(structure) for structure in structures]
+ lines.append('''if 1:
for c in shared.getclientlist():
if c is not gateway:
- c.exec_remote("terminal.setcursorto(%(x)d, %(y)d)")
-
- ''' % {'x': self.cursor.x, 'y': self.cursor.y})
+ rerepresent = c.ns['representstructure']
+ relines = []
+ for structid in %r:
+ obj = getstructure(structid)
+ relines.append(rerepresent(obj))
+ relines.append("terminal.postrepaintevent()")
+ c.exec_remote('\\n'.join(relines))
- def setcursorto(self, x, y):
- self.cursor.x = x
- self.cursor.y = y
- self.postrepaintevent()
+ ''' % [getstructureid(struct) for struct in structures])
+ self.servergateway.exec_remote('\n'.join(lines))
if __name__ == '__main__':