aboutsummaryrefslogtreecommitdiff
path: root/inputs
diff options
context:
space:
mode:
authorGravatar rcoh <rcoh@mit.edu>2011-01-27 16:50:59 -0500
committerGravatar rcoh <rcoh@mit.edu>2011-01-27 16:50:59 -0500
commit5fb3ea060025241105dc8e9a174513c112f9a133 (patch)
treee98b1b3eab0b05b0e518b08cbab086d224fd9250 /inputs
parent5d29906fff79bc6e4ba83be7028e1380a0014d21 (diff)
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.
Diffstat (limited to 'inputs')
-rw-r--r--inputs/PygameInput.py35
-rw-r--r--inputs/RandomLocs.py3
-rw-r--r--inputs/TCPInput.py9
-rw-r--r--inputs/UDPInput.py4
4 files changed, 33 insertions, 18 deletions
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:
+ <FollowMouse>True</FollowMouse> to receive an input every frame specifying the current mouse
+ position.
+ <Keyboard>True</Keyboard> to grab keystrokes
+ <Clicks>True</Clicks> 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> -- 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:
+ <Port> -- the Port to listen on."""
+
def inputInit(self):
HOST = '' # Symbolic name meaning all available interfaces
PORT = self.argDict['Port'] # Arbitrary non-privileged port