def from_dict(cls, dct, map): """Import from a dict compatible with Tiled's JSON plugin""" helpers.assert_item(dct, 'type', 'imagelayer') helpers.assert_item(dct, 'width', map.width) helpers.assert_item(dct, 'height', map.height) helpers.assert_item(dct, 'x', 0) helpers.assert_item(dct, 'y', 0) self = cls( map=map, name=dct.pop('name'), visible=dct.pop('visible', True), opacity=dct.pop('opacity', 1), image=image.open(dct.pop('image')), ) self.properties.update(dct.pop('properties', {})) return self
def from_dict(cls, dct): """Import from a dict compatible with Tiled's JSON plugin""" dct.pop('firstgid', None) html_trans = dct.pop('transparentcolor', None) if html_trans: trans = fileio.from_hexcolor(html_trans) else: trans = None self = cls( name=dct.pop('name'), tile_size=(dct.pop('tilewidth'), dct.pop('tileheight')), image=image.open( dct.pop('image'), size=(dct.pop('imagewidth'), dct.pop('imageheight')), trans=trans, ), margin=dct.pop('margin', 0), spacing=dct.pop('spacing', 0), ) self.properties.update(dct.pop('properties', {})) for number, properties in dct.pop('tileproperties', {}).items(): self[int(number)].properties.update(properties) for number, attrs in dct.pop('tiles', {}).items(): attrs = dict(attrs) probability = attrs.pop('probability', None) if probability is not None: self[int(number)].probability = probability terrain_indices = attrs.pop('terrain', None) if terrain_indices is not None: self[int(number)].terrain_indices = terrain_indices assert not attrs for terrain in dct.pop('terrains', []): terrain = dict(terrain) self.terrains.append_new(terrain.pop('name'), self[int(terrain.pop('tile'))]) assert not terrain tileoffset = dct.pop('tileoffset', None) if tileoffset: self.tile_offset = tileoffset['x'], tileoffset['y'] return self
def from_dict(cls, dct, base_path=None): """Import from a dict compatible with Tiled's JSON plugin""" html_trans = dct.pop('transparentcolor', None) if html_trans: trans = fileio.from_hexcolor(html_trans) else: trans = None self = cls( name=dct.pop('name'), tile_size=(dct.pop('tilewidth'), dct.pop('tileheight')), image=image.open( dct.pop('image'), size=(dct.pop('imagewidth'), dct.pop('imageheight')), trans=trans, ), margin=dct.pop('margin', 0), spacing=dct.pop('spacing', 0), ) if base_path: self.image.base_path = base_path self._fill_from_dict(dct, base_path) return self
def _fill_from_dict(self, dct, base_path): dct.pop('firstgid', None) if base_path: self.base_path = base_path self.properties.update(dct.pop('properties', {})) for number, properties in dct.pop('tileproperties', {}).items(): self[int(number)].properties.update(properties) tile_info = dct.pop('tiles', {}) for number in sorted(tile_info, key=int): attrs = dict(tile_info[number]) number = int(number) probability = attrs.pop('probability', None) if probability is not None: self[number].probability = probability terrain_indices = attrs.pop('terrain', None) if terrain_indices is not None: self[number].terrain_indices = terrain_indices if number > len(tile_info): raise ValueError() while 0 <= len(self) <= number: self._append_placeholder() filename = attrs.pop('image', None) if filename: self[number].image = image.open(filename) if base_path: self[number].image.base_path = base_path if attrs: raise ValueError('Extra tile attributes: %s' % ', '.join(attrs)) for terrain in dct.pop('terrains', []): terrain = dict(terrain) self.terrains.append_new(terrain.pop('name'), self[int(terrain.pop('tile'))]) assert not terrain tileoffset = dct.pop('tileoffset', None) if tileoffset: self.tile_offset = tileoffset['x'], tileoffset['y'] dct.pop('margin', None) dct.pop('spacing', None)