| 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 |
import os |
| 37 |
|
| 38 |
from pyglet import media |
| 39 |
|
| 40 |
|
| 41 |
class ReaderException(Exception): |
| 42 |
pass |
| 43 |
|
| 44 |
|
| 45 |
class PlayerReader: |
| 46 |
def __init__(self, player): |
| 47 |
self.player = player |
| 48 |
|
| 49 |
def line(self, line, lineno): |
| 50 |
parts = line.split() |
| 51 |
if parts[0] == 'position': |
| 52 |
if len(parts) < 4: |
| 53 |
raise ReaderException('Invalid position line %d' % lineno) |
| 54 |
self.player.position = tuple([float(x) for x in parts[1:]]) |
| 55 |
if parts[0] == 'cone_orientation': |
| 56 |
if len(parts) < 4: |
| 57 |
raise ReaderException('Invalid orientation line %d' % lineno) |
| 58 |
self.player.cone_orientation = tuple([float(x) for x in parts[1:]]) |
| 59 |
elif parts[0] == 'outer_cone_angle': |
| 60 |
if len(parts) < 2: |
| 61 |
raise ReaderException('Invalid angle line %d' % lineno) |
| 62 |
self.player.cone_outer_angle = float(parts[1]) |
| 63 |
elif parts[0] == 'inner_cone_angle': |
| 64 |
if len(parts) < 2: |
| 65 |
raise ReaderException('Invalid angle line %d' % lineno) |
| 66 |
self.player.cone_inner_angle = float(parts[1]) |
| 67 |
elif parts[0] == 'label': |
| 68 |
if len(parts) < 2: |
| 69 |
raise ReaderException('Invalid label line %d' % lineno) |
| 70 |
self.player.label = parts[1] |
| 71 |
|
| 72 |
|
| 73 |
class SpaceReader: |
| 74 |
def __init__(self, space): |
| 75 |
self.basedir = '' |
| 76 |
self.space = space |
| 77 |
|
| 78 |
def read(self, file): |
| 79 |
if not hasattr(file, 'read'): |
| 80 |
self.basedir = os.path.dirname(file) |
| 81 |
with open(file) as f: |
| 82 |
file = f.readlines() |
| 83 |
elif hasattr(file, 'name'): |
| 84 |
self.basedir = os.path.dirname(file.name) |
| 85 |
reader = None |
| 86 |
player = None |
| 87 |
lineno = 0 |
| 88 |
for line in file: |
| 89 |
lineno += 1 |
| 90 |
|
| 91 |
if not line.strip() or line.startswith('#'): |
| 92 |
continue |
| 93 |
|
| 94 |
if line.startswith(' '): |
| 95 |
if not reader: |
| 96 |
raise ReaderException('Unexpected indented block line %d' % lineno) |
| 97 |
reader.line(line, lineno) |
| 98 |
else: |
| 99 |
reader = None |
| 100 |
if player is not None: |
| 101 |
self.space.add_player(player) |
| 102 |
player = None |
| 103 |
|
| 104 |
parts = line.split() |
| 105 |
if parts[0] == 'loop': |
| 106 |
if len(parts) < 2: |
| 107 |
raise ReaderException('No loop filename line %d' % lineno) |
| 108 |
player = media.Player() |
| 109 |
player.loop = True |
| 110 |
player.queue(self.source(parts[1], streaming=False)) |
| 111 |
reader = PlayerReader(player) |
| 112 |
|
| 113 |
if player is not None: |
| 114 |
self.space.add_player(player) |
| 115 |
|
| 116 |
def source(self, filename, **kwargs): |
| 117 |
filename = os.path.join(self.basedir, filename) |
| 118 |
return media.load(filename, **kwargs) |