From 5fb3ea060025241105dc8e9a174513c112f9a133 Mon Sep 17 00:00:00 2001 From: rcoh Date: Thu, 27 Jan 2011 16:50:59 -0500 Subject: A metric $#%$-ton of changes. Added doc-strings to EVERYTHING. Phew. Fixed a massive bug that increases performance in by up to a factor of 60. A bunch of new behaviors for the class. --- inputs/PygameInput.py | 35 ++++++++++++++++++++++------------- inputs/RandomLocs.py | 3 +++ inputs/TCPInput.py | 9 ++++----- inputs/UDPInput.py | 4 ++++ 4 files changed, 33 insertions(+), 18 deletions(-) (limited to 'inputs') diff --git a/inputs/PygameInput.py b/inputs/PygameInput.py index 27b82b0..399a77e 100644 --- a/inputs/PygameInput.py +++ b/inputs/PygameInput.py @@ -6,18 +6,27 @@ from pygame.locals import * #This class processes input from an already running pygame instance and passes #it to the parent. This class requires an already running pygame instance. class PygameInput(Input): + """PygameInput is an input tied to the PygameDisplay. Specify: + True to receive an input every frame specifying the current mouse + position. + True to grab keystrokes + True to grab clicks. + + NB: If follow mouse is enabled, PygameInput will not return mouse and keypresses. You can, however, + instantiate other PygameInputs in the XML that will capture mouse and keypresses.""" def sensingLoop(self): - #try: - if self['FollowMouse']: - self.respond({Strings.LOCATION: pygame.mouse.get_pos()}) - return - for event in pygame.event.get(): - if event.type is KEYDOWN: - if event.key == 27: - self.die() - self.respond({Strings.LOCATION: (5,5),'Key': event.key}) - if event.type is MOUSEBUTTONDOWN: + if self['FollowMouse']: + self.respond({Strings.LOCATION: pygame.mouse.get_pos()}) + return + for event in pygame.event.get(): + if event.type is KEYDOWN: + if event.key == 27: + self.die() + if self['Keyboard']: + self.respond({'Key': event.key}) + return + else: + pygame.event.post(event) + if event.type is MOUSEBUTTONDOWN: + if self['Clicks']: self.respond({Strings.LOCATION: pygame.mouse.get_pos()}) - #except: - #raise Exception('Pygame not initialized. Pygame must be \ - #initialized.') diff --git a/inputs/RandomLocs.py b/inputs/RandomLocs.py index d1ce1c7..2719981 100644 --- a/inputs/RandomLocs.py +++ b/inputs/RandomLocs.py @@ -4,6 +4,9 @@ import util.Geo as Geo import util.Strings as Strings from operationscore.Input import * class RandomLocs(Input): + """RandomLocs is an Input that generates RandomLocations at a preset time interval. Just a + prototype, some assembly required.""" + def inputInit(self): self['LastEvent'] = clock.time() def sensingLoop(self): #TODO: move to params diff --git a/inputs/TCPInput.py b/inputs/TCPInput.py index 2bc69ef..17ea7e6 100644 --- a/inputs/TCPInput.py +++ b/inputs/TCPInput.py @@ -6,6 +6,10 @@ import logging as main_log import string class TCPInput(Input): + """TCPInput is a input to receive input on a TCP port. In its current incarnation, it parses + json data into python dicts. Warning: contains a bug where init will hang until it receives a + connection. Specify: + -- Port number to listen on.""" def inputInit(self): self.HOST = '' # Symbolic name meaning all available interfaces self.PORT = self.argDict['Port'] # Arbitrary non-privileged port @@ -34,12 +38,7 @@ class TCPInput(Input): for datagroup in data.split('\n'): if datagroup != None and datagroup != '': dataDict = json.loads(datagroup) - #print dataDict self.respond(dataDict) - #socketDict = {'data':dataDict, 'address':self.address} - #socketDict = {Strings.LOCATION: (dataDict['x'], dataDict['y'])} # like PygameInput - #print 'input' - #self.respond(socketDict) except Exception as exp: print str(exp) else: diff --git a/inputs/UDPInput.py b/inputs/UDPInput.py index e95bd33..83e2f77 100644 --- a/inputs/UDPInput.py +++ b/inputs/UDPInput.py @@ -1,6 +1,10 @@ from operationscore.Input import * import socket class UDPInput(Input): + """UDPInput is a barebones UDP Input class. It takes any data it receives and adds it to the + 'data' element of the response dict. It also notes the 'address'. Specify: + -- the Port to listen on.""" + def inputInit(self): HOST = '' # Symbolic name meaning all available interfaces PORT = self.argDict['Port'] # Arbitrary non-privileged port -- cgit v1.2.3