annotate pytouhou/formats/hint.py @ 544:b895ed2de71f

Implement text rendering for the SDL backend.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 15 May 2014 02:56:08 +0200
parents 6c0cb3eee33e
children d1f0bb0b7a17
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
400
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 ##
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 ## Copyright (C) 2013 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 ##
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 ##
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 ## GNU General Public License for more details.
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 ##
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 from collections import OrderedDict
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 def _read_n_int(value):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 values = value.split(', ')
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 return tuple(int(value) for value in values)
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 class Stage(list):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 def __init__(self, number):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 list.__init__(self)
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 self.number = number
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 class Hint(object):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 _fields = {'Stage': int,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 'Tips': None,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 'Remain': int,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 'Text': lambda v: v[1:-1],
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 'Pos': _read_n_int,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 'Count': int,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 'Base': str,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 'Align': str,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 'Time': int,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 'Alpha': int,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 'Color': _read_n_int,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 'Scale': float,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 'End': None,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 'StageEnd': None}
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 def __init__(self):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 self.version = 0.0
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 self.stages = []
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 @classmethod
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 def read(cls, file):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 tokens = []
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 for line in file:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 line = line.strip()
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 if not line:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 continue
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 if line[0] == '#':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 continue
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 if line == 'Version = 0.0':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 continue
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 field, _, value = line.partition(':')
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 field = field.rstrip()
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 value = value.lstrip()
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 parser = cls._fields[field]
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 if parser:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 tokens.append((field, parser(value)))
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 else:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 tokens.append((field, None))
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 stage_mode = False
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 tip_mode = False
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 stage = None
404
6c0cb3eee33e Add MoF’s hints support, and fix the Text timeout interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 400
diff changeset
79 tip = None
400
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 hints = cls()
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 stages = hints.stages
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 for token in tokens:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 key = token[0]
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 value = token[1]
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 if stage_mode:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 if key != 'StageEnd':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 if tip_mode:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 if key != 'End':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 tip[key] = value
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 else:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 assert tip_mode == True
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 stage.append(tip)
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 tip_mode = False
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 elif key == 'Tips':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 assert tip_mode == False
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 tip = OrderedDict()
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 tip_mode = True
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 else:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 assert stage_mode == True
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 stages.append(stage)
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 stage_mode = False
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 elif key == 'Stage':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 assert stage_mode == False
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 stage = Stage(value)
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 stage_mode = True
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 return hints
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 def write(self, file):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 file.write('# Hints file generated with PyTouhou\n\n\n')
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115 file.write('Version = {}\n\n'.format(self.version))
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117 for stage in self.stages:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 file.write('# ================================== \n')
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 file.write('Stage : {}\n\n'.format(stage.number))
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 for tip in stage:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122 file.write('Tips\n')
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 for key, value in tip.items():
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125 if key == 'Text':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 value = '"{}"'.format(value)
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 elif key == 'Pos':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 key = 'Pos\t'
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 if isinstance(value, tuple):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130 value = str(value)[1:-1]
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 file.write('\t{}\t: {}\n'.format(key, value))
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 file.write('End\n\n')
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 file.write('StageEnd\n')