aboutsummaryrefslogtreecommitdiff
path: root/inputs/PygameInput.py
blob: 8de55f963c24a8018d51f3a40b1d8f719b1c45fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import time
import util.Strings as Strings
from operationscore.Input import *
import pygame
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):
        if 'Scale' in self:
            scale = self['Scale']
        else:
            scale = 1
        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']:
                    try:
                        self.respond({'Key': event.key, 'KeyChar': chr(event.key)})
                    except:
                        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()})
                else:
                    pygame.event.post(event)