-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtokens.py
More file actions
230 lines (196 loc) · 7.13 KB
/
tokens.py
File metadata and controls
230 lines (196 loc) · 7.13 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from __future__ import unicode_literals
import base64
import hashlib
import io
import os
import uuid
import zipfile
import jinja2
from PIL import Image
from six import iteritems
################################################################################
### Asset class: manage images/etc stored with the token
asset_template = '''
<net.rptools.maptool.model.Asset>
<id>
<id>{{ asset.md5 }}</id>
</id>
<name>{{ asset.name }}</name>
<extension>{{ asset.ext }}</extension>
<image/>
</net.rptools.maptool.model.Asset>
'''.strip()
class Asset(object):
def __init__(self, name, ext, contents, md5=None):
self.contents = contents
self.name = name
self.ext = ext
if md5 is None:
md5 = hashlib.md5(contents).hexdigest()
self.md5 = md5
def asset_xml(self):
return jinja2.Template(asset_template).render(asset=self)
def __repr__(self):
return '<Asset {}.{}>'.format(self.name, self.ext)
################################################################################
### Macro class: container for metada about macros. Basically just a dict.
macro_defaults = {
'command': '',
'label': '',
'group': '',
'sortby': '',
'color': 'default',
'font_color': 'black',
'font_size': '1.00em',
'tooltip': '',
'hotkey': 'None',
}
class Macro(object):
def __init__(self, **kwargs):
for k, v in iteritems(macro_defaults):
setattr(self, k, v)
for k, v in iteritems(kwargs):
setattr(self, k, v)
################################################################################
### Token class
size_map = {
'Fine': 'fwABAc1lFSoBAAAAKgABAQ==',
'Diminutive': 'fwABAc1lFSoCAAAAKgABAQ==',
'Tiny': 'fwABAc5lFSoDAAAAKgABAA==',
'Small': 'fwABAc5lFSoEAAAAKgABAA==',
'Medium': 'fwABAc9lFSoFAAAAKgABAQ==',
'Large': 'fwABAdBlFSoGAAAAKgABAA==',
'Huge': 'fwABAdBlFSoHAAAAKgABAA==',
'Gargantuan': 'fwABAdFlFSoIAAAAKgABAQ==',
'Colossal': 'fwABAeFlFSoJAAAAKgABAQ==',
}
token_defaults = {
'is_visible': 'true',
'name': 'Default Token',
'label': '',
'gm_name': '',
'notes': '',
'gm_notes': '',
'owners': [],
'PC': True,
'layer': 'TOKEN',
'halo_color': -256,
'sight_type': 'Normal',
}
_toggles = ("Other Other2 Other3 Other4 Enlarged Dead Incapacitated "
"BullStrength Prone Hidden Disabled").split()
base_states = {k: ('boolean', 'false') for k in _toggles}
base_states['Health'] = ('big-decimal', '1')
# No need to explicitly store properties that will just be set to the default.
# Here's some you might want to set, though:
# 'HP', 'MaxHP', 'Nonlethal', 'TempHP',
# 'BAB', 'Level',
# 'Armor', 'ACAlways', 'DexDodge',
# 'Description', 'HasInit', 'Elevation',
# 'ACP', 'CMB', 'CMD',
# 'Strength', 'Dexterity', 'Constitution',
# 'Intelligence', 'Wisdom', 'Charisma',
# 'Fortitude', 'Reflex', 'Will',
# 'Perception', 'SenseMotive', 'Initiative',
# 'AbilityPool', 'AbilityPoolMax', 'Pool1Name',
# 'Pool2', 'Pool2Max', 'Pool2Name',
base_properties = {
'HasInit': 1,
'Fortitude': '{ConMod}',
'Reflex': '{DexMod}',
'Will': '{WisMod}',
'HP': '{MaxHP}',
}
base_macros = [
Macro(label='fort', group='saves', command='Fort: [d20+Fortitude]'),
Macro(label='ref', group='saves', command='Reflex: [d20+Reflex]'),
Macro(label='will', group='saves', command='Will: [d20+Will]'),
Macro(label='str', group='skills', sortby='_1', command='Str: [d20+StrMod] (ACP: [ACP])'),
Macro(label='dex', group='skills', sortby='_2', command='Dex: [d20+DexMod] (ACP: [ACP])'),
Macro(label='con', group='skills', sortby='_3', command='Con: [d20+ConMod]'),
Macro(label='int', group='skills', sortby='_4', command='Int: [d20+IntMod]'),
Macro(label='wis', group='skills', sortby='_5', command='Wis: [d20+WisMod]'),
Macro(label='cha', group='skills', sortby='_6', command='Cha: [d20+ChaMod]'),
Macro(label='perception', group='skills', command='Perception: [d20+Perception]'),
Macro(label='sense motive', group='skills', command='Sense Motive: [d20+SenseMotive]'),
Macro(label='mod HP', color='pink', sortby='0', command='''
[h: input(
"mode|Damage,Heal,Temp HP,Nonlethal|Choose|RADIO|ORIENT=H",
"Amt|0|Amount|TEXT"
)]
[h, switch(mode),code:
case 0: {
[HP = HP - Amt + min(TempHP, Amt)]
[TempHP = max(0, TempHP - Amt)]
};
case 1: { [HP = min(HP + Amt, MaxHP)] };
case 2: { [TempHP = TempHP + Amt] };
case 3: { [Nonlethal = Nonlethal + Amt] }]
[s:CurrentHitPoints]
'''.strip()),
]
properties_xml = '''<map>
<entry>
<string>version</string>
<string>1.4.0.0
</string>
</entry>
</map>'''
with open('templates/content.xml') as f:
content_template = jinja2.Template(f.read())
class Token(object):
def __init__(self, image, portrait_image=None, size='Medium',
states=None, properties=None, macros=None,
default_states=True, default_properties=True,
default_macros=True, **kwargs):
assert isinstance(image, Asset)
self.image = image
if portrait_image is not None:
assert isinstance(image, Asset)
self.portrait_image = portrait_image
self.id = base64.encodestring(uuid.uuid4().bytes).strip()
self.size_id = size_map[size]
for k, v in iteritems(token_defaults):
setattr(self, k, v)
for k, v in iteritems(kwargs):
setattr(self, k, v)
self.states = {}
if default_states:
for name, (typ, val) in iteritems(base_states):
self.states[name] = (typ, val)
if states:
for name, (typ, val) in iteritems(states):
self.states[name] = (typ, val)
self.properties = {}
if default_properties:
for name, val in iteritems(base_properties):
self.properties[name] = val
if properties:
for name, val in iteritems(properties):
self.properties[name] = val
self.macros = []
if default_macros:
self.macros.extend(base_macros)
if macros:
self.macros.extend(macros)
def _add_asset(self, f, asset):
f.writestr('assets/{}'.format(asset.md5), asset.asset_xml())
f.writestr('assets/{}.{}'.format(asset.md5, asset.ext), asset.contents)
def make_thumbnail(self):
with io.BytesIO(self.image.contents) as f_in:
image = Image.open(f_in)
image.thumbnail((50, 50))
with io.BytesIO() as f_out:
image.save(f_out, 'png')
return f_out.getvalue()
def content_xml(self):
return content_template.render(t=self)
def make_file(self, file, mode='w', compressed=True):
c = zipfile.ZIP_DEFLATED if compressed else zipfile.ZIP_STORED
with zipfile.ZipFile(file, mode=mode, compression=c) as f:
f.writestr('content.xml', self.content_xml())
f.writestr('properties.xml', properties_xml)
f.writestr('thumbnail', self.make_thumbnail())
self._add_asset(f, self.image)
if self.portrait_image is not None:
self._add_asset(f, self.portrait_image)